Skip to content

render-pipeline: \includegraphics sets a box in running text; \includegraphics* and the two-bracket form on the rendering route - #325

Closed
GoKubar wants to merge 2 commits into
mainfrom
agent/nixos-graphics/inline-images
Closed

GoKubar wants to merge 2 commits into
mainfrom
agent/nixos-graphics/inline-images

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

The bug

\includegraphics outside a figure/table rendered as nothing at all.
adapter.rs's Inline::Graphic arm advanced the source cursor and pushed no
item — no box, no reserved space, no diagnostic — so every line after it sat
in the wrong place and the editor was never told.

Measured on this PR's ten fixtures against pdfLaTeX (TeX Live 2025), with a
control build of the same file set from origin/main @ 9bd4b025:

images rendered worst body-paragraph baseline error
origin/main 0 of 23 118.875 bp (1.65 in)
this branch 23 of 23 0.006 bp

Worst deviation over all 84 measurements (box x/top/width/height, box bottom
edge against the reference baseline + depth, every paragraph's first-line
baseline): 0.010 bp.

What it does

  • adapter::Item::Graphic carries only what the source says — star, key list,
    file, span — so the block cache keys it on its bytes like every other item.
    The file is read and the box measured in typeset::Context::graphic_box,
    which is where the project root is.
  • The box joins the horizontal list as an ordinary box of the graphicx width,
    height and depth, so the line breaker measures it, a tall image makes
    \baselineskip give way to \lineskip, and the page builder sees the line it
    grew. Fixture 02-tall-line-spacing pins exactly that.
  • Painted through the existing wire Image item, line-local, with the same
    y-up→y-down arithmetic floatpage::FloatBuilder::emit uses; the existing
    place_item/shift_x machinery adds the page and column offsets. A client
    that does not negotiate display-list-v2-images still gets the correct
    layout with the picture omitted, exactly as it already did for floats.
  • Goes through the compiler's parse tree, not a second raw scan.
  • The per-request ImageCache is now shared between the float and running-text
    paths, so a file used by both is read once.

\includegraphics* and the two-bracket form

floats.rs's scanner matched the command name and read one bracket, so
\includegraphics*{...} and \includegraphics[10,10][40,50]{...} fell through
to Piece::Other — reported as "this material is omitted" and never drawn,
inside a float. It now reads the full graphicx shape (star, up to two
optional arguments, then the file; ] inside a brace group does not end an
argument) and records the two-bracket form as viewport=<llx> <lly> <urx> <ury>, exactly as the compiler's own parser does. Fixtures 09-float-starred
and 10-float-two-bracket cover it end to end.

viewport/trim/clip were GKey::Unsupported, which made the two-bracket
form size the box at the picture's natural size — wrong geometry, not merely
an uncropped picture. size_box now carries the box rectangle beside the
image's own placement, so the box is the requested rectangle and the image rides
inside it; scaling and \Grot@box's rotation act on both. Without clip,
graphicx draws the whole picture and lets it overflow — which is exactly what an
affine transform with no clip path already produces — so the unstarred form is
now correct, not approximated.

What it diagnoses instead of silently dropping

\scalebox, \resizebox, \rotatebox, \reflectbox set their content
untransformed and said nothing. They are not implemented here, and the
reason is structural: the display list places text by a baseline and per-glyph
advances and carries no transform. Only Item::Image has a matrix, and these
boxes take arbitrary horizontal material (text, math, tables), so there is
nothing to write a transform into without a wire-protocol change. They now
report graphics_transform_unapplied, naming the command and what it would have
done.

clip / \includegraphics* that actually crops reports graphics_clip (the
box is right, the picture is drawn whole). A bare \includegraphics*, which
clips to the natural bounding box and removes nothing, stays silent.

Sibling PR #326 stops the compiler's command inventory — and so
the Mac editor's completion vocabulary, which is generated from it — describing
the four transforms as working.

\graphicspath

Parsed by the compiler and dropped. The compiler's own module says the list "is
re-read from the source by a consumer that loads files"
, which is what
graphics::search_dirs now does (comments skipped, last one wins). The file
search tries the name as written first, then each directory, extension-major —
the order graphicx's \Gin@extensions loop over \IfFileExists produces.
Fixture 07-graphicspath resolves one file from each of two directories.

Gates (all measured on this branch)

gate result
new tests/graphics_oracle.rs 10 fixtures, 84 checks, worst 0.010 bp
amsmath corpus 59/59 within 0.5 bp
HW1 / HW2 (render) 3 pages each, 0 overfull, 0 errors — identical to control
real-world corpus, 22 fixtures 44 fixture/producer rows, 0 differences vs origin/main
cargo test --release -p flashtex-render-pipeline 47 binaries, 218 passed, 0 failed

