Elena Torró 7f0b62b994
🐛 Keep drop shadows out from under translucent shapes (#12904)
* 🐛 Keep drop shadows out from under translucent shapes at every zoom

Cut the shape out of its drop shadow, as CSS box-shadow does, and stop
large shadows from being cut off at high zoom.

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

* 🐛 Keep drop shadows out from under translucent shapes in SVG and PDF

Match the canvas: clip the shape out of its drop shadow on export.

AI-assisted-by: claude-opus-5-5
2026-11-10 15:05:08 +01:00

471 lines
14 KiB
Rust

use skia_safe::{self as skia, Paint, Rect};
pub use super::Color;
use crate::utils::get_image;
use crate::uuid::Uuid;
#[derive(Debug, Clone, PartialEq)]
pub struct Gradient {
start: (f32, f32),
end: (f32, f32),
opacity: u8,
width: f32,
colors: Vec<Color>,
offsets: Vec<f32>,
}
impl Gradient {
pub fn new(
start: (f32, f32),
end: (f32, f32),
opacity: u8,
width: f32,
stops: &[(Color, f32)],
) -> Self {
let mut gradient = Gradient {
start,
end,
opacity,
colors: vec![],
offsets: vec![],
width,
};
gradient.add_stops(stops);
gradient
}
fn add_stops(&mut self, stops: &[(Color, f32)]) {
let colors = stops.iter().map(|(color, _)| *color);
let offsets = stops.iter().map(|(_, offset)| *offset);
self.colors.extend(colors);
self.offsets.extend(offsets);
}
pub fn start(&self) -> (f32, f32) {
self.start
}
pub fn end(&self) -> (f32, f32) {
self.end
}
pub fn opacity(&self) -> u8 {
self.opacity
}
pub fn width(&self) -> f32 {
self.width
}
pub fn stops(&self) -> impl Iterator<Item = (Color, f32)> + '_ {
self.colors
.iter()
.copied()
.zip(self.offsets.iter().copied())
}
pub fn to_linear_shader(&self, rect: &Rect) -> Option<skia::Shader> {
let start = (
rect.left + self.start.0 * rect.width(),
rect.top + self.start.1 * rect.height(),
);
let end = (
rect.left + self.end.0 * rect.width(),
rect.top + self.end.1 * rect.height(),
);
let colors4f: Vec<skia::Color4f> = self
.colors
.iter()
.map(|c| skia::Color4f::from(*c))
.collect();
let colors = skia::gradient::Colors::new(
&colors4f,
Some(self.offsets.as_slice()),
skia::TileMode::Clamp,
None,
);
let gradient =
skia::gradient::Gradient::new(colors, skia::gradient::Interpolation::default());
skia::shaders::linear_gradient((start, end), &gradient, None)
}
pub fn to_radial_shader(&self, rect: &Rect) -> Option<skia::Shader> {
let center = skia::Point::new(
rect.left + self.start.0 * rect.width(),
rect.top + self.start.1 * rect.height(),
);
let end = skia::Point::new(
rect.left + self.end.0 * rect.width(),
rect.top + self.end.1 * rect.height(),
);
let direction = end - center;
let distance = (direction.x.powi(2) + direction.y.powi(2)).sqrt();
let angle = direction.y.atan2(direction.x).to_degrees();
// Based on the code from frontend/src/app/main/ui/shapes/gradients.cljs
let mut transform = skia::Matrix::new_identity();
transform.pre_translate((center.x, center.y));
transform.pre_rotate(angle + 90., skia::Point::new(0., 0.));
// We need an extra transform, because in skia radial gradients are circular and we need them to be ellipses if they must adapt to the shape
transform.pre_scale((self.width * rect.width() / rect.height(), 1.), None);
transform.pre_translate((-center.x, -center.y));
let colors4f: Vec<skia::Color4f> = self
.colors
.iter()
.map(|c| skia::Color4f::from(*c))
.collect();
let colors = skia::gradient::Colors::new(
&colors4f,
Some(self.offsets.as_slice()),
skia::TileMode::Clamp,
None,
);
let gradient =
skia::gradient::Gradient::new(colors, skia::gradient::Interpolation::default());
skia::shaders::radial_gradient((center, distance), &gradient, Some(&transform))
}
}
#[derive(Debug, Clone, PartialEq, Copy)]
pub struct ImageFillTransform {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImageFill {
id: Uuid,
opacity: u8,
width: i32,
height: i32,
keep_aspect_ratio: bool,
transform: Option<ImageFillTransform>,
// Opaque media type code, kept so the frontend can read it back.
mtype: u16,
}
impl ImageFill {
pub fn new(id: Uuid, opacity: u8, width: i32, height: i32, keep_aspect_ratio: bool) -> Self {
Self {
id,
opacity,
width,
height,
keep_aspect_ratio,
transform: None,
mtype: 0,
}
}
pub fn new_with_transform(
id: Uuid,
opacity: u8,
width: i32,
height: i32,
keep_aspect_ratio: bool,
transform: Option<ImageFillTransform>,
) -> Self {
Self {
id,
opacity,
width,
height,
keep_aspect_ratio,
transform,
mtype: 0,
}
}
pub fn with_mtype(self, mtype: u16) -> Self {
Self { mtype, ..self }
}
pub fn id(&self) -> Uuid {
self.id
}
pub fn opacity(&self) -> u8 {
self.opacity
}
pub fn keep_aspect_ratio(&self) -> bool {
self.keep_aspect_ratio
}
pub fn width(&self) -> i32 {
self.width
}
pub fn height(&self) -> i32 {
self.height
}
pub fn mtype(&self) -> u16 {
self.mtype
}
pub fn transform(&self) -> Option<&ImageFillTransform> {
self.transform.as_ref()
}
/// The part of `container` the image is laid over: its transform, else all of it.
pub fn dest_rect(&self, container: &Rect) -> Rect {
match &self.transform {
Some(tf) => Rect::from_xywh(
container.left + tf.x * container.width(),
container.top + tf.y * container.height(),
tf.width * container.width(),
tf.height * container.height(),
),
None => *container,
}
}
/// Where the image is drawn to cover its dest rect in `container`, centered.
pub fn cover_rect(&self, container: &Rect) -> Rect {
let dest = self.dest_rect(container);
let image_width = self.width as f32;
let image_height = self.height as f32;
let scale = (dest.width() / image_width).max(dest.height() / image_height);
let scaled_width = image_width * scale;
let scaled_height = image_height * scale;
Rect::from_xywh(
dest.left - (scaled_width - dest.width()) / 2.0,
dest.top - (scaled_height - dest.height()) / 2.0,
scaled_width,
scaled_height,
)
}
}
#[derive(Debug, Clone, PartialEq, Copy)]
pub struct SolidColor(pub Color);
#[derive(Debug, Clone, PartialEq)]
pub enum Fill {
Solid(SolidColor),
LinearGradient(Gradient),
RadialGradient(Gradient),
Image(ImageFill),
}
impl Fill {
pub fn opacity(&self) -> f32 {
match self {
Fill::Solid(SolidColor(color)) => color.a() as f32 / 255.0,
Fill::LinearGradient(g) => g.opacity as f32 / 255.0,
Fill::RadialGradient(g) => g.opacity as f32 / 255.0,
Fill::Image(i) => i.opacity as f32 / 255.0,
}
}
pub fn is_opaque(&self) -> bool {
match self {
Fill::Solid(SolidColor(color)) => color.a() == 255,
Fill::LinearGradient(g) | Fill::RadialGradient(g) => {
g.opacity == 255 && g.colors.iter().all(|c| c.a() == 255)
}
Fill::Image(i) => i.opacity == 255,
}
}
pub fn with_full_opacity(&self) -> Fill {
match self {
Fill::Solid(SolidColor(color)) => Fill::Solid(SolidColor(skia::Color::from_argb(
255,
color.r(),
color.g(),
color.b(),
))),
Fill::LinearGradient(g) => Fill::LinearGradient(Gradient {
opacity: 255,
..g.clone()
}),
Fill::RadialGradient(g) => Fill::RadialGradient(Gradient {
opacity: 255,
..g.clone()
}),
Fill::Image(i) => Fill::Image(ImageFill {
opacity: 255,
..i.clone()
}),
}
}
pub fn to_paint(&self, rect: &Rect, anti_alias: bool) -> skia::Paint {
match self {
Self::Solid(SolidColor(color)) => {
let mut p = skia::Paint::default();
p.set_color(*color);
p.set_style(skia::PaintStyle::Fill);
p.set_anti_alias(anti_alias);
p.set_blend_mode(skia::BlendMode::SrcOver);
p
}
Self::LinearGradient(gradient) => {
let mut p = skia::Paint::default();
p.set_shader(gradient.to_linear_shader(rect));
p.set_alpha(gradient.opacity);
p.set_style(skia::PaintStyle::Fill);
p.set_anti_alias(anti_alias);
p.set_blend_mode(skia::BlendMode::SrcOver);
p
}
Self::RadialGradient(gradient) => {
let mut p = skia::Paint::default();
p.set_shader(gradient.to_radial_shader(rect));
p.set_alpha(gradient.opacity);
p.set_style(skia::PaintStyle::Fill);
p.set_anti_alias(anti_alias);
p.set_blend_mode(skia::BlendMode::SrcOver);
p
}
Self::Image(image_fill) => {
let mut p = skia::Paint::default();
p.set_style(skia::PaintStyle::Fill);
p.set_anti_alias(anti_alias);
p.set_blend_mode(skia::BlendMode::SrcOver);
p.set_alpha(image_fill.opacity);
p
}
}
}
}
pub fn get_fill_shader(fill: &Fill, bounding_box: &Rect) -> Option<skia::Shader> {
match fill {
Fill::Solid(SolidColor(color)) => Some(skia::shaders::color(*color)),
Fill::LinearGradient(gradient) => gradient.to_linear_shader(bounding_box),
Fill::RadialGradient(gradient) => gradient.to_radial_shader(bounding_box),
Fill::Image(image_fill) => {
let mut image_shader = None;
let image = get_image(&image_fill.id);
if let Some(image) = image {
let sampling_options =
skia::SamplingOptions::new(skia::FilterMode::Linear, skia::MipmapMode::Nearest);
// Covers the dest rect (transform, else the whole box), centered.
// keep_aspect_ratio is not applied: the image always covers.
let tile_modes = (skia::TileMode::Clamp, skia::TileMode::Clamp);
let cover = image_fill.cover_rect(bounding_box);
let scale = cover.width() / image_fill.width as f32;
let mut matrix = skia::Matrix::new_identity();
matrix.pre_translate((cover.left, cover.top));
matrix.pre_scale((scale, scale), None);
let opacity = image_fill.opacity();
let alpha_color = skia::Color4f::new(1.0, 1.0, 1.0, opacity as f32 / 255.0);
let alpha_shader = skia::shaders::color(alpha_color.to_color());
image_shader = image.to_shader(tile_modes, sampling_options, &matrix);
if let Some(shader) = image_shader {
image_shader = Some(skia::shaders::blend(
skia::Blender::mode(skia::BlendMode::DstIn),
shader,
alpha_shader,
));
}
}
image_shader
}
}
}
pub fn merge_fills(fills: &[Fill], bounding_box: Rect) -> skia::Paint {
let mut fills_paint = skia::Paint::default();
if fills.is_empty() {
fills_paint.set_color(skia::Color::TRANSPARENT);
return fills_paint;
}
if fills.len() == 1 {
if let Fill::Solid(SolidColor(color)) = &fills[0] {
fills_paint.set_color(*color);
return fills_paint;
}
}
let mut combined_shader: Option<skia::Shader> = None;
for fill in fills {
let shader = get_fill_shader(fill, &bounding_box);
if let Some(shader) = shader {
combined_shader = match combined_shader {
// Use SrcOver and treat the newly encountered fill as the source (top),
// overlaying it over the previously composed shader (destination/bottom).
// This avoids edge bleed from underlying fills when anti-aliasing causes
// fractional coverage at shape boundaries.
Some(existing_shader) => Some(skia::shaders::blend(
skia::Blender::mode(skia::BlendMode::SrcOver),
shader,
existing_shader,
)),
None => Some(shader),
};
}
}
fills_paint.set_shader(combined_shader);
fills_paint
}
pub fn set_paint_fill(paint: &mut Paint, fill: &Fill, bounding_box: &Rect, remove_alpha: bool) {
if remove_alpha {
paint.set_color(skia::Color::BLACK);
paint.set_alpha(255);
return;
}
let shader = get_fill_shader(fill, bounding_box);
if let Some(shader) = shader {
paint.set_shader(shader);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn image_fill(transform: Option<ImageFillTransform>) -> ImageFill {
ImageFill::new_with_transform(Uuid::nil(), 255, 100, 50, true, transform)
}
#[test]
fn an_image_without_transform_covers_the_whole_container() {
let container = Rect::from_xywh(10.0, 20.0, 100.0, 100.0);
assert_eq!(image_fill(None).dest_rect(&container), container);
// 100x50 scaled by 2 to fill the height, centered horizontally.
assert_eq!(
image_fill(None).cover_rect(&container),
Rect::from_xywh(-40.0, 20.0, 200.0, 100.0)
);
}
#[test]
fn an_image_with_transform_covers_its_part_of_the_container() {
let container = Rect::from_xywh(10.0, 20.0, 200.0, 100.0);
let fill = image_fill(Some(ImageFillTransform {
x: 0.5,
y: -0.5,
width: 0.5,
height: 1.0,
}));
assert_eq!(
fill.dest_rect(&container),
Rect::from_xywh(110.0, -30.0, 100.0, 100.0)
);
assert_eq!(
fill.cover_rect(&container),
Rect::from_xywh(60.0, -30.0, 200.0, 100.0)
);
}
}