Honour TeX float number limits

Add top_number, bottom_number and total_number to Document with TeX defaults 2, 1 and 12. Top and bottom float branches, including the incremental edit path, refuse an extra float once its placement count or the total page count is reached, so the float moves to the next page.
This commit is contained in:
gloamxun committed 2026-10-21 08:57:16 +08:00
1 parent f2deac541e
commit 7385b8862a
3 files changed
+145 -3

No files matched your search

+141
View File
@@ -545,6 +545,15 @@ pub struct Document {
/// `\topfraction`, in percent).
#[serde(default = "default_top_fraction")]
pub top_fraction_percent: u8,
/// TeX's `\topnumber`: maximum top floats on one page.
#[serde(default = "default_top_number")]
pub top_number: u8,
/// TeX's `\bottomnumber`: maximum bottom floats on one page.
#[serde(default = "default_bottom_number")]
pub bottom_number: u8,
/// TeX's `\totalnumber`: maximum floats of any placement on one page.
#[serde(default = "default_total_number")]
pub total_number: u8,
/// Per-class insertion limits. Missing classes use the full page.
#[serde(default)]
pub insertion_classes: Vec<InsertionClass>,
@@ -566,6 +575,18 @@ fn default_top_fraction() -> u8 {
70
}
fn default_top_number() -> u8 {
2
}
fn default_bottom_number() -> u8 {
1
}
fn default_total_number() -> u8 {
12
}
impl Document {
pub fn new(page_goal: i64, units: Vec<Unit>) -> Self {
Self {
@@ -576,6 +597,9 @@ impl Document {
float_page_fraction_percent: 50,
bottom_fraction_percent: default_bottom_fraction(),
top_fraction_percent: default_top_fraction(),
top_number: default_top_number(),
bottom_number: default_bottom_number(),
total_number: default_total_number(),
insertion_classes: Vec::new(),
insertion_skip: 0,
}
@@ -1020,7 +1044,14 @@ impl Engine {
.filter(|entry| entry.placement == FloatPlacement::Top)
.map(|entry| entry.height)
.sum();
let top_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.count();
let fits_on_current = !current.items.is_empty()
&& top_count < usize::from(document.top_number)
&& current.floats.len() < usize::from(document.total_number)
&& used + float.height <= document.page_goal - insertion_height
&& (existing_top + float.height) * 100
<= i64::from(document.top_fraction_percent)
@@ -1041,6 +1072,13 @@ impl Engine {
used += float.height;
current.floats.push(float.clone());
} else if !current.items.is_empty()
&& current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Bottom)
.count()
< usize::from(document.bottom_number)
&& current.floats.len() < usize::from(document.total_number)
&& used + float.height
<= document.page_goal
- insertion_height
@@ -1377,7 +1415,14 @@ impl Engine {
.filter(|entry| entry.placement == FloatPlacement::Top)
.map(|entry| entry.height)
.sum();
let top_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.count();
let fits_on_current = !current.items.is_empty()
&& top_count < usize::from(next.top_number)
&& current.floats.len() < usize::from(next.total_number)
&& used + float.height <= next.page_goal - insertion_height
&& (existing_top + float.height) * 100
<= i64::from(next.top_fraction_percent) * next.page_goal;
@@ -1394,6 +1439,13 @@ impl Engine {
used += float.height;
current.floats.push(float.clone());
} else if !current.items.is_empty()
&& current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Bottom)
.count()
< usize::from(next.bottom_number)
&& current.floats.len() < usize::from(next.total_number)
&& used + float.height
<= next.page_goal - insertion_height - bottom_float_height(&current)
&& (used - next.top_skip - bottom_float_height(&current)) * 100
@@ -1858,6 +1910,95 @@ mod tests {
assert_eq!(layout.pages()[1].insertions.len(), 1);
}
#[test]
fn top_number_limits_top_floats_per_page() {
let mut document = Document::new(
1000,
vec![
Unit::paragraph(0, "alpha"),
Unit::float(1, FloatPlacement::Top, 10, 100),
Unit::paragraph(2, "beta"),
Unit::float(3, FloatPlacement::Top, 10, 100),
Unit::paragraph(4, "gamma"),
Unit::float(5, FloatPlacement::Top, 10, 100),
],
);
document.top_number = 1;
document.total_number = 10;
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 3);
for page in engine.pages() {
assert!(
page.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Top)
.count()
<= 1
);
}
}
#[test]
fn total_number_limits_mixed_float_placements() {
let mut document = Document::new(
1000,
vec![
Unit::float(0, FloatPlacement::Top, 10, 100),
Unit::paragraph(1, "alpha"),
Unit::float(2, FloatPlacement::Bottom, 10, 100),
],
);
document.top_number = 5;
document.bottom_number = 5;
document.total_number = 1;
document.text_fraction_percent = 0;
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2);
assert_eq!(engine.pages()[0].floats.len(), 1);
assert_eq!(engine.pages()[0].floats[0].placement, FloatPlacement::Top);
assert_eq!(engine.pages()[1].floats.len(), 1);
assert_eq!(
engine.pages()[1].floats[0].placement,
FloatPlacement::Bottom
);
}
#[test]
fn edited_layout_respects_float_number_limits() {
let mut document = Document::new(
1000,
vec![
Unit::paragraph(0, "alpha"),
Unit::float(1, FloatPlacement::Top, 10, 100),
Unit::paragraph(2, "beta"),
Unit::float(3, FloatPlacement::Top, 10, 100),
],
);
document.top_number = 1;
document.total_number = 10;
let mut engine = Engine::layout(&document);
let edited = Document::new(
1000,
vec![
Unit::paragraph(0, "alpha changed"),
Unit::float(1, FloatPlacement::Top, 10, 100),
Unit::paragraph(2, "beta"),
Unit::float(3, FloatPlacement::Top, 10, 100),
],
);
engine.edit(&edited, 1, "alpha changed");
assert_eq!(engine.pages().len(), 2);
for page in engine.pages() {
assert!(
page.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Top)
.count()
<= 1
);
}
}
#[test]
fn relayout_from_keeps_the_unchanged_page_prefix() {
let first = Unit::paragraph(0, "alpha");