Skip to content

fix: PDF reading and writing fixes from the November audit - #153

Open
Chimi6 wants to merge 7 commits into
mainfrom
fix-pdf
Open

Chimi6 wants to merge 7 commits into
mainfrom
fix-pdf

Conversation

@Chimi6

@Chimi6 Chimi6 commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Fixes from the November 2026 audit (reports 2a-pdf-correctness and 5-performance-memory): PDF reading, inflate memory, and tables in the PDF writer.

Fixes

Audit Fix Test
2a C1 A simple font's codes are always one byte, whatever code space its ToUnicode map states. Adobe writes <0000> <FFFF> there. Adobe's sample PDFs went from losing 40 to 97% of their words to matching pdftotext word for word. tests/fixtures/pdf-text/adobe-codespace.pdf, checked against pdftotext by tests/pdf_text.rs
5 H1 inflate() sizes its slab for the worst ratio only when the limit binds (a ZIP entry's size). With a loose limit (a PDF stream's 2 GB) it starts at 6x the input and doubles. The 1 MB growth floor is gone. deflate::inflate::tests::loose_limit_starts_small (fails on main: a 2.2 MB slab for 138 KB)
2a H1 /ActualText over several glyphs: the glyph that carries the text now reaches to the end of the span, unless the replacement is a tab or other blank. A word gap is measured from the furthest point the line has reached, so a combining mark set back over its letter opens no gap. tests/fixtures/pdf-text/actualtext-ligature.pdf; pdf::text::tests::a_mark_set_back_opens_no_gap
2a H2 A simple font with no /Encoding (or whose /Encoding has no /BaseEncoding) uses the encoding built into its embedded program: the Type 1 clear-text /Encoding array, or the CFF encoding and charset for Type 1C. A new cff::encoding_names reads the latter, with the 391 CFF standard strings. tests/fixtures/pdf-text/builtin-encoding.pdf (hand-built CMSY10 with bullets and arrows, against pdftotext); font::tests::a_type1_program_names_its_codes; cff::tests::a_custom_encoding_names_its_codes
2a H4 The page's line spacing is now the smallest drop that happens twice, not the most common one. In Markdown, a line inside a block that starts a list item starts its own block (a round or square bullet always; a dash or number only when indented or after the end of a sentence). A lone bold character is no longer a heading, and blank blocks make no empty paragraph. pdf::text::tests::paragraph_spacing_parts_short_paragraphs; pdf::markdown::tests::items_inside_a_block_start_their_own; tests/pdf_markdown.rs::bullets_from_a_built_in_encoding_make_a_list
2a H5, H6 PDF writer: a row taller than a page breaks across pages between its lines. When the longest words don't fit side by side, each column gets one character's width plus a share of the rest, and words break by characters. When there are more columns than fit at one character each, the table is set as lines of text with cells between bars. tests/to_pdf.rs::tables_stay_on_the_page (1,500-word cell; 14 columns plus a 120-character word; 40 columns: every line inside the margins, every character present)

Verification

All text outputs were compared against pdftotext, before and after each step, on 445 PDFs: every repo fixture, the audit's repros, its generated LibreOffice/Chrome/Ghostscript files, 54 system PDFs, and the pdf.js corpus.

  • C1: 23 files improve, none get worse.
  • H1:
    • 11 files improve.
    • gs-lig-hoefler.pdf scores lower only because pdftotext's own output on that file is garbled. Ours now reads the ligature words correctly.
    • The Hindi licence went from 2,452/5,111 missing/extra words to 444/260.
  • H2:
    • The pdfTeX repro gets its 3 bullets.
    • vbox-manual.pdf goes from 1,809 missing words to 482. The rest is pre-existing uppercase hyphen joining.
    • Two pdf.js files become exact.
  • H4: the word content of the text output is unchanged everywhere.
    • Markdown diffs were reviewed by hand on every file that changed. Paragraphs, numbered sections, and lists now separate (lo-lists, Chrome's nested list, the Apple licences, xcode-ack, tracemonkey, a form).
    • The remaining oddity: table cells in some forms now read as separate paragraphs instead of one run-on paragraph.
  • Writer: 164 md/html/docx/odt/pages/rtf inputs (repo fixtures plus the audit's) give text identical to main through pdftotext. The only exceptions are the two audit stress files that contain the bug cases. The repros were rendered and checked: nothing is off the page.

Item 2 timing (big.pdf, the audit's 1,390-page, 9.1 MB PDF, to text, median of 5, /usr/bin/time -l)

time peak RSS
before 1.64 s 234 MB
after 0.94 s 23 MB

Text output is byte-identical. The machine was loaded (load average around 28), so absolute times are noisy, but the ratio held on every run. Other inflate callers are unchanged (medians, outputs identical):

  • PNG to BMP: 0.122 s both
  • docx to md: 1.457 vs 1.446 s
  • xlsx to csv: 0.530 vs 0.529 s
  • ods to csv: 0.606 vs 0.600 s
  • TIFF to PNG: 0.523 s both

Checks

  • cargo test --all-targets: all pass.
  • cargo fmt --check: clean.
  • check-formats-doc.sh, cargo doc: OK.
  • cargo clippy --all-targets -D warnings: no findings in changed files. The local toolchain (1.100) reports 15 findings in untouched files (hevc, pages writer, rtf reader, planner) that are also on main.
  • Size (macOS release build; CI's Linux number not measured): main 4,704,848 bytes → 4,721,376 (+16,528 file, __text +12,160). Most of the file growth is page rounding from the writer commit (__text +2.2 KB). The H2 commit (CFF standard strings plus the Type 1/CFF encoding readers) is +7.5 KB of __text. The budget headroom on Linux is about 21 KB, so please check the CI size job.

…ode space

Adobe's tools write <0000> <FFFF> as the ToUnicode code space of one-byte
fonts; strings were read as two-byte codes with no mapping and dropped.
Audit 2a C1.
… binds

A PDF stream passes a 2 GB limit, so every content stream got a zeroed
slab of input x 1032 plus a megabyte (8 MB for a 5 KB page). A loose
limit now starts a few times the input and doubles, and growth has no
megabyte floor. A 1,390-page PDF to text: 1.64 s and 234 MB -> 0.94 s
and 23 MB, text identical. Audit 5 H1.
The span's text sits on its first glyph, which now reaches to the end of
the span's last glyph; a word gap is measured from the furthest a line
has reached, so a combining mark set back over its letter opens none.
Audit 2a H1.
… encoding

pdfTeX's CMSY10 carries neither /Encoding nor ToUnicode; its bullets and
arrows are named only in the embedded program. The Type 1 clear-text
/Encoding array and a Type 1C program's encoding and charset (with the
CFF standard strings) now give the base encoding. Audit 2a H2.
…mmon drop

The line spacing a block joins at is the smallest drop that happens
twice, not the most common one (on a page of short paragraphs that is
the paragraph spacing). A line inside a block that starts a list item
starts a block of its own; a lone bold character is no heading; blank
blocks make no empty paragraph. Audit 2a H4.
A row taller than a page breaks across pages between its lines; a table
whose longest words do not fit side by side gives each column a
character's width and a share of the rest, breaking words by characters;
one with more columns than that fits is set as lines of text, cells
between bars. Audit 2a H5 and H6.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant