Search code examples
xps

XPS text position -- what am I not understanding?


I have multiple examples, this is just the first one. I've been extracting some information from this file (I really wish there were a better way!) and something changed that broke my parser. Here's an example of the problem:

The first line displays the date over at the right margin, the second displays "Room 1" in a slightly larger, bolded font against the left margin farther down the page. Note, though, that the first line has a Y location about 250 units below the second. There's obviously something about the location I'm not understanding.

(I only need to be able to properly Y-sort the items. While this one is in the right order I have found them out of order in the past.)

<Glyphs RenderTransform="0.0403884,0,0,0.0403119,0,0" Fill="#ff000000" FontUri="/Documents/1/Resources/Fonts/6C15166D-E658-4B97-A6C0-E217017F767F.odttf" FontRenderingEmSize="327.68" StyleSimulations="None" OriginX="14079.4" OriginY="2270.24" Indices="26,60;17,61;3,59;48,63;68,60;85,60;70,60;75,61;3,62;21,60;19,60;21,61;21" UnicodeString="7. March 2022" />
<Glyphs RenderTransform="0.0646215,0,0,0.0644991,0,0" Fill="#ff000000" FontUri="/Documents/1/Resources/Fonts/FF418D14-C0F5-49FA-8F94-42C185369528.odttf" FontRenderingEmSize="327.68" StyleSimulations="None" OriginX="836.8" OriginY="1929.92" Indices="53,59;82,60;82,59;80,62;3,58;20" UnicodeString="Room 1" />

Solution

  • The reason you are seeing this unintuitive behavior can be determined by a careful (if painful) reading of the XPS standard. You are comparing the OriginY values of two sets of glyphs. Per the spec, section 12.1, OriginY is specified in the effective coordinate space. A bit further down, it mentions that RenderTransform establishes a new coordinate frame for the glyph run, and in so doing, it affects the origin x and origin y of the glyphs.

    To determine the actual coordinates of the glyphs on the page, you need to apply the render transform to the OriginX and OriginY. An explanation of how the RenderTransform works can be found in section 14.4.1

    For this specific example, it is possible to recover the actual coordinates of the glyphs by multiplying the OriginX by the first value in the RenderTransform, and OriginY by the fourth value in the RenderTransform. This will not hold true in all cases as the fifth and sixth values may specify additional x and y offsets, and the second and third values can introduce skew and rotation effects as well.