From 41456b9d31687f42d5677f3996fd6f7685cb9c43 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 12:54:39 +0800 Subject: [PATCH] Parse left and right math delimiters --- crates/texrra-tex-core/src/math.rs | 66 ++++++++++++++++++++++++++++-- docs/TEX_CORE.md | 2 + 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index a7ceaa7..d1f9030 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -11,6 +11,11 @@ pub enum MathAtom { Symbol(char), Row(Vec), Sqrt(Box), + Delimited { + left: char, + right: char, + body: Box, + }, Script { base: Box, sub: Option>, @@ -140,6 +145,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { depth: boxed.depth, } } + MathAtom::Delimited { left, right, body } => { + let body_box = layout_math(body, font); + MathBox { + width: font.width(*left) + body_box.width + font.width(*right), + height: body_box.height + 2 * 65_536, + depth: body_box.depth + 2 * 65_536, + } + } MathAtom::Script { base, sub, sup } => { let base_box = layout_math(base, font); let sub_box = sub.as_deref().map(|atom| layout_math(atom, font)); @@ -187,16 +200,33 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { /// later M4 parser can replace them. pub fn parse_math(input: &str) -> MathAtom { let chars: Vec = input.chars().filter(|ch| !ch.is_whitespace()).collect(); - let (atom, _) = parse_sequence(&chars, 0, false); + let (atom, _) = parse_sequence(&chars, 0, false, false); atom } -fn parse_sequence(chars: &[char], mut index: usize, stop_at_brace: bool) -> (MathAtom, usize) { +fn is_right_command(chars: &[char], index: usize) -> bool { + chars.get(index) == Some(&'\\') + && chars.get(index + 1) == Some(&'r') + && chars.get(index + 2) == Some(&'i') + && chars.get(index + 3) == Some(&'g') + && chars.get(index + 4) == Some(&'h') + && chars.get(index + 5) == Some(&'t') +} + +fn parse_sequence( + chars: &[char], + mut index: usize, + stop_at_brace: bool, + stop_at_right: bool, +) -> (MathAtom, usize) { let mut atoms = Vec::new(); while index < chars.len() { if stop_at_brace && chars[index] == '}' { break; } + if stop_at_right && is_right_command(chars, index) { + break; + } let (mut atom, mut next) = parse_atom(chars, index); let mut sub = None; let mut sup = None; @@ -231,7 +261,7 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { return (MathAtom::Row(Vec::new()), index); }; if character == '{' { - let (atom, next) = parse_sequence(chars, index + 1, true); + let (atom, next) = parse_sequence(chars, index + 1, true, false); let next = if chars.get(next) == Some(&'}') { next + 1 } else { @@ -260,6 +290,27 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { let (radicand, after) = parse_atom(chars, next); (MathAtom::Sqrt(Box::new(radicand)), after) } + "left" => { + let left = chars.get(next).copied().unwrap_or('('); + let (body, after) = parse_sequence(chars, next + 1, false, true); + let mut after = after; + // Consume `\right` and its delimiter. + if is_right_command(chars, after) { + after += 6; + } + let right = chars.get(after).copied().unwrap_or(')'); + if chars.get(after).is_some() { + after += 1; + } + ( + MathAtom::Delimited { + left, + right, + body: Box::new(body), + }, + after, + ) + } _ => (MathAtom::Symbol('?'), next), }; } @@ -321,6 +372,15 @@ mod tests { assert!(fraction.depth > 0); } + #[test] + fn left_right_delimiters_grow_with_their_body() { + let font = Cmr10Metrics::default(); + let atom = parse_math("\\left(\\frac{a}{b}\\right)"); + let boxed = layout_math(&atom, &font); + let plain = layout_math(&parse_math("(a)"), &font); + assert!(matches!(atom, MathAtom::Row(_))); + assert!(boxed.height > plain.height); + } #[test] fn operator_spacing_separates_binary_and_relation_atoms() { let font = Cmr10Metrics::default(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 3dbaefc..714b4c3 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -104,6 +104,8 @@ overfull ratio 4.11, badness 6942. TeX rejects the underfull candidate under its tolerance but still emits the moderately overfull 5-word line. Reproducing this needs a two-pass tolerance algorithm with an emergency overfull pass, not a single cost function. +- `MathAtom::Delimited { left, right, body }` and `\left...\right` + parsing grow the delimiter box with the body height/depth. - `MathClass` and TeX's 0/3/4/5mu inter-atom table add spacing for binary/relation/open/close/punctuation atoms in `MathAtom::Row`. - `parse_math` converts a small TeX math subset (symbols, `{}` groups,