Skip to content

pdf: exact route paints TikZ opacity through pgf's /pgf@CA and /pgf@ca ExtGStates (stacked on #527) - #533

Merged
GoKubar merged 3 commits into
mainfrom
agent/daniel-parent/pdf-exact-route-alpha
Sep 15, 2026
Merged

GoKubar merged 3 commits into
mainfrom
agent/daniel-parent/pdf-exact-route-alpha

Conversation

@d-q222

@d-q222 d-q222 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Stacked on #527. Review and merge #527 first; this PR's diff against main includes #527's commit until then.

What

Constant alpha on the crates/pdf exact route (exact.rs, v2.rs). Nothing under vendor/ changes.

  1. gs for pgf's alpha states only. New Op::StrokeAlpha(a) and Op::FillAlpha(a) write /pgf@CA<a> gs and /pgf@ca<a> gs. The writer, parser and validator all handle them. Any other gs name is still refused (ExtGState /GS0 is outside the exact export's bounded set). The validator also refuses an alpha outside [0, 1] and a gs while a path is being built.
  2. /ExtGState resources. Each page declares every state it selects once, sorted by name, inline, with pgf's one-key dictionaries: /ExtGState << /[email protected] << /CA 0.5 >> /[email protected] << /ca 0.4 >> >>. No page /Group is added, and pdflatex writes none. Object numbering does not change.
  3. Display list → operators. A paint alpha below 1 is no longer an error. The alpha is rounded like a colour component (5 decimals: 0.30000000000000004 becomes 0.3). It is selected right after the colour, inside the item's own q … Q:
    • path_stroke gets /pgf@CA<a> (from draw opacity).
    • path_fill, rule and glyph_run get /pgf@ca<a> (from fill opacity and text opacity).
    • A black item with alpha is wrapped in q … Q too.

Oracle (pdflatex 1.40 + pgf, uncompressed, measured)

\fill[red,opacity=0.4]             q 1 0 0 rg 1 0 0 RG /[email protected] gs /[email protected] gs … f Q
\draw[blue,draw opacity=0.5]       q 0 0 1 rg 0 0 1 RG /[email protected] gs 1.99255 w … S Q
\fill[green,fill opacity=0.3]      q 0 1 0 rg 0 1 0 RG /[email protected] gs … f Q
\node[text opacity=0.5]            q 0 G 0 g /[email protected] gs /[email protected] gs … BT … ET Q
resources                          /ExtGState 1 0 R, 1 0 obj << /[email protected] << /CA 0.4 >> /[email protected] << /ca 0.4 >> … >>

pgf builds the name from the option's own text (opacity=.4 → /[email protected], 0.40 → /[email protected], 1/3 → /[email protected]). The display list only carries an f64, so this route writes the shortest 5-decimal form (0.4).

Deviation from the task text: the task suggested one /GSn state per distinct (CA, ca) pair. This PR follows pgf instead: one state per distinct component value, named pgf@CA<a> or pgf@ca<a>. The display list has one alpha per item anyway.

Why

