linebender / resvg

An SVG rendering library.
Mozilla Public License 2.0
2.79k stars 225 forks source link

Simplify transform logic for glyphs #797

Closed LaurenzV closed 2 months ago

LaurenzV commented 2 months ago

This PR adds a method to get the transform of a glyph independent of the actual table that will be used to render the glyph. This allows user to perform their own logic for glyph selection (which is the case for me when rendering glyphs via PDF). The specific glyph transform methods use this new method as the base.

RazrFalcon commented 2 months ago

So you have changed the code you wrote before? Because I do not recognize it. Can you explain why we had multiple transform methods before and now just one is ok?

LaurenzV commented 2 months ago

Yes, that's the code I wrote before. We still have multiple transform methods but the difference is that I extracted the transforms common to all methods (i.e. span transform + cluster transform + glyph transform, you need to apply those regardless of whether you draw an outline or a bitmap glyph) into a separate method, and then each specific transform method calls those and applies additional transforms on top of that.

The reason I didn't do this the first time is that I was unable to disentangle the ts.pre_scale(1.0, -1.0) transform for outline methods from the other parts, as I got a test failure. But yesterday, after looking it again, I realized that this was because in the layout method, there was a transform that was applied wrongly (namely let ts = Transform::from_translate(x + glyph.dx as f32, glyph.dy as f32);, since in SVG coordinates and glyph coordinates the y axis is flipped, this should be -glyph.dy. And after making this change, I was able to disentangle it with all test cases still passing.

RazrFalcon commented 2 months ago

Got it. Thanks!