From 7376acffb2908ad24f87ff5fddccc054382698d8 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 19:13:09 +0800 Subject: [PATCH] Draw negated math with a diagonal rule --- crates/texrra-tex-core/src/pdf.rs | 16 +++++++ crates/texrra-tex-core/src/render.rs | 72 +++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/crates/texrra-tex-core/src/pdf.rs b/crates/texrra-tex-core/src/pdf.rs index 76c9d76..27e7af1 100755 --- a/crates/texrra-tex-core/src/pdf.rs +++ b/crates/texrra-tex-core/src/pdf.rs @@ -80,6 +80,22 @@ fn page_content(list: &DisplayList, height: i64, image_names: &BTreeMap { + content.push_str(&format!( + "{} w {} {} m {} {} l S\n", + pt(*line_width), + pt(*x1), + pt(height - *y1), + pt(*x2), + pt(height - *y2) + )); + } DisplayItem::Image { x, y, diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 962bf01..0d32866 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -35,6 +35,13 @@ pub enum DisplayItem { width: i64, height: i64, }, + Line { + x1: i64, + y1: i64, + x2: i64, + y2: i64, + width: i64, + }, Image { x: i64, y: i64, @@ -105,6 +112,12 @@ impl DisplayList { *x += dx; *y += dy; } + DisplayItem::Line { x1, y1, x2, y2, .. } => { + *x1 += dx; + *y1 += dy; + *x2 += dx; + *y2 += dy; + } DisplayItem::Image { x, y, .. } | DisplayItem::Link { x, y, .. } => { *x += dx; *y += dy; @@ -190,6 +203,22 @@ impl DisplayList { escape(content) )); } + DisplayItem::Line { + x1, + y1, + x2, + y2, + width, + } => { + let x1 = *x1 as f64 / 65_536.0; + let y1 = *y1 as f64 / 65_536.0; + let x2 = *x2 as f64 / 65_536.0; + let y2 = *y2 as f64 / 65_536.0; + let width = *width as f64 / 65_536.0; + out.push_str(&format!( + "\n" + )); + } DisplayItem::Image { x, y, @@ -641,7 +670,7 @@ impl Page { text: accent.to_string(), }); } - MathAtom::Underline { base } | MathAtom::Negated { base } => { + MathAtom::Underline { base } => { let base_box = math::layout_math(base, font); Self::paint_math(base, font, x, baseline, size, list); list.push(DisplayItem::Rule { @@ -651,6 +680,17 @@ impl Page { height: 26_214, }); } + MathAtom::Negated { base } => { + let base_box = math::layout_math(base, font); + Self::paint_math(base, font, x, baseline, size, list); + list.push(DisplayItem::Line { + x1: x, + y1: baseline + base_box.depth, + x2: x + base_box.width, + y2: baseline - base_box.height, + width: 26_214, + }); + } MathAtom::Boxed { body } => { let body_box = math::layout_math(body, font); Self::paint_math(body, font, x + 65_536, baseline, size, list); @@ -817,3 +857,33 @@ impl Page { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn negated_math_draws_a_diagonal_line() { + let mut items = Vec::new(); + Page::paint_math( + &MathAtom::Negated { + base: Box::new(MathAtom::Symbol('=')), + }, + &Cmr10Metrics::default(), + 0, + 0, + 10 * 65_536, + &mut items, + ); + assert!( + items + .iter() + .any(|item| matches!(item, DisplayItem::Line { .. })) + ); + assert!( + !items + .iter() + .any(|item| matches!(item, DisplayItem::Rule { .. })) + ); + } +}