Set pages with TeX glue orders

This commit is contained in:
gloamxun committed 2026-10-23 04:18:14 +08:00
1 parent 00c23a3511
commit 52e3e0647c
5 files changed
+172 -25

No files matched your search

+57
View File
@@ -457,6 +457,19 @@ impl Unit {
Self::vertical(id, vec![VerticalItem::Glue(height)])
}
/// Vertical glue with explicit stretch, shrink and TeX glue order.
///
/// Order 0 is ordinary glue, 1 is `\vfil`, 2 `\vfill`, 3 `\vfilll`; a
/// page is set with the highest order that carries stretch.
pub fn vglue(id: UnitId, width: i64, stretch: i64, shrink: i64, order: u8) -> Self {
Self::vertical(
id,
vec![VerticalItem::StretchGlue(VerticalGlue::with_order(
width, stretch, shrink, order,
))],
)
}
/// A filled rule in the vertical list.
pub fn rule(id: UnitId, width: i64, height: i64) -> Self {
Self::vertical(id, vec![VerticalItem::Rule { width, height }])
@@ -1901,6 +1914,13 @@ impl Engine {
if fragment.set == 0 {
return;
}
// `\vfil` and higher orders absorb the whole setting themselves;
// ordinary glue only shares when no fill order is present.
let order = if fragment.set > 0 {
fragment.stretch_order
} else {
0
};
let mut remaining = fragment.set;
let mut total = if remaining > 0 {
fragment.stretch
@@ -1911,6 +1931,9 @@ impl Engine {
let VerticalItem::StretchGlue(glue) = item else {
continue;
};
if glue.order != order {
continue;
}
let share = if remaining > 0 {
glue.stretch.max(0)
} else {
@@ -5142,6 +5165,7 @@ mod tests {
assert_eq!(rule_ys(&flush), vec![0, 80]);
}
#[test]
fn paged_layout_breaks_simple_vertical_lists() {
let document = Document::new(
100,
@@ -5159,6 +5183,39 @@ mod tests {
assert!(engine.pages().iter().all(|page| page.hash != 0));
}
#[test]
fn fill_glue_absorbs_the_page_set() {
let mut document = Document::new(
200,
vec![
Unit::rule(1, 100, 100),
Unit::vertical(
2,
vec![VerticalItem::StretchGlue(VerticalGlue::new(20, 10, 0))],
),
Unit::vertical(
3,
vec![VerticalItem::StretchGlue(VerticalGlue::with_order(
20, 1, 0, 1,
))],
),
Unit::penalty(4, -10_000),
],
);
document.ragged_bottom = false;
let engine = Engine::layout_paged(&document).expect("paged");
let page = &engine.pages()[0];
let widths: Vec<i64> = page
.items
.iter()
.filter_map(|item| match item {
VerticalItem::StretchGlue(glue) => Some(glue.width),
_ => None,
})
.collect();
assert_eq!(widths, vec![20, 80], "only the fil glue stretches");
}
#[test]
fn interpreted_output_sees_the_break_penalty() {
let mut document = Document::new(100, Vec::new());