fix: restore IoU fallback in _get_effective_y_bounds
Summary
Restores a dead-code fallback in ParagraphFinder._get_effective_y_bounds that was already documented in its docstring but silently short-circuited.
Problem
_get_effective_y_bounds (in babeldoc/format/pdf/document_il/midend/paragraph_finder.py) is documented to:
- use the visual bbox when its IoU with the PDF bbox is
>= 0.5, and - fall back to the PDF bbox otherwise.
But the implementation had an early return visual_box.y, visual_box.y2 before the fallback, so the fallback branch (pdf_box) was unreachable dead code and the visual bbox was always used.
Downstream symptom
On PDFs whose visual_bbox is systematically shorter than the PDF line box (in one Elsevier single-column paper the visual height is ~6pt vs a ~12pt PDF line height, with >52% of characters having visual_bbox↔box IoU < 0.5), the vertical collision histogram in _split_paragraph_into_lines found no gap between adjacent lines, so an entire multi-line paragraph collapsed into a single 43.8pt pseudo-line.
That pseudo-line then made inline math crossing a physical line break (e.g. p > 0.05, δ < 0.04, temperature τ = 0.1, seed s = 123) get grouped into one PdfFormula block whose characters kept their original two-line relative coordinates. After translation the relocate() step re-placed those characters at their original relative offsets, producing scattered / mis-ordered inline formula fragments and dropped decimal points.
Fix
Remove the early return so the intended visual_bbox / pdf_box IoU fallback runs exactly as the docstring describes.
visual_box = char.visual_bbox.box
- return visual_box.y, visual_box.y2
pdf_box = char.box
if calculate_iou_for_boxes(visual_box, pdf_box) >= 0.5:
return visual_box.y, visual_box.y2
return pdf_box.y, pdf_box.y2
This is a restoration of the author's original intent (the fallback branch already existed) rather than new logic.
Verification
- Confirmed on an affected paper (Elsevier CRFS, single-column DejaVuSerif body):
_split_paragraph_into_linesoutput 1 pseudo-line with 43.8pt vertical span for a paragraph that has 5 physical lines. After the fix the ioU<0.5 characters fall back to the PDF box and the collision histogram recovers the inter-line gaps. - No change to normal single-line inline formulas or sub/superscripts (the
>= 0.5branch is unchanged).
Related
- #613 is a different root cause (NewCM font names matching the broad formula-font pattern), not fixed here.
I can add a minimal regression test under tests/ if maintainers prefer, but the fix itself is a one-line deletion.
Summary by cubic
Restores the documented IoU fallback in _get_effective_y_bounds so the PDF bbox is used when it doesn't overlap the visual bbox well.
- The early return forced the visual bbox always, so the fallback branch was unreachable.
- On PDFs with short visual bboxes, adjacent lines merged into a single pseudo-line during collision detection.
- That collapsed paragraph made inline math crossing a line break render as scattered fragments after translation.
- Removing the early return restores the docstring behavior; the
>= 0.5branch is unchanged.