Skip to content

render-pipeline: literal text, and \verb/verbatim in the typewriter face NFSS selects - #187

Closed
GoKubar wants to merge 2 commits into
mainfrom
agent/kabir-claude/literal-verbatim-nfss
Closed

GoKubar wants to merge 2 commits into
mainfrom
agent/kabir-claude/literal-verbatim-nfss

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Based on main 89e49fed, not stacked. crates/render-pipeline only; no vendor edits, no compiler changes.

Companion PRs: #183 (the typewriter fonts this needs to measure against) and agent/kabir-claude/lstinline-compiler (the \lstinline half).

What was wrong

\verb and a verbatim body were lowered to ordinary body-face text with an unsupported_block limitation saying so ("the pipeline has no monospaced face or literal-text block"). The compiler had already tagged them TextFamily::Mono, but the pipeline re-derives TextStyle.family from a byte scan of the source for \ttfamily/\texttt, and \verb|x|'s bytes carry no such command — so the tag was dropped, the text came out roman, -- became an en dash and `` an opening quote.

Relationship to draft PR #145

This is #145's literal flag, rebuilt rather than merged. I confirmed the integrator's reasoning before starting: #145's base 85ec30c7 has neither nfss.rs nor toc.rs, and its head carries no nfss.rs at all — it has its own Role::Mono + MonoMetrics font-selection model (fonts.rs:329, :849, :888). Merging it would put two fields that can disagree about one fact into the same struct and would revert main's contents lists. Only #145's test material is reused. The ideas are taken; the mechanism is new.

The model

TextStyle gains literal: bool, merged by OR like the other flags and hashed by the incremental cache. Family selection is unchanged: the compiler's TextFamily::Mono is now routed to nfss::FamilyKind::Tt, and everything downstream — .fd shape tables, design-size boundaries, ectt/ec-lmtt metrics, lmmono* outlines — is main's existing NFSS path. There is no second font model.

literal changes three things, and deliberately only three:

