Repository navigation
feat(layout): keep with next, keep together, minimum lines at a break - #680
Merged
Merged
Conversation
A heading could end a page with its section starting overleaf, a paragraph could leave one line at the foot of a page, and a figure could be parted from its caption. Add opt-in KeepWithNext, KeepTogether and Paragraph.MinLinesBeforeBreak/MinLinesAfterBreak, decided in the one placement loop that both passes run, so the page count of the measuring pass is the page count of the drawing pass. A group is laid out on a copy of the cursor first. A request that cannot be honoured, because the run already starts a page or the group does not fit one, is dropped in a fixed order and listed as a warning (801-803), so no page is left empty and no document that saved now throws. Closes #306
A keep-together element whose first call answered Nothing moved to the next page and split there without a report. The check now runs until the element has split once, and the empty-page probe offers the box a page top gets and names the page after it as the retry page. The line counts are read only for a paragraph renderer the document places; a wrapper, a direct Layout call and WithArea contexts ignore them, so they cannot make a save throw. WithArea no longer copies the page-top flag. The keep-with-next, keep-together and line-count remarks state the rule that was measured, not a group-taller-than-a-page rule. The oracle tests route qpdf through the gate.
The keep-with-next report was made when the run was decided, so an element that then moved to the next page by its own keep-together or line count was reported on the page it had left. It is now made once the element's first piece is placed. The paragraph docs no longer compare a line count above the paragraph's length with KeepTogether, which differs for a paragraph taller than a page, and say that the second walk and the empty-page probe are skipped at a page top. New tests pin the report page, both tall-paragraph rows, the page-top guard under mirrored margins and the layout-call counts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #306
What changes
Layout gains opt-in control over where a page break may fall:
KeepWithNextonParagraph,Heading,TableElement,ListElementandLayoutImage.KeepTogetheron the same types except the image, which does not split.Paragraph.MinLinesBeforeBreakandMinLinesAfterBreak(default 1). A value below 1 throwsArgumentOutOfRangeExceptionfrom theinitaccessor. There is no upper bound.LayoutDiagnosticCode.KeepWithNextRelaxed(801),KeepTogetherRelaxed(802) andMinLinesRelaxed(803).StrictDiagnosticsstill saves.The rules live in the one placement loop that both passes run. A run of consecutive
KeepWithNextelements and the element after it form a group. The group is first placed on a copy of the cursor,
using the same loop, so the next element's own keep-together and line counts decide where its first
piece lands. The group holds when each element starts on the page the one before it ends on, which
for an element that splits is the page of its last piece. When it does not hold, the run moves to
the next page. A
PageBreak, or an element withBreakBefore, ends the group. A request thatcannot be honoured is dropped and reported: keep-with-next first, then keep-together, then the line
counts. A run that already starts a page is never moved, so no page is left empty.
A run taller than a page is not dropped for that alone. A paragraph that splits keeps only its last
piece with the element after it, and the pieces before that fall where the pages end. Measured on a
page of 11 lines: 3 filler lines, a 20-line
KeepWithNextparagraph and one more paragraph give8 + 11 + 1 lines, the last line on the page of the next paragraph, with no report. The request is
reported when the next element cannot share the page the last piece ends on, as when that piece
ends exactly at a page foot and the run is already at the top of a page.
The line counts are read for a paragraph the document places, as a
Paragraphor as aParagraphRendereradded to it. A customIRendererthat wraps aParagraphRenderer, a directParagraphRenderer.Layoutcall and a context made withLayoutContext.WithAreaignore them andplace as many lines as fit, so setting them there cannot make a save throw.
A
KeepTogetherelement is checked until it has split once, wherever the split comes from: thefirst call that splits it below a page top, or the first call at the top of the page a
Nothingsent it to (a line count that refused the first page, for one). Either way it is reported once.
The empty-page probe offers the box a page top is offered, which differs from the content height by
a rounding step on some geometries.
Nothing is placed or reported while the group is tried, so anchors, outline entries, page-label
ranges and mirrored margins follow the page an element finally lands on. Everything is decided in
both passes, so
{pages}is the number of pages drawn.Compatibility
All members are opt-in, so a document that sets none of them saves to the same bytes.
origin/mainbuild against this build, every new member set explicitly to its defaultThe two builds were told apart by the loaded assembly's version and SHA-256 (
2.3.3+2316dee,2D2B4E44C030FE40onorigin/main;2.3.3+fd8f9f0,F75BF0587A4EF5E6on this branch), whichthe probe printed with each run, and by the keep types being absent from one.
No deliberate output change. No document that saves today starts to throw.
One deviation from the planned "same page count or more" property.
RandomKeepFlags_...compares thepages that hold something, not all pages, because a recto break adds a blank page only when the page
it would start on is left-hand, and moving content can change that. The sweep above found no
document where the total fell, but the test does not assert that for the total.
Tests
KeepRulesTests(75 tests with the theory rows),KeepRulesByteIdentityTests(3) andKeepRulesOracleTests(2: qpdf--check, pdftotext per page, veraPDF PDF/A-2u on a twelve-chapterdocument with every heading kept; a missing qpdf now skips through the oracle gate). The acceptance
rows are read back as the words on each page.
Layout test project, Release, oracles required: 2,009 passed on net10.0 and on net11.0, 2 skipped on each (named-pipe tests). The three keep classes are 80 tests together.
Mutations
Each mutant was built in a separate worktree and run against the three keep test classes.
MinLinesBeforeBreak_moreThanAPage_afterAShortFiller_saves_andReportsTheSplits,RandomKeepFlags_..., theLargestLineCounts_...rowsMinLines_aboveTheLineCount_...andMinLines_relaxedOnEveryPageOfALongParagraph_...MinLines_aboveTheLineCount_...WithAreacopies the page-top flag, or the line-count flagWithArea_carriesTheFreshPageArea_andNeitherTheTopOfPageNorTheLineCountFlagARendererThatWrapsAParagraphRenderer_inAFreshContext_ignoresTheLineCounts,..._withWithArea_...AParagraphRendererAddedToTheDocument_readsTheLineCountsKeepTogetherParagraphOfThreePages_isReportedOnce_notOnceForEachPiece,KeepTogetherTable_afterAFullPage_...KeepTogetherParagraph_afterAFullPage_splitsAtThePageTop_andIsReportedOnce,KeepTogetherList_afterAFullPage_...KeepTogetherParagraph_thatFitsOnlyWhenOfferedThePageTopBox_movesWholekept >= MinLinesBeforeBreakbecomes>MinLinesBeforeBreak_ofExactlyTheLinesThatFit_keepsThemOnThePageFiveLineParagraph_whereFourFit_...,MinLinesBeforeAndAfterTogether_...MinLinesBeforeBreakFiveLineParagraph_whereFourFit_...,MinLines_aboveTheLineCount_...KeepTogetherParagraphOfThreePages_...MinLinesBeforeBreak_moreThanAPage_...ChainTallerThanAPage_isPlacedAsWithoutTheFlag_andReportedOnceKeepWithNextRelaxed_isReportedOnThePageTheFirstElementMovedToARunAtAPageTop_thatHoldsOnlyOnTheNextPage_isNotMovedOffItTheGroupSimulation_placesTheElementAfterTheRunOnlyAsFarAsItsFirstPieceAKeepTogetherParagraphAtThePageTop_isLaidOutOncePerPage_withNoProbeForAnEmptyPageOne mutant survives and is equivalent in output: naming the page before the right one as the page a
Nothingfrom the probe is retried on. The only reader of that box is the table renderer, for a rowtaller than a page, and it uses the box's y and height, which are the same on every page; only x
differs, and only under mirrored margins.
The mutants of the first round (keep-with-next decided in the drawing pass only, keep-together
dropped before keep-with-next, a run moved off a page top, placements reported while a group is
tried, a look past a
PageBreak, a break not moved up forMinLinesAfterBreak, keep-together nevertesting an empty page, the run not advancing the page) were not rerun after this change. Their tests
are unchanged and pass.
Cost
On 1,000 paragraphs of about 32 words on A4, 63 pages, where no paragraph straddles a page foot:
1,124
Layoutcalls unflagged. WithKeepWithNexton every paragraph the document is one run, andthe walk stops at the first element that starts on a later page than the one before it ended on, so
it costs 19 calls more: 1,143, 1.0003 times the allocation. With it on every second paragraph:
2,248 calls (2.25 per flag) and 1.0137 times the allocation. Every tenth: 1,348 calls, 1.0027 times.
KeepTogetheralone adds no calls on this document because no paragraph is split.IRenderer.Layoutsays the document may lay a renderer out ahead of the elements before it.
Not done
KeepWithNextparagraph so that more than its last piece stays with the nextelement.