Number equation environments in route-D

This commit is contained in:
gloamxun committed 2026-10-24 01:57:18 +08:00
1 parent 3cbfdabe03
commit bb897411d0
5 files changed
+207 -6

No files matched your search

+45
View File
@@ -53,6 +53,10 @@ pub enum MathAtom {
vertical: bool,
},
Strut,
Tagged {
body: Box<MathAtom>,
tag: String,
},
Limits {
base: Box<MathAtom>,
sub: Option<Box<MathAtom>>,
@@ -584,6 +588,18 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth + 2 * 65_536,
}
}
MathAtom::Tagged { body, tag } => {
let body_box = layout_math(body, font);
let tag_width: i64 = tag
.chars()
.map(|character| Cmr10Metrics::Roman.width(character))
.sum();
MathBox {
width: body_box.width.max(tag_width),
height: body_box.height.max(7 * 65_536),
depth: body_box.depth.max(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));
@@ -2092,3 +2108,32 @@ mod spacing_class_tests {
assert_eq!(width(r"\sum x") - sum - font.width('x'), 3 * 65_536 / 18);
}
}
#[cfg(test)]
mod tagged_equation_tests {
use super::*;
use crate::cmr10::Cmr10Metrics;
fn roman_width(text: &str) -> i64 {
text.chars()
.map(|character| Cmr10Metrics::Roman.width(character))
.sum()
}
#[test]
fn tagged_formula_reserves_room_for_the_tag() {
let font = Cmr10Metrics::Math;
let body = parse_math("x");
let plain = layout_math(&body, &font);
let tagged = layout_math(
&MathAtom::Tagged {
body: Box::new(body),
tag: "(12)".to_string(),
},
&font,
);
assert_eq!(tagged.width, plain.width.max(roman_width("(12)")));
assert_eq!(tagged.height, plain.height.max(7 * 65_536));
assert_eq!(tagged.depth, plain.depth.max(2 * 65_536));
}
}