Align centered and flushed blocks within the measure

This commit is contained in:
gloamxun committed 2026-10-24 20:15:31 +08:00
1 parent 2783dfb284
commit 925c916ea5
5 files changed
+104 -5

No files matched your search

+46 -1
View File
@@ -1058,6 +1058,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
ranges.reverse();
let mut lines = Vec::with_capacity(ranges.len());
let mut max_width = 0;
let mut measure = 0;
let line_count = ranges.len();
for (line_index, (start, end)) in ranges.into_iter().enumerate() {
let mut line: Vec<HorizontalItem> = Vec::new();
@@ -1100,6 +1101,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
// difference between the natural width and the measure across the
// glue, so the shipped line (and therefore the PDF) is flush.
let target = style.line_target(line_index);
measure = measure.max(target);
let (mut natural, mut stretch, mut shrink) = (0_i64, 0_i64, 0_i64);
let mut stretch_by_order = [0_i64; 4];
for item in &line {
@@ -1248,7 +1250,11 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
// 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,
width: if style.centered || style.right_aligned {
max_width.max(measure)
} else {
max_width
},
depth,
lines,
font: ParagraphFont::Roman,
@@ -1595,6 +1601,45 @@ mod tests {
assert_eq!(glyphs(&ten), glyphs(&twelve));
}
#[test]
fn aligned_paragraphs_keep_the_measure_width() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let base = ParagraphStyle {
line_width: 300 * 65_536,
par_indent: 0,
..ParagraphStyle::default()
};
let centered = layout_paragraph_with_font(
"short",
&ParagraphStyle {
centered: true,
..base.clone()
},
&font,
);
let right = layout_paragraph_with_font(
"short",
&ParagraphStyle {
right_aligned: true,
..base.clone()
},
&font,
);
assert_eq!(centered.width, 300 * 65_536);
assert_eq!(right.width, 300 * 65_536);
assert!(
centered.lines[0]
.iter()
.map(|item| match item {
HorizontalItem::Box(box_) => box_.width,
HorizontalItem::Glue { width, .. } => *width,
_ => 0,
})
.sum::<i64>()
< centered.width
);
}
#[test]
fn overfull_lines_do_not_exceed_the_declared_shrink() {
let font = crate::cmr10::Cmr10Metrics::Roman;