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(
+124 -10
View File
@@ -55,6 +55,11 @@ pub enum VerticalNode {
Float {
height: i64,
},
/// A bottom float: TeX reserves it at the bottom of the page, below the
/// body material.
BottomFloat {
height: i64,
},
Glue(VerticalGlue),
Penalty(i64),
Mark(Mark),
@@ -82,6 +87,8 @@ pub struct PageFragment {
pub badness: i64,
/// Height of the page box after glue setting.
pub height: i64,
/// Height reserved at the page bottom by bottom floats.
pub bottom: i64,
/// Depth of the page box, capped at `max_depth`.
pub depth: i64,
/// Total stretch of the vertical glue on the page.
@@ -102,6 +109,7 @@ struct Candidate {
stretch: i64,
shrink: i64,
reserved: i64,
bottom: i64,
penalty: i64,
end: usize,
next: usize,
@@ -117,6 +125,10 @@ pub struct PageBuilder {
pub insertion_limits: Vec<InsertionLimit>,
/// TeX `\textfloatsep`: space between top floats and the body.
pub text_float_sep: i64,
/// TeX floatsep: space between stacked bottom floats.
pub float_sep: i64,
/// TeX bottomfraction: percent of the page bottom floats may occupy.
pub bottom_fraction_percent: u8,
}
impl PageBuilder {
@@ -127,6 +139,8 @@ impl PageBuilder {
max_depth: 0,
insertion_limits: Vec::new(),
text_float_sep: 0,
float_sep: 0,
bottom_fraction_percent: 100,
}
}
@@ -197,6 +211,8 @@ impl PageBuilder {
let mut depth = 0_i64;
let mut first_box = true;
let mut has_float = false;
let mut bottom = 0_i64;
let mut bottom_count = 0_usize;
let mut usage: BTreeMap<u8, i64> = held.clone();
let mut reserved: i64 = usage.values().sum();
let mut candidates: Vec<Candidate> = Vec::new();
@@ -239,6 +255,7 @@ impl PageBuilder {
badness,
badness,
0,
bottom,
self.insertion_space(nodes, start, end, held),
);
return Some((
@@ -250,10 +267,50 @@ impl PageBuilder {
height += (*float_height).max(0);
has_float = true;
}
VerticalNode::BottomFloat {
height: float_height,
} => {
let gap = if bottom_count == 0 {
self.text_float_sep.max(0)
} else {
self.float_sep.max(0)
};
let needed = (*float_height).max(0) + gap;
let allowed = self.page_goal * i64::from(self.bottom_fraction_percent) / 100;
if height > 0
&& (height + reserved + bottom + needed > self.page_goal
|| bottom + needed > allowed)
{
let end = index.max(start);
let badness = self.badness(height, reserved + bottom, stretch, shrink);
let fragment = self.fragment(
start,
end,
height,
depth,
stretch,
shrink,
badness,
badness,
0,
bottom,
self.insertion_space(nodes, start, end, held),
);
return Some((
fragment,
index,
self.insertion_overflow(nodes, start, end, held),
));
}
bottom += needed;
bottom_count += 1;
}
VerticalNode::Glue(glue) => {
if index > start {
candidates.push(
self.candidate(height, depth, stretch, shrink, reserved, 0, index),
self.candidate(
height, depth, stretch, shrink, reserved, bottom, 0, index,
),
);
}
height += glue.width;
@@ -274,6 +331,7 @@ impl PageBuilder {
badness,
badness + *penalty,
*penalty,
bottom,
self.insertion_space(nodes, start, end, held),
);
return Some((
@@ -283,11 +341,9 @@ impl PageBuilder {
));
}
if index > start {
candidates.push(
self.candidate(
height, depth, stretch, shrink, reserved, *penalty, index,
),
);
candidates.push(self.candidate(
height, depth, stretch, shrink, reserved, bottom, *penalty, index,
));
}
}
VerticalNode::Insertion {
@@ -305,7 +361,16 @@ impl PageBuilder {
}
}
candidates.push(self.candidate(height, depth, stretch, shrink, reserved, 0, nodes.len()));
candidates.push(self.candidate(
height,
depth,
stretch,
shrink,
reserved,
bottom,
0,
nodes.len(),
));
let best = self.select(candidates)?;
let fragment = self.fragment(
start,
@@ -317,6 +382,7 @@ impl PageBuilder {
best.badness,
best.cost,
best.penalty,
best.bottom,
self.insertion_space(nodes, start, best.end, held),
);
Some((
@@ -377,6 +443,7 @@ impl PageBuilder {
stretch: i64,
shrink: i64,
reserved: i64,
bottom: i64,
penalty: i64,
end: usize,
) -> Candidate {
@@ -386,7 +453,7 @@ impl PageBuilder {
} else {
(height, depth)
};
let badness = self.badness(height, reserved, stretch, shrink);
let badness = self.badness(height, reserved + bottom, stretch, shrink);
Candidate {
cost: badness + penalty,
penalty,
@@ -396,6 +463,7 @@ impl PageBuilder {
stretch,
shrink,
reserved,
bottom,
end,
next: end + 1,
}
@@ -435,6 +503,7 @@ impl PageBuilder {
badness: i64,
cost: i64,
penalty: i64,
bottom: i64,
insertions: Vec<InsertionSpace>,
) -> PageFragment {
PageFragment {
@@ -444,6 +513,7 @@ impl PageBuilder {
penalty,
badness,
height,
bottom,
depth,
stretch,
shrink,
@@ -455,7 +525,7 @@ impl PageBuilder {
let feasible: Vec<Candidate> = candidates
.iter()
.filter(|candidate| {
let page = candidate.height + candidate.reserved;
let page = candidate.height + candidate.reserved + candidate.bottom;
page <= self.page_goal || page - self.page_goal <= candidate.shrink
})
.cloned()
@@ -468,7 +538,7 @@ impl PageBuilder {
pool.into_iter().min_by_key(|candidate| {
(
candidate.cost,
(candidate.height + candidate.reserved - self.page_goal).abs(),
(candidate.height + candidate.reserved + candidate.bottom - self.page_goal).abs(),
)
})
}
@@ -701,4 +771,48 @@ mod tests {
"the overflow material is held over to the next page"
);
}
#[test]
fn bottom_floats_reserve_space_at_the_page_bottom() {
let builder = PageBuilder::new(100);
let nodes = vec![
box_(80, 0),
glue(0, 0, 0),
VerticalNode::BottomFloat { height: 30 },
];
let pages = builder.build(&nodes);
assert_eq!(pages.len(), 2);
assert_eq!(pages[0].height, 80);
assert_eq!(pages[0].bottom, 0);
assert_eq!(pages[1].bottom, 30);
}
#[test]
fn stacked_bottom_floats_add_float_separators() {
let builder = PageBuilder {
float_sep: 5,
..PageBuilder::new(100)
};
let nodes = vec![
box_(50, 0),
VerticalNode::BottomFloat { height: 20 },
VerticalNode::BottomFloat { height: 20 },
];
let pages = builder.build(&nodes);
assert_eq!(pages.len(), 1);
assert_eq!(pages[0].bottom, 45);
}
#[test]
fn bottom_fraction_caps_bottom_float_space() {
let builder = PageBuilder {
bottom_fraction_percent: 20,
..PageBuilder::new(100)
};
let nodes = vec![box_(50, 0), VerticalNode::BottomFloat { height: 30 }];
let pages = builder.build(&nodes);
assert_eq!(pages.len(), 2);
assert_eq!(pages[0].bottom, 0);
assert_eq!(pages[1].bottom, 30);
}
}