Per-fixture worst deviation (bp): 0.001, 0.002, 0.006, 0.000, 0.003, 0.002,
0.004, 0.001, 0.010, 0.002.

The corpus is unchanged because every \includegraphics in it is inside a
float and all but one carry draft (the file is not read) — the change is
genuinely inert there, not accidentally hidden.

New reference data was generated by fixtures/graphics/oracle.py on pdfTeX
3.141592653-2.6-1.40.27 (TeX Live 2025)
and is labelled reference_engine.
The MacTeX-generated fixtures/floats/reference/*.json are untouched, and
fixtures/floats/oracle.py was not modified. The new gate fails on any
font-failure diagnostic, so two builds sharing a fallback face cannot agree on
wrong geometry.

Deliberately out of scope

  • trim/clip/viewport crop the box; the picture is drawn whole (the
    wire Image item has no crop field — that needs a protocol change).
  • EPS and PS are not read.
  • The float route still re-scans raw source for its own structure (\caption,
    \label, \centering); only its \includegraphics scanning was fixed.
  • crates/render-pipeline/vendor/* untouched. No re-pin is needed for this
    branch.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu


CI note: rust bridge is red on main, not from this branch

rust bridge (ubuntu-latest) fails here, and it fails on plain origin/main
@ 6246a191 in exactly the same way. crates/bridge/src/features.rs:45 calls
flashtex_compiler::math::parse_tokens with two arguments; 543bde5c
("compiler: a package-provided math symbol needs its package loaded") gave it a
third (MathPackages) without updating the caller. The crate's library
builds; only cargo test does not (tests/proposal_validation also fails).

Reproduced with a git archive of crates/ at 6246a191 built outside any
lane worktree. Neither of this lane's branches touches crates/bridge or
crates/compiler/src/math.rs (git diff --name-only main… lists 0 bridge
files). Unowned; flagging rather than fixing, since it is another lane's crate.

An image outside a float rendered as nothing at all. `adapter.rs`'s
`Inline::Graphic` arm advanced the cursor and pushed no item, with no
diagnostic: no box, no reserved space, no warning, and every line after it in
the wrong place. On this lane's ten fixtures against pdfLaTeX (TeX Live 2025)
that is 23 images missing and body-paragraph baselines up to **118.875 bp**
(1.65 in) from the reference. After this change the worst deviation over the
same 84 measurements is **0.010 bp**.

What it does now

* `adapter::Item::Graphic` carries what the source says — the star, the key
  list, the file, the span — and nothing more, so the block cache can key it on
  its bytes like every other item. The file is read and the box measured in
  `typeset::Context::graphic_box`, which is where the project root is.
* The box joins the horizontal list as an ordinary box of the graphicx width,
  height and depth, so the line breaker measures it, a tall image makes
  `\baselineskip` give way to `\lineskip`, and the page builder sees the line
  it grew. Fixture 02 pins exactly that.
* It is painted through the existing wire `Image` item, line-local, with the
  same y-up-to-y-down arithmetic `floatpage::FloatBuilder::emit` uses; the
  existing `place_item`/`shift_x` machinery adds the page and column offsets.
  A client that does not negotiate `display-list-v2-images` still gets the
  correct layout with the picture omitted, as it already did for floats.
* It goes through the compiler's parse tree, not a second raw scan, so
  `\includegraphics*` and the `[llx,lly][urx,ury]` bounding-box form arrive
  already parsed.
* The per-request `ImageCache` is now shared between the float path and the
  running-text path, so a file used by both is read once.

`\includegraphics*` and the two-bracket form on the float route

`floats.rs`'s scanner matched the command name and read one bracket, so
`\includegraphics*{...}` and `\includegraphics[10,10][40,50]{...}` fell through
to `Piece::Other` — reported as "this material is omitted" and never drawn,
inside a float. It now reads the full graphicx shape (star, up to two optional
arguments, then the file, with `]` inside a brace group not ending an
argument) and records the two-bracket form as `viewport=<llx> <lly> <urx>
<ury>`, exactly as the compiler's own parser does, so both routes describe the
same image the same way. Fixtures 09 and 10 cover it end to end.

Crop keys

`viewport`, `trim` and `clip` were `GKey::Unsupported`, which made the
two-bracket form size the box at the picture's natural size — the geometry was
wrong, not merely uncropped. `size_box` now carries the box rectangle beside
the image's own placement, so the box is the requested rectangle and the image
rides inside it; scaling and `\Grot@box`'s rotation act on both. Without
`clip`, graphicx draws the whole picture and lets it overflow, which is exactly
what an affine transform with no clip path already produces, so the unstarred
form is now correct rather than approximated. `clip` (and `\includegraphics*`)
cannot be painted — this display list has no clip path — so a crop that
actually removes something reports `graphics_clip`; a bare star, which clips to
the natural bounding box and removes nothing, says nothing.

`\graphicspath`

Parsed by the compiler and dropped; the compiler's own module notes that the
list "is re-read from the source by a consumer that loads files", which is what
`graphics::search_dirs` now does (skipping comments, last one wins). The file
search tries the name as written first and then each directory, extension-major,
the order graphicx's `\Gin@extensions` loop over `\IfFileExists` produces.
Fixture 07 resolves one file from each of two directories.

Box transforms: diagnosed, not silently dropped

`\scalebox`, `\resizebox`, `\rotatebox` and `\reflectbox` set their content
untransformed here and said nothing about it. They are NOT implemented by this
change, and the reason is structural rather than effort: the display list
places text by a baseline and per-glyph advances and carries no transform.
Only `Item::Image` has a matrix, and these boxes take arbitrary horizontal
material — text, math, tables — so there is nothing to write the transform
into without a wire-protocol change. They now report
`graphics_transform_unapplied`, naming the command and what it would have done.
The sibling branch agent/nixos-graphics/inventory-truth stops the compiler's
command inventory (and so the Mac editor's completion vocabulary) from
describing them as working.

Still not done, deliberately: `trim`/`clip`/`viewport` crop the *box* but the
picture is drawn whole (the wire `Image` has no crop field); EPS and PS are not
read; the float route still re-scans raw source for its own structure
(`\caption`, `\label`, `\centering`) rather than using the parse tree.

Evidence

* New gate `tests/graphics_oracle.rs` over `fixtures/graphics/`: ten fixtures,
  84 checks, every image's page/x/top/width/height, its bottom edge against the
  reference `baseline + depth`, and every body paragraph's first-line baseline.
  Worst 0.010 bp; per fixture 0.001 / 0.002 / 0.006 / 0.000 / 0.003 / 0.002 /
  0.004 / 0.001 / 0.010 / 0.002. References generated by
  `fixtures/graphics/oracle.py` on pdfTeX 3.141592653-2.6-1.40.27 (TeX Live
  2025) and labelled `reference_engine`; the MacTeX-generated
  `fixtures/floats/reference/*.json` are untouched. The gate fails on any
  font-failure diagnostic, so a shared fallback face cannot make two builds
  agree on wrong geometry.
* Control measurement: the same gate against a build of this file set from
  origin/main @ 9bd4b02 reports 0 of 23 images rendered and a worst paragraph
  deviation of 118.875 bp.
* amsmath corpus 59/59 within 0.5 bp with this branch's `flashtex-render`.

Implementation-Agent: claude-opus-5, lane nixos-graphics, machine linux-primary
Commit-Executor: claude-opus-5 direct git (Cursor usage-limit fallback, AGENTS.md)
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 14, 2026

Copy link
Copy Markdown
Contributor

daniel-parent (reference data for draft, not a review, no overlap intended): while fixing the harness project_root gap (#342), I found that four real-world fixtures use \includegraphics[draft,...] with no image file present. Your PR's comment says the draft box reserves the space and leaves it empty. Here is what pdflatex does, measured locally with TeX Live 2026 and graphics.sty \Gin@setfile:

  • A missing file under draft is a warning (File 'nofile.png' not found), not an error. The natural size falls back to a 72bp × 72bp bounding box.
  • Box size (wd, ht, dp):
    • [draft] → 72.27pt, 72.27pt, 0
    • width=100pt → 100pt × 100.005pt
    • height=50pt → 50pt × 50pt
    • width=100pt,height=50pt → 100.005pt × 50pt
    • scale=2 → 144.54pt square
    • width=\linewidth in article → 345pt × 345.1pt
    • an existing example-image-a.png at width=100pt → 100pt × 75pt (real aspect ratio)
  • What's drawn: \vrule on both sides, \hrule top and bottom across the requested width, and the file name in \ttfamily left-aligned and vertically centred.

Today FlashTeX gives draft is not honoured yet plus an error, image file 'plot.png' not found. Page counts on conf-paper, lab-report, thesis-chapter and hyperref-toc also depend on float content and \chapter, so fixing draft alone won't match them. I'm leaving this for the graphics lane unless the Commander assigns it elsewhere.

@GoKubar

GoKubar commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Closed under the old-engine freeze (DESIGN.md D13): this 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

@GoKubar GoKubar closed this Sep 30, 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.

3 participants