Draw cancel and bcancel math marks

This commit is contained in:
gloamxun committed 2026-10-20 19:19:13 +08:00
1 parent 9690dbdbed
commit b8a135d2b6
2 files changed
+83

No files matched your search

+57
View File
@@ -31,6 +31,11 @@ pub enum MathAtom {
Negated {
base: Box<MathAtom>,
},
Cancel {
base: Box<MathAtom>,
forward: bool,
backward: bool,
},
Boxed {
body: Box<MathAtom>,
},
@@ -171,6 +176,7 @@ fn atom_class(atom: &MathAtom) -> MathClass {
MathAtom::Classed { class, .. } => *class,
MathAtom::Row(atoms) => atoms.last().map(atom_class).unwrap_or(MathClass::Ordinary),
MathAtom::Negated { base } => atom_class(base),
MathAtom::Cancel { base, .. } => atom_class(base),
_ => MathClass::Ordinary,
}
}
@@ -383,6 +389,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: inner.depth,
}
}
MathAtom::Cancel { base, .. } => {
let inner = layout_math(base, font);
MathBox {
width: inner.width,
height: inner.height,
depth: inner.depth,
}
}
MathAtom::Underline { base } => {
let base_box = layout_math(base, font);
MathBox {
@@ -813,6 +827,39 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"cancel" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Cancel {
base: Box::new(base),
forward: true,
backward: false,
},
after,
)
}
"bcancel" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Cancel {
base: Box::new(base),
forward: false,
backward: true,
},
after,
)
}
"xcancel" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Cancel {
base: Box::new(base),
forward: true,
backward: true,
},
after,
)
}
"dot" => accent_atom(chars, next, '\u{0307}'),
"ddot" => accent_atom(chars, next, '\u{0308}'),
"check" => accent_atom(chars, next, '\u{030C}'),
@@ -1305,6 +1352,16 @@ mod tests {
assert_eq!(plain.height, numbered.height);
}
#[test]
fn cancel_commands_keep_the_base_box() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("x"), &font);
for command in [r"\cancel{x}", r"\bcancel{x}", r"\xcancel{x}"] {
let cancelled = layout_math(&parse_math(command), &font);
assert_eq!(cancelled.width, plain.width);
assert_eq!(cancelled.height, plain.height);
}
}
#[test]
fn dot_and_check_accents_raise_the_math_box() {
let font = Cmr10Metrics::default();
+26
View File
@@ -680,6 +680,32 @@ impl Page {
height: 26_214,
});
}
MathAtom::Cancel {
base,
forward,
backward,
} => {
let base_box = math::layout_math(base, font);
Self::paint_math(base, font, x, baseline, size, list);
if *forward {
list.push(DisplayItem::Line {
x1: x,
y1: baseline + base_box.depth,
x2: x + base_box.width,
y2: baseline - base_box.height,
width: 26_214,
});
}
if *backward {
list.push(DisplayItem::Line {
x1: x,
y1: baseline - base_box.height,
x2: x + base_box.width,
y2: baseline + base_box.depth,
width: 26_214,
});
}
}
MathAtom::Negated { base } => {
let base_box = math::layout_math(base, font);
Self::paint_math(base, font, x, baseline, size, list);