Honour topfraction for top floats

This commit is contained in:
gloamxun committed 2026-10-20 23:37:47 +08:00
1 parent 14a3775206
commit d81021d1a3
3 files changed
+96 -3

No files matched your search

+91
View File
@@ -537,6 +537,10 @@ pub struct Document {
/// `\bottomfraction`, in percent).
#[serde(default = "default_bottom_fraction")]
pub bottom_fraction_percent: u8,
/// Maximum percentage of a text page that top floats may occupy (TeX's
/// `\topfraction`, in percent).
#[serde(default = "default_top_fraction")]
pub top_fraction_percent: u8,
/// Per-class insertion limits. Missing classes use the full page.
#[serde(default)]
pub insertion_classes: Vec<InsertionClass>,
@@ -550,6 +554,10 @@ fn default_bottom_fraction() -> u8 {
30
}
fn default_top_fraction() -> u8 {
70
}
impl Document {
pub fn new(page_goal: i64, units: Vec<Unit>) -> Self {
Self {
@@ -559,6 +567,7 @@ impl Document {
text_fraction_percent: default_text_fraction(),
float_page_fraction_percent: 50,
bottom_fraction_percent: default_bottom_fraction(),
top_fraction_percent: default_top_fraction(),
insertion_classes: Vec::new(),
}
}
@@ -649,6 +658,13 @@ fn bottom_float_height(page: &Page) -> i64 {
.sum()
}
/// TeX `\topfraction`: a top float taller than this share of the page keeps
/// the rest of the page free of body text.
fn top_float_exceeds_page_fraction(page: &Page, page_goal: i64, percent: u8) -> bool {
let height = top_float_height(page);
height > 0 && page.items.is_empty() && height * 100 > i64::from(percent) * page_goal
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Checkpoint {
/// Index of the next unit to execute.
@@ -762,6 +778,15 @@ impl Engine {
}
match item {
VerticalItem::Rule { height, .. } => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
if current.items.is_empty() {
used = document.top_skip.max(*height);
} else if used + *height
@@ -776,6 +801,15 @@ impl Engine {
current.items.push(item.clone());
}
VerticalItem::Box(vertical_box) => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(vertical_box.height);
} else if used + vertical_box.height
@@ -820,6 +854,15 @@ impl Engine {
}
}
VerticalItem::Paragraph(paragraph) => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(paragraph.height);
} else if used + paragraph.height
@@ -834,6 +877,15 @@ impl Engine {
current.items.push(item.clone());
}
VerticalItem::Math { height, depth, .. } => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
let amount = height + depth;
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(amount);
@@ -1683,6 +1735,45 @@ mod tests {
assert_eq!(engine.layout.pages[1].floats.len(), 1);
}
#[test]
fn top_fraction_moves_body_off_a_tall_top_float() {
let unit = Unit {
id: 0,
source: "top fraction".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Float(Float {
placement: FloatPlacement::Top,
width: 40,
height: 80,
content: Vec::new(),
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 5,
}),
],
};
let mut document = Document::new(100, vec![unit]);
document.top_skip = 0;
document.float_page_fraction_percent = 10;
let engine = Engine::layout(&document);
assert_eq!(
engine.layout.pages.len(),
2,
"80% top float exceeds 70% limit"
);
assert_eq!(engine.layout.pages[0].floats.len(), 1);
assert!(engine.layout.pages[0].items.is_empty());
assert_eq!(engine.layout.pages[1].items.len(), 1);
document.top_fraction_percent = 100;
let engine = Engine::layout(&document);
assert_eq!(engine.layout.pages.len(), 1);
assert_eq!(engine.layout.pages[0].floats.len(), 1);
assert_eq!(engine.layout.pages[0].items.len(), 1);
}
#[test]
fn edit_ignores_structural_units() {
let rows = vec![HalignRow {