PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Shapes: a soft shape's transform frame follows its path (#5846)
David Mann24a77a18c000
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rsindex 50d248d3c..5c41a75ea 100644--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@ -867,18 +867,24 @@ pub fn paint_bounds(layer: &Layer) -> Option<Rect> {(!r.is_empty()).then_some(r)}-/// The bounding box of a shape layer's vector content: its path control bounds plus stroke width-/// (and the reach of a soft fill).-pub fn shape_bounds(sh: &photocraft_doc::ShapeLayer) -> Option<Rect> {+/// The bounding box of a shape layer's path and stroke, ignoring its Density and Feather: its+/// path control bounds plus stroke width. It is the frame Free Transform and the Move tool's+/// transform controls show for a soft shape, whose rendering reaches past it (in Photoshop a+/// shape's transform frame follows its path whatever its mask Density and Feather).+pub fn hard_shape_bounds(sh: &photocraft_doc::ShapeLayer) -> Option<Rect> {if sh.fill.is_none() && sh.stroke.as_ref().is_none_or(|s| s.width <= 0.0 || s.opacity <= 0.0) {return None;}let (x0, y0, x1, y1) = sh.path.control_bounds()?;let stroke_margin = sh.stroke.as_ref().filter(|s| s.opacity > 0.0).map_or(0.0, |s| s.width.ceil()) as i32;let r = Rect::new(x0.floor() as i32, y0.floor() as i32, x1.ceil() as i32, y1.ceil() as i32).inflate(stroke_margin);- if r.is_empty() {- return None;- }+ (!r.is_empty()).then_some(r)+}++/// The bounding box of a shape layer's vector content: its path control bounds plus stroke width+/// (and the reach of a soft fill).+pub fn shape_bounds(sh: &photocraft_doc::ShapeLayer) -> Option<Rect> {+ let r = hard_shape_bounds(sh)?;// A soft fill reaches past the path: by its feather, or everywhere below 100 % density.if sh.fill.is_some() && sh.has_soft_mask() {if sh.density.is_finite() && sh.density < 1.0 {diff --git a/crates/engine/src/transform_cmds.rs b/crates/engine/src/transform_cmds.rsindex 1c0153a42..baab06980 100644--- a/crates/engine/src/transform_cmds.rs+++ b/crates/engine/src/transform_cmds.rs@@ -57,6 +57,9 @@ fn framed_by_selection(doc: &Document, bounds: Rect) -> Rect {fn whole_layer_bounds(layer: &Layer) -> Rect {let content = match &layer.content {LayerContent::Group(g) => g.children.iter().map(whole_layer_bounds).fold(Rect::EMPTY, |a, b| a.union(&b)),+ // A soft shape (Density below 100 % or a Feather) renders past its path, over the whole+ // canvas below 100 %: its frame follows the path and stroke, as in Photoshop (#5846).+ LayerContent::Shape(sh) if sh.has_soft_mask() => photocraft_compose::hard_shape_bounds(sh).unwrap_or(Rect::EMPTY),_ => layer.surface().map_or(Rect::EMPTY, photocraft_compose::bounds::content_bounds),};if content.is_empty() { layer.mask.as_ref().map_or(Rect::EMPTY, |m| photocraft_compose::bounds::content_bounds(&m.surface)) } else { content }@@ -1423,4 +1426,42 @@ mod tests {assert!(s.execute("edit.transform", bad.clone()).is_err(), "{bad}");}}++ /// #5846: a shape's Density and Feather render past its path (over the whole canvas below+ /// 100 %), but its transform frame follows the path, as in Photoshop: the same box as the hard+ /// shape for Free Transform, the Move tool's controls and snapping, never the canvas.+ #[test]+ fn a_soft_shapes_frame_follows_its_path() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 200, "height": 200})).unwrap();+ let id = s.execute("shape.create", json!({"kind": "rect", "rect": [20, 30, 60, 40], "fill": "#000000"})).unwrap()["layer"].as_u64().unwrap();+ let id = photocraft_doc::LayerId(id);+ let boxes = |s: &Session| {+ let st = s.active().unwrap();+ let l = st.doc.layer(id).unwrap();+ (transform_bounds(&st.doc, l), transform_frame(&st.doc, l), crate::snap::layer_rect(&st.doc, id))+ };+ let hard = boxes(&s);+ assert_eq!(hard.0, Rect::new(20, 30, 80, 70), "the hard shape's frame");+ s.execute("layer.vectorMask.edit", json!({"density": 60, "feather": 20})).unwrap();+ {+ let st = s.active().unwrap();+ let LayerContent::Shape(sh) = &st.doc.layer(id).unwrap().content else { panic!("a shape") };+ let painted = photocraft_compose::bounds::content_bounds(sh.cache.as_ref().unwrap());+ assert_eq!(painted, st.doc.bounds(), "the soft fill renders over the whole canvas");+ }+ let soft = boxes(&s);+ assert_eq!(soft, hard, "the frame ignores Density and Feather");+ assert_ne!(soft.1, s.active().unwrap().doc.bounds());+ // Feather alone (density 100 %) leaves the frame on the path too, not grown by the blur.+ s.execute("layer.vectorMask.edit", json!({"density": 100, "feather": 20})).unwrap();+ assert_eq!(boxes(&s), hard);+ // Free Transform scales about the path's centre, not the canvas's.+ s.execute("edit.transform", json!({"layer": id.0, "scaleX": 200})).unwrap();+ let st = s.active().unwrap();+ let LayerContent::Shape(sh) = &st.doc.layer(id).unwrap().content else { panic!("a shape") };+ let (x0, y0, x1, y1) = sh.path.control_bounds().unwrap();+ assert_eq!((x0.round(), y0.round(), x1.round(), y1.round()), (-10.0, 30.0, 110.0, 70.0));+ assert_eq!((sh.density, sh.feather), (1.0, 20.0), "the transform keeps the soft settings");+ }}diff --git a/crates/ui-egui/src/layer_props_ui.rs b/crates/ui-egui/src/layer_props_ui.rsindex 96899a81d..70ecc2819 100644--- a/crates/ui-egui/src/layer_props_ui.rs+++ b/crates/ui-egui/src/layer_props_ui.rs@@ -126,7 +126,7 @@ const LINK_W: f32 = 20.0;pub fn properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {let mut run: Vec<(String, Value)> = Vec::new();if let Some(s) = layer.surface() {- let b = app.cached_bounds(layer.id.0, s);+ let b = app.layer_frame_bounds(layer, s);let u = LenUnit::of(app);let (dw, dh) = app.session.active().map_or((1.0, 1.0), |s| (f64::from(s.doc.size.width), f64::from(s.doc.size.height)));if section(ui, "transform", tl!("Transform")) {diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 5d95f2d66..75e356cf7 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -2349,6 +2349,16 @@ impl PhotocraftApp {self.bounds_cache.insert(key, (rev, b));b}++ /// The box Properties › Transform shows for `layer` (X, Y, W, H): its pixels' bounds, but a+ /// soft shape's path and stroke, whose Density and Feather render past them (#5846), as its+ /// transform frame does.+ pub fn layer_frame_bounds(&mut self, layer: &photocraft_doc::Layer, surface: &photocraft_raster::Surface) -> photocraft_geom::Rect {+ match &layer.content {+ photocraft_doc::LayerContent::Shape(sh) if sh.has_soft_mask() => photocraft_compose::hard_shape_bounds(sh).unwrap_or(photocraft_geom::Rect::EMPTY),+ _ => self.cached_bounds(layer.id.0, surface),+ }+ }}impl PhotocraftApp {diff --git a/crates/ui-egui/src/mask_props_ui_tests.rs b/crates/ui-egui/src/mask_props_ui_tests.rsindex eacafafa2..15486be0e 100644--- a/crates/ui-egui/src/mask_props_ui_tests.rs+++ b/crates/ui-egui/src/mask_props_ui_tests.rs@@ -206,6 +206,21 @@ fn shape_properties_edit_density_and_feather() {}}+/// #5846: Properties › Transform (X, Y, W, H) frames a soft shape by its path, as its transform+/// frame does, though its Density renders it over the whole canvas.+#[test]+fn a_soft_shapes_transform_fields_follow_its_path() {+ let mut session = photocraft_engine::Session::new();+ session.execute("file.new", json!({"width": 64, "height": 64})).unwrap();+ let id = session.execute("shape.create", json!({"kind": "rect", "rect": [16, 8, 20, 30]})).unwrap()["layer"].as_u64().unwrap();+ session.execute("layer.vectorMask.edit", json!({"density": 60, "feather": 20})).unwrap();+ let mut app = PhotocraftApp::new(session, crate::Services::default());+ let layer = app.session.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().clone();+ let surface = layer.surface().unwrap().clone();+ assert_eq!(surface.content_bounds(), photocraft_geom::Rect::new(0, 0, 64, 64), "the soft fill covers the canvas");+ assert_eq!(app.layer_frame_bounds(&layer, &surface), photocraft_geom::Rect::new(16, 8, 36, 38));+}+/// Document-wide row of mask coverage (or of the layer's own red channel) across the middle.fn row(h: &Harness<'_, PhotocraftApp>, of_mask: bool) -> Vec<f32> {let s = h.state().session.active().unwrap();diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 0dc692d06..e5da6386d 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -3442,7 +3442,7 @@ fn adjustment_controls(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: LayerId,fn layer_controls(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {let t = Tokens::get(ui.ctx());if let Some(s) = layer.surface() {- let b = app.cached_bounds(layer.id.0, s);+ let b = app.layer_frame_bounds(layer, s);egui::Grid::new("props-grid").num_columns(2).spacing([12.0, 6.0]).show(ui, |ui| {for (k, v) in [("X", b.x0.to_string()), ("Y", b.y0.to_string()), ("W", b.width().to_string()), ("H", b.height().to_string())] {ui.label(RichText::new(k).color(t.text_dim));