Report paragraph depth to the page builder

This commit is contained in:
gloamxun committed 2026-10-23 23:31:47 +08:00
1 parent bd28917f05
commit 0f4c1f85ae
8 files changed
+140 -9

No files matched your search

+54 -2
View File
@@ -910,6 +910,13 @@ 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
@@ -921,9 +928,17 @@ fn document_nodes(
} else {
None
};
let remaining_depth = if remaining.height > remaining.depth {
remaining.depth
} else {
0
};
// The line grid already budgets the descent; keep the
// grid height and report the descent separately for
// `\pagedepth`/`\maxdepth`.
nodes.push(VerticalNode::Box {
height: remaining.height,
depth: 0,
depth: remaining_depth,
});
owners.push(index);
items.push(VerticalItem::Paragraph(remaining.clone()));
@@ -958,7 +973,11 @@ fn document_nodes(
},
VerticalItem::Paragraph(paragraph) => VerticalNode::Box {
height: paragraph.height,
depth: 0,
depth: if paragraph.height > paragraph.depth {
paragraph.depth
} else {
0
},
},
VerticalItem::Halign(halign) => VerticalNode::Box {
height: halign.height,
@@ -5066,6 +5085,7 @@ mod tests {
lines,
height: 100,
width: 50,
depth: 0,
font: ParagraphFont::Roman,
left_skip: 0,
line_indents: vec![0; 10],
@@ -6191,6 +6211,38 @@ mod tests {
);
}
#[test]
fn output_routine_sees_paragraph_depth() {
let paragraph = Paragraph {
lines: vec![Vec::new()],
height: 8 * 65_536,
width: 5 * 65_536,
depth: 2 * 65_536,
font: ParagraphFont::Roman,
left_skip: 0,
line_indents: vec![0],
line_baselines: vec![8 * 65_536],
centered: false,
right_aligned: false,
};
let mut document = Document::new(
300 * 65_536,
vec![Unit::vertical(1, vec![VerticalItem::Paragraph(paragraph)])],
);
document.max_depth = 5 * 65_536;
document.output_routine = Some("\\ifdim\\pagedepth>0pt\\shipout\\box255\\fi".to_string());
let deep = Engine::layout_paged(&document).expect("paragraph page");
assert_eq!(deep.pages().len(), 1, "maxdepth keeps the paragraph depth");
document.max_depth = 0;
let capped = Engine::layout_paged(&document).expect("paragraph page");
assert_eq!(capped.pages().len(), 0, "maxdepth zero drops the depth");
document.output_routine = Some("\\ifdim\\pagedepth=0pt\\shipout\\box255\\fi".to_string());
let zero = Engine::layout_paged(&document).expect("paragraph page");
assert_eq!(zero.pages().len(), 1);
}
#[test]
fn page_prev_graf_counts_the_last_paragraph_lines() {
let style = ParagraphStyle {
+6 -2
View File
@@ -314,8 +314,12 @@ impl PageBuilder {
} else {
*box_height
};
height += contribution;
depth = *box_depth;
// TeX keeps at most \maxdepth of a box descent below the
// page; deeper material is added to the page height as the
// box is contributed. A following box replaces the current
// page depth, so earlier kept descents are discarded.
height += contribution + (*box_depth - self.max_depth).max(0);
depth = (*box_depth).min(self.max_depth);
first_box = false;
has_body = true;
}
+67
View File
@@ -302,6 +302,9 @@ pub struct Paragraph {
pub lines: Vec<Vec<HorizontalItem>>,
pub height: i64,
pub width: i64,
/// TeX depth of the paragraph box: the descent of its last line.
#[serde(default)]
pub depth: i64,
#[serde(default)]
pub font: ParagraphFont,
#[serde(default)]
@@ -346,11 +349,15 @@ impl Paragraph {
Vec::new()
};
let tail_height = (self.height - head_height).max(1);
let head_depth = line_extent(self.lines.last().map(Vec::as_slice).unwrap_or(&[])).1;
let tail_depth = line_extent(tail_lines.last().map(Vec::as_slice).unwrap_or(&[])).1;
self.height = head_height;
self.depth = head_depth;
Paragraph {
lines: tail_lines,
height: tail_height,
width: self.width,
depth: tail_depth,
font: self.font,
left_skip: self.left_skip,
line_indents: tail_indents,
@@ -411,6 +418,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
lines: Vec::new(),
height: 0,
width: 0,
depth: 0,
font: ParagraphFont::Roman,
left_skip: style.left_skip,
line_indents: Vec::new(),
@@ -1011,11 +1019,13 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
line_baselines.push(baseline);
}
}
let depth = extents.last().map_or(0, |(_, descent)| *descent);
Paragraph {
// The page builder still budgets paragraphs on the line grid; the
// baseline table below carries TeX's interline glue for painting.
height: lines.len() as i64 * style.line_height,
width: max_width,
depth,
lines,
font: ParagraphFont::Roman,
left_skip: style.left_skip,
@@ -1421,6 +1431,63 @@ mod tests {
assert_ne!(boxes[0].depth, boxes[1].depth);
}
#[test]
fn paragraph_depth_is_the_last_line_descent() {
let font = crate::cmr10::Cmr10Metrics::new();
let style = ParagraphStyle {
line_width: 400 * 65_536,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font("gyp", &style, &font);
assert_eq!(paragraph.lines.len(), 1);
assert_eq!(
paragraph.depth,
font.depth('p').max(font.depth('y')).max(font.depth('g'))
);
}
#[test]
fn split_at_keeps_each_part_last_line_descent() {
let boxed = |character: char, depth: i64| {
HorizontalItem::Box(HorizontalBox {
width: 5 * 65_536,
height: 5 * 65_536,
depth,
character: Some(character),
italic: 0,
})
};
let items = vec![
boxed('a', 0),
HorizontalItem::Glue {
width: 65_536,
stretch: 0,
shrink: 5 * 65_536,
order: 0,
},
boxed('g', 2 * 65_536),
HorizontalItem::Penalty {
width: 0,
penalty: -10_000,
flagged: false,
},
boxed('x', 0),
];
let style = ParagraphStyle {
line_width: 9 * 65_536,
line_height: 12 * 65_536,
..ParagraphStyle::default()
};
let mut paragraph = layout_items(&items, &style);
assert_eq!(paragraph.lines.len(), 2);
let tail = paragraph.split_at(1);
assert_eq!(paragraph.depth, 2 * 65_536);
assert_eq!(tail.depth, 0);
assert_eq!(paragraph.height, 12 * 65_536);
assert_eq!(tail.height, 12 * 65_536);
assert_eq!(paragraph.height + tail.height, 24 * 65_536);
}
#[test]
fn cmr10_italic_correction_extends_the_line_width() {
let font = crate::cmr10::Cmr10Metrics::default();
+6
View File
@@ -314,6 +314,8 @@ fn route_pages_text_with(
document.top_skip = top_skip_pt * PT;
document.par_skip = par_skip * PT;
document.text_float_sep = 0;
// LaTeX's default \maxdepth keeps descents out of the page height.
document.max_depth = 5 * PT;
Engine::layout(&document)
.pages()
.iter()
@@ -815,6 +817,7 @@ fn pages_respect_stretchy_vertical_glue_like_pdftex() {
let mut document = Document::new(550 * PT, units);
document.top_skip = 12 * PT;
document.text_float_sep = 0;
document.max_depth = 5 * PT;
let engine = Engine::layout_paged(&document).expect("paged layout");
let ours: Vec<String> = engine.pages().iter().map(page_alnum_text).collect();
assert_eq!(
@@ -986,6 +989,8 @@ fn top_floats_reserve_page_space_like_pdftex() {
let mut document = Document::new(550 * PT, units);
document.top_skip = 12 * PT;
document.text_float_sep = 20 * PT;
// LaTeX's default \maxdepth keeps 5pt of descent out of the page height.
document.max_depth = 5 * PT;
let engine = Engine::layout_paged(&document).expect("top floats are page-buildable");
let ours: Vec<String> = engine.pages().iter().map(page_alnum_text).collect();
assert_eq!(
@@ -1066,6 +1071,7 @@ fn footnotes_reserve_page_space_like_pdftex() {
document.top_skip = 12 * PT;
document.text_float_sep = 0;
document.insertion_skip = 12 * PT;
document.max_depth = 5 * PT;
let engine = Engine::layout_paged(&document).expect("insertions are page-buildable");
let ours: Vec<String> = engine.pages().iter().map(page_alnum_text).collect();
assert_eq!(