From 8cb09ea5a8826284b6ac64434825d8c252912ef1 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Fri, 23 Oct 2026 01:26:54 +0800 Subject: [PATCH] Support TeX looseness on paragraph final lines --- docs/ROUTE_D_PLAN.md | 20 ++++++++++++-------- docs/TEX_CORE.md | 27 +++++++++++++++------------ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index 531e346..4ab2abb 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -48,7 +48,7 @@ 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`). 299 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 305 workspace tests are green and no oracle fixture is ignored. Known gaps, in dependency order: @@ -56,10 +56,10 @@ Known gaps, in dependency order: 1. paragraph breaking reproduces TeX's passes and demerits (including `\adjdemerits`, `\doublehyphendemerits`, `\finalhyphendemerits`, `\hyphenpenalty`, `\exhyphenpenalty`, `\hyphenation` exceptions, - `\hangindent`/`\hangafter`/`\parshape` line geometry and `\parfillskip` - with or without `fil` and default `\sfcode` sentence spacing), but not yet - `\looseness`, custom `\sfcode` overrides or generic `fil`/`fill` glue - orders; + `\hangindent`/`\hangafter`/`\parshape` line geometry, `\parfillskip` + with or without `fil`, default `\sfcode` sentence spacing and + `\looseness`), but not yet custom `\sfcode` overrides or generic + `fil`/`fill` glue orders; 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 @@ -196,9 +196,13 @@ stretch is multiplied and shrink divided by the space factor, `\fontdimen7` is added from 2000, and `\frenchspacing` switches it off; a 150pt punctuated oracle fixture matches pdfLaTeX's line breaks and word positions. -Still open: generic `fil`/`fill` glue orders, `\looseness`, custom `\sfcode` -overrides, vertical-list skips (`\baselineskip`, `\lineskip`, `\parskip`), -double/final hyphen demerit oracle fixtures, and more width fixtures. +`\looseness` adjusts the final line count inside TeX's `[1,9]` window and is +reverted when no feasible layout exists; 120pt and 200pt fixtures require one +extra line and match pdfLaTeX exactly. + +Still open: generic `fil`/`fill` glue orders, custom `\sfcode` overrides, +vertical-list skips (`\baselineskip`, `\lineskip`, `\parskip`), double/final +hyphen demerit oracle fixtures, and more width fixtures. ### P2 — Vertical list, page builder, `\output` (L2) diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 58c929b..b7fc03f 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -109,7 +109,7 @@ and justification. | Milestone | Content | Acceptance | | --- | --- | --- | | M0 | vertical list, page builder, checkpoint, page hash, edit demo | edit on page 2 rebuilds only page 2 | -| M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, pdfLaTeX oracle matrix 100-240pt plus register fixtures; `\looseness`, `\parshape` and more fixtures next | +| M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing, `\looseness`, pdfLaTeX oracle matrix 100-240pt plus four register fixtures; custom `\sfcode`, generic fil and vertical skips next | | M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top/bottom `Float` placement; interpreted `\output`, float pages next | | M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | **partial**: source parsing, natural column widths, spans, rule rows, 10pt display-list painting and row-level page splitting; no booktabs/longtable parity yet | | M4 | math lists, scripts, fractions, radicals, accents, delimiters | **partial**: token parser, atom boxes with CMR10 metrics, delimiter scaling, accents and a display-list painter; no CMEX size variants or TeX math spacing classes yet | @@ -129,13 +129,16 @@ and justification. line is justified. Sentence spacing follows TeX's default `\sfcode` values: interword glue stretch is multiplied by the space factor, shrink is divided by it, `\fontdimen7` is added from factor 2000, and `\frenchspacing` - disables the effect. Remaining gaps are `\looseness`, custom `\sfcode` - overrides, generic `fil`/`fill` glue orders outside `\parfillskip`, and the - vertical-list skips (`\baselineskip`, `\lineskip`, `\parskip`). + disables the effect. `\looseness` shifts the paragraph by whole lines when + the requested count lies between one and nine lines away from the natural + count (the adjustment is reverted when no feasible layout exists). + Remaining gaps are custom `\sfcode` overrides, generic `fil`/`fill` glue + orders outside `\parfillskip`, and the vertical-list skips + (`\baselineskip`, `\lineskip`, `\parskip`). The pdfLaTeX oracle pins the 100-240pt measure matrix plus `\hangindent`, -`\parfillskip` and sentence-spacing fixtures; all match and no oracle fixture -is ignored. +`\parfillskip`, sentence-spacing and `\looseness` fixtures; all match and no +oracle fixture is ignored. - `\middle|` inside `\left...\right` is accepted as a delimiter symbol (not yet stretched). - `\sqrt[n]{x}` carries an optional degree atom; `\left.` is an empty @@ -300,7 +303,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph. -The crate currently has 188 unit tests plus six route-D integration tests and eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 299 tests (188 route-D unit, 6 route-D integration, 8 oracle, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. +The crate currently has 192 unit tests plus six route-D integration tests and ten oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 305 tests (192 route-D unit, 6 route-D integration, 10 oracle, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. ### M0 incremental demo @@ -328,11 +331,11 @@ The staged program that closes these milestones is described in Route D currently covers M0-M4/M6 as a working subset. The remaining milestones are deliberately open rather than approximated: -- **M1**: `\looseness`, custom `\sfcode` overrides, generic `fil`/`fill` - glue orders and the vertical-list skips are still open; the tolerance, penalty - and demerit registers, `\hyphenation` exceptions, - `\hangindent`/`\parshape`, `\parfillskip` with or without `fil`, and the - default `\sfcode`/`\frenchspacing` sentence spacing are modelled. +- **M1**: custom `\sfcode` overrides, generic `fil`/`fill` glue orders and + the vertical-list skips are still open; the tolerance, penalty and demerit + registers, `\hyphenation` exceptions, `\hangindent`/`\parshape`, + `\parfillskip` with or without `fil`, `\looseness`, and the default + `\sfcode`/`\frenchspacing` sentence spacing are modelled. - **M5**: `cmr10.tfm` is parsed at runtime with widths, glyph heights/depths and kern pairs. only used text faces embed Liberation Serif/Mono as Type0/Identity-H; each face is subset to the glyphs actually used (composite components included, glyph IDs retained); FontFile2 and ToUnicode streams use Flate compression; the ToUnicode CMap follows the used characters; ff/fi/fl/ffi/ffl ligatures come from TFM, and CMR10 italic corrections are applied at line ends; full NFSS family switching is not implemented. - **M6**: `\output`, `\insert`, mark/float output policies, class-file execution and math limit policies are only partially modeled; `OutputRoutine::wants_break` can force a page break between units, and insertion material is stacked by class from the page bottom; `\output` is still Rust code rather than interpreted tokens.