Skip to content

class-geometry: model letter.cls, so a letter stops being laid out as an article - #297

Merged
GoKubar merged 2 commits into
mainfrom
engine/letter-class-geometry
Sep 14, 2026
Merged

GoKubar merged 2 commits into
mainfrom
engine/letter-class-geometry

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

class-geometry: model letter.cls, so a letter stops being laid out as an article

ClassKind::parse knew three classes, so DocumentSetup::from_preamble
returned None for \documentclass{letter} and every consumer fell back to
article geometry (adapter::document_setup: article + the class options

  • \pagestyle{empty}). letter.cls shares only size1x.clo with article and
    replaces almost the whole frame around it, so that fallback was wrong in ten
    separate lengths at once:
length article 11pt Letter letter 11pt Letter
\topmargin 8pt (derived) 27pt (fixed)
\headsep 25pt 45pt
\footskip 30pt 25pt
\oddsidemargin 55pt (\@settopointed) 54.8775pt (not)
\evensidemargin derived separately = \oddsidemargin
\parindent 17pt 0pt
\parskip 0pt plus 1pt 7.66498pt rigid (0.7em)
\marginparwidth derived 90pt
\labelsep .5em 5pt
\footnotesep 7.7pt 12pt
\skip\footins 10pt plus 4 minus 2 10pt plus 2 minus 4

\textwidth, \textheight, \baselineskip and \topskip are genuinely
article's (letter inputs the same size file), and the new test asserts that
they do not move, so the two halves of the model stay honest about which
is which.

Also modelled, each verified rather than assumed:

  • No sectioning at all. \section, \chapter, \part, \maketitle
    and \tableofcontents are all undefined in a letter document
    (\@ifundefined on each, TeX Live 2025). headings() returns empty for
    Letter instead of article's table, chapter() is None, and
    secnumdepth/tocdepth keep latex.ltx's 0/0 (measured) rather than
    article's 3/3. mark_rules is empty: letter.cls's own \ps@headings
    builds its running head from \toname/\@date/\thepage and issues no
    marks, and there are no sectioning commands to issue them from.
  • Fewer declared options. letter.cls declares neither onecolumn/
    twocolumn nor titlepage/notitlepage nor openbib; pdflatex reports
    all six as "Unused global option(s)" for a letter, which the model now
    reproduces. Every other class keeps its previous declaration list and
    order
    unchanged.
  • \longindentation is not half the measure. New
    letter_indentation(&ClassOptions): letter.cls assigns
    \longindentation=.5\textwidth at class-load time and nothing updates it
    afterwards, so under [margin=1in] geometry at 11pt the text is
    469.75502pt wide while \longindentation is still 180pt — half of the
    class's 360pt. It takes ClassOptions, not a resolved PageParams,
    precisely so it cannot be handed the geometry width by accident; a closing
    block indented by half the measure would sit 54.88pt too far right. The
    committed fixtures/real-world/letter/reference.pdf confirms it: the
    "Sincerely," line starts at x = 251.328bp = 72 + 179.328, and 180pt is
    179.328bp.

Verification

New tests/letter_oracle.rs, in the style of tests/data/oracle.txt but
self-contained, so adding a letter case never means regenerating the shared
oracle data (it is untouched). 12 configurations — 10/11/12pt, twoside, A4,
legal, landscape, draft, and three geometry ones — 324 lengths, all exact
to the scaled point
, plus first-baseline positions from \pdfsavepos.

The probe was cross-checked before being trusted: it reproduces
tests/data/oracle.txt's own article-10pt b1 position exactly (8865054sp
from the top of the media), so it is the committed method, not a new one.

Two things the measurements forced into the open rather than being assumed:

  • This box's TeX Live 2025 defaults to A4, not the US Letter that
    letter_media() documents for MacTeX 2026, so without geometry the
    MediaBox is A4 even for letterpaper. That is exactly the engine-default
    behaviour PageFrame already models, so each fixture pins the media it was
    measured with and the test feeds it back through engine_default_media.
    Nothing in the test depends on which paper the engine happens to default
    to.
  • letter.cls line 405:
    \@texttop{\ifnum\c@page=1\vskip \z@ plus.00006fil\relax\fi}. On page 1
    only, a fil glue at the top of the text block takes a share of the page's
    leftover and pushes the first baseline down — 2160sp at 10pt. That is page
    building, not a frame length, so the frame is checked on page 2, where
    \@texttop is empty and the model is exact to the sp; page 1 is checked
    only for direction and bound. Forcing \raggedbottom (which also
    \let\@texttop\relax) makes page 1 land on the model's value exactly,
    which is how the cause was identified.

