From f9c734a1854a7699cc2827ee6b061a14e49baba2 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Sun, 8 Nov 2026 06:02:04 +0530 Subject: [PATCH] Look up the space glyph in ToUnicode by string in Font.spaceWidth ToUnicodeMap.charCodeOf expects a string, but spaceWidth passed the code point as a number, so the lookup never matched and the width came from widths[32] or defaultWidth instead of the font's space glyph. With a wrong space width the word-space threshold is too small, and letter-spaced text such as LibreOffice headings comes out as "T R A C K". --- src/core/fonts.js | 7 +++++-- test/pdfs/.gitignore | 1 + test/pdfs/tounicode_space_width.pdf | Bin 0 -> 1261 bytes test/unit/api_spec.js | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/pdfs/tounicode_space_width.pdf diff --git a/src/core/fonts.js b/src/core/fonts.js index 81f06c9b09f15..4b70c775601f4 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -3602,8 +3602,11 @@ class Font { } } // ... via toUnicode map - if (!charcode && this.toUnicode) { - charcode = this.toUnicode.charCodeOf(glyphUnicode); + if (!charcode && this.toUnicode && glyphUnicode !== undefined) { + charcode = + this.toUnicode instanceof IdentityToUnicodeMap + ? this.toUnicode.charCodeOf(glyphUnicode) + : this.toUnicode.charCodeOf(String.fromCodePoint(glyphUnicode)); } // setting it to unicode if negative or undefined if (charcode <= 0) { diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index 65b515800a631..135c13c042493 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -1062,6 +1062,7 @@ !issue18768_text_matrix_spacing.pdf !issue18768_type3_wide_glyph.pdf !issue18768_word_spaces.pdf +!tounicode_space_width.pdf !issue19954_reduced.pdf !issue1936_reduced.pdf !issue22462.pdf diff --git a/test/pdfs/tounicode_space_width.pdf b/test/pdfs/tounicode_space_width.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3564054f7eb452f3b1075f22b23403bb0d5262c8 GIT binary patch literal 1261 zcmZWpOK;*Z5WeSE%%xKIz}gNZDH1{|v}oD3+X_+s zq_Y}5{6auN88Hw2Fs*SIt%X6*9%0k|nEhUvje7;bQ-aqztK=@4syTDg+L8VP;L$hJ6Hrh~6N?o|H^SWXun~0p3ht{dx+*GwMVu21A`T&>L zIIZd%ut!@eOEq3TPnWn~?we+-b~JLF(3h&hKo7F{C3iUe-mNz;@m#w@4}Hi2i$)92 z5Mn_O(Sf0&JrP!#je-&9V;Ie5W5C9KK8w=MV_g;0Dw--XPX;97ZtX?iuLu6vAM$}$ zC7pgkBK7MSlVP4NA`KOh0;f`ny`_ugne*@oxHj0*?j}J~H{?(M^O1bBSS8+qh@U=4 zlcb+O1@F2c_){?Ny5#CiCj<^SeIM-N0lqw6bp7GEZ~D>KS*f?m`p0Ui|0&3DChMBs Wnea%pPFm-&3!Y@r#l`L8Ecy>3>|Kih literal 0 HcmV?d00001 diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 2c8bdbf6491fe..744310124a0a8 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -5370,6 +5370,21 @@ ABCD E`); await loadingTask.destroy(); }); + it("gets text content, using the ToUnicode space width for letter-spaced text", async function () { + const loadingTask = getDocument( + buildGetDocumentParams("tounicode_space_width.pdf") + ); + const pdfDoc = await loadingTask.promise; + const pdfPage = await pdfDoc.getPage(1); + const { items } = await pdfPage.getTextContent({ + disableNormalization: true, + }); + + expect(mergeText(items)).toEqual("TRACK TACK"); + + await loadingTask.destroy(); + }); + it("gets text content, preserving various word spaces", async function () { const loadingTask = getDocument( buildGetDocumentParams("issue18768_word_spaces.pdf")