From b8ae1cd92fccacfc5b30566e64c22bdebe0a75eb Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Wed, 14 Oct 2026 23:47:23 +0700 Subject: [PATCH] fix(brush): preserve Dual Brush tip spacing and scale its size with the primary brush (#4856) * fix(abr): prefer dual brush tip spacing over wrapper spacing * test(abr): cover conflicting dual brush spacing descriptors * fix(paint): scale secondary brush tip when changing primary diameter * fix(engine): preserve dual tip proportions through brush commands * test(brush): cover secondary tip scaling across settings, strokes and presets * style: apply rustfmt to Dual Brush ABR code * style: apply rustfmt to Dual Brush ABR code --- crates/engine/src/brush_cmds.rs | 7 +++++- crates/engine/src/brush_cmds/tests.rs | 32 +++++++++++++++++++++++++++ crates/io/src/abr_map/tests.rs | 23 +++++++++++++++++++ crates/paint/src/brush.rs | 11 ++++++++- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/brush_cmds.rs b/crates/engine/src/brush_cmds.rs index 370b6fcc78..5c1942441a 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 Result // 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.rs index d91e9ec4d7..f1c8188246 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.rs index 8bea763861..501e331af9 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.rs index 0dd4d21d65..354dd61d53 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;