Gates: crates/class-geometry 11 passed / 0 failed (7 new).
crates/render-pipeline --release --no-fail-fast: 213 passed, 0 failed, 1
ignored
— identical to the integration lane's published control for main.
crates/compiler is untouched by this PR.

Measured effect on fixtures/real-world/letter, render producer, v2 display
list, against a control build of the same base (5e6c65be):

  • the first body line starts at x = 72pt instead of 88.936pt — the
    control was indenting it by article's \parindent of 17pt, and letter's is
    0pt;
  • a page number appears at baseline 744.907bp, which is exactly
    text_top + \textheight + \footskip for letter (72.26999 + 650.43001 + 25
    = 747.7pt = 744.907bp). The control emitted no footer at all, because the
    unknown-class fallback forced \pagestyle{empty}; letter.cls ends with
    \pagestyle{plain}.

That second one is worth being explicit about: for this fixture pdflatex
also prints no number on page 1 — but for a different reason, \opening's
own \thispagestyle{empty} when \fromaddress is non-empty. The compiler
does not implement \opening yet (that is the companion crates/compiler
PR), so until it does, page 1 of a letter with an \address shows a page
number that pdflatex does not. The old behaviour was right here by accident
and wrong from page 2 on, where letters really are numbered.

Not in this PR

Stylesheet::from_resolved (crates/render-pipeline/src/style.rs) copies
\parindent, \topskip and \maxdepth out of the resolved class but leaves
\parskip, \leftmargini, \labelsep and \footnotesep at
Stylesheet::article's values, so a letter still gets article's
0pt plus 1pt inter-paragraph glue instead of its own rigid 7.66498pt. It
also derives raggedbottom as !(twoside || twocolumn), while letter.cls
line 404 is an unconditional \raggedbottom. Both are render-pipeline
changes, not class-geometry ones.

Implementation-Agent: claude-opus-5 (engine lane, linux-primary/NixOS).
Commit-Executor: claude-opus-5 direct git (Cursor usage-limit fallback).

Co-Authored-By: Claude Opus 5 [email protected]
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

@GoKubar
GoKubar force-pushed the engine/letter-class-geometry branch from d846aab to 3039f66 Compare September 14, 2026 05:44
… an article

`ClassKind::parse` knew three classes, so `DocumentSetup::from_preamble`
returned `None` for `\documentclass{letter}` and every consumer fell back to
**article** geometry (`adapter::document_setup`: article + the class options
+ `\pagestyle{empty}`). letter.cls shares only `size1x.clo` with article and
replaces almost the whole frame around it, so that fallback was wrong in ten
separate lengths at once:

| length | article 11pt Letter | letter 11pt Letter |
|---|---|---|
| `\topmargin` | 8pt (derived) | **27pt** (fixed) |
| `\headsep` | 25pt | **45pt** |
| `\footskip` | 30pt | **25pt** |
| `\oddsidemargin` | 55pt (`\@settopoint`ed) | **54.8775pt** (not) |
| `\evensidemargin` | derived separately | **= `\oddsidemargin`** |
| `\parindent` | 17pt | **0pt** |
| `\parskip` | 0pt plus 1pt | **7.66498pt** rigid (0.7em) |
| `\marginparwidth` | derived | **90pt** |
| `\labelsep` | .5em | **5pt** |
| `\footnotesep` | 7.7pt | **12pt** |
| `\skip\footins` | 10pt plus 4 minus 2 | **10pt plus 2 minus 4** |

`\textwidth`, `\textheight`, `\baselineskip` and `\topskip` are genuinely
article's (letter inputs the same size file), and the new test asserts that
they do *not* move, so the two halves of the model stay honest about which
is which.

Also modelled, each verified rather than assumed:

