Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix shadow rendering for tiny-skia backend #2715

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions core/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ impl Default for Quad {
}
}

impl Quad {
/// Calculates bounds area with [`Shadow`] expandation.
pub fn bounds_with_shadow(&self) -> Rectangle {
let shadow = self.shadow;

if shadow.color.a > 0.0 {
Rectangle {
x: self.bounds.x + shadow.offset.x - shadow.blur_radius,
y: self.bounds.y + shadow.offset.y - shadow.blur_radius,
width: self.bounds.width + shadow.blur_radius * 2.0,
height: self.bounds.height + shadow.blur_radius * 2.0,
}
} else {
self.bounds
}
}
}

/// The styling attributes of a [`Renderer`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Style {
Expand Down
19 changes: 8 additions & 11 deletions tiny_skia/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ impl Engine {
quad.bounds.height.is_normal(),
"Quad with non-normal height!"
);
let physical_bounds_with_shadow =
quad.bounds_with_shadow() * transformation;

let physical_bounds = quad.bounds * transformation;

if !clip_bounds.intersects(&physical_bounds) {
if !clip_bounds.intersects(&physical_bounds_with_shadow) {
return;
}

let clip_mask = (!physical_bounds.is_within(&clip_bounds))
let clip_mask = (!physical_bounds_with_shadow.is_within(&clip_bounds))
.then_some(clip_mask as &_);

let physical_bounds = quad.bounds * transformation;

let transform = into_transform(transformation);

// Make sure the border radius is not larger than the bounds
Expand All @@ -76,12 +78,7 @@ impl Engine {
let shadow = quad.shadow;

if shadow.color.a > 0.0 {
let shadow_bounds = Rectangle {
x: quad.bounds.x + shadow.offset.x - shadow.blur_radius,
y: quad.bounds.y + shadow.offset.y - shadow.blur_radius,
width: quad.bounds.width + shadow.blur_radius * 2.0,
height: quad.bounds.height + shadow.blur_radius * 2.0,
} * transformation;
let shadow_bounds = quad.bounds_with_shadow() * transformation;

let radii = fill_border_radius
.into_iter()
Expand Down Expand Up @@ -148,7 +145,7 @@ impl Engine {
pixmap.as_ref(),
&tiny_skia::PixmapPaint::default(),
tiny_skia::Transform::default(),
None,
clip_mask,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tiny_skia/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Layer {
&previous.quads,
&current.quads,
|(quad, _)| {
quad.bounds
quad.bounds_with_shadow()
.expand(1.0)
.intersection(&current.bounds)
.into_iter()
Expand Down
Loading