PDF to Word: whole words in PDFs without space glyphs #3

Open
mnovak wants to merge 0 commits from fix/word-spaces into main
Owner

Found while making the Day 2 Word file on the installed app: a real PDF (WPS Writer; no space glyphs, each glyph its own Tj, words split over text objects) came out as "Dichotomou s Image Segmentat ion", "evalu ati on", and "Terms ofUse".

Cause

  • pdfium invents zero-width spaces (font size 1.0) where text objects meet, some of them inside a word; lines() kept them.
  • The gap rule (0.18 em fixed) missed justified word gaps of 0.14 em.

Fix

  • pdfium's whitespace is never copied; a space comes from the gap between two visible letters. A whitespace character only lowers the gap needed (0.05 em instead of 0.1 em).
  • The gap is measured against the line's median letter gap, so narrow justified gaps count and letter-spaced headings are not split into letters.

Tests

  • word.rs unit tests: the exact characters pdfium gave for the real file; real space glyphs (incl. a narrow one); 0.14 em justified gaps; a heading tracked to 0.15 em.
  • tests/word.rs: a PDF with no space glyphs, words split over text objects, Td-moved pieces and the WPS layout (flipped Tm, a Tj per glyph).
  • The real file now converts word for word (checked by hand; not committed, it is not ours).
  • Workspace: 151 passed, 0 failed; clippy 0.

Self-review

  • The median needs 4+ gaps; shorter lines fall back to 0, i.e. the plain 0.1 em rule. A two-letter line with tracking could still be split. Rare, accepted.
  • A threshold lower than before (0.1 em above the line's letter gap instead of 0.18 em) could add a space inside a word with strong positive kerning in a line otherwise set tight. None seen in the tests or the LibreOffice round trip.
  • Glyphs with no Unicode mapping (here the curly quotes) still come out as U+FFFD: pdfium cannot know them, and guessing would be worse.
Found while making the Day 2 Word file on the installed app: a real PDF (WPS Writer; no space glyphs, each glyph its own `Tj`, words split over text objects) came out as "Dichotomou s Image Segmentat ion", "evalu ati on", and "Terms ofUse". ## Cause - pdfium invents zero-width spaces (font size 1.0) where text objects meet, some of them inside a word; `lines()` kept them. - The gap rule (0.18 em fixed) missed justified word gaps of 0.14 em. ## Fix - pdfium's whitespace is never copied; a space comes from the gap between two visible letters. A whitespace character only lowers the gap needed (0.05 em instead of 0.1 em). - The gap is measured against the line's median letter gap, so narrow justified gaps count and letter-spaced headings are not split into letters. ## Tests - `word.rs` unit tests: the exact characters pdfium gave for the real file; real space glyphs (incl. a narrow one); 0.14 em justified gaps; a heading tracked to 0.15 em. - `tests/word.rs`: a PDF with no space glyphs, words split over text objects, Td-moved pieces and the WPS layout (flipped Tm, a Tj per glyph). - The real file now converts word for word (checked by hand; not committed, it is not ours). - Workspace: 151 passed, 0 failed; clippy 0. ## Self-review - The median needs 4+ gaps; shorter lines fall back to 0, i.e. the plain 0.1 em rule. A two-letter line with tracking could still be split. Rare, accepted. - A threshold lower than before (0.1 em above the line's letter gap instead of 0.18 em) could add a space inside a word with strong positive kerning in a line otherwise set tight. None seen in the tests or the LibreOffice round trip. - Glyphs with no Unicode mapping (here the curly quotes) still come out as U+FFFD: pdfium cannot know them, and guessing would be worse.
mnovak added 1 commit 2026-11-17 03:08:53 +01:00
This branch is already included in the target branch. There is nothing to merge.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin fix/word-spaces:fix/word-spaces
git checkout fix/word-spaces
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mnovak/Pictor-public#3