Reserve skip before the first insertion class

This commit is contained in:
gloamxun committed 2026-10-21 08:28:34 +08:00
1 parent e052a0d0f3
commit 180c05e425
3 files changed
+91 -10

No files matched your search

+87 -8
View File
@@ -548,6 +548,10 @@ pub struct Document {
/// Per-class insertion limits. Missing classes use the full page.
#[serde(default)]
pub insertion_classes: Vec<InsertionClass>,
/// TeX's `\skip\footins`: space reserved between the page body and the
/// first insertion class on a page. Zero by default for compatibility.
#[serde(default)]
pub insertion_skip: i64,
}
fn default_text_fraction() -> u8 {
@@ -573,6 +577,7 @@ impl Document {
bottom_fraction_percent: default_bottom_fraction(),
top_fraction_percent: default_top_fraction(),
insertion_classes: Vec::new(),
insertion_skip: 0,
}
}
@@ -921,7 +926,16 @@ impl Engine {
}
}
VerticalItem::Insertion(insertion) => {
let capacity = document.page_goal.saturating_sub(document.top_skip).max(1);
let gap = if current.insertions.is_empty() {
document.insertion_skip.max(0)
} else {
0
};
let capacity = document
.page_goal
.saturating_sub(document.top_skip)
.saturating_sub(gap)
.max(1);
if insertion_height + insertion.height > capacity
&& (!current.items.is_empty() || !current.insertions.is_empty())
{
@@ -931,7 +945,12 @@ impl Engine {
}
let class_limit = document
.insertion_class_limit(insertion.class)
.min(document.page_goal.saturating_sub(document.top_skip))
.min(
document
.page_goal
.saturating_sub(document.top_skip)
.saturating_sub(gap),
)
.max(1);
let class_height: i64 = current
.insertions
@@ -950,6 +969,7 @@ impl Engine {
}
let mut chunk = Vec::new();
let mut chunk_height = 0;
let mut chunk_gap = gap;
for sub_item in &insertion.items {
let sub_height = item_height(sub_item);
if chunk_height + sub_height > class_limit && !chunk.is_empty() {
@@ -958,10 +978,11 @@ impl Engine {
height: chunk_height,
items: std::mem::take(&mut chunk),
});
insertion_height = chunk_height;
insertion_height = chunk_gap + chunk_height;
chunk_height = 0;
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
chunk_gap = gap;
}
chunk.push(sub_item.clone());
chunk_height += sub_height;
@@ -972,19 +993,19 @@ impl Engine {
height: chunk_height,
items: chunk,
});
insertion_height = chunk_height;
insertion_height = chunk_gap + chunk_height;
}
} else {
let goal = document.page_goal - insertion_height;
if (!current.items.is_empty() || !current.insertions.is_empty())
&& (used + insertion.height > goal
&& (used + gap + insertion.height > goal
|| class_height + insertion.height > class_limit)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
insertion_height += insertion.height;
insertion_height += gap + insertion.height;
current.insertions.push(insertion.clone());
}
}
@@ -1297,13 +1318,18 @@ impl Engine {
}
}
VerticalItem::Insertion(insertion) => {
let gap = if current.insertions.is_empty() {
next.insertion_skip.max(0)
} else {
0
};
let goal = next.page_goal - insertion_height;
if !current.items.is_empty() && used + insertion.height > goal {
if !current.items.is_empty() && used + gap + insertion.height > goal {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
insertion_height += insertion.height;
insertion_height += gap + insertion.height;
current.insertions.push(insertion.clone());
}
VerticalItem::Mark(mark) => {
@@ -1798,6 +1824,59 @@ mod tests {
assert_eq!(layout.pages()[1].insertions.len(), 1);
}
#[test]
fn insertion_skip_reserves_space_before_the_first_insertion() {
let insertion = || {
Unit::insertion(
2,
1,
80,
vec![VerticalItem::Rule {
width: 65_536,
height: 80,
}],
)
};
let mut document = Document::new(100, vec![Unit::rule(1, 10, 10), insertion()]);
document.insertion_classes = vec![InsertionClass {
number: 1,
fraction_percent: 100,
max_height: None,
}];
assert_eq!(Engine::layout(&document).pages().len(), 1);
document.insertion_skip = 20;
let layout = Engine::layout(&document);
assert_eq!(layout.pages().len(), 2);
assert_eq!(layout.pages()[0].insertions.len(), 0);
assert_eq!(layout.pages()[1].insertions.len(), 1);
// A body plus insertion plus skip that exactly fills the page must not
// be pushed to a second page.
let mut exact = Document::new(
100,
vec![
Unit::rule(1, 10, 10),
Unit::insertion(
2,
1,
70,
vec![VerticalItem::Rule {
width: 65_536,
height: 70,
}],
),
],
);
exact.insertion_skip = 20;
exact.insertion_classes = vec![InsertionClass {
number: 1,
fraction_percent: 100,
max_height: None,
}];
assert_eq!(Engine::layout(&exact).pages().len(), 1);
}
#[test]
fn incremental_layout_places_a_fitting_late_top_float_the_same_way() {
let mut document = Document::new(