Repository navigation
Stop NominalWordWrap wrapping one character early at exact widths [patch] - #173
Merged
Merged
Conversation
The line length is floor(wrapWidth / glyphWidth) in double. Glyph widths such as 0.7f are not exact in binary, so a width of exactly n glyphs often divides to n - ε and floors to n - 1: "hello world" at 11 × 0.7 wrapped onto two lines. About 45% of exact multiples were one short for every glyph width tried. A relative tolerance of 1e-6 is added before flooring. It absorbs the representation error without rounding up a width that is half a glyph short. The int.MaxValue clamp is unchanged. Fixes #171 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TUnnjk1kZxBU7o2kbNsSaP
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TUnnjk1kZxBU7o2kbNsSaP
|
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 #171
Summary
Before:
NominalWordWrapcomputed its line length asMath.Floor((double)wrapWidth / nominalGlyphWidth). Glyph widths such as0.7fare not exact in binary, so a width of exactlynglyphs often divides ton - εand floors ton - 1. For example,"hello world".NominalWordWrap(11 * 0.7f, 0.7f)returned two lines. About 45% of exact multiples came out one short, whatever glyph width I tried.After: I took the issue's suggested fix and add a relative tolerance before flooring:
Math.Floor(raw + raw * 1e-6). Theint.MaxValueclamp is unchanged, and the result for infinity is still infinity.Tests
NominalWordWrapFitsExactlyNGlyphsInAWidthOfNGlyphsis data-driven over g ∈ {0.1, 0.7, 6.6, 7.2, 13.3}. For n = 1 to 200 it checks that a width ofn * ggives a first line of exactlyncharacters.NominalWordWrapKeepsAWordThatExactlyFillsTheWidthOnOneLineis the issue's"hello world"repro.NominalWordWrapDoesNotRoundUpAWidthHalfAGlyphShortchecks that a width of(n + 0.5) * gstill holdsn, so the tolerance never rounds up a width that is genuinely short.StringExtensions.cschange reverted, all five glyph-width rows and the repro test fail. With it in place, the fullExtensions.Testsuite passes (206/206), including the existing NaN, infinity and clamp tests.Generated by Claude Code