Parse substack into stacked math rows

This commit is contained in:
gloamxun committed 2026-10-20 13:30:56 +08:00
1 parent 60949cf91c
commit f18fc5187f
2 files changed
+31

No files matched your search

+29
View File
@@ -59,6 +59,7 @@ pub enum MathAtom {
delimiter: char,
size: u8,
},
StackLines(Vec<MathAtom>),
Stack {
top: Box<MathAtom>,
base: Box<MathAtom>,
@@ -314,6 +315,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: 2 * 65_536,
}
}
MathAtom::StackLines(rows) => {
let boxes: Vec<MathBox> = rows.iter().map(|atom| layout_math(atom, font)).collect();
MathBox {
width: boxes.iter().map(|b| b.width).max().unwrap_or(0),
height: boxes.iter().map(|b| b.height).sum(),
depth: boxes.last().map_or(0, |b| b.depth),
}
}
MathAtom::Stack { top, base, above } => {
let top = layout_math(top, font);
let base = layout_math(base, font);
@@ -678,6 +687,19 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
(after + 1).min(chars.len()),
)
}
"substack" => {
let mut after = next;
if chars.get(after) == Some(&'{') {
after += 1;
}
let start = after;
while after < chars.len() && chars[after] != '}' {
after += 1;
}
let content: String = chars[start..after].iter().collect();
let rows = content.split("\\\\").map(parse_math).collect();
(MathAtom::StackLines(rows), (after + 1).min(chars.len()))
}
"overset" | "underset" | "stackrel" => {
let (top, after) = parse_atom(chars, next);
let (base, after) = parse_atom(chars, after);
@@ -1036,6 +1058,13 @@ mod tests {
assert!(tall.width > plain.width);
}
#[test]
fn substack_stacks_multiple_rows() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("a"), &font);
let stacked = layout_math(&parse_math(r"\substack{a\\b}"), &font);
assert!(stacked.height > plain.height);
}
#[test]
fn overbrace_accepts_a_superscript_label() {
let font = Cmr10Metrics::default();
+2
View File
@@ -233,3 +233,5 @@ for a later overlaid slash.
| M7 incremental integration | partial | edit replay + page hashes, not wired into v1 preview |
v1 remains the production renderer; the route-D core is additive and tested independently.
`\substack{a\\b}` parses each row and stacks them as `MathAtom::StackLines`.