Skip to content

Dispatch content stream operators by matching on PdfOperatorVariant - #594

Merged
Velli20 merged 1 commit into
typed-reader-images-color-spacesfrom
remove-operator-backend-traits
Oct 21, 2026
Merged

Velli20 merged 1 commit into
typed-reader-images-color-spacesfrom
remove-operator-backend-traits

Conversation

@Velli20

@Velli20 Velli20 commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Stacked on #593 (both touch the canvas XObject/graphics-state handlers and variants.rs). Retarget to main once #593 merges.

Why

pdf_operator_backend.rs defined twelve per-category operator traits plus the PdfOperatorBackend supertrait. In practice:

  • PdfCanvas was the only production implementor; the other was a test-only RecordingBackend.
  • No code was generic over any single category trait, and none had default methods, so splitting them didn't let anyone implement just part of the set.
  • RecordingBackend turned each parsed PdfOperatorVariant into RecordedOperation, a near-copy of the same enum.

PdfOperatorVariant is already a closed, data-only description of every operator, so the trait layer only added indirection.

What

  • Deleted pdf_operator_backend.rs, recording_pdf_operator_backend.rs, PdfOperatorVariant::call and BeginMarkedContentWithProps::call. BeginMarkedContentWithProps now exposes tag() and properties() instead.
  • Added PdfCanvas::apply_operator in canvas_operator_dispatch.rs. The canvas_*_ops.rs and shading.rs trait impls are now pub(crate) inherent methods.
  • Removed thin wrappers:
    • q/Q call save/restore directly.
    • Rendering intent, flatness, BX/EX and d0/d1 share one no-op arm.
    • The never-dispatched mark_point* handlers are gone.
  • Tests check the parsed operators directly instead of replaying them through the recorder. Tests that only exercised dispatch were removed or narrowed to parsing.
  • Updated the architecture note in AGENTS.md.

Verification

  • cargo fmt --check, cargo clippy --all --workspace and cargo check --no-default-features are clean.
  • cargo test --workspace passes, excluding pdf-graphics-skia and pdf-conformance, which need a Skia download that wasn't available locally.
  • cargo check --target wasm32-unknown-unknown -p web-canvas-example passes.
  • text_recording bench, shipped profile, 5 runs alternating with the previous commit: −0.5% to +0.9% per case, within noise.

🤖 Generated with Claude Code

The operator backend traits (twelve per-category traits plus the
PdfOperatorBackend supertrait) had one production implementor, PdfCanvas,
and a test-only recorder that rebuilt a near-copy of PdfOperatorVariant.
Nothing was generic over a single category trait, so the layer only added
indirection over an enum that already describes every operator.

PdfCanvas now dispatches through apply_operator, and its handlers are
inherent methods. Tests check the parsed operators directly instead of
replaying them through the recorder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Velli20
Velli20 merged commit d7a24b9 into typed-reader-images-color-spaces Oct 21, 2026
6 checks passed
@Velli20
Velli20 deleted the remove-operator-backend-traits branch October 21, 2026 19:57
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