Skip to content
Open
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 `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
- Fix `doc.list()` drawing the label of a `numbered` or `lettered` item with the line wrapper's own options object, which threw `unsupported number: NaN` for `align` `center` and `right`, applied a word spacing derived from the previous line to the label for `align` `justify`, and sized the underline, strike, link and goTo of every label after the first from the previous item's text
Expand Down
8 changes: 7 additions & 1 deletion lib/mixins/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ export default {
} else {
// render paragraphs as single lines
for (let line of string.split('\n')) {
const lineWidth = this.widthOfString(line, options);
let lineWidth = this.widthOfString(line, options);
if (options.wordSpacing) {
// whitespace runs were collapsed above, so each gap is one character
const trimmed = line.trim();
const space = /\s/g;
while (space.test(trimmed)) lineWidth += options.wordSpacing;
}
this.y += lineHeight;
contentWidth = Math.max(contentWidth, lineWidth);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,27 @@ Q
new RegExp(`50 [\\d.]+ m\n${end.replace('.', '\\.')}\\d* [\\d.]+ l`),
]);
});

test('bounds of text with lineBreak false and wordSpacing', () => {
const docData = logData(document);
const options = { lineBreak: false, underline: true, wordSpacing: 5 };
const bounds = document.boundsOfString(
'Hello big world',
50,
50,
options,
);

document.text('Hello big world', 50, 50, options);
document.end();

// as wide as the underline, which includes the word spacing
expect(bounds.width.toFixed(2)).toBe('88.74');
expect(docData).toContainChunk([
'stream',
/50 [\d.]+ m\n138\.74\d* [\d.]+ l/,
]);
});
});

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