diff --git a/frontend/playwright/ui/render-wasm-specs/shapes.spec.js b/frontend/playwright/ui/render-wasm-specs/shapes.spec.js index 1026bcc4a1..3c3311f4f2 100644 --- a/frontend/playwright/ui/render-wasm-specs/shapes.spec.js +++ b/frontend/playwright/ui/render-wasm-specs/shapes.spec.js @@ -305,3 +305,19 @@ test("Renders a clipped frame with a large blur drop shadow", async ({ await expect(workspace.canvas).toHaveScreenshot(); }); + +test("Renders a file with solid, dotted, dashed and mixed stroke styles", async ({ + page, +}) => { + const workspace = new WasmWorkspacePage(page); + await workspace.setupEmptyFile(); + await workspace.mockGetFile("render-wasm/get-file-stroke-styles.json"); + + await workspace.goToWorkspace({ + id: "b888b894-3697-80d3-8006-51cc8a55c200", + pageId: "b888b894-3697-80d3-8006-51cc8a55c210", + }); + await workspace.waitForFirstRenderWithoutUI(); + + await expect(workspace.canvas).toHaveScreenshot(); +}); diff --git a/frontend/playwright/ui/render-wasm-specs/shapes.spec.js-snapshots/Renders-a-file-with-solid-dotted-dashed-and-mixed-stroke-styles-1.png b/frontend/playwright/ui/render-wasm-specs/shapes.spec.js-snapshots/Renders-a-file-with-solid-dotted-dashed-and-mixed-stroke-styles-1.png new file mode 100644 index 0000000000..f23422a736 Binary files /dev/null and b/frontend/playwright/ui/render-wasm-specs/shapes.spec.js-snapshots/Renders-a-file-with-solid-dotted-dashed-and-mixed-stroke-styles-1.png differ diff --git a/render-wasm/src/render/strokes.rs b/render-wasm/src/render/strokes.rs index 0d7797b8fb..94375c6d93 100644 --- a/render-wasm/src/render/strokes.rs +++ b/render-wasm/src/render/strokes.rs @@ -26,7 +26,7 @@ fn draw_stroke_on_rect( // Draw the different kind of strokes for a rect is straightforward, we just need apply a stroke to: // - The same rect if it's a center stroke // - A bigger rect if it's an outer stroke - // - A smaller rect if it's an outer stroke + // - A smaller rect if it's an inner stroke let stroke_rect = stroke.aligned_rect(rect, scale); let mut paint = stroke.to_paint(selrect, svg_attrs, antialias); @@ -34,7 +34,9 @@ fn draw_stroke_on_rect( let filter = compose_filters(blur, shadow); paint.set_image_filter(filter); - match corners { + // By default just draw the rect. Only dotted inner/outer strokes need + // clipping to prevent the dotted pattern from appearing in wrong areas. + let draw_stroke = || match corners { Some(radii) => { let radii = stroke.outer_corners(radii); let rrect = RRect::new_rect_radii(stroke_rect, &radii); @@ -43,6 +45,24 @@ fn draw_stroke_on_rect( None => { canvas.draw_rect(stroke_rect, &paint); } + }; + + if let Some(clip_op) = stroke.clip_op() { + let layer_rec = skia::canvas::SaveLayerRec::default().paint(&paint); + canvas.save_layer(&layer_rec); + match corners { + Some(radii) => { + let rrect = RRect::new_rect_radii(*rect, radii); + canvas.clip_rrect(rrect, clip_op, antialias); + } + None => { + canvas.clip_rect(*rect, clip_op, antialias); + } + } + draw_stroke(); + canvas.restore(); + } else { + draw_stroke(); } } @@ -62,7 +82,7 @@ fn draw_stroke_on_circle( // Draw the different kind of strokes for an oval is straightforward, we just need apply a stroke to: // - The same oval if it's a center stroke // - A bigger oval if it's an outer stroke - // - A smaller oval if it's an outer stroke + // - A smaller oval if it's an inner stroke let stroke_rect = stroke.aligned_rect(rect, scale); let mut paint = stroke.to_paint(selrect, svg_attrs, antialias); @@ -70,7 +90,19 @@ fn draw_stroke_on_circle( let filter = compose_filters(blur, shadow); paint.set_image_filter(filter); - canvas.draw_oval(stroke_rect, &paint); + // By default just draw the circle. Only dotted inner/outer strokes need + // clipping to prevent the dotted pattern from appearing in wrong areas. + if let Some(clip_op) = stroke.clip_op() { + let layer_rec = skia::canvas::SaveLayerRec::default().paint(&paint); + canvas.save_layer(&layer_rec); + let mut clip_path = skia::Path::new(); + clip_path.add_oval(rect, None); + canvas.clip_path(&clip_path, clip_op, antialias); + canvas.draw_oval(stroke_rect, &paint); + canvas.restore(); + } else { + canvas.draw_oval(stroke_rect, &paint); + } } fn draw_outer_stroke_path( diff --git a/render-wasm/src/shapes/strokes.rs b/render-wasm/src/shapes/strokes.rs index e45c011a14..599cd83f3d 100644 --- a/render-wasm/src/shapes/strokes.rs +++ b/render-wasm/src/shapes/strokes.rs @@ -119,6 +119,19 @@ impl Stroke { self.width *= value; } + /// Returns the clip operation for dotted inner/outer strokes. + /// Returns `None` when no clipping is needed (center or non-dotted). + pub fn clip_op(&self) -> Option { + if self.style != StrokeStyle::Dotted || self.kind == StrokeKind::Center { + return None; + } + match self.kind { + StrokeKind::Inner => Some(skia::ClipOp::Intersect), + StrokeKind::Outer => Some(skia::ClipOp::Difference), + StrokeKind::Center => None, + } + } + pub fn delta(&self) -> f32 { match self.kind { StrokeKind::Inner => 0., @@ -128,20 +141,28 @@ impl Stroke { } pub fn outer_rect(&self, rect: &Rect) -> Rect { - match self.kind { - StrokeKind::Inner => Rect::from_xywh( - rect.left + (self.width / 2.), - rect.top + (self.width / 2.), - rect.width() - self.width, - rect.height() - self.width, - ), - StrokeKind::Center => Rect::from_xywh(rect.left, rect.top, rect.width(), rect.height()), - StrokeKind::Outer => Rect::from_xywh( - rect.left - (self.width / 2.), - rect.top - (self.width / 2.), - rect.width() + self.width, - rect.height() + self.width, - ), + match (self.kind, self.style) { + (StrokeKind::Inner, StrokeStyle::Dotted) | (StrokeKind::Outer, StrokeStyle::Dotted) => { + // Boundary so circles center on it and semicircles match after clipping + *rect + } + _ => match self.kind { + StrokeKind::Inner => Rect::from_xywh( + rect.left + (self.width / 2.), + rect.top + (self.width / 2.), + rect.width() - self.width, + rect.height() - self.width, + ), + StrokeKind::Center => { + Rect::from_xywh(rect.left, rect.top, rect.width(), rect.height()) + } + StrokeKind::Outer => Rect::from_xywh( + rect.left - (self.width / 2.), + rect.top - (self.width / 2.), + rect.width() + self.width, + rect.height() + self.width, + ), + }, } } @@ -155,6 +176,11 @@ impl Stroke { } pub fn outer_corners(&self, corners: &Corners) -> Corners { + if matches!(self.style, StrokeStyle::Dotted | StrokeStyle::Dashed) { + // Path at boundary so no corner offset + return *corners; + } + let offset = match self.kind { StrokeKind::Center => 0.0, StrokeKind::Inner => -self.width / 2.0,