PolyMeilex / sctk-adwaita

Adwaita-like SCTK Frame
MIT License
26 stars 19 forks source link

Improve ab-glyph rendering use outline px_bounds to inform pixmap size #65

Closed alexheretic closed 2 months ago

alexheretic commented 2 months ago

I saw there were some recent issues with oob on the pixmap when rendering using _abglyph.

The root cause is the use of "layout" positions (font.height(), h_advance etc) to inform the actual rendering pixmap size. The sizes for laying out glyphs are not always the same as the bounds required for actually rendering the glyphs. For example, a glyph at x=0 may have an outline/render that actually includes some pixels to the left of that layout origin.

ab-glyph provides OutlinedGlyph::px_bounds which accounts for this and will always have enough space to render each pixel returned by OutlinedGlyph::draw correctly. Continuing the example where the glyph (at x=0) outline reaches out to the left of the origin, the px_bounds.min.x of that glyph would be negative.

So outlining the glyphs and using px_bounds gives the correct pixmap size. It is then important to convert the px_bounds into "pixmap space" when calculating the pixel index, since px_bounds.min.x may be non-zero. I added comments to explain this in the code.

After all that we should end up with smaller pixmaps than before and p_idx should never go oob. (The pixmaps could be even smaller actually, I padding the vertical size with the full "ascent" of the font just to make positioning of the pixmap consistent e.g. so "Tg" & "gg" would have the same height pixmap).

I replaced the log with a debug_assert, since it "should never happen" but if a bug is still there or is introduced we'd probably prefer release code to still not panic.

Please feel free to ping me directly if you have problems/questions about _abglyph code in future.

PolyMeilex commented 2 months ago

That's awesome, thanks!

Please feel free to ping me directly if you have problems/questions about ab_glyph code in future.

Cool, I appreciate that, I never took the time to learn ab-glyph properly, so that will be helpful for sure.