diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 24a22a6..8b58073 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1907,17 +1907,19 @@ impl Engine { let mut shipped = Vec::new(); let mut deadcycles = 0_i64; let mut pageno = 1_i64; + // TeX's output registers are global: assignments made by the + // routine on one page stay visible on later pages. + let mut insertion_counts = [1000_i64; 256]; + let mut insertion_dimens = [0_i64; 256]; + let mut insertion_skips = [0_i64; 256]; + for class in &document.insertion_classes { + let slot = class.number as usize; + insertion_counts[slot] = i64::from(class.multiplier); + insertion_dimens[slot] = class.max_height.unwrap_or(0); + insertion_skips[slot] = class.skip.unwrap_or(document.insertion_skip); + } 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]; - let mut insertion_dimens = [0_i64; 256]; - let mut insertion_skips = [0_i64; 256]; - for class in &document.insertion_classes { - let slot = class.number as usize; - insertion_counts[slot] = i64::from(class.multiplier); - insertion_dimens[slot] = class.max_height.unwrap_or(0); - insertion_skips[slot] = class.skip.unwrap_or(document.insertion_skip); - } let mut context = output::OutputContext { page_goal: page_goals.get(index).copied().unwrap_or(document.page_goal), page_total, @@ -1939,6 +1941,9 @@ impl Engine { prev_graf: page_prev_graf(&page), }; let shipped_now = program.shipout_count(&mut context).ok()?; + insertion_counts = context.insertion_counts; + insertion_dimens = context.insertion_dimens; + insertion_skips = context.insertion_skips; pageno = context.pageno.wrapping_add(1); deadcycles = context.deadcycles; if shipped_now > 0 { @@ -2071,6 +2076,16 @@ impl Engine { let mut pages = Vec::new(); let mut pageno = 1_i64; let mut deadcycles = 0_i64; + // Output registers are global in TeX; keep assignments between pages. + let mut insertion_counts = [1000_i64; 256]; + let mut insertion_dimens = [0_i64; 256]; + let mut insertion_skips = [0_i64; 256]; + for class in &document.insertion_classes { + let slot = class.number as usize; + insertion_counts[slot] = i64::from(class.multiplier); + insertion_dimens[slot] = class.max_height.unwrap_or(0); + insertion_skips[slot] = class.skip.unwrap_or(document.insertion_skip); + } while start < nodes.len() { builder.set_page_goal(goal); let fragment = builder.build_from(nodes, start).into_iter().next()?; @@ -2108,14 +2123,17 @@ impl Engine { top_skip: document.top_skip, max_depth: document.max_depth, output_penalty: fragment.penalty, - insertion_counts: [1000; 256], - insertion_dimens: [0; 256], - insertion_skips: [0; 256], + insertion_counts, + insertion_dimens, + insertion_skips, prev_graf: page_prev_graf(&page), pageno, deadcycles, }; let shipped = program.shipout_count(&mut context).ok()?; + insertion_counts = context.insertion_counts; + insertion_dimens = context.insertion_dimens; + insertion_skips = context.insertion_skips; pageno = context.pageno.wrapping_add(1); deadcycles = context.deadcycles; if shipped > 0 { @@ -5887,6 +5905,24 @@ mod tests { assert_eq!(page_prev_graf(&empty), 0); } + #[test] + fn output_register_assignments_persist_across_pages() { + let mut document = Document::new( + 100, + vec![ + Unit::rule(1, 10, 40), + Unit::penalty(2, -10_000), + Unit::rule(3, 10, 40), + ], + ); + document.output_routine = Some( + r"\ifnum\pageno=1\count2=1500\shipout\box255\else\ifnum\count2=1500\shipout\box255\fi\fi" + .into(), + ); + let engine = Engine::layout(&document); + assert_eq!(engine.pages().len(), 2); + } + #[test] fn output_routine_sees_the_page_depth() { let math = Unit { diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index a8af961..b56f82a 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`). 419 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 420 workspace tests are green and no oracle fixture is ignored. Known gaps, in dependency order: @@ -77,10 +77,10 @@ Known gaps, in dependency order: 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, and - an assigned `\vsize` changes later page goals for documents without - insertions or floats, but insertion-register writes still do not feed back - into page building, `\box255` content is not inspectable and the full + `\outputpenalty` and those insertion registers, 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 + insertion-register writes still do not 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`), diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 2050c92..71168d6 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -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`/`\vsize`/`\textheight` dimension assignments from the source (preamble included) and apply them; `\textheight` wins over `\vsize` because LaTeX resets `\vsize` from `\textheight` on every page, and project previews reuse the same geometry parsing. `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`, `\skip2=6pt` and `\vsize=300pt` mutate the per-page context, and `\pageno` is carried into the next page's context; for documents without insertions or floats the paged builder runs the routine after each page, so an assigned `\vsize` sets the goal of the pages the routine has not seen yet. 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: five page fixtures (single-line paragraphs, `\parskip=6pt`, multi-line paragraphs split across pages, a 300pt `\textheight`, and a 30pt `\topskip`) 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`/`\vsize`/`\textheight` dimension assignments from the source (preamble included) and apply them; `\textheight` wins over `\vsize` because LaTeX resets `\vsize` from `\textheight` on every page, and project previews reuse the same geometry parsing. `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`, `\skip2=6pt` and `\vsize=300pt` mutate the per-page context; `\pageno` and the insertion registers keep their assigned values for later pages, since TeX's output registers are global; for documents without insertions or floats the paged builder runs the routine after each page, so an assigned `\vsize` sets the goal of the pages the routine has not seen yet. 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: five page fixtures (single-line paragraphs, `\parskip=6pt`, multi-line paragraphs split across pages, a 300pt `\textheight`, and a 30pt `\topskip`) 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 286 unit tests plus seven route-D integration tests and twenty-four oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 419 tests (286 route-D unit, 7 route-D integration, 24 oracle, 71 v1 integration, 24 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. +The crate currently has 287 unit tests plus seven route-D integration tests and twenty-four oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 420 tests (287 route-D unit, 7 route-D integration, 24 oracle, 71 v1 integration, 24 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. ### M0 incremental demo