Parse left and right math delimiters

This commit is contained in:
gloamxun committed 2026-10-20 12:54:39 +08:00
1 parent c545c7ff9a
commit 41456b9d31
2 files changed
+65 -3

No files matched your search

+63 -3
View File
@@ -11,6 +11,11 @@ pub enum MathAtom {
Symbol(char), Symbol(char),
Row(Vec<MathAtom>), Row(Vec<MathAtom>),
Sqrt(Box<MathAtom>), Sqrt(Box<MathAtom>),
Delimited {
left: char,
right: char,
body: Box<MathAtom>,
},
Script { Script {
base: Box<MathAtom>, base: Box<MathAtom>,
sub: Option<Box<MathAtom>>, sub: Option<Box<MathAtom>>,
@@ -140,6 +145,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: boxed.depth, 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 } => { MathAtom::Script { base, sub, sup } => {
let base_box = layout_math(base, font); let base_box = layout_math(base, font);
let sub_box = sub.as_deref().map(|atom| layout_math(atom, 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. /// later M4 parser can replace them.
pub fn parse_math(input: &str) -> MathAtom { pub fn parse_math(input: &str) -> MathAtom {
let chars: Vec<char> = input.chars().filter(|ch| !ch.is_whitespace()).collect(); let chars: Vec<char> = input.chars().filter(|ch| !ch.is_whitespace()).collect();
let (atom, _) = parse_sequence(&chars, 0, false); let (atom, _) = parse_sequence(&chars, 0, false, false);
atom 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(); let mut atoms = Vec::new();
while index < chars.len() { while index < chars.len() {
if stop_at_brace && chars[index] == '}' { if stop_at_brace && chars[index] == '}' {
break; break;
} }
if stop_at_right && is_right_command(chars, index) {
break;
}
let (mut atom, mut next) = parse_atom(chars, index); let (mut atom, mut next) = parse_atom(chars, index);
let mut sub = None; let mut sub = None;
let mut sup = None; let mut sup = None;
@@ -231,7 +261,7 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
return (MathAtom::Row(Vec::new()), index); return (MathAtom::Row(Vec::new()), index);
}; };
if character == '{' { 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(&'}') { let next = if chars.get(next) == Some(&'}') {
next + 1 next + 1
} else { } else {
@@ -260,6 +290,27 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
let (radicand, after) = parse_atom(chars, next); let (radicand, after) = parse_atom(chars, next);
(MathAtom::Sqrt(Box::new(radicand)), after) (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), _ => (MathAtom::Symbol('?'), next),
}; };
} }
@@ -321,6 +372,15 @@ mod tests {
assert!(fraction.depth > 0); 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] #[test]
fn operator_spacing_separates_binary_and_relation_atoms() { fn operator_spacing_separates_binary_and_relation_atoms() {
let font = Cmr10Metrics::default(); let font = Cmr10Metrics::default();
+2
View File
@@ -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 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 this needs a two-pass tolerance algorithm with an emergency overfull pass, not
a single cost function. 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 - `MathClass` and TeX's 0/3/4/5mu inter-atom table add spacing for
binary/relation/open/close/punctuation atoms in `MathAtom::Row`. binary/relation/open/close/punctuation atoms in `MathAtom::Row`.
- `parse_math` converts a small TeX math subset (symbols, `{}` groups, - `parse_math` converts a small TeX math subset (symbols, `{}` groups,