Skip to content

fix(layout): cut a table row taller than a page between its lines - #661

Merged
Tim81 merged 8 commits into
mainfrom
fix/table-row-split
Nov 21, 2026
Merged

Tim81 merged 8 commits into
mainfrom
fix/table-row-split

Conversation

@Tim81

@Tim81 Tim81 commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Closes #516

What changed

A table row taller than a page is cut between the lines of its cells and continues on the next page. On 2.3.3 Save threw InvalidOperationException ("An element is too tall to fit on a single page") for it, with no hint of the row or the cell.

  • A row is cut only when it does not fit the room left and fits neither box a fresh page offers: the page under the repeated header rows and inside the table's own margins. A row that fits one of those boxes moves to the next page whole. A row that fits the page alone is cut when a header row or a table margin takes room from it (20 pt header row plus a 47-line row, or a 47-line row with table Margins of 10, on a 300x600 page with 10 pt margins: 2.3.3 throws, this build saves 2 pages). A table whose rows each fit the area a fresh page leaves under its header rows and inside its margins cuts no row.
  • Each cell is cut at its own line: the slice holds the most lines n with n * leading + vertical padding fitting the room, by the same test the fit loop applies to a whole row. Every slice is drawn as a whole cell (background, border, padding); a cell with no line left draws an empty rectangle.
  • The row starts on the page the rows before it are on when at least one line fits there; otherwise the table breaks before it, as for any row.
  • The repeated header rows are drawn above every slice. The row is wrapped once and the lines travel with the overflow renderer, so a row of L lines over P pages wraps O(L) text.
  • Tagged output: one Table per page, one TR per row and slice, one cell element per cell of each slice. A cell cut across pages is tagged on each page: a cell that holds a line is a TD, or a TH in a header row, and a slice cell with no line is an empty TD, also in a header row (the Tagged PDF Best Practice Guide, 4.2.6.2, says "empty cells are always cells, never cells"). This is the per-page-fragment shape every split table already has.
  • Still refused with InvalidOperationException, whose message is the old text followed by a sentence naming what cannot be cut: header rows that do not fit a page; a row that declares a RowSpan or lies under one (rows named); a row with a cell whose vertical padding plus one line does not fit the page under the header rows (row and cell named).
  • LayoutContext gets an internal FreshPageArea, set by the document on every layout call in both passes and copied by WithArea. LayoutResult gets an internal Nothing(string reason) and Reason; the retry throws ElementTooTall().Message + " " + reason. A TableRenderer given a context built through the public constructor has no such box and does not start a cut; a row already cut keeps cutting in its overflow renderer under such a context only at the width it was cut at; at another width the saved lines are dropped and the row is laid out again from its first line, which without the box is not cut. No public API changes.
  • XML docs on TableElement, TableRenderer.Layout, Cell.RowSpan, Cell.Padding, Document.Add(TableElement), LayoutContext and LayoutResult; CHANGELOG entry under Fixed.

Deliberate output changes

  1. Every input that threw the too-tall exception because of a row that can be cut now saves.
  2. For such inputs a non-finite Cell.Background or TableElement.BorderColor channel, a NaN left margin, a NaN right margin or a -Infinity left margin (table or document) used to throw InvalidOperationException before anything was drawn. The row is now drawn, so the canvas throws ArgumentException (ParamName null), as it does for the same value in any row that fits. (Row.Background is not in this list: TableElement.Add(Row) refuses a non-finite channel at the call.) A NaN text colour or negative border width in such a row keeps InvalidOperationException but the message is replaced, not extended. A +Infinity table bottom margin keeps InvalidOperationException; its message gains the cell sentence.
  3. A table or document whose right margin edge is -Infinity and whose row is too tall for a page now saves (2 pages for 48 lines, 3 for 100; the same table with a short row already saved on 2.3.3). Listed in the CHANGELOG.

The existing test that pinned the refusal of a hard-broken 300-character cell on a 200pt page now pins the continuation (16 lines on page 1, 14 on page 2). The test pinning the too-tall message for a RowSpan group taller than the page now expects the same text plus the sentence naming rows 0 to 39.

2.3.3 against this build

Page 300x600, 10pt margins (content 580pt), Helvetica 10 (leading 12), columns 40pt and 100pt, default padding; a row of n one-word lines is 12 n + 8 pt tall. The 2.3.3 column is 2.3.3+1c61fa6; the build column is this branch. Bytes are for the whole file.

