DXF/DWG 字体格式与标注尺寸计算指南

<span data-lang="zh">DXF/DWG 字体格式与标注尺寸计算指南</span><span data-lang="en">DXF/DWG Font Format & Dimension Size Calculation Guide</span>

DXF/DWG 字体格式与标注尺寸计算指南DXF/DWG Font Format & Dimension Size Calculation Guide

Font & Dimension Guide
深入解析 DXF/DWG 文件中的字体编码方式、MTEXT 内联格式代码,以及如何通过组码精确计算标注文字与箭头的实际显示尺寸。Deep dive into font encoding in DXF/DWG, MTEXT inline format codes, and how to precisely calculate dimension text & arrow display sizes from group codes.
📖

概述Overview

在 DXF/DWG 文件中,字体信息通过 STYLE 表定义,并在 TEXT/MTEXT/DIMENSION 等实体中引用。理解字体格式的关键是把握三组关系:In DXF/DWG files, font information is defined in the STYLE table and referenced by TEXT, MTEXT, DIMENSION entities. Understanding font formats revolves around three relationships:

STYLE → Entity
STYLE 表定义字体文件名(组码 3)、固定高度(组码 40)。TEXT/MTEXT 通过组码 7 引用样式名,DIMENSION 通过组码 3 引用 DIMSTYLE。STYLE table defines font filename (code 3) and fixed height (code 40). TEXT/MTEXT reference style name via code 7; DIMENSION references DIMSTYLE via code 3.
SHX vs TTF
SHX(形字体)文件小、速度快,适合工程标注;TTF(TrueType)字体美观、支持 Unicode,适合标题和说明文字。SHX (shape fonts) are small and fast, ideal for engineering dimensions. TTF (TrueType) fonts are beautiful and Unicode-compatible, ideal for titles and annotations.
DIMSCALE
几乎所有标注尺寸的最终显示值都需要乘以 DIMSCALE(标注全局比例因子)。理解 DIMSCALE 是计算标注字体尺寸的核心。Almost all dimension display values are multiplied by DIMSCALE (global dimension scale factor). Understanding DIMSCALE is the core of dimension size calculation.
📝

STYLE 表字体相关组码STYLE Table Font-Related Group Codes

STYLE 表位于 TABLES 节,定义了全局可用的文字样式。每个样式记录包含字体文件名、固定高度、宽度比例等属性。The STYLE table is in the TABLES section, defining globally available text styles. Each record contains font filename, fixed height, width factor, and other attributes.

组码Code 字段名Field Name 中文说明Description 数据类型Type
2Style Name样式名称(如 “Standard”、”DIM_STYLE”)Style name (e.g. “Standard”, “DIM_STYLE”)String
3Font Filename字体文件名(SHX 或 TTF,如 txt.shx、arial.ttf)Font filename (SHX or TTF, e.g. txt.shx, arial.ttf)String
4Bigfont Filename大字体文件名(中文等双字节文字,如 gbcbig.shx、hztxt.shx)Bigfont filename (CJK etc., e.g. gbcbig.shx, hztxt.shx)String
40Fixed Text Height固定文字高度(0.0=可变高度,由实体自身控制)Fixed text height (0.0 = variable, controlled by entity)Double
41Width Factor宽度比例因子(1.0=标准宽度)Width factor (1.0 = standard)Double
50Oblique Angle倾斜角度(斜体字)Oblique angle (italic)Double
71Text Generation Flags文字生成标志(2=倒置, 4=反向)Text generation flags (2=upside-down, 4=backward)Int16
中文环境常用字体组合:Common font combinations for Chinese environments:
gbenor.shx (国标英文)+(GB English) + gbcbig.shx (国标中文大字体)—— 工程图纸最常用(GB Chinese bigfont) — most common for engineering drawings
txt.shx ++ hztxt.shx —— 另一种常见组合— another common combination
T

TEXT 实体字体相关组码TEXT Entity Font-Related Group Codes

TEXT 是单行文字实体,其字体由引用的 STYLE 决定,文字高度由自身组码 40 或样式固定高度控制。TEXT is a single-line text entity. Its font is determined by the referenced STYLE, and its height is controlled by code 40 or the style’s fixed height.

