Skip to content

render-pipeline: the listings rendering engine on the NFSS font model - #285

Closed
GoKubar wants to merge 4 commits into
agent/kabir-claude/verbatim-microtype-frenchspacingfrom
agent/kabir-claude/listings-engine
Closed

GoKubar wants to merge 4 commits into
agent/kabir-claude/verbatim-microtype-frenchspacingfrom
agent/kabir-claude/listings-engine

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #262 (agent/kabir-claude/verbatim-listings-relanding, base b7a1e530), which already carries the merge of main where #248's sixteen listings fixtures and their TeX Live 2025 references landed. Head is 70dab5ba; the only substantive commit after b7a1e530 is 972ee828, and the diff against the base is crates/render-pipeline only.

Draft PR #145's 1623-line listings.rs was correctly not merged — it was written against a Role::Mono/MonoMetrics font model main has since replaced with NFSS. This takes #145's intent and #248's test material and writes the mechanism against the current model.

What it does

lstlisting and \lstinline set the way listings.sty sets them: the column algorithm, the vertical skips, the numbers gutter, the frames, tabsize/gobble, xleftmargin, breaklines, and the language styles.

  • src/listings.rs (new) — the source scan (\begin{lstlisting}[…], \lstset{…}), the option list, and the character machine that decides which output box every code character lands in. No metrics: it is a pure function of the source.
  • src/typeset.rs — Context::listing_blocks turns that plan into glyphs, kerns and rules once NFSS has selected the face.
  • src/adapter.rs — Block::Listing, found from the source bytes the way Block::Picture finds a tikzpicture; the compiler's flush-left lowering of the body is dropped.

Everything below is transcribed from TeX Live 2025's listings.sty 1.10b / lstmisc.sty and checked against pdflatex on this host. pdflatex is an oracle only; it is not in the product path.

A listing is not a \trivlist

\lst@Init is \par\penalty-50 \vspace\lst@aboveskip … \normalbaselines; \lst@DeInit is the mirror image. No \topsep, no \partopsep, no \@totalleftmargin. \vspace is the unstarred LaTeX one — a plain \vskip that does not \addvspace-merge — so two adjacent listings add both skips (27-lst-showstringspaces: 6 + 6 + 12 = 24.0 pt between them, not 18).

\normalbaselines runs before basicstyle, so a size-changing basicstyle sets \baselineskip for the listing: the above gap uses the listing's, the below gap the following text's.

One box per token, not per character

\lst@OutputToken sets \hbox to <N>\lst@width{\lst@lefthss c1 \hss c2 \hss … cN \lst@righthss} for a maximal run of letters or of "other" characters. \lst@width is fixed once at InitVars from basicstyle and fontadjust is false by default, so a keywordstyle/commentstyle switch moves glyphs inside a cell — [c] divides the slack the natural widths leave — but never the grid.

The "comma anomaly" is \lst@do@noligs

#145 recorded that a token immediately followed by a comma has its slack divided into N+2 parts instead of N+1, and could not explain it. It is \lst@SelectCharTable's \let\do\lst@do@noligs \verbatim@nolig@list, which rewrites every character of \@noligs as \lst@NoLig <old meaning>; \lst@NoLig is \advance\lst@length\m@ne \lst@Append\lst@nolig, so it appends a zero-width box to whatever token is currently open, before that character's own processing decides to flush it. Same \lst@length, one more item, one more \hss.

Measured on this host to confirm and bound it:

  • abcdefghijkl followed by each of ! " ( ) + . / : ; = ? [ ] * 0 Z keeps the N+1 divisor; only , and - — the two \@noligs characters that occur in the fixtures — change it.
  • abcdefghijkl , (a space between) keeps N+1: the space flushed the token first.
  • ab,,cd puts both commas in one two-cell box (, + \lst@nolig + ,, 4 \hss), at 146.8441 / 153.1205 bp against a measured 146.8439 / 153.1203.

All three are what the \lst@NoLig reading predicts and none is what "a following comma" alone predicts. The \lst@Delay/\lst@MergeToken hypothesis in the specification is refuted: those macros are not on this path.

listings suppresses ligatures unconditionally

