From 946f8b1b762cedfafd7e7c83b3621c224524fdb9 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 13:46:54 +0800 Subject: [PATCH] Paint math atoms in the route-D display list --- crates/texrra-tex-core/src/lib.rs | 74 +++++++- crates/texrra-tex-core/src/math.rs | 8 +- crates/texrra-tex-core/src/render.rs | 262 +++++++++++++++++++++++++++ docs/TEX_CORE.md | 2 + 4 files changed, 338 insertions(+), 8 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 6a5c814..4b21588 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -54,7 +54,10 @@ pub struct Halign { #[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum VerticalItem { Box(VerticalBox), - Rule { width: i64, height: i64 }, + Rule { + width: i64, + height: i64, + }, Glue(i64), Penalty(i64), Insertion(Insertion), @@ -62,6 +65,12 @@ pub enum VerticalItem { Float(Float), Halign(Halign), Paragraph(Paragraph), + Math { + atom: crate::math::MathAtom, + width: i64, + height: i64, + depth: i64, + }, } impl Halign { @@ -371,10 +380,12 @@ impl Unit { let math_box = math::layout_math(&atom, font); Self::vertical( id, - vec![VerticalItem::Box(VerticalBox { + vec![VerticalItem::Math { + atom, width: math_box.width, - height: math_box.height + math_box.depth, - })], + height: math_box.height, + depth: math_box.depth, + }], ) } @@ -623,6 +634,21 @@ impl Engine { } current.items.push(item.clone()); } + VerticalItem::Math { height, depth, .. } => { + let amount = height + depth; + if current.items.is_empty() && top_float_height(¤t) == 0 { + used = document.top_skip.max(amount); + } else if used + amount + > document.page_goal - insertion_height - bottom_float_height(¤t) + { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = document.top_skip.max(amount); + insertion_height = 0; + } else { + used += amount; + } + current.items.push(item.clone()); + } VerticalItem::Glue(amount) => { if !current.items.is_empty() { used += amount; @@ -764,6 +790,7 @@ impl Engine { VerticalItem::Float(float) => float.height, VerticalItem::Halign(halign) => halign.height, VerticalItem::Paragraph(paragraph) => paragraph.height, + VerticalItem::Math { height, depth, .. } => height + depth, }) .sum(); if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() { @@ -854,6 +881,21 @@ impl Engine { } current.items.push(item.clone()); } + VerticalItem::Math { height, depth, .. } => { + let amount = height + depth; + if current.items.is_empty() && top_float_height(¤t) == 0 { + used = next.top_skip.max(amount); + } else if used + amount + > next.page_goal - insertion_height - bottom_float_height(¤t) + { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = next.top_skip.max(amount); + insertion_height = 0; + } else { + used += amount; + } + current.items.push(item.clone()); + } VerticalItem::Glue(amount) => { if !current.items.is_empty() { used += amount; @@ -1279,6 +1321,30 @@ mod tests { assert_eq!(halign.cell_box(2, 0), Some((30, 110))); } + #[test] + fn math_display_list_paints_symbols_and_fraction_rule() { + let atom = crate::math::parse_math("x + \\frac{a}{b}"); + let document = Document::new( + 400, + vec![Unit::math(1, atom, &crate::cmr10::Cmr10Metrics::new())], + ); + let engine = Engine::layout(&document); + let list = engine.pages()[0].display_list(); + let text = list + .items + .iter() + .filter(|item| matches!(item, DisplayItem::Text { .. })) + .count(); + let rules = list + .items + .iter() + .filter(|item| matches!(item, DisplayItem::Rule { .. })) + .count(); + assert!(text >= 4, "expected math glyphs, got {text}"); + assert!(rules >= 1, "expected a fraction rule"); + let svg = list.to_svg(); + assert!(svg.contains(">x")); + } #[test] fn paragraph_display_list_paints_characters() { let document = Document::new(400, vec![Unit::paragraph(1, "hello world")]); diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 08acbd4..dfab6bf 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -6,7 +6,7 @@ use crate::cmr10::Cmr10Metrics; -#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum MathAtom { Symbol(char), Row(Vec), @@ -115,7 +115,7 @@ pub struct MathBox { } /// TeX math atom classes used for inter-atom spacing. -#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum MathClass { Ordinary, Binary, @@ -822,8 +822,8 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { let delimiter = chars.get(next).copied().unwrap_or('|'); (MathAtom::Symbol(delimiter), next + 1) } - "max" | "min" | "lim" | "log" | "ln" | "sin" | "cos" | "tan" | "cot" | "sec" - | "csc" | "exp" | "det" | "dim" | "ker" | "hom" | "arg" | "deg" | "gcd" => { + "max" | "min" | "log" | "ln" | "sin" | "cos" | "tan" | "cot" | "sec" | "csc" + | "exp" | "det" | "dim" | "ker" | "hom" | "arg" | "deg" | "gcd" => { (MathAtom::Text(command.clone()), next) } "mathcal" | "mathbb" | "mathbf" | "mathit" | "mathsf" | "mathtt" | "mathfrak" diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 8abdb66..c707b21 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -5,6 +5,8 @@ //! primitives so the route-D core can be inspected before the PDF backend is //! wired up. +use crate::cmr10::Cmr10Metrics; +use crate::math::{self, MathAtom}; use crate::{Halign, HorizontalItem, Page, VerticalItem}; /// One absolute drawing primitive in scaled points. @@ -160,6 +162,16 @@ impl Page { VerticalItem::Glue(height) => { y += (*height).max(0); } + VerticalItem::Math { + atom, + height, + depth, + .. + } => { + let font = Cmr10Metrics::new(); + Self::paint_math(atom, &font, 0, y + *height, 655_360, &mut list.items); + y += *height + *depth; + } VerticalItem::Paragraph(paragraph) => { let lines = paragraph.lines.len().max(1) as i64; let line_height = (paragraph.height / lines).max(1); @@ -213,4 +225,254 @@ impl Page { list.height = y; list } + + fn paint_math( + atom: &MathAtom, + font: &Cmr10Metrics, + x: i64, + baseline: i64, + size: i64, + list: &mut Vec, + ) { + match atom { + MathAtom::Symbol(character) => { + if !character.is_whitespace() { + list.push(DisplayItem::Text { + x, + y: baseline, + size, + text: character.to_string(), + }); + } + } + MathAtom::Text(text) => { + let mut cursor = x; + for character in text.chars() { + list.push(DisplayItem::Text { + x: cursor, + y: baseline, + size, + text: character.to_string(), + }); + cursor += font.width(character); + } + } + MathAtom::Row(atoms) => { + let mut cursor = x; + for child in atoms { + let width = math::layout_math(child, font).width; + Self::paint_math(child, font, cursor, baseline, size, list); + cursor += width; + } + } + MathAtom::Script { base, sub, sup } => { + let base_box = math::layout_math(base, font); + Self::paint_math(base, font, x, baseline, size, list); + let small = (size as f64 * 0.7) as i64; + if let Some(sup) = sup { + Self::paint_math( + sup, + font, + x + base_box.width, + baseline - base_box.height * 2 / 3, + small, + list, + ); + } + if let Some(sub) = sub { + Self::paint_math( + sub, + font, + x + base_box.width, + baseline + base_box.depth * 2 / 3, + small, + list, + ); + } + } + MathAtom::Fraction { + numerator, + denominator, + } => { + let num = math::layout_math(numerator, font); + let den = math::layout_math(denominator, font); + let width = num.width.max(den.width); + Self::paint_math( + numerator, + font, + x + (width - num.width) / 2, + baseline - 8 * 65_536, + size, + list, + ); + Self::paint_math( + denominator, + font, + x + (width - den.width) / 2, + baseline + 12 * 65_536, + size, + list, + ); + list.push(DisplayItem::Rule { + x, + y: baseline - 2 * 65_536, + width, + height: 26_214, + }); + } + MathAtom::Sqrt { radicand, .. } => { + let box_ = math::layout_math(radicand, font); + list.push(DisplayItem::Text { + x, + y: baseline, + size, + text: "√".to_string(), + }); + Self::paint_math(radicand, font, x + 4 * 65_536, baseline, size, list); + list.push(DisplayItem::Rule { + x: x + 4 * 65_536, + y: baseline - box_.height - 65_536, + width: box_.width, + height: 26_214, + }); + } + MathAtom::Delimited { left, right, body } => { + let body_box = math::layout_math(body, font); + if *left != '.' { + list.push(DisplayItem::Text { + x, + y: baseline, + size, + text: left.to_string(), + }); + } + Self::paint_math(body, font, x + font.width(*left), baseline, size, list); + if *right != '.' { + list.push(DisplayItem::Text { + x: x + font.width(*left) + body_box.width, + y: baseline, + size, + text: right.to_string(), + }); + } + } + MathAtom::Accent { accent, base } => { + let base_box = math::layout_math(base, font); + Self::paint_math(base, font, x, baseline, size, list); + list.push(DisplayItem::Text { + x: x + base_box.width / 2, + y: baseline - base_box.height - 65_536, + size, + text: accent.to_string(), + }); + } + MathAtom::Underline { base } | MathAtom::Negated { base } => { + let base_box = math::layout_math(base, font); + Self::paint_math(base, font, x, baseline, size, list); + list.push(DisplayItem::Rule { + x, + y: baseline + base_box.depth, + width: base_box.width, + height: 26_214, + }); + } + MathAtom::Boxed { body } => { + let body_box = math::layout_math(body, font); + Self::paint_math(body, font, x + 65_536, baseline, size, list); + list.push(DisplayItem::Rule { + x, + y: baseline - body_box.height - 65_536, + width: body_box.width + 131_072, + height: 26_214, + }); + list.push(DisplayItem::Rule { + x, + y: baseline + body_box.depth + 65_536, + width: body_box.width + 131_072, + height: 26_214, + }); + } + MathAtom::Phantom { + body, + horizontal, + vertical, + } => { + if *horizontal || !*vertical { + Self::paint_math(body, font, x, baseline, size, list); + } + } + MathAtom::StackLines(lines) => { + let height = 12 * 65_536; + for (index, line) in lines.iter().enumerate() { + Self::paint_math(line, font, x, baseline + index as i64 * height, size, list); + } + } + MathAtom::Stack { top, base, above } => { + Self::paint_math(base, font, x, baseline, size, list); + let base_box = math::layout_math(base, font); + let top_box = math::layout_math(top, font); + Self::paint_math( + top, + font, + x + (base_box.width - top_box.width) / 2, + if *above { + baseline - base_box.height - top_box.depth + } else { + baseline + base_box.depth + top_box.height + }, + size, + list, + ); + } + MathAtom::Brace { base, .. } + | MathAtom::Smash { body: base, .. } + | MathAtom::Limits { base, .. } + | MathAtom::Classed { body: base, .. } => { + Self::paint_math(base, font, x, baseline, size, list) + } + MathAtom::Binomial { + numerator, + denominator, + } => { + let num = math::layout_math(numerator, font); + let den = math::layout_math(denominator, font); + let width = num.width.max(den.width); + Self::paint_math( + numerator, + font, + x + (width - num.width) / 2, + baseline - 8 * 65_536, + size, + list, + ); + Self::paint_math( + denominator, + font, + x + (width - den.width) / 2, + baseline + 12 * 65_536, + size, + list, + ); + } + MathAtom::BigOperator { name } => { + for (index, character) in name.chars().enumerate() { + list.push(DisplayItem::Text { + x: x + index as i64 * font.width(character), + y: baseline, + size, + text: character.to_string(), + }); + } + } + MathAtom::SizedDelimiter { delimiter, .. } => { + list.push(DisplayItem::Text { + x, + y: baseline, + size, + text: delimiter.to_string(), + }); + } + MathAtom::Strut => {} + } + } } diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 824b385..345e036 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -249,3 +249,5 @@ v1 remains the production renderer; the route-D core is additive and tested inde M3 painter prototype: `Page::display_list()` / `Halign::display_list()` produce absolute text and rule primitives, and `DisplayList::to_svg()` writes a standalone SVG. Paragraph text and math are not yet painted. Paragraph painter: `Unit::paragraph` now stores `VerticalItem::Paragraph`, and `Page::display_list()` paints each line's `HorizontalBox` characters at their line advance. Boxes created by hyphenation carry `-`; justification is approximated by item widths. + +Math painter: stores ; paints symbols, rows, scripts, fractions (with a rule), roots, delimiters, accents, underlines, boxes and stacks. Script/fraction positions are simplified TeX geometry.