diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 1cd3889..5758f14 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -172,12 +172,31 @@ impl Layout { } } +/// Shipout hook corresponding to TeX's `\output` stage. +/// +/// This is deliberately small: the routine can inspect and mutate each +/// assembled page (and later change page styles) but it is Rust code rather +/// than an interpreted token list. +pub trait OutputRoutine { + fn shipout(&mut self, page: &mut Page, page_number: usize); +} + +pub struct NoOutput; + +impl OutputRoutine for NoOutput { + fn shipout(&mut self, _page: &mut Page, _page_number: usize) {} +} + pub struct Engine { layout: Layout, } impl Engine { pub fn layout(document: &Document) -> Self { + Self::layout_with_output(document, &mut NoOutput) + } + + pub fn layout_with_output(document: &Document, output: &mut impl OutputRoutine) -> Self { let mut pages = Vec::new(); let mut checkpoints = Vec::with_capacity(document.units.len()); let mut current = Page::empty(); @@ -247,7 +266,8 @@ impl Engine { page.sources.push((unit.id, source_hash(&unit.source))); } } - for page in &mut pages { + for (index, page) in pages.iter_mut().enumerate() { + output.shipout(page, index + 1); page.seal_in_place(); } Self { @@ -567,6 +587,31 @@ mod tests { ); } + struct PageStamp { + numbers: Vec, + } + + impl OutputRoutine for PageStamp { + fn shipout(&mut self, page: &mut Page, page_number: usize) { + self.numbers.push(page_number); + page.marks.push(Mark { + class: 9, + text: format!("page {page_number}"), + }); + } + } + + #[test] + fn output_routine_sees_and_mutates_each_page() { + let mut stamp = PageStamp { + numbers: Vec::new(), + }; + let engine = Engine::layout_with_output(&document(), &mut stamp); + assert_eq!(stamp.numbers.len(), engine.layout.pages.len()); + assert_eq!(stamp.numbers[0], 1); + assert_eq!(engine.layout.pages[0].marks[0].text, "page 1"); + } + #[test] fn checkpoint_round_trips_through_serde() { let engine = Engine::layout(&document()); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index f417e15..04a94ce 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -71,7 +71,7 @@ next paragraph milestones. | --- | --- | --- | | 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, pdfTeX fixture and minimum line fill; hyphenation next | -| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, page-bottom `Insertion` reserve, running `Mark`s; `\output`/float placements next | +| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, Rust `OutputRoutine` shipout hook; interpreted `\output`, float placements next | | M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | table width/rules match reference | | M4 | math lists, scripts, fractions, radicals, accents, delimiters | arXiv formulas render without overlap | | M5 | NFSS/TFM metrics, CM/Latin Modern | same line/page breaks as reference | @@ -106,8 +106,10 @@ a single cost function. but not yet part of page badness. `VerticalItem::Insertion` material is attached to `Page::insertions` and reserves height from the page goal. `VerticalItem::Mark` material is collected into `Page::marks` and included - in the page hash, as a base for running headers. There is no executable - `\output` routine and no `\insert`/float wiring yet. + in the page hash. `OutputRoutine::shipout` runs before sealing and may + mutate each page; this models the `\output` stage in Rust rather than as + an interpreted token list, and cannot yet change page goals or place + floats. - No macro state, output routine, math, fonts or PDF output in the new core yet. - The v1 server and project API still use `src/rust_typeset` and are unchanged.