#187 assumed a roman basicstyle keeps its ordinary ligatures because listings applies only \@noligs. It does not. New fixture 34-lst-roman-ligatures sets office fluffy affix waffle with no basicstyle at all: pdflatex emits o f f i c e as six separate CMR10 glyphs, no fi, no ffi. Every character is its own box here, which is why. \verb and \@verbatim keep Shaper::shape_with's \@noligs split, which is the right mechanism there; the two constructs are deliberately not unified.

tabsize, frames, xleftmargin

  • \lst@ProcessTabulator really does keep absolute tab stops, unlike \@verbatim (compiler: a verbatim tab is one space, not an 8-column stop #235): \lst@GotoTabStop splits at beginning-of-line into pure lost space, and elsewhere into a box one \lst@outputspace wide with the remainder inserted around it.
  • \lst@frameH's box is exactly framesep + framerule high, so the measured above-gap aboveskip + \lineskip + framesep + framerule + 0.7·bls falls out of the ordinary interline rule with no \prevdepth term — listings' own \ifdim\prevdepth<\@cclvi\p@ guard is inverted and never fires for a real previous depth, so the \advance\@tempdima\prevdepth inside it is dead code. The regime where framesep + framerule >= \baselineskip (no \vskip at all) is not modelled and is flagged in the source; no committed fixture is in it.
  • xleftmargin=2em is expanded inside the listing after basicstyle, so 30-lst-lstset-margin's 2em is 2 × cmtt8's quad. cmtt8's em is 8.5 pt, not 8.4 — ec-lmtt8's QUAD is 1.0625 by tftopl, matching.

The \lstset blocker, worked around in the pipeline

The compiler has no \lstset and drops lstlisting's option list, so the pipeline reads both from the source bytes — the arrangement #188 anticipated — and the adapter drops the inlines the compiler made of a \lstset argument, the same treatment Block::Picture gives a tikzpicture's body text. That unblocks 28-lstinline and 30-lst-lstset-margin without touching crates/compiler or the vendored mirror, and without waiting for a re-pin. No vendor/ or font file is touched.

Fixtures: 30 of 34 gated, 13 before

16–24, 26–31, 33, 34 join the thirteen verbatim fixtures already gated. Worst error 0.062 bp (28-lstinline), every other listings fixture ≤ 0.006 bp, zero font-substitution diagnostics. LISTINGS_NOT_YET is emptied and removed; 25-lst-java-roman moves into NOT_YET with a measured reason.

Two fixtures are new, with their own TeX Live 2025 references, for options #248's material left uncovered:

  • 33-lst-columns-lr — columns={[l]fixed} and columns={[r]fixed} (adjacent advance exactly W, first glyph at 0 and at W−w).
  • 34-lst-roman-ligatures — the ligature finding above.

25-lst-java-roman stays out for a reason that is not about listings: 228 of its 231 glyphs are exact, and the three that are not are OT1 slot lookups the pipeline's T1/EC addressing cannot reach. listings sets " as \char34 and the showstringspaces space as \verbvisiblespace, which under pdfTeX is \asciispace = \char32 of the current font; in a roman basicstyle those are cmr10's own slots, 0.500002 em (quotedblright) and 0.277779 em (suppress) by tftopl, against T1's quotedbl and visiblespace (0.5 em in ec-lmr10). They land 0.62, 1.10 and 0.62 bp out.

Gates, each against a git archive b7a1e530 control measured the same way

Gate Control This branch
crates/render-pipeline cargo test --release --no-fail-fast, bundled fonts exported 217 passed / 0 failed / 1 ignored 229 / 0 / 1 (+12 listings unit tests)
verbatim_oracle fixtures gated 13 of 32 30 of 34, worst 0.062 bp
amsmath corpus 59/59 within 0.5 bp 59/59 within 0.5 bp
Real-world corpus (--no-oracle --no-pixels), 22 fixtures × 2 producers 724 diagnostics, 346 errors, 5 overfull, 0 font substitutions identical: same status, page count and full diagnostic set for every fixture/producer
HW1 / HW2 render 3 pages, 0 errors, 0 overfull 3 pages, 0 errors, 0 overfull

listings-manual (new on main) exercises this change hardest: its display-list item count goes 286/233/278 → 662/752/238 as the two listings move onto the real cell grid in the typewriter face, and its page count, status and every one of its fifteen diagnostics are unchanged — the three overfull_hbox it already had stay at 9.82, 118.42 and 52.24 pt, and breaklines keeps the listing bodies from adding any.

Known gaps

  • breaklines inside \lstinline. \lstinline itself is exact (28-lstinline, 115 glyphs), but it still goes through the base's literal-text path, which has no break points. pdflatex breaks inside an inline listing when breaklines is on; that is the remaining 118.42 pt overfull in listings-manual. It needs a break-point item in adapter::Item and is deliberately left to a follow-up rather than bolted on at the end of this change.
  • Not exercised by any fixture, so not implemented or implemented untested: columns=spaceflexible, fontadjust, frameround, framex*margin, shadowbox (drawn as single), rulecolor/backgroundcolor, stepnumber/firstnumber/numberblanklines (implemented, untested), keepspaces/showspaces/showtabs (implemented, untested), breakatwhitespace (implemented, untested), prebreak/postbreak, emph/morekeywords/literate, escapechar/texcl/mathescape, caption/label/float, firstline/lastline, a listing that breaks across a page. Unmodelled keys are reported as an unsupported_block limitation rather than silently ignored.
  • The frame's per-line \vrules are emitted per line rather than merged into one rectangle; the oracle compares glyphs, not rules, but both framed fixtures' rules were checked by hand and agree with the committed rectangles to 0.001 bp once the segments are joined (21-lst-frame-single: 153.6936 → 208.2889, against 153.694 + 54.595).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu


Integration update — merged with #268 and #271, now 32 of 34

This PR now sits at the top of the lane's stack: #262 → #268 → #271 → #285, and its base has been repointed accordingly. The last commit merges #268 (\@verbatim's \trivlist inside a list) and #271 (microtype's verbatim patch, and \frenchspacing inside \verb), which this branch had forked below.