rule why
Ligatures Shaper::shape_with splits the ligature/kern run before each of ` ' , - < > exactly where latex.ltx's \@noligs puts its \kern\z@
Spaces a space or tab becomes Item::LiteralSpace — an empty box one interword space wide \@xobeysp = \leavevmode\nobreak\ ; glue after a break is discarded (tex.web §879) and would swallow a verbatim line's indentation, and T1 slot 32 of ectt is the visible space, not a blank
Hyphenation never, independently of family a roman basicstyle listing must not acquire hyphens its source does not have

Protrusion and expansion need no new rule: microtype's default sets are rm*/sf*.

Where this differs from #145 on purpose: #145 shaped literal text one character at a time. Splitting only at \@noligs's six characters is what LaTeX actually does, and it keeps the ordinary ligatures of a listing set in a roman basicstyle — the exact case #145 documents as its one unexplained mismatch (25-lst-java-roman).

Two source-reading bugs found and fixed

  • \verb|x y|Z came out 3.32 pt too wide — one cmr10 interword space. The compiler's span for a \verb is the control word alone, so |x y| was read as the gap before the next token and its space typeset. literal_command_body now parses the delimited body from the source and prev_end advances past the closing delimiter. Measured against pdfTeX: Z at 175.190 vs 175.189 after, 178.510 before.
  • Verbatim tabs were eight columns wide. The line text was taken from the compiler's verbatim_display, a display string that expands a tab to an 8-column stop and renders \verb*'s space as U+00B7. LaTeX does neither — \@verbatim sets a tab as one ordinary space, which is why <tab>one starts one typewriter width from the margin in pdfTeX and not eight. Reading the source bytes instead fixed fixtures 03, 11 and 12 outright.

Oracle

fixtures/verbatim/ is 17 fixtures with pdfLaTeX MacTeX 2026 references taken from #145 — read-only, never regenerated: this host has TeX Live 2025. (Spot-checked: TL 2025 reproduces fixture 07's reference to 0.001 bp, so the two distributions agree on this material.) tests/verbatim_oracle.rs matches glyph origins within 0.1 bp, requires equal glyph and page counts, and fails on any font_unavailable / required_metrics_unavailable / ec_metrics_unavailable / font_outline_substituted diagnostic.

10 of 17 match pdfLaTeX exactly, worst 0.006 bp:

PASS 01-verbatim-basic         194 glyphs, worst 0.005 bp
PASS 02-verbatim-ligatures     164 glyphs, worst 0.005 bp
PASS 03-verbatim-tabs          172 glyphs, worst 0.005 bp
PASS 04-verbatim-blank-lines   208 glyphs, worst 0.005 bp
PASS 07-verbatim-t1            205 glyphs, worst 0.005 bp
PASS 08-verbatim-lmodern       205 glyphs, worst 0.005 bp
PASS 09-verbatim-vmode         258 glyphs, worst 0.005 bp
PASS 11-verbatim-11pt          174 glyphs, worst 0.006 bp
PASS 12-verbatim-12pt          174 glyphs, worst 0.006 bp
PASS 14-verbatim-pagebreak    1587 glyphs, worst 0.005 bp

The other 7 are committed with their references and each listed in NOT_YET with the reason it does not match; a second test asserts every fixture on disk is either gated or listed, so none can be added and quietly left unmeasured.

The test needs the typewriter faces from #183. Until that lands it prints a SKIP naming the dependency rather than measuring against a substitution. The numbers above were produced by staging those files in temporarily.

Gates — control-compared, both font modes

A git archive control checkout of the shared base 89e49fed was measured the same way, because a global FLASHTEX_TFM_DIRS breaks several tests on main that deliberately build a partial-font control (FontSet::new reads the environment, so e.g. missing_required_metrics_are_a_blocking_diagnostic_not_a_silent_fallback stops failing and mathbb_paints_from_new_computer_modern_when_bundled gets NewCM back). cargo test --release --no-fail-fast:

checkout no FLASHTEX_* with fonts configured
control 89e49fed 170 / 0 / 1 162 / 8 / 1
this branch 172 / 0 / 1 164 / 8 / 1

The failing sets are byte-identical (diff of the sorted names): those 8 are pre-existing and environment-sensitive, not regressions. The two added tests pass in both modes because they never read FLASHTEX_* — they build their FontSet with FontSet::with_dirs. That is the durable fix for the 8 as well.

Other gates:

  • amsmath oracle corpus: 59/59 within 0.5 bp.
  • real-world corpus (10 fixtures): 204 diagnostics, 0 font-substitution, every fixture's status and page count identical to the control. HW1 3 pages, HW2 3 pages.

Deliberately not rebuilt from #145

  • The listings engine. listings.rs is 1623 lines reimplementing listings.sty/lstmisc.sty/lstlang1.sty — the key model, \lst@FillFixed/\lst@CalcLostSpaceAndOutput, four language tables, frames, numbers, breaklines. Rebuilding it faithfully is a body of work in its own right, and none of it can disagree with NFSS until it exists. What it needs underneath is literal plus typewriter selection, which is what this lands. Not attempted rather than half-attempted.
  • verbatim* / \verb* visible spaces (fixtures 05, 06). pdfTeX sets each space as \char32 of the T1 typewriter font. The bundled OpenType Latin Modern has no visiblespace glyph — pdfTeX draws it from the Type 1 ec font — so this needs a shaper special case and a glyph to draw. LiteralBody.starred is parsed and kept for it.
  • The verbatim environment's \trivlist geometry (fixtures 10, 13, 15): \leftskip\@totalleftmargin inside a list, the overfull box of a too-long line, and the size declaration in force. Verbatim is still lowered to a flush-left paragraph with forced breaks; a real verbatim block is the natural next step and is what render-pipeline: verbatim, verb and listings as LaTeX sets them #145 built.

Resume notes

  1. Merge fonts: bundle the typewriter metrics and outlines \texttt needs #183 first, or this test skips.
  2. Next highest-value step: a pipeline-owned verbatim block (its own lines at exact baselines, \trivlist glue, \null empty lines) — that is fixtures 10, 13 and 15 and is the prerequisite for display listings.
  3. Then the visible space, then the listings engine on top of literal.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

GoKubar and others added 2 commits September 13, 2026 13:50
…ace NFSS selects

Before this, `\verb` and a `verbatim` body were lowered to ordinary
body-face text with an `unsupported_block` limitation saying so ("the
pipeline has no monospaced face or literal-text block"). The compiler had
already tagged them `TextFamily::Mono`, but the pipeline re-derives
`TextStyle.family` from a byte scan of the source for `\ttfamily`/`\texttt`,
and `\verb|x|`'s bytes carry no such command -- so the tag was dropped and
the text came out roman, with `--` turned into an en dash and ``` `` ```
into an opening quote.

This is the `literal` flag of draft PR #145, rebuilt on main's NFSS model
rather than merged. #145 predates `nfss.rs` (its base, 85ec30c, has neither
`nfss.rs` nor `toc.rs`) and carries its own `Role::Mono` + `MonoMetrics`
font-selection model; merging it would put two fields that can disagree
about one fact into the same struct and would revert main's contents lists.
Only its *test material* is reused here. What is taken is the idea; the
mechanism is new.

## The model

`TextStyle` gains `literal: bool`, merged by OR like the other flags and
hashed by the incremental cache. Family selection is unchanged: the
compiler's `TextFamily::Mono` is now routed to `nfss::FamilyKind::Tt` and
everything downstream -- `.fd` shape tables, design-size boundaries,
`ectt`/`ec-lmtt` metrics, `lmmono*` outlines -- is main's existing NFSS
path. There is no second font model.

`literal` changes three things, and deliberately only three:

* **Ligatures.** `Shaper::shape_with(.., literal)` splits the ligature/kern
  run before each of `` ` `` `'` `,` `-` `<` `>`. That is exactly where
  latex.ltx's `\@noligs` puts its `\kern\z@`, so `--` stays two hyphens and
  ``` `` ``` two grave accents. #145 shaped literal text one character at a
  time; splitting only at `\@noligs`'s six characters is what LaTeX actually
  does, and it keeps the ordinary ligatures of a listing set in a roman
  `basicstyle` -- the exact case #145 documents as its one mismatch
  (`25-lst-java-roman`).
