fix(layout): compare rendered gutters, and order a balanced page by column

NA
3mo ago
+2583-50

Fixes #3966
Fixes #3967

Those two are the pre-existing pair, now filed separately so they are reviewable on their own terms.
Related but not fixed here: #3964, which is normalizeColumnLayout and resolveColumnLayout
disagreeing about which gutter survives a dropped column — a different function and a different
trigger from #3966.

Six defects in the column-layout and column-balancing path. The first two are older than the RTL work and reachable in plain LTR documents; the rest came out of review on this PR and on #3953. A commit each.

A seventh used to be here: the getColumnAtX RTL boundary fix has moved down into #3953 (ad9bde6), where the bound it corrects is introduced, so that #3953 and #3961 no longer merge carrying a line already known to be wrong. The commit that carried it here also held an unrelated columnRenderLayoutsEqual change; that stayed, renamed to describe what it actually does.

[!IMPORTANT]
Depends on #3953 and cannot merge before it. Not merely for convenience: the balancing sort
this replaces was last touched there, and the geometry it reads carries direction and
contentWidth, which #3953 introduces.

The nine commits above fix(contracts): resolve an RTL column boundary the way geometry places content are mine.
In order: compare the gutters that render, order a balanced page by column, stop an unreachable scalar gap from splitting an explicit region, decide which column owns a fragment from more than its origin, record the flow column on an anchored table fragment, stop the gap comment contradicting the sub-pixel guard, name the fragment kinds that actually record a column, trust a fragment's origin by its width, not by its edges, and fall a hole in the per-column gaps back to the scalar. Everything below them belongs to #3953 and disappears once it
merges.

This is stacked directly on #3953, not on #3961. The two share no files, so #3961's footnote
band work is deliberately not in this diff. Either can merge first once #3953 is in.

CONTRIBUTING.md asks for main, and its branch does not exist here to target, so the base is
main and the stack shows up in the diff. The first commit is independent of the RTL work and
could be split out against main on its own if you would rather take it that way.

1. Per-column gaps were invisible to render equality

columnRenderLayoutsEqual skipped per-column gaps, with a note saying they were ignored "until geometry/separators consume them (step 4)". Step 4 has landed: buildColumnGeometry reads gaps[i] ?? gap for both the column x and the separator x, so a gaps-only delta moves every column after the first.

While they were skipped, two sections differing only in their per-column gaps compared render-equal. Nothing split the region and nothing invalidated the normalized-columns cache, so the later section was laid out with the earlier one's gutters, and the painter drew the whole page's separators from them.

Why not just compare the authored arrays. They do not describe what renders, so that trades the bug for its mirror image. resolveColumnLayout emits gaps only when the author supplied them and pads a short array with 0, while geometry falls back to the scalar gap and floors at 0:

ABrendersauthored arrays
gap: 48, no gapsgaps: [48, 48]identicaldiffer → spurious split
gaps: [48, 0]gaps: [48, -10]identicaldiffer → spurious split
gaps: [20]gaps: [20, 0]differequal → missed

A spurious split is not a harmless cache miss. isColumnConfigChanging → forceMidPageRegion resets state.columnIndex to 0, so content after the break restarts in the first column instead of continuing where it was, and an extra columnRegions entry is emitted — which is what bounds the painter's separator spans.

So the comparison derives the effective gutters the way normalizeColumnLayout does — gaps[i] positionally when present, the scalar gap otherwise, each floored at 0 — which keeps the predicate exactly as discriminating as the geometry it stands in for.

Two tests named after the temporary state ("...before geometry uses gaps", "...when only later per-column gaps differ") asserted the old behaviour and now assert the new one. Both fail if the comparison is removed.

2. Column balancing reconstructed document order from raw x

When a multi-column section ends mid-page, balanceSectionOnPage redistributes its fragments and writes the balanced x and y back onto them in the order it derived. That order was a sort on raw x, on the premise that every fragment in a column shares one origin — so getting it wrong reorders the page rather than merely laying it out oddly.

The premise does not hold:

  • a negative w:ind shifts a paragraph's origin (layout-paragraph.ts adds negativeLeftIndent),
  • a float remeasure shifts it again (offsetX), as does floatAlignment: right|center,
  • resolveTableFrame right-aligns or centres an over-wide table, moving its origin outside the column entirely — and end is the default justification for any bidiVisual table.

A difference of 1e-7 was enough to swap two paragraphs.

Sorting by the column each fragment occupies fixes it, and the ordinal is fill order, so it needs no RTL special case — an x comparison needed one because column 0 sits on the right there and document order descends in x.

The ordinal always resolves to a number. A sort key that is sometimes absent leaves the comparator mixing two metrics, which is not a total order; Array.prototype.sort may then return different orders for the same input, and it does differ between engines — so the product and this repo's own suite would not agree on the same page. Three steps, in order:

  1. the column the engine recorded, where it kept one. Tables carry columnIndex; ordinary paragraph fragments do not, so the remaining steps carry most fragments.
  2. otherwise the column containing the origin. This stays correct for content that merely overflows its column, which an overlap comparison gets wrong once the columns are unequal enough for the spill to cover more than the column it came from.
  3. otherwise the column the box covers most, for an origin hung into a gutter — falling back to the clamping hit-test walk when the box touches no column at all.

Ties resolve on y, then on arrival order, so the sort is total and stable.

Tests

contracts 540 · layout-engine (bun) 970 · layout-bridge 1895 · painters/dom 1552. tsc -b tsconfig.references.json exits 0, vp fmt --check clean.

One pre-existing failure, unrelated and present on a clean base: painters/dom persistent-page-surface "1,003 pages", a scale test that exceeds its budget under worker contention and passes in isolation.

Every new and changed assertion was mutation-checked. Removing the gutter comparison fails three tests across two packages; ignoring the recorded columnIndex, forcing the ordinal path off, reversing the ordinal order, dropping the margin conversion, and dropping the y tie-break each fail a test named for that behaviour.

New coverage: authored-vs-effective gutter pairs in both directions, an over-wide table whose origin lands in the neighbouring column, a shuffled input array (the paginator's array order is not a contract), and a left margin wide enough to be mistaken for a column offset.

Not included

Two findings from the same review are left out to keep this reviewable, both in files this branch does not touch:

  • resolveColumnLayout re-pairs gaps after dropping unusable widths while normalizeColumnLayout slices positionally, so {count: 3, equalWidth: false, widths: [0, 200, 150], gaps: [10, 40]} yields [10] vs [40]. Direction-independent, and it also feeds page.columns.
  • columnOwningSpan in the DOM painter attributes by greatest overlap. That drops an over-wide table at its own column's origin when the box reaches the strip width, and names the wrong column when the widths are genuinely unequal.