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 {
+16
View File
@@ -30,6 +30,8 @@ pub struct OutputContext {
pub page_filll_stretch: i64,
/// TeX `\pageshrink`: shrink of the vertical glue so far.
pub page_shrink: i64,
/// TeX `\vbadness`: badness of the break that produced the page.
pub vbadness: i64,
/// TeX `\vsize`: the configured page height.
pub vsize: i64,
/// TeX `\topskip`.
@@ -47,6 +49,8 @@ pub struct OutputContext {
pub max_deadcycles: i64,
/// TeX `\prevgraf`: lines in the paragraph most recently broken.
pub prev_graf: i64,
/// TeX `\insertpenalties`: insertion items held over from the page.
pub insert_penalties: i64,
/// TeX `\count<n>` for insertion class `n`: the insertion multiplier.
pub insertion_counts: [i64; 256],
/// TeX `\dimen<n>` for insertion class `n`: the class' maximum size.
@@ -66,6 +70,7 @@ impl Default for OutputContext {
page_fill_stretch: 0,
page_filll_stretch: 0,
page_shrink: 0,
vbadness: 0,
vsize: 0,
top_skip: 0,
max_depth: 0,
@@ -74,6 +79,7 @@ impl Default for OutputContext {
deadcycles: 0,
max_deadcycles: 25,
prev_graf: 0,
insert_penalties: 0,
// TeX's default insertion multiplier is 1000.
insertion_counts: [1000; 256],
insertion_dimens: [0; 256],
@@ -298,6 +304,8 @@ enum IntegerRegister {
PageNo,
DeadCycles,
MaxDeadCycles,
VBadness,
InsertPenalties,
OutputPenalty,
PrevGraf,
InsertionCount(u8),
@@ -313,6 +321,8 @@ impl Integer {
IntegerRegister::MaxDeadCycles => context.max_deadcycles,
IntegerRegister::OutputPenalty => context.output_penalty,
IntegerRegister::PrevGraf => context.prev_graf,
IntegerRegister::VBadness => context.vbadness,
IntegerRegister::InsertPenalties => context.insert_penalties,
IntegerRegister::InsertionCount(class) => context.insertion_counts[class as usize],
},
}
@@ -726,6 +736,8 @@ fn parse_integer(tokens: &[Token], index: &mut usize) -> Result<Integer, String>
"maxdeadcycles" => IntegerRegister::MaxDeadCycles,
"outputpenalty" => IntegerRegister::OutputPenalty,
"prevgraf" => IntegerRegister::PrevGraf,
"vbadness" => IntegerRegister::VBadness,
"insertpenalties" => IntegerRegister::InsertPenalties,
"count" => {
*index += 1;
if matches!(
@@ -949,6 +961,8 @@ fn read_integer(context: &OutputContext, register: IntegerRegister) -> i64 {
IntegerRegister::MaxDeadCycles => context.max_deadcycles,
IntegerRegister::OutputPenalty => context.output_penalty,
IntegerRegister::PrevGraf => context.prev_graf,
IntegerRegister::VBadness => context.vbadness,
IntegerRegister::InsertPenalties => context.insert_penalties,
IntegerRegister::InsertionCount(class) => context.insertion_counts[class as usize],
}
}
@@ -963,6 +977,8 @@ fn write_integer(context: &mut OutputContext, register: IntegerRegister, value:
IntegerRegister::InsertionCount(class) => {
context.insertion_counts[class as usize] = value;
}
// Read-only for the output routine: the parser never assigns them.
IntegerRegister::VBadness | IntegerRegister::InsertPenalties => {}
}
}