PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(brush): preserve Dual Brush tip spacing and scale its size with the primary brush (#4856)
Nigel Ihza Firdausyb8ae1cd92fcc
diff --git a/crates/engine/src/brush_cmds.rs b/crates/engine/src/brush_cmds.rsindex 370b6fcc7..5c1942441 100644--- a/crates/engine/src/brush_cmds.rs+++ b/crates/engine/src/brush_cmds.rs@@ -143,6 +143,11 @@ pub fn merge_brush(base: &BrushSettings, patch: &Value, cmd: &str) -> Result<Bruif let Some(p) = pattern {out.texture.pattern = p;}+ if patch.get("size").is_some() && !touches(&["dualBrush", "size"]) {+ let new_size = out.size;+ out.size = base.size;+ out.resize_with_dual_brush(new_size);+ }Ok(out)}@@ -188,7 +193,7 @@ pub fn resolve_brush(s: &Session, p: &Value, cmd: &str) -> Result<BrushSettings>// A library preset's tips stay on disk until a stroke uses them (#1843).s.load_brush_tips(&mut b).map_err(|e| bad(cmd, e))?;if let Some(v) = num(p, "size") {- b.size = v.max(0.5);+ b.resize_with_dual_brush(v.max(0.5));}if let Some(v) = num(p, "hardness") {b.hardness = v.clamp(0.0, 1.0);diff --git a/crates/engine/src/brush_cmds/tests.rs b/crates/engine/src/brush_cmds/tests.rsindex d91e9ec4d..f1c818824 100644--- a/crates/engine/src/brush_cmds/tests.rs+++ b/crates/engine/src/brush_cmds/tests.rs@@ -349,6 +349,38 @@ fn set_brush_merges_fields() {assert_eq!(s.tools.brush.texture.pattern, chalk_pattern);}+#[test]+fn resizing_a_brush_resizes_its_enabled_secondary_tip() {+ let mut s = session(64, 64);+ s.execute("tools.setBrush", json!({"size": 40, "dualBrush": {"enabled": true, "size": 12}})).unwrap();+ s.execute("tools.setBrush", json!({"size": 80})).unwrap();+ assert_eq!((s.tools.brush.size, s.tools.brush.dual_brush.size), (80.0, 24.0));++ s.execute("tools.setBrush", json!({"brush": {"size": 20}})).unwrap();+ assert_eq!((s.tools.brush.size, s.tools.brush.dual_brush.size), (20.0, 6.0));++ // Editing both diameters in the same patch is an explicit secondary-tip size.+ s.execute("tools.setBrush", json!({"size": 40, "dualBrush": {"size": 18}})).unwrap();+ assert_eq!((s.tools.brush.size, s.tools.brush.dual_brush.size), (40.0, 18.0));+ s.execute("tools.setBrush", json!({"dualBrush": {"size": 9}})).unwrap();+ assert_eq!(s.tools.brush.dual_brush.size, 9.0);++ // The stroke-level size override must use the same proportional dimensions.+ let for_stroke = resolve_brush(&s, &json!({"size": 80, "points": [[10, 10]]}), "paint.stroke").unwrap();+ assert_eq!((for_stroke.size, for_stroke.dual_brush.size), (80.0, 18.0));+ assert_eq!((s.tools.brush.size, s.tools.brush.dual_brush.size), (40.0, 9.0));+}++#[test]+fn an_uncaptured_preset_size_keeps_dual_brush_proportions() {+ let mut s = session(32, 32);+ s.execute("tools.setBrush", json!({"size": 40, "dualBrush": {"enabled": true, "size": 10}})).unwrap();+ s.execute("brush.presets.save", json!({"name": "Dual", "captureSize": false})).unwrap();+ s.execute("tools.setBrush", json!({"size": 80})).unwrap();+ s.execute("tools.setBrush", json!({"preset": "Dual"})).unwrap();+ assert_eq!((s.tools.brush.size, s.tools.brush.dual_brush.size), (80.0, 20.0));+}+#[test]fn set_brush_keeps_untouched_bitmaps_and_replaces_touched_ones() {let tile = |w, h, k: u32| GrayTile::from_fn(w, h, move |x, y| ((x * k + y) % 5) as f32 / 4.0);diff --git a/crates/io/src/abr_map/tests.rs b/crates/io/src/abr_map/tests.rsindex 8bea76386..501e331af 100644--- a/crates/io/src/abr_map/tests.rs+++ b/crates/io/src/abr_map/tests.rs@@ -177,6 +177,29 @@ fn v6_settings_map_onto_brush_sections() {}}+#[test]+fn dual_brush_tip_spacing_takes_precedence_over_wrapper_spacing() {+ let preset = Descriptor::new("brushPreset")+ .with("Nm ", t("Close-spaced dual brush"))+ .with("Brsh", Value::Descriptor(Descriptor::new("computedBrush").with("Dmtr", u(b"#Pxl", 40.0))))+ .with(+ "dualBrush",+ Value::Descriptor(+ Descriptor::new("dualBrush")+ .with("useDualBrush", Value::Boolean(true))+ .with("Spcn", prc(100.0))+ .with("Brsh", Value::Descriptor(Descriptor::new("computedBrush").with("Dmtr", u(b"#Pxl", 12.0)).with("Spcn", prc(5.0)))),+ ),+ );+ let bytes = write_v6(2, &[], &[], &[preset], false).unwrap();+ let imported = read_abr(&bytes, "Dual").unwrap();+ assert_eq!(imported.presets.len(), 1);+ let brush = &imported.presets[0].brush;+ assert!(brush.dual_brush.enabled);+ assert!((brush.dual_brush.spacing - 0.05).abs() < 1e-6, "the tip's 5% spacing must not be replaced with the wrapper's 100%");+ assert!((brush.spacing - 0.25).abs() < 1e-6, "main brush spacing is independent");+}+#[test]fn missing_pattern_and_tip_are_reported() {let orphan = Descriptor::new("brushPreset").with("Nm ", t("Orphan")).with("Brsh", Value::Descriptor(sampled_tip("$nowhere")));diff --git a/crates/paint/src/brush.rs b/crates/paint/src/brush.rsindex 0dd4d21d6..354dd61d5 100644--- a/crates/paint/src/brush.rs+++ b/crates/paint/src/brush.rs@@ -582,6 +582,15 @@ impl Default for BrushSettings {}impl BrushSettings {+ /// Resize the primary tip and keep an enabled Dual Brush tip at the same relative size.+ /// An explicit Dual Brush size edit should bypass this and retain the requested diameter.+ pub fn resize_with_dual_brush(&mut self, new_size: f32) {+ if self.dual_brush.enabled && self.size.is_finite() && self.size > 0.0 && new_size.is_finite() && new_size > 0.0 {+ self.dual_brush.size = (self.dual_brush.size * (new_size / self.size)).clamp(0.5, MAX_BRUSH_SIZE);+ }+ self.size = new_size;+ }+/// Copy the settings with the primary and dual diameters and Scatter amounts constrained for/// rasterization.///@@ -714,7 +723,7 @@ impl BrushPreset {pub fn picked_over(&self, current: &BrushSettings) -> BrushSettings {let mut b = self.brush.clone().picked_over(current);if !self.captures.size {- b.size = current.size;+ b.resize_with_dual_brush(current.size);}if !self.captures.tool_settings {b.mode = current.mode;