Reserve insertion class skip in the paged builder

This commit is contained in:
gloamxun committed 2026-10-24 00:23:55 +08:00
1 parent 1dc3e95a04
commit 0805f3c932
4 files changed
+90 -6

No files matched your search

+22 -2
View File
@@ -2240,13 +2240,18 @@ impl Engine {
page.floats = floats;
let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum();
page.insertions = insertions.into_values().collect();
let insertion_skip: i64 = page
.insertions
.iter()
.map(|insertion| document.insertion_skip_for(insertion.class))
.sum();
page.sources = sources;
page.seal_in_place();
let shipped = match routine.as_mut() {
Some(routine) => routine.ship(
document,
&page,
(document.page_goal - reserved).max(0),
(document.page_goal - reserved - insertion_skip).max(0),
fragment.penalty,
fragment.depth,
fragment.stretch,
@@ -2287,12 +2292,17 @@ impl Engine {
let mut page = Page::empty();
let reserved: i64 = insertions.values().map(|insertion| insertion.height).sum();
page.insertions = insertions.into_values().collect();
let insertion_skip: i64 = page
.insertions
.iter()
.map(|insertion| document.insertion_skip_for(insertion.class))
.sum();
page.seal_in_place();
let shipped = match routine.as_mut() {
Some(routine) => routine.ship(
document,
&page,
(document.page_goal - reserved).max(0),
(document.page_goal - reserved - insertion_skip).max(0),
10_000,
0,
0,
@@ -2359,6 +2369,16 @@ impl Engine {
height: document.insertion_class_limit(class.number),
})
.collect(),
insertion_skips: document
.insertion_classes
.iter()
.map(|class| {
(
class.number,
class.skip.unwrap_or(document.insertion_skip).max(0),
)
})
.collect(),
text_float_sep: document.text_float_sep.max(0),
float_sep: document.float_sep.max(0),
bottom_fraction_percent: document.bottom_fraction_percent,
+64
View File
@@ -151,6 +151,9 @@ pub struct PageBuilder {
pub top_skip: i64,
pub max_depth: i64,
pub insertion_limits: Vec<InsertionLimit>,
/// TeX `\skip<number>`: space above an insertion class on a page that
/// carries it. Applied once per class, on top of the class limit.
pub insertion_skips: Vec<(u8, i64)>,
/// TeX `\textfloatsep`: space between top floats and the body.
pub text_float_sep: i64,
/// TeX floatsep: space between stacked bottom floats.
@@ -177,6 +180,7 @@ impl PageBuilder {
top_skip: 0,
max_depth: 0,
insertion_limits: Vec::new(),
insertion_skips: Vec::new(),
text_float_sep: 0,
float_sep: 0,
bottom_fraction_percent: 100,
@@ -196,6 +200,14 @@ impl PageBuilder {
.map_or(self.page_goal, |limit| limit.height.max(0))
}
/// TeX's `\skip<number>` for `class`.
fn insertion_skip(&self, class: u8) -> i64 {
self.insertion_skips
.iter()
.find(|(number, _)| *number == class)
.map_or(0, |(_, skip)| (*skip).max(0))
}
/// Replace the page goal used by later `build`/`build_from` calls.
pub fn set_page_goal(&mut self, page_goal: i64) {
self.page_goal = page_goal.max(1);
@@ -594,7 +606,12 @@ impl PageBuilder {
height: insert,
} => {
let limit = self.insertion_limit(*class);
let first = !usage.contains_key(class);
let skip = self.insertion_skip(*class);
let used = usage.entry(*class).or_insert(0);
if first {
reserved += skip;
}
let capacity = (limit - *used).max(0);
let taken = (*insert).min(capacity);
*used += taken;
@@ -1135,6 +1152,53 @@ mod tests {
InsertionLimit { class, height }
}
#[test]
fn insertion_skip_reserves_space_once_per_class() {
let nodes = [
VerticalNode::Box {
height: 30,
depth: 0,
},
VerticalNode::Glue(VerticalGlue {
width: 0,
stretch: 100,
shrink: 0,
order: 0,
}),
VerticalNode::Insertion {
class: 1,
height: 50,
},
VerticalNode::Glue(VerticalGlue {
width: 0,
stretch: 100,
shrink: 0,
order: 0,
}),
VerticalNode::Box {
height: 20,
depth: 0,
},
];
let mut plain = PageBuilder::new(100);
plain.insertion_limits = vec![InsertionLimit {
class: 1,
height: 100,
}];
assert_eq!(plain.build(&nodes).len(), 1);
let mut skipped = PageBuilder::new(100);
skipped.insertion_limits = vec![InsertionLimit {
class: 1,
height: 100,
}];
skipped.insertion_skips = vec![(1, 10)];
let pages = skipped.build(&nodes);
assert_eq!(pages.len(), 2);
assert_eq!(pages[0].start, 0);
assert_eq!(pages[0].end, 3);
}
#[test]
fn insertion_reservation_pushes_the_break_earlier() {
let mut builder = PageBuilder::new(100);