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

+21 -19
View File
@@ -48,13 +48,13 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
PDF opt-in with `?engine=route-d`). 282 workspace tests exist (281 run, one
`#[ignore]`d until P1 supplies TeX tolerance/demerits).
PDF opt-in with `?engine=route-d`). 285 workspace tests are green and no oracle
fixture is ignored.
Known gaps, in dependency order:
1. paragraph breaking is TeX-like but not TeX-exact (simplified demerits, no
`\parshape`/`\looseness`/spacefactor);
1. paragraph breaking reproduces TeX's passes and demerits, but not yet
`\parshape`, `\looseness`, spacefactor or explicit `\tolerance` overrides;
2. the page builder is a scrolling builder with fraction policies, not a TeX
page builder with badness and an interpreted `\output`;
3. `\insert` classes are data structures, not TeX registers and an output
@@ -123,10 +123,10 @@ Deliverables:
- a `--regen`-style path for refreshing goldens.
Status: the foundation landed in `crates/texrra-tex-core/tests/oracle.rs`. The
150pt CMR10 fixture matches pdfLaTeX word for word (x tolerance 0.05pt), and the
ink-band test now runs on that same oracle-verified fixture. The 180pt fixture is
`#[ignore]`d with the reason "P1: TeX tolerance and demerits" because pdfLaTeX's
emergency pass accepts a 5/7 word split that the simplified demerit model rejects.
150pt CMR10 fixture matches pdfLaTeX word for word (x tolerance 0.05pt) and
exercises the overfull fallback; the 180pt fixture exercises the ordinary
pass-1 solution and matches as well. The ink-band test runs on the same
oracle-verified paragraph.
Acceptance: at least 20 paragraph fixtures and 5 page fixtures wired into
`cargo test`, harness green.
@@ -160,17 +160,19 @@ key instead of the first (`k` + `a` picked up `w`'s kern), and justification
squeezed overfull lines past their declared shrink. Both are fixed and the
150pt fixture now matches pdfLaTeX word for word.
A demerits prototype (two-pass tolerance, fitness classes, `(line_penalty +
badness)^2` plus penalty and adjacency terms) was written and measured, then
reverted because it did not reproduce pdfLaTeX's emergency choices: it chose
`[26, 29]` at 150pt and `[6, 7, 10]` at 180pt where TeX chose `[30, 25]` and
`[7, 7, 8]`. The remaining work is exactly TeX's integer algorithm rather than
a floating-point approximation: fixed-point glue-set ratios, badness clamped to
10000 as soon as the ratio exceeds one, the three passes (`\pretolerance`,
`\tolerance`, emergency), fitness-class adjacency, double/final hyphen
demerits, `\parfillskip`'s infinite stretch for the final line, and TeX's
tie-breaking when two solutions have equal demerits. The 180pt oracle fixture
stays ignored until that lands.
Status (continued): the breaker now reproduces TeX's passes and demerits.
Pass 0 uses `\pretolerance` without hyphenation, pass 1 `\tolerance` with it,
and a third pass with `\tolerance=10000` plus `\emergencystretch` runs only when
that register is positive. With zero emergencystretch TeX gives up and fills
each line to the first break at or past the measure, which is exactly the
overfull fallback implemented here. Demerits combine line penalty, badness,
break penalties and adjacent fitness changes, and the final line keeps its
natural width. Both oracle fixtures now pass, so TeX's emergency choices are
reproduced rather than approximated.
Still open: explicit `\tolerance`/`\pretolerance` overrides, flagged
(hyphenated) demerits, `\parfillskip` variants, `\hangindent`/`\parshape`,
spacefactor, and more width fixtures.
### P2 — Vertical list, page builder, `\output` (L2)