From b7496c590126e698bbf4d2632c1e8a16121d9591 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 24 Nov 2026 13:16:09 +0100 Subject: [PATCH] :bug: Fix background blur in PDF export The blurred backdrop is drawn in page space keeping the scale of the PDF canvas. Reset to identity, it was drawn about four times too small at the page corner, so the blur did not show. AI-assisted-by: claude-opus-5-5 --- render-wasm/src/render/pdf.rs | 52 ++++++++++++++++++++++++++++++++ render-wasm/src/render/vector.rs | 13 +++++--- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/render-wasm/src/render/pdf.rs b/render-wasm/src/render/pdf.rs index 29ddd4f863..4f7bca8848 100644 --- a/render-wasm/src/render/pdf.rs +++ b/render-wasm/src/render/pdf.rs @@ -215,6 +215,58 @@ mod tests { .count() } + #[test] + fn embedded_background_blur_follows_the_page_scale() { + // A PDF page canvas starts scaled by raster_dpi / 72. The embedded + // backdrop was drawn after resetting the matrix, so it landed that + // many times too small, outside the panel: the PDF showed no blur. + use crate::shapes::{Blur, BlurType, Frame}; + + let mut pool = ShapesPool::new(); + let (board, red, panel) = (uid(1), uid(2), uid(3)); + { + let shape = pool.add_shape(board); + shape.set_parent(Uuid::nil()); + shape.set_shape_type(Type::Frame(Frame::default())); + shape.set_selrect(0.0, 0.0, 100.0, 100.0); + shape.set_fills(vec![Fill::Solid(SolidColor(skia::Color::BLUE))]); + shape.add_child(red); + shape.add_child(panel); + } + add_solid_rect(&mut pool, red, (0.0, 0.0, 50.0, 100.0), skia::Color::RED); + pool.get_mut(&red).unwrap().set_parent(board); + { + let shape = pool.add_shape(panel); + shape.set_parent(board); + shape.set_shape_type(Type::Rect(Rect::default())); + shape.set_selrect(20.0, 20.0, 80.0, 80.0); + shape.set_background_blur(Some(Blur::new(BlurType::BackgroundBlur, false, 16.0))); + } + + let mut resources = RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let raster_scale = 4.0; + let mut surface = skia::surfaces::raster_n32_premul((400, 400)).expect("surface"); + let page = skia::Rect::from_xywh(0.0, 0.0, 100.0, 100.0); + { + let canvas = surface.canvas(); + canvas.scale((raster_scale, raster_scale)); + vector::render_tree_pdf(&mut resources, canvas, &board, &pool, page).expect("render"); + } + let image = surface.image_snapshot(); + let pixels = image.peek_pixels().expect("pixels"); + // Just left of the red/blue edge, inside the panel: blurred, it takes + // some blue; sharp, it is pure red. + let near_edge = pixels.get_color((47 * 4, 50 * 4)); + assert!( + near_edge.b() > 20, + "blurred backdrop expected, got {near_edge:?}" + ); + // Outside the panel the page stays sharp. + let outside = pixels.get_color((47 * 4, 10 * 4)); + assert_eq!(outside, skia::Color::RED); + } + #[test] fn multi_page_pdf_has_one_page_per_shape() { let mut pool = ShapesPool::new(); diff --git a/render-wasm/src/render/vector.rs b/render-wasm/src/render/vector.rs index c00930a05c..327a38c35f 100644 --- a/render-wasm/src/render/vector.rs +++ b/render-wasm/src/render/vector.rs @@ -847,6 +847,9 @@ fn render_background_blur_image( blur_surface.image_snapshot() }; + // Page-space transform of the canvas, which a PDF page starts scaled by + // raster_dpi / 72: the backdrop is drawn back in this space below. + let page_matrix = canvas.local_to_device(); let matrix = shape.centered_transform(); canvas.save(); canvas.concat(&matrix); @@ -871,10 +874,12 @@ fn render_background_blur_image( clip_to_shape(canvas, shape, true); } } - // Draw the pre-blurred full-page bitmap in device space (1 image px per - // device unit) so it aligns with the page regardless of the shape transform. - canvas.reset_matrix(); - canvas.draw_image(&blurred, (0.0, 0.0), None); + // Draw the pre-blurred full-page bitmap over the page rect, in page space, + // so it aligns with the page regardless of the shape transform. Resetting + // to device space instead dropped the PDF page scale and drew the bitmap + // outside the clip. + canvas.set_matrix(&page_matrix); + canvas.draw_image_rect(&blurred, None, bounds, &Paint::default()); canvas.restore(); Ok(()) }