Paint math atoms in the route-D display list

This commit is contained in:
gloamxun committed 2026-10-20 13:46:54 +08:00
1 parent 38bacdfbd5
commit 946f8b1b76
4 files changed
+338 -8

No files matched your search

+70 -4
View File
@@ -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(&current) == 0 {
used = document.top_skip.max(amount);
} else if used + amount
> document.page_goal - insertion_height - bottom_float_height(&current)
{
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(&current) == 0 {
used = next.top_skip.max(amount);
} else if used + amount
> next.page_goal - insertion_height - bottom_float_height(&current)
{
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</text>"));
}
#[test]
fn paragraph_display_list_paints_characters() {
let document = Document::new(400, vec![Unit::paragraph(1, "hello world")]);