Improve ab-glyph rendering use outline px_bounds to inform pixmap size #65
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I saw there were some recent issues with oob on the pixmap when rendering using ab_glyph.
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 byOutlinedGlyph::draw
correctly. Continuing the example where the glyph (at x=0) outline reaches out to the left of the origin, thepx_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 ab_glyph code in future.