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

+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();