PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(vector): Fill Path stays inside the active selection (#2387) (#2410)
Kaustubh Tripathi099aa57db5f7
diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rsindex 6c555389d..fda3ba8b4 100644--- a/crates/engine/src/vector_cmds.rs+++ b/crates/engine/src/vector_cmds.rs@@ -1014,6 +1014,11 @@ fn path_fill(s: &mut Session, p: &Value) -> Result<Value> {if feather > 0.0 {cov = sel::feather(&cov, area.width() as usize, area.height() as usize, feather);}+ // An active selection limits the fill, soft edges included, as every other fill does (#2387).+ if let Some(selection) = doc.selection.as_ref() {+ let mask = sel::mask_from_surface(Some(selection), area);+ cov.iter_mut().zip(&mask).for_each(|(c, m)| *c *= m);+ }let locks = doc.effective_locks(id);let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;if locks.pixels || locks.all {diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rsindex 97e1b8a29..e0abe8e77 100644--- a/crates/engine/src/vector_cmds/tests.rs+++ b/crates/engine/src/vector_cmds/tests.rs@@ -361,6 +361,42 @@ fn fill_and_stroke_path_on_pixel_layer() {assert!(s.execute("path.fill", json!({})).is_err());}+#[test]+fn fill_path_stays_inside_the_selection() {+ let alpha = |s: &Session, x, y| {+ let st = s.active().unwrap();+ st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(x, y)[3]+ };+ for depth in [8, 16, 32] {+ // A hard rectangle, an ellipse and a feathered rectangle, all over the left half.+ for extra in [json!({}), json!({"ellipse": true}), json!({"feather": 4})] {+ let mut s = session(100, 100, depth);+ s.execute("layer.new.layer", json!({})).unwrap();+ let mut rect = json!({"x": 0, "y": 0, "width": 50, "height": 100});+ rect.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());+ s.execute("select.rect", rect).unwrap();+ s.execute("path.set", json!({"path": {"subpaths": [{"closed": true, "knots": [[10, 10], [90, 10], [90, 90], [10, 90]]}]}})).unwrap();+ s.execute("path.fill", json!({"color": "#ff0000", "opacity": 100})).unwrap();+ let mask = selection_mask(&s);+ assert!(alpha(&s, 30, 50) > 0.99, "{depth} {extra}: inside the selection");+ assert_eq!(alpha(&s, 70, 50), 0.0, "{depth} {extra}: outside the selection");+ // At every pixel the fill's alpha is at most the selection's coverage there.+ for (x, y) in [(46, 50), (49, 50), (51, 50), (25, 12), (12, 25)] {+ let m = mask[(y * 100 + x) as usize];+ assert!(alpha(&s, x, y) <= m + 0.01, "{depth} {extra} ({x},{y}): alpha {} > selection {m}", alpha(&s, x, y));+ }+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(alpha(&s, 30, 50), 0.0, "{depth} {extra}: undo restores");+ }+ }+ // Without a selection the whole path fills, as before.+ let mut s = session(100, 100, 8);+ s.execute("layer.new.layer", json!({})).unwrap();+ s.execute("path.set", json!({"path": {"subpaths": [{"closed": true, "knots": [[10, 10], [90, 10], [90, 90], [10, 90]]}]}})).unwrap();+ s.execute("path.fill", json!({"color": "#ff0000"})).unwrap();+ assert!(alpha(&s, 30, 50) > 0.99 && alpha(&s, 70, 50) > 0.99);+}+#[test]fn clipping_path_requires_saved_path_and_tracks_rename_delete() {let mut s = session(60, 60, 8);