Reserve insertion class skip in the paged builder

This commit is contained in:
gloamxun committed 2026-10-24 00:23:55 +08:00
1 parent 1dc3e95a04
commit 0805f3c932
4 files changed
+90 -6

No files matched your search

+22 -2
View File
@@ -2240,13 +2240,18 @@ impl Engine {
page.floats = floats; page.floats = floats;
let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum(); let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum();
page.insertions = insertions.into_values().collect(); page.insertions = insertions.into_values().collect();
let insertion_skip: i64 = page
.insertions
.iter()
.map(|insertion| document.insertion_skip_for(insertion.class))
.sum();
page.sources = sources; page.sources = sources;
page.seal_in_place(); page.seal_in_place();
let shipped = match routine.as_mut() { let shipped = match routine.as_mut() {
Some(routine) => routine.ship( Some(routine) => routine.ship(
document, document,
&page, &page,
(document.page_goal - reserved).max(0), (document.page_goal - reserved - insertion_skip).max(0),
fragment.penalty, fragment.penalty,
fragment.depth, fragment.depth,
fragment.stretch, fragment.stretch,
@@ -2287,12 +2292,17 @@ impl Engine {
let mut page = Page::empty(); let mut page = Page::empty();
let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum(); let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum();
page.insertions = insertions.into_values().collect(); page.insertions = insertions.into_values().collect();
let insertion_skip: i64 = page
.insertions
.iter()
.map(|insertion| document.insertion_skip_for(insertion.class))
.sum();
page.seal_in_place(); page.seal_in_place();
let shipped = match routine.as_mut() { let shipped = match routine.as_mut() {
Some(routine) => routine.ship( Some(routine) => routine.ship(
document, document,
&page, &page,
(document.page_goal - reserved).max(0), (document.page_goal - reserved - insertion_skip).max(0),
10_000, 10_000,
0, 0,
0, 0,
@@ -2359,6 +2369,16 @@ impl Engine {
height: document.insertion_class_limit(class.number), height: document.insertion_class_limit(class.number),
}) })
.collect(), .collect(),
insertion_skips: document
.insertion_classes
.iter()
.map(|class| {
(
class.number,
class.skip.unwrap_or(document.insertion_skip).max(0),
)
})
.collect(),
text_float_sep: document.text_float_sep.max(0), text_float_sep: document.text_float_sep.max(0),
float_sep: document.float_sep.max(0), float_sep: document.float_sep.max(0),
bottom_fraction_percent: document.bottom_fraction_percent, bottom_fraction_percent: document.bottom_fraction_percent,
+64
View File
@@ -151,6 +151,9 @@ pub struct PageBuilder {
pub top_skip: i64, pub top_skip: i64,
pub max_depth: i64, pub max_depth: i64,
pub insertion_limits: Vec<InsertionLimit>, pub insertion_limits: Vec<InsertionLimit>,
/// TeX `\skip<number>`: space above an insertion class on a page that
/// carries it. Applied once per class, on top of the class limit.
pub insertion_skips: Vec<(u8, i64)>,
/// TeX `\textfloatsep`: space between top floats and the body. /// TeX `\textfloatsep`: space between top floats and the body.
pub text_float_sep: i64, pub text_float_sep: i64,
/// TeX floatsep: space between stacked bottom floats. /// TeX floatsep: space between stacked bottom floats.
@@ -177,6 +180,7 @@ impl PageBuilder {
top_skip: 0, top_skip: 0,
max_depth: 0, max_depth: 0,
insertion_limits: Vec::new(), insertion_limits: Vec::new(),
insertion_skips: Vec::new(),
text_float_sep: 0, text_float_sep: 0,
float_sep: 0, float_sep: 0,
bottom_fraction_percent: 100, bottom_fraction_percent: 100,
@@ -196,6 +200,14 @@ impl PageBuilder {
.map_or(self.page_goal, |limit| limit.height.max(0)) .map_or(self.page_goal, |limit| limit.height.max(0))
} }
/// TeX's `\skip<number>` for `class`.
fn insertion_skip(&self, class: u8) -> i64 {
self.insertion_skips
.iter()
.find(|(number, _)| *number == class)
.map_or(0, |(_, skip)| (*skip).max(0))
}
/// Replace the page goal used by later `build`/`build_from` calls. /// Replace the page goal used by later `build`/`build_from` calls.
pub fn set_page_goal(&mut self, page_goal: i64) { pub fn set_page_goal(&mut self, page_goal: i64) {
self.page_goal = page_goal.max(1); self.page_goal = page_goal.max(1);
@@ -594,7 +606,12 @@ impl PageBuilder {
height: insert, height: insert,
} => { } => {
let limit = self.insertion_limit(*class); let limit = self.insertion_limit(*class);
let first = !usage.contains_key(class);
let skip = self.insertion_skip(*class);
let used = usage.entry(*class).or_insert(0); let used = usage.entry(*class).or_insert(0);
if first {
reserved += skip;
}
let capacity = (limit - *used).max(0); let capacity = (limit - *used).max(0);
let taken = (*insert).min(capacity); let taken = (*insert).min(capacity);
*used += taken; *used += taken;
@@ -1135,6 +1152,53 @@ mod tests {
InsertionLimit { class, height } InsertionLimit { class, height }
} }
#[test]
fn insertion_skip_reserves_space_once_per_class() {
let nodes = [
VerticalNode::Box {
height: 30,
depth: 0,
},
VerticalNode::Glue(VerticalGlue {
width: 0,
stretch: 100,
shrink: 0,
order: 0,
}),
VerticalNode::Insertion {
class: 1,
height: 50,
},
VerticalNode::Glue(VerticalGlue {
width: 0,
stretch: 100,
shrink: 0,
order: 0,
}),
VerticalNode::Box {
height: 20,
depth: 0,
},
];
let mut plain = PageBuilder::new(100);
plain.insertion_limits = vec![InsertionLimit {
class: 1,
height: 100,
}];
assert_eq!(plain.build(&nodes).len(), 1);
let mut skipped = PageBuilder::new(100);
skipped.insertion_limits = vec![InsertionLimit {
class: 1,
height: 100,
}];
skipped.insertion_skips = vec![(1, 10)];
let pages = skipped.build(&nodes);
assert_eq!(pages.len(), 2);
assert_eq!(pages[0].start, 0);
assert_eq!(pages[0].end, 3);
}
#[test] #[test]
fn insertion_reservation_pushes_the_break_earlier() { fn insertion_reservation_pushes_the_break_earlier() {
let mut builder = PageBuilder::new(100); let mut builder = PageBuilder::new(100);
+2 -2
View File
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and `\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
PDF opt-in with `?engine=route-d`). 471 workspace tests are green and no oracle PDF opt-in with `?engine=route-d`). 472 workspace tests are green and no oracle
fixture is ignored. fixture is ignored.
Known gaps, in dependency order: Known gaps, in dependency order:
@@ -88,7 +88,7 @@ source-level `\hyphenation{...}` exceptions and `\parshape` measures,
box height and depth, and the assigned values carry box height and depth, and the assigned values carry
over to later pages (TeX output registers are global); an assigned `\vsize` over to later pages (TeX output registers are global); an assigned `\vsize`
changes later page goals for documents without insertions or floats, but changes later page goals for documents without insertions or floats, but
the paged builder now runs the routine page by page, so `\dimen<n>` and `\count<n>` assignments change the insertion limits and reservations of later pages (a `\skip<n>` assignment still has no effect on that path, which never applied `\skip` there), `\box255` content is not inspectable and the full `\output` machinery is still missing; the paged builder now runs the routine page by page, so `\dimen<n>` and `\count<n>` assignments change the insertion limits and reservations of later pages; the configured `\skip<n>` reserves space above its class once per page, while an output-routine `\skip<n>` assignment still has no effect because the builder's parameters are fixed before the page loop; `\box255` content is not inspectable and the full `\output` machinery is still missing;
4. `\halign` handles spans/omit/templates but not the full TeX column algorithm; 4. `\halign` handles spans/omit/templates but not the full TeX column algorithm;
5. math is a compact subset without the CM math fonts (`cmmi`, `cmsy`, `cmex`), 5. math is a compact subset without the CM math fonts (`cmmi`, `cmsy`, `cmex`),
display-math line breaking or `\mathchoice`-level styling; display-math line breaking or `\mathchoice`-level styling;
+2 -2
View File
@@ -129,7 +129,7 @@ and justification.
| --- | --- | --- | | --- | --- | --- |
| M0 | vertical list, page builder, checkpoint, page hash, edit demo | edit on page 2 rebuilds only page 2 | | M0 | vertical list, page builder, checkpoint, page hash, edit demo | edit on page 2 rebuilds only page 2 |
| M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing, `\spaceskip`/`\xspaceskip`, `\raggedright` (infinite `\rightskip`), `\looseness`, `\baselineskip`/`\lineskip`/`\parskip` interline glue, pdfLaTeX oracle matrix 90-240pt plus register, geometry, `\sfcode`, `\parindent`, `\parshape`, `\leftskip`/`\rightskip`, `\hfil`/`\hfill` glue orders, `\prevgraf` in the output routine, and vertical-glue fixtures | | M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing, `\spaceskip`/`\xspaceskip`, `\raggedright` (infinite `\rightskip`), `\looseness`, `\baselineskip`/`\lineskip`/`\parskip` interline glue, pdfLaTeX oracle matrix 90-240pt plus register, geometry, `\sfcode`, `\parindent`, `\parshape`, `\leftskip`/`\rightskip`, `\hfil`/`\hfill` glue orders, `\prevgraf` in the output routine, and vertical-glue fixtures |
| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top/bottom `Float` placement; `page::PageBuilder` scores candidate breakpoints with TeX page badness, glue setting (with `\raggedbottom`/`\flushbottom` applied when pages are materialised, and TeX glue orders `\vfil`/`\vfill`/`\vfilll` honoured so only the highest order with stretch is used), `\topskip`/`\maxdepth`, and `Engine::layout_paged` uses it for documents whose units are single vertical items, placing top and bottom floats (with `\floatsep`, a `\bottomfraction` cap and the `\topnumber`/`\bottomnumber`/`\totalnumber` limits, with `\floatpagefraction` deciding when a float-heavy page ships without body) and insertion classes on that path, while in-text floats flow inline as body material; paged layouts replay edits from the edited unit's page and fall back to a full rebuild only when insertions, bottom floats or an interpreted `\output` are present; `\output` is interpreted for a `\shipout`/`\ifdim`/`\ifnum`/`\ifodd`/`\ifcase` subset; `\pageno`, `\outputpenalty` and `\deadcycles` are readable and assignable registers (`\pageno=...` carries into the next page, and an assigned `\vsize` retargets later pages when the document has no insertions or floats), `\ht255`, `\dp255` and `\wd255` expose the page box height, depth and width to the routine, and `\hsize` is readable (and assignable for the rest of the run) while `\wd255` tracks it; route-D sets `\hsize` from its 480pt text measure, insertion class registers `\count<n>`/`\dimen<n>`/`\skip<n>` read the configured multiplier, maximum height and skip of class `n` (with `\count0` reading `\pageno`) and accept assignments that outlive the routine (the paged builder runs the routine after each page, so an assigned `\dimen<n>` or `\count<n>` changes the insertion capacity and reservation of later pages), `\deadcycles` accumulates pages the output routine did not ship and resets on the next shipout, `\maxdeadcycles` (default 25, assignable) makes the page builder ship the page itself once that many dead cycles have accumulated, `PageFragment::penalty` carries the break penalty (`\outputpenalty`: the penalty value when the break landed on a penalty, otherwise 10000, as TeX reports for glue breaks, goal overflow and the final page), and `PageFragment::stretches` records glue stretch per TeX order so `\pagefilstretch`/`\pagefillstretch`/`\pagefilllstretch` are readable in the output routine; `\vbadness` reports the badness of the break that produced the page and `\insertpenalties` the number of insertion items held over from it | | M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top/bottom `Float` placement; `page::PageBuilder` scores candidate breakpoints with TeX page badness, glue setting (with `\raggedbottom`/`\flushbottom` applied when pages are materialised, and TeX glue orders `\vfil`/`\vfill`/`\vfilll` honoured so only the highest order with stretch is used), `\topskip`/`\maxdepth`, and `Engine::layout_paged` uses it for documents whose units are single vertical items, placing top and bottom floats (with `\floatsep`, a `\bottomfraction` cap and the `\topnumber`/`\bottomnumber`/`\totalnumber` limits, with `\floatpagefraction` deciding when a float-heavy page ships without body) and insertion classes on that path, while in-text floats flow inline as body material; paged layouts replay edits from the edited unit's page and fall back to a full rebuild only when insertions, bottom floats or an interpreted `\output` are present; `\output` is interpreted for a `\shipout`/`\ifdim`/`\ifnum`/`\ifodd`/`\ifcase` subset; `\pageno`, `\outputpenalty` and `\deadcycles` are readable and assignable registers (`\pageno=...` carries into the next page, and an assigned `\vsize` retargets later pages when the document has no insertions or floats), `\ht255`, `\dp255` and `\wd255` expose the page box height, depth and width to the routine, and `\hsize` is readable (and assignable for the rest of the run) while `\wd255` tracks it; route-D sets `\hsize` from its 480pt text measure, insertion class registers `\count<n>`/`\dimen<n>`/`\skip<n>` read the configured multiplier, maximum height and skip of class `n` (with `\count0` reading `\pageno`) and accept assignments that outlive the routine (the paged builder runs the routine after each page, so an assigned `\dimen<n>` or `\count<n>` changes the insertion capacity and reservation of later pages); the configured `\skip<n>` also reserves space above its class once per page in the paged builder, `\deadcycles` accumulates pages the output routine did not ship and resets on the next shipout, `\maxdeadcycles` (default 25, assignable) makes the page builder ship the page itself once that many dead cycles have accumulated, `PageFragment::penalty` carries the break penalty (`\outputpenalty`: the penalty value when the break landed on a penalty, otherwise 10000, as TeX reports for glue breaks, goal overflow and the final page), and `PageFragment::stretches` records glue stretch per TeX order so `\pagefilstretch`/`\pagefillstretch`/`\pagefilllstretch` are readable in the output routine; `\vbadness` reports the badness of the break that produced the page and `\insertpenalties` the number of insertion items held over from it |
| M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | **partial**: source parsing, natural column widths, spans, rule rows, 10pt display-list painting and row-level page splitting; no booktabs/longtable parity yet | | M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | **partial**: source parsing, natural column widths, spans, rule rows, 10pt display-list painting and row-level page splitting; no booktabs/longtable parity yet |
| M4 | math lists, scripts, fractions, radicals, accents, delimiters | **partial**: token parser, atom boxes with CMR10 metrics, delimiter scaling, accents and a display-list painter; no CMEX size variants or TeX math spacing classes yet | | M4 | math lists, scripts, fractions, radicals, accents, delimiters | **partial**: token parser, atom boxes with CMR10 metrics, delimiter scaling, accents and a display-list painter; no CMEX size variants or TeX math spacing classes yet |
| M5 | NFSS/TFM metrics, CM/Latin Modern | same line/page breaks as reference | | M5 | NFSS/TFM metrics, CM/Latin Modern | same line/page breaks as reference |
@@ -367,7 +367,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph. Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
The crate currently has 314 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 471 tests (314 route-D unit, 7 route-D integration, 35 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 13 binary units), with none ignored. v1 remains the live server path. The crate currently has 315 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 472 tests (315 route-D unit, 7 route-D integration, 35 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 13 binary units), with none ignored. v1 remains the live server path.
### M0 incremental demo ### M0 incremental demo