Skip to content

Fix text shifting after an unused soft hyphen or zero-width space - #397

Merged
tdewolff merged 1 commit into
tdewolff:masterfrom
scttfrdmn:fix-unused-optional-break
Nov 16, 2026
Merged

tdewolff merged 1 commit into
tdewolff:masterfrom
scttfrdmn:fix-unused-optional-break

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

GlyphsToItems turns U+00AD (soft hyphen) and U+200B (zero-width space) into a penalty that carries the glyph, as a potential break. In RichText.ToText, when that penalty is not the line's break (the // potential but unused hyphen branch), the glyph index bg isn't advanced past it. Every unused soft hyphen or zero-width space on a line therefore shifts the following span text by one glyph:

rt.WriteString("abc­def ghi­jkl mno")
rt.ToText(1000, 0, Left, Top, nil) // spans read "abc­def ghi­jkl m": the last glyphs are lost

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 bg past 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: TestRichTextUnusedOptionalBreaks covers both characters with Left and Justify. It fails on master and passes with the fix. go test . and go test ./text/... pass.

Found while using canvas for inkcap, a Markdown-to-PDF typesetter.

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.
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.
@tdewolff
tdewolff merged commit 9889514 into tdewolff:master Nov 16, 2026
@tdewolff

Copy link
Copy Markdown
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.
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