extended/tikz-clipping-patterns (from #42's cases; only the .tex was used, no reference PDFs) still failed on the #527 route with paint alpha 0.4 is not 1; alpha needs an ExtGState.

flashtex build main.tex, run with a scratch copy (not committed) whose crates/render-pipeline/vendor/pdf was replaced by the given crates/pdf:

vendor/pdf source exit last line
#527 head e7e6405d 1 flashtex: error: pdf: payload.pages[0].items[3]: paint alpha 0.4 is not 1; alpha needs an ExtGState, which is outside the bounded operator set
this branch 14c152ef 0 flashtex: main.tex: recovered, 1 page, 1 error, 3 warnings -> pdf main.pdf, v2 main.json

The remaining "1 error" is the existing \usetikzlibrary preamble recovery, and it also appears before. The output page has /ExtGState << /[email protected] << /ca 0.4 >> >> and q 1 0 0 rg 1 0 0 RG /[email protected] gs … f Q. A Ghostscript rasterisation shows the red and blue circles overlapping translucently (purple where they overlap), plus the clipped blue!20 disc with its red stroke.

Overlap check

Test results

  • cargo test (crates/pdf):
    • test result: ok. 53 passed; 0 failed (lib; rejects_operators_outside_the_bounded_set now checks sh plus a non-pgf gs name)
    • test result: ok. 12 passed; 0 failed (tests/exact)
    • test result: ok. 5 passed; 0 failed (images)
    • test result: ok. 11 passed; 0 failed (navigation)
    • test result: ok. 28 passed; 0 failed (render)
    • test result: ok. 3 passed; 0 failed (type1)
    • test result: ok. 10 passed; 0 failed (tests/v2)
  • New tests in tests/v2:
    • tikz_alpha_selects_pgf_ext_gstates_inside_the_items_save_restore: checks the exact operator stream for fill, stroke, rule, a repeated alpha and CA/ca at the same value. It also reads the page's /ExtGState dictionary back through PdfFile, and runs verify::check_structure, a content round-trip and a determinism check.
    • alpha_ext_gstates_are_deduplicated_per_page: 10 selections on 2 pages give one entry per page, and an opaque page declares none.
    • only_pgf_alpha_ext_gstates_are_accepted
    • hand_built_envelope_… now covers a glyph run at alpha 0.5: q 0.5 0 1 rg /[email protected] gs BT … ET Q.
  • cargo check --tests (crates/rendering-core): Finished.

Not done

  • flashtex build does not use this until crates/render-pipeline/vendor/pdf is re-pinned to a main that has pdf: exact route rounds colours to xcolor's 5 decimals and paints TikZ path_stroke/path_fill #527 and this PR (per VENDORING.md). This PR does not edit vendor/.
  • Not byte-identical to pgf:
    • For opacity= pgf writes both CA and ca. The display list has one alpha per item, so only the state the item paints with is written. For a fill-only or stroke-only item this changes no pixel.
    • The name uses the rounded f64 (0.4), not the option's own text (.4).
    • Resources are inline per page, where pgf uses one shared indirect dictionary.
  • In tikz-clipping-patterns, pattern= is still ignored (the square renders solid black) and \shade is skipped. Both happen upstream in the TikZ subset.

🤖 Generated with Claude Code

https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz

…Z path items

What changed:
- v2: `pdf_number` formats an f64 as a bounded PDF number (shortest decimal
  rounded half away from zero, no exponent, -0 -> 0); sRGB colour components
  use 5 digits (pdflatex measured: RGB{20,80,170} -> 0.07843 0.31374 0.66667
  rg). Replaces the exact-binary requirement that refused 0.8 and 0.07843.
- v2: `path_fill` / `path_stroke` items (with clips) become
  q [rg RG] [clip W n] [w M d J j] path f|f*|S Q, in pgf's pdfTeX order.
- exact: `M` (miter limit) joins the bounded operator set.

What was run:
- cargo test (crates/pdf): lib 53 passed; tests/v2 7 passed; all targets 0 failed
- cargo check --tests (crates/rendering-core): ok

Next step: re-pin crates/render-pipeline/vendor/pdf so `flashtex build` uses it.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…tates

What changed:
- exact: Op::StrokeAlpha / Op::FillAlpha, written `/pgf@CA<a> gs` and
  `/pgf@ca<a> gs`, the names and one-key dictionaries pgf's pdfTeX driver
  writes (measured with pdflatex 1.40). `gs` joins the bounded operator set
  for those names only; any other ExtGState is still refused. Validation
  refuses an alpha outside [0, 1] and `gs` during path construction. Each
  page declares the states it selects once, sorted by name, inline:
  `/ExtGState << /[email protected] << /ca 0.4 >> >>`.
- v2: a paint alpha below 1 is no longer an error. It is rounded like a
  colour component and selected right after the colour inside the item's
  `q … Q`: path_stroke sets CA, path_fill, rule and glyph_run set ca.
- Tests: exact operator stream and resources for stroke and fill alpha,
  per-page dedup, refusal of other gs names, text alpha on a glyph run.

What was run:
- cargo test (crates/pdf): test result: ok. 53 passed; 0 failed (lib),
  12 passed (tests/exact), 5 (images), 11 (navigation), 28 (render),
  3 (type1), 10 passed; 0 failed (tests/v2).
- cargo check --tests (crates/rendering-core): Finished.

Next step: build extended/tikz-clipping-patterns with a scratch copy whose
render-pipeline/vendor/pdf is this crate.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
#533 is stacked on #527. Squashing the parent onto main rewrote its commits, so
crates/pdf/src/v2.rs and tests/v2.rs conflicted wholesale. The branch already
contains #527's work, and the only commit on main touching those two paths since
the merge base is #527's squash, so the branch side is the complete one.

Implementation-Agent: orchestrator-astra
Commit-Executor: orchestrator-astra (direct; Cursor usage limit)
@GoKubar
GoKubar merged commit c82655d into main Sep 15, 2026
10 of 21 checks passed
@GoKubar
GoKubar deleted the agent/daniel-parent/pdf-exact-route-alpha 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.

2 participants