From 963e4d4808ca08f934ebe16711e3ef264691f772 Mon Sep 17 00:00:00 2001 From: Swastik Tripathi Date: Mon, 23 Nov 2026 08:57:50 +0530 Subject: [PATCH] Don't split a surrogate pair when breaking a long word or adding an ellipsis --- CHANGELOG.md | 1 + lib/line_wrapper.js | 19 ++++++++++++-- tests/unit/line_wrapper.spec.js | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf2b3cd..0cdd4a50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/line_wrapper.js b/lib/line_wrapper.js index d70932f6..0d8bce15 100644 --- a/lib/line_wrapper.js +++ b/lib/line_wrapper.js @@ -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(); @@ -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 @@ -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 diff --git a/tests/unit/line_wrapper.spec.js b/tests/unit/line_wrapper.spec.js index de46695a..866a53d8 100644 --- a/tests/unit/line_wrapper.spec.js +++ b/tests/unit/line_wrapper.spec.js @@ -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); + }, + ); + }); });