Skip to content

fix(layout): move rowSpan groups only where all pages have the same margin - #1117

Merged
DemchaAV merged 3 commits into
developfrom
fix/rowspan-move-uniform-margins
Nov 30, 2026
Merged

DemchaAV merged 3 commits into
developfrom
fix/rowspan-move-uniform-margins

Conversation

@DemchaAV

@DemchaAV DemchaAV commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Why

Since #1105 a table moves a group of rows held together by rowSpan cells to the next page instead of cutting inside it, when the group fits an empty page. It measures the group against the page it is on. With per-page margins the next page, or a later one, can be shorter, so some tables that 2.5.3 laid out now throw AtomicNodeTooLargeException. Both examples use 400×400 pages with 20pt margins and a 200pt spacer, so page 1 has 160pt left for a 184.35pt group (rows of 124.55pt and 59.80pt):

  • The group meets a page that cannot hold it. Pages 2 and later hold 100pt. 2.5.3 cut after the group's first row. develop moves the group to page 2 and throws, 184.35 on 100.
  • The group fits, but a later row does not. Page 2 holds 200pt, and pages 3 and later hold 100pt. A 124.55pt row follows the group. 2.5.3 cut the group, and its second row and the later row both fitted page 2. develop moves the group to page 2, so the later row goes to page 3 and throws, 124.55 on 100.

In the second example the group fits the page it moves to, so checking the next page's height would not be enough.

In an earlier pass, a group could also move to a page with a column narrower than the table. The next pass then failed with the table-width error, a known limit in #1116.

What changed

The rule. A group moves only where every page has the same margins. #1105's rule is unchanged there. Where margins differ, the cut falls after the last row that fits, as in 2.5.3.

  • PageGeometry.hasUniformMargin() (new, package-private) compares the margins the pages end up with, after header and footer space is reserved. All four sides count, with no tolerance (-0.0 equals 0.0).
    • Rules that change nothing do not count: a rule with the document's own margin, a rule that a later one covers completely, or rules that give every page the same new margin (from(1, …)).
    • Rules for pages the document never reaches do count: if page 5 has a different margin, the tables of a two-page document are cut as in 2.5.3. The answer depends only on the document's margins, so it is the same in every pass, and no pass is added.
  • prepareTable reads the answer once and keeps it in PreparedTableLayout. Every head and tail keeps it too, so all pieces of a table follow one rule. Without a page geometry, the margins count as the same.
  • cutOutsideSpanGroups returns the cut unchanged where margins differ.

SplitRequest and the values in it do not change, and only the table split reads the new flag. tableFirstSliceHeight returns the same value as before. Its Javadoc, and a comment on box moves in LayoutCompiler, now say that the split can cut inside the group this value counts, so a heading or box can move when its table would not, or stay when it does.

The cost

If any page's margins differ, every table in the document goes back to 2.5.3's cut, even between two pages with the same margins. One cover page with its own margin is enough. develop keeps some of these groups whole. As in 2.5.3:

  • A spanning cell that is cut can run past the bottom of the page. In the first example it ends at y = -4.35, below the page edge.
  • The next page can open with the rest of the rows the cell covers, and with nothing in the cell's column.
  • A DOCX export starts no Word section inside a merged cell. If a page with other top or bottom margins starts there, Word keeps the margins of the page before it, and the export reports this.

Known limits

This PR does not change these:

Compared with 2.5.3 and develop

I compared the published 2.5.3 jars, develop (610c102) and this PR locally on 58,000 generated documents. The generator is not part of the tests. The documents have tables with and without rowSpan, some with repeated headers, plus text, shapes and spacers. All sets but the third also have one level of sections. The first also has rows, stacks and page breaks. The third and fourth are built from the same random numbers as the second, so the sets are not independent. None has keepWithNext or page references.

  • With 2.5.3 I compared node pages, y and size, and fragment pages, x, y and height, all to 0.01pt, or the full exception text.
  • With develop I compared exact numbers, spanning cells included. Where margins are the same, every result matches develop. Where they differ, every result matches develop with group moves turned off.

"Fit" means that 2.5.3 placed every fragment (text, shapes, table rows) above the bottom margin, to 0.6pt. For tables the check sees only row boxes. It does not see spanning cells or spacers, so the last two rows count those separately, in every document each version lays out.

mixed content (12,000) mostly tables (20,000) tables, no sections (20,000) no page-margin rules (6,000)
Fit in 2.5.3 5,799 9,268 9,416 3,282
… identical: develop → this PR 5,069 → 5,361 5,608 → 8,104 5,667 → 8,503 1,866 → 1,866
… laid out differently 565 → 292 3,293 → 897 3,391 → 668 1,416 → 1,416
… refused 165 → 146 367 → 267 358 → 245 0 → 0
Spanning cell past the margin: 2.5.3 / develop / this PR 1,009 / 724 / 956 4,197 / 2,191 / 3,665 4,432 / 2,314 / 3,894 1,578 / 610 / 610
Spacer past the margin: 2.5.3 / develop / this PR 128 / 0 / 0 254 / 0 / 0 245 / 0 / 0 0 / 0 / 0