组码Code 英文名称English Name 中文说明Description 数据类型Type
1Text Value文字内容(主文字值)Text contentString
7Text Style Name文字样式名,关联 STYLE 表Text style name, references STYLE tableString
10Insertion Point插入点(配合 20, 30)Insertion point (with 20, 30)Double
40Text Height文字高度(当 STYLE 固定高度为 0 时生效)Text height (effective when style fixed height = 0)Double
41Width Factor宽度比例因子Width factorDouble
50Rotation Angle旋转角度(度)Rotation angle (degrees)Double
51Oblique Angle倾斜角度(Oblique)Oblique angleDouble
71Text Generation Flags文字生成标志(2=倒置, 4=反向)Text gen flags (2=upside-down, 4=backward)Int16
72Horizontal Justification水平对齐方式H-justificationInt16
73Vertical Justification垂直对齐方式V-justificationInt16
重要规则Important Rule 若 STYLE 的固定高度(组码 40)> 0,TEXT 实体自身的组码 40 将被忽略,强制使用样式固定高度。工程中通常将标注用样式设为 0.0,以便由 DIMSTYLE 控制。If the STYLE’s fixed height (code 40) > 0, the TEXT entity’s own code 40 is ignored, forcing the style fixed height. In practice, dimension styles are usually set to 0.0 to let DIMSTYLE control.
M

MTEXT 实体与格式码MTEXT Entity & Format Codes

MTEXT 是多行文字实体,支持 RTF 风格的内联格式代码,可在同一段文字中切换字体、大小、颜色、上下标等格式。MTEXT is a multiline text entity supporting RTF-style inline format codes, allowing font, size, color, sub/superscript switches within the same text.

