diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index de42160..ee603a4 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1650,6 +1650,7 @@ impl Engine { }; let mut pages = Vec::new(); let mut page_penalties = Vec::new(); + let mut page_depths = Vec::new(); let mut held: BTreeMap> = BTreeMap::new(); for fragment in builder.build(&nodes) { let mut items = Vec::new(); @@ -1707,6 +1708,7 @@ impl Engine { page.seal_in_place(); pages.push(page); page_penalties.push(fragment.penalty); + page_depths.push(fragment.depth); } // Insertion material that did not fit its class limit on the page where @@ -1730,6 +1732,7 @@ impl Engine { page.seal_in_place(); pages.push(page); page_penalties.push(0); + page_depths.push(0); } if let Some(source) = &document.output_routine { let program = output::OutputProgram::parse(source).ok()?; @@ -1739,6 +1742,7 @@ impl Engine { let context = output::OutputContext { page_goal: document.page_goal, page_total, + page_depth: page_depths.get(index).copied().unwrap_or(0), vsize: document.page_goal, top_skip: document.top_skip, max_depth: document.max_depth, @@ -5040,6 +5044,30 @@ mod tests { ); } + #[test] + fn output_routine_sees_the_page_depth() { + let math = Unit { + id: 1, + source: String::new(), + items: vec![VerticalItem::Math { + atom: crate::math::MathAtom::Symbol('x'), + width: 10, + height: 500_000, + depth: 300_000, + }], + paragraph_style: None, + }; + let mut document = Document::new(800_000, vec![math]); + document.max_depth = 400_000; + document.output_routine = Some(r"\ifdim\pagedepth>4pt\shipout\box255\fi".to_owned()); + let kept = Engine::layout_paged(&document).expect("math is page-buildable"); + assert_eq!(kept.pages().len(), 1); + + document.max_depth = 0; + let dropped = Engine::layout_paged(&document).expect("math is page-buildable"); + assert_eq!(dropped.pages().len(), 0); + } + #[test] fn paged_layout_refuses_floats_but_places_insertions() { let floated = Document::new(100, vec![Unit::float(1, FloatPlacement::Top, 100, 20)]); diff --git a/crates/texrra-tex-core/src/output.rs b/crates/texrra-tex-core/src/output.rs index 8ea2096..0e419ed 100644 --- a/crates/texrra-tex-core/src/output.rs +++ b/crates/texrra-tex-core/src/output.rs @@ -3,7 +3,7 @@ //! Only the subset that decides *which* page box leaves the page builder is //! supported today: `\shipout\box255`, `\relax`, `\message{...}` and //! `\ifdim` comparisons over the page registers `\pagegoal`, `\pagetotal`, -//! `\vsize`, `\topskip` and `\maxdepth`. Anything else is a parse error, which +//! `\pagedepth`, `\vsize`, `\topskip` and `\maxdepth`. Anything else is a parse error, which //! makes `Engine::layout_paged` fall back to the scrolling builder. use std::fmt; @@ -15,6 +15,8 @@ pub struct OutputContext { pub page_goal: i64, /// TeX `\pagetotal`: the height accumulated on the current page. pub page_total: i64, + /// TeX `\pagedepth`: the depth of the page box so far. + pub page_depth: i64, /// TeX `\vsize`: the configured page height. pub vsize: i64, /// TeX `\topskip`. @@ -96,6 +98,7 @@ enum Dimension { enum Register { PageGoal, PageTotal, + PageDepth, Vsize, TopSkip, MaxDepth, @@ -108,6 +111,7 @@ impl Dimension { Self::Register(register) => match register { Register::PageGoal => context.page_goal, Register::PageTotal => context.page_total, + Register::PageDepth => context.page_depth, Register::Vsize => context.vsize, Register::TopSkip => context.top_skip, Register::MaxDepth => context.max_depth, @@ -473,6 +477,7 @@ fn parse_dimension(tokens: &[Token], index: &mut usize) -> Result Register::PageGoal, "pagetotal" => Register::PageTotal, + "pagedepth" => Register::PageDepth, "vsize" => Register::Vsize, "topskip" => Register::TopSkip, "maxdepth" => Register::MaxDepth, @@ -560,6 +565,7 @@ mod tests { OutputContext { page_goal: 612 * 65_536, page_total, + page_depth: 0, vsize: 612 * 65_536, top_skip: 10 * 65_536, max_depth: 0, @@ -575,6 +581,15 @@ mod tests { assert_eq!(program.shipout_count(&context(0)).unwrap(), 1); } + #[test] + fn page_depth_register_is_visible() { + let program = OutputProgram::parse(r"\ifdim\pagedepth>4pt\shipout\box255\fi").unwrap(); + let mut deep = context(0); + deep.page_depth = 5 * 65_536; + assert_eq!(program.shipout_count(&deep).unwrap(), 1); + assert_eq!(program.shipout_count(&context(0)).unwrap(), 0); + } + #[test] fn conditional_ships_only_large_pages() { let program = OutputProgram::parse(r"\ifdim\pagetotal>50pt\shipout\box255\fi").unwrap(); diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index 5787831..586dae5 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`). 353 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 355 workspace tests are green and no oracle fixture is ignored. Known gaps, in dependency order: @@ -237,7 +237,7 @@ Scope: - glue setting in vertical lists (ragged/flush bottom), `\vsize`, `\voffset`, `\topskip`, `\maxdepth`, `\splittopskip`, `\splitmaxdepth`; - TeX page builder: candidate breakpoints, cost = badness + penalty, page goal - recomputation as insertions arrive, `\pagegoal`, `\pagetotal`, + recomputation as insertions arrive, `\pagegoal`, `\pagetotal`, `\pagedepth`, `\pagestretch`, `\pagefilstretch`, `\pagefillstretch`, `\pagefilllstretch`, `\pageshrink`, `\pagedepth`; - marks: `\mark`, `\botmark`, `\firstmark`, `\topmark`, `\marks` (marks are diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index eb93e25..22af869 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -239,9 +239,11 @@ oracle fixture is ignored. in the page hash. `OutputRoutine::shipout` runs before sealing and may mutate each page; this models the `\output` stage in Rust. The interpreted subset lives in `output::OutputProgram` (`Document::output_routine`), which - understands `\shipout\box255`, `\relax`, `\message{...}` and `\ifdim` / - `\else` / `\fi` over the page registers `\pagegoal`, `\pagetotal`, `\vsize`, - `\topskip` and `\maxdepth`; it cannot yet change page goals. Top `Float` + understands `\shipout\box255`, `\relax`, `\message{...}`, `\ifdim`, + `\ifnum`, `\ifodd` and `\ifcase` over the page registers `\pagegoal`, + `\pagetotal`, `\pagedepth`, `\vsize`, `\topskip`, `\maxdepth`, + `\outputpenalty`, `\pageno` and `\deadcycles`; it cannot yet change page + goals. Top `Float` material is deferred to the next page when it arrives late; bottom floats reserve height on the current page and must satisfy `Document.text_fraction_percent` (default 50%); a page holding only floats @@ -332,7 +334,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 235 unit tests plus seven route-D integration tests and thirteen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 353 tests (235 route-D unit, 7 route-D integration, 13 oracle, 71 v1 integration, 20 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. +The crate currently has 237 unit tests plus seven route-D integration tests and thirteen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 355 tests (237 route-D unit, 7 route-D integration, 13 oracle, 71 v1 integration, 20 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. ### M0 incremental demo