Everything below was re-measured by the integrating lane on the merged result, not taken on report.

32 of 34 fixtures match pdfLaTeX exactly, worst 0.006 bp. The 17 listings fixtures gated here, plus all 15 verbatim/\verb ones — 10-verbatim-itemize and 32-verbatim-microtype arrive with the merge.

NOT_YET is down to two, each with a measurement rather than a plan:

  • 15-verbatim-small — needs the compiler's style on Inline::Verbatim/Block::Verbatim (PR compiler: listings setup commands set nothing; verbatim carries the size declaration #261) plus a vendor re-pin. Verified by an uncommitted vendor swap: with it the horizontal geometry is exact and the block is left exactly 1.0 pt low, which \showoutput attributes to the block taking the body's 12 pt baselineskip instead of \small's 11 pt.
  • 25-lst-java-roman — 228 of 231 glyphs exact; the three that miss are OT1 slot lookups (\char34 = cmr10 quotedblright 0.500002 em, \char32 = suppress 0.277779 em) that the T1/EC font model cannot address.

One fixture got better from the merge than from either side alone

28-lstinline: 0.062 bp → 0.005 bp. #271 carries the fix for the interword space that was being dropped after an inline \verb/\lstinline — after_control_word stayed set even though prev_end had already advanced past the closing delimiter — and that space is in this fixture's first line. Worth noting because it was the one listings fixture an order of magnitude off the rest.

Gates on the merged result

Control is a git archive checkout of this branch's own base b7a1e530, built and measured identically with the bundled fonts exported.

gate control merged
crates/render-pipeline cargo test --release --no-fail-fast 217 / 0 / 1 229 / 0 / 1
amsmath oracle corpus — 59 / 59 within 0.5 bp
real-world corpus, 22 fixtures × 2 producers 739 diagnostics 724
real-world corpus status and page count — identical, every entry
real-world corpus font-substitution diagnostics 0 0
HW1 / HW2 render — 3 / 3 pages, 0 errors, 0 overfull
fixtures matching pdfLaTeX 30 of 34 32 of 34

The +12 are this branch's listings unit tests. The two corpus entries that differ from main are the ones #271 already documents (twelvept-plain and listings-manual, both losing unsupported_block limitations); the listings engine itself adds no corpus diagnostic in either direction, and listings-manual's three overfull_hbox stay at exactly 9.82 / 118.42 / 52.24 pt.

Relaying the note this branch could not deliver

