Skip to content

Look up the space glyph in ToUnicode by string in Font.spaceWidth - #22474

Open
jayhemnani9910 wants to merge 1 commit into
mozilla:masterfrom
jayhemnani9910:fix-spacewidth-tounicode-lookup
Open

jayhemnani9910 wants to merge 1 commit into
mozilla:masterfrom
jayhemnani9910:fix-spacewidth-tounicode-lookup

Conversation

@jayhemnani9910

Copy link
Copy Markdown

Font.spaceWidth looks up the code of the space glyph with this.toUnicode.charCodeOf(glyphUnicode), where glyphUnicode is a number. ToUnicodeMap stores strings, so the lookup never matches and the width comes from widths[32] or defaultWidth instead of the font's own space glyph. encodeString in the same file already converts with String.fromCodePoint (and passes the number through for IdentityToUnicodeMap); this does the same.

Since the word-space detection now uses spaceWidth, the wrong value shows up in text content. A heading exported from LibreOffice with letter-spacing: 2pt (simple font, space mapped through Differences and ToUnicode) extracts as T R A C K E D H E A D I N G T E X T; pdftotext gives TRACKED HEADING TEXT.

Added tounicode_space_width.pdf (hand-written, Helvetica with /Differences [1 /T /R /A /C /K /space], ToUnicode, TJ with -165 between glyphs) and a unit test in api_spec.js. It fails before the change (T R A C K T A C K) and passes after. gulp lint on the changed files passes; the unit tests pass except the 20 specs that need linked PDFs, which could not be downloaded here.

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".
@jayhemnani9910
jayhemnani9910 force-pushed the fix-spacewidth-tounicode-lookup branch from 214f8ae to f9c734a Compare November 8, 2026 18:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants