Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Unreleased

- Fix wrapped text with `align: 'center'` sitting left of the middle of the line when `characterSpacing` or `wordSpacing` is set, and with `align: 'right'` running past the right edge by the `wordSpacing` of each gap between words
- Fix `boundsOfString()` with `lineBreak: false` leaving out `wordSpacing`, so its width now matches the rendered text and its underline
- Fix the underline, strike, link and goTo of wrapped text, and `boundsOfString()` with a `width`, counting `characterSpacing` and `wordSpacing` twice, so they no longer run past the text and match the same text drawn with `lineBreak: false`
- Fix the Node build throwing once a bundler inlines it into an application bundle, `Cannot find module '#standard-fonts/Helvetica'` on the first document from an ESM bundle and `Invalid URL` while importing a CommonJS one, because the standard font metrics and the PDF/A ICC profile were resolved relative to pdfkit's own package directory at import time. Both are now resolved on first use, a standard font that cannot be loaded names itself and the fix in its error, and `registerStdFonts` is exported from the Node build as it already was from the browser build, so a bundle can register the fonts it uses from `pdfkit/standard-fonts/<Name>`. The Node build now requires the standard fonts by a path relative to the built file, which replaces the `#standard-fonts/*` import mapping and lets an ESM bundle load them from a copy of `js/standard-fonts` next to it. Fixes #1801
Expand Down
13 changes: 12 additions & 1 deletion lib/mixins/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,22 @@ export default {
switch (align) {
case 'right':
textWidth = this.widthOfString(text.replace(/\s+$/, ''), options);
if (wordSpacing) {
const gaps = text.trim().split(/\s+/).length - 1;
textWidth += (wordSpacing * gaps * horizontalScaling) / 100;
}
x += options.lineWidth - textWidth;
break;

case 'center':
x += options.lineWidth / 2 - options.textWidth / 2;
// the wrapper's textWidth also counts the spacing after the last
// word and the word spacing a second time
textWidth =
options.textWidth -
((characterSpacing + wordSpacing * options.wordCount) *
horizontalScaling) /
100;
x += options.lineWidth / 2 - textWidth / 2;
break;

case 'justify':
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,32 @@ Q
/50 [\d.]+ m\n138\.74\d* [\d.]+ l/,
]);
});

test.each([
['center', { characterSpacing: 4 }, '182.628', '317.372'],
['center', { wordSpacing: 5 }, '205.628', '294.372'],
['right', { wordSpacing: 5 }, '361.256', '450'],
])('%s aligned text with %o', (align, spacing, start, end) => {
const docData = logData(document);

document.text('Hello big world', 50, 50, {
width: 400,
align,
underline: true,
...spacing,
});
document.end();

// the underline spans the drawn text, so it sits in the middle of the
// line or ends at its right edge
const escape = (n) => n.replace('.', '\\.');
expect(docData).toContainChunk([
'stream',
new RegExp(
`${escape(start)}\\d* [\\d.]+ m\n${escape(end)}\\d* [\\d.]+ l`,
),
]);
});
});

describe('text with structure parent links', () => {
Expand Down
Loading