The three specification corrections are received and stand as the record — \lst@do@noligs (not \lst@Delay/\lst@MergeToken) as the comma anomaly, \lst@breakProcessOther making ) end its token under breaklines, and \lst@frameInit's \prevdepth advance being dead code against \@cclvi\p@. 34-lst-roman-ligatures confirming unconditional ligature suppression with no basicstyle at all is the right way to have closed that one.

breaklines inside \lstinline is agreed as the next step and is the remaining 118.42 pt overfull in listings-manual: reduced to one paragraph with that fixture's preamble, pdflatex overfulls the same line by 113.38 pt without breaklines and by nothing at all with it.

GoKubar and others added 4 commits September 13, 2026 21:22
…le-material' into agent/kabir-claude/listings-engine

# Conflicts:
#	crates/render-pipeline/fixtures/verbatim/reference/28-lstinline.json
`lstlisting` and `\lstinline` set the way listings.sty sets them: the
column algorithm, the skips, the gutter, the frames, `tabsize`/`gobble`,
`xleftmargin`, `breaklines` and the language styles. Draft PR #145's
1623-line `listings.rs` was written against a `Role::Mono`/`MonoMetrics`
font model main has since replaced; this takes its intent and its test
material and writes the mechanism against NFSS.

`src/listings.rs` is the source scan and the character machine, all of it
transcribed from TeX Live 2025's listings.sty 1.10b / lstmisc.sty and
checked against pdflatex on this host:

* A listing is **not** a `\trivlist`. `\lst@Init` is `\par\penalty-50
  \vspace\lst@aboveskip ... \normalbaselines`, `\lst@DeInit` the mirror
  image; `\vspace` is the unstarred LaTeX one, so two adjacent listings
  add both skips (27-lst-showstringspaces: 6+6+12 = 24 pt).
* One `\hbox to <N>\lst@width` per *token*, not per character, filled
  `\lst@lefthss c1 \hss c2 ... cN \lst@righthss`. `\lst@width` is fixed
  once at `InitVars` from `basicstyle` and `fontadjust` is false, so a
  `keywordstyle`/`commentstyle` switch moves glyphs *inside* a cell
  (`[c]` divides the slack the natural widths leave) but never the grid.
* The "comma anomaly" #145 recorded and could not explain is
  `\lst@do@noligs`. `\lst@SelectCharTable` rewrites each character of
  `\verbatim@nolig@list` as `\lst@NoLig <old meaning>`, and `\lst@NoLig`
  appends a zero-width `\lst@nolig` to whatever token is *open* before
  the character's own processing flushes it: same `\lst@length`, one more
  item, so `[c]` divides by N+2. Measured here: of `! " ( ) + . / : ; = ?
  [ ] * 0 Z` after a 12-letter token only `,` and `-` change the divisor,
  and `ab,,cd` puts both commas in one two-cell box -- both are what the
  `\lst@NoLig` reading predicts.
* `\lst@ProcessTabulator` really does keep absolute tab stops, unlike
  `\@verbatim` (PR #235); `\lst@GotoTabStop` splits at beginning-of-line.
* `\lst@frameInit`'s box is `framesep + framerule` high, so the measured
  above-gap `aboveskip + \lineskip + framesep + framerule + 0.7bls` falls
  out of the ordinary interline rule with no `\prevdepth` term --
  listings' own `\ifdim\prevdepth<\@cclvi\p@` guard never fires.
* listings suppresses ligatures and kerns unconditionally, roman
  `basicstyle` included: every character is its own box here. `\verb`
  and `\@verbatim` keep `Shaper::shape_with`'s `\@noligs` split, which is
  the right mechanism *there*; the two are deliberately not unified.

The compiler has no `\lstset` and drops `lstlisting`'s option list, so
the pipeline reads both from the source bytes (the arrangement PR #188
anticipated) and the adapter drops the inlines the compiler made of a
`\lstset` argument -- the same treatment `Block::Picture` gives a
`tikzpicture`'s body text. That unblocks 28-lstinline and
30-lst-lstset-margin without touching `crates/compiler` or the vendored
mirror.

`verbatim_oracle`: 30 of 34 fixtures gated (13 before), worst 0.062 bp,
no substitution diagnostics. 33-lst-columns-lr and 34-lst-roman-ligatures
are new, with their own TeX Live 2025 references, for the `[l]`/`[r]`
alignments and for the ligature finding above (`office` sets as six
glyphs in a roman `basicstyle`). 25-lst-java-roman stays out: listings
sets `"` as `\char34` and the `showstringspaces` space as
`\verbvisiblespace` = `\asciispace` = `\char32` of the current font, and
in a roman `basicstyle` those are cmr10's own OT1 slots (0.500002 em and
0.277779 em by `tftopl`) which the pipeline's T1/EC addressing cannot
reach; 3 of its 231 glyphs land 0.62-1.10 bp out.

