mirror of
https://github.com/penpot/penpot.git
synced 2025-12-11 22:14:05 +01:00
* ♻️ Move shape type serialization to wasm module * ♻️ Refactor serialization of constraints and vertical alignment into wasm module * ♻️ Refactor serialization and model of shape blur * ♻️ Refactor bool serialization to the wasm module * ♻️ Split wasm::layout into submodules * ♻️ Refactor serialization of AlignItems, AlignContent, JustifyItems and JustifyContent * ♻️ Refactor serialization of WrapType and FlexDirection * ♻️ Refactor serialization of JustifySelf * ♻️ Refactor serialization of GridCell * ♻️ Refactor serialization of AlignSelf * 🐛 Fix AlignSelf not being serialized * ♻️ Refactor handling of None variants in Raw* enums * ♻️ Refactor serialization of grid direction * ♻️ Refactor serialization of GridTrack and GridTrackType * ♻️ Refactor serialization of Sizing * ♻️ Refactor serialization of ShadowStyle * ♻️ Refactor serialization of StrokeCap and StrokeStyle * ♻️ Refactor serialization of BlendMode * ♻️ Refactor serialization of FontStyle * ♻️ Refactor serialization of GrowType
118 lines
2.7 KiB
Rust
118 lines
2.7 KiB
Rust
use skia_safe::{self as skia, image_filters, ImageFilter, Paint};
|
|
|
|
use super::Color;
|
|
use crate::render::filters::compose_filters;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
pub enum ShadowStyle {
|
|
Drop,
|
|
Inner,
|
|
}
|
|
|
|
impl Default for ShadowStyle {
|
|
fn default() -> Self {
|
|
Self::Drop
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
pub struct Shadow {
|
|
pub color: Color,
|
|
pub blur: f32,
|
|
pub spread: f32,
|
|
pub offset: (f32, f32),
|
|
style: ShadowStyle,
|
|
hidden: bool,
|
|
}
|
|
|
|
impl Shadow {
|
|
pub fn new(
|
|
color: Color,
|
|
blur: f32,
|
|
spread: f32,
|
|
offset: (f32, f32),
|
|
style: ShadowStyle,
|
|
hidden: bool,
|
|
) -> Self {
|
|
Self {
|
|
color,
|
|
blur,
|
|
spread,
|
|
offset,
|
|
style,
|
|
hidden,
|
|
}
|
|
}
|
|
|
|
pub fn style(&self) -> ShadowStyle {
|
|
self.style
|
|
}
|
|
|
|
pub fn hidden(&self) -> bool {
|
|
self.hidden
|
|
}
|
|
|
|
pub fn get_drop_shadow_filter(&self) -> Option<ImageFilter> {
|
|
let mut filter = image_filters::drop_shadow_only(
|
|
(self.offset.0, self.offset.1),
|
|
(self.blur, self.blur),
|
|
self.color,
|
|
None,
|
|
None,
|
|
None,
|
|
);
|
|
|
|
if self.spread > 0. {
|
|
filter = image_filters::dilate((self.spread, self.spread), filter, None);
|
|
}
|
|
|
|
filter
|
|
}
|
|
|
|
pub fn get_inner_shadow_paint(
|
|
&self,
|
|
antialias: bool,
|
|
blur_filter: Option<&ImageFilter>,
|
|
) -> Paint {
|
|
let mut paint = Paint::default();
|
|
let shadow_filter = self.get_inner_shadow_filter();
|
|
let filter = compose_filters(blur_filter, shadow_filter.as_ref());
|
|
paint.set_image_filter(filter);
|
|
paint.set_anti_alias(antialias);
|
|
paint
|
|
}
|
|
|
|
pub fn get_inner_shadow_filter(&self) -> Option<ImageFilter> {
|
|
let sigma = self.blur * 0.5;
|
|
let mut filter = skia::image_filters::drop_shadow_only(
|
|
(self.offset.0, self.offset.1), // DPR?
|
|
(sigma, sigma),
|
|
skia::Color::WHITE,
|
|
None,
|
|
None,
|
|
None,
|
|
);
|
|
|
|
filter = skia::image_filters::color_filter(
|
|
skia::color_filters::blend(self.color, skia::BlendMode::SrcOut).unwrap(),
|
|
filter,
|
|
None,
|
|
);
|
|
|
|
if self.spread > 0. {
|
|
filter = skia::image_filters::dilate((self.spread, self.spread), filter, None);
|
|
}
|
|
|
|
filter = skia::image_filters::blend(skia::BlendMode::SrcIn, None, filter, None);
|
|
|
|
filter
|
|
}
|
|
|
|
pub fn scale_content(&mut self, value: f32) {
|
|
self.blur *= value;
|
|
self.spread *= value;
|
|
self.offset.0 *= value;
|
|
self.offset.1 *= value;
|
|
}
|
|
}
|