Skip to content

fix(extraction): preserve the baseline of a fraction numerator's subscript - #3152

Open
dancvv wants to merge 4 commits into
yfedoseev:mainfrom
dancvv:fix/fraction-subscript-bounds
Open

dancvv wants to merge 4 commits into
yfedoseev:mainfrom
dancvv:fix/fraction-subscript-bounds

Conversation

@dancvv

@dancvv dancvv commented Nov 25, 2026

Copy link
Copy Markdown

Closes #3113

What and why

As an active user of the project, I picked up this issue and took the initiative to implement a fix. I used Codex to assist with root cause analysis and improve regression test coverage. The test verifies that the numerator’s subscript stays at the same y-coordinate as its glyph, even when an equals sign appears beside the fraction.

The 3 in the synthetic Q₃ numerator is drawn at y = 705.9 pt. When an equals sign appears to the left, extract_spans previously moved the digit to y = 700.0 pt. The fix checks for a closer base above the digit before snapping it to the lower row.

Tests

  • The in-memory PDF regression failed on main (aad918a) before the fix. With the fix, both extract_spans and extract_chars return y = 705.9 pt. The denominator stays at 691.6 pt.
  • With the patch applied to aad918a, the default Rust suite had 8,826 passes, 0 failures and 268 ignored tests. Formatting, Clippy and the Python binding reproduction also passed.

Regression on real PDFs

Against both main (aad918a) and v0.3.78, the 13 control PDFs (264 pages) had identical extracted text and all 37,299 spans matched. Minimum word Jaccard was 1.0, with zero spacing differences. On page 4 of the reporter’s PDF, all seven adjusted digits matched their character coordinates. The reported numerator moved from 751.78 to 757.68 pt.

AI assistance disclosure

Codex wrote the candidate extraction change and regression tests. The commits include Assisted-by: Codex. Contributor review and DCO sign-off are pending.

@github-actions github-actions Bot added the ai-assisted PR discloses AI co-authorship (informational) label Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@dancvv
dancvv force-pushed the fix/fraction-subscript-bounds branch from 0a39f0a to 45722a3 Compare November 25, 2026 06:23
@dancvv

dancvv commented Nov 25, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 25, 2026
Assisted-by: Codex
Signed-off-by: dancvv <35890032+dancvv@users.noreply.github.com>
An intervening smaller base on a higher row can carry the glyph as its subscript. Keep the drawn position when that base occupies the gap to the glyph, while retaining the existing superscript alignment for ordinary scripts and full-size delimiters.

Assisted-by: Codex
Signed-off-by: dancvv <35890032+dancvv@users.noreply.github.com>
Assisted-by: Codex
Signed-off-by: dancvv <35890032+dancvv@users.noreply.github.com>
Signed-off-by: dancvv <35890032+dancvv@users.noreply.github.com>
@dancvv
dancvv force-pushed the fix/fraction-subscript-bounds branch from fae15ab to 433a011 Compare November 25, 2026 06:35
@dancvv
dancvv marked this pull request as ready for review November 25, 2026 18:18
@dancvv
dancvv requested a review from yfedoseev as a code owner November 25, 2026 18:18
@dancvv

dancvv commented Nov 26, 2026

Copy link
Copy Markdown
Author

Hi @yfedoseev, could you please rerun the failed Java 11 check on Ubuntu? Compilation passed, but Maven couldn't resolve the Surefire dependencies, so the tests never started. Thanks!

@yfedoseev

Copy link
Copy Markdown
Owner

Hi @dancvv, thank you for picking this up! The write-up is clear, the synthetic reproducer is exactly the right shape, and the root-cause work showed us where to look. Contributions like this are always welcome.

We took a deeper look at the problem itself before reading the patch, and found that #3113 is one visible case of a wider defect in how scripts are attached to rows. The guard in this PR fixes the reported layout, but the cause underneath is still there. Below is what we found and the change we think the fix needs. It's more than the PR set out to do, so please read it as a direction we'd like to agree on together, not a list of mistakes.

What's actually going wrong

snap_superscript_baselines treats any run more than 1.15× larger within reach as a script's base. It judges "raised" against that run, and then writes the base's baseline into the span's public bbox.y. Two problems follow:

  1. The wrong base can win. In the issue's case Q is the 3's real base, but it is skipped, because the 3 is lowered relative to it, so the = claims the 3. The same mechanism gets other layouts wrong:
    • a small numerator beside a relation;
    • a subscript of a superscript (e^{x_1}, or a formula in an exponent);
    • a superscript of a superscript (x^{a^b}): both boxes move and still land on different rows;
    • body text right after a tall delimiter or operator;
    • a table cell beside a larger label.
  2. The public box moves even when the base is right. A footnote or affiliation marker is reported at its base's baseline instead of where it is drawn, so extract_spans and extract_chars disagree, which is what [Bug]: extract_spans - a subscript's box is snapped onto the row beside it #3113 shows. ISO 32000-1 places a glyph at its painted position (§9.3.7 text rise, §9.4.4 T_rm), and nothing permits moving it. The other PDF text engines we checked never move a glyph's reported box either: they decide line membership separately.

The snap exists only to keep scripts in their row for reading order. That's a grouping decision, and it shouldn't change coordinates.

