diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 21b4074..ee39abc 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -610,6 +610,9 @@ pub struct Document { /// TeX `\paperheight`: height of the output page. #[serde(default = "default_paper_height")] pub paper_height: i64, + /// TeX `\hsize`: width of the page box handed to the output routine. + #[serde(default = "default_text_width")] + pub text_width: i64, /// TeX-like `\topskip`: distance from the page top to the first baseline. #[serde(default)] pub top_skip: i64, @@ -705,6 +708,10 @@ fn default_paper_height() -> i64 { 792 * 65_536 } +fn default_text_width() -> i64 { + 345 * 65_536 +} + fn default_text_fraction() -> u8 { 50 } @@ -765,6 +772,7 @@ impl Document { page_breaks: Vec::new(), paper_width: default_paper_width(), paper_height: default_paper_height(), + text_width: default_text_width(), top_skip: 0, max_depth: 0, text_fraction_percent: default_text_fraction(), @@ -2148,6 +2156,7 @@ impl Engine { page_filll_stretch: page_filll_stretches.get(index).copied().unwrap_or(0), page_shrink: page_shrinks.get(index).copied().unwrap_or(0), vbadness: page_badnesses.get(index).copied().unwrap_or(10_000), + hsize: document.text_width, vsize: document.page_goal, top_skip: document.top_skip, max_depth: document.max_depth, @@ -2349,6 +2358,7 @@ impl Engine { page_filll_stretch: fragment.stretches[3], page_shrink: fragment.shrink, vbadness: fragment.badness, + hsize: document.text_width, vsize: goal, top_skip: document.top_skip, max_depth: document.max_depth, @@ -6508,6 +6518,20 @@ mod tests { assert_eq!(dropped.pages().len(), 0); } + #[test] + fn output_routine_sees_the_text_width() { + let rule = Unit::rule(1, 100 * 65_536, 10 * 65_536); + let mut document = Document::new(100 * 65_536, vec![rule]); + document.text_width = 320 * 65_536; + document.output_routine = Some(r"\ifdim\hsize=320pt\shipout\box255\fi".to_owned()); + let kept = Engine::layout_paged(&document).expect("rule is page-buildable"); + assert_eq!(kept.pages().len(), 1); + + document.output_routine = Some(r"\ifdim\wd255=319pt\shipout\box255\fi".to_owned()); + let dropped = Engine::layout_paged(&document).expect("rule is page-buildable"); + assert_eq!(dropped.pages().len(), 0); + } + #[test] fn paged_layout_expands_multi_item_units_and_places_insertions() { let stacked = Document::new( diff --git a/crates/texrra-tex-core/src/output.rs b/crates/texrra-tex-core/src/output.rs index e98a3d1..6634038 100644 --- a/crates/texrra-tex-core/src/output.rs +++ b/crates/texrra-tex-core/src/output.rs @@ -32,6 +32,8 @@ pub struct OutputContext { pub page_shrink: i64, /// TeX `\vbadness`: badness of the break that produced the page. pub vbadness: i64, + /// TeX `\hsize`: width of the page box handed to the output routine. + pub hsize: i64, /// TeX `\vsize`: the configured page height. pub vsize: i64, /// TeX `\topskip`. @@ -70,6 +72,7 @@ impl Default for OutputContext { page_fill_stretch: 0, page_filll_stretch: 0, page_shrink: 0, + hsize: 0, vbadness: 0, vsize: 0, top_skip: 0, @@ -242,6 +245,7 @@ fn parse_arithmetic_target( let class = parse_insertion_class(tokens, index, "\\skip", true)?; Ok(ArithmeticTarget::Dimension(Register::InsertionSkip(class))) } + "hsize" => Ok(ArithmeticTarget::Dimension(Register::Hsize)), "vsize" => Ok(ArithmeticTarget::Dimension(Register::Vsize)), other => Err(format!("unsupported arithmetic target \\{other}")), } @@ -261,9 +265,12 @@ enum Register { BoxHeight255, /// TeX `\dp255`: depth of the page box handed to the output routine. BoxDepth255, + /// TeX `\wd255`: width of the page box handed to the output routine. + BoxWidth255, Vsize, TopSkip, MaxDepth, + Hsize, InsertionDimen(u8), InsertionSkip(u8), } @@ -283,9 +290,11 @@ impl Dimension { Register::PageShrink => context.page_shrink, Register::BoxHeight255 => context.page_total, Register::BoxDepth255 => context.page_depth, + Register::BoxWidth255 => context.hsize, Register::Vsize => context.vsize, Register::TopSkip => context.top_skip, Register::MaxDepth => context.max_depth, + Register::Hsize => context.hsize, Register::InsertionDimen(class) => context.insertion_dimens[class as usize], Register::InsertionSkip(class) => context.insertion_skips[class as usize], }, @@ -807,6 +816,12 @@ fn parse_dimension(tokens: &[Token], index: &mut usize) -> Result { + *index += 1; + expect_box_255(tokens, index, "\\wd")?; + return Ok(Dimension::Register(Register::BoxWidth255)); + } + "hsize" => Register::Hsize, "vsize" => Register::Vsize, "topskip" => Register::TopSkip, "maxdepth" => Register::MaxDepth, @@ -995,6 +1010,7 @@ fn write_dimension( context.insertion_skips[class as usize] = value; } Register::Vsize => context.vsize = value, + Register::BoxWidth255 | Register::Hsize => context.hsize = value, other => return Err(format!("cannot assign to page register {other:?}")), } Ok(()) @@ -1117,6 +1133,17 @@ mod tests { assert_eq!(context.pageno, 6); } + #[test] + fn output_routine_reads_hsize_and_the_page_box_width() { + let program = OutputProgram::parse( + "\\ifdim\\hsize=480pt\\shipout\\box255\\fi\\ifdim\\wd255=480pt\\shipout\\box255\\fi", + ) + .unwrap(); + let mut context = context(10 * 65_536); + context.hsize = 480 * 65_536; + assert_eq!(program.shipout_count(&mut context).unwrap(), 2); + } + #[test] fn output_routine_can_assign_registers() { let mut context = context(0); diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index a25d1af..0bd7862 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`). 467 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 469 workspace tests are green and no oracle fixture is ignored. Known gaps, in dependency order: @@ -84,7 +84,7 @@ source-level `\hyphenation{...}` exceptions and `\parshape` measures, 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, `\ht255`/`\dp255` expose the page + `\outputpenalty` and those insertion registers, `\ht255`/`\dp255`/`\wd255` expose the page 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 diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index fb85ab5..5e4e90d 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` and `\dp255` expose the page box height and depth to the routine, 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, `\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 for the duration of the routine, `\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 310 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 467 tests (310 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 312 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 469 tests (312 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 diff --git a/src/route_d.rs b/src/route_d.rs index 581a13b..c3578ae 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -863,6 +863,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do let mut document = Document::new(700 * SCALE, units); // LaTeX sets \maxdepth=.5\topskip; sources may override it later. document.max_depth = 5 * SCALE; + document.text_width = 480 * SCALE; document.page_breaks = paragraphs .iter() .enumerate()