From 4c2aefd4908885332bd35179151c162ec3991349 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sat, 24 Oct 2026 01:42:49 +0800 Subject: [PATCH] Use CMEX size variants for big math delimiters --- crates/texrra-tex-core/src/cmr10.rs | 90 +++++++++++++++++++++++++++++ crates/texrra-tex-core/src/math.rs | 70 +++++++++++++++++----- docs/ROUTE_D_PLAN.md | 2 +- docs/TEX_CORE.md | 4 +- 4 files changed, 148 insertions(+), 18 deletions(-) diff --git a/crates/texrra-tex-core/src/cmr10.rs b/crates/texrra-tex-core/src/cmr10.rs index 17a764f..3446108 100644 --- a/crates/texrra-tex-core/src/cmr10.rs +++ b/crates/texrra-tex-core/src/cmr10.rs @@ -414,3 +414,93 @@ mod cmsy_tests { assert_eq!(Cmr10Metrics::Roman.width('\u{00D7}'), 327_680); } } + +/// Metrics for a CMEX delimiter size variant: size 1 = `\big`, 2 = `\Big`, +/// 3 = `\bigg`, 4 = `\Bigg`. +pub fn cmex_delimiter(delimiter: char, size: u8) -> Option<(i64, i64, i64)> { + let slot = match (delimiter, size) { + ('(', 1) => 1, + (')', 1) => 2, + ('[', 1) => 3, + (']', 1) => 4, + ('\u{230A}', 1) => 5, + ('\u{230B}', 1) => 6, + ('\u{2308}', 1) => 7, + ('\u{2309}', 1) => 8, + ('{', 1) => 9, + ('}', 1) => 10, + ('\u{27E8}', 1) => 11, + ('\u{27E9}', 1) => 12, + ('(', 2) => 17, + (')', 2) => 18, + ('[', 2) => 105, + (']', 2) => 106, + ('\u{230A}', 2) => 107, + ('\u{230B}', 2) => 108, + ('\u{2308}', 2) => 109, + ('\u{2309}', 2) => 110, + ('{', 2) => 111, + ('}', 2) => 112, + ('\u{27E8}', 2) => 69, + ('\u{27E9}', 2) => 70, + ('(', 3) => 19, + (')', 3) => 20, + ('[', 3) => 21, + (']', 3) => 22, + ('\u{230A}', 3) => 23, + ('\u{230B}', 3) => 24, + ('\u{2308}', 3) => 25, + ('\u{2309}', 3) => 26, + ('{', 3) => 27, + ('}', 3) => 28, + ('\u{27E8}', 3) => 29, + ('\u{27E9}', 3) => 30, + ('(', 4) => 33, + (')', 4) => 34, + ('[', 4) => 35, + (']', 4) => 36, + ('\u{230A}', 4) => 37, + ('\u{230B}', 4) => 38, + ('\u{2308}', 4) => 39, + ('\u{2309}', 4) => 40, + ('{', 4) => 41, + ('}', 4) => 42, + ('\u{27E8}', 4) => 43, + ('\u{27E9}', 4) => 44, + _ => return None, + }; + let metrics = math_extension_metrics(); + let code = char::from(slot); + // CMEX delimiters sit almost entirely below the baseline; TeX centres them + // on the math axis (a quarter em at 10pt) before reporting height/depth. + let extent = metrics.height(code) + metrics.depth(code); + let axis = 655_360 / 4; + Some(( + metrics.width(code), + extent / 2 + axis, + (extent / 2 - axis).max(0), + )) +} + +#[cfg(test)] +mod cmex_delimiter_tests { + use super::*; + + #[test] + fn cmex_delimiters_have_distinct_size_variants() { + let big = cmex_delimiter('(', 1).expect("cmex has a big paren"); + let big_big = cmex_delimiter('(', 2).expect("cmex has a Big paren"); + let bigg = cmex_delimiter('(', 3).expect("cmex has a bigg paren"); + let bigg_bigg = cmex_delimiter('(', 4).expect("cmex has a Bigg paren"); + assert!(big.0 > 0 && big.1 > 0); + assert!(big_big.1 > big.1); + assert!(bigg.1 > big_big.1); + assert!(bigg_bigg.1 > bigg.1); + let extent = |m: (i64, i64, i64)| m.1 + m.2; + assert!(extent(big_big) > extent(big)); + assert!(extent(bigg) > extent(big_big)); + assert!(extent(bigg_bigg) > extent(bigg)); + assert!(cmex_delimiter('.', 1).is_none()); + assert!(cmex_delimiter('x', 1).is_none()); + } +} diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index abb6292..f2a2f77 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -4,7 +4,7 @@ //! geometry the route-D page builder needs; token-to-atom conversion and the //! final painter arrive in later M4 slices. -use crate::cmr10::Cmr10Metrics; +use crate::cmr10::{Cmr10Metrics, cmex_delimiter}; #[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum MathAtom { @@ -375,20 +375,30 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { } MathAtom::Classed { body, .. } => layout_math(body, font), MathAtom::SizedDelimiter { delimiter, size } => { - let extent = match *size { - 1 => 8 * 65_536, - 2 => 11 * 65_536, - 3 => 14 * 65_536, - _ => 17 * 65_536, - }; - MathBox { - width: if *delimiter == '.' { - 0 - } else { - (extent as f64 * 0.35) as i64 - }, - height: extent, - depth: 2 * 65_536, + if *delimiter == '.' { + MathBox { + width: 0, + height: 0, + depth: 0, + } + } else if let Some((width, height, depth)) = cmex_delimiter(*delimiter, *size) { + MathBox { + width, + height, + depth, + } + } else { + let extent = match *size { + 1 => 8 * 65_536, + 2 => 11 * 65_536, + 3 => 14 * 65_536, + _ => 17 * 65_536, + }; + MathBox { + width: (extent as f64 * 0.35) as i64, + height: extent, + depth: 2 * 65_536, + } } } MathAtom::StackLines(rows) => { @@ -1852,3 +1862,33 @@ mod cmsy_symbol_tests { assert_ne!(laid_out.width, Cmr10Metrics::Roman.width('\u{00D7}')); } } + +#[cfg(test)] +mod cmex_size_tests { + use super::*; + use crate::cmr10::{Cmr10Metrics, cmex_delimiter}; + + #[test] + fn big_delimiters_use_cmex_metrics() { + let font = Cmr10Metrics::Math; + let big = layout_math(&parse_math(r"\big("), &font); + let (width, height, depth) = cmex_delimiter('(', 1).expect("cmex big paren"); + assert_eq!(big.width, width); + assert_eq!(big.height, height); + assert_eq!(big.depth, depth); + let plain = layout_math(&parse_math("("), &font); + assert!(big.height > plain.height); + } + + #[test] + fn bigger_delimiters_pick_larger_cmex_variants() { + let font = Cmr10Metrics::Math; + let big = layout_math(&parse_math(r"\Big["), &font); + let bigg = layout_math(&parse_math(r"\bigg["), &font); + let slot_big = cmex_delimiter('[', 2).expect("cmex Big bracket"); + let slot_bigg = cmex_delimiter('[', 3).expect("cmex bigg bracket"); + assert_eq!(big.width, slot_big.0); + assert_eq!(bigg.width, slot_bigg.0); + assert!(bigg.height > big.height); + } +} diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index c953f8f..c43a89e 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`). 496 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 499 workspace tests are green and no oracle fixture is ignored. Status after G1 (P0-P2): the paragraph model reproduces TeX's passes, diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 3d83ecc..00c3740 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -131,7 +131,7 @@ and justification. | 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, and `\mark{...}`/`\marks{...}` writes inside the routine are carried to the following page's mark list with their class; `\pageno`, `\outputpenalty` and `\deadcycles` are readable and assignable registers (`\pageno=...` carries into the next page, and an assigned `\vsize` retargets the pages the routine has not seen yet, even when the document raises insertion material; only a document that defers bottom floats keeps its initial goal, because the carried queue cannot be rebuilt after a retarget), `\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 that outlive the routine (the paged builder runs the routine after each page, so an assigned `\dimen` or `\count` changes the insertion capacity and reservation of later pages); the configured `\skip` also reserves space above its class once per page in the paged builder, and a `\skip` assignment rebuilds the pages the routine has not seen yet with the new reservation, `\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; the paged builder splits a tall alignment into page-sized row chunks that keep the table's column widths, so long tables continue across pages; `p{width}` paragraph columns wrap cell text and grow row heights, wrapped rows participate in row-level splitting, explicit per-column `\tabskip` glue is honoured through layout, painting and page chunks, and booktabs rules carry their own weights and spans (`\toprule`/`\bottomrule` 0.8pt, `\midrule`/`\cmidrule` 0.5pt, with `\cmidrule(lr){a-b}` trimming and column ranges), `@{}` / `@{\hspace{...}}` column expressions override the inter-column glue, and row-boundary `\noalign{\vskip ...}` glue is honoured by layout, painting and page chunks; longtable head/foot sections (`\endfirsthead`/`\endhead`/`\endfoot`/`\endlastfoot`) are parsed, continuation chunks repeat the head rows and the foot block paints once at the end, LaTeX `tabular` environments are captured and laid out through the same `\halign` path (rules are stripped from the column spec, `p{...}`/`m{...}`/`b{...}` become wrapping columns, `@{}` expressions are ignored for spacing), though `\valign`, nested tables and full tabular parity remain open | -| M4 | math lists, scripts, fractions, radicals, accents, delimiters | **partial**: token parser, atom boxes with Computer Modern metrics (italic letters from the bundled `cmmi10`, big operators from `cmex10`, symbols such as `\times`/`\leq`/`\infty` from `cmsy10`), delimiter scaling, accents and a display-list painter; display formulas wider than the measure are split at top-level binary/relation operators into multiple lines (an explicit approximation of TeX's `\split`/`align` behaviour); no CMEX size variants or full TeX math spacing classes yet | +| M4 | math lists, scripts, fractions, radicals, accents, delimiters | **partial**: token parser, atom boxes with Computer Modern metrics (italic letters from the bundled `cmmi10`, big operators from `cmex10`, symbols such as `\times`/`\leq`/`\infty` from `cmsy10`), delimiter scaling, accents and a display-list painter; display formulas wider than the measure are split at top-level binary/relation operators into multiple lines (an explicit approximation of TeX's `\split`/`align` behaviour); CMEX delimiter size variants back `\big`/`\Big`/`\bigg`/`\Bigg`; no full TeX math spacing classes yet | | M5 | NFSS/TFM metrics, CM/Latin Modern | same line/page breaks as reference | | M6 | class/package execution (`article`, `sn-jnl`) | Overleaf page-count parity | | M7 | `\special`, PDF XObjects, hyperref annotations | PDF structure parity | @@ -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 336 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 496 tests (336 route-D unit, 15 engine unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 v1 unit, 1 core unit), with none ignored. v1 remains the live server path. +The crate currently has 339 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 499 tests (339 route-D unit, 15 engine unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 v1 unit, 1 core unit), with none ignored. v1 remains the live server path. ### M0 incremental demo