fix(extractor): size text by its vertical scale so stretched justified lines are not headings
Problem
Justified text from InDesign is often painted with horizontal glyph scaling in the text matrix, for example Tm [14.98 0 0 14 ...] or [12.88 0 0 14 ...] for 14 pt text. effective_font_size took the larger of the two axis scales, so a 14 pt line drawn at 107% width got font_size 14.98.
Those odd sizes occur on only a few lines of a document, so calculate_font_stats gives them a rarity of 0.97 or more. In detect_header_level the size ratio (1.07) is under the 1.2 threshold, but the rarity fallback accepts a line that is not in a paragraph, has at most 8 words and has rarity >= 0.97. Each emitted heading resets in_paragraph, so the next wrapped line qualifies too, and runs of 4-5 consecutive body lines come out as ##.
Fix
effective_font_size uses the vertical scale (the length of the matrix's y vector). Horizontal scaling stretches glyphs sideways and does not change the em height. The horizontal scale is used only if the matrix has no vertical extent. Rotated and mirrored matrices have equal axes and are unchanged. Tz is handled separately (#510) and is not touched.
run_geometry assumed the em was the larger matrix scale, so it now divides by the y-vector length instead. Without this, stretched text would get a bounding box about half its real height ([2 0 0 1] at 12 pt gave 6 pt). All callers pass effective_font_size, so none double-compensates.
On the Saudi Personal Data Protection Law PDF (64 pages) the output goes from 24 to 14 ## lines (384 to 370 lines of stdout, header included). The 45 fixture PDFs in tests/fixtures give identical pdf2md output before and after.
Tests
effective_font_size_ignores_horizontal_stretch(unit)horizontally_stretched_body_lines_are_not_headings: 40 body lines at 14 pt with stretchedTmmatrices stay plain text, while a real 20 pt heading still becomes a heading. It fails without the fix.run_geometry_takes_the_em_height_from_the_y_axis,scaled_run_geometry_keeps_stretched_height_under_horizontal_scaleandhorizontally_stretched_text_keeps_its_full_height: bounding boxes of stretched, rotated andTz-scaled text keep their full height.
Related to firecrawl/firecrawl#5931, but not part of fixing it.
Checks not run
cargo clippy -- -D warningsfails on main with 3nonminimal_boolerrors (src/extractor/clip_boundaries.rs:78twice,src/markdown/convert.rs:1543). Nothing is reported in the changed code.- The pdf-evals
bench.py test/scoreruns were not run (the repo was not available). Built and tested with Rust 1.92.0 instead of the pinned 1.98.0.
Summary by cubic
Fixes font size calculation for horizontally stretched text so justified body lines with odd matrix scales no longer get misread as headings.
effective_font_sizenow sizes text by the matrix's vertical scale (the em height); a 14 pt line drawn at 107% width stays 14 pt instead of reading as 14.98 pt.- Learned heading detection was accepting those rare odd sizes, turning runs of short body lines into
##headings; on the Saudi Personal Data Protection Law PDF false headings drop from 24 to 14. run_geometrytakes its height from the y axis too, so stretched text keeps a full-height bounding box instead of a halved one.- The horizontal scale is used only when the y vector is exactly zero; tiny nonzero vertical scales still win, and rotated and mirrored matrices are unchanged.