"Refused" counts every exception. The counts below are for the first three sets. They are all 0 in the fourth.

  • Fit in 2.5.3, refused here (146 / 267 / 245). All have margins that differ. In local copies I put back 2.5.3's rules one at a time and together. These lay the documents out again, each document counted once:
    • 124 / 252 / 245: the atomic-block check. In 2.5.3 a spacer ran past the margin.
    • 12 / 8 / 0: the rule for moving a box. For one of the 8, the old first-slice height alone is also enough.
    • 3 / 3 / 0: the rule for moving a box together with the old first-slice height. Neither is enough alone.
    • 2 / 2 / 0: the old first-slice height.
    • 3 / 0 / 0: the rule for moving a box. This is the column limit. For one of the 3, the atomic-block check alone is also enough.
    • 1 / 2 / 0: the closing inset. The one in the first set comes after a page break.
    • 1 / 0 / 0: the old first-slice height together with the atomic-block check. With only the height, a spacer that ran past the margin in 2.5.3 is refused instead.
  • Refused here, laid out on develop: 25 / 90 / 94. 2.5.3 also refused 19 / 84 / 90 of them. Of those it laid out, 1 / 5 / 3 fit and 5 / 1 / 1 did not. This PR lays out 24 / 109 / 119 that develop refuses.
  • Spanning cells. Where both versions lay the document out, 226 / 1,471 / 1,571 have one past the margin here but not on develop. Against 2.5.3, it is 1 / 2 / 0, all because of fix(layout): draw a box's decoration on its own box and keep atomic blocks and closing insets on the page they land on #1107's rules for moving a box and for closing insets.
  • Not traced. Where margins differ, 272 / 252 / 0 of the documents that fit in 2.5.3 lay out differently. With all four 2.5.3 rules back (atomic-block check, box move, closing inset, first-slice height), 84 and 175 of them are identical. I did not trace the causes of the rest. With those four rules back, every fragment of the fitting documents where margins differ is where 2.5.3 puts it, except section decoration in 95 documents of the first set.

Verification

./mvnw -B -ntp clean test -pl :graph-compose-qa -am → BUILD SUCCESS, 6,541 tests, 0 failures, 4 skipped (core 968, render-pdf 351, render-docx 2,851, testing 5, render-pptx 233, templates 128, qa 2,005). No snapshot or visual baseline moved. No existing assertion changed. DocxPageMarginSectionsTest passes unedited, but one of its comments is now out of date. It says the layout cuts inside a span only when the rows do not fit an empty page.

  • PageGeometryTest (+22): 5 sets of overrides that leave one margin and 14 that leave two, an open-ended range, negative zero, and 6,174 override lists checked page by page.
  • TableSpanGroupPageMarginsTest (new, core, 8): the same tables split both ways, with and without a repeated header. Heads, tails and a tail of a tail keep the rule.
  • TableRowSpanPageMarginsTest (new, qa, 15): whole documents.
    • Ten cases have margins that differ. They check 2.5.3's pages and positions, and the bottom of the spanning cell wherever a group is cut. They include a rule for a page the document never reaches, and a table too wide for the page its group would move to. All ten pass on 2.5.3 and fail on develop.
    • In the other five, groups move whole: no rules, two kinds of rules that change nothing, one new margin on every page, and a footer that makes the margins equal. They pass on develop and fail on 2.5.3.
  • MultiPassCapacityRefusalTest (+1). Its 256pt rowSpan document keeps its layout, but its first pass now cuts the group and refuses nothing. A new document, with rows of 238pt and 78pt, covers a rowSpan table that is refused only in the first pass.
  • TableRowSpanSplitTest passes the new record component to a constructor. Its class comment and TableRowSpanPageBreakTest's now say that their rule needs the same margins on every page.

I ran 35 sabotages, covering 28 different breaks: 24 against the core tests and 11 against qa, with seven run against both. A failing test caught each run, and the sources were restored byte-identical.

The 141 example layouts and the 859 distinct layouts from the template tests match develop. Two examples use rowSpan, and no template does. 7 and 69 of these layouts have margins that differ. None has both.

No public API change: japicmp for core and extract-api.mjs --from-reactor --check pass.

Lane: shared-engine (table split).

…argin

Since #1105 a table moves a group of rows held together by rowSpan cells
to the next page instead of cutting inside it, if the group fits an empty
page of the current page's size. With per-page margins the next page can
be shorter. A 184.35pt group that 2.5.3 cut on page 1 moved to a 100pt
page 2 and threw AtomicNodeTooLargeException. A group that moved to a page
that could hold it could also send a later row to a page that could not
hold that row.

- Where page margins differ, splitTable cuts after the last row that fits,
  as in 2.5.3. A spanning cell can again run past the bottom margin, and
  the next page can again open with the rest of the rows it covers. A DOCX
  export starts no Word section there. Where every page has the same
  margin, the rule from #1105 does not change.
- PageGeometry.hasUniformMargin (package-private) compares the margins the
  pages end up with, so a rule that changes nothing does not count. A rule
  with another margin for a page the document never reaches does. Negative
  zero equals zero.
- prepareTable reads the answer once and keeps it in the prepared table,
  and every head and tail keeps it too.

SplitRequest and the height tableFirstSliceHeight returns do not change.
No public API change.
The height that box moves and keepWithNext count for a table that opens
with a rowSpan group is the whole group, but the split can cut inside it.
So a box or heading can also stay on a page when the table moves to the
next one. The comment on box moves and the Javadoc of
tableFirstSliceHeight now say so. Only comments change.
The split moves a group only if it fits an empty page below the repeated
header rows. The Javadoc of splitTable and tableFirstSliceHeight left the
header rows out. The class comment of TableRowSpanPageMarginsTest said
that every table with different page margins lays out as in 2.5.3, but
only the cut is the same. Only comments change.
@DemchaAV
DemchaAV marked this pull request as ready for review November 30, 2026 17:39
@DemchaAV
DemchaAV merged commit c2eb401 into develop Nov 30, 2026
13 checks passed
@DemchaAV
DemchaAV deleted the fix/rowspan-move-uniform-margins branch November 30, 2026 18:07
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