Fix edge cases in glyph rasterization#67
Merged
Merged
Conversation
This was referenced May 4, 2026
This was referenced May 4, 2026
underoot
approved these changes
May 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes two bugs in glyph rasterization, both rooted in misinterpreting
TextMetrics:Fix #63: Inner EDT misses outside-ink seeds in the buffer region
The inner Euclidean distance transform was restricted to
[buffer, buffer, glyphWidth, glyphHeight], which excluded the surrounding buffer pixels as seeds. When ink touches the bbox edge (common —Math.ceil(actualBoundingBoxAscent)puts the topmost ink row exactly at the bbox top), the nearest "outside" pixel sits one row outside the bbox and is invisible to the EDT, collapsing the inner distance and producing a hard cutoff (see #63's video).Fix: pad the inner EDT region by 1 px on each side. The buffer region is uniformly seeded as outside-ink, so the closest external seed for any interior pixel is always exactly one pixel outside the bbox — 1 px of padding is provably sufficient. Padding is clamped to
min(buffer, 1)to stay in-bounds whenbuffer = 0.Fix #65:
actualBoundingBoxLeftsign conventionPer the HTML spec,
actualBoundingBoxLeftis positive when ink extends to the left of the origin. The old code treated it as positive=right, so glyphs whose ink sits to the right of origin (typical for letters like "P") were placed at the wrong position within the SDF texture, with extra phantom padding on one side.Fix: negate when computing
glyphLeft = Math.floor(-actualBoundingBoxLeft). This preservesglyphLeft's existing public semantics (canvas-x of ink left edge, positive=right), so all the downstream formulas (glyphWidth,fillTextplacement) keep working unchanged.Downstream impact
The standalone SDF output changes — glyphs are now correctly placed within their texture without phantom padding. Consumers that position quads via
glyphLeft(e.g. mapbox-gl-js'smetrics.leftinsymbol/quads.ts) render pixel-identically: the old bug was self-cancelling because the wrong ink offset within the texture was compensated by the equally wrongglyphLeftvalue used for placement. Glyph atlas entries become slightly smaller for glyphs with non-zeroactualBoundingBoxLeft(memory win).Tests
材fixtures regenerated — old fixtures encoded the buggy ink placement.does not return negative-width glyphsmock updated: the original metrics described a legitimate (if unusual) glyph under the corrected sign convention; flipping ABBLeft's sign restores the test's pathological-input intent.Closes #63, closes #65. Supersedes #64 and #66.