组码Code 英文名称English Name 中文说明Description 数据类型Type
1Text Value多行文字内容(含内联格式代码Text content (with inline format codes)String
3Additional Text附加文字(内容超长时的续接)Additional text (continuation)String
7Text Style Name文字样式名,关联 STYLE 表Text style name, references STYLE tableString
10Insertion Point插入点Insertion pointDouble
40Nominal Text Height标称文字高度(基础字号)Nominal / character height (base font size)Double
41Reference Rectangle Width参考矩形宽度(强制换行边界)Reference rectangle width (wrap boundary)Double
42Reference Rectangle Height参考矩形高度Reference rectangle heightDouble
71Attachment Point附着点对齐方式(1=左上, 5=正中 等)Attachment point (1=top-left, 5=middle-center, etc.)Int16
72Drawing Direction文字绘制方向Drawing directionInt16
73Line Spacing Style行间距样式Line spacing styleInt16
🎨

MTEXT 内联格式代码MTEXT Inline Format Codes

MTEXT 内容字符串中支持大量类似 RTF 的控制码。花括号 {} 用于分组,控制格式的作用范围,最多可嵌套 8 层。MTEXT content supports RTF-like control codes. Braces {} group format scope, nestable up to 8 levels.

代码Code 英文名称English Name 中文说明Description 示例Example
\PNew Paragraph换行Line breakLine1\PLine2
\HText Height文字高度,相对写法 \H2x; 或绝对写法 \H2;Text height, relative \H2x; or absolute \H2;\H2x;大字
\WText Width文字宽度,相对 \W0.8x; 或绝对 \W0.8;Text width, relative \W0.8x; or absolute \W0.8;\W0.8x;压缩
\TTracking字符间距,如 \T0.5x;Character spacing, e.g. \T0.5x;\T0.5x;加宽
\FFont Switch字体切换,如 \F宋体; 或 \Farial;Font switch, e.g. \FSimSun; or \Farial;\F宋体;中文
\fFont Switch (braced)花括号内字体切换:{\f宋体;文字}Braced font switch: {\fSimSun;text}{\f宋体;中文}
\CColor Switch颜色切换,\C1;=红, \C3;=绿, \C5;=蓝Color switch, \C1;=red, \C3;=green, \C5;=blue\C1;红色
\AAlignment对齐:\A0;=底, \A1;=中, \A2;=顶Alignment: \A0;=bottom, \A1;=middle, \A2;=top\A1;居中
\SStacking堆叠/分数:\SA^ B;=上标, \SX/Y;=分数Stack: \SA^ B;=superscript, \SX/Y;=fraction\S1#4; = 1/4
\QSlant Angle文字倾斜角度,如 \Q30;Text slant angle, e.g. \Q30;\Q30;倾斜
\L / \lUnderline开始 / 停止下划线Start / stop underline\L下划线\l
\O / \oOverline开始 / 停止上划线Start / stop overline\O上划线\o
\K / \kStrikethrough开始 / 停止删除线Start / stop strikethrough\K删除线\k
\~Non-breaking Space不间断空格Non-breaking spaceA\~B
{}Group Braces花括号分组,控制格式作用范围,最多嵌套 8 层Grouping braces, control format scope, max 8 nesting{\H2x;大字}
\\Escape转义字符,如 \\{ 输出 {Escape, e.g. \\{ outputs {\\{

格式代码示例Format Code Examples

-- MTEXT 原始字符串示例 --
{\f宋体;\H2x;这是大字}{\fArial;\H0.5x;small}{\C1;\L红色下划线文字\l}
\S1#2; = 1/2 分数 \P
E=mc\S2^ ; = E=mc²
解析技巧Parsing Tip 解析 MTEXT 内容时,先拆分花括号分组,再在每个组内解析反斜杠代码。遇到 \F 或 \f 时切换字体,遇到 \H 时切换高度,遇到 } 时恢复上一层格式状态。When parsing MTEXT, first split by brace groups, then parse backslash codes within each group. Switch font on \F/\f, switch height on \H, and restore previous state on }.
⚖️

SHX vs TTF 字体对比SHX vs TTF Font Comparison

DXF/DWG 支持两类字体:AutoCAD 专有的 SHX 形字体和操作系统标准的 TTF TrueType 字体。两者在存储方式、显示效果和工程适用性上有显著差异。DXF/DWG supports two font types: AutoCAD-proprietary SHX shape fonts and OS-standard TTF TrueType fonts. They differ significantly in storage, rendering, and engineering applicability.

特性Feature SHX 字体SHX Font TTF 字体TTF Font
文件格式File format.shx AutoCAD 专用形文件AutoCAD shape file.ttf 操作系统标准字体OS standard font
DXF 存储DXF storageSTYLE 组码 3 记录 .shx 文件名STYLE code 3 stores .shx filenameSTYLE 组码 3 记录 .ttf 文件名STYLE code 3 stores .ttf filename
文件大小File size小,DWG/DXF 占用空间小Small, DWG/DXF footprint is small较大Larger
显示速度Display speedREGEN 和 PAN 速度快Fast REGEN and PAN文字多时较慢,可能有更新时差Slower with many texts, may have update lag
字形精度Glyph quality线型字体,美观度一般但工程可用Stroke fonts, acceptable for engineering轮廓字体,平滑美观Outline fonts, smooth and beautiful
UnicodeUnicode早期大字体可能不完全符合 UnicodeEarly bigfonts may not fully support Unicode完整支持 Unicode 和多语言Full Unicode & multilingual support
字高精确度Height accuracy精确,实际高度与指定高度一致Precise, actual = specified可能与指定字高存在微小差异May have slight deviation
CAD 厚度效果CAD thickness支持 Thickness 产生立体效果Supports Thickness for 3D effect不支持Not supported
跨平台性Cross-platform需随图附带字体文件Must ship font files with drawing依赖目标系统是否安装Depends on target OS installation
适用场景Best for工程标注、正文(速度优先)Dimensions, body text (speed priority)标题、说明(美观优先)Titles, notes (beauty priority)
工程实践建议Engineering Best Practices
正文、标注优先使用 SHX 组合(如 gbenor.shx + gbcbig.shx),兼顾速度和中英文显示Body text and dimensions: prefer SHX combo (e.g. gbenor.shx + gbcbig.shx) for speed and CJK support
需要高美观度或输出可搜索 PDF 时,标题可用 TTFFor high aesthetics or searchable PDF output, use TTF for titles
打开图纸提示缺少 SHX 时,建议替换而非忽略,否则文字可能无法显示When opening a drawing with missing SHX, replace rather than ignore, or text may not display
📐

标注字体尺寸计算Dimension Font Size Calculation

在 DXF/DWG 中,标注文字和箭头的实际显示尺寸由 DIMSTYLE 和 STYLE 共同决定。核心公式涉及 DIMTXT(标注文字高度)、DIMASZ(箭头大小)和 DIMSCALE(全局比例因子)。In DXF/DWG, the actual display size of dimension text and arrows is determined jointly by DIMSTYLE and STYLE. The core formula involves DIMTXT, DIMASZ, and DIMSCALE.

实际标注文字高度 = DIMTXT × DIMSCALEActual Dim Text Height = DIMTXT × DIMSCALE
(前提是文字样式的固定高度 = 0)(provided STYLE fixed height = 0)
实际箭头大小 = DIMASZ × DIMSCALEActual Arrow Size = DIMASZ × DIMSCALE
📋

DIMSTYLE 关键组码DIMSTYLE Key Group Codes

DIMSTYLE 表位于 TABLES 节。注意:HEADER 段中系统变量名(如 $DIMTXT)使用组码 9 + 40 存储值,而 DIMSTYLE 表中使用固定组码编号。DIMSTYLE table is in the TABLES section. Note: HEADER variables (e.g. $DIMTXT) use code 9 + 40, while DIMSTYLE table uses fixed code numbers.

HEADER 段中的标注变量Dimension Variables in HEADER Section

变量名Variable组码Code值类型Value Type中文说明Description
$DIMSCALE40Double标注全局比例因子(几乎所有标注尺寸都乘以此因子)Global dimension scale factor (multiplied to nearly all dimension sizes)
$DIMTXT40Double标注文字高度Dimension text height
$DIMASZ40Double标注箭头大小Dimension arrow size
$DIMCLRD70Int16尺寸线颜色(0=ByBlock, 256=ByLayer)Dimension line color (0=ByBlock, 256=ByLayer)
$DIMCLRT70Int16标注文字颜色Dimension text color
$DIMTFILL70Int16文字背景填充(0=无, 1=画布颜色, 2=指定颜色)Text fill (0=none, 1=canvas color, 2=specified color)

DIMSTYLE 表中的固定组码Fixed Group Codes in DIMSTYLE Table

组码Code系统变量Variable中文说明Description数据类型Type
2name标注样式名称Dimension style nameString
40DIMSCALE标注全局比例因子Global dimension scaleDouble
41DIMASZ标注箭头大小Arrow sizeDouble
42DIMEXO延伸线偏移原点的距离Extension line offset from originDouble
44DIMEXE延伸线超出尺寸线的距离Extension line extension beyond dim lineDouble
140DIMTXT标注文字高度(文字样式高度为 0 时生效)Dimension text height (effective when style height = 0)Double
141DIMCEN圆心标记和中心线图形的大小Center mark / centerline sizeDouble
147DIMGAP尺寸线断裂以放置文字时的间距Gap around dimension textDouble
340dimtxsty_handle指向 STYLE 表记录的硬键句柄(DXF 2000+)Hard handle to STYLE table record (DXF 2000+)Handle
关键注意Key Attention DIMSTYLE 表中 DIMTXSTY(文字样式名)不是以字符串直接存储,而是通过组码 340 的硬键句柄指向 STYLE 表。解析时需要根据该句柄查找 STYLE 表获取样式名。In DIMSTYLE, DIMTXSTY is not stored as a string directly; it is referenced via code 340 hard handle to the STYLE table. You must look up the STYLE table by handle.
🔢

通过 DXF 组码计算标注实际显示尺寸的步骤Steps to Calculate Actual Dimension Display Size from DXF Group Codes

  1. 读取 DIMSTYLE 表Read DIMSTYLE Table
    在 TABLES 节找到目标 DIMSTYLE 记录,读取:In the TABLES section, find the target DIMSTYLE record and read:
    组码 40 = DIMSCALE, 组码 140 = DIMTXT, 组码 41 = DIMASZ
  2. 读取关联的文字样式Read Associated Text Style
    通过 DIMSTYLE 组码 340(句柄)在 STYLE 表中查找对应记录,读取:Look up the corresponding record in the STYLE table via DIMSTYLE code 340 (handle), and read:
    组码 40 = STYLE 固定文字高度
  3. 判断计算路径Determine Calculation Path
    若 STYLE 固定高度 = 0,走路径 A;若 > 0,走路径 B。If STYLE fixed height = 0, use Path A; if > 0, use Path B.
  4. 路径 A:STYLE 高度 = 0Path A: STYLE height = 0
    标注文字实际高度 = DIMTXT × DIMSCALEActual text height = DIMTXT × DIMSCALE
    箭头实际大小 = DIMASZ × DIMSCALEActual arrow size = DIMASZ × DIMSCALE
  5. 路径 B:STYLE 高度 > 0Path B: STYLE height > 0
    标注文字实际高度 = STYLE 固定高度(DIMTXT 被忽略)Actual text height = STYLE fixed height (DIMTXT is ignored)
    箭头实际大小 = DIMASZ × DIMSCALE(箭头仍受 DIMSCALE 影响)Actual arrow size = DIMASZ × DIMSCALE (arrow still affected by DIMSCALE)
  6. 特殊情况:DIMTXT = 0Special case: DIMTXT = 0
    若 STYLE 高度也为 0,使用系统默认值 2.5:If STYLE height is also 0, use system default 2.5:
    实际文字高度 = 2.5 × DIMSCALEActual text height = 2.5 × DIMSCALE
工程最佳实践Engineering Best Practice 将标注所用文字样式的固定高度设为 0.0,让 DIMSTYLE 的 DIMTXT(组码 140)完全控制标注文字高度。这样修改 DIMSCALE 时,文字和箭头会同步缩放,保持比例协调。Set the fixed height of the dimension text style to 0.0, letting DIMSTYLE’s DIMTXT (code 140) fully control dimension text height. This way, changing DIMSCALE scales text and arrows proportionally.
💡

实际案例Practical Examples

案例 1:标准建筑图纸(1:100 出图)Example 1: Standard Architectural Drawing (1:100)

DXF 读取值:DXF values read:
DIMSCALE = 100, DIMTXT = 2.5, DIMASZ = 2.5, STYLE 固定高度 = 0

计算:Calculation:
实际文字高度 = 2.5 × 100 = 250 模型单位model units
实际箭头大小 = 2.5 × 100 = 250 模型单位model units

解释:Explanation: 建筑图纸按 1:100 打印,模型空间中标注文字显示为 250 单位高,打印到图纸上缩小 100 倍后正好为 2.5mm,符合国标要求。For 1:100 plotting, model space text shows at 250 units high; after 100x reduction on paper, it becomes exactly 2.5mm, meeting national standards.

案例 2:机械零件图(1:1 出图)Example 2: Mechanical Part Drawing (1:1)

DXF 读取值:DXF values read:
DIMSCALE = 1, DIMTXT = 3.5, DIMASZ = 3, STYLE 固定高度 = 0

计算:Calculation:
实际文字高度 = 3.5 × 1 = 3.5 模型单位 = 打印后 mmmodel units = mm after printing
实际箭头大小 = 3 × 1 = 3 模型单位 = 打印后 mmmodel units = mm after printing

案例 3:STYLE 固定高度 > 0(不推荐)Example 3: STYLE Fixed Height > 0 (Not Recommended)

DXF 读取值:DXF values read:
DIMSCALE = 50, DIMTXT = 2.5, DIMASZ = 2.5, STYLE 固定高度 = 5

计算:Calculation:
文字高度 = STYLE 固定高度 = Text height = STYLE fixed height = 5 (DIMTXT 被忽略!)(DIMTXT ignored!)
实际箭头大小 = 2.5 × 50 = 125

问题:Problem: 文字高度只有 5,而箭头却变成了 125,比例严重失调!Text height is only 5, but arrows become 125 — severely disproportionate!

案例 4:DIMTXT = 0 的极端情况Example 4: Edge Case with DIMTXT = 0

DXF 读取值:DXF values read:
DIMSCALE = 10, DIMTXT = 0, DIMASZ = 2.5, STYLE 固定高度 = 0

计算:Calculation:
文字高度 = 默认值 2.5 × 10 = Text height = default 2.5 × 10 = 25
实际箭头大小 = 2.5 × 10 = 25

解释:Explanation: 当 DIMTXT 和 STYLE 固定高度都为 0 时,AutoCAD 使用系统默认文字高度 2.5,再乘以 DIMSCALE。When both DIMTXT and STYLE fixed height are 0, AutoCAD uses the system default text height 2.5, then multiplies by DIMSCALE.

DXF/DWG 字体格式与标注尺寸计算指南 | 基于 Autodesk DXF Reference 与 ezdxf 文档编制DXF/DWG Font Format & Dimension Size Calculation Guide | Based on Autodesk DXF Reference & ezdxf docs