Expose page stretch and shrink to the output routine

This commit is contained in:
gloamxun committed 2026-10-23 03:23:57 +08:00
1 parent 62d3c4154e
commit ca36c94eb3
5 files changed
+69 -6

No files matched your search

+8
View File
@@ -1651,6 +1651,8 @@ impl Engine {
let mut pages = Vec::new();
let mut page_penalties = Vec::new();
let mut page_depths = Vec::new();
let mut page_stretches = Vec::new();
let mut page_shrinks = Vec::new();
let mut held: BTreeMap<u8, Vec<VerticalItem>> = BTreeMap::new();
for fragment in builder.build(&nodes) {
let mut items = Vec::new();
@@ -1709,6 +1711,8 @@ impl Engine {
pages.push(page);
page_penalties.push(fragment.penalty);
page_depths.push(fragment.depth);
page_stretches.push(fragment.stretch);
page_shrinks.push(fragment.shrink);
}
// Insertion material that did not fit its class limit on the page where
@@ -1733,6 +1737,8 @@ impl Engine {
pages.push(page);
page_penalties.push(0);
page_depths.push(0);
page_stretches.push(0);
page_shrinks.push(0);
}
if let Some(source) = &document.output_routine {
let program = output::OutputProgram::parse(source).ok()?;
@@ -1743,6 +1749,8 @@ impl Engine {
page_goal: document.page_goal,
page_total,
page_depth: page_depths.get(index).copied().unwrap_or(0),
page_stretch: page_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,
max_depth: document.max_depth,
+29 -1
View File
@@ -3,7 +3,8 @@
//! Only the subset that decides *which* page box leaves the page builder is
//! supported today: `\shipout\box255`, `\relax`, `\message{...}` and
//! `\ifdim` comparisons over the page registers `\pagegoal`, `\pagetotal`,
//! `\pagedepth`, `\vsize`, `\topskip` and `\maxdepth`. Anything else is a parse error, which
//! `\pagedepth`, `\pagestretch`, `\pageshrink`, `\vsize`, `\topskip` and
//! `\maxdepth`. Anything else is a parse error, which
//! makes `Engine::layout_paged` fall back to the scrolling builder.
use std::fmt;
@@ -17,6 +18,10 @@ pub struct OutputContext {
pub page_total: i64,
/// TeX `\pagedepth`: the depth of the page box so far.
pub page_depth: i64,
/// TeX `\pagestretch`: stretch of the vertical glue so far.
pub page_stretch: i64,
/// TeX `\pageshrink`: shrink of the vertical glue so far.
pub page_shrink: i64,
/// TeX `\vsize`: the configured page height.
pub vsize: i64,
/// TeX `\topskip`.
@@ -99,6 +104,8 @@ enum Register {
PageGoal,
PageTotal,
PageDepth,
PageStretch,
PageShrink,
Vsize,
TopSkip,
MaxDepth,
@@ -112,6 +119,8 @@ impl Dimension {
Register::PageGoal => context.page_goal,
Register::PageTotal => context.page_total,
Register::PageDepth => context.page_depth,
Register::PageStretch => context.page_stretch,
Register::PageShrink => context.page_shrink,
Register::Vsize => context.vsize,
Register::TopSkip => context.top_skip,
Register::MaxDepth => context.max_depth,
@@ -478,6 +487,8 @@ fn parse_dimension(tokens: &[Token], index: &mut usize) -> Result<Dimension, Str
"pagegoal" => Register::PageGoal,
"pagetotal" => Register::PageTotal,
"pagedepth" => Register::PageDepth,
"pagestretch" => Register::PageStretch,
"pageshrink" => Register::PageShrink,
"vsize" => Register::Vsize,
"topskip" => Register::TopSkip,
"maxdepth" => Register::MaxDepth,
@@ -566,6 +577,8 @@ mod tests {
page_goal: 612 * 65_536,
page_total,
page_depth: 0,
page_stretch: 0,
page_shrink: 0,
vsize: 612 * 65_536,
top_skip: 10 * 65_536,
max_depth: 0,
@@ -581,6 +594,21 @@ mod tests {
assert_eq!(program.shipout_count(&context(0)).unwrap(), 1);
}
#[test]
fn page_stretch_and_shrink_are_visible() {
let program = OutputProgram::parse(r"\ifdim\pagestretch>9pt\shipout\box255\fi").unwrap();
let mut stretched = context(0);
stretched.page_stretch = 10 * 65_536;
assert_eq!(program.shipout_count(&stretched).unwrap(), 1);
assert_eq!(program.shipout_count(&context(0)).unwrap(), 0);
let shrink = OutputProgram::parse(r"\ifdim\pageshrink>9pt\shipout\box255\fi").unwrap();
let mut shrunk = context(0);
shrunk.page_shrink = 10 * 65_536;
assert_eq!(shrink.shipout_count(&shrunk).unwrap(), 1);
assert_eq!(shrink.shipout_count(&context(0)).unwrap(), 0);
}
#[test]
fn page_depth_register_is_visible() {
let program = OutputProgram::parse(r"\ifdim\pagedepth>4pt\shipout\box255\fi").unwrap();
+25
View File
@@ -79,6 +79,10 @@ pub struct PageFragment {
pub height: i64,
/// Depth of the page box, capped at `max_depth`.
pub depth: i64,
/// Total stretch of the vertical glue on the page.
pub stretch: i64,
/// Total shrink of the vertical glue on the page.
pub shrink: i64,
/// Accepted insertion space per class, including material held over from
/// the previous page.
pub insertions: Vec<InsertionSpace>,
@@ -90,6 +94,7 @@ struct Candidate {
badness: i64,
height: i64,
depth: i64,
stretch: i64,
shrink: i64,
reserved: i64,
penalty: i64,
@@ -201,6 +206,8 @@ impl PageBuilder {
end,
height,
depth,
stretch,
shrink,
badness,
badness + *penalty,
*penalty,
@@ -242,6 +249,8 @@ impl PageBuilder {
best.end,
best.height,
best.depth,
best.stretch,
best.shrink,
best.badness,
best.cost,
best.penalty,
@@ -321,6 +330,7 @@ impl PageBuilder {
badness,
height,
depth,
stretch,
shrink,
reserved,
end,
@@ -357,6 +367,8 @@ impl PageBuilder {
end: usize,
height: i64,
depth: i64,
stretch: i64,
shrink: i64,
badness: i64,
cost: i64,
penalty: i64,
@@ -370,6 +382,8 @@ impl PageBuilder {
badness,
height,
depth,
stretch,
shrink,
insertions,
}
}
@@ -447,6 +461,17 @@ mod tests {
assert_eq!(pages[0].badness, 0);
}
#[test]
fn fragments_report_glue_stretch_and_shrink() {
let builder = PageBuilder::new(100);
let nodes = vec![box_(40, 0), glue(0, 20, 5), box_(40, 0)];
let pages = builder.build(&nodes);
assert_eq!(pages.len(), 1);
assert_eq!(pages[0].height, 80);
assert_eq!(pages[0].stretch, 20);
assert_eq!(pages[0].shrink, 5);
}
#[test]
fn forced_penalty_breaks_the_page() {
let builder = PageBuilder::new(100);