mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Brush: the Dual Brush tip keeps its proportion at tiny main sizes (#4809)
Photoshop scales the Dual Brush diameter by the main size ratio, unrounded, with a 5000 px cap and no lower floor: main 100 / dual 20 resized to 1 px gives a 0.2 px dual tip, and back at 100 px it is 20 px again. PhotoCraft already scaled the dual tip (#4856) but floored it at 0.5 px, so a trip to 1 px and back left a 50 px dual tip. Drop the floor (rendering bounds the diameter itself), keep the cap, and leave a non-positive or NaN result alone. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
b630d7cb9a
commit
0ce7e00fee
3 files changed
+110
-1
No files matched your search
@@ -371,6 +371,57 @@ fn resizing_a_brush_resizes_its_enabled_secondary_tip() {
|
||||
assert_eq!((s.tools.brush.size, s.tools.brush.dual_brush.size), (40.0, 9.0));
|
||||
}
|
||||
|
||||
/// Every way of changing the main size scales the Dual Brush tip as Photoshop does (#4809): `[` /
|
||||
/// `]`, the options bar / Brushes panel / Alt+right-drag (`tools.setBrush` with only a size), down
|
||||
/// to 1 px and back without losing the proportion to a floor.
|
||||
#[test]
|
||||
fn every_size_change_scales_the_dual_tip_and_keeps_its_proportion() {
|
||||
let mut s = session(32, 32);
|
||||
s.execute("tools.setBrush", json!({"brush": {"size": 100, "dualBrush": {"enabled": true, "size": 20}}})).unwrap();
|
||||
let sizes = |s: &Session| (s.tools.brush.size, s.tools.brush.dual_brush.size);
|
||||
s.execute("tools.decreaseBrushSize", json!({})).unwrap();
|
||||
assert_eq!(sizes(&s), (80.0, 16.0));
|
||||
s.execute("tools.increaseBrushSize", json!({})).unwrap();
|
||||
assert_eq!(sizes(&s), (100.0, 20.0));
|
||||
// A coalesced drag (options bar slider, Alt+right-drag) is a run of size-only patches.
|
||||
for size in [60, 10, 1] {
|
||||
s.execute("tools.setBrush", json!({"brush": {"size": size}, "coalesce": "drag"})).unwrap();
|
||||
}
|
||||
assert_eq!(s.tools.brush.size, 1.0);
|
||||
assert!((s.tools.brush.dual_brush.size - 0.2).abs() < 1e-5, "no 0.5 px floor: {}", s.tools.brush.dual_brush.size);
|
||||
s.execute("tools.setBrush", json!({"brush": {"size": 100}})).unwrap();
|
||||
assert_eq!(s.tools.brush.size, 100.0);
|
||||
assert!((s.tools.brush.dual_brush.size - 20.0).abs() < 1e-3, "back to 20 px: {}", s.tools.brush.dual_brush.size);
|
||||
// Painting with the sub-pixel dual tip works (the renderer bounds it).
|
||||
s.execute("tools.setBrush", json!({"brush": {"size": 1}})).unwrap();
|
||||
s.execute("paint.stroke", json!({"points": [[4, 16], [28, 16]]})).unwrap();
|
||||
}
|
||||
|
||||
/// Picking a preset that captured its size sets both diameters from the preset: the dual tip is
|
||||
/// not scaled on top of that.
|
||||
#[test]
|
||||
fn a_captured_preset_size_is_not_scaled_twice() {
|
||||
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": "Dual40"})).unwrap();
|
||||
s.execute("tools.setBrush", json!({"size": 200})).unwrap();
|
||||
assert_eq!(s.tools.brush.dual_brush.size, 50.0);
|
||||
s.execute("tools.setBrush", json!({"preset": "Dual40"})).unwrap();
|
||||
assert_eq!((s.tools.brush.size, s.tools.brush.dual_brush.size), (40.0, 10.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_sizes_fail_and_leave_the_dual_tip_alone() {
|
||||
let mut s = session(32, 32);
|
||||
s.execute("tools.setBrush", json!({"brush": {"size": 100, "dualBrush": {"enabled": true, "size": 20}}})).unwrap();
|
||||
for bad in [json!(1e30), json!("big"), json!([1]), json!({"x": 1}), json!(null)] {
|
||||
let _ = s.execute("tools.setBrush", json!({"brush": {"size": bad}}));
|
||||
assert_eq!(s.tools.brush.dual_brush.size, 20.0, "{bad}");
|
||||
assert_eq!(s.tools.brush.size, 100.0, "{bad}");
|
||||
}
|
||||
assert!(s.execute("tools.setBrush", json!({"brush": {"size": 1e30}})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_uncaptured_preset_size_keeps_dual_brush_proportions() {
|
||||
let mut s = session(32, 32);
|
||||
|
||||
@@ -589,9 +589,19 @@ 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.
|
||||
///
|
||||
/// As in Photoshop, the dual diameter is multiplied by the size ratio unrounded and only capped
|
||||
/// at the top ([`MAX_BRUSH_SIZE`]): a 100 px brush with a 20 px dual tip shrunk to 1 px has a
|
||||
/// 0.2 px dual tip and grows back to exactly 20 px. A sub-pixel floor here would lose the
|
||||
/// proportion on the way back up (#4809); rendering bounds the diameter itself
|
||||
/// ([`Self::bounded_for_render`]).
|
||||
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);
|
||||
let scaled = self.dual_brush.size * (new_size / self.size);
|
||||
// NaN fails the comparison; infinity is capped.
|
||||
if scaled > 0.0 {
|
||||
self.dual_brush.size = scaled.min(MAX_BRUSH_SIZE);
|
||||
}
|
||||
}
|
||||
self.size = new_size;
|
||||
}
|
||||
|
||||
@@ -1493,3 +1493,51 @@ fn an_aliased_stroke_at_partial_opacity_is_one_flat_colour() {
|
||||
assert_eq!(s.rgba(x, 5), first, "pixel {x} of the Pencil line");
|
||||
}
|
||||
}
|
||||
|
||||
/// Photoshop (measured): a 100 px brush with a 20 px Dual Brush tip resized to 50 / 2 / 1 px has a
|
||||
/// 10 / 0.4 / 0.2 px dual tip, unrounded and with no floor, and back at 100 px it is 20 px again; past
|
||||
/// 5000 px the dual tip is capped there and the proportion is lost (#4809).
|
||||
#[test]
|
||||
fn resizing_scales_the_dual_tip_like_photoshop() {
|
||||
let mut b = BrushSettings { size: 100.0, ..brush() };
|
||||
b.dual_brush.enabled = true;
|
||||
b.dual_brush.size = 20.0;
|
||||
for (main, dual) in [(50.0, 10.0), (2.0, 0.4), (1.0, 0.2), (100.0, 20.0), (240.0, 48.0), (100.0, 20.0)] {
|
||||
b.resize_with_dual_brush(main);
|
||||
assert_eq!(b.size, main);
|
||||
assert!((b.dual_brush.size - dual).abs() < 1e-4, "main {main}: dual {} != {dual}", b.dual_brush.size);
|
||||
}
|
||||
// The cap: 300 px at 100 px would be 15000 px at 5000 px; Photoshop stores 5000 and scales from it.
|
||||
b.dual_brush.size = 300.0;
|
||||
b.resize_with_dual_brush(MAX_BRUSH_SIZE);
|
||||
assert_eq!(b.dual_brush.size, MAX_BRUSH_SIZE);
|
||||
b.resize_with_dual_brush(100.0);
|
||||
assert_eq!(b.dual_brush.size, 100.0);
|
||||
// A sub-pixel dual tip renders at the renderer's smallest diameter.
|
||||
b.dual_brush.size = 20.0;
|
||||
b.resize_with_dual_brush(1.0);
|
||||
assert_eq!(b.bounded_for_render().dual_brush.size, 0.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resizing_with_bad_sizes_leaves_the_dual_tip_alone() {
|
||||
let mut b = BrushSettings { size: 100.0, ..brush() };
|
||||
b.dual_brush.enabled = true;
|
||||
b.dual_brush.size = 20.0;
|
||||
for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 0.0, -5.0] {
|
||||
b.size = 100.0;
|
||||
b.resize_with_dual_brush(bad);
|
||||
assert_eq!(b.dual_brush.size, 20.0, "{bad}");
|
||||
}
|
||||
for bad_dual in [f32::NAN, 0.0, -3.0] {
|
||||
b.size = 100.0;
|
||||
b.dual_brush.size = bad_dual;
|
||||
b.resize_with_dual_brush(50.0);
|
||||
assert_eq!(b.dual_brush.size.to_bits(), bad_dual.to_bits(), "a bad dual size {bad_dual} is left as it was");
|
||||
}
|
||||
// A disabled dual tip keeps its size.
|
||||
b.dual_brush = DualBrush { enabled: false, size: 20.0, ..Default::default() };
|
||||
b.size = 100.0;
|
||||
b.resize_with_dual_brush(50.0);
|
||||
assert_eq!(b.dual_brush.size, 20.0);
|
||||
}
|
||||
Reference in new issue
Block a user