From b8a135d2b650839f9b7d3f058342125319b009c8 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 19:19:13 +0800 Subject: [PATCH] Draw cancel and bcancel math marks --- crates/texrra-tex-core/src/math.rs | 57 ++++++++++++++++++++++++++++ crates/texrra-tex-core/src/render.rs | 26 +++++++++++++ 2 files changed, 83 insertions(+) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index b4dd0dd..1c69545 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -31,6 +31,11 @@ pub enum MathAtom { Negated { base: Box, }, + Cancel { + base: Box, + forward: bool, + backward: bool, + }, Boxed { body: Box, }, @@ -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(); diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 0d32866..f4b9f4a 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -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);