Input 2.3.3 This build
46 lines, no header saves, 1707 bytes same, 1707
47 lines, no header saves, 1709 same, 1709
48 / 49 / 100 lines, no header throws too tall saves (2107 / 2118 / 2665)
header + 46 lines saves, 1737 same, 1737
header + 47 / 48 / 49 / 100 lines throws too tall saves (2161 / 2177 / 2178 / 2758)
20pt row, then 46 / 47 lines saves (1738 / 2113) same
20pt row, then 48 lines throws too tall saves, 2147
one 6,000-character cell, A4, default margins throws too tall saves, 2 pages
48 lines, footer band throws (band wording) saves
header row taller than the page throws, generic throws, message ends "The repeated header (table row 0) is 728.0pt tall, but a page leaves 580.0pt for the table. ..."
RowSpan group taller than the page throws, generic throws, message ends "Table rows 0 to 1 are joined by a RowSpan, ... Together they are 736.0pt tall, ..."
padding 400 on a cell throws, generic throws, message ends "Table row 0, cell 0 needs 812.0pt ..."
line taller than the page (font 600) throws, generic throws, message ends "Table row 0, cell 0 needs 728.0pt ..."

Every "same" row is from one probe run printing both assembly versions; the probe runs on 2.3.3 and on 7079efc gave identical outcomes and byte counts for every row above that 2.3.3 saved.

Differential fuzz of random tables (1 to 3 columns, up to 70 rows, row and cell backgrounds, RowSpan, ColSpan, header rows, footer, tagged or not, four page heights), this branch against 7079efc:

Run seeds saved on both: byte-identical refused before, saves now saved before, refused now other change
rows that mostly fit a page 3,000 1,865 of 1,865 119 0 0
rows up to 70 lines 2,500 501 of 501 493 0 0
rows up to 70 lines, no header, no spans 2,500 624 of 624 1,876 0 0

In the last run every one of the 1,257 saved files without a footer draws exactly the characters its cells hold (counted from the Tj strings). qpdf 12.4.2 --check passes on the first 120 newly saved files of the second run.

Non-finite geometry, measured on origin/main 5aea9ff and on this branch with one probe built against each (each run printed its Layout assembly path): a table margin and a document margin edge of NaN, +Infinity or -Infinity on each of the four edges, for a 5-, 48- and 100-line row, plus a NaN cell background, an infinite border colour, a NaN text colour and a border width of -1 on a 48-line row. Every 5-line case behaves the same on both builds, except a +Infinity bottom table margin, which keeps InvalidOperationException and gains the sentence naming the cell. On the taller rows, a NaN left or right margin and a -Infinity left margin (table or document), a NaN cell background and an infinite border colour change from InvalidOperationException to the canvas ArgumentException (deliberate output change 2); a -Infinity right margin saves (deliberate output change 3); the NaN text colour and the -1 border width keep InvalidOperationException with their message replaced by one naming the cause. Every other case behaves the same on both builds: for example a NaN top margin throws ArgumentException, a NaN bottom margin and a -Infinity top or bottom margin save, a +Infinity left or right table margin keeps the too-tall message, and a +Infinity document margin is refused by Document with ArgumentException (margins). A +Infinity bottom table margin keeps InvalidOperationException on every row height and gains the cell sentence.

Tests (Release, oracles required)

TableRowSplitTests (new, 44 cases): the boundary table above with exact rectangles and baselines per page, a header repeated on three pages, a 20pt row then a cut row (starts on the current page), 29 full rows then a cut row (starts on the next page), cells of different leading, a short cell beside a tall one, rows after a cut row, a footer with {pages}, the 6,000-character cell, row and cell backgrounds and the repeated header background per slice, tagged structure counts, each still-refused shape with the exact message (and the band wording first), a refusal after other rows, a non-finite background and text colour, a context without the box, WithArea, a cut row resumed at another width, a NaN width, an allocation-ratio pin (20,000 lines against 5,000, bound 6), qpdf --check with pdftotext page order, veraPDF ua1 (with and without a header) and 2u.

veraPDF 1.30.4 on four cut-row files in an embedded font: PDF/UA-1 plain, PDF/UA-1 under a header, PDF/A-2u with a row background, PDF/A-2u under a header with a row background: each isCompliant="true", failedRules="0", so the failed-rule list is empty for all four. qpdf --check passes on the same four.

Gates at fbed88b (main at 2484e51 merged in), oracles required (veraPDF 1.30.4, qpdf 12.4.2,
poppler 26.08.0): VellumPdf.Layout.Tests in Release, both TFMs together, 3,300 run, 3,296 passed,
4 skipped (the Linux-only named-pipe and /proc tests), 0 failed. Release solution build: 0
warnings, 0 errors. dotnet format --verify-no-changes, the clean-room check and the roadmap check
each exit 0.

Mutations

Each mutation was applied in a detached checkout of the commit, the test project rebuilt (the DLL timestamp checked), the three table test classes run, and the checkout restored with git checkout -- src.

