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

Round text galley sizes to nearest UI point size #4578

Merged
merged 1 commit into from
May 29, 2024

Conversation

emilk
Copy link
Owner

@emilk emilk commented May 29, 2024

Previously, many labels had non-integer widths. This lead to rounding errors.

This was most notable for the new Area sizing code:

We would run the initial sizing pass, to measure the size of e.g. a tooltip.
Say the tooltip contains text that was 100.123 ui points wide. With a 16pt border, that becomes 116.123, which is stored in the Area state as the width. The next frame, we use that stored size as the wrapping width. With perfect precision, we would then tell the label to wrap to 100.123 pts, which the text would just fit in. However, due to rounding errors we might end up asking it to wrap to 100.122 pts, meaning the last word would now wrap and end up on the next line.

By rounding label sizes to perfect integers, we avoid such rounding errors, and most ui elements will now end up on perfect integer point coordinates (and f32 can precisely express and do arithmetic on all integers < 2^24).

Visually this has very little impact. Some labels move by a pixel here and there, mostly for the better.

@emilk emilk merged commit cc3b362 into master May 29, 2024
37 checks passed
@emilk emilk deleted the emilk/round-galley-sizes branch May 29, 2024 16:23
@emilk emilk changed the title Round text galley sizes to nearest ui point size Round text galley sizes to nearest UI point size Jun 27, 2024
hacknus pushed a commit to hacknus/egui that referenced this pull request Oct 30, 2024
Previously, many labels had non-integer widths. This lead to rounding
errors.

This was most notable for the new `Area` sizing code:

We would run the initial sizing pass, to measure the size of e.g. a
tooltip.
Say the tooltip contains text that was 100.123 ui points wide. With a
16pt border, that becomes 116.123, which is stored in the `Area` state
as the width. The next frame, we use that stored size as the wrapping
width. With perfect precision, we would then tell the label to wrap to
100.123 pts, which the text would _just_ fit in. However, due to
rounding errors we might end up asking it to wrap to 100.12**2** pts,
meaning the last word would now wrap and end up on the next line.

By rounding label sizes to perfect integers, we avoid such rounding
errors, and most ui elements will now end up on perfect integer point
coordinates (and `f32` can precisely express and do arithmetic on all
integers < 2^24).

Visually this has very little impact. Some labels move by a pixel here
and there, mostly for the better.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant