Place top floats through the paged builder

This commit is contained in:
gloamxun committed 2026-10-23 03:30:15 +08:00
1 parent bc0d29ace4
commit aa081e3dc6
5 files changed
+136 -13

No files matched your search

+43
View File
@@ -50,6 +50,11 @@ pub enum VerticalNode {
height: i64,
depth: i64,
},
/// A top float: TeX puts it at the top of a page and never below body
/// material, so a late float starts the next page.
Float {
height: i64,
},
Glue(VerticalGlue),
Penalty(i64),
Mark(Mark),
@@ -110,6 +115,8 @@ pub struct PageBuilder {
pub top_skip: i64,
pub max_depth: i64,
pub insertion_limits: Vec<InsertionLimit>,
/// TeX `\textfloatsep`: space between top floats and the body.
pub text_float_sep: i64,
}
impl PageBuilder {
@@ -119,6 +126,7 @@ impl PageBuilder {
top_skip: 0,
max_depth: 0,
insertion_limits: Vec::new(),
text_float_sep: 0,
}
}
@@ -168,6 +176,7 @@ impl PageBuilder {
let mut shrink = 0_i64;
let mut depth = 0_i64;
let mut first_box = true;
let mut has_float = false;
let mut usage: BTreeMap<u8, i64> = held.clone();
let mut reserved: i64 = usage.values().sum();
let mut candidates: Vec<Candidate> = Vec::new();
@@ -180,6 +189,11 @@ impl PageBuilder {
} => {
let contribution = if first_box {
(*box_height).max(self.top_skip)
+ if has_float {
self.text_float_sep.max(0)
} else {
0
}
} else {
*box_height
};
@@ -187,6 +201,35 @@ impl PageBuilder {
depth = *box_depth;
first_box = false;
}
VerticalNode::Float {
height: float_height,
} => {
if height > 0 {
// TeX never places a top float below body material:
// the float starts the next page instead.
let end = index.max(start);
let badness = self.badness(height, reserved, stretch, shrink);
let fragment = self.fragment(
start,
end,
height,
depth,
stretch,
shrink,
badness,
badness,
0,
self.insertion_space(nodes, start, end, held),
);
return Some((
fragment,
index,
self.insertion_overflow(nodes, start, end, held),
));
}
height += (*float_height).max(0);
has_float = true;
}
VerticalNode::Glue(glue) => {
if index > start {
candidates.push(