Justify route-D lines and compare ink bands with pdfTeX

This commit is contained in:
gloamxun committed 2026-10-22 19:45:31 +08:00
1 parent 93330a85ef
commit 4414f27438
4 files changed
+250 -7

No files matched your search

+64 -1
View File
@@ -282,7 +282,8 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
ranges.reverse();
let mut lines = Vec::with_capacity(ranges.len());
let mut max_width = 0;
for (start, end) in ranges {
let line_count = ranges.len();
for (line_index, (start, end)) in ranges.into_iter().enumerate() {
let mut line: Vec<HorizontalItem> = Vec::new();
for (index, item) in items[start..end].iter().enumerate() {
match item {
@@ -317,6 +318,68 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
) {
line.pop();
}
// Bake the justification into the line: TeX distributes the
// 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_width - style.left_skip - style.right_skip).max(1)
- if line_index == 0 { style.par_indent } else { 0 };
let (mut natural, mut stretch, mut shrink) = (0_i64, 0_i64, 0_i64);
for item in &line {
match item {
HorizontalItem::Box(box_item) => natural += box_item.width,
HorizontalItem::Glue {
width,
stretch: stretch_amount,
shrink: shrink_amount,
} => {
natural += *width;
stretch += *stretch_amount;
shrink += *shrink_amount;
}
HorizontalItem::Penalty { width, .. } => natural += *width,
HorizontalItem::Discretionary { .. } => {}
}
}
let is_last = line_index + 1 == line_count;
if !is_last {
let delta = target - natural;
if delta > 0 && stretch > 0 {
let mut remaining = delta;
for item in &mut line {
if let HorizontalItem::Glue {
width,
stretch: stretch_amount,
..
} = item
&& *stretch_amount > 0
{
let share = delta * *stretch_amount / stretch;
*width += share;
remaining -= share;
}
}
if remaining != 0
&& let Some(HorizontalItem::Glue { width, .. }) = line
.iter_mut()
.rev()
.find(|item| matches!(item, HorizontalItem::Glue { .. }))
{
*width += remaining;
}
} else if delta < 0 && shrink > 0 {
for item in &mut line {
if let HorizontalItem::Glue {
width,
shrink: shrink_amount,
..
} = item
&& *shrink_amount > 0
{
*width += delta * *shrink_amount / shrink;
}
}
}
}
let width: i64 = line
.iter()
.map(|item| match item {