* **No sectioning at all.** `\section`, `\chapter`, `\part`, `\maketitle`
  and `\tableofcontents` are all undefined in a `letter` document
  (`\@ifundefined` on each, TeX Live 2025). `headings()` returns empty for
  Letter instead of article's table, `chapter()` is `None`, and
  `secnumdepth`/`tocdepth` keep latex.ltx's 0/0 (measured) rather than
  article's 3/3. `mark_rules` is empty: letter.cls's own `\ps@headings`
  builds its running head from `\toname`/`\@date`/`\thepage` and issues no
  marks, and there are no sectioning commands to issue them from.
* **Fewer declared options.** letter.cls declares neither `onecolumn`/
  `twocolumn` nor `titlepage`/`notitlepage` nor `openbib`; pdflatex reports
  all six as "Unused global option(s)" for a letter, which the model now
  reproduces. Every other class keeps its previous declaration list *and
  order* unchanged.
* **`\longindentation` is not half the measure.** New
  `letter_indentation(&ClassOptions)`: letter.cls assigns
  `\longindentation=.5\textwidth` at class-load time and nothing updates it
  afterwards, so under `[margin=1in]` geometry at 11pt the text is
  469.75502pt wide while `\longindentation` is still **180pt** — half of the
  class's 360pt. It takes `ClassOptions`, not a resolved `PageParams`,
  precisely so it cannot be handed the geometry width by accident; a closing
  block indented by half the measure would sit 54.88pt too far right. The
  committed `fixtures/real-world/letter/reference.pdf` confirms it: the
  "Sincerely," line starts at x = 251.328bp = 72 + 179.328, and 180pt is
  179.328bp.

## Verification

New `tests/letter_oracle.rs`, in the style of `tests/data/oracle.txt` but
self-contained, so adding a `letter` case never means regenerating the shared
oracle data (it is untouched). 12 configurations — 10/11/12pt, twoside, A4,
legal, landscape, draft, and three `geometry` ones — **324 lengths, all exact
to the scaled point**, plus first-baseline positions from `\pdfsavepos`.

The probe was cross-checked before being trusted: it reproduces
`tests/data/oracle.txt`'s own `article-10pt` `b1` position exactly (8865054sp
from the top of the media), so it is the committed method, not a new one.

Two things the measurements forced into the open rather than being assumed:

* **This box's TeX Live 2025 defaults to A4**, not the US Letter that
  `letter_media()` documents for MacTeX 2026, so without `geometry` the
  MediaBox is A4 even for `letterpaper`. That is exactly the engine-default
  behaviour `PageFrame` already models, so each fixture pins the media it was
  measured with and the test feeds it back through `engine_default_media`.
  Nothing in the test depends on which paper the engine happens to default
  to.
* **letter.cls line 405**:
  `\@Texttop{\ifnum\c@page=1\vskip \z@ plus.00006fil\relax\fi}`. On page 1
  only, a fil glue at the top of the text block takes a share of the page's
  leftover and pushes the first baseline down — 2160sp at 10pt. That is page
  building, not a frame length, so the frame is checked on **page 2**, where
  `\@texttop` is empty and the model is exact to the sp; page 1 is checked
  only for direction and bound. Forcing `\raggedbottom` (which also
  `\let\@Texttop\relax`) makes page 1 land on the model's value exactly,
  which is how the cause was identified.

Gates: `crates/class-geometry` 11 passed / 0 failed (7 new).
`crates/render-pipeline --release --no-fail-fast`: **213 passed, 0 failed, 1
ignored** — identical to the integration lane's published control for main.
`crates/compiler` is untouched by this PR.

Measured effect on `fixtures/real-world/letter`, render producer, v2 display
list, against a control build of the same base (`5e6c65be`):

* the first body line starts at x = **72pt** instead of 88.936pt — the
  control was indenting it by article's `\parindent` of 17pt, and letter's is
  0pt;
* a page number appears at baseline **744.907bp**, which is exactly
  `text_top + \textheight + \footskip` for letter (72.26999 + 650.43001 + 25
  = 747.7pt = 744.907bp). The control emitted no footer at all, because the
  unknown-class fallback forced `\pagestyle{empty}`; letter.cls ends with
  `\pagestyle{plain}`.

