Parse left and right math delimiters
This commit is contained in:
1 parent
c545c7ff9a
commit
41456b9d31
2 files changed
+65
-3
No files matched your search
@@ -11,6 +11,11 @@ pub enum MathAtom {
|
||||
Symbol(char),
|
||||
Row(Vec<MathAtom>),
|
||||
Sqrt(Box<MathAtom>),
|
||||
Delimited {
|
||||
left: char,
|
||||
right: char,
|
||||
body: Box<MathAtom>,
|
||||
},
|
||||
Script {
|
||||
base: Box<MathAtom>,
|
||||
sub: Option<Box<MathAtom>>,
|
||||
@@ -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<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
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
Reference in new issue
Block a user