Set vertical glue for ragged and flushed pages

This commit is contained in:
gloamxun committed 2026-10-23 04:13:12 +08:00
1 parent 448152cea4
commit 00c23a3511
6 files changed
+143 -14

No files matched your search

+36 -1
View File
@@ -12,7 +12,7 @@ use std::collections::BTreeMap;
use crate::Mark;
/// Vertical glue with the stretch and shrink TeX uses to set a page.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct VerticalGlue {
pub width: i64,
pub stretch: i64,
@@ -95,6 +95,9 @@ pub struct PageFragment {
pub stretch: i64,
/// Total shrink of the vertical glue on the page.
pub shrink: i64,
/// 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,
/// Accepted insertion space per class, including material held over from
/// the previous page.
pub insertions: Vec<InsertionSpace>,
@@ -129,6 +132,8 @@ pub struct PageBuilder {
pub float_sep: i64,
/// TeX bottomfraction: percent of the page bottom floats may occupy.
pub bottom_fraction_percent: u8,
/// TeX `\raggedbottom` (default) versus `\flushbottom`.
pub ragged_bottom: bool,
}
impl PageBuilder {
@@ -141,6 +146,7 @@ impl PageBuilder {
text_float_sep: 0,
float_sep: 0,
bottom_fraction_percent: 100,
ragged_bottom: true,
}
}
@@ -506,6 +512,19 @@ impl PageBuilder {
bottom: i64,
insertions: Vec<InsertionSpace>,
) -> PageFragment {
let reserved: i64 = insertions.iter().map(|space| space.height).sum();
let target = (self.page_goal - bottom - reserved).max(0);
let set = if self.ragged_bottom {
0
} else if height < target {
// TeX stretches glue past its nominal amount (the glue set ratio
// is unbounded upward); only shrink is clamped.
target - height
} else if height > target {
-(height - target).min(shrink)
} else {
0
};
PageFragment {
start,
end,
@@ -517,6 +536,7 @@ impl PageBuilder {
depth,
stretch,
shrink,
set,
insertions,
}
}
@@ -595,6 +615,21 @@ mod tests {
}
#[test]
#[test]
fn flush_bottom_stretches_glue_to_the_goal() {
let nodes = vec![box_(40, 0), glue(0, 10, 0), box_(40, 0)];
let mut builder = PageBuilder::new(120);
assert_eq!(
builder.build(&nodes)[0].set,
0,
"ragged pages do not set glue"
);
builder.ragged_bottom = false;
let pages = builder.build(&nodes);
assert_eq!(pages.len(), 1);
assert_eq!(pages[0].set, 40);
}
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)];