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 pages = Vec::new();
let mut page_penalties = Vec::new(); let mut page_penalties = Vec::new();
let mut page_depths = 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(); let mut held: BTreeMap<u8, Vec<VerticalItem>> = BTreeMap::new();
for fragment in builder.build(&nodes) { for fragment in builder.build(&nodes) {
let mut items = Vec::new(); let mut items = Vec::new();
@@ -1709,6 +1711,8 @@ impl Engine {
pages.push(page); pages.push(page);
page_penalties.push(fragment.penalty); page_penalties.push(fragment.penalty);
page_depths.push(fragment.depth); 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 // Insertion material that did not fit its class limit on the page where
@@ -1733,6 +1737,8 @@ impl Engine {
pages.push(page); pages.push(page);
page_penalties.push(0); page_penalties.push(0);
page_depths.push(0); page_depths.push(0);
page_stretches.push(0);
page_shrinks.push(0);
} }
if let Some(source) = &document.output_routine { if let Some(source) = &document.output_routine {
let program = output::OutputProgram::parse(source).ok()?; let program = output::OutputProgram::parse(source).ok()?;
@@ -1743,6 +1749,8 @@ impl Engine {
page_goal: document.page_goal, page_goal: document.page_goal,
page_total, page_total,
page_depth: page_depths.get(index).copied().unwrap_or(0), 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, vsize: document.page_goal,
top_skip: document.top_skip, top_skip: document.top_skip,
max_depth: document.max_depth, 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 //! Only the subset that decides *which* page box leaves the page builder is
//! supported today: `\shipout\box255`, `\relax`, `\message{...}` and //! supported today: `\shipout\box255`, `\relax`, `\message{...}` and
//! `\ifdim` comparisons over the page registers `\pagegoal`, `\pagetotal`, //! `\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. //! makes `Engine::layout_paged` fall back to the scrolling builder.
use std::fmt; use std::fmt;
@@ -17,6 +18,10 @@ pub struct OutputContext {
pub page_total: i64, pub page_total: i64,
/// TeX `\pagedepth`: the depth of the page box so far. /// TeX `\pagedepth`: the depth of the page box so far.
pub page_depth: i64, 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. /// TeX `\vsize`: the configured page height.
pub vsize: i64, pub vsize: i64,
/// TeX `\topskip`. /// TeX `\topskip`.
@@ -99,6 +104,8 @@ enum Register {
PageGoal, PageGoal,
PageTotal, PageTotal,
PageDepth, PageDepth,
PageStretch,
PageShrink,
Vsize, Vsize,
TopSkip, TopSkip,
MaxDepth, MaxDepth,
@@ -112,6 +119,8 @@ impl Dimension {
Register::PageGoal => context.page_goal, Register::PageGoal => context.page_goal,
Register::PageTotal => context.page_total, Register::PageTotal => context.page_total,
Register::PageDepth => context.page_depth, Register::PageDepth => context.page_depth,
Register::PageStretch => context.page_stretch,
Register::PageShrink => context.page_shrink,
Register::Vsize => context.vsize, Register::Vsize => context.vsize,
Register::TopSkip => context.top_skip, Register::TopSkip => context.top_skip,
Register::MaxDepth => context.max_depth, Register::MaxDepth => context.max_depth,
@@ -478,6 +487,8 @@ fn parse_dimension(tokens: &[Token], index: &mut usize) -> Result<Dimension, Str
"pagegoal" => Register::PageGoal, "pagegoal" => Register::PageGoal,
"pagetotal" => Register::PageTotal, "pagetotal" => Register::PageTotal,
"pagedepth" => Register::PageDepth, "pagedepth" => Register::PageDepth,
"pagestretch" => Register::PageStretch,
"pageshrink" => Register::PageShrink,
"vsize" => Register::Vsize, "vsize" => Register::Vsize,
"topskip" => Register::TopSkip, "topskip" => Register::TopSkip,
"maxdepth" => Register::MaxDepth, "maxdepth" => Register::MaxDepth,
@@ -566,6 +577,8 @@ mod tests {
page_goal: 612 * 65_536, page_goal: 612 * 65_536,
page_total, page_total,
page_depth: 0, page_depth: 0,
page_stretch: 0,
page_shrink: 0,
vsize: 612 * 65_536, vsize: 612 * 65_536,
top_skip: 10 * 65_536, top_skip: 10 * 65_536,
max_depth: 0, max_depth: 0,
@@ -581,6 +594,21 @@ mod tests {
assert_eq!(program.shipout_count(&context(0)).unwrap(), 1); 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] #[test]
fn page_depth_register_is_visible() { fn page_depth_register_is_visible() {
let program = OutputProgram::parse(r"\ifdim\pagedepth>4pt\shipout\box255\fi").unwrap(); 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, pub height: i64,
/// Depth of the page box, capped at `max_depth`. /// Depth of the page box, capped at `max_depth`.
pub depth: i64, 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 /// Accepted insertion space per class, including material held over from
/// the previous page. /// the previous page.
pub insertions: Vec<InsertionSpace>, pub insertions: Vec<InsertionSpace>,
@@ -90,6 +94,7 @@ struct Candidate {
badness: i64, badness: i64,
height: i64, height: i64,
depth: i64, depth: i64,
stretch: i64,
shrink: i64, shrink: i64,
reserved: i64, reserved: i64,
penalty: i64, penalty: i64,
@@ -201,6 +206,8 @@ impl PageBuilder {
end, end,
height, height,
depth, depth,
stretch,
shrink,
badness, badness,
badness + *penalty, badness + *penalty,
*penalty, *penalty,
@@ -242,6 +249,8 @@ impl PageBuilder {
best.end, best.end,
best.height, best.height,
best.depth, best.depth,
best.stretch,
best.shrink,
best.badness, best.badness,
best.cost, best.cost,
best.penalty, best.penalty,
@@ -321,6 +330,7 @@ impl PageBuilder {
badness, badness,
height, height,
depth, depth,
stretch,
shrink, shrink,
reserved, reserved,
end, end,
@@ -357,6 +367,8 @@ impl PageBuilder {
end: usize, end: usize,
height: i64, height: i64,
depth: i64, depth: i64,
stretch: i64,
shrink: i64,
badness: i64, badness: i64,
cost: i64, cost: i64,
penalty: i64, penalty: i64,
@@ -370,6 +382,8 @@ impl PageBuilder {
badness, badness,
height, height,
depth, depth,
stretch,
shrink,
insertions, insertions,
} }
} }
@@ -447,6 +461,17 @@ mod tests {
assert_eq!(pages[0].badness, 0); 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] #[test]
fn forced_penalty_breaks_the_page() { fn forced_penalty_breaks_the_page() {
let builder = PageBuilder::new(100); let builder = PageBuilder::new(100);
+2 -1
View File
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and `\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
PDF opt-in with `?engine=route-d`). 355 workspace tests are green and no oracle PDF opt-in with `?engine=route-d`). 357 workspace tests are green and no oracle
fixture is ignored. fixture is ignored.
Known gaps, in dependency order: Known gaps, in dependency order:
@@ -238,6 +238,7 @@ Scope:
`\topskip`, `\maxdepth`, `\splittopskip`, `\splitmaxdepth`; `\topskip`, `\maxdepth`, `\splittopskip`, `\splitmaxdepth`;
- TeX page builder: candidate breakpoints, cost = badness + penalty, page goal - TeX page builder: candidate breakpoints, cost = badness + penalty, page goal
recomputation as insertions arrive, `\pagegoal`, `\pagetotal`, `\pagedepth`, recomputation as insertions arrive, `\pagegoal`, `\pagetotal`, `\pagedepth`,
`\pagestretch`, `\pageshrink`,
`\pagestretch`, `\pagefilstretch`, `\pagefillstretch`, `\pagefilllstretch`, `\pagestretch`, `\pagefilstretch`, `\pagefillstretch`, `\pagefilllstretch`,
`\pageshrink`, `\pagedepth`; `\pageshrink`, `\pagedepth`;
- marks: `\mark`, `\botmark`, `\firstmark`, `\topmark`, `\marks` (marks are - marks: `\mark`, `\botmark`, `\firstmark`, `\topmark`, `\marks` (marks are
+5 -4
View File
@@ -241,9 +241,10 @@ oracle fixture is ignored.
subset lives in `output::OutputProgram` (`Document::output_routine`), which subset lives in `output::OutputProgram` (`Document::output_routine`), which
understands `\shipout\box255`, `\relax`, `\message{...}`, `\ifdim`, understands `\shipout\box255`, `\relax`, `\message{...}`, `\ifdim`,
`\ifnum`, `\ifodd` and `\ifcase` over the page registers `\pagegoal`, `\ifnum`, `\ifodd` and `\ifcase` over the page registers `\pagegoal`,
`\pagetotal`, `\pagedepth`, `\vsize`, `\topskip`, `\maxdepth`, `\pagetotal`, `\pagedepth`, `\pagestretch`, `\pageshrink`, `\vsize`,
`\outputpenalty`, `\pageno` and `\deadcycles`; it cannot yet change page `\topskip`, `\maxdepth`, `\outputpenalty`, `\pageno` and `\deadcycles`;
goals. Top `Float` `PageFragment` also reports the page's total glue stretch and shrink. It
cannot yet change page goals. Top `Float`
material is deferred to the next page when it arrives late; bottom material is deferred to the next page when it arrives late; bottom
floats reserve height on the current page and must satisfy floats reserve height on the current page and must satisfy
`Document.text_fraction_percent` (default 50%); a page holding only floats `Document.text_fraction_percent` (default 50%); a page holding only floats
@@ -334,7 +335,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph. Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
The crate currently has 237 unit tests plus seven route-D integration tests and thirteen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 355 tests (237 route-D unit, 7 route-D integration, 13 oracle, 71 v1 integration, 20 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. The crate currently has 239 unit tests plus seven route-D integration tests and thirteen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 357 tests (239 route-D unit, 7 route-D integration, 13 oracle, 71 v1 integration, 20 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
### M0 incremental demo ### M0 incremental demo