Place bottom floats through the paged builder

This commit is contained in:
gloamxun committed 2026-10-23 03:57:44 +08:00
1 parent 7b9d543e49
commit 14ca3b91d9
5 files changed
+198 -34

No files matched your search

+53 -7
View File
@@ -845,13 +845,16 @@ fn document_nodes(
class: insertion.class,
height: document.insertion_reserve(insertion.class, insertion.height),
},
VerticalItem::Float(float) if matches!(float.placement, FloatPlacement::Top) => {
VerticalNode::Float {
VerticalItem::Float(float) => match float.placement {
FloatPlacement::Top => VerticalNode::Float {
height: float.height,
}
}
// Bottom and in-text floats still need the scrolling builder.
VerticalItem::Float(_) => return None,
},
FloatPlacement::Bottom => VerticalNode::BottomFloat {
height: float.height,
},
// In-text floats are body material, not page-builder floats.
FloatPlacement::Here => return None,
},
};
nodes.push(node);
owners.push(index);
@@ -1854,6 +1857,8 @@ impl Engine {
})
.collect(),
text_float_sep: document.text_float_sep.max(0),
float_sep: document.float_sep.max(0),
bottom_fraction_percent: document.bottom_fraction_percent,
}
}
@@ -5272,7 +5277,7 @@ mod tests {
#[test]
fn paged_layout_refuses_floats_but_places_insertions() {
let floated = Document::new(100, vec![Unit::float(1, FloatPlacement::Bottom, 100, 20)]);
let floated = Document::new(100, vec![Unit::float(1, FloatPlacement::Here, 100, 20)]);
assert!(Engine::layout_paged(&floated).is_none());
let insertion = Document::new(
@@ -5297,6 +5302,47 @@ mod tests {
assert_eq!(layout.pages()[0].insertions[0].items.len(), 1);
}
#[test]
fn paged_layout_places_bottom_floats_at_the_page_end() {
let mut document = Document::new(
100,
vec![
Unit::rule(1, 100, 60),
Unit::float(2, FloatPlacement::Bottom, 100, 30),
],
);
document.text_float_sep = 0;
let layout = Engine::layout_paged(&document).expect("bottom floats are page-buildable");
assert_eq!(layout.pages().len(), 1);
assert_eq!(layout.pages()[0].floats.len(), 1);
assert_eq!(
layout.pages()[0].floats[0].placement,
FloatPlacement::Bottom
);
}
#[test]
fn paged_layout_defers_a_bottom_float_that_cannot_fit() {
let mut document = Document::new(
100,
vec![
Unit::rule(1, 100, 80),
Unit::float(2, FloatPlacement::Bottom, 100, 30),
Unit::rule(3, 100, 10),
],
);
document.text_float_sep = 0;
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);
assert_eq!(layout.pages()[1].floats.len(), 1);
assert_eq!(
layout.pages()[1].floats[0].placement,
FloatPlacement::Bottom
);
}
#[test]
fn paged_layout_places_top_floats() {
let document = Document::new(