Conversation
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 foliojs#1806.
blikblum
requested changes
Sep 30, 2026
Comment on lines
+187
to
+190
| const wordCount = line.trim() ? line.trim().split(/\s+/).length : 0; | ||
| const lineWidth = | ||
| this.widthOfString(line, options) + | ||
| (options.wordSpacing ?? 0) * Math.max(0, wordCount - 1); |
Member
There was a problem hiding this comment.
Perf can be improved: double trim call, temporary array creation.
Address review: avoid the second trim call and the split array.
Whitespace runs are collapsed when wordSpacing is set, so each gap is one character and a global regex test loop counts them.
Contributor
Author
|
Changed: it trims once, and the gaps are counted with a |
This branch has not been deployed
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.
What kind of change does this PR introduce?
Bug fix, follow-up to #1806.
With
lineBreak: false,boundsOfString()measures each line withwidthOfString(line, options)(lib/mixins/text.js:185-190), which leaves outwordSpacing._fragmentadds it to each word and to the underline width, so the bounds are narrower than what is drawn:lineBreak: falseis the only way to reach this branch, since_initOptionssets a width otherwise. This addswordSpacingfor each gap between words, with the word count computed as in_fragment, like the wrapped branch after #1806. Output without spacing options is unchanged (I comparedboundsOfStringover 256 combinations of text, alignment,lineBreak, width,characterSpacing,horizontalScalingand rotation before and after).The new test follows the #1806 tests in
tests/unit/text.spec.jsand fails on master.prettier --check,lintandnpm test(527 tests) pass on Node 22.Checklist:
Written with AI assistance (Claude); I have reviewed the change.