From 100ff983f5fa2fdba625aee6c7400b8e72669913 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 25 Oct 2026 15:17:44 +0100 Subject: [PATCH] fix(engine): turn a shape's gradient stroke with the canvas (#9239) (#9240) Image Rotation, rotated Crop, canvas flips and Image Size mapped a shape layer's gradient fill through map_fill but never its stroke paint, so a gradient stroke kept its old angle and offset while the fill turned. Co-authored-by: Claude Opus 5.5 --- crates/engine/src/canvas_geom.rs | 3 +++ crates/engine/src/canvas_geom/tests.rs | 32 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/crates/engine/src/canvas_geom.rs b/crates/engine/src/canvas_geom.rs index 77ac5a2d06..0a4ed6faa4 100644 --- a/crates/engine/src/canvas_geom.rs +++ b/crates/engine/src/canvas_geom.rs @@ -137,6 +137,9 @@ pub(crate) fn transform_layer_geometry(l: &mut Layer, a: &Affine, content: bool, if let Some(f) = &mut sh.fill { map_fill(a, f); } + if let Some(st) = &mut sh.stroke { + map_fill(a, &mut st.paint); + } if content { crate::vector_cmds::transform_shape(sh, a); } diff --git a/crates/engine/src/canvas_geom/tests.rs b/crates/engine/src/canvas_geom/tests.rs index 2ba0dfbd66..473a82e062 100644 --- a/crates/engine/src/canvas_geom/tests.rs +++ b/crates/engine/src/canvas_geom/tests.rs @@ -221,6 +221,38 @@ fn gradient_fill_offset_stays_a_fraction_of_the_frame() { assert!((offset.0 - 0.1).abs() < 1e-6 && (offset.1 - 0.25).abs() < 1e-6, "{offset:?}"); } +/// The shape's (fill, stroke) gradient angles after `cmd` on a shape whose fill and stroke both +/// start as a 0° (left to right) gradient. +fn shape_gradient_angles_after(cmd: &str, p: serde_json::Value) -> (f32, f32) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": W, "height": H})).unwrap(); + let g = json!({"gradient": {"stops": [[0, "#ff0000"], [1, "#0000ff"]], "angle": 0}}); + s.execute("shape.create", json!({"kind": "rect", "rect": [10, 8, 30, 20], "fill": g, "stroke": {"width": 6, "fill": g}})).unwrap(); + s.execute(cmd, p).unwrap(); + let angle = |f: &Fill| if let Fill::Gradient { angle, .. } = f { *angle } else { panic!("not a gradient: {f:?}") }; + let sh = doc(&s).walk().into_iter().find_map(|(_, _, l)| if let LayerContent::Shape(sh) = &l.content { Some(sh.clone()) } else { None }).unwrap(); + (angle(sh.fill.as_ref().unwrap()), angle(&sh.stroke.as_ref().unwrap().paint)) +} + +/// A gradient stroke turns and mirrors with the canvas like the fill does (#9239). +#[test] +fn shape_gradient_stroke_turns_with_the_canvas() { + let same = |a: f32, b: f32| { + let d = (a - b).rem_euclid(360.0); + d < 1e-3 || d > 360.0 - 1e-3 + }; + for (cmd, p, want) in [ + ("image.imageRotation.90cw", json!({}), -90.0), + ("image.imageRotation.flipCanvasHorizontal", json!({}), 180.0), + ("image.imageRotation.flipCanvasVertical", json!({}), 0.0), + ("image.rotation.arbitrary", json!({"angle": 30, "direction": "cw"}), -30.0), + ] { + let (fill, stroke) = shape_gradient_angles_after(cmd, p); + assert!(same(fill, want), "{cmd}: fill {fill}, want {want}"); + assert!(same(stroke, want), "{cmd}: stroke {stroke}, want {want}"); + } +} + fn type_and_shape_origin(d: &Document) -> ([f64; 6], [f64; 2]) { let mut t = None; let mut k = None;