Repository navigation
Fix text shifting after an unused soft hyphen or zero-width space - #397
Merged
Merged
Conversation
GlyphsToItems emits a penalty carrying the U+00AD / U+200B glyph as a potential break. When ToText assembles a line and such a penalty is not the line's break (the "potential but unused hyphen" branch), the glyph count was not advanced past it, so every unused soft hyphen or zero-width space on a line shifted the following span text by one glyph: "abc\u00ADdef ghi\u00ADjkl mno" came back as "…jkl m". Advance past the glyph, and make it invisible and zero-width so nothing is drawn for it.
This was referenced Nov 16, 2026
scttfrdmn
added a commit
to scttfrdmn/inkcap
that referenced
this pull request
Nov 16, 2026
…112) canvas's ToText doesn't advance past the glyph of an unused soft hyphen or zero-width space, so the text after each one shifts by a glyph, which garbled soft-hyphenated paragraphs (#69) and blocks U+200B break points for long tokens (#60). The fix is proposed upstream as tdewolff/canvas#397. Until it merges, inkcap imports github.com/scttfrdmn/canvas: the commit inkcap already pinned (b3beae08c578), plus that fix, with the module path renamed (branch 'inkcap' of the fork). A rename rather than a replace directive, because 'go install …@Version' refuses modules whose go.mod has replace directives. TestSoftHyphens checks that unused soft hyphens leave the text intact and that wrapped text keeps every character. Run against the fork with the fix reverted (via a local replace), it fails. The README notes the fork. Refs #90.
Owner
|
Thank you for the PR! |
scttfrdmn
added a commit
to scttfrdmn/inkcap
that referenced
this pull request
Nov 16, 2026
A long URL or identifier had no break opportunity, so it ran past the right margin, and in a table it overlapped the next column. - breakable inserts zero-width spaces, which canvas treats as invisible optional breaks (working since the fork fix, tdewolff/canvas#397): after / ? & # (not inside ://), at camelCase boundaries, and every 20 characters of an unbroken run. In table cells, whose widths are only known at layout, every token of 12+ (code, links) or 24+ (prose) characters gets them, and cellWidths treats them as word boundaries so columns can shrink. Elsewhere only a token wider than 70% of the measure does. Break points cost Knuth-Plass work on every layout pass, and adding them to every moderately long URL nearly doubled render time; with the gate, the 2000-paragraph benchmark is back to baseline (1.8-2.0s against 1.7-1.9s on main). - Paragraph and table-row splits used to rebuild text from the laid-out spans and add a space at every line end, which would put a stray space into a URL at a zero-width-space break. lineCuts now aligns the lines with the original runs, and splitAt cuts the runs there, keeping faces, links, zero-width spaces and soft hyphens; a soft hyphen that ends the head becomes a visible '-'. joinLines remains only as a fallback. Tests: TestBreakable (the rules, visible text unchanged, the short-token threshold), TestLongTokensStayInMeasure (no overflow in a paragraph or in a three-column table) and TestSplitPreservesText (the original is exactly head + separator + tail at every possible split). Removing the break points fails the overflow test 4 times, and forcing the old rebuild path fails the split test.
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.
GlyphsToItemsturns U+00AD (soft hyphen) and U+200B (zero-width space) into a penalty that carries the glyph, as a potential break. InRichText.ToText, when that penalty is not the line's break (the// potential but unused hyphenbranch), the glyph indexbgisn't advanced past it. Every unused soft hyphen or zero-width space on a line therefore shifts the following span text by one glyph:In a PDF this shows up as words split apart, wide gaps and overlapping glyphs in soft-hyphenated text. It also means you can't use U+200B to give long URLs or identifiers somewhere to break.
Fix: advance
bgpast the penalty's glyph. Keep the glyph in the span so the span text matches the source, but make it zero-advance and swap its glyph ID for the space glyph, so fonts without a U+200B glyph don't draw.notdef. A used soft hyphen still becomes a visible-, as before.Test:
TestRichTextUnusedOptionalBreakscovers both characters withLeftandJustify. It fails on master and passes with the fix.go test .andgo test ./text/...pass.Found while using canvas for inkcap, a Markdown-to-PDF typesetter.