Carry plus and minus on inter-paragraph skip glue

This commit is contained in:
gloamxun committed 2026-10-23 14:53:00 +08:00
1 parent 6f1310a876
commit 08a312be82
4 files changed
+101 -13

No files matched your search

+74 -8
View File
@@ -670,6 +670,12 @@ pub struct Document {
/// TeX `\parskip`: vertical glue inserted between paragraphs.
#[serde(default)]
pub par_skip: i64,
/// TeX stretch for the `\parskip` glue inserted between paragraphs.
#[serde(default)]
pub par_skip_stretch: i64,
/// TeX shrink for the `\parskip` glue inserted between paragraphs.
#[serde(default)]
pub par_skip_shrink: i64,
/// Extra horizontal displacement of the page origin (`\hoffset`).
#[serde(default)]
pub h_offset: i64,
@@ -767,6 +773,8 @@ impl Document {
text_float_sep: default_text_float_sep(),
in_text_sep: default_in_text_sep(),
par_skip: 0,
par_skip_stretch: 0,
par_skip_shrink: 0,
h_offset: 0,
v_offset: 0,
max_deadcycles: default_max_deadcycles(),
@@ -887,11 +895,15 @@ fn document_nodes(
if position == 0 && previous_paragraph && matches!(item, VerticalItem::Paragraph(_)) {
nodes.push(VerticalNode::Glue(VerticalGlue::new(
document.par_skip,
0,
0,
document.par_skip_stretch,
document.par_skip_shrink,
)));
owners.push(index);
items.push(VerticalItem::Glue(document.par_skip));
items.push(VerticalItem::StretchGlue(VerticalGlue::new(
document.par_skip,
document.par_skip_stretch,
document.par_skip_shrink,
)));
}
let node = match item {
VerticalItem::Box(vertical_box) => VerticalNode::Box {
@@ -1104,6 +1116,8 @@ fn place_paragraph_flow(
page_goal: i64,
top_skip: i64,
par_skip: i64,
par_skip_stretch: i64,
par_skip_shrink: i64,
bottom: i64,
) {
let mut pending = paragraph.clone();
@@ -1114,6 +1128,16 @@ fn place_paragraph_flow(
} else {
par_skip
};
let skip_stretch = if current.items.is_empty() {
0
} else {
par_skip_stretch
};
let skip_shrink = if current.items.is_empty() {
0
} else {
par_skip_shrink
};
let base = if at_top { top_skip } else { *used + skip };
let available = page_goal - *insertion_height - bottom - base;
let lines = pending.lines.len();
@@ -1121,8 +1145,14 @@ fn place_paragraph_flow(
if at_top {
*used = top_skip.max(pending.height);
} else {
if skip > 0 {
current.items.push(VerticalItem::Glue(skip));
if skip != 0 || skip_stretch != 0 || skip_shrink != 0 {
current
.items
.push(VerticalItem::StretchGlue(VerticalGlue::new(
skip,
skip_stretch,
skip_shrink,
)));
}
*used = base + pending.height;
}
@@ -1148,8 +1178,14 @@ fn place_paragraph_flow(
if at_top {
*used = top_skip.max(head.height);
} else {
if skip > 0 {
current.items.push(VerticalItem::Glue(skip));
if skip != 0 || skip_stretch != 0 || skip_shrink != 0 {
current
.items
.push(VerticalItem::StretchGlue(VerticalGlue::new(
skip,
skip_stretch,
skip_shrink,
)));
}
*used = base + head.height;
}
@@ -1533,6 +1569,8 @@ impl Engine {
document.page_goal,
document.top_skip,
document.par_skip.max(0),
document.par_skip_stretch.max(0),
document.par_skip_shrink.max(0),
bottom,
);
}
@@ -2296,7 +2334,10 @@ impl Engine {
// replaying at the unit's first real item instead.
while first_node < nodes.len()
&& owners[first_node] == unit_index
&& matches!(node_items[first_node], VerticalItem::Glue(_))
&& matches!(
node_items[first_node],
VerticalItem::Glue(_) | VerticalItem::StretchGlue(_)
)
&& !matches!(
document.units[unit_index].items.first(),
Some(VerticalItem::Glue(_))
@@ -2595,6 +2636,8 @@ impl Engine {
next.page_goal,
next.top_skip,
next.par_skip.max(0),
next.par_skip_stretch.max(0),
next.par_skip_shrink.max(0),
bottom,
);
}
@@ -5887,6 +5930,29 @@ mod tests {
assert_eq!(engine.pages()[0].sources, vec![(1, source_hash(""))]);
}
#[test]
fn paragraph_skip_carries_its_stretch_into_pages() {
let first = Unit::paragraph(1, "alpha beta gamma delta epsilon zeta");
let second = Unit::paragraph(2, "eta theta iota kappa lambda mu nu");
let mut document = Document::new(550 * 65_536, vec![first, second]);
document.top_skip = 12 * 65_536;
document.par_skip_stretch = 6 * 65_536;
document.par_skip_shrink = 2 * 65_536;
let engine = Engine::layout_paged(&document).expect("paged layout");
let page = &engine.pages()[0];
assert!(
page.items.iter().any(|item| matches!(
item,
VerticalItem::StretchGlue(glue)
if glue.width == 0
&& glue.stretch == 6 * 65_536
&& glue.shrink == 2 * 65_536
)),
"parskip stretch missing from page items: {:?}",
page.items
);
}
#[test]
fn paged_layout_inserts_par_skip_between_paragraphs() {
let mut style = ParagraphStyle::default();