* **Spaces.** A space or tab in literal text becomes `Item::LiteralSpace`,
  latex.ltx's `\@xobeysp` = `\leavevmode\nobreak\ `: an empty box one
  interword space wide. It cannot be interword glue, because glue after a
  line break is discarded (tex.web 879) and a verbatim line's indentation
  would vanish with it; and the space must not be *shaped*, because T1 slot
  32 of `ectt` is the visible space, not a blank.
* **Hyphenation.** Never, independently of the family (a roman `basicstyle`
  listing must not acquire hyphens its source does not have). Protrusion and
  expansion need no new rule: microtype's default sets are `rm*`/`sf*`.

Two source-reading fixes fall out:

* `literal_command_body` parses the delimited body of
  `\verb`/`\verb*`/`\lstinline` from the source. The compiler's span is the
  control word alone, so `|x y|` was being read as the *gap* before the next
  token and its space typeset -- `\verb|x y|Z` came out 3.32 pt (one cmr10
  space) too wide. `prev_end` now advances past the closing delimiter.
* Verbatim line text and `\verb` bodies are read from the source bytes, not
  from the compiler's `verbatim_display` string, which expands a tab to an
  8-column stop and renders `\verb*`'s space as U+00B7. LaTeX does neither:
  `\@verbatim` sets a tab as one ordinary space, which is why `<tab>one`
  starts one typewriter width from the margin in pdfTeX and not eight. That
  fixed fixtures 03, 11 and 12 outright. (A compiler-side fix would be
  better still, but this crate builds against a pinned vendor mirror.)

## Oracle

