Report page badness and held-over insertions to the output routine

This commit is contained in:
gloamxun committed 2026-10-24 00:02:46 +08:00
1 parent bf31936146
commit 01ed1b7c21
5 files changed
+89 -6

No files matched your search

+66
View File
@@ -1990,6 +1990,8 @@ impl Engine {
let mut page_fill_stretches = Vec::new();
let mut page_filll_stretches = Vec::new();
let mut page_shrinks = Vec::new();
let mut page_badnesses = Vec::new();
let mut page_insert_penalties = Vec::new();
let mut page_goals: Vec<i64> = Vec::new();
let mut held: BTreeMap<u8, Vec<VerticalItem>> = BTreeMap::new();
let fragments = builder.build_with_deferred_bottom_floats(&nodes);
@@ -2009,6 +2011,7 @@ impl Engine {
})
.collect();
let mut sources = Vec::new();
let mut insert_penalties = 0_i64;
let mut insertions: BTreeMap<u8, Insertion> = BTreeMap::new();
let mut used: BTreeMap<u8, i64> = BTreeMap::new();
@@ -2047,6 +2050,7 @@ impl Engine {
);
attach_insertion(&mut insertions, &mut used, class, accepted, reserved);
if !remainder.is_empty() {
insert_penalties += remainder.len() as i64;
held.entry(class).or_default().extend(remainder);
}
}
@@ -2079,6 +2083,8 @@ impl Engine {
page_fill_stretches.push(fragment.stretches[2]);
page_filll_stretches.push(fragment.stretches[3]);
page_shrinks.push(fragment.shrink);
page_badnesses.push(fragment.badness);
page_insert_penalties.push(insert_penalties);
}
// Insertion material that did not fit its class limit on the page where
@@ -2111,6 +2117,8 @@ impl Engine {
page_fill_stretches.push(0);
page_filll_stretches.push(0);
page_shrinks.push(0);
page_badnesses.push(10_000);
page_insert_penalties.push(0);
}
if let Some(source) = &document.output_routine {
let program = output::OutputProgram::parse(source).ok()?;
@@ -2139,6 +2147,7 @@ impl Engine {
page_fill_stretch: page_fill_stretches.get(index).copied().unwrap_or(0),
page_filll_stretch: page_filll_stretches.get(index).copied().unwrap_or(0),
page_shrink: page_shrinks.get(index).copied().unwrap_or(0),
vbadness: page_badnesses.get(index).copied().unwrap_or(10_000),
vsize: document.page_goal,
top_skip: document.top_skip,
max_depth: document.max_depth,
@@ -2150,6 +2159,7 @@ impl Engine {
pageno,
deadcycles,
prev_graf: page_prev_graf(&page),
insert_penalties: page_insert_penalties.get(index).copied().unwrap_or(0),
};
let forced = deadcycles >= document.max_deadcycles.max(0);
if forced {
@@ -2338,6 +2348,7 @@ impl Engine {
page_fill_stretch: fragment.stretches[2],
page_filll_stretch: fragment.stretches[3],
page_shrink: fragment.shrink,
vbadness: fragment.badness,
vsize: goal,
top_skip: document.top_skip,
max_depth: document.max_depth,
@@ -2347,6 +2358,7 @@ impl Engine {
insertion_dimens,
insertion_skips,
prev_graf: page_prev_graf(&page),
insert_penalties: 0,
pageno,
deadcycles,
};
@@ -3227,6 +3239,60 @@ mod tests {
assert_eq!(engine.pages().len(), 1);
}
#[test]
fn output_routine_sees_page_break_badness() {
// The first page overfills the 50pt goal, so its break is maximally
// bad; the second page fits exactly. Only the overfull page ships.
let mut document = Document::new(
50,
vec![
Unit::rule(1, 0, 60),
Unit::penalty(2, 0),
Unit::rule(3, 0, 50),
],
);
document.output_routine = Some(r"\ifnum\vbadness=10000\shipout\box255\fi".to_owned());
let engine = Engine::layout_paged(&document).expect("paged");
assert_eq!(engine.pages().len(), 1);
}
#[test]
fn output_routine_sees_insertion_penalties() {
// The class limit fits one 20pt item, so the second is held over and
// reported through \insertpenalties.
let mut document = Document::new(
200,
vec![
Unit::rule(1, 0, 20),
Unit::insertion(
2,
0,
40,
vec![
VerticalItem::Rule {
width: 0,
height: 20,
},
VerticalItem::Rule {
width: 0,
height: 20,
},
],
),
],
);
document.insertion_classes = vec![InsertionClass {
number: 0,
fraction_percent: 100,
max_height: Some(20),
skip: None,
multiplier: 1000,
}];
document.output_routine = Some(r"\ifnum\insertpenalties=1\shipout\box255\fi".to_owned());
let engine = Engine::layout_paged(&document).expect("paged");
assert_eq!(engine.pages().len(), 1);
}
#[test]
fn output_routine_can_force_an_early_page_break() {
struct BreakAfterTwo {