说明
在开发「前端导出word」需求时(Docx工具问题排查记录),遇到一些单位转换的问题,在查阅了一些规范后解决了,特此记录一下。
Office Open XML
它也称为OpenXML或OOXML,是一种基于XML的办公文档格式,包括Word、Excel、PowerPoint以及Chart、Diagram、Shape等。标准规范定义在了ecma-376中。
所以这里的标题「word中的“单位”」指的就是OOXML测量单位,这个单位是Twip(twentieths of a point),翻译过来就是“二十分之一的点”,简称“缇”,在OpenXML中将这个单位用dxa这个缩写来表示(TODO 具体为什么用这个等我找找来源)。
常用单位
in英寸
全称为inch
来自百度百科:英寸是使用于联合王国(UK,即英国(英联邦)及其以前的殖民地)的长度单位,英文简写是in。 1英寸等于2.54厘米,12英寸为1英尺,36英寸为1码。
所以先屏幕的对角线有多少厘米,然后除以2.54就是屏幕大小(英寸)。
1inch = 2.54cm
pt点(磅)
全称为point,中文意思为“点”,也叫磅。是一个物理长度单位,指的是72分之一英寸。
1inch = 72pt 丨 1pt = 1/72inch
px(像素)
全称为pixel像素,是一个虚拟长度单位,如果px要换算成物理长度,需要指定DPI,所以会随着DPI的变化而变化。
常用的 px 字体大小使用96dpi(TODO 找资料)
当图片尺寸以像素为单位时,我们需要指定其固定的分辨率,才能将图片尺寸与现实中的实际尺寸相互转换。例如大多数网页制作常用图片分辨率为72,即每英寸像素为72,1英寸等于2.54厘米,那么通过换算可以得出每厘米等于28像素;又如15x15厘米长度的图片,等于420*420像素的长度。
1px = 1/DPI * 72 * 20 个twip
1px = 1/96 * 72 * 20 = 15Twip(dxa)
dpi(每英寸像素数)
全称为Dots Per Inch,是一个量度单位,用于点阵数码影像,指每英寸上,所能印刷的网点数。和PPI有区别。
DPI是指每英寸的像素,也就是扫描精度。DPI越低,扫描的清晰度越低,由于受网络传输速度的影响,web上使用的图片都是72dpi,但是冲洗照片不能使用这个参数,必须是300dpi或者更高350dpi。例如要冲洗4*6英寸的照片,扫描精度必须是300dpi,那么文件尺寸应该是(*300)*(6*300)=1200像素*1800像素。
dxa(缇)
和前面提到的Twip(twentieths of a point)是一个单位,只是叫法不一样。在Docx工具问题排查记录中提到过如何获取一个word文件的xml源码,在源码中可以搜索到很多这个单位w:type="dxa" />。
比如A4纸张尺寸标准是210mm × 297mm,先换算到inch(除以25.4mm),再换算到pt(乘以72),最后换算到dxa/Twip(乘以20):
宽 = 210/25.4 * 72 * 20 ≈ 11906
高 = 297/25.4 * 72 * 20 ≈ 16838
验证一下,新建一个word文档,设置纸张大小为A4,另存为选择xml,用浏览器或者文本编辑器打开xml,搜索<w:pgSz,可以看到和我们计算的结果一致:
<w:pgSz w:w="11906" w:h="16838"/>word point
word的文字大小用到的值并不是pt,而是half point,磅的一半,比如<w:sz w:val="24"/>表示的12pt大小。
字号大小尺寸关系:
| 字号 | word point | pixel (96dpi) | 毫米 |
|---|---|---|---|
| 初号 | 42pt | 56px | 14.82mm |
| 小初 | 36pt | 48px | 12.70mm |
| 一号 | 26pt | 34.7px | 9.17mm |
| 小一 | 24pt | 32px | 8.47mm |
| 二号 | 22pt | 29.3px | 7.76mm |
| 小二 | 18pt | 24px | 6.35mm |
| 三号 | 16pt | 21.3px | 5.64mm |
| 小三 | 15pt | 20px | 5.29mm |
| 四号 | 14pt | 18.7px | 4.94mm |
| 小四 | 12pt | 16px | 4.23mm |
| 五号 | 10.5pt | 14px | 3.70mm |
| 小五 | 9pt | 12px | 3.18mm |
| 六号 | 7.5pt | 10px | 2.56mm |
| 小六 | 6.5pt | 8.7px | 2.29mm |
| 七号 | 5.5pt | 7.3px | 1.94mm |
| 八号 | 5pt | 6.7px | 1.76mm |
官方的表格宽度类型及说明(来自Ecma Office Open XML Part 1的17.18.90部分):
| Enumeration Value | Description |
|---|---|
| auto (Automatically Determined Width) | Specifies that the value for the measurement of the current table width property in the parent table shall be automatically determined by the table layout algorithm when the table is displayed (this width can be adjusted as appropriate).If this value is inappropriate for the current measurement (i.e. this measurement is not affected by that algorithm), then this width type and the associated value can be ignored. |
| dxa (Width in Twentieths of a Point) | Specifies that the value for the measurement of the current table width property in the parent table shall be interpreted as twentieths of a point (1/1440 of an inch). |
| nil (No Width) | Specifies that the current width is zero, regardless of any width value specified on the parent element. |
| pct (Width in Percent of Table Width) | Specifies that the value for the measurement of the current table width property in the parent table shall be interpreted as whole percentage point when a percent sign (U+0025) is present. These percentages shall be calculated relative to the extents specified by the parent XML element. If this value is inappropriate for the current measurement (i.e. this measurement is not part of the width of the table), then this width type and the associated value can be ignored. |
其他
其他还有些读者可以自行了解:Fiftieths of a Percent、EMUs(English Metric Unit)、Degree