`fixtures/verbatim/` is 17 fixtures with pdfLaTeX **MacTeX 2026** references
taken from #145 -- read-only, never regenerated: this host has TeX Live
2025. `tests/verbatim_oracle.rs` matches glyph origins within **0.1 bp**,
requires equal glyph and page counts, and fails on any
`font_unavailable` / `required_metrics_unavailable` / `ec_metrics_unavailable`
/ `font_outline_substituted` diagnostic.

**10 of 17 match pdfLaTeX exactly**, worst 0.006 bp: 01-basic, 02-ligatures,
03-tabs, 04-blank-lines, 07-T1, 08-lmodern, 09-vmode, 11-11pt, 12-12pt,
14-pagebreak (1587 glyphs across a page break). The other 7 are committed
with their references and each listed in `NOT_YET` with the reason it does
not match yet; a second test asserts every fixture on disk is either gated
or listed, so none can be added and quietly left unmeasured.

The test needs the typewriter faces, which arrive with the bundled-fonts PR
(#183). Until then it prints a SKIP naming that dependency rather than
measuring against a substitution. Verified by staging those files in
temporarily: 2 passed, 0 failed, the 10 fixtures listed above.

## Gates

* crates/render-pipeline `cargo test --release`, no FLASHTEX_* exports:
  172 passed, 0 failed, 1 ignored (main: 170/0/1; the two new ones are this
  oracle).
* amsmath oracle corpus: 59/59 within 0.5 bp.
* real-world corpus (10 fixtures): 204 diagnostics, 0 font-substitution,
  every fixture's status and page count identical to main. HW1 3 pages,
  HW2 3 pages.

## Not rebuilt from #145, and why

**The listings engine.** #145's `listings.rs` is 1623 lines reimplementing
listings.sty/lstmisc.sty/lstlang1.sty: the key model, the
`\lst@FillFixed`/`\lst@CalcLostSpaceAndOutput` column algorithm, four
language tables, frames, numbers, breaklines. Rebuilding it faithfully is a
body of work in its own right, and none of it can disagree with NFSS until
it exists -- the substrate it needs is `literal` plus typewriter selection,
which is what this change lands. It is not attempted here rather than
half-attempted; the `\lstinline` half is the companion compiler PR.

**`verbatim*` / `\verb*` visible spaces** (fixtures 05, 06). pdfTeX sets
each space as `\char32` of the T1 typewriter font, the `visiblespace` glyph.
The bundled OpenType Latin Modern has no such glyph -- pdfTeX draws it from
the Type 1 ec font -- so this needs a shaper special case *and* a glyph to
draw. `LiteralBody.starred` is parsed and kept for it.

**The `verbatim` environment's `\trivlist` geometry** (fixtures 10, 13, 15):
`\leftskip\@totalleftmargin` inside a list, the overfull box of a too-long
line, and the size declaration in force. Verbatim is still lowered to a
flush-left paragraph with forced breaks; a real verbatim block, which is
what those need, is the natural next step and is what #145 built.

Implementation-Agent: engine-engineer (linux-primary, NixOS PC)
Commit-Executor: engine-engineer via git (Cursor usage limit fallback)
Co-authored-by: GoKubar <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
The compiler's span for a `\verb` is the control word alone on the path
that reaches `Inline::Verbatim` (measured: `A \verb|x y|Z` gives span
68..73, the five bytes of `\verb`), while the lexer's own `TokenKind::Verb`
span covers the delimiters too. Matching the command name at `span.start`
rather than requiring `span.end` to be the end of the control word reads
the same delimited body under either convention, so a vendor re-pin cannot
silently change which bytes are treated as the body.

crates/render-pipeline `cargo test --release --test verbatim_oracle`:
2 passed, 0 failed; the 10 gated fixtures still match pdfLaTeX within
0.006 bp.

Implementation-Agent: engine-engineer (linux-primary, NixOS PC)
Commit-Executor: engine-engineer via git (Cursor usage limit fallback)
Co-authored-by: GoKubar <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
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