Insert parskip between paragraphs in the paged builder

This commit is contained in:
gloamxun committed 2026-10-23 03:26:53 +08:00
1 parent ca36c94eb3
commit bc0d29ace4
3 files changed
+54 -5

No files matched your search

+47 -1
View File
@@ -785,8 +785,22 @@ fn document_nodes(
let mut nodes = Vec::new();
let mut owners = Vec::new();
let mut items = Vec::new();
let mut previous_paragraph = false;
for (index, unit) in document.units.iter().enumerate() {
for item in &unit.items {
for (position, item) in unit.items.iter().enumerate() {
if position == 0
&& document.par_skip > 0
&& previous_paragraph
&& matches!(item, VerticalItem::Paragraph(_))
{
nodes.push(VerticalNode::Glue(VerticalGlue::new(
document.par_skip,
0,
0,
)));
owners.push(index);
items.push(VerticalItem::Glue(document.par_skip));
}
let node = match item {
VerticalItem::Box(vertical_box) => VerticalNode::Box {
height: vertical_box.height,
@@ -821,6 +835,10 @@ fn document_nodes(
owners.push(index);
items.push(item.clone());
}
previous_paragraph = unit
.items
.last()
.is_some_and(|item| matches!(item, VerticalItem::Paragraph(_)));
}
Some((nodes, owners, items))
}
@@ -4946,6 +4964,34 @@ mod tests {
assert_eq!(engine.pages()[0].sources, vec![(1, source_hash(""))]);
}
#[test]
fn paged_layout_inserts_par_skip_between_paragraphs() {
let mut style = ParagraphStyle::default();
style.line_height = 30;
let mut document = Document::new(
60,
vec![
Unit::paragraph_with_style(1, "one", style.clone()),
Unit::paragraph_with_style(2, "two", style),
],
);
assert_eq!(
Engine::layout_paged(&document)
.expect("paragraphs are page-buildable")
.pages()
.len(),
1
);
document.par_skip = 10;
let engine = Engine::layout_paged(&document).expect("paragraphs are page-buildable");
assert_eq!(engine.pages().len(), 2);
assert!(matches!(
engine.pages()[1].items.first(),
Some(VerticalItem::Paragraph(_))
));
}
#[test]
fn paged_layout_can_split_one_unit_across_pages() {
let document = Document::new(