Add math accent atoms

This commit is contained in:
gloamxun committed 2026-10-20 12:55:13 +08:00
1 parent 41456b9d31
commit 63927110d9
2 files changed
+63

No files matched your search

+60
View File
@@ -16,6 +16,10 @@ pub enum MathAtom {
right: char,
body: Box<MathAtom>,
},
Accent {
accent: char,
base: Box<MathAtom>,
},
Script {
base: Box<MathAtom>,
sub: Option<Box<MathAtom>>,
@@ -153,6 +157,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: body_box.depth + 2 * 65_536,
}
}
MathAtom::Accent { base, .. } => {
let base_box = layout_math(base, font);
MathBox {
width: base_box.width,
height: base_box.height + 2 * 65_536,
depth: base_box.depth,
}
}
MathAtom::Script { base, sub, sup } => {
let base_box = layout_math(base, font);
let sub_box = sub.as_deref().map(|atom| layout_math(atom, font));
@@ -290,6 +302,46 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
let (radicand, after) = parse_atom(chars, next);
(MathAtom::Sqrt(Box::new(radicand)), after)
}
"hat" | "widehat" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Accent {
accent: '\u{0302}',
base: Box::new(base),
},
after,
)
}
"tilde" | "widetilde" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Accent {
accent: '\u{0303}',
base: Box::new(base),
},
after,
)
}
"bar" | "overline" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Accent {
accent: '\u{0305}',
base: Box::new(base),
},
after,
)
}
"vec" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Accent {
accent: '\u{20D7}',
base: Box::new(base),
},
after,
)
}
"left" => {
let left = chars.get(next).copied().unwrap_or('(');
let (body, after) = parse_sequence(chars, next + 1, false, true);
@@ -372,6 +424,14 @@ mod tests {
assert!(fraction.depth > 0);
}
#[test]
fn accents_add_height_without_changing_width() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("x"), &font);
let accented = layout_math(&parse_math("\\hat{x}"), &font);
assert_eq!(accented.width, plain.width);
assert!(accented.height > plain.height);
}
#[test]
fn left_right_delimiters_grow_with_their_body() {
let font = Cmr10Metrics::default();
+3
View File
@@ -104,6 +104,9 @@ overfull ratio 4.11, badness 6942. TeX rejects the underfull candidate under
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
a single cost function.
- `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`,
`\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds
accent height without changing the base width.
- `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