Honour bottomfraction when placing bottom floats

This commit is contained in:
gloamxun committed 2026-10-20 23:33:21 +08:00
1 parent c2dbf405ab
commit 14a3775206
3 files changed
+49 -4

No files matched your search

+44 -1
View File
@@ -533,6 +533,10 @@ pub struct Document {
pub text_fraction_percent: u8,
/// `\floatpagefraction` equivalent for dedicated float pages.
pub float_page_fraction_percent: u8,
/// Maximum percentage of a page that bottom floats may occupy (TeX's
/// `\bottomfraction`, in percent).
#[serde(default = "default_bottom_fraction")]
pub bottom_fraction_percent: u8,
/// Per-class insertion limits. Missing classes use the full page.
#[serde(default)]
pub insertion_classes: Vec<InsertionClass>,
@@ -542,6 +546,10 @@ fn default_text_fraction() -> u8 {
50
}
fn default_bottom_fraction() -> u8 {
30
}
impl Document {
pub fn new(page_goal: i64, units: Vec<Unit>) -> Self {
Self {
@@ -550,6 +558,7 @@ impl Document {
top_skip: 0,
text_fraction_percent: default_text_fraction(),
float_page_fraction_percent: 50,
bottom_fraction_percent: default_bottom_fraction(),
insertion_classes: Vec::new(),
}
}
@@ -934,6 +943,8 @@ impl Engine {
- bottom_float_height(&current)
&& (used - document.top_skip - bottom_float_height(&current)) * 100
>= document.text_fraction_percent as i64 * document.page_goal
&& (float.height + bottom_float_height(&current)) * 100
<= document.bottom_fraction_percent as i64 * document.page_goal
{
current.floats.push(float.clone());
} else {
@@ -1633,13 +1644,45 @@ mod tests {
}),
],
};
let engine = Engine::layout(&Document::new(60, vec![unit]));
let mut document = Document::new(60, vec![unit]);
document.bottom_fraction_percent = 100;
let engine = Engine::layout(&document);
assert_eq!(engine.layout.pages.len(), 2);
assert_eq!(engine.layout.pages[0].floats.len(), 1);
assert_eq!(engine.layout.pages[0].items.len(), 1);
assert_eq!(engine.layout.pages[1].items.len(), 1);
}
#[test]
fn bottom_fraction_moves_a_tall_float_to_the_next_page() {
let unit = Unit {
id: 0,
source: "tall bottom float".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
height: 80,
}),
VerticalItem::Float(Float {
placement: FloatPlacement::Bottom,
width: 40,
height: 50,
content: Vec::new(),
}),
],
};
let document = Document::new(100, vec![unit]);
let engine = Engine::layout(&document);
assert_eq!(
engine.layout.pages.len(),
2,
"50% bottom float exceeds 30% limit"
);
assert!(engine.layout.pages[0].floats.is_empty());
assert_eq!(engine.layout.pages[1].floats.len(), 1);
}
#[test]
fn edit_ignores_structural_units() {
let rows = vec![HalignRow {