Skip to content

Center and right align wrapped text with character and word spacing - #1811

Merged
blikblum merged 1 commit into
foliojs:masterfrom
youdie006:align-spacing
Oct 6, 2026
Merged

blikblum merged 1 commit into
foliojs:masterfrom
youdie006:align-spacing

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix, follow-up to #1806, which left center alignment alone.

On wrapped text, align: 'center' used the wrapper's textWidth, which counts the spacing after the last word and the word spacing a second time, so the line sat left of the middle. align: 'right' measured the line without wordSpacing, so it ran past the right edge by wordSpacing for each gap. Margins of doc.text('Hello big world', 50, 50, { width: 400, align, ...spacing }), read from the glyph boxes pdftotext -bbox reports:

align, spacing master (left / right) this PR
center, characterSpacing: 4 130.63 / 134.63 132.63 / 132.63
center, wordSpacing: 5 148.13 / 163.13 155.63 / 155.63
right, wordSpacing: 5 321.26 / -10.00 311.26 / 0.00

With no spacing set the output is unchanged. Alignment with horizontalScaling is off on master even without spacing (right-aligned text at 120% ends 80pt past the edge) and is not addressed here. The new tests fail on master and pass with the change. prettier --check, lint and npm test (61 files, 530 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.

Center alignment used the wrapper's textWidth, which counts the spacing
after the last word and the word spacing twice, so the line sat left of
the middle. Right alignment measured the line without wordSpacing, so
it ran past the right edge by wordSpacing for each gap.
@blikblum
blikblum merged commit 1ce8ed0 into foliojs:master Oct 6, 2026
3 checks passed
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