Add a pdfLaTeX oracle and clamp justification glue

This commit is contained in:
gloamxun committed 2026-10-23 00:33:35 +08:00
1 parent 1e2d4afd6b
commit 9726f5f736
6 files changed
+379 -18

No files matched your search

+55 -2
View File
@@ -342,7 +342,10 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
let is_last = line_index + 1 == line_count;
if !is_last {
let delta = target - natural;
// TeX clamps the glue set ratio to the available stretch or
// shrink; an overfull line stays overfull instead of pulling its
// glue past the declared shrink.
let delta = (target - natural).clamp(-shrink, stretch);
if delta > 0 && stretch > 0 {
let mut remaining = delta;
for item in &mut line {
@@ -367,6 +370,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
*width += remaining;
}
} else if delta < 0 && shrink > 0 {
let mut remaining = delta;
for item in &mut line {
if let HorizontalItem::Glue {
width,
@@ -375,9 +379,19 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
} = item
&& *shrink_amount > 0
{
*width += delta * *shrink_amount / shrink;
let share = delta * *shrink_amount / shrink;
*width += share;
remaining -= share;
}
}
if remaining != 0
&& let Some(HorizontalItem::Glue { width, .. }) = line
.iter_mut()
.rev()
.find(|item| matches!(item, HorizontalItem::Glue { .. }))
{
*width += remaining;
}
}
}
let width: i64 = line
@@ -430,6 +444,9 @@ pub fn text_items_with_font(
stretch: font.stretch(),
shrink: font.shrink(),
});
// TeX never kerns across interword glue.
previous_box = None;
previous_character = None;
}
let points: std::collections::HashSet<usize> = if word.is_ascii()
&& word
@@ -531,6 +548,42 @@ mod tests {
.count()
}
#[test]
fn overfull_lines_do_not_exceed_the_declared_shrink() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let style = ParagraphStyle {
line_width: 150 * 65_536,
par_indent: 0,
line_height: 12 * 65_536,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font(
"alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu",
&style,
&font,
);
let first = &paragraph.lines[0];
let mut total = 0_i64;
for item in first {
match item {
HorizontalItem::Box(box_) => total += box_.width,
HorizontalItem::Glue { width, shrink, .. } => {
total += *width;
assert!(
*width >= font.space() - *shrink,
"glue shrank past its declared shrink"
);
}
HorizontalItem::Penalty { width, .. } => total += *width,
HorizontalItem::Discretionary { .. } => {}
}
}
assert!(
total > style.line_width,
"an overfull line must stay overfull instead of shrinking glue past its limits"
);
}
#[test]
fn words_break_at_glue_and_lines_are_justified() {
let style = ParagraphStyle {