Skip to content

fix(layout): keep lines that fit the page exactly despite float rounding - #3630

Draft
adarshsm wants to merge 1 commit into
diegomura:masterfrom
adarshsm:fix/3587-text-last-line-tolerance
Draft

adarshsm wants to merge 1 commit into
diegomura:masterfrom
adarshsm:fix/3587-text-last-line-tolerance

Conversation

@adarshsm

Copy link
Copy Markdown

Addresses #3587 (re-opening of #3436). I couldn't open the REPL from the issue, so I reproduced the symptom it describes with a hand-built tree instead.

Problem

When the room left on a page is exactly the height of N lines, the text should keep N lines on that page. It gets N-1, and when that falls below orphans the whole text moves to the next page, which leaves the big gap the issue shows. Adding a bit more text pushes it one more line, so the gap goes away.

A text of 12 lines at lineHeight: 13.7 on a page 6 * 13.7 high paginates as [5, 5, 2] lines instead of [6, 6].

Root cause

lineIndexAtHeight (used by splitText) picks the first line whose bottom is strictly greater than the available height. The available height comes from Yoga, which stores it as a float32. The line boxes are float64. Math.fround(6 * 13.7) is a bit smaller than 6 * 13.7, so the 6th line "doesn't fit" by a few millionths of a point.

resolvePagination already knows about this when it decides whether a node must be split at all (SAFETY_THRESHOLD = 0.001 in shouldSplit). The line-level check had no tolerance, so the two disagreed.

Fix

Use the same 0.001 tolerance in lineIndexAtHeight.

Tests

  • lineIndexAtHeight: a line that fits to within float32 rounding is kept.
  • resolvePagination: the 12-line example above paginates as [6, 6].
  • Both fail without the change (5 !== 6, [5, 5, 2]) and pass with it. packages/layout/tests/{steps,text,node} all pass (333 tests). The paginate/* and engineSelection suites can't load in my checkout (@react-pdf/paginate isn't linked), so I didn't run them.
  • The maintainer said [Bug] Text goes into 2nd page leaving big gap #3436 should be fixed by the new pagination engine (feat: new pagination engine and Page layout prop #3502). This PR is for the default engine, which still has the issue. I did not render a PDF.

🤖 Generated with Claude Code

lineIndexAtHeight compared a line's float64 bottom against the page room,
which Yoga stores as float32, with no tolerance. A line that fits exactly
was pushed to the next page, and when that left fewer lines than orphans
the whole text moved, leaving a big gap.

Use the same 0.001 tolerance that pagination uses to decide whether a
node needs splitting.

Refs diegomura#3587

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 18, 2026

Copy link
Copy Markdown

@adarshsm is attempting to deploy a commit to the diegomura Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a70860b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@react-pdf/layout Patch
@react-pdf/renderer Patch
@react-pdf/examples Patch
@react-pdf/math Patch
@react-pdf/mermaid Patch
@react-pdf/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant