Skip to content

pdf-renderer: Refactor PdfRenderer to own document and decouple canva…#113

Merged
Velli20 merged 1 commit intomainfrom
pdf-renderer-refactor
Mar 15, 2026
Merged

pdf-renderer: Refactor PdfRenderer to own document and decouple canva…#113
Velli20 merged 1 commit intomainfrom
pdf-renderer-refactor

Conversation

@Velli20
Copy link
Owner

@Velli20 Velli20 commented Mar 15, 2026

…s backend

This commit refactors the PdfRenderer struct to take ownership of the PdfDocument and removes the lifetime parameters and direct coupling to a canvas backend. The renderer now exposes methods to access the owned document and to render pages by passing a mutable backend reference per call. The render_page_to_recording function is now a method, and a helper for page lookup is added.

…s backend

This commit refactors the PdfRenderer struct to take ownership of the PdfDocument
and removes the lifetime parameters and direct coupling to a canvas backend. The
renderer now exposes methods to access the owned document and to render pages by
passing a mutable backend reference per call. The render_page_to_recording
function is now a method, and a helper for page lookup is added.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 15, 2026 16:09
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors pdf-renderer so PdfRenderer owns the PdfDocument and no longer holds a long-lived mutable borrow of a specific canvas backend, instead taking a mutable backend reference per render call. The examples and page-cache docs are updated to use the new API.

Changes:

  • Refactor PdfRenderer to own PdfDocument, remove lifetimes/generic backend coupling, and add document() / into_document() accessors.
  • Update rendering entrypoints to accept &mut impl CanvasBackend per call, including render_page_cached and the recording helper now as a method.
  • Update Skia/FemtoVG/Emscripten examples to use the new renderer ownership model.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
crates/pdf-renderer/src/lib.rs Refactors PdfRenderer ownership + rendering API; updates cache helper to take &PdfRenderer.
crates/pdf-renderer/src/page_cache.rs Updates module docs/example to use the new PdfRenderer recording method.
examples/skia.rs Removes Arc<PdfDocument> usage; stores a PdfRenderer in the application state and renders with a per-call backend.
examples/femtovg.rs Updates app trait/state to pass around &PdfRenderer rather than &PdfDocument.
examples/emscripten.rs Switches thread-local storage from PdfDocument to PdfRenderer; updates cached rendering callsites accordingly.
Comments suppressed due to low confidence (1)

crates/pdf-renderer/src/lib.rs:49

  • PdfRenderer::render now takes a canvas_backend: &mut B argument, but the doc comment’s “# Parameters” section only documents page_index. Please update the docs to include canvas_backend (and what coordinate space/size it represents) so callers know what to pass.
    /// Renders a page onto the canvas backend.
    ///
    /// # Parameters
    ///
    /// - `page_index` – Zero-based index of the page to render.
    ///

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +121 to 125
CURRENT_RENDERER.with(|renderer| {
let renderer_ref = renderer.borrow();
let Some(renderer) = renderer_ref.as_ref() else {
return -2;
};
Comment on lines 132 to 136
// Cache miss: render to recording canvas
let recording = render_page_to_recording(document, page_index, width, height)?;
let recording = renderer.render_page_to_recording(page_index, width, height)?;

// Replay to the actual backend
recording.replay(backend)?;
Comment on lines +69 to +70
/// This records all drawing commands for a page into a resolution-independent
/// `RecordingCanvas` that can be replayed to any backend at any size.
Comment on lines +21 to 23
//! let recording = renderer.render_page_to_recording(page_index, width, height)?;
//! cache.insert(page_index, recording.clone());
//! recording.replay(&mut backend)?;
@Velli20 Velli20 merged commit d604526 into main Mar 15, 2026
8 checks passed
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