DXF/DWG 字体格式与标注尺寸计算指南DXF/DWG Font Format & Dimension Size Calculation Guide
概述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 表字体相关组码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 |
|---|---|---|---|
| 2 | Style Name | 样式名称(如 “Standard”、”DIM_STYLE”)Style name (e.g. “Standard”, “DIM_STYLE”) | String |
| 3 | Font Filename | 字体文件名(SHX 或 TTF,如 txt.shx、arial.ttf)Font filename (SHX or TTF, e.g. txt.shx, arial.ttf) | String |
| 4 | Bigfont Filename | 大字体文件名(中文等双字节文字,如 gbcbig.shx、hztxt.shx)Bigfont filename (CJK etc., e.g. gbcbig.shx, hztxt.shx) | String |
| 40 | Fixed Text Height | 固定文字高度(0.0=可变高度,由实体自身控制)Fixed text height (0.0 = variable, controlled by entity) | Double |
| 41 | Width Factor | 宽度比例因子(1.0=标准宽度)Width factor (1.0 = standard) | Double |
| 50 | Oblique Angle | 倾斜角度(斜体字)Oblique angle (italic) | Double |
| 71 | Text Generation Flags | 文字生成标志(2=倒置, 4=反向)Text generation flags (2=upside-down, 4=backward) | Int16 |
gbenor.shx (国标英文)+(GB English) + gbcbig.shx (国标中文大字体)—— 工程图纸最常用(GB Chinese bigfont) — most common for engineering drawingstxt.shx ++ hztxt.shx —— 另一种常见组合— another common combination
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 |
|---|---|---|---|
| 1 | Text Value | 文字内容(主文字值)Text content | String |
| 7 | Text Style Name | 文字样式名,关联 STYLE 表Text style name, references STYLE table | String |
| 10 | Insertion Point | 插入点(配合 20, 30)Insertion point (with 20, 30) | Double |
| 40 | Text Height | 文字高度(当 STYLE 固定高度为 0 时生效)Text height (effective when style fixed height = 0) | Double |
| 41 | Width Factor | 宽度比例因子Width factor | Double |
| 50 | Rotation Angle | 旋转角度(度)Rotation angle (degrees) | Double |
| 51 | Oblique Angle | 倾斜角度(Oblique)Oblique angle | Double |
| 71 | Text Generation Flags | 文字生成标志(2=倒置, 4=反向)Text gen flags (2=upside-down, 4=backward) | Int16 |
| 72 | Horizontal Justification | 水平对齐方式H-justification | Int16 |
| 73 | Vertical Justification | 垂直对齐方式V-justification | Int16 |
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 |
|---|---|---|---|
| 1 | Text Value | 多行文字内容(含内联格式代码)Text content (with inline format codes) | String |
| 3 | Additional Text | 附加文字(内容超长时的续接)Additional text (continuation) | String |
| 7 | Text Style Name | 文字样式名,关联 STYLE 表Text style name, references STYLE table | String |
| 10 | Insertion Point | 插入点Insertion point | Double |
| 40 | Nominal Text Height | 标称文字高度(基础字号)Nominal / character height (base font size) | Double |
| 41 | Reference Rectangle Width | 参考矩形宽度(强制换行边界)Reference rectangle width (wrap boundary) | Double |
| 42 | Reference Rectangle Height | 参考矩形高度Reference rectangle height | Double |
| 71 | Attachment Point | 附着点对齐方式(1=左上, 5=正中 等)Attachment point (1=top-left, 5=middle-center, etc.) | Int16 |
| 72 | Drawing Direction | 文字绘制方向Drawing direction | Int16 |
| 73 | Line Spacing Style | 行间距样式Line spacing style | Int16 |
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 |
|---|---|---|---|
\P | New Paragraph | 换行Line break | Line1\PLine2 |
\H | Text Height | 文字高度,相对写法 \H2x; 或绝对写法 \H2;Text height, relative \H2x; or absolute \H2; | \H2x;大字 |
\W | Text Width | 文字宽度,相对 \W0.8x; 或绝对 \W0.8;Text width, relative \W0.8x; or absolute \W0.8; | \W0.8x;压缩 |
\T | Tracking | 字符间距,如 \T0.5x;Character spacing, e.g. \T0.5x; | \T0.5x;加宽 |
\F | Font Switch | 字体切换,如 \F宋体; 或 \Farial;Font switch, e.g. \FSimSun; or \Farial; | \F宋体;中文 |
\f | Font Switch (braced) | 花括号内字体切换:{\f宋体;文字}Braced font switch: {\fSimSun;text} | {\f宋体;中文} |
\C | Color Switch | 颜色切换,\C1;=红, \C3;=绿, \C5;=蓝Color switch, \C1;=red, \C3;=green, \C5;=blue | \C1;红色 |
\A | Alignment | 对齐:\A0;=底, \A1;=中, \A2;=顶Alignment: \A0;=bottom, \A1;=middle, \A2;=top | \A1;居中 |
\S | Stacking | 堆叠/分数:\SA^ B;=上标, \SX/Y;=分数Stack: \SA^ B;=superscript, \SX/Y;=fraction | \S1#4; = 1/4 |
\Q | Slant Angle | 文字倾斜角度,如 \Q30;Text slant angle, e.g. \Q30; | \Q30;倾斜 |
\L / \l | Underline | 开始 / 停止下划线Start / stop underline | \L下划线\l |
\O / \o | Overline | 开始 / 停止上划线Start / stop overline | \O上划线\o |
\K / \k | Strikethrough | 开始 / 停止删除线Start / stop strikethrough | \K删除线\k |
\~ | Non-breaking Space | 不间断空格Non-breaking space | A\~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²
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 storage | STYLE 组码 3 记录 .shx 文件名STYLE code 3 stores .shx filename | STYLE 组码 3 记录 .ttf 文件名STYLE code 3 stores .ttf filename |
| 文件大小File size | 小,DWG/DXF 占用空间小Small, DWG/DXF footprint is small | 较大Larger |
| 显示速度Display speed | REGEN 和 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) |
– 正文、标注优先使用 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.
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 |
|---|---|---|---|
$DIMSCALE | 40 | Double | 标注全局比例因子(几乎所有标注尺寸都乘以此因子)Global dimension scale factor (multiplied to nearly all dimension sizes) |
$DIMTXT | 40 | Double | 标注文字高度Dimension text height |
$DIMASZ | 40 | Double | 标注箭头大小Dimension arrow size |
$DIMCLRD | 70 | Int16 | 尺寸线颜色(0=ByBlock, 256=ByLayer)Dimension line color (0=ByBlock, 256=ByLayer) |
$DIMCLRT | 70 | Int16 | 标注文字颜色Dimension text color |
$DIMTFILL | 70 | Int16 | 文字背景填充(0=无, 1=画布颜色, 2=指定颜色)Text fill (0=none, 1=canvas color, 2=specified color) |
DIMSTYLE 表中的固定组码Fixed Group Codes in DIMSTYLE Table
| 组码Code | 系统变量Variable | 中文说明Description | 数据类型Type |
|---|---|---|---|
| 2 | name | 标注样式名称Dimension style name | String |
| 40 | DIMSCALE | 标注全局比例因子Global dimension scale | Double |
| 41 | DIMASZ | 标注箭头大小Arrow size | Double |
| 42 | DIMEXO | 延伸线偏移原点的距离Extension line offset from origin | Double |
| 44 | DIMEXE | 延伸线超出尺寸线的距离Extension line extension beyond dim line | Double |
| 140 | DIMTXT | 标注文字高度(文字样式高度为 0 时生效)Dimension text height (effective when style height = 0) | Double |
| 141 | DIMCEN | 圆心标记和中心线图形的大小Center mark / centerline size | Double |
| 147 | DIMGAP | 尺寸线断裂以放置文字时的间距Gap around dimension text | Double |
| 340 | dimtxsty_handle | 指向 STYLE 表记录的硬键句柄(DXF 2000+)Hard handle to STYLE table record (DXF 2000+) | Handle |
通过 DXF 组码计算标注实际显示尺寸的步骤Steps to Calculate Actual Dimension Display Size from DXF Group Codes
-
读取 DIMSTYLE 表Read DIMSTYLE Table
在 TABLES 节找到目标 DIMSTYLE 记录,读取:In the TABLES section, find the target DIMSTYLE record and read:
组码 40= DIMSCALE,组码 140= DIMTXT,组码 41= DIMASZ -
读取关联的文字样式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 固定文字高度 -
判断计算路径Determine Calculation Path
若 STYLE 固定高度 = 0,走路径 A;若 > 0,走路径 B。If STYLE fixed height = 0, use Path A; if > 0, use Path B. -
路径 A:STYLE 高度 = 0Path A: STYLE height = 0
标注文字实际高度 = DIMTXT × DIMSCALEActual text height = DIMTXT × DIMSCALE
箭头实际大小 = DIMASZ × DIMSCALEActual arrow size = DIMASZ × DIMSCALE -
路径 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) -
特殊情况: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
实际案例Practical Examples
案例 1:标准建筑图纸(1:100 出图)Example 1: Standard Architectural Drawing (1:100)
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)
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)
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
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
