From 0805f3c93243754df29c17564b62bb7ace9f66a5 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sat, 24 Oct 2026 00:23:55 +0800 Subject: [PATCH] Reserve insertion class skip in the paged builder --- crates/texrra-tex-core/src/lib.rs | 24 ++++++++++- crates/texrra-tex-core/src/page.rs | 64 ++++++++++++++++++++++++++++++ docs/ROUTE_D_PLAN.md | 4 +- docs/TEX_CORE.md | 4 +- 4 files changed, 90 insertions(+), 6 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index aa43af3..97460bb 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -2240,13 +2240,18 @@ impl Engine { page.floats = floats; let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum(); 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.seal_in_place(); let shipped = match routine.as_mut() { Some(routine) => routine.ship( document, &page, - (document.page_goal - reserved).max(0), + (document.page_goal - reserved - insertion_skip).max(0), fragment.penalty, fragment.depth, fragment.stretch, @@ -2287,12 +2292,17 @@ impl Engine { let mut page = Page::empty(); let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum(); 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(); let shipped = match routine.as_mut() { Some(routine) => routine.ship( document, &page, - (document.page_goal - reserved).max(0), + (document.page_goal - reserved - insertion_skip).max(0), 10_000, 0, 0, @@ -2359,6 +2369,16 @@ impl Engine { height: document.insertion_class_limit(class.number), }) .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), float_sep: document.float_sep.max(0), bottom_fraction_percent: document.bottom_fraction_percent, diff --git a/crates/texrra-tex-core/src/page.rs b/crates/texrra-tex-core/src/page.rs index b759ee5..df766bb 100644 --- a/crates/texrra-tex-core/src/page.rs +++ b/crates/texrra-tex-core/src/page.rs @@ -151,6 +151,9 @@ pub struct PageBuilder { pub top_skip: i64, pub max_depth: i64, pub insertion_limits: Vec, + /// TeX `\skip`: 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. pub text_float_sep: i64, /// TeX floatsep: space between stacked bottom floats. @@ -177,6 +180,7 @@ impl PageBuilder { top_skip: 0, max_depth: 0, insertion_limits: Vec::new(), + insertion_skips: Vec::new(), text_float_sep: 0, float_sep: 0, bottom_fraction_percent: 100, @@ -196,6 +200,14 @@ impl PageBuilder { .map_or(self.page_goal, |limit| limit.height.max(0)) } + /// TeX's `\skip` 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. pub fn set_page_goal(&mut self, page_goal: i64) { self.page_goal = page_goal.max(1); @@ -594,7 +606,12 @@ impl PageBuilder { height: insert, } => { 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); + if first { + reserved += skip; + } let capacity = (limit - *used).max(0); let taken = (*insert).min(capacity); *used += taken; @@ -1135,6 +1152,53 @@ mod tests { 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] fn insertion_reservation_pushes_the_break_earlier() { let mut builder = PageBuilder::new(100); diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index e77c0fb..264353f 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`, `\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and 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 -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. 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 over to later pages (TeX output registers are global); an assigned `\vsize` changes later page goals for documents without insertions or floats, but - the paged builder now runs the routine page by page, so `\dimen` and `\count` assignments change the insertion limits and reservations of later pages (a `\skip` 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` and `\count` assignments change the insertion limits and reservations of later pages; the configured `\skip` reserves space above its class once per page, while an output-routine `\skip` 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; 5. math is a compact subset without the CM math fonts (`cmmi`, `cmsy`, `cmex`), display-math line breaking or `\mathchoice`-level styling; diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index c050be6..ba3df12 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -129,7 +129,7 @@ and justification. | --- | --- | --- | | 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 | -| 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`/`\dimen`/`\skip` 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` or `\count` 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`/`\dimen`/`\skip` 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` or `\count` changes the insertion capacity and reservation of later pages); the configured `\skip` 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 | | 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 | @@ -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. -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