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 a word too long for the line being split between the two halves of a character outside the BMP (an emoji or a math letter such as 𝕆), which drew both halves as broken glyphs. The same goes for the text cut to make room for an ellipsis
- Add an `onPageAdded` document option, a function called with the document each time a page is added, including the automatic first page that a `pageAdded` listener attached after construction would miss. Fixes #1529
- Fix smooth curves (`S`, `T`) in SVG paths reflecting the control point of an arc or of a curve of the other kind. As the SVG spec says, they now start from the current point there. An arc with a zero radius is now drawn as a straight line instead of nothing

Expand Down
19 changes: 17 additions & 2 deletions lib/line_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ const isWhitespaceBreak = (text, bk) =>
bk.position >= text.length ||
WHITESPACE_BREAK.test(text[bk.position - 1]);

// whether position i is between the two halves of a surrogate pair
const splitsSurrogatePair = (text, i) =>
i > 0 &&
i < text.length &&
/[\uD800-\uDBFF]/.test(text[i - 1]) &&
/[\uDC00-\uDFFF]/.test(text[i]);

class LineWrapper extends EventEmitter {
constructor(document, options) {
super();
Expand Down Expand Up @@ -191,9 +198,14 @@ class LineWrapper extends EventEmitter {
}
}

// don't split a surrogate pair (a character outside the BMP)
if (l > 0 && l < word.length && splitsSurrogatePair(word, l)) {
w = this.wordWidth(word.slice(0, --l));
}

// check for the edge case where a single character cannot fit into a line.
if (l === 0 && this.spaceLeft === this.lineWidth) {
l = 1;
l = splitsSurrogatePair(word, 1) ? 2 : 1;
}

// send a required break unless this is the last piece and a linebreak is not specified
Expand Down Expand Up @@ -298,7 +310,10 @@ class LineWrapper extends EventEmitter {
// remove characters from the buffer until the ellipsis fits
// to avoid infinite loop need to stop while-loop if buffer is empty string
while (buffer && textWidth > this.lineWidth) {
buffer = buffer.slice(0, -1).replace(/\s+$/, '');
const end = splitsSurrogatePair(buffer, buffer.length - 1)
? -2
: -1;
buffer = buffer.slice(0, end).replace(/\s+$/, '');
textWidth = this.wordWidth(buffer + this.ellipsis);
}
// need to add ellipsis only if there is enough space for it
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/line_wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,48 @@ describe('LineWrapper', () => {
});
});
});
describe('characters outside the BMP', () => {
// 𝕆 and the other double-struck letters are surrogate pairs in a JS string
const word = 'π”Έπ”Ήβ„‚π”»π”Όπ”½π”Ύβ„π•€π•π•‚π•ƒπ•„β„•π•†β„™β„šβ„π•Šπ•‹π•Œπ•π•Žπ•π•β„€'.repeat(3);
const splitsPair = (line) =>
/^[\uDC00-\uDFFF]/.test(line) || /[\uD800-\uDBFF]$/.test(line);

const wrapLines = (text, options) => {
// needs real glyphs: the standard fonts give these no width, and with
// .notdef half a pair is as wide as the whole one
document.font('examples/fonts/DejaVuSans.ttf');
const lines = [];
const wrapper = new LineWrapper(document, options);
wrapper.on('line', (line) => {
lines.push(line);
document.y += 10;
});
wrapper.wrap(text, options);
return lines;
};

test.each([17, 23, 31, 47, 60, 75])(
'does not split a surrogate pair when it breaks a long word (width %i)',
(width) => {
const lines = wrapLines(word, { width });
expect(lines.length).toBeGreaterThan(1);
expect(lines.filter(splitsPair)).toEqual([]);
expect(lines.join('')).toBe(word);
},
);

test.each([20, 37, 55])(
'does not leave half a surrogate pair before the ellipsis (width %i)',
(width) => {
const lines = wrapLines(`${word} ${word}`, {
width,
height: 10,
ellipsis: true,
});
expect(lines).toHaveLength(1);
expect(lines[0].endsWith('…')).toBe(true);
expect(splitsPair(lines[0].slice(0, -1))).toBe(false);
},
);
});
});
Loading