Match TeX hyphenation and justification semantics

This commit is contained in:
texrra committed 2026-10-23 01:00:45 +08:00
1 parent ac134b3c4e
commit 4fbc43f5ff
6 files changed
+5074 -56

No files matched your search

@@ -0,0 +1,14 @@
as-so-ciate
as-so-ciates
dec-li-na-tion
oblig-a-tory
phil-an-thropic
present
presents
project
projects
reci-procity
re-cog-ni-zance
ref-or-ma-tion
ret-ri-bu-tion
ta-ble
File diff suppressed because it is too large. Load diff
+72 -22
View File
@@ -363,8 +363,10 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
}
let mut ranges = solution.unwrap_or_else(|| {
// TeX's fallback when no feasible break exists: fill each line up to
// the first break at or past the measure, then continue.
// TeX's final pass forces an overfull break only as a last resort.
// Emulate that choice: take the longest prefix that still satisfies
// \tolerance, and fall back to the first prefix that overflows the
// measure when no feasible line exists.
let mut greedy = Vec::new();
let mut start_index = 0;
while start_index + 1 < count {
@@ -374,7 +376,8 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
} else {
0
};
let mut chosen = count - 1;
let mut feasible = None;
let mut overflow = None;
for end_index in start_index + 1..count {
let start = breakpoints[start_index];
let end = breakpoints[end_index];
@@ -382,37 +385,72 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
continue;
}
let mut natural = 0_i64;
let mut stretch = 0_i64;
let mut shrink = 0_i64;
let mut has_box = false;
for (offset, item) in items[start..end].iter().enumerate() {
let last_item = start + offset + 1 == end;
match item {
HorizontalItem::Box(box_item) => natural += box_item.width,
HorizontalItem::Glue { width, .. } => {
if start + offset + 1 != end {
natural += *width;
HorizontalItem::Box(box_item) => {
natural += box_item.width;
has_box = true;
}
HorizontalItem::Glue {
width,
stretch: stretch_amount,
shrink: shrink_amount,
} => {
if last_item {
continue;
}
natural += *width;
stretch += *stretch_amount;
shrink += *shrink_amount;
}
HorizontalItem::Penalty { .. } => {}
HorizontalItem::Discretionary {
pre_break_width,
no_break_width,
} => {
if start + offset + 1 == end {
natural += *pre_break_width;
natural += if last_item {
*pre_break_width
} else {
natural += *no_break_width;
}
*no_break_width
};
}
}
}
if natural >= target {
chosen = end_index;
break;
if !has_box {
continue;
}
if overflow.is_none() && natural >= target {
overflow = Some(end_index);
}
let badness = if end == items.len() && natural <= target {
0
} else if natural < target {
if stretch > 0 {
(100.0 * ((target - natural) as f64 / stretch as f64).powi(3))
.round()
.min(10_000.0) as i64
} else {
10_000
}
} else if shrink > 0 {
(100.0 * ((natural - target) as f64 / shrink as f64).powi(3))
.round()
.min(10_000.0) as i64
} else {
10_000
};
if badness <= TOLERANCE {
feasible = Some(end_index);
}
}
let chosen = feasible.or(overflow).unwrap_or(count - 1);
greedy.push((breakpoints[start_index], breakpoints[chosen]));
start_index = chosen;
}
// The DP path produces ranges from the end backwards; build the
// fallback the same way so the single reverse below applies to both.
greedy.reverse();
greedy
});
@@ -479,10 +517,11 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
let is_last = line_index + 1 == line_count;
if !is_last {
// 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);
// 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 {
let mut remaining = delta;
for item in &mut line {
@@ -558,6 +597,13 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
}
/// TeX Live's American English hyphenation patterns (`hyph-en-us`, 2005),
/// which are what pdfLaTeX loads for `\language` 0 with the `english` option.
/// The vendored plain-TeX patterns differ (for example `kappa`), so route-D
/// uses the same data as the reference implementation.
const HYPHENATION_PATTERNS: &str = include_str!("../assets/hyph-en-us-patterns.txt");
const HYPHENATION_EXCEPTIONS: &str = include_str!("../assets/hyph-en-us-exceptions.txt");
/// Convert text using TFM character widths and fontdimen glue parameters.
pub fn text_items_with_font(
text: &str,
@@ -565,7 +611,9 @@ pub fn text_items_with_font(
font: &crate::cmr10::Cmr10Metrics,
) -> Vec<HorizontalItem> {
let mut items = Vec::new();
let hyphenator = hyphenate::Hyphenator::plain_tex_en_us();
let mut hyphenator = hyphenate::Hyphenator::default();
hyphenator.load_patterns(HYPHENATION_PATTERNS);
hyphenator.insert_exceptions(HYPHENATION_EXCEPTIONS);
let caser = hyphenate::AsciiLowerCaser::default();
// TeX's text fonts turn runs of hyphens into en/em dashes. cmr10's TFM
// does not carry these ligatures, so apply the same mapping before
@@ -631,10 +679,12 @@ pub fn text_items_with_font(
italic: font.italic(character),
}));
let boundary = character_index + 1;
// TeX's defaults for American English are \lefthyphenmin=2 and
// \righthyphenmin=3.
if !ligated
&& points.contains(&boundary)
&& boundary >= 2
&& boundary + 2 <= word.chars().count()
&& boundary + 3 <= word.chars().count()
{
items.push(HorizontalItem::Discretionary {
pre_break_width: font.width('-'),
+10
View File
@@ -261,3 +261,13 @@ fn paragraph_breaks_match_pdftex_at_150pt() {
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.
#[test]
fn paragraph_breaks_match_pdftex_across_measures() {
for width in [100u32, 120, 150, 180, 200, 240] {
compare_fixture(WORDS, width);
}
}