That second one is worth being explicit about: for **this** fixture pdflatex
also prints no number on page 1 — but for a different reason, `\opening`'s
own `\thispagestyle{empty}` when `\fromaddress` is non-empty. The compiler
does not implement `\opening` yet (that is the companion `crates/compiler`
PR), so until it does, page 1 of a letter with an `\address` shows a page
number that pdflatex does not. The old behaviour was right here by accident
and wrong from page 2 on, where letters really are numbered.

## Not in this PR

`Stylesheet::from_resolved` (`crates/render-pipeline/src/style.rs`) copies
`\parindent`, `\topskip` and `\maxdepth` out of the resolved class but leaves
`\parskip`, `\leftmargini`, `\labelsep` and `\footnotesep` at
`Stylesheet::article`'s values, so a letter still gets article's
`0pt plus 1pt` inter-paragraph glue instead of its own rigid 7.66498pt. It
also derives `raggedbottom` as `!(twoside || twocolumn)`, while letter.cls
line 404 is an unconditional `\raggedbottom`. Both are render-pipeline
changes, not class-geometry ones.

Implementation-Agent: claude-opus-5 (engine lane, linux-primary/NixOS).
Commit-Executor: claude-opus-5 direct git (Cursor usage-limit fallback).

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@GoKubar

GoKubar commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Independent evaluation from lane nixos-pscalc-letter (linux-primary/NixOS), asked to decide whether this PR is stale or worth reviving. Verdict: viable, merge it. Measured, not read:

  • Merges onto current main (e2066db0) cleanly, no conflicts.
  • Its own suite on that merge: crates/class-geometry 11 passed / 0 failed, including all 7 of this PR's tests (letter_lengths_match_pdflatex, letter_first_baseline_matches_pdflatex_to_the_scaled_point, letter_geometry_is_not_article_geometry, letter_indentation_splits_the_measure, letter_has_no_sectioning_and_plain_pages, letter_does_not_declare_the_column_or_titlepage_options, from_preamble_recognises_the_letter_class).
  • Corpus effect, tools/visual-oracle/rank.py, 22 fixtures, against a baseline taken from main in the same worktree with the same harness: letter dx within 0.5 bp 53.21% → 93.12%; corpus dx 89.88% → 90.45%. No other fixture moves at all, and page counts stay 22/22 (verified by reading /Type /Page and the page-tree /Count out of the reference and candidate PDFs, not from the harness summary).

class-geometry is a live path dependency of render-pipeline (flashtex-class-geometry = { path = "../class-geometry" }, not vendored), so this PR reaches flashtex-render the moment it lands — unlike #316, which is behind the vendor/compiler pin.

Two corrections to what is being said about this fixture elsewhere, both measured on this machine:

  1. The 11 pt option is already applied, on main, without this PR. The rendered font_size for letter is 11439011 in bp_2pow20 = 10.9091 bp = 10.9500 TeX pt, both before and after this PR. The reference's SFRM1095 against our LMRoman10-Regular is a family difference — the fixture loads [T1]{fontenc} with no lmodern, so pdflatex gets cm-super's ecrm1095 while we use Latin Modern, which shares CM's metrics. It is not a size bug, and there is nothing to fix there.
  2. letter dy stays at 0.00% with this PR (median dy −165.62 bp, unchanged). That is expected and is not a defect in this PR — the vertical failure is the \address/\opening/\closing/\ps block geometry, which is compiler: implement the letter document class #316's parsing plus a render-pipeline change that does not exist yet (see my comment on compiler: implement the letter document class #316).

Nothing here asks you to change the PR. Evidence only, so it does not get re-derived by a third lane.

Implementation-Agent: Claude Opus 5 (lane nixos-pscalc-letter, linux-primary)

@GoKubar
GoKubar marked this pull request as ready for review September 14, 2026 22:31
@GoKubar

GoKubar commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Verified against current main and unblocking this.

Its CI red is stale, not real: the failing rust bridge jobs are from run 34811341115, cut before #334 fixed bridge/src/features.rs (parse_tokens gained a MathPackages argument). Several PRs carried that same phantom failure today; each one passed once rebased. I merged current main in and ran it here:

class-geometry    11 passed / 0 failed
bridge           170 passed / 0 failed
render-pipeline  358 passed / 0 failed

Measured effect, from the lane that evaluated it: fixtures/real-world/letter dx 53.21% → 93.12%, corpus dx 89.88% → 90.45%, and no other fixture moves. class-geometry is a live path dependency rather than a vendored one, so this reaches flashtex-render the moment it lands — unlike #316, which is compiler-side and therefore inert until the vendor pin moves.

