Paint paragraph glyphs in the route-D display list

This commit is contained in:
gloamxun committed 2026-10-20 13:42:58 +08:00
1 parent d07ff59d31
commit 38bacdfbd5
4 files changed
+84 -6

No files matched your search

+46 -4
View File
@@ -61,6 +61,7 @@ pub enum VerticalItem {
Mark(Mark),
Float(Float),
Halign(Halign),
Paragraph(Paragraph),
}
impl Halign {
@@ -356,10 +357,7 @@ impl Unit {
Self {
id,
source,
items: vec![VerticalItem::Box(VerticalBox {
width: paragraph.width.max(style.line_width),
height: paragraph.height,
})],
items: vec![VerticalItem::Paragraph(paragraph)],
paragraph_style: Some(style),
}
}
@@ -611,6 +609,20 @@ impl Engine {
}
current.items.push(item.clone());
}
VerticalItem::Paragraph(paragraph) => {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(paragraph.height);
} else if used + paragraph.height
> document.page_goal - insertion_height - bottom_float_height(&current)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip.max(paragraph.height);
insertion_height = 0;
} else {
used += paragraph.height;
}
current.items.push(item.clone());
}
VerticalItem::Glue(amount) => {
if !current.items.is_empty() {
used += amount;
@@ -751,6 +763,7 @@ impl Engine {
VerticalItem::Mark(_) => 0,
VerticalItem::Float(float) => float.height,
VerticalItem::Halign(halign) => halign.height,
VerticalItem::Paragraph(paragraph) => paragraph.height,
})
.sum();
if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() {
@@ -827,6 +840,20 @@ impl Engine {
}
current.items.push(item.clone());
}
VerticalItem::Paragraph(paragraph) => {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(paragraph.height);
} else if used + paragraph.height
> next.page_goal - insertion_height - bottom_float_height(&current)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip.max(paragraph.height);
insertion_height = 0;
} else {
used += paragraph.height;
}
current.items.push(item.clone());
}
VerticalItem::Glue(amount) => {
if !current.items.is_empty() {
used += amount;
@@ -1252,6 +1279,21 @@ mod tests {
assert_eq!(halign.cell_box(2, 0), Some((30, 110)));
}
#[test]
fn paragraph_display_list_paints_characters() {
let document = Document::new(400, vec![Unit::paragraph(1, "hello world")]);
let engine = Engine::layout(&document);
let list = engine.pages()[0].display_list();
let texts = list
.items
.iter()
.filter(|item| matches!(item, DisplayItem::Text { .. }))
.count();
assert!(texts >= 10, "expected painted characters, got {texts}");
let svg = list.to_svg();
assert!(svg.contains(">h</text>"));
assert!(svg.contains(">w</text>"));
}
#[test]
fn halign_display_list_paints_cells_and_rules() {
let unit = Unit::halign_from_source(1, r"A & B \\ \hline C & D", 2, 10);
+7 -1
View File
@@ -6,6 +6,8 @@ pub struct HorizontalBox {
pub width: i64,
pub height: i64,
pub depth: i64,
/// The source character when this box came from text, for display lists.
pub character: Option<char>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
@@ -55,7 +57,7 @@ impl Default for ParagraphStyle {
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Paragraph {
pub lines: Vec<Vec<HorizontalItem>>,
pub height: i64,
@@ -86,6 +88,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
width: style.char_width,
height: style.line_height,
depth: 0,
character: Some(character),
}));
}
}
@@ -247,12 +250,14 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
width: *pre_break_width,
height: style.line_height,
depth: 0,
character: Some('-'),
}));
} else if *no_break_width > 0 {
line.push(HorizontalItem::Box(HorizontalBox {
width: *no_break_width,
height: style.line_height,
depth: 0,
character: None,
}));
}
}
@@ -317,6 +322,7 @@ pub fn text_items_with_font(
width: font.width(character),
height: style.line_height,
depth: 0,
character: Some(character),
}));
let boundary = character_index + 1;
if points.contains(&boundary) && boundary >= 2 && boundary + 2 <= word.chars().count() {
+29 -1
View File
@@ -5,7 +5,7 @@
//! primitives so the route-D core can be inspected before the PDF backend is
//! wired up.
use crate::{Halign, Page, VerticalItem};
use crate::{Halign, HorizontalItem, Page, VerticalItem};
/// One absolute drawing primitive in scaled points.
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -160,6 +160,34 @@ impl Page {
VerticalItem::Glue(height) => {
y += (*height).max(0);
}
VerticalItem::Paragraph(paragraph) => {
let lines = paragraph.lines.len().max(1) as i64;
let line_height = (paragraph.height / lines).max(1);
let size = ((line_height as f64) * 0.7) as i64;
for line in &paragraph.lines {
let baseline = y + ((line_height as f64) * 0.8) as i64;
let mut x = 0_i64;
for item in line {
match item {
HorizontalItem::Box(box_) => {
if let Some(character) = box_.character {
list.push(DisplayItem::Text {
x,
y: baseline,
size,
text: character.to_string(),
});
}
x += box_.width;
}
HorizontalItem::Glue { width, .. } => x += *width,
HorizontalItem::Penalty { .. }
| HorizontalItem::Discretionary { .. } => {}
}
}
y += line_height;
}
}
VerticalItem::Halign(halign) => {
let mut nested = halign.display_list();
nested.place(0, y);
+2
View File
@@ -247,3 +247,5 @@ v1 remains the production renderer; the route-D core is additive and tested inde
`\mathstrut` is a zero-width strut with 7pt height and 2pt depth.
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.