PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Layers: Combine Shapes merges the selected shape layers (#2400) (#2906)
David Mann66c0b59a2391
diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rsindex 54e93191d..5bfa7026f 100644--- a/crates/engine/src/vector_cmds.rs+++ b/crates/engine/src/vector_cmds.rs@@ -1296,29 +1296,119 @@ fn toggle_vector_mask(s: &mut Session, p: &Value, key: &str) -> Result<Value> {/// Traces the exact coverage of the shape's path back into plain outlines.fn merge_components(s: &mut Session, p: &Value) -> Result<Value> {- let id = layer_id(s, p)?;let tol = f64p(p, "tolerance").unwrap_or(0.1).clamp(0.05, 10.0);- with_shape(s, id, "Merge Shape Components", |sh, _| {- // Trace at 4× resolution so the fitted outline follows the anti-aliased edge closely.- const K: f64 = 4.0;- let up = sh.path.transform(&Affine::scale(K));- let r = vector::fill_rasterizer(&up, vector::DEFAULT_TOLERANCE);- let Some(b) = r.pixel_bounds() else {- sh.path.subpaths.clear();- return Ok(());+ if let Some(ids) = combine_targets(s, p, &["layer"])? {+ return merge_shape_layers(s, &ids, None, "Merge Shape Components", Some(tol));+ }+ let id = layer_id(s, p)?;+ with_shape(s, id, "Merge Shape Components", |sh, _| bake_components(sh, tol))?;+ shape_info(s, id)+}++/// Bakes `sh`'s path operations into plain combined outlines, traced from the exact coverage.+fn bake_components(sh: &mut ShapeLayer, tol: f64) -> Result<()> {+ // Trace at 4× resolution so the fitted outline follows the anti-aliased edge closely.+ const K: f64 = 4.0;+ let up = sh.path.transform(&Affine::scale(K));+ let r = vector::fill_rasterizer(&up, vector::DEFAULT_TOLERANCE);+ let Some(b) = r.pixel_bounds() else {+ sh.path.subpaths.clear();+ return Ok(());+ };+ let b = b.inflate(2);+ if u64::from(b.width()) * u64::from(b.height()) > 400_000_000 {+ return Err(EngineError::Other("shape too large to merge".into()));+ }+ let cov = r.render(b);+ let traced = vector::trace::trace_mask(&cov, b, 0.0, tol * K);+ sh.path = traced.transform(&Affine::scale(1.0 / K));+ sh.live = None;+ sh.psd_raw = None;+ Ok(())+}++/// The shape layers Layer › Combine Shapes merges into one: with none of the `narrow` params+/// given and two or more shape layers selected, those layers bottom-to-top (other selected layers+/// are left alone). `None` keeps the single-layer behaviour (acting on one layer's components).+fn combine_targets(s: &Session, p: &Value, narrow: &[&str]) -> Result<Option<Vec<LayerId>>> {+ if narrow.iter().any(|k| p.get(*k).is_some()) {+ return Ok(None);+ }+ let d = s.active().ok_or(EngineError::NoDocument)?;+ let sel = d.selected_layers();+ let ids = crate::layer_multi_cmds::top_level(&d.doc, &sel);+ let ids: Vec<LayerId> = ids.into_iter().filter(|id| d.doc.layer(*id).is_some_and(|l| matches!(l.content, LayerContent::Shape(_)))).collect();+ Ok((ids.len() > 1).then_some(ids))+}++/// Layer › Combine Shapes with several shape layers selected: Photoshop merges them into the+/// top-most one. The bottom layer's components keep their operations; every component of the+/// layers above takes `op` (front shapes subtract, intersect… in stacking order), or, with no+/// `op` (Merge Shape Components), keeps its own with each layer's first component combined, and+/// the result is then baked with tolerance `bake`. The other layers are removed; one history step.+fn merge_shape_layers(s: &mut Session, ids: &[LayerId], op: Option<PathOp>, label: &str, bake: Option<f64>) -> Result<Value> {+ let (&keep, _) = ids.split_last().ok_or_else(|| EngineError::Other("Combine Shapes needs two or more shape layers".into()))?;+ if let Some(st) = s.active() {+ for id in ids {+ let locks = st.doc.effective_locks(*id);+ if locks.all || locks.pixels {+ return Err(EngineError::Other("a selected shape layer is locked".into()));+ }+ }+ }+ s.edit(label, |doc, active| {+ let mut path: Option<Path> = None;+ for id in ids {+ let l = doc.layer(*id).ok_or(EngineError::NoLayer(*id))?;+ let LayerContent::Shape(sh) = &l.content else {+ return Err(EngineError::Other(format!("layer {} is not a shape layer", id.0)));+ };+ let Some(acc) = path.as_mut() else {+ path = Some(sh.path.clone());+ continue;+ };+ for (j, sp) in sh.path.subpaths.iter().enumerate() {+ let mut sp = sp.clone();+ sp.op = match op {+ Some(op) => op,+ None if j == 0 => PathOp::Combine,+ None => sp.op,+ };+ acc.subpaths.push(sp);+ }+ }+ let path = path.ok_or_else(|| EngineError::Other("Combine Shapes needs two or more shape layers".into()))?;+ let snapshot = doc.clone();+ let l = doc.layer_mut(keep).ok_or(EngineError::NoLayer(keep))?;+ let LayerContent::Shape(sh) = &mut l.content else {+ return Err(EngineError::Other(format!("layer {} is not a shape layer", keep.0)));};- let b = b.inflate(2);- if u64::from(b.width()) * u64::from(b.height()) > 400_000_000 {- return Err(EngineError::Other("shape too large to merge".into()));- }- let cov = r.render(b);- let traced = vector::trace::trace_mask(&cov, b, 0.0, tol * K);- sh.path = traced.transform(&Affine::scale(1.0 / K));- sh.live = None;- sh.psd_raw = None;+ let mut merged = ShapeLayer { path, live: None, psd_raw: None, cache: None, ..sh.clone() };+ if let Some(tol) = bake {+ bake_components(&mut merged, tol)?;+ }+ vector::flatten::validate_shape(&merged.path, merged.stroke.as_ref()).map_err(|e| bad(label, e))?;+ refresh_shape(&snapshot, &mut merged);+ *sh = merged;+ for id in ids.iter().filter(|id| **id != keep) {+ doc.remove(*id);+ }+ *active = Some(keep);Ok(())})?;- shape_info(s, id)+ crate::layer_multi_cmds::reselect(s, vec![keep], Some(keep));+ shape_info(s, keep)+}++/// Layer › Combine Shapes › Unite / Subtract Front Shape / Intersect / Exclude: merges the+/// selected shape layers with `op` ([`merge_shape_layers`]), or with one layer (or an explicit+/// `layer`/`subpath`) sets the operation of that layer's components (`shape.edit`'s `op`).+fn combine_shapes(s: &mut Session, p: &Value, op: &str, label: &str) -> Result<Value> {+ if let Some(ids) = combine_targets(s, p, &["layer", "subpath"])? {+ let op = op_from(op).ok_or_else(|| EngineError::Other(format!("unknown op `{op}`")))?;+ return merge_shape_layers(s, &ids, Some(op), label, None);+ }+ shape_edit(s, &with(p, "op", json!(op)))}fn selection_to_shape(s: &mut Session, p: &Value) -> Result<Value> {@@ -1340,6 +1430,9 @@ fn selection_to_shape(s: &mut Session, p: &Value) -> Result<Value> {// Specs// ---------------------------------------------------------------------------+/// Params of the Layer › Combine Shapes operations.+const COMBINE_PARAMS: &str = r##"{"layer":id?,"subpath":index? (default all but the first)} (no layer/subpath and two or more shape layers selected: merges them into the top-most, whose components above the bottom layer's take the operation)"##;+pub(crate) const PATH_FORM: &str = r##"path: {"subpaths":[{"closed":bool=true,"op":"combine|subtract|intersect|exclude","knots":[[x,y] | {"anchor":[x,y],"in":[x,y],"out":[x,y],"smooth":bool}]}],"fillRule":"nonzero|evenodd","inverted":bool}"##;macro_rules! spec {@@ -1546,30 +1639,29 @@ pub fn specs() -> Vec<CommandSpec> {spec!("layer.rasterize.shape", "Rasterize Shape", [], r##"{"layer":id?} (no layer: every selected shape layer)"##, has_layer, |s, p| {if p.get("layer").is_some() { shape_rasterize(s, p) } else { crate::extra_cmds::rasterize(s, p, Some("shape")) }}),- spec!(- "layer.combineShapes.unite",- "Unite Shapes",- [],- r##"{"layer":id?,"subpath":index? (default all but the first)}"##,- has_layer,- |s, p| shape_edit(s, &with(p, "op", json!("combine")))- ),- spec!("layer.combineShapes.subtractFrontShape", "Subtract Front Shape", [], r##"{"layer":id?,"subpath":index?}"##, has_layer, |s, p| shape_edit(+ // With two or more shape layers selected (and no `layer`/`subpath`), these merge them into+ // the top-most one, as in Photoshop; with one, they set the operation of its components.+ spec!("layer.combineShapes.unite", "Unite Shapes", [], COMBINE_PARAMS, has_layer, |s, p| combine_shapes(s, p, "combine", "Unite Shapes")),+ spec!("layer.combineShapes.subtractFrontShape", "Subtract Front Shape", [], COMBINE_PARAMS, has_layer, |s, p| combine_shapes(s,- &with(p, "op", json!("subtract"))+ p,+ "subtract",+ "Subtract Front Shape")),- spec!("layer.combineShapes.intersectShapeAreas", "Intersect Shape Areas", [], r##"{"layer":id?,"subpath":index?}"##, has_layer, |s, p| shape_edit(+ spec!("layer.combineShapes.intersectShapeAreas", "Intersect Shape Areas", [], COMBINE_PARAMS, has_layer, |s, p| combine_shapes(s,- &with(p, "op", json!("intersect"))+ p,+ "intersect",+ "Intersect Shape Areas")),- spec!("layer.combineShapes.excludeOverlappingShapes", "Exclude Overlapping Shapes", [], r##"{"layer":id?,"subpath":index?}"##, has_layer, |s, p| {- shape_edit(s, &with(p, "op", json!("exclude")))+ spec!("layer.combineShapes.excludeOverlappingShapes", "Exclude Overlapping Shapes", [], COMBINE_PARAMS, has_layer, |s, p| {+ combine_shapes(s, p, "exclude", "Exclude Overlapping Shapes")}),spec!("layer.combineShapes.mergeShapeComponents","Merge Shape Components",[],- r##"{"layer":id?,"tolerance":px=0.1} (bakes the path operations into plain combined outlines, traced from the exact coverage)"##,+ r##"{"layer":id?,"tolerance":px=0.1} (bakes the path operations into plain combined outlines, traced from the exact coverage; no layer and several shape layers selected: merges them into the top-most first)"##,has_layer,merge_components),diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rsindex a073e4615..a9ddcf3df 100644--- a/crates/engine/src/vector_cmds/tests.rs+++ b/crates/engine/src/vector_cmds/tests.rs@@ -694,3 +694,108 @@ fn shape_info_bounds_of_curved_shapes_are_exact() {}}}++/// Two overlapping 40×40 rectangle shape layers ("Rectangle 1" black below, "Rectangle 2" grey+/// above, overlapping by 20×20) with both selected, like the Layers panel in #2400.+fn two_selected_rects(depth: u32) -> (Session, u64, u64) {+ let mut s = session(100, 100, depth);+ let a = s.execute("shape.create", json!({"rect": [10, 10, 40, 40], "fill": "#000000"})).unwrap()["layer"].as_u64().unwrap();+ let b = s.execute("shape.create", json!({"rect": [30, 30, 40, 40], "fill": "#808080"})).unwrap()["layer"].as_u64().unwrap();+ s.execute("layer.select", json!({"layer": a})).unwrap();+ s.execute("layer.select", json!({"layer": b, "mode": "add"})).unwrap();+ assert_eq!(s.active().unwrap().selected_layers(), [LayerId(a), LayerId(b)]);+ (s, a, b)+}++#[test]+fn combine_shapes_merges_the_selected_shape_layers_into_the_top_one() {+ for depth in [8, 16, 32] {+ for (cmd, area) in [+ ("layer.combineShapes.unite", 2800.0),+ ("layer.combineShapes.subtractFrontShape", 1200.0),+ ("layer.combineShapes.intersectShapeAreas", 400.0),+ ("layer.combineShapes.excludeOverlappingShapes", 2400.0),+ ("layer.combineShapes.mergeShapeComponents", 2800.0),+ ] {+ let (mut s, a, b) = two_selected_rects(depth);+ let before = doc(&s).clone();+ let top_fill = shape(&s, b).fill;+ let history = s.active().unwrap().history.past_len();+ let r = s.execute(cmd, json!({})).unwrap();+ assert_eq!(r["layer"], b, "{cmd}");+ let d = doc(&s);+ assert!(d.layer(LayerId(a)).is_none(), "{cmd}: the lower shape layer is merged away");+ assert_eq!(d.walk().len(), 2, "{cmd}: Background plus the merged shape");+ let l = d.layer(LayerId(b)).unwrap();+ assert_eq!(l.name, "Rectangle 2");+ let sh = shape(&s, b);+ assert!((alpha_sum(&sh) - area).abs() < 2.0, "{cmd} at {depth} bits: covers {} px, not {area}", alpha_sum(&sh));+ assert_eq!(sh.fill, top_fill, "{cmd}: the top layer's fill");+ assert_eq!(s.active().unwrap().active_layer, Some(LayerId(b)));+ assert_eq!(s.active().unwrap().selected_layers(), [LayerId(b)]);+ assert_eq!(s.active().unwrap().history.past_len(), history + 1, "{cmd}: one history step");+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(*doc(&s), before, "{cmd}: undo restores both layers");+ }+ }+}++#[test]+fn combine_shapes_keeps_the_single_layer_behaviour() {+ let (mut s, a, b) = two_selected_rects(8);+ // An explicit layer narrows it to that layer's components, as before.+ s.execute("shape.create", json!({"addTo": b, "rect": [50, 50, 10, 10]})).unwrap();+ s.execute("layer.combineShapes.subtractFrontShape", json!({"layer": b})).unwrap();+ let d = doc(&s);+ assert!(d.layer(LayerId(a)).is_some());+ assert_eq!(shape(&s, b).path.subpaths[1].op, PathOp::Subtract);+ assert!((alpha_sum(&shape(&s, b)) - 1500.0).abs() < 2.0);+ // One selected shape layer: the operation applies to its own components.+ s.execute("layer.select", json!({"layer": b})).unwrap();+ s.execute("layer.combineShapes.unite", json!({})).unwrap();+ assert!(doc(&s).layer(LayerId(a)).is_some());+ assert!((alpha_sum(&shape(&s, b)) - 1600.0).abs() < 2.0);+}++#[test]+fn combine_shapes_ignores_selected_non_shape_layers() {+ let (mut s, a, b) = two_selected_rects(8);+ let bg = doc(&s).walk()[0].2.id;+ s.execute("layer.select", json!({"layer": bg.0, "mode": "add"})).unwrap();+ assert_eq!(s.active().unwrap().selected_layers().len(), 3);+ s.execute("layer.combineShapes.unite", json!({})).unwrap();+ let d = doc(&s);+ assert!(d.layer(bg).is_some(), "the Background is left alone");+ assert!(d.layer(LayerId(a)).is_none());+ assert!((alpha_sum(&shape(&s, b)) - 2800.0).abs() < 2.0);+}++#[test]+fn combine_shapes_fails_gracefully() {+ let (mut s, a, _) = two_selected_rects(8);+ let before = doc(&s).clone();+ let history = s.active().unwrap().history.past_len();+ let bad_params = [json!({"layer": 999_999}), json!({"layer": a, "subpath": 99}), json!({"subpath": 99}), json!({"tolerance": "x", "layer": 999_999})];+ for cmd in [+ "layer.combineShapes.unite",+ "layer.combineShapes.subtractFrontShape",+ "layer.combineShapes.intersectShapeAreas",+ "layer.combineShapes.excludeOverlappingShapes",+ "layer.combineShapes.mergeShapeComponents",+ ] {+ for p in &bad_params {+ if cmd.ends_with("mergeShapeComponents") && p.get("subpath").is_some() {+ continue; // `subpath` means nothing to Merge Shape Components+ }+ assert!(s.execute(cmd, p.clone()).is_err(), "{cmd} {p}");+ assert_eq!(*doc(&s), before, "{cmd} {p}");+ }+ }+ assert_eq!(s.active().unwrap().history.past_len(), history);+ // Locked layers refuse the merge without touching the document.+ s.execute("layer.lockLayers", json!({"all": true})).unwrap();+ assert!(doc(&s).effective_locks(LayerId(a)).all);+ let before = doc(&s).clone();+ assert!(s.execute("layer.combineShapes.unite", json!({})).is_err());+ assert_eq!(*doc(&s), before);+}