Ship float-heavy pages without body material

This commit is contained in:
gloamxun committed 2026-10-23 12:45:53 +08:00
1 parent 594a57e414
commit 66dfb10b60
4 files changed
+82 -4

No files matched your search

+23
View File
@@ -1965,6 +1965,7 @@ impl Engine {
top_number: usize::from(document.top_number),
bottom_number: usize::from(document.bottom_number),
total_number: usize::from(document.total_number),
float_page_fraction_percent: document.float_page_fraction_percent,
ragged_bottom: document.ragged_bottom,
}
}
@@ -5798,6 +5799,28 @@ mod tests {
);
}
#[test]
fn paged_layout_honours_float_page_fraction() {
let mut document = Document::new(
100,
vec![
Unit::float(1, FloatPlacement::Top, 100, 60),
Unit::rule(2, 100, 10),
],
);
document.text_fraction_percent = 0;
document.float_page_fraction_percent = 50;
let layout = Engine::layout_paged(&document).expect("float page builds");
assert_eq!(layout.pages().len(), 2);
assert!(layout.pages()[0].items.is_empty());
assert_eq!(layout.pages()[0].floats.len(), 1);
document.float_page_fraction_percent = 90;
let layout = Engine::layout_paged(&document).expect("shared page builds");
assert_eq!(layout.pages().len(), 1);
assert_eq!(layout.pages()[0].items.len(), 1);
}
#[test]
fn paged_float_reserves_text_float_sep_in_the_display_list() {
let mut document = Document::new(
+55
View File
@@ -159,6 +159,9 @@ pub struct PageBuilder {
pub bottom_number: usize,
/// TeX `\totalnumber`: maximum floats, of any placement, on one page.
pub total_number: usize,
/// TeX `\floatpagefraction`: percent of the page floats must occupy
/// before the body is pushed to the next page.
pub float_page_fraction_percent: u8,
/// TeX `\raggedbottom` (default) versus `\flushbottom`.
pub ragged_bottom: bool,
}
@@ -176,6 +179,7 @@ impl PageBuilder {
top_number: 2,
bottom_number: 1,
total_number: 12,
float_page_fraction_percent: 50,
ragged_bottom: true,
}
}
@@ -263,6 +267,37 @@ impl PageBuilder {
height: box_height,
depth: box_depth,
} => {
// TeX ships a page that is mostly floats on its own; the
// body starts on the next page instead of sharing it.
if has_float
&& !has_body
&& height * 100
>= i64::from(self.float_page_fraction_percent) * self.page_goal
{
let end = index.max(start);
let badness =
self.badness(height, reserved + bottom, stretch, shrink, stretch_order);
let fragment = self.fragment(
start,
end,
height,
depth,
stretch,
shrink,
stretch_order,
badness,
badness,
0,
bottom,
order_stretches(&nodes[start..end]),
self.insertion_space(nodes, start, end, held),
);
return Some((
fragment,
index,
self.insertion_overflow(nodes, start, end, held),
));
}
let contribution = if first_box {
(*box_height).max(self.top_skip)
+ if has_float {
@@ -1035,6 +1070,26 @@ mod tests {
assert_eq!(pages[1].bottom, 10);
}
#[test]
fn float_page_fraction_pushes_the_body_to_the_next_page() {
let nodes = vec![
VerticalNode::Float { height: 60 },
VerticalNode::Box {
height: 10,
depth: 0,
},
];
let mut builder = PageBuilder::new(100);
builder.float_page_fraction_percent = 50;
let pages = builder.build(&nodes);
assert_eq!(pages.len(), 2);
assert_eq!((pages[0].start, pages[0].end), (0, 1));
assert_eq!(pages[1].start, 1);
builder.float_page_fraction_percent = 90;
assert_eq!(builder.build(&nodes).len(), 1);
}
#[test]
fn bottom_fraction_caps_bottom_float_space() {
let builder = PageBuilder {