Retarget later pages after a vsize assignment

This commit is contained in:
gloamxun committed 2026-10-24 00:28:29 +08:00
1 parent 0805f3c932
commit de84ffc979
3 files changed
+83 -12

No files matched your search

+74 -6
View File
@@ -1516,6 +1516,7 @@ impl OutputRoutine for NoOutput {
/// insertion class, as it does inside TeX's output routine.
struct PagedOutput {
program: output::OutputProgram,
goal: i64,
pageno: i64,
deadcycles: i64,
counts: [i64; 256],
@@ -1539,6 +1540,7 @@ impl PagedOutput {
}
Self {
program,
goal: document.page_goal,
pageno: 1,
deadcycles: 0,
counts,
@@ -1574,7 +1576,7 @@ impl PagedOutput {
page_shrink: shrink,
vbadness: badness,
hsize: document.text_width,
vsize: document.page_goal,
vsize: self.goal,
top_skip: document.top_skip,
max_depth: document.max_depth,
output_penalty: penalty,
@@ -1599,6 +1601,7 @@ impl PagedOutput {
self.counts = context.insertion_counts;
self.dimens = context.insertion_dimens;
self.skips = context.insertion_skips;
self.goal = context.vsize.max(1);
self.pageno = context.pageno.wrapping_add(1);
self.deadcycles = context.deadcycles;
if shipped_now > 0 {
@@ -2149,7 +2152,7 @@ impl Engine {
);
}
}
let builder = Self::paged_builder(document);
let mut builder = Self::paged_builder(document);
let mut routine = match &document.output_routine {
Some(source) => Some(PagedOutput::new(
document,
@@ -2159,8 +2162,11 @@ impl Engine {
};
let mut pages = Vec::new();
let mut held: BTreeMap<u8, Vec<VerticalItem>> = BTreeMap::new();
let fragments = builder.build_with_deferred_bottom_floats(&nodes);
for fragment in &fragments {
let mut fragments = builder.build_with_deferred_bottom_floats(&nodes);
let mut page_index = 0;
while page_index < fragments.len() {
let fragment = fragments[page_index].clone();
let fragment = &fragment;
let mut items = Vec::new();
let mut marks = Vec::new();
let mut floats: Vec<Float> = fragment
@@ -2251,7 +2257,7 @@ impl Engine {
Some(routine) => routine.ship(
document,
&page,
(document.page_goal - reserved - insertion_skip).max(0),
(builder.page_goal - reserved - insertion_skip).max(0),
fragment.penalty,
fragment.depth,
fragment.stretch,
@@ -2265,6 +2271,21 @@ impl Engine {
if shipped {
pages.push(page);
}
if let Some(active) = routine.as_ref() {
let goal = active.goal;
let has_bottom_float = nodes
.iter()
.any(|node| matches!(node, VerticalNode::BottomFloat { .. }));
if goal != builder.page_goal && !has_bottom_float {
let next_start = fragments
.get(page_index + 1)
.map_or(nodes.len(), |next| next.start);
builder.set_page_goal(goal);
fragments.truncate(page_index + 1);
fragments.extend(builder.build_from(&nodes, next_start));
}
}
page_index += 1;
}
// Insertion material that did not fit its class limit on the page where
@@ -2302,7 +2323,7 @@ impl Engine {
Some(routine) => routine.ship(
document,
&page,
(document.page_goal - reserved - insertion_skip).max(0),
(builder.page_goal - reserved - insertion_skip).max(0),
10_000,
0,
0,
@@ -3274,6 +3295,53 @@ mod tests {
assert_eq!(result.layout.pages[1].insertions.len(), 0);
}
#[test]
fn output_routine_can_retarget_pages_with_insertions() {
const PT: i64 = 65_536;
let build = |routine: Option<&str>| {
let insertion = Unit::insertion(
2,
0,
50 * PT,
vec![VerticalItem::Rule {
width: 100 * PT,
height: 50 * PT,
}],
);
let mut document = Document::new(
250 * PT,
vec![
Unit::rule(1, 100 * PT, 100 * PT),
insertion,
Unit::rule(3, 100 * PT, 100 * PT),
Unit::vskip(6, 0),
Unit::rule(4, 100 * PT, 100 * PT),
Unit::vskip(7, 0),
Unit::rule(5, 100 * PT, 100 * PT),
],
);
document.insertion_classes = vec![InsertionClass {
number: 0,
fraction_percent: 100,
max_height: Some(250 * PT),
multiplier: 1000,
skip: None,
}];
document.output_routine = routine.map(str::to_owned);
Engine::layout_paged(&document).expect("paged layout")
};
let base = build(None);
assert_eq!(base.layout.pages.len(), 2);
assert_eq!(base.layout.pages[0].insertions.len(), 1);
// \vsize assigned by the routine retargets the pages it has not seen
// yet, even though the document raises insertion material.
let retargeted = build(Some(r#"\ifnum\pageno=1\vsize=150\fi\shipout\box255"#));
assert_eq!(retargeted.layout.pages.len(), 3);
assert_eq!(retargeted.layout.pages[0].insertions.len(), 1);
}
#[test]
fn output_routine_can_change_vsize_for_later_pages() {
let mut units = Vec::new();