Implementation-Agent: Claude Opus 5 (listings lane, linux-primary / NixOS)
Commit-Executor: Claude Opus 5 via Claude Code (direct-agent Git execution
after the observed Cursor usage limit)
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
…ub.com:flash-tex/flashtex into agent/kabir-claude/listings-engine

# Conflicts:
#	crates/render-pipeline/fixtures/verbatim/reference/28-lstinline.json
The listings engine branched from PR #262 and so did not carry #268
(`\@verbatim`'s `\trivlist` inside a list) or #271 (microtype's verbatim
patch, and `\frenchspacing` inside `\verb`). Merging them makes the stack
linear -- #262 -> #268 -> #271 -> this -- and moves both of their fixtures
into `GATED` alongside the seventeen listings ones.

The only conflict is `tests/verbatim_oracle.rs`'s three lists, resolved by
taking every name both sides gate:

* `GATED` 32 = the 15 verbatim/`\verb` fixtures (`10-verbatim-itemize` and
  `32-verbatim-microtype` from this merge) plus the 17 listings ones.
* `NOT_YET` 2: `15-verbatim-small` keeps the `\showoutput` measurement from
  #271, `25-lst-java-roman` keeps the OT1-slot measurement from the listings
  lane.
* `LISTINGS_NOT_YET`, added in #262 so `main`'s freshly merged #248 fixtures
  were accounted for rather than failing the accounting test, is gone: every
  name in it is now either gated or in `NOT_YET`.

One fixture got better from the merge rather than from either side alone.
**`28-lstinline` goes from 0.062 bp to 0.005 bp**: #271 carries the fix for
the interword space that was being dropped after an inline `\verb` or
`\lstinline` (`after_control_word` stayed set even though `prev_end` had
already advanced past the closing delimiter), and that space is in this
fixture's first line.

Gates, control-compared against a `git archive` checkout of the listings
branch's own base `b7a1e530`, built and measured identically:

| gate | control | merged |
|---|---|---|
| `crates/render-pipeline` `cargo test --release --no-fail-fast` | 217 / 0 / 1 | 229 / 0 / 1 |
| amsmath oracle corpus | -- | 59/59 within 0.5 bp |
| real-world corpus, 22 fixtures x 2 producers | 739 diagnostics | 724 |
| real-world corpus status and page count | -- | identical, every entry |
| real-world corpus font-substitution diagnostics | 0 | 0 |
| HW1 / HW2 render | -- | 3 / 3 pages, 0 errors, 0 overfull |
| verbatim + listings fixtures matching pdfLaTeX | 30 of 34 | **32 of 34** |

The +12 are the listings lane's unit tests. The two corpus entries that
differ are the ones #271 already documents (`twelvept-plain` and
`listings-manual`, both losing `unsupported_block` limitations; the three
`overfull_hbox` in `listings-manual` are pdfLaTeX's own behaviour minus
`breaklines` inside `\lstinline`). The listings engine itself adds no corpus
diagnostic in either direction.

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
@d-q222

d-q222 commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Superseded — reimplemented independently and landed via #453 (64cb851b) and #543 (591967fd).

Verified on origin/main: crates/render-pipeline/src/listings.rs (1507 lines), fixtures 16-31, and tests/listings_keys.rs. The deliverable — listings on NFSS — is present.


Closed by the Commander after a stale-draft screening pass that required evidence in both directions — a landing SHA to close, an empty grep to keep open. If this verdict is wrong, say so on issue #2 and I will reopen: an asymmetric bar has produced wrong calls here in both directions before.

@d-q222 d-q222 closed this Sep 16, 2026
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.

2 participants