Let custom output routines force a page break

This commit is contained in:
gloamxun committed 2026-10-20 21:25:00 +08:00
1 parent 77f05513ed
commit 7e0090c7a5
2 files changed
+50 -2

No files matched your search

+48
View File
@@ -667,6 +667,13 @@ impl Layout {
/// than an interpreted token list.
pub trait OutputRoutine {
fn shipout(&mut self, page: &mut Page, page_number: usize);
/// TeX's output routine may decide to ship a page before it is full.
/// This hook is consulted between units, so a return of `true` seals the
/// current non-empty page and starts the next one.
fn wants_break(&mut self, _page: &Page, _used: i64, _page_goal: i64) -> bool {
false
}
}
pub struct NoOutput;
@@ -691,6 +698,12 @@ impl Engine {
let mut used = 0_i64;
let mut insertion_height = 0_i64;
for (unit_index, unit) in document.units.iter().enumerate() {
if !current.items.is_empty() && output.wants_break(&current, used, document.page_goal) {
output.shipout(&mut current, pages.len() + 1);
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
checkpoints.push(Checkpoint {
unit_index,
page_index: pages.len(),
@@ -1266,6 +1279,41 @@ mod tests {
assert_eq!(result.layout.pages[1].insertions.len(), 0);
}
#[test]
fn output_routine_can_force_an_early_page_break() {
struct BreakAfterTwo {
shipped: usize,
}
impl OutputRoutine for BreakAfterTwo {
fn shipout(&mut self, _page: &mut Page, _page_number: usize) {
self.shipped += 1;
}
fn wants_break(&mut self, page: &Page, _used: i64, _page_goal: i64) -> bool {
self.shipped == 0 && page.items.len() >= 2
}
}
let units = (0..4)
.map(|index| Unit {
id: index,
source: String::new(),
paragraph_style: None,
items: vec![VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
})],
})
.collect();
let document = Document::new(30, units);
let mut output = BreakAfterTwo { shipped: 0 };
let engine = Engine::layout_with_output(&document, &mut output);
assert_eq!(engine.pages().len(), 2);
assert_eq!(engine.pages()[0].items.len(), 2);
assert_eq!(engine.pages()[1].items.len(), 2);
assert!(output.shipped >= 1);
}
#[test]
fn top_skip_sets_the_first_baseline_distance() {
let unit = |id| Unit {