Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

feat(vector): add shape from selection with simplify tolerance (#214) (#3954)

Tarek Mohamed6b6a9234d1b7
diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rs
index e32a7e41f..14579e595 100644
--- a/crates/engine/src/vector_cmds.rs
+++ b/crates/engine/src/vector_cmds.rs
@@ -1641,7 +1641,7 @@ fn combine_shapes(s: &mut Session, p: &Value, op: &str, label: &str) -> Result<V
}
fn selection_to_shape(s: &mut Session, p: &Value) -> Result<Value> {
- let tol = f64p(p, "tolerance").unwrap_or(2.0).clamp(0.5, 10.0);
+ let tol = f64p(p, "tolerance").or_else(|| f64p(p, "simplifyTolerance")).unwrap_or(2.0).clamp(0.5, 10.0);
let d = s.active().ok_or(EngineError::NoDocument)?;
let area = d.doc.bounds();
let mask = sel::mask_from_surface(d.doc.selection.as_ref(), area);
@@ -1896,14 +1896,7 @@ pub fn specs() -> Vec<CommandSpec> {
has_layer,
merge_components
),
- spec!(
- "select.convertToShape",
- "Convert Selection to Shape",
- [],
- r##"{"tolerance":px=2,"fill":"#rrggbb"=foreground,"name":str?} → shape.info"##,
- has_selection,
- selection_to_shape
- ),
+ spec!("select.convertToShape", "Convert to Shape", [], r##"{"tolerance":0.5..10 px=2} → shape.info"##, has_selection, selection_to_shape),
CommandSpec {
id: "layer.vectorMask.info",
label: "Vector Mask Info",
diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rs
index 21207d3e9..4c2a9a907 100644
--- a/crates/engine/src/vector_cmds/tests.rs
+++ b/crates/engine/src/vector_cmds/tests.rs
@@ -363,6 +363,30 @@ fn selection_to_work_path_roundtrip_iou() {
let r = s.execute("select.convertToShape", json!({"fill": "#00ff00"})).unwrap();
assert_eq!(r["fill"], "#00ff00");
assert_eq!(r["path"]["subpaths"].as_array().unwrap().len(), 3);
+
+ // tolerance and simplifyTolerance parameter
+ s.execute("path.toSelection", json!({"name": "src"})).unwrap();
+ let r_fine = s.execute("select.convertToShape", json!({"tolerance": 0.5, "name": "Fine Shape"})).unwrap();
+ assert_eq!(r_fine["name"], "Fine Shape");
+ let knots = |r: &Value| r["path"]["subpaths"].as_array().unwrap().iter().map(|sp| sp["knots"].as_array().unwrap().len()).sum::<usize>();
+ let fine_knots = knots(&r_fine);
+
+ s.execute("path.toSelection", json!({"name": "src"})).unwrap();
+ let r_coarse = s.execute("select.convertToShape", json!({"simplifyTolerance": 5.0, "name": "Coarse Shape"})).unwrap();
+ assert_eq!(r_coarse["name"], "Coarse Shape");
+ let coarse_knots = knots(&r_coarse);
+ assert!(fine_knots > coarse_knots, "fine tolerance {fine_knots} must have more knots than coarse {coarse_knots}");
+
+ // Undo restores state
+ assert!(s.undo());
+ assert_eq!(s.active().unwrap().doc.layers.last().unwrap().name, "Fine Shape");
+}
+
+#[test]
+fn convert_to_shape_requires_selection() {
+ let mut s = session(100, 100, 8);
+ assert!(!s.is_enabled("select.convertToShape"));
+ assert!(s.execute("select.convertToShape", json!({})).is_err());
}
#[test]
diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rs
index 31ef15711..2c336672d 100644
--- a/crates/ui-egui/src/filter_dialog.rs
+++ b/crates/ui-egui/src/filter_dialog.rs
@@ -289,6 +289,7 @@ pub fn has_dialog(command: &str) -> bool {
| "layer.layerStyle.globalLight"
| "image.mode.colorTable"
| "edit.definePattern"
+ | "select.convertToShape"
))
&& photocraft_engine::commands::find(command).is_some_and(|c| !parse_spec(c.params).is_empty())
}