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 {
+2 -2
View File
@@ -262,7 +262,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
The crate currently has 131 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 214 tests (131 route-D unit, 1 route-D integration, 71 v1 integration, 6 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
The crate currently has 132 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 215 tests (132 route-D unit, 1 route-D integration, 71 v1 integration, 6 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
### M0 incremental demo
@@ -291,7 +291,7 @@ deliberately open rather than approximated:
pdfTeX line breaks and full hyphenation demerits are not reproduced generally.
- **M5**: `cmr10.tfm` is parsed at runtime with widths, glyph heights/depths and kern pairs. all five text faces embed Liberation Serif/Mono as Type0/Identity-H with `/W` widths; ff/fi/fl/ffi/ffl ligatures come from TFM, but full NFSS family switching and italic correction are not implemented.
- **M6**: `\output`, `\insert`, mark/float output policies, class-file execution and
math limit policies are only partially modeled.
math limit policies are only partially modeled; `OutputRoutine::wants_break` can force a page break between units, but `\output` is still Rust code rather than interpreted tokens.
- **M7**: `POST /api/route-d/preview` and `/api/route-d/edit/{unit}` expose
route-D pages and edit diff; the preview path lays out
blank-line-separated plain text and returns route-D pages with SVG and page