Start held-over insertions with splittopskip

This commit is contained in:
gloamxun committed 2026-10-23 12:15:27 +08:00
1 parent c54a56ad36
commit e632db02cf
4 files changed
+74 -5

No files matched your search

+68
View File
@@ -630,6 +630,10 @@ pub struct Document {
/// first insertion class on a page. Zero by default for compatibility.
#[serde(default)]
pub insertion_skip: i64,
/// TeX's `\splittopskip`: glue placed at the top of insertion material
/// carried over to the next page. Defaults to 10pt.
#[serde(default = "default_split_top_skip")]
pub split_top_skip: i64,
/// TeX floatsep: vertical space between two floats of the same placement
/// on one page. Defaults to 12pt.
#[serde(default = "default_float_sep")]
@@ -673,6 +677,10 @@ fn default_float_sep() -> i64 {
12 * 65_536
}
fn default_split_top_skip() -> i64 {
10 * 65_536
}
fn default_text_float_sep() -> i64 {
20 * 65_536
}
@@ -724,6 +732,7 @@ impl Document {
total_number: default_total_number(),
insertion_classes: Vec::new(),
insertion_skip: 0,
split_top_skip: default_split_top_skip(),
float_sep: default_float_sep(),
text_float_sep: default_text_float_sep(),
in_text_sep: default_in_text_sep(),
@@ -920,6 +929,27 @@ fn item_height(item: &VerticalItem) -> i64 {
/// Returns `(accepted, remainder, reserved_height)`. A single item larger than
/// the capacity is still accepted alone: TeX only splits insertion material at
/// item boundaries.
/// TeX starts a split insertion's continuation with `\splittopskip`. Only add
/// the glue when the class capacity can also hold the first content item;
/// otherwise a class with no remaining capacity would accept the glue forever
/// and never make progress.
fn held_insertion_material(
document: &Document,
capacity: i64,
mut material: Vec<VerticalItem>,
) -> Vec<VerticalItem> {
let split = document.split_top_skip.max(0);
let first = material
.iter()
.map(item_height)
.find(|height| *height > 0)
.unwrap_or(0);
if !material.is_empty() && split > 0 && capacity >= split.saturating_add(first) {
material.insert(0, VerticalItem::Glue(split));
}
material
}
fn take_insertion_prefix(
document: &Document,
class: u8,
@@ -1748,6 +1778,7 @@ impl Engine {
let material = held.remove(&class).unwrap_or_default();
let limit = document.insertion_class_limit(class);
let capacity = (limit - used.get(&class).copied().unwrap_or(0)).max(0);
let material = held_insertion_material(document, capacity, material);
let (accepted, remainder, reserved) =
take_insertion_prefix(document, class, material, capacity);
attach_insertion(&mut insertions, &mut used, class, accepted, reserved);
@@ -1819,6 +1850,7 @@ impl Engine {
for class in classes {
let material = held.remove(&class).unwrap_or_default();
let limit = document.insertion_class_limit(class);
let material = held_insertion_material(document, limit, material);
let (accepted, remainder, reserved) =
take_insertion_prefix(document, class, material, limit);
attach_insertion(&mut insertions, &mut used, class, accepted, reserved);
@@ -3373,6 +3405,42 @@ mod tests {
assert_eq!(engine.pages().len(), 2);
}
#[test]
fn held_over_insertions_start_with_split_top_skip() {
let scale = 65_536;
let mut document = Document::new(
200 * scale,
vec![Unit::insertion(
1,
0,
120 * scale,
vec![
VerticalItem::Rule {
width: 0,
height: 60 * scale,
},
VerticalItem::Rule {
width: 0,
height: 60 * scale,
},
],
)],
);
document.split_top_skip = 20 * scale;
document.insertion_classes = vec![InsertionClass {
number: 0,
fraction_percent: 50,
max_height: None,
skip: None,
multiplier: 1000,
}];
let engine = Engine::layout_paged(&document).expect("paged");
assert_eq!(engine.pages().len(), 2);
let second = &engine.pages()[1].insertions[0];
assert_eq!(second.items.first(), Some(&VerticalItem::Glue(20 * scale)));
assert_eq!(second.height, 80 * scale);
}
#[test]
fn insertion_skip_reserves_space_before_the_first_insertion() {
let insertion = || {