Support explicit vertical skips between paragraphs

This commit is contained in:
gloamxun committed 2026-10-23 14:15:17 +08:00
1 parent d596ef2a95
commit 0102a20120
4 files changed
+150 -7

No files matched your search

+88
View File
@@ -243,6 +243,9 @@ pub struct Unit {
/// editable through a plain source string.
#[serde(default)]
pub paragraph_style: Option<ParagraphStyle>,
/// Extra vertical glue before this unit (TeX `\vskip`/`\smallskip`).
#[serde(default)]
pub space_before: i64,
}
impl Unit {
@@ -305,6 +308,7 @@ impl Unit {
id,
source: "halign".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![VerticalItem::Halign(Halign {
rows,
column_widths,
@@ -510,6 +514,7 @@ impl Unit {
source,
items: vec![VerticalItem::Paragraph(paragraph)],
paragraph_style: Some(style),
space_before: 0,
}
}
@@ -538,6 +543,7 @@ impl Unit {
source: String::new(),
items,
paragraph_style: None,
space_before: 0,
}
}
@@ -838,6 +844,15 @@ fn document_nodes(
let mut items = Vec::new();
let mut previous_paragraph = false;
for (index, unit) in document.units.iter().enumerate() {
if unit.space_before > 0 {
nodes.push(VerticalNode::Glue(VerticalGlue::new(
unit.space_before,
0,
0,
)));
owners.push(index);
items.push(VerticalItem::Glue(unit.space_before));
}
for (position, item) in unit.items.iter().enumerate() {
if position == 0 && previous_paragraph && matches!(item, VerticalItem::Paragraph(_)) {
nodes.push(VerticalNode::Glue(VerticalGlue::new(
@@ -1347,6 +1362,18 @@ impl Engine {
page_marks: current.marks.clone(),
page_floats: current.floats.clone(),
});
if unit.space_before > 0 && !current.items.is_empty() {
let lead = unit.space_before;
let bottom = bottom_float_area(&current, document.text_float_sep);
if used + lead > document.page_goal - insertion_height - bottom {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
} else {
used += lead;
current.items.push(VerticalItem::Glue(lead));
}
}
for item in &unit.items {
if !matches!(
item,
@@ -2282,7 +2309,9 @@ impl Engine {
_ => None,
})
.unwrap_or(ParagraphFont::Roman);
let space_before = next.units[unit_index].space_before;
next.units[unit_index] = Unit::paragraph_with_style_and_face(unit_id, source, style, font);
next.units[unit_index].space_before = space_before;
if self.layout.checkpoints.is_empty() {
if !self.relayout_paged_incremental(&next, unit_index) {
self.replace_with_paged(&next);
@@ -2382,6 +2411,18 @@ impl Engine {
page_marks: current.marks.clone(),
page_floats: current.floats.clone(),
});
if unit.space_before > 0 && !current.items.is_empty() {
let lead = unit.space_before;
let bottom = bottom_float_area(&current, next.text_float_sep);
if used + lead > next.page_goal - insertion_height - bottom {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
} else {
used += lead;
current.items.push(VerticalItem::Glue(lead));
}
}
for item in &unit.items {
if !matches!(
item,
@@ -2823,6 +2864,7 @@ mod tests {
id: 7,
source: "footnote layout".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Insertion(Insertion {
class: 0,
@@ -2950,6 +2992,7 @@ mod tests {
id: index,
source: String::new(),
paragraph_style: None,
space_before: 0,
items: vec![VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
@@ -2971,6 +3014,7 @@ mod tests {
id,
source: "boxes".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -3020,6 +3064,7 @@ mod tests {
id: 0,
source: "penalty".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -3083,6 +3128,7 @@ mod tests {
id: 0,
source: text.to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -3141,6 +3187,7 @@ mod tests {
id: 0,
source: "float".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -3174,6 +3221,7 @@ mod tests {
id: 0,
source: "float".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -3776,6 +3824,7 @@ mod tests {
id: 0,
source: "bottom float".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -3811,6 +3860,7 @@ mod tests {
id: 0,
source: "tall bottom float".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -3843,6 +3893,7 @@ mod tests {
id: 0,
source: "top fraction".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Float(Float {
gap_before: 0,
@@ -5425,6 +5476,7 @@ mod tests {
id: 0,
source: "top float".to_owned(),
paragraph_style: None,
space_before: 0,
items: vec![
VerticalItem::Float(Float {
placement: FloatPlacement::Top,
@@ -5863,6 +5915,7 @@ mod tests {
depth: 30,
}],
paragraph_style: None,
space_before: 0,
};
let mut document =
Document::new(100, vec![math, Unit::vskip(2, 0), Unit::rule(3, 100, 20)]);
@@ -5952,6 +6005,7 @@ mod tests {
depth: 300_000,
}],
paragraph_style: None,
space_before: 0,
};
let mut document = Document::new(800_000, vec![math]);
document.max_depth = 400_000;
@@ -6449,4 +6503,38 @@ mod tests {
engine.edit(&document, 3, "epsilon zeta");
assert_eq!(engine.recomputed_pages(), pages);
}
#[test]
fn space_before_inserts_vertical_glue_between_units() {
let mut second = Unit::paragraph(2, "bbb");
second.space_before = 20;
let document = Document::new(100, vec![Unit::paragraph(1, "aaa"), second]);
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 1);
let items = &engine.pages()[0].items;
assert_eq!(items.len(), 3);
assert!(matches!(items[1], VerticalItem::Glue(20)));
}
#[test]
fn space_before_can_force_a_paged_break() {
let mut second = Unit::paragraph(2, "bbb");
second.space_before = 20;
let document = Document::new(30, vec![Unit::paragraph(1, "aaa"), second]);
let engine = Engine::layout_paged(&document).expect("paged layout");
assert_eq!(engine.pages().len(), 2);
}
#[test]
fn incremental_layout_keeps_space_before() {
let mut second = Unit::paragraph(2, "bbb");
second.space_before = 20;
let document = Document::new(100, vec![Unit::paragraph(1, "aaa"), second]);
let mut engine = Engine::layout(&document);
engine.edit(&document, 2, "bbb edited");
let page = &engine.pages()[0];
assert!(
page.items
.iter()
.any(|item| matches!(item, VerticalItem::Glue(20)))
);
}
}