Shrink overfull final lines and widen the oracle matrix

This commit is contained in:
gloamxun committed 2026-10-23 02:45:11 +08:00
1 parent bcd3b6fa66
commit 6305f059ac
5 files changed
+98 -50

No files matched your search

+59 -30
View File
@@ -799,15 +799,17 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
} else {
0
};
if !is_last || !style.par_fillskip_fil {
natural += fillskip;
stretch += fillskip_stretch;
// TeX can stretch normal glue past its nominal stretch (badness
// simply exceeds 100), but it never shrinks past the declared
// shrink: an overfull line keeps its overflow instead of pulling
// glue beyond its limits.
let delta = (target - natural).max(-shrink);
if delta > 0 && stretch > 0 {
natural += fillskip;
stretch += fillskip_stretch;
// TeX can stretch normal glue past its nominal stretch (badness
// simply exceeds 100), but it never shrinks past the declared
// shrink: an overfull line keeps its overflow instead of pulling
// glue beyond its limits. A final line with a `fil` parfillskip only
// shrinks: the infinite stretch absorbs any shortfall, so only a
// line wider than the measure adjusts its finite glue.
let delta = (target - natural).max(-shrink);
if delta > 0 {
if !is_last || !style.par_fillskip_fil {
let mut remaining = delta;
for item in &mut line {
if let HorizontalItem::Glue {
@@ -830,30 +832,30 @@ 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,
shrink: shrink_amount,
..
} = item
&& *shrink_amount > 0
{
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 { .. }))
}
} else if delta < 0 {
let mut remaining = delta;
for item in &mut line {
if let HorizontalItem::Glue {
width,
shrink: shrink_amount,
..
} = item
&& *shrink_amount > 0
{
*width += remaining;
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
.iter()
@@ -1181,6 +1183,33 @@ mod tests {
);
}
#[test]
fn overfull_final_line_shrinks_its_glue() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let style = ParagraphStyle {
line_width: 23 * 65_536,
par_indent: 0,
emergencystretch: 0,
line_height: 12 * 65_536,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font("aa bb", &style, &font);
assert_eq!(paragraph.line_count(), 1);
let glue = paragraph.lines[0].iter().find_map(|item| match item {
HorizontalItem::Glue { width, .. } => Some(*width),
_ => None,
});
let glue = glue.expect("the final line must keep its interword glue");
assert!(
glue < font.space(),
"an overfull final line must shrink its glue"
);
assert!(
glue >= font.space() - font.shrink(),
"the final line must not shrink past the declared shrink"
);
}
#[test]
fn words_break_at_glue_and_lines_are_justified() {
let style = ParagraphStyle {
+22 -4
View File
@@ -406,12 +406,30 @@ fn paragraph_breaks_match_pdftex_at_180pt() {
compare_fixture(WORDS, 180);
}
/// Several measures in one run: 100/120pt exercise hyphenated and overfull
/// breaks, 150pt needs TeX's final-pass overfull box, and 180/200/240pt are
/// ordinary tolerance solutions.
/// Several measures in one run: 90/100/110/120pt exercise hyphenated and
/// overfull breaks, 130/150/160pt need TeX's final-pass overfull box, and
/// 180/200/220/240pt are ordinary tolerance solutions.
#[test]
fn paragraph_breaks_match_pdftex_across_measures() {
for width in [100u32, 120, 150, 180, 200, 240] {
for width in [90u32, 100, 110, 120, 130, 150, 160, 180, 200, 220, 240] {
compare_fixture(WORDS, width);
}
}
/// `\emergencystretch` gives the final pass extra stretch, so TeX can avoid
/// an overfull line and break the paragraph into more lines.
#[test]
fn emergencystretch_matches_pdftex() {
compare_fixture_with(WORDS, 150, "\\emergencystretch=10pt ", |style| {
style.emergencystretch = 10 * 65_536;
});
}
/// `\leftskip` narrows the measure and shifts every line, so line starts and
/// breaks must match pdfLaTeX.
#[test]
fn leftskip_matches_pdftex() {
compare_fixture_with(WORDS, 150, "\\leftskip=20pt ", |style| {
style.left_skip = 20 * 65_536;
});
}