Cap page depth at Document max_depth

This commit is contained in:
gloamxun committed 2026-10-23 01:48:21 +08:00
1 parent 9599982b91
commit 169119a48d
5 files changed
+66 -10

No files matched your search

+37 -1
View File
@@ -557,6 +557,10 @@ pub struct Document {
/// TeX-like `\topskip`: distance from the page top to the first baseline.
#[serde(default)]
pub top_skip: i64,
/// TeX-like `\maxdepth`: depth a page box keeps below the baseline. Deeper
/// material moves into the page height, as in TeX's page builder.
#[serde(default)]
pub max_depth: i64,
/// Minimum percentage of the page that text must occupy before a bottom
/// float is allowed onto the page (TeX's `\textfraction`, in percent).
#[serde(default = "default_text_fraction")]
@@ -646,6 +650,7 @@ impl Document {
units,
page_goal,
top_skip: 0,
max_depth: 0,
text_fraction_percent: default_text_fraction(),
float_page_fraction_percent: 50,
bottom_fraction_percent: default_bottom_fraction(),
@@ -1390,7 +1395,7 @@ impl Engine {
let builder = PageBuilder {
page_goal: document.page_goal,
top_skip: document.top_skip,
max_depth: 0,
max_depth: document.max_depth,
};
let mut pages = Vec::new();
for fragment in builder.build(&nodes) {
@@ -4368,6 +4373,37 @@ mod tests {
assert!(engine.pages().iter().all(|page| page.hash != 0));
}
#[test]
fn paged_layout_limits_page_depth() {
let math = Unit {
id: 1,
source: String::new(),
items: vec![VerticalItem::Math {
atom: crate::math::MathAtom::Symbol('x'),
width: 10,
height: 70,
depth: 30,
}],
paragraph_style: None,
};
let mut document =
Document::new(100, vec![math, Unit::vskip(2, 0), Unit::rule(3, 100, 20)]);
let deep = Engine::layout_paged(&document).expect("math is page-buildable");
assert_eq!(
deep.pages().len(),
2,
"depth beyond maxdepth pushes the rule onto its own page"
);
document.max_depth = 30;
let capped = Engine::layout_paged(&document).expect("math is page-buildable");
assert_eq!(
capped.pages().len(),
1,
"allowed depth keeps the rule on the first page"
);
}
#[test]
fn paged_layout_refuses_floats_and_insertions() {
let floated = Document::new(100, vec![Unit::float(1, FloatPlacement::Top, 100, 20)]);
+20 -2
View File
@@ -8,7 +8,6 @@
//! `Engine`; wiring this module into `Document` is the next P2 step.
use crate::Mark;
use std::cmp::Reverse;
/// Vertical glue with the stretch and shrink TeX uses to set a page.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -250,7 +249,7 @@ impl PageBuilder {
feasible
};
pool.into_iter()
.min_by_key(|candidate| (candidate.cost, Reverse(candidate.height)))
.min_by_key(|candidate| (candidate.cost, (candidate.height - self.page_goal).abs()))
}
}
@@ -347,6 +346,25 @@ mod tests {
assert_eq!(pages[0].depth, 4);
}
#[test]
fn overfull_pages_take_the_least_overfull_break() {
let builder = PageBuilder::new(50);
let nodes = [
VerticalNode::Box {
height: 60,
depth: 0,
},
VerticalNode::Glue(VerticalGlue::new(0, 0, 0)),
VerticalNode::Box {
height: 40,
depth: 0,
},
];
let pages = builder.build(&nodes);
assert_eq!(pages.len(), 2);
assert_eq!(pages[0].height, 60, "the shorter overfull page wins");
}
#[test]
fn marks_do_not_change_page_heights() {
let builder = PageBuilder::new(100);