One correction worth recording against the original premise for this work: the letter fixture's font difference is not a size bug. Rendered font_size is 10.95 TeX pt on plain main, before any fix — SFRM1095 vs LMRoman10-Regular is a family difference (cm-super EC vs Latin Modern, same Computer Modern metrics, different outlines). The 11 pt theory that had been attached to this fixture is wrong and there is nothing to fix there.

@GoKubar
GoKubar merged commit 6a47ed4 into main Sep 14, 2026
21 checks passed
GoKubar added a commit that referenced this pull request Sep 15, 2026
…ng skips, page-1 \@Texttop (#482)

* render-pipeline: re-pin vendor/compiler faa7d48 -> 6a47ed4, implement CleverReference, default math-class-override

The pin was 13 `crates/compiler` commits behind `main`, which left merged work
inert in the pipeline. The one this was asked for is #474: `\paragraph` and
`\subparagraph` are real compiler commands there, so `ps-calculus`'s four
`\paragraph is not supported by this compiler version` errors are gone through
the `render` producer. Nothing in the pipeline had to change for it -- the
run-in head was already laid out by `adapter::run_in_heading_at`, which reads
the command back from the source; the old pin's parser simply had no
`"paragraph"` arm. Also now visible: #472 (mathtools colon relations), #473
(preamble `\setlength`/`\addtolength`), #455, #454, #456, #457, #460, #416,
#424, #428, #444, #408.

One new compiler API surfaced: `parser::Inline::CleverReference` (#460,
cleveref). Three exhaustive matches in `adapter.rs` stopped building --
`inline_span`, the incremental paragraph-cache hash, and `items_from_inlines`.
It is implemented, not dropped behind a wildcard:

- `adapter::clever_reference_text` resolves `\cref`/`\Cref`/`\crefrange`/
  `\cpageref`/`\labelcref` to text in `lower_inline`, exactly where `Reference`
  already was, so `items_from_inlines` keeps its `unreachable!` instead of
  gaining a swallow-everything arm.
- `Labels` gains `kinds` (from `Inline::Label::kind`) and `cleveref` (from
  `Parsed::cleveref`) to feed it, and `Labels::needs_pages` now also answers
  true for a `\cpageref`, which otherwise had no page numbers to print.
- The grouping, consecutive-number ranges, `and`/`, and` joining and
  parenthesised equation numbers mirror the compiler's private
  `layout::clever_reference_text`. The naming is *not* duplicated:
  `xref::cleveref_name`/`cleveref_kind` are public and are called, so
  `\crefname` and the `capitalise`/`noabbrev` options stay compiler-owned.
- The one thing it cannot do is link. The compiler has no link backend, so
  `CleverReference::linked` is carried in the cache key and otherwise unused.

`tests/cleveref.rs` was written against this and marked
`#[ignore = "needs vendor re-pin past GH-CLEVEREF"]`. It is un-ignored and
passes unchanged: `Section 2` and `eqs. (1) and (2)`.

`math-class-override` becomes a default feature, as `amsmath-inline` and
`par-leading` did once their preconditions were met: the re-pinned `math.rs`
declares `pub class_override: Option<AtomClass>` and `pub enum AtomClass`.
Without it #472 is merged but inert here, because `typeset::class_override_of`
re-derives `\colon`'s class from source text that is identical with and without
amsmath loaded. `class_override_of` stays as the no-default-features fallback.

No other vendor directory had to move. `compiler`'s four path dependencies
(`font-engine`, `tex-text-encoding`, `tex-boxes`, `tex-expansion`) are
unchanged from the pin, and the three `tex-*` crates have had no commits at all
since `87a513de`. `math-layout` *is* 8 commits behind `main` (#186, #229), but
`compiler` does not depend on it, so that is a separate re-pin. The stale
opening paragraph of VENDORING.md (which still named `ea4ee5c8` as the
`compiler` pin and "~150 commits") is corrected.

Verified with `FLASHTEX_FONT_DIRS`/`FLASHTEX_TFM_DIRS` set to the bundled
`apps/mac/Fonts`:

- `cargo test --release --no-fail-fast -p flashtex-render-pipeline`:
  365 passed / 0 failed (364 baseline + the un-ignored cleveref test).
- amsmath oracle corpus: `TOTAL 59/59 within 0.5 bp`.
- `tabular_oracle: 106/106 fixtures match pdflatex`.
- Real-world corpus, all 22 fixtures: page counts checked by counting
  `/Type /Page` leaf objects in each `reference.pdf` and in each produced
  `render.pdf`, not by reading the harness summary -- 22/22 agree. Zero
  font-FAILURE diagnostics on either producer. HW1 and HW2: 3 pages, 0 errors.
- The `packages::supersede_message` regression the last re-pin hit did not
  return: every `... are recognised but not implemented` line in the report is
  `render 0 / compiler N`, so none leak through the pipeline.

Implementation-Agent: claude-opus-5 render-pipeline lane (agent-a98bd88c4a9d49a22)
Commit-Executor: claude-opus-5 render-pipeline lane, direct git (observed Cursor usage limit)
Co-authored-by: GoKubar <[email protected]>

* render-pipeline: re-pin vendor/compiler onto #316 so the pipeline can see CBlock::LetterBlock

Stacked on #477 (the re-pin to `main`). **Blocked on #316 merging**, and
deliberately so: this is a temporary pin to an unmerged branch, the second such
row in `vendor/VENDORING.md` after `vector-graphics`.

Why a pin to a branch at all: PR #316 adds `parser::Block::LetterBlock`, and
`adapter::inlines_of` matches `CBlock` exhaustively with no catch-all, so the
render-pipeline side of the `letter` class is a hard compile error against any
vendored compiler that lacks the variant. It cannot be written first and pinned
later. `engine/letter-class-compiler` is `main` + one commit (`15264d7b`,
rebased and green: 664 passed / 0 failed on `crates/compiler`), and
`vendor/compiler` here is a plain `git archive` of its `crates/compiler`.

**When #316 merges, re-pin this row to `main`** — `git archive <main>:crates/compiler`
and nothing else. The compiler row of `VENDORING.md` says so in the first
sentence.

Two exhaustive matches in `adapter.rs` needed the new variant: `inlines_of` and
the unit-building block walk. Neither gained a wildcard. `LetterBlock` carries
`Vec<Vec<Inline>>` rather than one flat slice, so it is lowered in
`lower_blocks` alongside `Verbatim`/`TitleBlock`/`VFill`: each line becomes an
ordinary paragraph in the nearest alignment the pipeline has today
(`flushright` for `\opening`'s return address, `flushleft` for the recipient
and the closing), with **one typed `unsupported_block` limitation per block**
naming exactly what is not applied — the `\longindentation` offset, the class's
own vertical skips, and the `\raggedleft` box whose lines share a left edge
where `flushright` aligns their right ones. No line of an `\opening` or a
`\closing` is dropped, and nothing about the class is silently approximated.

The typesetting PR stacked on this one replaces that arm and spends the skips
the compiler already resolved.

Verified with `FLASHTEX_FONT_DIRS`/`FLASHTEX_TFM_DIRS` set to the bundled
`apps/mac/Fonts`:

- `cargo test --release --no-fail-fast -p flashtex-render-pipeline`:
  365 passed / 0 failed.
- Real-world corpus, all 22 fixtures: page counts checked by counting
  `/Type /Page` leaf objects in each `reference.pdf` and in each produced
  `render.pdf` — 22/22 agree. Zero font-FAILURE diagnostics.
- `letter` compiles through the `render` producer with **0 errors**, where the
  #477 pin still reported seven `unknown_command`s (`\signature`, `\address`,
  `\opening`, `\closing`, `\encl`, `\cc`, `\ps`).

Implementation-Agent: claude-opus-5 render-pipeline/letter lane (agent-a98bd88c4a9d49a22)
Commit-Executor: claude-opus-5 direct git (observed Cursor usage limit)
Co-authored-by: GoKubar <[email protected]>

* render-pipeline: typeset letter.cls — class \parskip, \opening/\closing skips, page-1 \@Texttop

Stacked on the re-pin that gives the pipeline `CBlock::LetterBlock`, which in
turn is stacked on #477 and blocked on #316. This is the third and last link of
the `letter` chain: #297 gave the class its page frame, #316 gave the compiler
the class's commands, and this makes the pipeline put the result where pdfLaTeX
puts it.

`fixtures/real-world/letter` was **218 words at 0% vertical accuracy**, the
worst whole-fixture failure in the corpus. Landing the compiler alone did not
move it: the class's commands parsed, and the page was still 93 bp out with
**0 of 227 aligned words within 0.5 bp on the vertical axis**. Four separate
things were wrong, and they had to be fixed in this order because each one hid
the next.

## 1. `\parskip` is a class length, and the pipeline never read it

`Stylesheet::from_resolved` copied `\parindent`, `\topskip` and `\maxdepth` out
of `flashtex-class-geometry` but not `\parskip`, so every document kept
`Stylesheet::article`'s `0pt plus 1pt`. For article/report/book that is
identical to what the class says, which is why nobody noticed. letter.cls line
91 sets `0.7em` — 7.66498pt rigid at 11pt — so **every paragraph gap in every
letter was 7.6 bp short**, and the error accumulated down the page.

This is a no-op for every other class: `class-geometry`'s article `\parskip` is
`Glue::new("0pt", "1pt", "0pt")` and `document-style`'s `PARSKIP` is
`Skip::new(0.0, 1.0, 0.0)`. A document's own `\setlength\parskip` (#473) is
applied later in `adapt_cached` and still wins.

Effect alone: vertical agreement after removing a rigid page shift, 26.4% →
85.0%; max |dy| 186.5 → 67.6 bp.

## 2. `\opening`'s blocks are one paragraph each, not one paragraph per line

The compiler hands over `lines: Vec<Vec<Inline>>` with the class's own skips
already resolved into `gap_before_pt`, `gap_after_pt` and `extra_gap_after_pt`
— all multiples of that `\parskip`. `lower_blocks` now spends them instead of
dropping them, and joins each run of lines with `Inline::LineBreak` into **one**
paragraph. That distinction is the whole structure: a `\\` inside a paragraph
costs `\baselineskip`, a new paragraph costs `\baselineskip` *plus* `\parskip`,
and letter.cls sets the address and the recipient as single `\\`-separated
groups (a `tabular{l@{}}` and a `{\raggedright ...\par}`). The one mid-block
gap, `\\*[2\parskip]` between `\fromaddress` and `\@date`, does split the
paragraph, so the `\vspace` carrying it has the next paragraph's own `\parskip`
taken out of it.

Effect: max |dy| 67.6 → 41.9 bp — by then a *rigid* offset, the whole page
uniformly too high.

## 3. `{\raggedleft ...}` is a declaration, not a `flushright` environment

The recipient and the closing are now plain paragraphs rather than `Styled`
ones. `letter.cls` writes them as `\raggedright` *declarations*, which carry
none of `\trivlist`'s `\topsep`/`\partopsep` (9pt + 3pt at 11pt); a `Styled`
block does, and it put both blocks 12pt too low. They are short, unwrapped
lines, where `\raggedright` and justification set identical text, so this is
the right vertical answer and the same horizontal one.

The return address must stay right-aligned, so it stays `Styled(FlushRight)`,
and the `env_close` pass — which gives any non-`Plain` paragraph
`\@endparenv`'s `\@topsepadd` on the assumption that it came from an
environment — now skips the paragraphs `lower_blocks` made from a letter block.
They are keyed by their first inline's span, not the block's, because
`\address`'s text comes from the **preamble** and lies outside `\opening`'s own
span entirely.

## 4. letter.cls's page-1 `\@texttop`

Line 405:

    \def\@Texttop{\ifnum\c@page=1\vskip \z@ plus.00006fil\relax\fi}

On page 1 only, a fil glue sits at the top of the text block. Line 404's
`\raggedbottom` — unguarded, unlike article's `\if@twoside\else\raggedbottom\fi`,
so a `[twoside]` letter gets it too, which `Stylesheet::from_resolved` now also
models — puts `\@textbottom`'s `\vskip \z@ \@plus.0001fil` at the bottom. The
page's leftover space is shared in the ratio of their stretch, so the top takes
3/8 of it and the first baseline moves down by that much.

`crates/class-geometry`'s `letter_oracle` documents this exactly and is why it
checks its frame model on page 2 and only bounds page 1: the shift is page
building, not a frame length, so it belongs here. Without it every letter's
page 1 rode 41.95 bp too high on this fixture — a rigid offset that held *0%*
of the page's words outside 0.5 bp however exactly the spacing between them was
set.

Effect: max |dy| 41.9 → 4.3 bp.

## Where `letter` now is

| | dx ≤ 0.5 bp | dy ≤ 0.5 bp | dy ≤ 0.5 after a rigid shift | max \|dy\| |
|---|---|---|---|---|
| before #316 | — | — | — | the page had no letter structure at all |
| #316 + re-pin only | 93.39% | **0.00%** | 26.43% | 186.46 bp |
| this PR | 93.39% | 3.96% | **89.43%** | **4.29 bp** |

Measured with `tools/visual-oracle/rank.py` against the pinned
`fixtures/real-world/letter/reference.pdf`. Line by line the address block is
within 0.39 bp, the whole body (17 of the 26 lines) within 1.37 bp, and nothing
is worse than 4.29 bp.

## What is still wrong, measured rather than hidden

Three residuals, each diagnosed and none of them silent:

* **2.92 bp** at the boundary into `\closing`. Its argument goes in a
  `\parbox` whose first line is a `\strut` and whose height drives the
  interline glue TeX chooses; the pipeline has no `\parbox` box model to hang
  that on.
* **0.98 bp** at the boundary out of `\opening`'s return address, which
  pdfLaTeX sets in a `tabular` whose last row carries `\@arstrut`'s
  `0.3\baselineskip` depth where a text line carries its glyphs'.
* Those two make our content 2.91 bp taller than the reference's between the
  first and last baseline, which feeds back through the `\@texttop` share above
  and leaves the whole page a uniform ~1.4 bp low. That is why `dy ≤ 0.5` is
  3.96% and not ~100%: the structure is right and the total height is not. Fix
  either box model and the axis should snap.

Horizontally, the one remaining item is `\longindentation`: the closing and
signature sit at the left margin instead of 180pt in (max |dx| 179.328 bp is
exactly that length). `Block::Paragraph` has no per-paragraph left offset or
measure, so it cannot be expressed yet; it keeps its typed `unsupported_block`
limitation naming the exact offset, as does the `\raggedleft` box whose lines
share a *left* edge where `flushright` aligns their right ones.

One more difference this PR does not fix and the new test records: this page
carries a folio and the reference does not, because `\opening` runs
`\thispagestyle{empty}` whenever `\fromaddress` is non-empty (letter.cls
170-176). That is a page-style event the compiler does not emit yet.

`crates/render-pipeline/tests/letter_class_geometry.rs` pins all four fixes
against the reference's own baselines, with a per-boundary tolerance that is
the measured residual rather than a margin chosen to pass — tightening those
numbers is the next lane's gate.

Verified with `FLASHTEX_FONT_DIRS`/`FLASHTEX_TFM_DIRS` set to the bundled
`apps/mac/Fonts`:

- `cargo test --release --no-fail-fast -p flashtex-render-pipeline`:
  **370 passed / 0 failed** (365 + the 5 new letter tests).
- amsmath oracle corpus `TOTAL 59/59 within 0.5 bp`;
  `tabular_oracle: 106/106 fixtures match pdflatex`.
- Real-world corpus, all 22 fixtures: page counts checked by counting
  `/Type /Page` leaf objects in each `reference.pdf` and in each produced
  `render.pdf` — **22/22** agree. Zero font-FAILURE diagnostics on either
  producer. HW1 and HW2: 3 pages, 0 errors.
- Full `tools/visual-oracle/rank.py` sweep of the corpus in
  `docs/evidence/visual-oracle-letter-after/`: no fixture regressed, and the
  21 non-letter fixtures are untouched by construction — the `\parskip` read is
  a no-op for their classes and the other two changes are gated on
  `ClassKind::Letter`.

Implementation-Agent: claude-opus-5 render-pipeline/letter lane (agent-a98bd88c4a9d49a22)
Commit-Executor: claude-opus-5 direct git (observed Cursor usage limit)
Co-authored-by: GoKubar <[email protected]>

* vendor: re-pin compiler to main c95977d (now that #316's letter class has landed)
@GoKubar
GoKubar deleted the engine/letter-class-geometry branch September 16, 2026 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant