After I got confused with the Office Open XML (OOXML) measuring units for a couple of hours, I thought I’d share what I found on it.
I order to avoid floating point calculations and still maintain high precision the format uses some odd measurement units. Don’t think you can get away with inches, centimeters or pixels!
UPDATE: I created a conversion tool for all those units, available here.
UPDATE 2: Just fixed the DNS, so the conversion tool should be available again quite soon 🙂
Twentieths of a point (dxa)
The main unit in OOXML is a twentieth of a point. This is used for specifying page dimensions, margins, tabs, etc.
The international default letter size is ISO 216 A4 (210x297mm ~ 8.3×11.7in) and expressed as this:
// pageSize: with and height in 20th of a point <w:pgSz w:w="11906" w:h="16838"/>
|20th of a Point||Points
As you see it doesn’t really come out even, but even enough. As you can see here, word processes files at 72dpi.
Half-points are used to specify font sizes. A font-size of 12pt equals 24 half points:
// run properties <w:rPr> // size value in half-points <w:sz w:val="24"/> </w:rPr>
Fiftieths of a Percent
This is used for relative measurements in some places. It can for example be used for specifying tables total with, cell with and margins.
<w:tbl> <w:tblPr> <!-- table width in 50th of a percent --> <w:tblW w:w="2500" w:type="pct"/> </w:tblPr> <w:tblGrid/> <w:tr> <w:tc> <w:p> <w:r> <w:t>Hello, World!</w:t> </w:r> </w:p> </w:tc> </w:tr> </w:tbl>
This prints a table that takes 50% of the available with. If you want to specify the with in twentieth of points instead, you have to use specify w:type=”dxa”.
0.5 * 5000 = 2500pct
EMUs (English Metric Unit)
EMUs are used for coordinates in vector-based drawings and embedded pictures. The EMU is a virtual unit to bridge both centimeters and inches. One inch equates to 914400 EMUs and a centimeter is 360000. Actually I found out that the number 914400 is calculated by (the least common multiple of 100 and 254) times 72. As I understand it, this ensures that you can convert forth and back between integer 100th inches, millimeters and pixels with out any floating points. Is that correct, anybody?
Since OOXML is too verbose, the full markup would just confuse even more. Lets say we have a picture that we want to fit into a table cell.
The cell with is 4,25cm which equals to 2410 dxa.
<w:tcW w:w="2410" w:type="dxa"/>
The original picture is 295x413px at 72dpi and is embedded using DrawingML. The target picture size in the document has to be specified twice. Once for the drawing canvas (extent) and a second time for the embedded picture itself.
When you try to draw this in word itself you’ll have a hard time to match the actual cell size. This version is set by manually calculating the target picture size.
<w:drawing> <wp:anchor ...> ... <!-- drawing canvas size --> <wp:extent cx="1530350" cy="2142490"/> ... <a:graphic> <a:graphicData> <pic:pic> ... <!-- shape properties --> <pic:spPr bwMode="auto"> <!-- 2D transform --> <a:xfrm> ... <!-- picture size --> <a:ext cx="1530350" cy="2142490"/> </a:xfrm> ... </pic:spPr> </pic:pic> </a:graphicData> </a:graphic> </wp:anchor> </w:drawing>
|20th of a Point||Points
Even though this is the most logical way to calculate it, it involves floating points. You can simply avoid those by calculating:
2410 * 914400 / 72 / 20 = 1530350
which can be simplified to:
2410 * 635 = 1530350
Since we want to maintain the aspect ratio of the original picture at 295:413 we have to calculate the height for the cell in dxas and for the pictures in EMUs.
The width-to-height-ratio for this picture is 1.4. So the height for the cell is 2410*1.4 = 3374 dxa and 3374*635 = 2142490 emu.
Thanks for reading! I hope this helps.