What we found in the PR itself

  • The new guard is geometric and works for any script. It's the right kind of rule, but it fires only when the numerator is smaller than the operator and more than 1.15× its script; the issue's case clears that just barely (9 against 8.69). A display-size numerator (the same size as the =), a numerator close in size to its script, a subscript of a superscript set within half an em, and a subscript after a large operator set low all still get snapped.
  • test_full_size_delimiter_in_the_gap_does_not_capture_a_script asserts that a stacked numerator is reported at the row baseline (334.7) rather than where it's drawn (339.1). That's the behaviour [Bug]: extract_spans - a subscript's box is snapped onto the row beside it #3113 reports, so the test would lock it in.
  • Two of the three new tests also pass on main, so they guard against regression but don't show the fix.
  • The loop rewrite keeps behaviour, but it drops the comments explaining why subscripts are never snapped (the subscript-digit substitution relies on it), the kerning slack, and combining diacritics. We'd like those kept.

The change we'd suggest

1. Keep every box where it's drawn, and group rows on a separate key.

  • Add a non-serialised TextSpan field, e.g. snap_shift: f32 (drawn baseline − row baseline, 0 for most spans), and row_y() = bbox.y − snap_shift.
  • snap_superscript_baselines sets only snap_shift and never writes bbox.
  • Grouping code reads row_y(), and geometry code reads bbox:
    • row_y(): simple_sort_spans, sort_spans_by_columns, the row-aware band sort, reorder_same_line_runs, and the line-break y_diff in document.rs assembly.
    • bbox: dedup, merge_adjacent_spans (so a letter superscript no longer glues into its base), the sub/superscript substitution and merge passes, to_chars/words/lines, and markdown/html <sup>/<sub>.

2. Attach a script to its own base, judged by geometry only.

  • A script's base is the nearest run to its left on the same writing axis, overlapping it vertically, whose advance ends within a quarter em of the script's start, with nothing drawn between the two.
  • "Raised" or "lowered" is measured against that base's drawn baseline.
  • Attachment is transitive: a script of a script joins its base's row.
  • Runs stacked over one base, about 0.8 em or more apart (a numerator over a denominator), are a stack and keep their own rows. That threshold is an estimate, so please measure it.
  • Runs on another rotation are left alone.
  • Keep it O(n log n): there's a 50k-span scaling test.

3. Tests.

  • Synthetic PDFs, one BT/Tm/Tj/ET per run as in your reproducer. Place glyphs with Tm, not Ts.
  • Each test asserts the script's span y equals its char y, plus the text. One test per layout above, each failing on main today.
  • Keep nested subscripts and limits under a large operator as guards.
  • The existing snap unit tests assert row_y() for the row and bbox.y for the drawn position.
  • Name tests by defect class.

4. The issue's case in extract_text should read like the same page without the =: "AP =", then "Q3", then "L3". A glyph on AP's baseline shouldn't pull the numerator onto AP's line.

5. One logical change. Reviving the superscript-digit substitution test (currently #[ignore]d, because the snap defeats it) and revisiting the numeric-run relaxation in merge_sub_superscript_spans can be a follow-up PR.

6. Housekeeping.

  • The description says review and DCO sign-off are pending, but the commits carry Signed-off-by. Since the change was AI-written, please confirm you've reviewed every line, per CONTRIBUTING.md.
  • Please format the CHANGELOG entry like its neighbours.

About regression testing

To be transparent: we haven't run a regression on real documents for this yet, either for your patch or for the direction above. Everything here comes from reading the code, the spec and the geometry. We'll run it once there's a version to test, against both main and v0.3.78, on documents that use this code: author/affiliation blocks, chemistry, maths papers with nested scripts and fractions, drop caps, tables with larger headers, and large pages for timing. It may turn up cases that need further adjustment, such as the stack threshold, or places that relied on the moved box, so a few more changes may be needed after it.

How we'd like to go from here

This can mean some back-and-forth: a design-level change like this, followed by a regression run, often takes a few rounds of review. Please pick whichever option suits you:

  1. You'd like to make the changes. Great, and we'll help along the way. Ask anything before you start, and we can agree the approach here first so you don't spend time on something we'd ask you to redo. If it runs past about three rounds of review, we'll take it from there and finish the remaining changes on top of your commits, so your work and credit stay in the history.
  2. You'd rather we take it from here. Also fine. Just say so, and we'll build our changes on top of your commits, keeping your authorship and credit in the CHANGELOG, and run the regression ourselves.

Either way, thank you. This report and the reproducer found a real problem that goes beyond the one case.

@dancvv

dancvv commented Nov 29, 2026

Copy link
Copy Markdown
Author

Thanks for offering to take this over, @yfedoseev. I’m happy to go with option 2.

I tried your approach locally. It fixed the original fraction, but my prototype changed the reading order on two pages: mathtools, p. 15 and chemformula, p. 22. I hope they’re useful as regression tests.

@yfedoseev

Copy link
Copy Markdown
Owner

@dancvv thank you, and thanks for trying the approach. Those two pages are exactly what we need: a fix that changes the reading order there is not right yet.

We'll take it from here. We'll rebuild both layouts, mathtools p. 15 and chemformula p. 22, as small synthetic PDFs in code, since the originals are third-party and stay out of the repository. They become regression tests beside your fraction reproducer, and the fix has to keep all three right. You're credited for the report and the cases in the CHANGELOG.

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

Labels

ai-assisted PR discloses AI co-authorship (informational)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: extract_spans - a subscript's box is snapped onto the row beside it

2 participants