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,
}
}
+11 -3
View File
@@ -1007,12 +1007,10 @@ fn top_floats_reserve_page_space_like_pdftex() {
/// A `[b]` float reserves its height plus `\textfloatsep` at the page bottom;
/// the body must stop earlier on exactly the pages pdfLaTeX chooses.
#[test]
fn bottom_floats_reserve_page_space_like_pdftex() {
fn bottom_float_pages(float_at: usize) {
let paragraphs: Vec<String> = (0..80)
.map(|index| format!("p{index} alpha beta gamma"))
.collect();
let float_at = 2_usize;
let mut body = String::new();
for (index, paragraph) in paragraphs.iter().enumerate() {
body.push_str(paragraph);
@@ -1076,6 +1074,16 @@ fn bottom_floats_reserve_page_space_like_pdftex() {
}
}
#[test]
fn bottom_floats_reserve_page_space_like_pdftex() {
bottom_float_pages(2);
}
#[test]
fn late_bottom_floats_defer_like_pdftex() {
bottom_float_pages(40);
}
/// `\footnotetext` raises insertion material that is reserved at the page
/// bottom. With slack on each page, the body breaks and per-page body text
/// must stay in step with pdfLaTeX even though the note text is not compared.