Gate bottom floats on the text fraction

This commit is contained in:
gloamxun committed 2026-10-20 18:44:31 +08:00
1 parent ea037d66bb
commit e3d5ff3abf
1 file changed
+33
+33
View File
@@ -517,6 +517,14 @@ pub struct Document {
/// TeX-like `\topskip`: distance from the page top to the first baseline.
#[serde(default)]
pub top_skip: i64,
/// Minimum percentage of the page that text must occupy before a bottom
/// float is allowed onto the page (TeX's `\textfraction`, in percent).
#[serde(default = "default_text_fraction")]
pub text_fraction_percent: u8,
}
fn default_text_fraction() -> u8 {
50
}
impl Document {
@@ -525,6 +533,7 @@ impl Document {
units,
page_goal,
top_skip: 0,
text_fraction_percent: default_text_fraction(),
}
}
@@ -806,6 +815,8 @@ impl Engine {
<= document.page_goal
- insertion_height
- bottom_float_height(&current)
&& (used - document.top_skip - bottom_float_height(&current)) * 100
>= document.text_fraction_percent as i64 * document.page_goal
{
current.floats.push(float.clone());
} else {
@@ -1319,6 +1330,28 @@ mod tests {
assert_eq!(engine.layout.pages[1].items.len(), 1);
}
#[test]
fn text_fraction_keeps_a_bottom_float_off_a_mostly_empty_page() {
let mut document = Document::new(
100,
vec![
Unit::vertical(
1,
vec![VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
})],
),
Unit::float(2, FloatPlacement::Bottom, 10, 50),
],
);
document.top_skip = 0;
document.text_fraction_percent = 50;
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2);
assert!(engine.pages()[0].floats.is_empty());
assert_eq!(engine.pages()[1].floats.len(), 1);
}
#[test]
fn bottom_float_reserves_space_on_its_page() {
let unit = Unit {