🐛 Fix shape drop shadows on image fills with alpha (#13279)

* 🐛 Shape drop shadows by the alpha of image fills

* 🐛 Shape exported drop shadows by the alpha of image fills

* 🐛 Cast child silhouettes for image-fill frame drop shadows

Image-only fills can leave transparent holes, so the direct container
drop-shadow path must not skip descendant silhouettes. Exclude those
frames from the fast path and from the covering check, and cover the
gate with unit tests.

AI-assisted-by: composer

* ♻️ Drop unreachable image branches from frame shadow fast path

AI-assisted-by: claude-opus-5-5

* ⚡ Size raster export image shadow masks to the output resolution

AI-assisted-by: claude-opus-5-5

---------

Co-authored-by: Alejandro Alonso <alejandroalonsofernandez@gmail.com>
This commit is contained in:
Elena Torró 2026-11-26 13:02:41 +01:00 committed by GitHub
parent 09b53a4932
commit 9c61a5bd9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 248 additions and 54 deletions

View File

@ -3533,11 +3533,15 @@ impl RenderState {
let world_offset = (mapped.x, mapped.y);
// The opacity of fills and strokes shouldn't affect the shadow,
// so we paint everything black with the same opacity.
// so we paint everything black with the same opacity. Image fills stay
// so their alpha shapes the shadow; `shadow_mask_paint` tints them.
let plain_shape_mut = plain_shape.to_mut();
plain_shape_mut.clear_fills();
if shape.has_fills() {
plain_shape_mut.add_fill(Fill::Solid(SolidColor(skia::Color::BLACK)));
let image_silhouette = shape.has_only_image_fills();
if image_silhouette {
let image_fills = shape.fills().map(Fill::with_full_opacity).collect();
plain_shape_mut.set_fills(image_fills);
} else if shape.has_fills() {
plain_shape_mut.set_fills(vec![Fill::Solid(SolidColor(skia::Color::BLACK))]);
}
// Reuse existing strokes and only override their fill color.
@ -3554,7 +3558,13 @@ impl RenderState {
// (which defers strokes to render_shape_exit for clipped frames).
plain_shape_mut.clip_content = false;
let spread_outset = if shadow.spread > 0.0 && shape.spreads_through_strokes() {
// Image spread dilates the mask, which on a tile only sees its margin.
let image_spread = image_silhouette && shadow.spread > 0.0;
let dilate_fits_tile =
!image_spread || shadow.spread * scale <= self.surfaces.margins().width as f32;
let spread_outset = if image_spread {
None
} else if shadow.spread > 0.0 && shape.spreads_through_strokes() {
plain_shape_mut.apply_shadow_spread(shadow.spread);
None
} else {
@ -3574,10 +3584,25 @@ impl RenderState {
return Ok(());
}
let shadow_paint = shadows::shadow_mask_paint(
radius_to_sigma(shadow.blur),
image_silhouette,
shadow.spread,
);
let layer_rec = skia::canvas::SaveLayerRec::default().paint(&shadow_paint);
// blur=0 at high zoom: draw directly on DropShadows with geometric spread (no filter).
if scale > 1.0 && shadow.blur <= 0.0 {
if scale > 1.0 && shadow.blur <= 0.0 && dilate_fits_tile {
let drop_canvas = self.surfaces.canvas(SurfaceId::DropShadows);
drop_canvas.save();
if image_silhouette {
drop_canvas.save_layer(
&skia::canvas::SaveLayerRec::default()
.bounds(&bounds)
.paint(&shadow_paint),
);
} else {
drop_canvas.save();
}
//drop_canvas.scale((scale, scale));
//drop_canvas.translate(translation);
@ -3602,24 +3627,8 @@ impl RenderState {
return Ok(());
}
// Create filter with blur only (no offset, no spread - handled geometrically)
let blur_only_filter = if transformed_shadow.blur > 0.0 {
let sigma = radius_to_sigma(transformed_shadow.blur);
Some(skia::image_filters::blur((sigma, sigma), None, None, None))
} else {
None
};
let mut shadow_paint = skia::Paint::default();
if let Some(blur_filter) = blur_only_filter {
shadow_paint.set_image_filter(blur_filter);
}
shadow_paint.set_blend_mode(skia::BlendMode::SrcOver);
let layer_rec = skia::canvas::SaveLayerRec::default().paint(&shadow_paint);
// Low zoom path: use blur filter but apply offset and spread geometrically
if use_low_zoom_path {
if use_low_zoom_path && dilate_fits_tile {
let drop_canvas = self.surfaces.canvas(SurfaceId::DropShadows);
drop_canvas.save_layer(&layer_rec);
//drop_canvas.scale((scale, scale));

View File

@ -154,16 +154,37 @@ fn spread_inset(spread: f32) -> Option<f32> {
Some(-spread).filter(|&s| s > 0.0)
}
fn blur_layer_paint(blur: f32, sigma_scale: f32) -> skia::Paint {
/// Layer paint for a black shadow mask, blurred by `sigma`. An image mask
/// keeps its alpha, so the paint tints it black and dilates it by `spread`:
/// images have no outline to outset.
pub(crate) fn shadow_mask_paint(sigma: f32, image_mask: bool, spread: f32) -> skia::Paint {
let dilate = if image_mask && spread > 0.0 {
skia::image_filters::dilate((spread, spread), None, None)
} else {
None
};
let filter = if sigma > 0.0 {
skia::image_filters::blur((sigma, sigma), None, dilate, None)
} else {
dilate
};
let mut paint = skia::Paint::default();
if blur > 0.0 {
let sigma = radius_to_sigma(blur) * sigma_scale;
paint.set_image_filter(skia::image_filters::blur((sigma, sigma), None, None, None));
paint.set_image_filter(filter);
if image_mask {
paint.set_color_filter(skia::color_filters::blend(
skia::Color::BLACK,
skia::BlendMode::SrcIn,
));
}
paint.set_blend_mode(skia::BlendMode::SrcOver);
paint
}
fn blur_layer_paint(blur: f32, sigma_scale: f32) -> skia::Paint {
shadow_mask_paint(radius_to_sigma(blur) * sigma_scale, false, 0.0)
}
fn draw_frame_shadow_rect(
surfaces: &mut super::Surfaces,
surface_id: SurfaceId,

View File

@ -4,8 +4,9 @@ use skia_safe::{self as skia, Canvas, Paint, RRect};
use crate::error::Result;
use crate::shapes::{
circle_segments_local, merge_fills, radius_to_sigma, rect_segments_local, stroke_to_path,
BlurType, Fill, Frame, Path, Rect, Shape, SolidColor, Stroke, StrokeKind, StrokeStyle, Type,
circle_segments_local, has_only_image_fills, merge_fills, radius_to_sigma, rect_segments_local,
stroke_to_path, BlurType, Fill, Frame, Path, Rect, Shape, SolidColor, Stroke, StrokeKind,
StrokeStyle, Type,
};
use crate::state::ShapesPoolRef;
use crate::uuid::Uuid;
@ -33,8 +34,9 @@ pub(super) struct VectorRenderer<'a> {
compose_fills: bool,
/// Set when an enclosing layer already applies the shape's layer blur.
blur_in_layer: bool,
/// Set inside a container's drop-shadow pass: text glyphs paint opaque.
silhouette: bool,
/// Set inside a container's drop-shadow pass, to its spread: text glyphs
/// paint opaque and image fills grow by the spread.
silhouette: Option<f32>,
}
impl<'a> VectorRenderer<'a> {
@ -50,7 +52,7 @@ impl<'a> VectorRenderer<'a> {
scale,
compose_fills,
blur_in_layer: false,
silhouette: false,
silhouette: None,
}
}
@ -74,6 +76,13 @@ impl ShapeRenderer for VectorRenderer<'_> {
return Ok(());
}
if let Some(spread) = self
.silhouette
.filter(|&spread| spread > 0.0 && has_only_image_fills(fills))
{
return draw_image_silhouette(self.shared, self.canvas, shape, fills, spread);
}
let blur_filter = self.layer_blur_filter(shape);
let has_image_fills = fills.iter().any(|f| matches!(f, Fill::Image(_)));
if !self.compose_fills || has_image_fills {
@ -137,16 +146,26 @@ impl ShapeRenderer for VectorRenderer<'_> {
.bounds(&layer_bounds)
.paint(&paint);
self.canvas.save_layer(&layer_rec);
let mut fill_paint = Paint::default();
fill_paint.set_anti_alias(true);
fill_paint.set_color(skia::Color::BLACK);
// Spread as a mitered outline: Skia's dilate filter runs as
// SkSL per pixel on CPU canvases, which takes minutes.
if shadow.spread > 0.0 {
fill_paint.set_style(skia::PaintStyle::StrokeAndFill);
fill_paint.set_stroke_width(2.0 * shadow.spread);
if shape.has_only_image_fills() {
draw_image_silhouette(
self.shared,
self.canvas,
shape,
&shape.fills,
shadow.spread,
)?;
} else {
let mut fill_paint = Paint::default();
fill_paint.set_anti_alias(true);
fill_paint.set_color(skia::Color::BLACK);
// Spread as a mitered outline: Skia's dilate filter runs as
// SkSL per pixel on CPU canvases, which takes minutes.
if shadow.spread > 0.0 {
fill_paint.set_style(skia::PaintStyle::StrokeAndFill);
fill_paint.set_stroke_width(2.0 * shadow.spread);
}
draw_shape_geometry(self.canvas, shape, &fill_paint);
}
draw_shape_geometry(self.canvas, shape, &fill_paint);
self.canvas.restore();
}
}
@ -198,7 +217,7 @@ impl ShapeRenderer for VectorRenderer<'_> {
let text_content = text_content.new_bounds(shape.selrect());
let mut paragraph_builders =
text_content.paragraph_builder_group_from_text(Some(self.silhouette));
text_content.paragraph_builder_group_from_text(Some(self.silhouette.is_some()));
let blur_filter = self.layer_blur_filter(shape);
// Text drop shadows: one filter layer per shadow over fill + stroke
@ -588,8 +607,7 @@ fn render_tree_inner(
} else {
element.fills.as_slice()
};
let silhouette = opts.silhouette.is_some();
render_leaf(shared, canvas, element, fills, scale, silhouette)?;
render_leaf(shared, canvas, element, fills, scale, opts.silhouette)?;
}
}
@ -983,6 +1001,7 @@ fn render_frame(
let mut renderer = VectorRenderer::new(canvas, shared, scale, true);
// The opacity/blend layer above carries the frame's layer blur.
renderer.blur_in_layer = needs_layer;
renderer.silhouette = opts.silhouette;
renderer.draw_fills(element, &element.fills)?;
renderer.draw_fill_inner_shadows(element)?;
canvas.restore();
@ -1040,7 +1059,12 @@ fn render_container_drop_shadows(
// Spread grows every silhouette shape instead of dilating the layer.
let spread = opts.silhouette.unwrap_or(0.0) + shadow.spread.max(0.0);
if draw_fills && !element.fills.is_empty() {
if draw_fills && element.has_only_image_fills() {
canvas.save();
canvas.concat(&element.centered_transform());
draw_image_silhouette(shared, canvas, element, &element.fills, spread)?;
canvas.restore();
} else if draw_fills && !element.fills.is_empty() {
let mut background = Cow::Borrowed(element);
if spread > 0.0 {
background.to_mut().selrect.outset((spread, spread));
@ -1086,12 +1110,19 @@ fn spread_shape(shape: &Shape, spread: f32) -> Cow<'_, Shape> {
/// `shape` as drawn in a container's drop-shadow pass: grown by `spread` and
/// solid black, since fill and stroke opacity don't affect the shadow (GPU).
/// Image-only fills stay (export draws images opaque) and grow in
/// `draw_image_silhouette` instead.
fn silhouette_shape(shape: &Shape, spread: f32) -> Cow<'_, Shape> {
let mut shape = spread_shape(shape, spread);
let image_silhouette = shape.has_only_image_fills();
let mut shape = if image_silhouette {
Cow::Borrowed(shape)
} else {
spread_shape(shape, spread)
};
if shape.has_fills() || !shape.strokes.is_empty() {
let black = Fill::Solid(SolidColor(skia::Color::BLACK));
let shape = shape.to_mut();
if shape.has_fills() {
if shape.has_fills() && !image_silhouette {
shape.clear_fills();
shape.add_fill(black.clone());
}
@ -1112,7 +1143,7 @@ fn render_leaf(
element: &Shape,
fills: &[Fill],
scale: f32,
silhouette: bool,
silhouette: Option<f32>,
) -> Result<()> {
let needs_layer = element.needs_layer();
@ -1200,6 +1231,111 @@ pub(super) fn render_leaf_content<R: ShapeRenderer + ?Sized>(
// Private helpers (canvas-only)
// ---------------------------------------------------------------------------
/// Image spread mask resolution on canvases without pixels (PDF): the PDF
/// raster DPI in points. Every mask is capped to `MAX_SPREAD_MASK_SIZE` pixels
/// per side.
const VECTOR_SPREAD_MASK_PX_PER_UNIT: f32 = 300.0 / 72.0;
const MAX_SPREAD_MASK_SIZE: f32 = 4096.0;
/// Raster canvases get the mask at their own device resolution, which is all
/// the output can show.
fn spread_mask_px_per_unit(canvas: &Canvas) -> f32 {
if canvas.image_info().color_type() == skia::ColorType::Unknown {
return VECTOR_SPREAD_MASK_PX_PER_UNIT;
}
let device_scale = canvas.local_to_device_as_3x3().max_scale();
if device_scale > 0.0 {
device_scale
} else {
VECTOR_SPREAD_MASK_PX_PER_UNIT
}
}
/// Image fills as a drop-shadow silhouette: their alpha, grown by `spread`
/// with the square dilate the GPU uses. Skia's `dilate` runs as SkSL per pixel
/// on CPU canvases, so the mask grows by stamping instead.
fn draw_image_silhouette(
shared: &mut RenderResources,
canvas: &Canvas,
shape: &Shape,
fills: &[Fill],
spread: f32,
) -> Result<()> {
if spread <= 0.0 {
return draw_image_fills(shared, canvas, shape, fills);
}
let mut bounds = shape.selrect;
bounds.outset((spread, spread));
let px_per_unit = spread_mask_px_per_unit(canvas)
.min(MAX_SPREAD_MASK_SIZE / bounds.width().max(bounds.height()));
let width = (bounds.width() * px_per_unit).ceil() as i32;
let height = (bounds.height() * px_per_unit).ceil() as i32;
if width <= 0 || height <= 0 {
return Ok(());
}
let info = skia::ImageInfo::new_a8((width, height));
let Some(mut surface) = skia::surfaces::raster(&info, None, None) else {
return Ok(());
};
{
let mask = surface.canvas();
mask.clear(skia::Color::TRANSPARENT);
mask.scale((px_per_unit, px_per_unit));
mask.translate((-bounds.left, -bounds.top));
draw_image_fills(shared, mask, shape, fills)?;
}
// Square dilate: stamp the mask over itself at ±1, ±2, ±4… px per axis.
// No step goes past one beyond the reach so far, so the union has no gaps.
let radius = (spread * px_per_unit).round() as i32;
surface.canvas().reset_matrix();
for (dx, dy) in [(1, 0), (0, 1)] {
let mut reach = 0;
while reach < radius {
let step = (reach + 1).min(radius - reach);
let snapshot = surface.image_snapshot();
for sign in [-1, 1] {
let offset = ((sign * step * dx) as f32, (sign * step * dy) as f32);
surface.canvas().draw_image(&snapshot, offset, None);
}
reach += step;
}
}
let image = surface.image_snapshot();
let dest = skia::Rect::from_xywh(
bounds.left,
bounds.top,
width as f32 / px_per_unit,
height as f32 / px_per_unit,
);
let mut paint = Paint::default();
paint.set_color(skia::Color::BLACK);
canvas.draw_image_rect_with_sampling_options(
&image,
None,
dest,
skia::SamplingOptions::new(skia::FilterMode::Linear, skia::MipmapMode::None),
&paint,
);
Ok(())
}
fn draw_image_fills(
shared: &mut RenderResources,
canvas: &Canvas,
shape: &Shape,
fills: &[Fill],
) -> Result<()> {
for fill in fills.iter().rev() {
if let Fill::Image(image_fill) = fill {
draw_image_fill(shared, canvas, shape, image_fill, None)?;
}
}
Ok(())
}
fn draw_image_fill(
shared: &mut RenderResources,
canvas: &Canvas,

View File

@ -1842,6 +1842,10 @@ impl Shape {
!self.fills.is_empty()
}
pub fn has_only_image_fills(&self) -> bool {
has_only_image_fills(&self.fills)
}
/// Whether this fill-less leaf paints its group's fills (SVG inheritance,
/// broken by `fill="none"`).
pub fn inherits_fills(&self) -> bool {
@ -2120,16 +2124,21 @@ impl Shape {
/// (`render_direct_frame_drop_shadow`) instead of filter surfaces and
/// descendant silhouettes.
///
/// Requires at least one fill; fill opacity/type does not matter because the fast
/// path shadows the frame geometry as a solid mask.
/// Requires at least one fill; fill opacity does not matter because the fast
/// path shadows the frame geometry as a solid mask. Image-only fills take the
/// slow path so children over transparent image areas still cast.
///
/// The fast path draws fill geometry only. On the slow path, visible strokes also
/// contribute to the shadow silhouette, so frames with outer/center strokes can
/// look slightly narrower here. We keep them eligible anyway for performance.
/// look slightly narrower here.
pub fn uses_direct_container_drop_shadow(&self, tree: ShapesPoolRef) -> bool {
if !self.is_filled_frame() {
return false;
}
// Image fills can have alpha holes; the fast path would skip child silhouettes.
if self.has_only_image_fills() {
return false;
}
if self.blend_mode() != BlendMode::default() {
return false;
}
@ -2149,8 +2158,11 @@ impl Shape {
/// When true, the container's own fill shadow mask is enough and descendant
/// silhouettes can be skipped (same geometry assumption as the direct path).
/// Image-only fills never cover: PNG alpha can leave holes over children.
pub fn container_fill_covers_shadow_descendants(&self, tree: ShapesPoolRef) -> bool {
self.is_filled_frame() && self.descendants_contained_for_frame_shadow(tree, self.selrect())
self.is_filled_frame()
&& !self.has_only_image_fills()
&& self.descendants_contained_for_frame_shadow(tree, self.selrect())
}
fn is_filled_frame(&self) -> bool {
@ -2708,7 +2720,7 @@ mod tests {
}
#[test]
fn frame_with_any_fill_uses_direct_container_drop_shadow() {
fn frame_with_solid_fill_uses_direct_container_drop_shadow() {
for (fill, opacity) in [
(Fill::Solid(SolidColor(skia::Color::WHITE)), 1.0),
(
@ -2722,6 +2734,17 @@ mod tests {
}
}
#[test]
fn frame_with_image_fill_rejects_direct_container_drop_shadow() {
let (pool, frame_id) = frame_with_fill_and_child(
Fill::Image(ImageFill::new(Uuid::new_v4(), 255, 64, 64, true)),
1.0,
);
let frame = pool.get(&frame_id).expect("frame");
assert!(!frame.uses_direct_container_drop_shadow(&pool));
assert!(!frame.container_fill_covers_shadow_descendants(&pool));
}
#[test]
fn clipped_frame_with_child_drop_shadow_rejects_direct_path() {
let (mut pool, frame_id) =

View File

@ -254,6 +254,11 @@ pub enum Fill {
Image(ImageFill),
}
/// Whether `fills` is non-empty and every fill is an image.
pub fn has_only_image_fills(fills: &[Fill]) -> bool {
!fills.is_empty() && fills.iter().all(|fill| matches!(fill, Fill::Image(_)))
}
impl Fill {
pub fn opacity(&self) -> f32 {
match self {