Skip to content

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

Space untimed segments by distance when Spacing is off (#1036)

jason4short0ff15f9ed904
diff --git a/crates/paint/src/brush.rs b/crates/paint/src/brush.rs
index 914fc1a8e..9f7b11cb2 100644
--- a/crates/paint/src/brush.rs
+++ b/crates/paint/src/brush.rs
@@ -367,7 +367,7 @@ pub struct BrushSettings {
/// Spacing between dabs as a fraction of the diameter.
pub spacing: f32,
/// Photoshop's Spacing checkbox. Off: the pointer's speed sets the spacing (one dab every
- /// [`crate::dynamics::SPEED_SPACING_MS`] of stroke time; without timestamps, one per input point).
+ /// [`crate::dynamics::SPEED_SPACING_MS`] of stroke time; without timestamps, by distance).
pub spacing_enabled: bool,
/// Maximum coverage for the whole stroke.
pub opacity: f32,
diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs
index 2c5758ec2..9f0d026ff 100644
--- a/crates/paint/src/brush_tests.rs
+++ b/crates/paint/src/brush_tests.rs
@@ -801,9 +801,11 @@ fn spacing_off_spaces_dabs_by_pointer_speed() {
assert_eq!((slow, fast), (51, 6));
// Never denser than half a pixel, however slow.
assert!(dabs_of(&b, &timed(1.0e9)).len() <= 201);
- // No timestamps: one dab per input point.
- let pts: Vec<StrokePoint> = (0..7).map(|i| StrokePoint::new(f64::from(i) * 15.0, 10.0, 1.0)).collect();
- assert_eq!(dabs_of(&b, &pts).len(), 7);
+ // No timestamps (a ⇧-click line): spaced by distance like Spacing on, not a dab per point.
+ let pts = [StrokePoint::new(0.0, 10.0, 1.0), StrokePoint::new(90.0, 10.0, 1.0), StrokePoint::new(100.0, 10.0, 1.0)];
+ let checked = BrushSettings { spacing_enabled: true, ..b.clone() };
+ assert_eq!(dabs_of(&b, &pts).len(), dabs_of(&checked, &pts).len());
+ assert!(dabs_of(&b, &pts).len() > 3);
// Checked: fixed spacing (25 % of 20 px = 5 px) whatever the speed.
b.spacing_enabled = true;
assert_eq!(dabs_of(&b, &timed(400.0)).len(), dabs_of(&b, &timed(40.0)).len());
diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs
index e54d137f6..bd84ed4b3 100644
--- a/crates/paint/src/dynamics.rs
+++ b/crates/paint/src/dynamics.rs
@@ -178,7 +178,7 @@ impl PathWalker {
}
/// Space steps by stroke time instead of distance ([`SPEED_SPACING_MS`]): faster movement
- /// spreads them out. Segments without timestamps get one step at their end point.
+ /// spreads them out. Segments without timestamps are spaced by distance.
pub fn speed_spacing(mut self, interval_ms: f64) -> Self {
self.speed = Some(interval_ms.max(0.1));
self
@@ -209,7 +209,10 @@ impl PathWalker {
self.emit(f, out);
}
if len > 1e-9 {
- match self.speed {
+ // Speed spacing needs the segment's duration; without timestamps (a straight ⇧-click
+ // line, or input that carries no times) the segment is spaced by distance instead.
+ let dt = p.time - a.time;
+ match self.speed.filter(|_| dt > 0.0 && dt.is_finite()) {
None => {
while self.next_at <= len + 1e-9 {
let q = lerp_pt(&a, &p, (self.next_at / len).min(1.0));
@@ -219,23 +222,17 @@ impl PathWalker {
self.next_at -= len;
}
Some(iv) => {
- let dt = p.time - a.time;
- if dt > 0.0 && dt.is_finite() {
- self.speed_acc += dt;
- let max_n = (len / MIN_SPEED_STEP).ceil().max(1.0) as usize;
- let mut n = 0;
- while self.speed_acc >= iv && n < max_n {
- self.speed_acc -= iv;
- let f = ((dt - self.speed_acc) / dt).clamp(0.0, 1.0);
- self.emit(lerp_pt(&a, &p, f), out);
- n += 1;
- }
- if n == max_n {
- self.speed_acc = self.speed_acc.rem_euclid(iv);
- }
- } else {
- // No timestamps: one dab per input point.
- self.emit(p, out);
+ self.speed_acc += dt;
+ let max_n = (len / MIN_SPEED_STEP).ceil().max(1.0) as usize;
+ let mut n = 0;
+ while self.speed_acc >= iv && n < max_n {
+ self.speed_acc -= iv;
+ let f = ((dt - self.speed_acc) / dt).clamp(0.0, 1.0);
+ self.emit(lerp_pt(&a, &p, f), out);
+ n += 1;
+ }
+ if n == max_n {
+ self.speed_acc = self.speed_acc.rem_euclid(iv);
}
}
}