Clamp held-over insertion depth with splitmaxdepth

This commit is contained in:
gloamxun committed 2026-10-23 23:35:20 +08:00
1 parent 0f4c1f85ae
commit a09b1d1d36
5 files changed
+84 -12

No files matched your search

+72 -7
View File
@@ -655,6 +655,11 @@ pub struct Document {
/// carried over to the next page. Defaults to 10pt.
#[serde(default = "default_split_top_skip")]
pub split_top_skip: i64,
/// TeX's `\splitmaxdepth`: maximum depth kept for the first box of
/// insertion material carried over to the next page. Any excess depth is
/// moved into that box's height. Defaults to `\maxdimen` (no clamping).
#[serde(default = "default_split_max_depth")]
pub split_max_depth: i64,
/// TeX floatsep: vertical space between two floats of the same placement
/// on one page. Defaults to 12pt.
#[serde(default = "default_float_sep")]
@@ -708,6 +713,10 @@ fn default_float_sep() -> i64 {
12 * 65_536
}
fn default_split_max_depth() -> i64 {
i64::MAX
}
fn default_split_top_skip() -> i64 {
10 * 65_536
}
@@ -769,6 +778,7 @@ impl Document {
insertion_classes: Vec::new(),
insertion_skip: 0,
split_top_skip: default_split_top_skip(),
split_max_depth: default_split_max_depth(),
float_sep: default_float_sep(),
text_float_sep: default_text_float_sep(),
in_text_sep: default_in_text_sep(),
@@ -910,13 +920,6 @@ fn document_nodes(
}
if let VerticalItem::Paragraph(paragraph) = item {
if paragraph.lines.len() > 1 {
// Paragraphs built with fixed-cell styles mix raw units with
// TFM extents; only trust a descent that fits the box.
let depth = if paragraph.height > paragraph.depth {
paragraph.depth
} else {
0
};
// TeX breaks pages between lines. Expand a paragraph into
// one node per line so the page builder is free to break
// inside it; each slice renders as its own single-line
@@ -1034,6 +1037,42 @@ fn item_height(item: &VerticalItem) -> i64 {
}
}
/// TeX depth contributed by a vertical item. Only paragraph and math items
/// currently carry depth; boxes have no depth field.
fn item_depth(item: &VerticalItem) -> i64 {
match item {
VerticalItem::Paragraph(paragraph) => paragraph.depth,
VerticalItem::Math { depth, .. } => *depth,
_ => 0,
}
}
/// TeX `\splitmaxdepth`: clamp the depth of the first box in held-over
/// insertion material, moving the excess depth into its height so the page
/// accounts for it exactly once.
fn clamp_split_depth(document: &Document, material: &mut [VerticalItem]) {
let limit = document.split_max_depth.max(0);
for item in material.iter_mut() {
let depth = item_depth(item);
if depth <= limit {
continue;
}
let excess = depth - limit;
match item {
VerticalItem::Paragraph(paragraph) => {
paragraph.height += excess;
paragraph.depth = limit;
}
VerticalItem::Math { height, depth, .. } => {
*height += excess;
*depth = limit;
}
_ => {}
}
return;
}
}
/// TeX `\prevgraf` for a finished page: the line count of the last paragraph
/// broken on it.
fn page_prev_graf(page: &Page) -> i64 {
@@ -1070,6 +1109,7 @@ fn held_insertion_material(
if !material.is_empty() && split > 0 && capacity >= split.saturating_add(first) {
material.insert(0, VerticalItem::Glue(split));
}
clamp_split_depth(document, &mut material);
material
}
@@ -3901,6 +3941,31 @@ mod tests {
assert_eq!(second.height, 80 * scale);
}
#[test]
fn split_max_depth_clamps_the_first_held_item() {
let scale = 65_536;
let mut document = Document::new(1000 * scale, vec![]);
document.split_top_skip = 0;
document.split_max_depth = 3 * scale;
let material = held_insertion_material(
&document,
1000 * scale,
vec![VerticalItem::Math {
atom: crate::math::MathAtom::Symbol('x'),
width: scale,
height: 20 * scale,
depth: 10 * scale,
}],
);
match &material[0] {
VerticalItem::Math { height, depth, .. } => {
assert_eq!(*depth, 3 * scale);
assert_eq!(*height, 27 * scale);
}
other => panic!("unexpected material {other:?}"),
}
}
#[test]
fn insertion_skip_reserves_space_before_the_first_insertion() {
let insertion = || {