Repository navigation
render-pipeline + paragraph-layout: font expansion and protrusion inside inline math (microtype) - #175
render-pipeline + paragraph-layout: font expansion and protrusion inside inline math (microtype)#175GoKubar wants to merge 2 commits into
Conversation
Resume notes (paused again for machine handoff, 2026-09-13)Branch Scope vs #148 / #157 / #177's lecture-notes overfull
Test harness fix (this commit)The old test grouped our runs within 6bp but pdflatex rows within 3bp (
Measured (scratch vendor = this branch's paragraph-layout, reverted)
Next steps
|
…croItem::math)
pdfTeX does not treat an inline formula as an opaque box when it breaks a
paragraph. `mlist_to_hlist` leaves char nodes, kerns and glue between
`\mathon` and `\mathoff`, and `line_break`'s character loop, `hpack`'s
`cal_expand_ratio`/`subst_ex_font` and `cp_skipable` all walk into them
(verified against pdftex.web and `\showbox`: a formula's `\fam0` chars print
as `\OT1/cmr/m/n/10.95 (+20) (`, a formula-final `3` gets
`\kern-0.27376 (right margin)`, a line-initial `(` gets `\kern-1.28116`).
`MicroItem` gains `math: Option<MicroMath>`: the formula's top-level nodes
(`MathNode::{Char{params,code,width}, Kern{width,normal}, Glue{width,stretch,
shrink}, Box{width}}`, all in sp) beside the box that already carried it.
- `MtCtx` counts their width and their glue and font stretch/shrink.
- The protrusion search walks into the formula, so its first and last
character protrude like any other.
- The packer sets the math glue, expands the chars, and returns one
`PositionedGlyph` per node with its x offset inside the formula.
- A node list that does not add up to the box width (within 0.001pt) is
ignored and the formula stays one box: a caller that cannot describe a
formula exactly never moves it.
- Which characters expand is the caller's decision (`params`), matching
microtype.cfg's `expansion=alltext-nott`: OT1/cmr digits and
`( ) [ ] + = ; : !` do, OML/OMS/OMX glyphs and anything math packs into a
box (scripts, fractions, `\sum`, `\sin`, `\left..\right`, `\text`) do not.
`layout_paragraph` (the non-microtype path) is untouched.
`cargo test --release`: 29 + 12 + 24 + ... all passed, 0 failed across the 15
test binaries, including the 32-fixture `microtype_oracle` and the new
`tests/microtype_math.rs` (3 passed).
Implementation-Agent: claude-opus-5 (kabir-claude lane, linux-primary)
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
…er microtype
Under `\usepackage{microtype}` pdfTeX expands the character nodes of an
inline formula with the rest of the line and lets a formula's first or last
character protrude into the margin. We handed the microtype line breaker one
opaque box per formula, so every glyph after the first formula on a line sat
up to 1.7pt away from pdflatex's.
`math_top_nodes` flattens math-layout's root into paragraph-layout's
`MathNode`s. It opens the atom hboxes that are siblings in TeX's list
(`[char, italic kern]`, `[nucleus, shifted script]`) and leaves every other
hbox a box, so scripts, fractions, `\sum`, `\sin` and `\left..\right` stay
unexpanded exactly as pdfTeX has them. `MathRec.shape` (`List`, `Kerned`,
`Opaque`) records where the top-level hlist lives; a formula holding a
top-level grid stays opaque. Top-level text-size roman characters get
`OT1/{cmr|lmr}/m/n` microtype parameters resolved from the roman TFM, which
is what microtype.cfg's `expansion=alltext-nott` selects. The painter then
moves the top-level nodes to the positions the packer chose.
New oracle `fixtures/microtype-math` (5 documents, 15 justified paragraphs,
`generate.py`; MacTeX pdfTeX 1.40.29) and `tests/microtype_math.rs`.
Measured on this machine against the committed expectations, same fonts and
metrics on both sides:
| fixture | main `1222354e` | with this commit |
|---|---|---|
| 01-digits-parens | 15/15 lines, **54** pen positions > 0.1pt, worst 1.678pt | 15/15, **0**, worst 0.026pt |
| 02-letters-relations | 15/15 lines, **38** > 0.1pt | 15/15, **0**, worst 0.066pt |
| 04-line-edges | **line breaks differ** | **17/17 lines**, 0 > 0.1pt, worst 0.049pt |
03 and 05 are `#[ignore]`d with their measured causes: `\sum` is 1.003pt
narrow (cmex10 at its 10pt design size, not the math size) and `\cdots`
5.475pt narrow (three `\cdotp`s, not `\mathinner`) — both PR #177; and the
compiler drops `\mathbb{Z}` and sets `\colon` literally — the compiler lane.
Neither is font expansion. Every other construct in those fixtures (`\frac`,
`\sqrt`, scripts, `+`, `=`, `\quad`, `\mid`, `\max`, `!`) is within 0.12pt.
Inline math's `\medmuskip`/`\thickmuskip` stretch and the Bin/Rel breakpoints
are `a2f85bfa`/`2c373747` on main, not this commit; this only adds expansion
and protrusion on top of them.
Gates (this machine; `apps/mac/Fonts` + a dereferenced TeX Live 2025 metric
tree whose four digest-bound TFMs are byte-identical to MacTeX's):
- render-pipeline `cargo test --release`: no suite differs from main except
`microtype_math` (main 0/5, here 3 passed + 2 ignored). `inline_math_breaks`
still 39/46 with no overfull line; `microtype_fixtures` 8/8.
- paragraph-layout `cargo test --release`: all green.
- compiler `cargo test --release`: 395 passed, 0 failed (same as main).
- amsmath corpus (`oracle.py check`): **59/59** within 0.5bp, same as main.
- Real-world corpus, all 10 fixtures with a reference PDF: line-for-line
identical to main, 0 overfull each. HW1 and HW2: 3 pages, 0 overfull,
identical to main. None of them loads microtype, so this is a no-change
gate, as intended.
Nothing under `vendor/` is committed; the gates above ran with an
uncommitted scratch copy of this branch's `crates/paragraph-layout` in
`vendor/paragraph-layout`, reverted afterwards. `vendor/paragraph-layout`
must be re-pinned to the commit before this one for the pipeline to build
standalone.
Implementation-Agent: claude-opus-5 (kabir-claude lane, linux-primary)
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
d6c62d0 to
df1bbae
Compare
Resume notes — 2026-09-13, moved to the NixOS PC (
|
| fixture | main |
this branch |
|---|---|---|
| 01-digits-parens | 15/15 lines, 54 pen positions > 0.1pt, worst 1.678pt | 15/15, 0 over 0.1pt, worst 0.026pt |
| 02-letters-relations | 15/15 lines, 38 > 0.1pt | 15/15, 0 over 0.1pt, worst 0.066pt |
| 04-line-edges | line breaks differ | 17/17 lines identical, 0 over 0.1pt, worst 0.049pt |
| 03-scripts-fractions | 13/13 lines, 30 > 0.1pt | 13/13, 13 > 0.1pt — #[ignore], blocked on #177 |
| 05-operators-functions | line breaks differ | line breaks differ — #[ignore], blocked on the compiler \colon/\mathbb gap |
03 and 05 are out-of-lane, with the causes measured rather than assumed (\showthe\wd, calibrated on $a_i$, eight control formulas agreeing to ≤0.0003pt): \sum is 1.003pt narrow because it is cmex10 at its 10pt design size instead of 10.95pt (11.55836 − 10.55560 = 1.00276pt), \cdots is 5.475pt narrow = exactly 3 × \thinmuskip, and \sin/\log are 1.899pt / 2.055pt narrow. Both fixtures stay in the repo with the blocking work named in the #[ignore] reason.
Gates (all on this machine)
- amsmath corpus 59/59 within 0.5bp — same as
main. ✅ - Real-world corpus, all 10 fixtures with a reference PDF: line-for-line identical to
main, 0 overfull each. ✅ - HW1 / HW2: 3 pages, 0 overfull, identical to
main. ✅ - render-pipeline
cargo test --release: no suite differs frommainexceptmicrotype_math(main0/5 → 3 passed, 2 ignored). paragraph-layout 99/99. compiler 395/395. - Pre-existing and identical on
main, so not from this branch:font_families_oracle0/1,math_symbols6/10,metrics_provenance4/5.
Next steps
- Re-pin
vendor/paragraph-layoutto0edd200c(integrator) so the pipeline builds standalone. - Optionally split into PR A (
0edd200c) and PR B (df1bbae6); both commits are already one crate each. - Drop the two
#[ignore]s as render-pipeline: math at the current text size, amsmath cmex designs, \dots/\bmod/\pmod spacing, one-letter \mathrm (needs #172 + #174 re-pin) #177 and the compiler\colon/\mathbbwork land — each is a direct acceptance check for that work. - Re-run the three passing fixtures on the Mac against MacTeX to confirm they hold there too.
NixOS trap worth recording
The rooted TFM reader refuses symlinked path components, and every directory in a NixOS texmf-dist store path is a symlink, so pointing FLASHTEX_TFM_DIRS at the host tree gives Refused(SymlinkComponent { component: "lm" }) → required_metrics_unavailable → math laid out with the OpenType MATH table, which silently changes every line break and makes the whole fixture set look broken. A dereferenced copy plus doc/fonts/lm/GUST-FONT-LICENSE.TXT fixes it; apps/mac/Fonts/texmf in the repo is the other usable tree. amsmath_corpus/oracle.py forces FLASHTEX_TFM_DIRS to its --fonts argument, so it needs a flat directory with the OTFs, the TFMs and the licence together. Also: pdftotext is not installed here, so fixtures/microtype-math/generate.py cannot run on this machine — no committed expectation was regenerated.
🤖 Generated with Claude Code
|
test |
|
Correction to the "test" note above: this PR was closed under the old-engine freeze (DESIGN.md D13). It is old-path feature or hand-port work, and the old engine retires at the P5 switch-over. The branch is kept; reopen it if it turns out to be a critical user-facing fix. — kabir-claude (Commander), nightly review 2026-09-30 |
Font expansion and character protrusion for the glyphs of an inline formula under
\usepackage{microtype}.Rebased onto current
main1222354eand rescoped. Branchagent/kabir-claude/math-inline-expansion@df1bbae6, two commits, nothing undervendor/committed. Resumed on the NixOS PC; see "Environment" below for what that changes about the evidence.Scope, after main moved
mainnow carries the Mac lane's2c373747(paragraphs break inside inline math after Bin/Rel atoms) anda2f85bfa(inline math's\medmuskip/\thickmuskipstretch with the line, plusfixtures/real-world/inline-mathandtests/inline_math_breaks.rs). That is the math-glue half of what this PR used to claim. It is no longer in scope here and this branch does not touch it —inline_math_breaksstill reports 39/46 with no overfull line on top of this change.What is left, and all this PR is now about:
\fam0(OT1/cmr) digits and( ) [ ] + = ; : !inside a formula take the line's expansion ratio, exactly as\showboxshows (\mathon \OT1/cmr/m/n/10.95 (+20) ( …). OML/OMS/OMX glyphs and anything math packs into a box — scripts, fractions,\sum,\sin,\left..\right,\text— do not, which is what microtype.cfg'sexpansion=alltext-nottselects.3gets\kern-0.27376 (right margin), a line-initial(gets\kern-1.28116 (left margin); a math node of width 0 and a normal kern arecp_skipable.Explicitly not in this PR, and narrowed out of it: inline math stretch/shrink in the plain (non-microtype) breaker and the lecture-notes overfull follow-up (the Mac owns these; landed as
a2f85bfa), and the compiler\colon/\mathbbgaps (the Mac owns these too). This does not duplicate #148 or #157: those add stretch/shrink to math-layout's glue and use it for displays; neither changes inline math in a paragraph.Commits
0edd200cparagraph-layout —MicroItem.math: Option<MicroMath>carries the formula's top-level nodes (MathNode::{Char{params,code,width}, Kern{width,normal}, Glue{width,stretch,shrink}, Box{width}}, all in sp) beside the box that already held it.MtCtxcounts their width and their glue and font stretch/shrink; the protrusion search walks into the formula; the packer sets the math glue, expands the chars and returns onePositionedGlyphper node. A node list that does not add up to the box width (within 0.001pt) is ignored and the formula stays one box.layout_paragraph(the non-microtype path) is untouched.df1bbae6render-pipeline —math_top_nodesflattens math-layout's root into those nodes, opening the atom hboxes that are siblings in TeX's list ([char, italic kern],[nucleus, shifted script]) and leaving every other hbox a box.MathRec.shape(List/Kerned/Opaque) records where the top-level hlist lives. Top-level text-size roman chars getOT1/{cmr|lmr}/m/nparameters from the roman TFM. The painter moves the nodes to the packed positions. New oraclefixtures/microtype-math+tests/microtype_math.rs.vendor/paragraph-layoutmust be re-pinned to0edd200cforcrates/render-pipelineto build standalone. Everything below ran with an uncommitted scratch copy of this branch'scrates/paragraph-layoutinvendor/, reverted afterwards.Measured: this branch vs
main, same machine, same fonts and metricsThe oracle is
fixtures/microtype-math/*.expected(MacTeX pdfTeX 1.40.29, committed, not regenerated).main1222354e#[ignore], see below#[ignore], see below04 is the protrusion case:
maincannot break those lines as pdflatex at all, this branch does, to 0.05pt.The two
#[ignore]d fixtures, with measured causesBoth are natural-width gaps that exist before any expansion applies. Measured against
\showthe\wdof the same formulas (pdfTeX 1.40.27 here), with a constant calibrated on$a_i$and cross-checked on eight formulas that agree to ≤0.0003pt:03 → PR #177.
\sumis 1.003pt narrow, identically in$\sum a_i$,$\sum_{i=1}^n i=\frac{n(n+1)}{2}$and the fixture. pdflatex's\sumis 11.55836pt = 1.05556 em of cmex10 at 10.95pt; ours is 10.5556pt — the same glyph at cmex10's 10pt design size. 11.55836 − 10.55560 = 1.00276pt, and the measured gap is 1.003pt.\cdotsis 5.475pt narrow alone (exactly 3 ×\thinmuskipat 10.95pt) and 3.649pt narrow inside$a_1+a_2+\cdots+a_n$: three\cdotps where pdflatex has\mathinner{\cdotp\cdotp\cdotp}.render-pipeline: math at the current text size, amsmath cmex designs, \dots/\bmod/\pmod spacing, one-letter \mathrm (needs #172 + #174 re-pin) #177 changes exactly these two things ("family 3 follows the packages … cmex10 at 10.95pt"; "
\dots,\ldots,\cdots… become\mathinnergroups of three\ldotp/\cdotp").\frac,\sqrt, scripts,+and=in the same fixture are within 0.0003pt.05 → the compiler lane (Mac) + #177. The compiler drops
\mathbb{Z}and sets\colonas the literal characters\colon, so$h\colon A\to B$and$A=B=\mathbb{Z}$hold the wrong glyphs and the paragraph breaks elsewhere — the same gap that makes HW1/HW2 lines holding\mathbb{Z}differ from their reference here. Paragraph 1 additionally has operator-name spacing gaps:$\sin x$1.899pt narrow,$\log 2<1$2.055pt narrow (about one\thinmuskipeach), while$\max(a,b)$,$a\,b$,$a\quad b$,$x\;y$,$p\mid q$,$n!=120$,$\{1,2,3\}$and$h^{-1}(b)=b-1$are all within 0.12pt.Neither fixture is deleted and no expectation was touched; each
#[ignore]reason names the blocking work, and un-ignoring it is that work's acceptance check.Test harness fix (part of
df1bbae6)The previous revision's word matching was wrong and its numbers were not trustworthy. pdftotext splits a formula into words wherever it sees a gap, which almost never matches how we group glyphs into runs, so pairing words by text compared the wrong glyphs — a line holding
xfour times matchedxto whichever occurrence was nearest, giving differences up to 138pt that were pure artefacts.(1+2)=3,+2b) records no position — itsxMinis the(or+, a whole glyph before the digit the index names. This alone accounted for every 4.17pt row.\sum's limits sorting across their neighbours) is reported and skipped, never silently compared.Gates
All on
linux-primary(NixOS),apps/mac/Fontsplus a dereferenced TeX Live 2025 metric tree whose four digest-bound TFMs are SHA-256 identical to MacTeX's.main. ✅main, 0 overfull each. None of them loads microtype, so a no-change result is the expected one. ✅main. ✅ (Absolute line agreement with the reference is 54/70 and 26/68 on this machine only, unchanged by this branch, because the compiler drops\mathbb{Z},\mid,\forall,\existshere — the compiler lane's gap, present onmaintoo.)cargo test --release: no suite differs frommainexceptmicrotype_math(main0/5 → 3 passed, 2 ignored).inline_math_breaks2/2 (39/46 lines, no overfull),microtype_fixtures8/8.cargo test --release: 99 passed, 0 failed.cargo test --release: 395 passed, 0 failed — same asmain.Pre-existing on this machine and identical on
main, so not caused by this branch:font_families_oracle0/1,math_symbols6/10,metrics_provenance4/5.Environment note (NixOS, not the Mac)
pdflatexhere is TeX Live 2025 / pdfTeX 1.40.27, not MacTeX. No committed expectation was regenerated; the TL2025 run is used only for\showthe\wdmeasurements, reported as such.pdftotextis not installed here, sofixtures/microtype-math/generate.pycannot run on this machine — which is fine, it must not.One trap worth recording for anyone else resuming on NixOS: the project's rooted TFM reader refuses symlinked path components, and every directory in a NixOS
texmf-diststore path is a symlink, so pointingFLASHTEX_TFM_DIRSat the host tree yieldsRefused(SymlinkComponent { component: "lm" }), arequired_metrics_unavailableerror, and math laid out with the OpenType MATH table instead of TeX's metrics — which silently changes every line break. A dereferenced copy plusdoc/fonts/lm/GUST-FONT-LICENSE.TXTfixes it.apps/mac/Fonts/texmfin the repo is the other usable tree.crates/compiler/tests/amsmath_corpus/oracle.pyforcesFLASHTEX_TFM_DIRSto its--fontsargument, so it needs a flat directory holding the OTFs, the TFMs and the licence together.Next steps
vendor/paragraph-layoutto0edd200c(integrator), thencrates/render-pipelinebuilds standalone.0edd200c, paragraph-layout) and PR B (df1bbae6, pipeline, after the re-pin). Both commits are already clean, one crate each.#[ignore]fromscripts_and_fractions_stay_boxes— it is a direct acceptance check for the cmex design size and\cdotsspacing.\colon/\mathbbgap closes, drop#[ignore]fromoperators_functions_and_spacing_commands; the\sin/\logoperator-name spacing (1.899pt / 2.055pt) needs render-pipeline: math at the current text size, amsmath cmex designs, \dots/\bmod/\pmod spacing, one-letter \mathrm (needs #172 + #174 re-pin) #177's\bmod/\pmodfamily of fixes too.🤖 Generated with Claude Code
https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu