Reproduce TeX paragraph breaking with tolerance passes and demerits

- replace the single-tolerance DP with pass 0 (pretolerance, no hyphenation),
  pass 1 (tolerance, hyphenation) and an optional emergency pass
- add emergencystretch to ParagraphStyle; the emergency pass only runs when it
  is positive, otherwise TeX's overfull fallback applies and each line fills to
  the first break at or past the measure
- compute badness and fitness classes like TeX and fold them with penalties
  into demerits, ignoring the trailing forced penalty on the final line
- keep forward order in the greedy fallback
- enable the 180pt pdfLaTeX oracle fixture and add an emergencystretch test
- give route-D extraction tests scaled body styles so fixed-cell
  ParagraphStyle defaults no longer mix raw units with TFM advances
- document the passes, oracle results and remaining P1 gaps
This commit is contained in:
gloamxun committed 2026-10-23 00:50:57 +08:00
1 parent 03aa1c5110
commit ac134b3c4e
8 files changed
+331 -164

No files matched your search

+2 -3
View File
@@ -255,10 +255,9 @@ fn paragraph_breaks_match_pdftex_at_150pt() {
compare_fixture(WORDS, 150);
}
/// P1 work: matching pdfTeX's emergency line breaking needs TeX's tolerance
/// and demerit model, so this fixture currently records the divergence.
/// The same text at a wider measure; TeX produces four lines here (7, 8, 9
/// and 1 words), which exercises the pass-1 solution rather than the fallback.
#[test]
#[ignore = "P1: TeX tolerance and demerits"]
fn paragraph_breaks_match_pdftex_at_180pt() {
compare_fixture(WORDS, 180);
}
+26 -10
View File
@@ -2,9 +2,29 @@
//! incremental paragraph edit.
use texrra_tex_core::{
Document, Engine, ParagraphFont, ParagraphStyle, Unit, pdf::display_lists_to_pdf,
Document, Engine, ParagraphFont, ParagraphStyle, Unit, cmr10::Cmr10Metrics,
pdf::display_lists_to_pdf,
};
/// Paragraph style in TeX scaled points, driven by the CMR10 font metrics.
fn body_style() -> ParagraphStyle {
let font = Cmr10Metrics::default();
ParagraphStyle {
line_width: 480 * 65_536,
left_skip: 0,
right_skip: 0,
centered: false,
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
line_height: 12 * 65_536,
char_width: font.width('x'),
space_width: font.space(),
space_stretch: font.stretch(),
space_shrink: font.shrink(),
}
}
fn paragraph(id: u64, marker: &str) -> Unit {
let text = format!("{} {}", marker, "alpha beta gamma delta ".repeat(80));
Unit::paragraph_with_style(id, text, ParagraphStyle::default())
@@ -133,6 +153,7 @@ fn route_d_page_matches_pdftex_raster_geometry() {
centered: false,
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
line_height: 12 * 65_536,
char_width: 10 * 65_536,
space_width: 4 * 65_536,
@@ -280,6 +301,7 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
centered: false,
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
line_height: 12 * 65_536,
char_width: 10 * 65_536,
space_width: 4 * 65_536,
@@ -388,7 +410,7 @@ fn accented_text_round_trips_through_pdf_extraction() {
vec![Unit::paragraph_with_style(
1,
"caf\u{e9} na\u{ef}ve r\u{e9}sum\u{e9}",
ParagraphStyle::default(),
body_style(),
)],
);
let engine = Engine::layout(&document);
@@ -443,14 +465,7 @@ fn route_d_text_matches_pdftex_extraction() {
let body =
"The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs.";
let document = Document::new(
61,
vec![Unit::paragraph_with_style(
1,
body,
ParagraphStyle::default(),
)],
);
let document = Document::new(61, vec![Unit::paragraph_with_style(1, body, body_style())]);
let engine = Engine::layout(&document);
let lists: Vec<_> = engine
.pages()
@@ -564,6 +579,7 @@ fn route_d_multiline_bands_track_pdftex() {
centered: false,
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
line_height: 12 * 65_536,
char_width: 10 * 65_536,
space_width: 4 * 65_536,