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

+40
View File
@@ -1727,6 +1727,9 @@ impl Engine {
let mut page_penalties = Vec::new();
let mut page_depths = Vec::new();
let mut page_stretches = Vec::new();
let mut page_fil_stretches = Vec::new();
let mut page_fill_stretches = Vec::new();
let mut page_filll_stretches = Vec::new();
let mut page_shrinks = Vec::new();
let mut held: BTreeMap<u8, Vec<VerticalItem>> = BTreeMap::new();
let fragments = builder.build(&nodes);
@@ -1798,6 +1801,9 @@ impl Engine {
page_penalties.push(fragment.penalty);
page_depths.push(fragment.depth);
page_stretches.push(fragment.stretch);
page_fil_stretches.push(fragment.stretches[1]);
page_fill_stretches.push(fragment.stretches[2]);
page_filll_stretches.push(fragment.stretches[3]);
page_shrinks.push(fragment.shrink);
}
@@ -1824,6 +1830,9 @@ impl Engine {
page_penalties.push(0);
page_depths.push(0);
page_stretches.push(0);
page_fil_stretches.push(0);
page_fill_stretches.push(0);
page_filll_stretches.push(0);
page_shrinks.push(0);
}
if let Some(source) = &document.output_routine {
@@ -1836,6 +1845,9 @@ impl Engine {
page_total,
page_depth: page_depths.get(index).copied().unwrap_or(0),
page_stretch: page_stretches.get(index).copied().unwrap_or(0),
page_fil_stretch: page_fil_stretches.get(index).copied().unwrap_or(0),
page_fill_stretch: page_fill_stretches.get(index).copied().unwrap_or(0),
page_filll_stretch: page_filll_stretches.get(index).copied().unwrap_or(0),
page_shrink: page_shrinks.get(index).copied().unwrap_or(0),
vsize: document.page_goal,
top_skip: document.top_skip,
@@ -5216,6 +5228,34 @@ mod tests {
assert_eq!(widths, vec![20, 80], "only the fil glue stretches");
}
#[test]
fn output_routine_sees_fill_stretch_orders() {
let mut document = Document::new(200, Vec::new());
document.units = vec![
Unit::rule(0, 10, 40),
Unit::vertical(
1,
vec![VerticalItem::StretchGlue(VerticalGlue::with_order(
20,
4 * 65_536,
0,
2,
))],
),
Unit::penalty(2, -10_000),
Unit::rule(3, 10, 40),
];
document.output_routine = Some(r"\ifdim\pagefillstretch>3pt\shipout\box255\fi".to_owned());
let engine = Engine::layout_paged(&document).expect("paged");
assert_eq!(engine.pages().len(), 1);
assert!(
engine.pages()[0]
.items
.iter()
.any(|item| matches!(item, VerticalItem::Rule { .. }))
);
}
#[test]
fn interpreted_output_sees_the_break_penalty() {
let mut document = Document::new(100, Vec::new());
+37
View File
@@ -20,6 +20,12 @@ pub struct OutputContext {
pub page_depth: i64,
/// TeX `\pagestretch`: stretch of the vertical glue so far.
pub page_stretch: i64,
/// TeX `\pagefilstretch`: stretch of `\vfil` glue on the page.
pub page_fil_stretch: i64,
/// TeX `\pagefillstretch`: stretch of `\vfill` glue on the page.
pub page_fill_stretch: i64,
/// TeX `\pagefilllstretch`: stretch of `\vfilll` glue on the page.
pub page_filll_stretch: i64,
/// TeX `\pageshrink`: shrink of the vertical glue so far.
pub page_shrink: i64,
/// TeX `\vsize`: the configured page height.
@@ -105,6 +111,9 @@ enum Register {
PageTotal,
PageDepth,
PageStretch,
PageFilStretch,
PageFillStretch,
PageFilllStretch,
PageShrink,
Vsize,
TopSkip,
@@ -120,6 +129,9 @@ impl Dimension {
Register::PageTotal => context.page_total,
Register::PageDepth => context.page_depth,
Register::PageStretch => context.page_stretch,
Register::PageFilStretch => context.page_fil_stretch,
Register::PageFillStretch => context.page_fill_stretch,
Register::PageFilllStretch => context.page_filll_stretch,
Register::PageShrink => context.page_shrink,
Register::Vsize => context.vsize,
Register::TopSkip => context.top_skip,
@@ -488,6 +500,9 @@ fn parse_dimension(tokens: &[Token], index: &mut usize) -> Result<Dimension, Str
"pagetotal" => Register::PageTotal,
"pagedepth" => Register::PageDepth,
"pagestretch" => Register::PageStretch,
"pagefilstretch" => Register::PageFilStretch,
"pagefillstretch" => Register::PageFillStretch,
"pagefilllstretch" => Register::PageFilllStretch,
"pageshrink" => Register::PageShrink,
"vsize" => Register::Vsize,
"topskip" => Register::TopSkip,
@@ -578,6 +593,9 @@ mod tests {
page_total,
page_depth: 0,
page_stretch: 0,
page_fil_stretch: 0,
page_fill_stretch: 0,
page_filll_stretch: 0,
page_shrink: 0,
vsize: 612 * 65_536,
top_skip: 10 * 65_536,
@@ -609,6 +627,25 @@ mod tests {
assert_eq!(shrink.shipout_count(&context(0)).unwrap(), 0);
}
#[test]
fn fill_stretch_registers_are_visible() {
let program = OutputProgram::parse(r"\ifdim\pagefilstretch>9pt\shipout\box255\fi").unwrap();
let mut context = context(0);
context.page_fil_stretch = 10 * 65_536;
assert_eq!(program.shipout_count(&context).unwrap(), 1);
assert_eq!(program.shipout_count(&self::context(0)).unwrap(), 0);
let fill = OutputProgram::parse(r"\ifdim\pagefillstretch>4pt\shipout\box255\fi").unwrap();
let mut context = self::context(0);
context.page_fill_stretch = 5 * 65_536;
assert_eq!(fill.shipout_count(&context).unwrap(), 1);
let filll = OutputProgram::parse(r"\ifdim\pagefilllstretch>4pt\shipout\box255\fi").unwrap();
let mut context = self::context(0);
context.page_filll_stretch = 5 * 65_536;
assert_eq!(filll.shipout_count(&context).unwrap(), 1);
}
#[test]
fn page_depth_register_is_visible() {
let program = OutputProgram::parse(r"\ifdim\pagedepth>4pt\shipout\box255\fi").unwrap();
+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);