Expose per-order page stretch to the output routine

This commit is contained in:
gloamxun committed 2026-10-23 04:22:27 +08:00
1 parent 52e3e0647c
commit a4388942f0
6 files changed
+117 -5

No files matched your search

+35
View File
@@ -112,6 +112,9 @@ pub struct PageFragment {
pub stretch_order: u8,
/// Total shrink of the vertical glue on the page.
pub shrink: i64,
/// Stretch of each glue order: index 0 is normal, 1 is `\vfil`, 2 is
/// `\vfill` and 3 is `\vfilll`.
pub stretches: [i64; 4],
/// Signed glue-setting amount applied to the page: positive stretch to
/// fill the goal, negative shrink to fit it (zero when ragged).
pub set: i64,
@@ -283,6 +286,7 @@ impl PageBuilder {
badness,
0,
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
);
return Some((
@@ -323,6 +327,7 @@ impl PageBuilder {
badness,
0,
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
);
return Some((
@@ -372,6 +377,7 @@ impl PageBuilder {
badness + *penalty,
*penalty,
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
);
return Some((
@@ -434,6 +440,7 @@ impl PageBuilder {
best.cost,
best.penalty,
best.bottom,
order_stretches(&nodes[start..best.end]),
self.insertion_space(nodes, start, best.end, held),
);
Some((
@@ -570,6 +577,7 @@ impl PageBuilder {
cost: i64,
penalty: i64,
bottom: i64,
stretches: [i64; 4],
insertions: Vec<InsertionSpace>,
) -> PageFragment {
let reserved: i64 = insertions.iter().map(|space| space.height).sum();
@@ -597,6 +605,7 @@ impl PageBuilder {
stretch,
stretch_order,
shrink,
stretches,
set,
insertions,
}
@@ -625,6 +634,17 @@ impl PageBuilder {
}
}
fn order_stretches(nodes: &[VerticalNode]) -> [i64; 4] {
let mut stretches = [0_i64; 4];
for node in nodes {
if let VerticalNode::Glue(glue) = node {
let order = usize::from(glue.order.min(3));
stretches[order] += glue.stretch.max(0);
}
}
stretches
}
fn effective_stretch(stretch_orders: &[i64; 4]) -> (i64, u8) {
for (order, stretch) in stretch_orders.iter().enumerate().rev() {
if *stretch > 0 {
@@ -727,6 +747,21 @@ mod tests {
assert_eq!(pages[0].shrink, 5);
}
#[test]
fn fragments_record_stretch_by_glue_order() {
let builder = PageBuilder::new(200);
let nodes = vec![
box_(50, 0),
VerticalNode::Glue(VerticalGlue::with_order(0, 20, 0, 0)),
VerticalNode::Glue(VerticalGlue::with_order(0, 10, 0, 1)),
VerticalNode::Glue(VerticalGlue::with_order(0, 5, 0, 2)),
box_(50, 0),
];
let pages = builder.build(&nodes);
assert_eq!(pages.len(), 1);
assert_eq!(pages[0].stretches, [20, 10, 5, 0]);
}
#[test]
fn forced_penalty_breaks_the_page() {
let builder = PageBuilder::new(100);