Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix glyph clipping at the bottom #33

Merged
merged 1 commit into from
Feb 23, 2021
Merged

Fix glyph clipping at the bottom #33

merged 1 commit into from
Feb 23, 2021

Conversation

mourner
Copy link
Member

@mourner mourner commented Feb 22, 2021

Looks like the code that clips by bbox doesn't round the metrics values correctly, leading to glyph SDFs being clipped by 1px at the bottom. This is especially noticeable on lower font sizes. Example from the demo with 24px:

Before:
image

After:
image

The corresponding code for glyph width doesn't affect rendering so I left it as is. And the offset change is just something that looked like a leftover unless I'm missing something.

@mourner mourner requested a review from ChrisLoer February 22, 2021 15:00
Copy link
Contributor

@ChrisLoer ChrisLoer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

index.js Show resolved Hide resolved
@@ -55,7 +55,7 @@ export default class TinySDF {

// If the glyph overflows the canvas size, it will be clipped at the bottom/right
const glyphWidth = Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxRight - actualBoundingBoxLeft));
const glyphHeight = Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxAscent + actualBoundingBoxDescent));
const glyphHeight = Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxAscent) + Math.ceil(actualBoundingBoxDescent));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned that the same logic for glyphWidth "doesn't affect rendering": I think that's just because the fonts we're using typically have an actualBoundingBoxLeft that's slightly negative? So even if the resulting glyphWidth is one pixel too small, the glyph doesn't look clipped from the right because it's actually eating slightly into the buffer on the left.

Right now we're counting on that as a common property of fonts, since we hardwire the x drawing position to this.buffer, and hardwire the left metric to 0. If we're worried, it would be straightforward to hook it up to the actual metrics the way we do for height.

@mourner mourner merged commit 88d5b01 into main Feb 23, 2021
@mourner mourner deleted the fix-bottom-clipping branch April 16, 2021 15:12
@mourner mourner mentioned this pull request Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants