Build genfrac fractions with explicit delimiters
This commit is contained in:
1 parent
d237e0cc9b
commit
ab5e857705
4 files changed
+98
-5
No files matched your search
@@ -1403,6 +1403,45 @@ fn operator_default_limits(base: &MathAtom) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn read_braced_text(chars: &[char], mut index: usize) -> Option<(String, usize)> {
|
||||
while index < chars.len() && (chars[index].is_whitespace() || chars[index] == '\u{0}') {
|
||||
index += 1;
|
||||
}
|
||||
if chars.get(index) != Some(&'{') {
|
||||
return None;
|
||||
}
|
||||
let start = index + 1;
|
||||
let mut depth = 1_usize;
|
||||
index = start;
|
||||
while index < chars.len() {
|
||||
match chars[index] {
|
||||
'{' => depth += 1,
|
||||
'}' => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
let text: String = chars[start..index].iter().collect();
|
||||
return Some((text, index + 1));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn delimiter_from_text(text: &str) -> Option<char> {
|
||||
let chars: Vec<char> = text.trim().chars().collect();
|
||||
if chars.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let (delimiter, end) = parse_delimiter(&chars, 0);
|
||||
if end != chars.len() {
|
||||
return None;
|
||||
}
|
||||
Some(delimiter)
|
||||
}
|
||||
|
||||
fn parse_limits(chars: &[char], next: usize, base: MathAtom) -> (MathAtom, usize) {
|
||||
let mut after = next;
|
||||
let mut forced = None;
|
||||
@@ -1941,6 +1980,37 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
Some((atom, after)) => (atom, after),
|
||||
None => (MathAtom::Symbol('?'), next),
|
||||
},
|
||||
"genfrac" => {
|
||||
let mut cursor = next;
|
||||
let mut fields = Vec::new();
|
||||
for _ in 0..6 {
|
||||
let (text, after) = match read_braced_text(chars, cursor) {
|
||||
Some(parsed) => parsed,
|
||||
None => return (MathAtom::Symbol('?'), cursor),
|
||||
};
|
||||
fields.push(text);
|
||||
cursor = after;
|
||||
}
|
||||
let left = delimiter_from_text(&fields[0]);
|
||||
let right = delimiter_from_text(&fields[1]);
|
||||
let thickness = fields[2].trim();
|
||||
let bar = !thickness.is_empty() && thickness != "0pt";
|
||||
let numerator = parse_math(&fields[4]);
|
||||
let denominator = parse_math(&fields[5]);
|
||||
let mut atom = MathAtom::Fraction {
|
||||
numerator: Box::new(numerator),
|
||||
denominator: Box::new(denominator),
|
||||
bar,
|
||||
};
|
||||
if left.is_some() || right.is_some() {
|
||||
atom = MathAtom::Delimited {
|
||||
left: left.unwrap_or('.'),
|
||||
right: right.unwrap_or('.'),
|
||||
body: Box::new(atom),
|
||||
};
|
||||
}
|
||||
(atom, cursor)
|
||||
}
|
||||
"frac" | "tfrac" | "dfrac" | "cfrac" => {
|
||||
let (numerator, after) = parse_atom(chars, next);
|
||||
let (denominator, after) = parse_atom(chars, after);
|
||||
@@ -3075,6 +3145,29 @@ mod tests {
|
||||
let limited = layout_math(&parse_math(r"\mathop{argmax}_{x}"), &font);
|
||||
assert!(limited.depth > plain.depth);
|
||||
}
|
||||
#[test]
|
||||
fn genfrac_builds_fractions_with_optional_delimiters() {
|
||||
let font = Cmr10Metrics::default();
|
||||
fn only(atom: MathAtom) -> MathAtom {
|
||||
match atom {
|
||||
MathAtom::Row(mut atoms) if atoms.len() == 1 => atoms.remove(0),
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
let plain = only(parse_math(r"\genfrac{}{}{0pt}{}{a}{b}"));
|
||||
let MathAtom::Fraction { bar, .. } = &plain else {
|
||||
panic!("expected a bare fraction");
|
||||
};
|
||||
assert!(!bar, "an empty or 0pt thickness suppresses the rule");
|
||||
let framed = only(parse_math(r"\genfrac{[}{]}{0.4pt}{}{a}{b}"));
|
||||
let MathAtom::Delimited { left, right, body } = &framed else {
|
||||
panic!("expected a delimited fraction");
|
||||
};
|
||||
assert_eq!((*left, *right), ('[', ']'));
|
||||
assert!(matches!(**body, MathAtom::Fraction { bar: true, .. }));
|
||||
assert!(layout_math(&framed, &font).width > layout_math(&plain, &font).width);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integrals_keep_scripts_beside_them_by_default() {
|
||||
let font = Cmr10Metrics::default();
|
||||
|
||||
Reference in new issue
Block a user