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 text in a standard font garbling or dropping the characters after a tab, a carriage return or any other character code below `0x10`, which was written into the hex string of the text as a single digit, so the following digits paired up wrongly: a `\r` before a line break showed as `Ð`, and `Tab\there` was read back as `Tab–†W&P`. Fixes #1425, #606
- Reuse the glyph width and kerning tables of a standard font across documents instead of building them again for every document that uses it, which made a short document using two standard fonts more than twice as slow to create
- Fix `doc.table()` parsing a font passed as binary data (e.g. registered with `registerFont(name, uint8Array)`) again for every cell, when it restores the font after measuring and drawing the cell, which made tables with such fonts slower. A font loaded from the same `Uint8Array` is now reused without parsing it again
- Add `lineBreak: 'whitespace'` to `doc.text()`, which breaks a line only at whitespace, a soft hyphen or a zero-width space. The Unicode line breaking rules the wrapper follows also allow a break after a hyphen or a slash, so `design-system` or `https://github.com/foliojs/pdfkit` could be cut at a line end, and a text extractor that joins the lines, such as a résumé parser, reads two words
Expand Down
2 changes: 1 addition & 1 deletion lib/font/afm.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class AFMFont {
for (let i = 0, len = text.length; i < len; i++) {
let char = text.charCodeAt(i);
char = WIN_ANSI_MAP[char] || char;
res.push(char.toString(16));
res.push(char.toString(16).padStart(2, '0'));
}

return res;
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/afm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ describe('AFMFont', () => {
expect(font.getKernPair('A', '.notdef')).toBe(0);
});

test('encodes character codes below 0x10 as two hex digits', () => {
const font = new AFMFont(Helvetica);

expect(font.encodeText('a\tb\r\x01')).toEqual([
'61',
'09',
'62',
'0d',
'01',
]);
});

test('does not mutate generated data while decoding it', () => {
const originalData = JSON.stringify(Helvetica);
const font = new AFMFont(Helvetica);
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ describe('Text', () => {
expect(docData).toContainText({ text: 'simple text' });
});

test('with a tab or carriage return keeps the following characters', () => {
const docData = logData(document);

document.text('a\tb\rc', { lineBreak: false });
document.end();

expect(docData).toContainText({ text: 'a\tb\rc' });
});

test('with destination', () => {
// just check that there is no exception
document.text('simple text', { destination: 'anchor' });
Expand Down