diff --git a/crates/texrra-tex-core/assets/cmsy10.tfm b/crates/texrra-tex-core/assets/cmsy10.tfm new file mode 100644 index 0000000..3e4488b Binary files /dev/null and b/crates/texrra-tex-core/assets/cmsy10.tfm differ diff --git a/crates/texrra-tex-core/src/cmr10.rs b/crates/texrra-tex-core/src/cmr10.rs index d8bce2f..17a764f 100644 --- a/crates/texrra-tex-core/src/cmr10.rs +++ b/crates/texrra-tex-core/src/cmr10.rs @@ -27,6 +27,7 @@ static CMTT10_TFM: &[u8] = include_bytes!("../assets/cmtt10.tfm"); static CMSS10_TFM: &[u8] = include_bytes!("../assets/cmss10.tfm"); static CMMI10_TFM: &[u8] = include_bytes!("../assets/cmmi10.tfm"); static CMEX10_TFM: &[u8] = include_bytes!("../assets/cmex10.tfm"); +static CMSY10_TFM: &[u8] = include_bytes!("../assets/cmsy10.tfm"); static CMR10_METRICS: OnceLock = OnceLock::new(); static CMBX10_METRICS: OnceLock = OnceLock::new(); @@ -36,6 +37,7 @@ static CMTT10_METRICS: OnceLock = OnceLock::new(); static CMSS10_METRICS: OnceLock = OnceLock::new(); static CMMI10_METRICS: OnceLock = OnceLock::new(); static CMEX10_METRICS: OnceLock = OnceLock::new(); +static CMSY10_METRICS: OnceLock = OnceLock::new(); fn parse(bytes: &[u8]) -> TfmMetrics { TfmMetrics::parse(bytes).expect("embedded TFM must be valid") @@ -73,6 +75,10 @@ fn parse_math_extension() -> TfmMetrics { parse(CMEX10_TFM) } +fn parse_math_symbol() -> TfmMetrics { + parse(CMSY10_TFM) +} + fn math_italic_metrics() -> &'static TfmMetrics { CMMI10_METRICS.get_or_init(parse_math_italic) } @@ -81,6 +87,10 @@ fn math_extension_metrics() -> &'static TfmMetrics { CMEX10_METRICS.get_or_init(parse_math_extension) } +fn math_symbol_metrics() -> &'static TfmMetrics { + CMSY10_METRICS.get_or_init(parse_math_symbol) +} + fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics { match font { Cmr10Metrics::Roman => CMR10_METRICS.get_or_init(parse_roman), @@ -132,6 +142,14 @@ impl Cmr10Metrics { return width; } } + if self.is_math() { + if let Some(slot) = math_symbol_slot(character) { + let width = math_symbol_metrics().width(char::from(slot)); + if width > 0 { + return width; + } + } + } let width = self.metrics().width(character); if width > 0 { width } else { 327_680 } } @@ -143,6 +161,11 @@ impl Cmr10Metrics { return height; } } + if self.is_math() { + if let Some(slot) = math_symbol_slot(character) { + return math_symbol_metrics().height(char::from(slot)); + } + } self.metrics().height(character) } @@ -153,6 +176,11 @@ impl Cmr10Metrics { return depth; } } + if self.is_math() { + if let Some(slot) = math_symbol_slot(character) { + return math_symbol_metrics().depth(char::from(slot)); + } + } self.metrics().depth(character) } @@ -160,6 +188,11 @@ impl Cmr10Metrics { if self.is_math() && character.is_ascii_alphabetic() { return math_italic_metrics().italic(character); } + if self.is_math() { + if let Some(slot) = math_symbol_slot(character) { + return math_symbol_metrics().italic(char::from(slot)); + } + } self.metrics().italic(character) } @@ -268,3 +301,116 @@ mod tests { assert!(cmex_operator("not-an-operator").is_none()); } } + +/// Map a Unicode math symbol to its slot in the bundled cmsy10 font. +pub fn math_symbol_slot(character: char) -> Option { + let slot = match character { + '\u{00B7}' => 2, // \cdot + '\u{00D7}' => 3, // \times + '\u{00F7}' => 5, // \div + '\u{00B1}' => 7, // \pm + '\u{2213}' => 8, // \mp + '\u{2295}' => 9, // \oplus + '\u{2296}' => 10, // \ominus + '\u{2297}' => 11, // \otimes + '\u{2298}' => 12, // \oslash + '\u{2299}' => 13, // \odot + '\u{224D}' => 17, // \asymp + '\u{2261}' => 18, // \equiv + '\u{2286}' => 19, // \subseteq + '\u{2287}' => 20, // \supseteq + '\u{2264}' => 21, // \leq + '\u{2265}' => 22, // \geq + '\u{2AAF}' => 23, // \preceq + '\u{2AB0}' => 24, // \succeq + '\u{223C}' => 25, // \sim + '\u{2248}' => 26, // \approx + '\u{2282}' => 27, // \subset + '\u{2283}' => 28, // \supset + '\u{226A}' => 29, // \ll + '\u{226B}' => 30, // \gg + '\u{227A}' => 31, // \prec + '\u{227B}' => 32, // \succ + '\u{2190}' => 33, // \leftarrow + '\u{2192}' => 34, // \rightarrow + '\u{2191}' => 35, // \uparrow + '\u{2193}' => 36, // \downarrow + '\u{2194}' => 37, // \leftrightarrow + '\u{2197}' => 38, // \nearrow + '\u{2198}' => 39, // \searrow + '\u{2243}' => 40, // \simeq + '\u{21D0}' => 41, // \Leftarrow + '\u{21D2}' => 42, // \Rightarrow + '\u{21D1}' => 43, // \Uparrow + '\u{21D3}' => 44, // \Downarrow + '\u{21D4}' => 45, // \Leftrightarrow + '\u{2196}' => 46, // \nwarrow + '\u{2199}' => 47, // \swarrow + '\u{221D}' => 48, // \propto + '\u{2032}' => 49, // \prime + '\u{221E}' => 50, // \infty + '\u{2208}' => 51, // \in + '\u{220B}' => 52, // \ni + '\u{25B3}' => 53, // \triangle + '\u{25BD}' => 54, // \triangledown + '\u{21A6}' => 56, // \mapsto + '\u{2200}' => 57, // \forall + '\u{2203}' => 58, // \exists + '\u{00AC}' => 59, // \neg + '\u{2205}' => 60, // \emptyset + '\u{211C}' => 61, // \Re + '\u{2111}' => 62, // \Im + '\u{22A4}' => 63, // \top + '\u{22A5}' => 64, // \bot + '\u{2135}' => 65, // \aleph + '\u{222A}' => 92, // \cup + '\u{2229}' => 93, // \cap + '\u{228E}' => 94, // \uplus + '\u{2227}' => 95, // \wedge + '\u{2228}' => 96, // \vee + '\u{22A2}' => 97, // \vdash + '\u{22A3}' => 98, // \dashv + '\u{230A}' => 99, // \lfloor + '\u{230B}' => 100, // \rfloor + '\u{2308}' => 101, // \lceil + '\u{2309}' => 102, // \rceil + '\u{007C}' => 107, // \vert + '\u{2016}' => 108, // \Vert + '\u{2195}' => 109, // \updownarrow + '\u{21D5}' => 110, // \Updownarrow + '\u{2240}' => 112, // \wr + '\u{2210}' => 114, // \coprod + '\u{2207}' => 115, // \nabla + '\u{222C}' | '\u{222D}' => 117, + '\u{2294}' => 117, // \sqcup + '\u{2293}' => 118, // \sqcap + '\u{00A7}' => 121, // \S + '\u{2020}' => 122, // \dagger + '\u{2021}' => 123, // \ddagger + '\u{00B6}' => 124, // \P + '\u{2663}' => 125, // \clubsuit + '\u{2662}' => 126, // \diamondsuit + '\u{2661}' => 127, // \heartsuit + '\u{2660}' => 128, // \spadesuit + _ => return None, + }; + Some(slot) +} + +#[cfg(test)] +mod cmsy_tests { + use super::*; + + #[test] + fn cmsy_provides_math_symbol_metrics() { + let font = Cmr10Metrics::Math; + let times = font.width('\u{00D7}'); + let slot = math_symbol_metrics().width(char::from(3u8)); + assert_eq!(times, slot); + assert!(times > 0); + let less_equal = font.width('\u{2264}'); + assert!(less_equal > 0); + assert_ne!(times, less_equal); + assert_eq!(Cmr10Metrics::Roman.width('\u{00D7}'), 327_680); + } +} diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 44e6be2..abb6292 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -652,6 +652,93 @@ fn parse_delimiter(chars: &[char], index: usize) -> (char, usize) { } } +fn math_symbol_command(name: &str) -> Option { + let character = match name { + "cdot" => '\u{00B7}', + "times" => '\u{00D7}', + "div" => '\u{00F7}', + "pm" => '\u{00B1}', + "mp" => '\u{2213}', + "oplus" => '\u{2295}', + "ominus" => '\u{2296}', + "otimes" => '\u{2297}', + "oslash" => '\u{2298}', + "odot" => '\u{2299}', + "asymp" => '\u{224D}', + "equiv" => '\u{2261}', + "subseteq" => '\u{2286}', + "supseteq" => '\u{2287}', + "leq" | "le" => '\u{2264}', + "geq" | "ge" => '\u{2265}', + "preceq" => '\u{2AAF}', + "succeq" => '\u{2AB0}', + "sim" => '\u{223C}', + "approx" => '\u{2248}', + "subset" => '\u{2282}', + "supset" => '\u{2283}', + "ll" => '\u{226A}', + "gg" => '\u{226B}', + "prec" => '\u{227A}', + "succ" => '\u{227B}', + "leftarrow" | "gets" => '\u{2190}', + "rightarrow" | "to" => '\u{2192}', + "uparrow" => '\u{2191}', + "downarrow" => '\u{2193}', + "leftrightarrow" => '\u{2194}', + "nearrow" => '\u{2197}', + "searrow" => '\u{2198}', + "simeq" => '\u{2243}', + "Leftarrow" => '\u{21D0}', + "Rightarrow" => '\u{21D2}', + "Uparrow" => '\u{21D1}', + "Downarrow" => '\u{21D3}', + "Leftrightarrow" => '\u{21D4}', + "nwarrow" => '\u{2196}', + "swarrow" => '\u{2199}', + "propto" => '\u{221D}', + "prime" => '\u{2032}', + "infty" => '\u{221E}', + "in" => '\u{2208}', + "ni" => '\u{220B}', + "triangle" => '\u{25B3}', + "triangledown" => '\u{25BD}', + "mapsto" => '\u{21A6}', + "forall" => '\u{2200}', + "exists" => '\u{2203}', + "neg" | "lnot" => '\u{00AC}', + "emptyset" | "varnothing" => '\u{2205}', + "Re" => '\u{211C}', + "Im" => '\u{2111}', + "top" => '\u{22A4}', + "bot" => '\u{22A5}', + "aleph" => '\u{2135}', + "cup" => '\u{222A}', + "cap" => '\u{2229}', + "uplus" => '\u{228E}', + "wedge" | "land" => '\u{2227}', + "vee" | "lor" => '\u{2228}', + "vdash" => '\u{22A2}', + "dashv" => '\u{22A3}', + "updownarrow" => '\u{2195}', + "Updownarrow" => '\u{21D5}', + "wr" => '\u{2240}', + "coprod" => '\u{2210}', + "nabla" => '\u{2207}', + "sqcup" => '\u{2294}', + "sqcap" => '\u{2293}', + "S" => '\u{00A7}', + "dagger" => '\u{2020}', + "ddagger" => '\u{2021}', + "P" => '\u{00B6}', + "clubsuit" => '\u{2663}', + "diamondsuit" => '\u{2662}', + "heartsuit" => '\u{2661}', + "spadesuit" => '\u{2660}', + _ => return None, + }; + Some(character) +} + fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { let mut index = index; while chars.get(index) == Some(&'\u{0}') { @@ -1143,7 +1230,10 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { after, ) } - _ => (MathAtom::Symbol('?'), next), + other => match math_symbol_command(other) { + Some(character) => (MathAtom::Symbol(character), next), + None => (MathAtom::Symbol('?'), next), + }, }; } (MathAtom::Symbol(character), index + 1) @@ -1739,3 +1829,26 @@ mod tests { assert!(matches!(&atoms[2], MathAtom::Sqrt { .. })); } } + +#[cfg(test)] +mod cmsy_symbol_tests { + use super::*; + use crate::cmr10::Cmr10Metrics; + + #[test] + fn symbol_commands_use_cmsy_metrics() { + let font = Cmr10Metrics::Math; + let atom = parse_math(r"\times"); + let MathAtom::Row(row) = &atom else { + panic!("atom: {atom:?}"); + }; + assert!( + matches!(row.first(), Some(MathAtom::Symbol('\u{00D7}'))), + "row: {row:?}" + ); + let laid_out = layout_math(&atom, &font); + assert_eq!(laid_out.width, font.width('\u{00D7}')); + assert!(laid_out.width > 0); + assert_ne!(laid_out.width, Cmr10Metrics::Roman.width('\u{00D7}')); + } +} diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index 181cfa1..c953f8f 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`). 494 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 496 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 9a650f1..3d83ecc 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 the bundled `cmex10`), 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 `cmsy10` symbol alphabet, CMEX size variants or 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); no CMEX size variants or 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 334 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 494 tests (334 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 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. ### M0 incremental demo