Defer bottom floats like TeX page building

This commit is contained in:
gloamxun committed 2026-10-23 23:48:28 +08:00
1 parent 17638996a1
commit 4dd28b3aa7
6 files changed
+66 -34

No files matched your search

+23 -10
View File
@@ -1992,11 +1992,22 @@ impl Engine {
let mut page_shrinks = Vec::new();
let mut page_goals: Vec<i64> = Vec::new();
let mut held: BTreeMap<u8, Vec<VerticalItem>> = BTreeMap::new();
let fragments = builder.build(&nodes);
let fragments = builder.build_with_deferred_bottom_floats(&nodes);
for fragment in &fragments {
let mut items = Vec::new();
let mut marks = Vec::new();
let mut floats = Vec::new();
let mut floats: Vec<Float> = fragment
.bottom_floats
.iter()
.map(|height| Float {
placement: FloatPlacement::Bottom,
width: 0,
height: *height,
gap_before: 0,
gap_after: 0,
content: Vec::new(),
})
.collect();
let mut sources = Vec::new();
let mut insertions: BTreeMap<u8, Insertion> = BTreeMap::new();
let mut used: BTreeMap<u8, i64> = BTreeMap::new();
@@ -2039,13 +2050,13 @@ impl Engine {
held.entry(class).or_default().extend(remainder);
}
}
item @ VerticalItem::Float(float) => {
if float.placement == FloatPlacement::Here {
items.push(item.clone());
} else {
floats.push(float.clone());
}
}
item @ VerticalItem::Float(float) => match float.placement {
FloatPlacement::Here => items.push(item.clone()),
FloatPlacement::Top => floats.push(float.clone()),
// Bottom floats, including deferred ones carried from
// the previous page, arrive through the fragment.
FloatPlacement::Bottom => {}
},
other => items.push(other.clone()),
}
}
@@ -6499,7 +6510,9 @@ mod tests {
let layout = Engine::layout_paged(&document).expect("bottom floats are page-buildable");
assert_eq!(layout.pages().len(), 2);
assert_eq!(layout.pages()[0].floats.len(), 0);
assert_eq!(layout.pages()[0].items.len(), 1);
// TeX keeps filling the page instead of breaking at the float.
assert_eq!(layout.pages()[0].items.len(), 2);
assert_eq!(layout.pages()[1].items.len(), 0);
assert_eq!(layout.pages()[1].floats.len(), 1);
assert_eq!(
layout.pages()[1].floats[0].placement,
+11
View File
@@ -122,6 +122,9 @@ pub struct PageFragment {
/// Accepted insertion space per class, including material held over from
/// the previous page.
pub insertions: Vec<InsertionSpace>,
/// Bottom floats placed at this page's bottom, as heights in placement
/// order, including floats carried over from the page before.
pub bottom_floats: Vec<i64>,
}
#[derive(Clone, Debug)]
@@ -287,6 +290,7 @@ impl PageBuilder {
stretches: [0; 4],
set: 0,
insertions: Vec::new(),
bottom_floats: floats.to_vec(),
}
}
@@ -368,6 +372,7 @@ impl PageBuilder {
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
bottom_floats.clone(),
);
return Some((
fragment,
@@ -422,6 +427,7 @@ impl PageBuilder {
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
bottom_floats.clone(),
);
return Some((
fragment,
@@ -498,6 +504,7 @@ impl PageBuilder {
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
bottom_floats.clone(),
);
return Some((
fragment,
@@ -558,6 +565,7 @@ impl PageBuilder {
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
bottom_floats.clone(),
);
return Some((
fragment,
@@ -623,6 +631,7 @@ impl PageBuilder {
best.bottom,
order_stretches(&nodes[start..best.end]),
self.insertion_space(nodes, start, best.end, held),
bottom_floats.clone(),
);
Some((
fragment,
@@ -762,6 +771,7 @@ impl PageBuilder {
bottom: i64,
stretches: [i64; 4],
insertions: Vec<InsertionSpace>,
bottom_floats: Vec<i64>,
) -> PageFragment {
let reserved: i64 = insertions.iter().map(|space| space.height).sum();
let target = (self.page_goal - bottom - reserved).max(0);
@@ -792,6 +802,7 @@ impl PageBuilder {
stretches,
set,
insertions,
bottom_floats,
}
}