From a2c5160a11339295c6c90480dcc0ec69ee380340 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Thu, 2 Jan 2025 08:45:32 +0100 Subject: [PATCH] wip --- render-wasm/src/shapes/renderable.rs | 66 ++++++++++++++++++---------- render-wasm/src/shapes/strokes.rs | 41 +++++------------ 2 files changed, 52 insertions(+), 55 deletions(-) diff --git a/render-wasm/src/shapes/renderable.rs b/render-wasm/src/shapes/renderable.rs index f681fab30..70a53ff80 100644 --- a/render-wasm/src/shapes/renderable.rs +++ b/render-wasm/src/shapes/renderable.rs @@ -40,10 +40,6 @@ impl Renderable for Shape { ); } - let mut paint = skia::Paint::default(); - paint.set_blend_mode(self.blend_mode.into()); - paint.set_alpha_f(self.opacity); - Ok(()) } @@ -146,26 +142,48 @@ fn render_stroke( .draw_oval(&stroke_rect, &stroke.to_paint(&selrect)); } Kind::Path(path) => { - //TODO - surface.canvas().save(); - let mut stroke_rect = stroke.clone().outer_rect(&selrect); - let mut fill_paint = skia::Paint::default(); - fill_paint.set_blend_mode(skia::BlendMode::SrcOver); - fill_paint.set_anti_alias(true); - surface.canvas().draw_path( - &path.to_skia_path().transform(path_transform.unwrap()), - &fill_paint, - ); - let mut path_paint = skia::Paint::default(); - path_paint.set_blend_mode(skia::BlendMode::SrcOut); - path_paint.set_style(skia::PaintStyle::Stroke); - path_paint.set_stroke_width(2. * stroke.clone().width()); - path_paint.set_anti_alias(true); - surface.canvas().draw_path( - &path.to_skia_path().transform(path_transform.unwrap()), - &path_paint, - ); - surface.canvas().restore(); + match stroke { + Stroke::InnerStroke(attrs) => { + surface.canvas().clip_path( + &path.to_skia_path().transform(path_transform.unwrap()), + skia::ClipOp::Intersect, + true, + ); + + let mut paint_stroke = stroke.to_paint(&selrect); + paint_stroke.set_stroke_width(attrs.width * 2.); + surface.canvas().draw_path( + &path.to_skia_path().transform(path_transform.unwrap()), + &paint_stroke, + ); + } + Stroke::CenterStroke(_) => { + surface.canvas().draw_path( + &path.to_skia_path().transform(path_transform.unwrap()), + &stroke.to_paint(&selrect), + ); + } + Stroke::OuterStroke(attrs) => { + surface.canvas().save(); + + let mut paint_stroke = stroke.to_paint(&selrect); + paint_stroke.set_stroke_width(attrs.width * 2.); + surface.canvas().draw_path( + &path.to_skia_path().transform(path_transform.unwrap()), + &paint_stroke, + ); + + let mut clear_paint = skia::Paint::default(); + clear_paint.set_blend_mode(skia::BlendMode::Clear); + clear_paint.set_anti_alias(true); + + surface.canvas().draw_path( + &path.to_skia_path().transform(path_transform.unwrap()), + &clear_paint, + ); + surface.canvas().restore(); + } + } } } } diff --git a/render-wasm/src/shapes/strokes.rs b/render-wasm/src/shapes/strokes.rs index 1fc7dfd9d..bfee6b2ad 100644 --- a/render-wasm/src/shapes/strokes.rs +++ b/render-wasm/src/shapes/strokes.rs @@ -87,7 +87,7 @@ impl Stroke { rect.width() - attrs.width, rect.height() - attrs.width, ), - Stroke::CenterStroke(attrs) => { + Stroke::CenterStroke(_) => { math::Rect::from_xywh(rect.left, rect.top, rect.width(), rect.height()) } Stroke::OuterStroke(attrs) => math::Rect::from_xywh( @@ -99,27 +99,6 @@ impl Stroke { } } - pub fn outer_path(self, path: &skia::Path) -> skia::Path { - // match self { - // Stroke::InnerStroke(attrs) => math::Rect::from_xywh( - // rect.left + (attrs.width / 2.), - // rect.top + (attrs.width / 2.), - // rect.width() - attrs.width, - // rect.height() - attrs.width, - // ), - // Stroke::CenterStroke(attrs) => { - // math::Rect::from_xywh(rect.left, rect.top, rect.width(), rect.height()) - // } - // Stroke::OuterStroke(attrs) => math::Rect::from_xywh( - // rect.left - (attrs.width / 2.), - // rect.top - (attrs.width / 2.), - // rect.width() + attrs.width, - // rect.height() + attrs.width, - // ), - // } - path.clone() - } - pub fn fill_mut(&mut self) -> &mut Fill { match self { Stroke::InnerStroke(attrs) => &mut attrs.fill, @@ -151,15 +130,15 @@ impl Stroke { } Stroke::CenterStroke(attrs) => { - let mut paint = attrs.fill.to_paint(rect); - paint.set_style(skia::PaintStyle::Stroke); - paint.set_stroke_width(attrs.width); - paint.set_blend_mode(skia::BlendMode::SrcOver); - paint.set_stroke_join(skia::paint::Join::Miter); // Unión en pico - paint.set_stroke_miter(10.0); // Longitud de la muesca para picos más pronunciados - paint.set_stroke_cap(skia::paint::Cap::Butt); // Termina la línea sin agregar espacio adicional - paint - } + let mut paint = attrs.fill.to_paint(rect); + paint.set_style(skia::PaintStyle::Stroke); + paint.set_stroke_width(attrs.width); + paint.set_blend_mode(skia::BlendMode::SrcOver); + paint.set_stroke_join(skia::paint::Join::Miter); // Unión en pico + paint.set_stroke_miter(10.0); // Longitud de la muesca para picos más pronunciados + paint.set_stroke_cap(skia::paint::Cap::Butt); // Termina la línea sin agregar espacio adicional + paint + } Stroke::OuterStroke(attrs) => { let mut paint = attrs.fill.to_paint(rect); paint.set_style(skia::PaintStyle::Stroke);