Skip to content

Fix wrapped text decorations counting character and word spacing twice - #1806

Merged
blikblum merged 1 commit into
foliojs:masterfrom
youdie006:wrapped-spacing-width
Sep 29, 2026
Merged

blikblum merged 1 commit into
foliojs:masterfrom
youdie006:wrapped-spacing-width

Conversation

@youdie006

@youdie006 youdie006 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix.

The line wrapper's textWidth already includes characterSpacing and wordSpacing for each word (lib/line_wrapper.js, wordWidth() and emitLine). _fragment and boundsOfString then add both again, so on wrapped text the underline, strike, link and goTo rectangles and the bounds width are too wide. The lineBreak: false path measures the text itself and is correct.

doc.text('Hello big world', x, y, { underline: true, ...spacing }) at 12pt:

spacing lineBreak: false width: 400, master width: 400, this PR
none 78.74 78.74 78.74
characterSpacing: 4 134.74 194.74 134.74
wordSpacing: 5 88.74 113.74 88.74

boundsOfString(..., { width: 400 }) returns the same widths. With no spacing set, the output for left, center, right and justify is unchanged.

The fix measures the line in both places, the way the lineBreak: false fallback in _fragment already does. Center alignment still uses the wrapper's textWidth and is not touched here. The new tests in tests/unit/text.spec.js fail on master and pass with the change. prettier --check, lint and npm test (61 files, 526 tests) pass on Node 22; I did not run Node 20 or 24.

Checklist:

  • Unit Tests
  • Documentation N/A
  • Update CHANGELOG.md
  • Ready to be merged

Written with AI assistance (Claude); I have reviewed the change.

The line wrapper's textWidth already includes characterSpacing and
wordSpacing for each word, and _fragment and boundsOfString added both
again, so the underline, strike, link and goTo of wrapped text and the
bounds width were too wide. Measure the line text instead, as the
lineBreak: false path already does.
@blikblum
blikblum merged commit 3806311 into foliojs:master Sep 29, 2026
3 checks passed
blikblum pushed a commit that referenced this pull request Oct 4, 2026
* Include wordSpacing in boundsOfString with lineBreak: false

The single-line branch of boundsOfString used widthOfString, which does
not include word spacing, while _fragment adds it to each word and to
the underline width. Add wordSpacing for each word gap, as the wrapped
branch does since #1806.

* Count word gaps only when wordSpacing is set, with one trim

Address review: avoid the second trim call and the split array.

* Count word gaps without building an array

Whitespace runs are collapsed when wordSpacing is set, so each gap is
one character and a global regex test loop counts them.

---------

Co-authored-by: youdie006 <youdie006@users.noreply.github.com>
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