Support directional smash options

This commit is contained in:
gloamxun committed 2026-10-20 13:10:40 +08:00
1 parent c311938129
commit e650177585
2 files changed
+39 -4

No files matched your search

+37 -3
View File
@@ -558,12 +558,36 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
)
}
"smash" => {
let (body, after) = parse_atom(chars, next);
let mut cursor = next;
while cursor < chars.len() && chars[cursor].is_whitespace() {
cursor += 1;
}
let mut top = true;
let mut bottom = true;
if chars.get(cursor) == Some(&'[') {
let mut end = cursor + 1;
while end < chars.len() && chars[end] != ']' {
match chars[end] {
't' => {
top = true;
bottom = false;
}
'b' => {
top = false;
bottom = true;
}
_ => {}
}
end += 1;
}
cursor = (end + 1).min(chars.len());
}
let (body, after) = parse_atom(chars, cursor);
(
MathAtom::Smash {
body: Box::new(body),
top: true,
bottom: true,
top,
bottom,
},
after,
)
@@ -828,6 +852,16 @@ mod tests {
assert_eq!(calligraphic.height, upright.height);
}
#[test]
fn smash_can_target_only_the_top_or_bottom() {
let font = Cmr10Metrics::default();
let top = layout_math(&parse_math("\\smash[t]{x}"), &font);
let bottom = layout_math(&parse_math("\\smash[b]{x}"), &font);
assert_eq!(top.height, 0);
assert!(top.depth > 0);
assert!(bottom.height > 0);
assert_eq!(bottom.depth, 0);
}
#[test]
fn smash_removes_height_but_keeps_width() {
let font = Cmr10Metrics::default();
+2 -1
View File
@@ -113,7 +113,8 @@ 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::Smash { body, top, bottom }` implements `\smash` (zero height/depth).
- `MathAtom::Smash { body, top, bottom }` implements `\smash` and
`\smash[t]`/`\smash[b]`.
- `MathAtom::Phantom { body, horizontal, vertical }` implements
`\phantom`, `\hphantom`, `\vphantom` dimension-only boxes.
- `MathAtom::BigOperator { name }` gives `\sum`/`\prod` 12pt width and