diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index a20bb6c..20041ca 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1872,6 +1872,7 @@ impl Engine { let program = output::OutputProgram::parse(source).ok()?; let mut shipped = Vec::new(); let mut deadcycles = 0_i64; + let mut pageno = 1_i64; for (index, page) in pages.into_iter().enumerate() { let page_total: i64 = page.items.iter().map(item_height).sum(); let mut insertion_counts = [1000_i64; 256]; @@ -1883,7 +1884,7 @@ impl Engine { insertion_dimens[slot] = class.max_height.unwrap_or(0); insertion_skips[slot] = class.skip.unwrap_or(document.insertion_skip); } - let context = output::OutputContext { + let mut context = output::OutputContext { page_goal: page_goals.get(index).copied().unwrap_or(document.page_goal), page_total, page_depth: page_depths.get(index).copied().unwrap_or(0), @@ -1899,10 +1900,13 @@ impl Engine { insertion_counts, insertion_dimens, insertion_skips, - pageno: index as i64 + 1, + pageno, deadcycles, }; - if program.shipout_count(&context).ok()? > 0 { + let shipped_now = program.shipout_count(&mut context).ok()?; + pageno = context.pageno.wrapping_add(1); + deadcycles = context.deadcycles; + if shipped_now > 0 { shipped.push(page); deadcycles = 0; } else { @@ -2744,6 +2748,24 @@ mod tests { assert!(engine.pages().is_empty()); } + #[test] + fn output_routine_can_rewrite_the_page_number() { + let mut document = Document::new( + 100, + vec![ + Unit::rule(1, 10, 10), + Unit::penalty(2, -10_000), + Unit::rule(3, 10, 10), + Unit::penalty(4, -10_000), + Unit::rule(5, 10, 10), + ], + ); + document.output_routine = + Some(r"\ifnum\pageno=1\pageno=10\fi\ifnum\pageno=11\shipout\box255\fi".to_owned()); + let engine = Engine::layout_paged(&document).expect("paged"); + assert_eq!(engine.pages().len(), 1); + } + #[test] fn output_routine_can_force_an_early_page_break() { struct BreakAfterTwo { diff --git a/crates/texrra-tex-core/src/output.rs b/crates/texrra-tex-core/src/output.rs index 8a7a71e..c910e05 100644 --- a/crates/texrra-tex-core/src/output.rs +++ b/crates/texrra-tex-core/src/output.rs @@ -3,9 +3,11 @@ //! 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`, -//! `\pagedepth`, `\pagestretch`, `\pageshrink`, `\vsize`, `\topskip` and -//! `\maxdepth`. Anything else is a parse error, which -//! makes `Engine::layout_paged` fall back to the scrolling builder. +//! `\pagedepth`, `\pagestretch`, `\pageshrink`, `\vsize`, `\topskip`, +//! `\maxdepth`, the insertion registers `\count`/`\dimen`/`\skip`, +//! and assignments such as `\pageno=1` or `\count2=2000`. Anything else is a +//! parse error, which makes `Engine::layout_paged` fall back to the scrolling +//! builder. use std::fmt; @@ -92,7 +94,7 @@ impl OutputProgram { /// Execute the program for one page and return how many times it called /// `\shipout`. - pub fn shipout_count(&self, context: &OutputContext) -> Result { + pub fn shipout_count(&self, context: &mut OutputContext) -> Result { let mut shipped = 0; execute(&self.ops, context, &mut shipped)?; Ok(shipped) @@ -128,6 +130,14 @@ enum Op { branches: Vec>, else_ops: Vec, }, + AssignInteger { + register: IntegerRegister, + value: Integer, + }, + AssignDimension { + register: Register, + value: Dimension, + }, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -373,6 +383,34 @@ fn parse_sequence(tokens: &[Token], index: &mut usize, stop: &[&str]) -> Result< } ops.push(Op::Message(text)); } + "pageno" | "deadcycles" | "outputpenalty" | "count" | "dimen" | "skip" => { + *index += 1; + let (integer_register, dimension_register) = match name.as_str() { + "pageno" => (Some(IntegerRegister::PageNo), None), + "deadcycles" => (Some(IntegerRegister::DeadCycles), None), + "outputpenalty" => (Some(IntegerRegister::OutputPenalty), None), + "count" => { + let class = parse_insertion_class(tokens, index, "\\count", false)?; + (Some(IntegerRegister::InsertionCount(class)), None) + } + "dimen" => { + let class = parse_insertion_class(tokens, index, "\\dimen", true)?; + (None, Some(Register::InsertionDimen(class))) + } + _ => { + let class = parse_insertion_class(tokens, index, "\\skip", true)?; + (None, Some(Register::InsertionSkip(class))) + } + }; + expect_assignment(tokens, index, name)?; + if let Some(register) = integer_register { + let value = parse_integer(tokens, index)?; + ops.push(Op::AssignInteger { register, value }); + } else if let Some(register) = dimension_register { + let value = parse_dimension(tokens, index)?; + ops.push(Op::AssignDimension { register, value }); + } + } "ifdim" => { *index += 1; let left = parse_dimension(tokens, index)?; @@ -483,6 +521,16 @@ fn parse_sequence(tokens: &[Token], index: &mut usize, stop: &[&str]) -> Result< } } +fn expect_assignment(tokens: &[Token], index: &mut usize, command: &str) -> Result<(), String> { + match tokens.get(*index) { + Some(Token::Relation('=')) => { + *index += 1; + Ok(()) + } + _ => Err(format!("\\{command} expects an assignment")), + } +} + fn parse_relation(tokens: &[Token], index: &mut usize, command: &str) -> Result { let relation = match tokens.get(*index) { Some(Token::Relation('<')) => Relation::Less, @@ -598,7 +646,7 @@ fn parse_insertion_class( } } -fn execute(ops: &[Op], context: &OutputContext, shipped: &mut usize) -> Result<(), String> { +fn execute(ops: &[Op], context: &mut OutputContext, shipped: &mut usize) -> Result<(), String> { for op in ops { match op { Op::Shipout => *shipped += 1, @@ -659,6 +707,31 @@ fn execute(ops: &[Op], context: &OutputContext, shipped: &mut usize) -> Result<( let odd = value.resolve(context).rem_euclid(2) == 1; execute(if odd { then_ops } else { else_ops }, context, shipped)?; } + Op::AssignInteger { register, value } => { + let value = value.resolve(context); + match register { + IntegerRegister::PageNo => context.pageno = value, + IntegerRegister::DeadCycles => context.deadcycles = value, + IntegerRegister::OutputPenalty => context.output_penalty = value, + IntegerRegister::InsertionCount(class) => { + context.insertion_counts[*class as usize] = value; + } + } + } + Op::AssignDimension { register, value } => { + let value = value.resolve(context); + match register { + Register::InsertionDimen(class) => { + context.insertion_dimens[*class as usize] = value; + } + Register::InsertionSkip(class) => { + context.insertion_skips[*class as usize] = value; + } + other => { + return Err(format!("cannot assign to page register {other:?}")); + } + } + } } } Ok(()) @@ -691,7 +764,7 @@ mod tests { #[test] fn ships_the_page_box() { let program = OutputProgram::parse(r"\shipout\box255").unwrap(); - assert_eq!(program.shipout_count(&context(0)).unwrap(), 1); + assert_eq!(program.shipout_count(&mut context(0)).unwrap(), 1); } #[test] @@ -699,14 +772,14 @@ mod tests { let program = OutputProgram::parse(r"\ifdim\pagestretch>9pt\shipout\box255\fi").unwrap(); let mut stretched = context(0); stretched.page_stretch = 10 * 65_536; - assert_eq!(program.shipout_count(&stretched).unwrap(), 1); - assert_eq!(program.shipout_count(&context(0)).unwrap(), 0); + assert_eq!(program.shipout_count(&mut stretched).unwrap(), 1); + assert_eq!(program.shipout_count(&mut context(0)).unwrap(), 0); let shrink = OutputProgram::parse(r"\ifdim\pageshrink>9pt\shipout\box255\fi").unwrap(); let mut shrunk = context(0); shrunk.page_shrink = 10 * 65_536; - assert_eq!(shrink.shipout_count(&shrunk).unwrap(), 1); - assert_eq!(shrink.shipout_count(&context(0)).unwrap(), 0); + assert_eq!(shrink.shipout_count(&mut shrunk).unwrap(), 1); + assert_eq!(shrink.shipout_count(&mut context(0)).unwrap(), 0); } #[test] @@ -714,18 +787,18 @@ mod tests { let program = OutputProgram::parse(r"\ifdim\pagefilstretch>9pt\shipout\box255\fi").unwrap(); let mut context = context(0); context.page_fil_stretch = 10 * 65_536; - assert_eq!(program.shipout_count(&context).unwrap(), 1); - assert_eq!(program.shipout_count(&self::context(0)).unwrap(), 0); + assert_eq!(program.shipout_count(&mut context).unwrap(), 1); + assert_eq!(program.shipout_count(&mut self::context(0)).unwrap(), 0); let fill = OutputProgram::parse(r"\ifdim\pagefillstretch>4pt\shipout\box255\fi").unwrap(); let mut context = self::context(0); context.page_fill_stretch = 5 * 65_536; - assert_eq!(fill.shipout_count(&context).unwrap(), 1); + assert_eq!(fill.shipout_count(&mut context).unwrap(), 1); let filll = OutputProgram::parse(r"\ifdim\pagefilllstretch>4pt\shipout\box255\fi").unwrap(); let mut context = self::context(0); context.page_filll_stretch = 5 * 65_536; - assert_eq!(filll.shipout_count(&context).unwrap(), 1); + assert_eq!(filll.shipout_count(&mut context).unwrap(), 1); } #[test] @@ -733,8 +806,28 @@ mod tests { 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); + assert_eq!(program.shipout_count(&mut deep).unwrap(), 1); + assert_eq!(program.shipout_count(&mut context(0)).unwrap(), 0); + } + + #[test] + fn output_routine_can_assign_registers() { + let mut context = context(0); + context.pageno = 7; + let program = OutputProgram::parse(r"\pageno=3\ifnum\pageno=3\shipout\box255\fi").unwrap(); + assert_eq!(program.shipout_count(&mut context).unwrap(), 1); + assert_eq!(context.pageno, 3); + + let counts = + OutputProgram::parse(r"\count2=1500\ifnum\count2=1000\shipout\box255\fi").unwrap(); + let mut context = self::context(0); + assert_eq!(counts.shipout_count(&mut context).unwrap(), 0); + assert_eq!(context.insertion_counts[2], 1500); + + let skip = OutputProgram::parse(r"\skip2=5pt\ifdim\skip2>4pt\shipout\box255\fi").unwrap(); + let mut context = self::context(0); + assert_eq!(skip.shipout_count(&mut context).unwrap(), 1); + assert_eq!(context.insertion_skips[2], 5 * 65_536); } #[test] @@ -745,24 +838,24 @@ mod tests { context.insertion_skips[1] = 3 * 65_536; let count = OutputProgram::parse(r"\ifnum\count1=2000\shipout\box255\fi").unwrap(); - assert_eq!(count.shipout_count(&context).unwrap(), 1); - assert_eq!(count.shipout_count(&self::context(0)).unwrap(), 0); + assert_eq!(count.shipout_count(&mut context).unwrap(), 1); + assert_eq!(count.shipout_count(&mut self::context(0)).unwrap(), 0); let dimen = OutputProgram::parse(r"\ifdim\dimen1>6pt\shipout\box255\fi").unwrap(); - assert_eq!(dimen.shipout_count(&context).unwrap(), 1); + assert_eq!(dimen.shipout_count(&mut context).unwrap(), 1); let skip = OutputProgram::parse(r"\ifdim\skip1<4pt\shipout\box255\fi").unwrap(); - assert_eq!(skip.shipout_count(&context).unwrap(), 1); + assert_eq!(skip.shipout_count(&mut context).unwrap(), 1); let pageno = OutputProgram::parse(r"\ifnum\count0=1\shipout\box255\fi").unwrap(); - assert_eq!(pageno.shipout_count(&context).unwrap(), 1); + assert_eq!(pageno.shipout_count(&mut context).unwrap(), 1); } #[test] fn conditional_ships_only_large_pages() { let program = OutputProgram::parse(r"\ifdim\pagetotal>50pt\shipout\box255\fi").unwrap(); - assert_eq!(program.shipout_count(&context(40 * 65_536)).unwrap(), 0); - assert_eq!(program.shipout_count(&context(60 * 65_536)).unwrap(), 1); + assert_eq!(program.shipout_count(&mut context(40 * 65_536)).unwrap(), 0); + assert_eq!(program.shipout_count(&mut context(60 * 65_536)).unwrap(), 1); } #[test] @@ -772,37 +865,37 @@ mod tests { .unwrap(); let mut small = context(40 * 65_536); small.page_total = 40 * 65_536; - assert_eq!(program.shipout_count(&small).unwrap(), 1); - assert_eq!(program.shipout_count(&context(70 * 65_536)).unwrap(), 1); + assert_eq!(program.shipout_count(&mut small).unwrap(), 1); + assert_eq!(program.shipout_count(&mut context(70 * 65_536)).unwrap(), 1); } #[test] fn page_goal_register_is_visible() { let program = OutputProgram::parse(r"\ifdim\pagegoal<600pt\shipout\box255\fi").unwrap(); - assert_eq!(program.shipout_count(&context(0)).unwrap(), 0); + assert_eq!(program.shipout_count(&mut context(0)).unwrap(), 0); let mut small_page = context(0); small_page.page_goal = 500 * 65_536; - assert_eq!(program.shipout_count(&small_page).unwrap(), 1); + assert_eq!(program.shipout_count(&mut small_page).unwrap(), 1); } #[test] fn ifnum_reads_page_and_penalty_registers() { let program = OutputProgram::parse(r"\ifnum\pageno>1\shipout\box255\fi").unwrap(); let mut context = context(0); - assert_eq!(program.shipout_count(&context).unwrap(), 0); + assert_eq!(program.shipout_count(&mut context).unwrap(), 0); context.pageno = 2; - assert_eq!(program.shipout_count(&context).unwrap(), 1); + assert_eq!(program.shipout_count(&mut context).unwrap(), 1); } #[test] fn ifnum_reads_negative_output_penalty() { let program = OutputProgram::parse(r"\ifnum\outputpenalty<-9999\shipout\box255\fi").unwrap(); - let context = context(0); - assert_eq!(program.shipout_count(&context).unwrap(), 0); + let mut context = context(0); + assert_eq!(program.shipout_count(&mut context).unwrap(), 0); let mut forced = context; forced.output_penalty = -10_000; - assert_eq!(program.shipout_count(&forced).unwrap(), 1); + assert_eq!(program.shipout_count(&mut forced).unwrap(), 1); } #[test] @@ -810,9 +903,9 @@ mod tests { let program = OutputProgram::parse(r"\ifodd\pageno\shipout\box255\else\relax\fi").unwrap(); let mut context = context(0); context.pageno = 1; - assert_eq!(program.shipout_count(&context).unwrap(), 1); + assert_eq!(program.shipout_count(&mut context).unwrap(), 1); context.pageno = 2; - assert_eq!(program.shipout_count(&context).unwrap(), 0); + assert_eq!(program.shipout_count(&mut context).unwrap(), 0); } #[test] @@ -821,11 +914,11 @@ mod tests { OutputProgram::parse(r"\ifcase\pageno\relax\or\shipout\box255\else\relax\fi").unwrap(); let mut context = context(0); context.pageno = 1; - assert_eq!(program.shipout_count(&context).unwrap(), 1); + assert_eq!(program.shipout_count(&mut context).unwrap(), 1); context.pageno = 0; - assert_eq!(program.shipout_count(&context).unwrap(), 0); + assert_eq!(program.shipout_count(&mut context).unwrap(), 0); context.pageno = 5; - assert_eq!(program.shipout_count(&context).unwrap(), 0); + assert_eq!(program.shipout_count(&mut context).unwrap(), 0); } #[test] diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index efd25c5..5db63c6 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`). 402 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 404 workspace tests are green and no oracle fixture is ignored. Known gaps, in dependency order: @@ -73,10 +73,12 @@ yet generic `fil`/`fill` glue float placement goes through the paged builder, and the interpreted `\output` subset handles `\shipout`, `\ifdim` and `\ifnum` over page registers such as `\pageno`, `\outputpenalty` and `\deadcycles`; -3. `\insert` classes model `\count`/`\dimen`/`\skip` but are not TeX - registers addressed by number; the interpreted output routine covers page - shipping and `\ifdim`/`\ifnum` comparisons but not the full `\output` - machinery; +3. `\insert` classes are TeX registers addressed by number (`\count`, + `\dimen`, `\skip`, with `\count0` aliasing `\pageno`) and the + interpreted output routine can assign `\pageno`, `\deadcycles`, + `\outputpenalty` and those insertion registers for the current page, but + writes do not yet feed back into page building, `\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 1b96dd3..198fcba 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -116,7 +116,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` and vertical-glue fixtures; generic fil glue and `\prevgraf` next | -| 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 or an interpreted `\output` are present; `\output` is interpreted for a `\shipout`/`\ifdim`/`\ifnum`/`\ifodd`/`\ifcase` subset; `\pageno`, `\outputpenalty` and `\deadcycles` are readable registers, insertion class registers `\count`/`\dimen`/`\skip` read the configured multiplier, maximum height and skip of class `n` (with `\count0` reading `\pageno`), `\deadcycles` accumulates pages the output routine did not ship and resets on the next shipout, `PageFragment::penalty` carries the break penalty, and `PageFragment::stretches` records glue stretch per TeX order so `\pagefilstretch`/`\pagefillstretch`/`\pagefilllstretch` are readable in the output routine | +| 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 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), insertion class registers `\count`/`\dimen`/`\skip` read the configured multiplier, maximum height and skip of class `n` (with `\count0` reading `\pageno`) and accept assignments for the duration of the routine, `\deadcycles` accumulates pages the output routine did not ship and resets on the next shipout, `PageFragment::penalty` carries the break penalty, and `PageFragment::stretches` records glue stretch per TeX order so `\pagefilstretch`/`\pagefillstretch`/`\pagefilllstretch` are readable in the output routine | | 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 | @@ -338,7 +338,7 @@ M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments. -M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, respect `top_fraction_percent` (TeX `\topfraction`, default 70%) and `bottom_fraction_percent` (TeX `\bottomfraction`, default 30%), and can take a dedicated float page. The incremental edit path applies the same float fractions, float number limits, insertion-class limits and per-class `skip` rules as full layout. `Document::float_sep` (TeX `\floatsep`, default 12pt) reserves a gap between stacked floats of the same placement in both paths. `Document::text_float_sep` (TeX `\textfloatsep`, default 20pt) reserves exactly one gap between the float block and the body text on a page that has both, again in both paths; `Page::display_list_with_layout(page_goal, text_float_sep)` applies the same gap when rendering, while `display_list_with_goal` stays gap-free for callers that do not know the document settings. Float material at the top of a page starts at the top margin: `\topskip` is consumed only by body text, matching TeX float pages. `FloatPlacement::Here` models `[h]` floats: they keep their place in the body flow with `Document::in_text_sep` (TeX `\intextsep`, default 12pt) above and below, move to the next page when they do not fit, and count against `\totalnumber`. The page stores the float's `gap_after` so the display list advances the flow by the same amount the builder reserved. A standalone `page::PageBuilder` implements TeX's candidate-breakpoint cost model (page badness from accumulated glue stretch/shrink plus break penalties, `\topskip` for the first box, `\maxdepth` depth shifting) with unit tests covering overfull avoidance, exact glue filling, forced and costed breaks, `\topskip`, `\maxdepth`, marks, insertion limits and top-float placement; every fragment records the penalty that broke it. `Engine::layout_paged` now builds pages through it for documents whose units are single vertical items (boxes, rules, paragraphs, math, haligns, glue, penalties, marks and insertion classes, including per-class limits and held-over material; held-over material starts with `Document::split_top_skip` (TeX `\splittopskip`, default 10pt) when the class capacity can also hold its first item); `Document::max_depth` feeds TeX's `\maxdepth` into the builder, and among equal-cost breakpoints the candidate closest to the page goal wins, so an overfull page takes the least overfull break. Paragraphs also split across pages at line boundaries, so body material no longer overflows a page as one box. `Document::h_offset` and `v_offset` (TeX `\hoffset`/`\voffset`, default zero) feed `pdf::PageOrigin`, and `display_lists_to_pdf_with_paper` translates the page body without moving the sheet; a route-D fixture checks a 10pt/20pt shift against `pdftotext -bbox`. `Document::paper_width` and `paper_height` (TeX `\paperwidth`/`\paperheight`, default US Letter) set the PDF MediaBox and the preview page box, and both route-D endpoints strip leading `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight` dimension assignments from the source and apply them. `Engine::top_mark`, `first_mark` and `bot_mark` implement TeX's carried mark registers per class. `Document::output_routine` runs an interpreted `\output` program over the paged fragments (`output::OutputProgram`: `\shipout\box255`, `\relax`, `\message`, `\ifdim`, `\ifnum`, `\ifodd` and `\ifcase` over the page registers, now including `\pageno`, `\outputpenalty` and `\deadcycles`), dropping pages the routine does not ship. The `\pagegoal` register handed to the output routine is reduced by the insertion space reserved on that page. Paged layouts record the node index that starts each page, so `Engine::edit` on a paged layout reuses the sealed prefix and rebuilds only from the edited unit's page onward; an interpreted `\output` routine still forces a full rebuild, and an edit whose replay would begin before the first insertion does too, because held-over material and dropped pages move page boundaries. The scrolling builder's float path remains to be ported. The oracle harness also compares whole pages: three page fixtures (single-line paragraphs, `\parskip=6pt`, and multi-line paragraphs split across pages) assert the same page count and the same per-page alphanumeric text as pdfLaTeX at 345pt measure, 550pt `\vsize`, 12pt `\topskip` and 15pt `\parindent`. +M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, respect `top_fraction_percent` (TeX `\topfraction`, default 70%) and `bottom_fraction_percent` (TeX `\bottomfraction`, default 30%), and can take a dedicated float page. The incremental edit path applies the same float fractions, float number limits, insertion-class limits and per-class `skip` rules as full layout. `Document::float_sep` (TeX `\floatsep`, default 12pt) reserves a gap between stacked floats of the same placement in both paths. `Document::text_float_sep` (TeX `\textfloatsep`, default 20pt) reserves exactly one gap between the float block and the body text on a page that has both, again in both paths; `Page::display_list_with_layout(page_goal, text_float_sep)` applies the same gap when rendering, while `display_list_with_goal` stays gap-free for callers that do not know the document settings. Float material at the top of a page starts at the top margin: `\topskip` is consumed only by body text, matching TeX float pages. `FloatPlacement::Here` models `[h]` floats: they keep their place in the body flow with `Document::in_text_sep` (TeX `\intextsep`, default 12pt) above and below, move to the next page when they do not fit, and count against `\totalnumber`. The page stores the float's `gap_after` so the display list advances the flow by the same amount the builder reserved. A standalone `page::PageBuilder` implements TeX's candidate-breakpoint cost model (page badness from accumulated glue stretch/shrink plus break penalties, `\topskip` for the first box, `\maxdepth` depth shifting) with unit tests covering overfull avoidance, exact glue filling, forced and costed breaks, `\topskip`, `\maxdepth`, marks, insertion limits and top-float placement; every fragment records the penalty that broke it. `Engine::layout_paged` now builds pages through it for documents whose units are single vertical items (boxes, rules, paragraphs, math, haligns, glue, penalties, marks and insertion classes, including per-class limits and held-over material; held-over material starts with `Document::split_top_skip` (TeX `\splittopskip`, default 10pt) when the class capacity can also hold its first item); `Document::max_depth` feeds TeX's `\maxdepth` into the builder, and among equal-cost breakpoints the candidate closest to the page goal wins, so an overfull page takes the least overfull break. Paragraphs also split across pages at line boundaries, so body material no longer overflows a page as one box. `Document::h_offset` and `v_offset` (TeX `\hoffset`/`\voffset`, default zero) feed `pdf::PageOrigin`, and `display_lists_to_pdf_with_paper` translates the page body without moving the sheet; a route-D fixture checks a 10pt/20pt shift against `pdftotext -bbox`. `Document::paper_width` and `paper_height` (TeX `\paperwidth`/`\paperheight`, default US Letter) set the PDF MediaBox and the preview page box, and both route-D endpoints strip leading `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight` dimension assignments from the source and apply them. `Engine::top_mark`, `first_mark` and `bot_mark` implement TeX's carried mark registers per class. `Document::output_routine` runs an interpreted `\output` program over the paged fragments (`output::OutputProgram`: `\shipout\box255`, `\relax`, `\message`, `\ifdim`, `\ifnum`, `\ifodd` and `\ifcase` over the page registers, now including `\pageno`, `\outputpenalty` and `\deadcycles`), dropping pages the routine does not ship. Assignments such as `\pageno=10`, `\deadcycles=0`, `\count2=2000`, `\dimen2=30pt` and `\skip2=6pt` mutate the per-page context, and `\pageno` is carried into the next page's context. The `\pagegoal` register handed to the output routine is reduced by the insertion space reserved on that page. Paged layouts record the node index that starts each page, so `Engine::edit` on a paged layout reuses the sealed prefix and rebuilds only from the edited unit's page onward; an interpreted `\output` routine still forces a full rebuild, and an edit whose replay would begin before the first insertion does too, because held-over material and dropped pages move page boundaries. The scrolling builder's float path remains to be ported. The oracle harness also compares whole pages: three page fixtures (single-line paragraphs, `\parskip=6pt`, and multi-line paragraphs split across pages) assert the same page count and the same per-page alphanumeric text as pdfLaTeX at 345pt measure, 550pt `\vsize`, 12pt `\topskip` and 15pt `\parindent`. M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the painter uses 10pt glyphs in 12pt rows. Rules and spans are painted; tall tables split at row boundaries (`Halign::split_for_height`) with column widths preserved. @@ -346,7 +346,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 278 unit tests plus seven route-D integration tests and seventeen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 402 tests (278 route-D unit, 7 route-D integration, 17 oracle, 71 v1 integration, 22 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. +The crate currently has 280 unit tests plus seven route-D integration tests and seventeen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 404 tests (280 route-D unit, 7 route-D integration, 17 oracle, 71 v1 integration, 22 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. ### M0 incremental demo