Mutation Failing tests
never cut (CanCut false) 23, e.g. ARowOneLineTooTall_isCutAfterTheLastLineThatFits
cut whenever the row does not fit this area (drop the "fits a fresh page" test) ARowThatFitsAFreshPage_butNotTheRoomLeft_movesWhole
re-wrap the cut row on every page ACellOfManyLines_allocatesLinearlyInItsLines
row height ignores the slice RowsAfterACutRow_followItsLastSlice
refusal reason dropped 10, e.g. ALineThatFitsOnlyWithoutTheHeader_isRefused_namingTheHeader
a RowSpan group may be cut 4, e.g. ARowUnderAHeaderRowSpan_isRefused_andTheRefusalTerminates
a cell that makes no progress is not refused 5
one line fewer per slice 21
a slice cell with no line gets no TD 2, e.g. ATaggedCutRow_underAHeader_repeatsTheThOnEveryPage
resume width compared with == (NaN restarts the row) ACutRow_withANaNWidth_isRefusedByTheCanvas_notByTheContinuationLimit
document does not pass the fresh-page box 31
fresh boxes not deflated by the table margins ARowThatFitsThePageButNotInsideTheTableMargins_isCut
resumed zero-line guard removed ACutRow_resumedInAnAreaThatHoldsNoLine_isNothing
resume needs the fresh box ACutRow_thatBegan_isResumedUnderAContextWithoutTheBox
a plain context cuts 2 (ARendererWithoutAFreshPageBox_*)
"fits both" instead of "fits neither" survives (see limits)
header height ignored when cutting 2, e.g. AHeaderAndARowTooTall_repeatTheHeaderAndCutTheRow
WithArea drops the box WithArea_keepsTheFreshPageBox
document ignores the reason 10
resumed row does not advance 21
draw loop does not stop after the cut row survives: after the cut row the running height equals the slice's bottom, so the loop's own bottom test stops it; the explicit break is a guard

Checked against the specifications

  • ISO 14289-1:2014, 7.5 (p. 6): table headers are tagged per ISO 32000-1 Table 337 and Table 349, and structure elements of type TH should have a Scope. It says nothing about a table cut by a page break. The slices use the same Table/TR/TD types and attributes as any row.
  • Tagged PDF Best Practice Guide, 3.4 (p. 9) wants content that spans pages in one structure element, and its table checklist wants one Table for a table that spans pages. This change does not do that for a cut cell, as no split table in the library does; the page-fragment shape is the existing one and is documented.
  • Same guide, 4.2.6.2 (table checklist): "empty cells are always cells, never cells"; the empty slice cell follows it.
  • ISO 19005-2, 6.1.13: a slice draws a subset of a cell's existing lines, so no show string gets longer. The table renderer writes no q/Q (a search of src/VellumPdf.Layout/Rendering/Table for SaveState and RestoreState finds neither), so the nesting limit is untouched.

Not done, and limits

  • A cell cut across pages is two TD elements, not one structure element with two marked-content sequences; see above.
  • RowSpan groups, header rows and cells that cannot hold one line stay refused.
  • The two fresh-page boxes (the content area, and the area from the top of the content area to its bottom, which can differ by a rounding step) are both tested, but no test builds a geometry where they differ: the step is far below the 0.001 pt fit tolerance on the 300x600 pages tried, and I found no public-API geometry where one box holds a row and the other does not. Mutating "fits neither" to "fits both" therefore survives. The cut condition uses "fits neither".
  • A single unbroken cell token longer than 32,767 bytes under a PDF/A claim is unchanged.

A row that did not fit the room left, and would not fit a page of its own
either, made Save throw 'too tall' with no hint of the row. It is now cut
between the lines of its cells and continues on the next page, with the
repeated header rows above each slice. A row that fits a page still moves to
the next page whole.

Header rows, rows held by a RowSpan and rows with a cell that cannot hold one
line stay refused, and the message names the rows or the cell. The document
tells the renderer what a fresh page offers, so a renderer given a context it
did not build never cuts.

Closes #516
The Tagged PDF Best Practice Guide reserves TH for cells that head something
and says an empty cell is always a TD. A cut header row can leave a cell with
no line on a page, which was written as an empty TH.
# Conflicts:
#	src/VellumPdf.Layout/Rendering/Table/TableRenderer.cs
The rule that an empty cell is a TD, never a TH, is in the Tagged PDF Best Practice Guide's table checklist (4.2.6.2), not 3.5 (empty structure elements).
The shipped prose said a row that fits a page is never cut. It is cut
when it fits neither fresh-page box, which a repeated header or the
table margins can make true of a row that fits the page alone. Also
corrects the TH/TD claim for header rows, the non-finite margin
changelog entry, and what holds for a context the document did not
build. Adds tests for the margin-deflated box and for resuming a cut
row under a plain context.
Round 1 said a header row always names the header rows, that a cut row
keeps cutting under any context, and that a table returns the rows that
fit before an uncut row. Each is false under a header after data, another
width, or a RowSpan group. Adds a test that kills a doubled margin
deflation of the fresh page box.
@Tim81
Tim81 merged commit 9747913 into main Nov 21, 2026
4 checks passed
@Tim81
Tim81 deleted the fix/table-row-split branch November 26, 2026 20:21
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.

Layout: a table row taller than the page is refused instead of paginated

1 participant