Skip to content

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

Merge PR #3330: Moving a shape keeps its rendering: Artboards to Files matches the reference app

aiongg7654482fd17c
diff --cc crates/engine/src/vector_cmds.rs
index 7a871abad,745711b50..2b4712dab
--- a/crates/engine/src/vector_cmds.rs
+++ b/crates/engine/src/vector_cmds.rs
@@@ -494,6 -491,23 +491,30 @@@ pub fn translate_vectors(doc: &Document
}
}
+ /// Moves a shape by `(dx, dy)`. A whole-pixel move shifts the rendered pixels as they are, so a
+ /// shape from a PSD keeps Photoshop's anti-aliasing (as moved type does). The render is cut at
+ /// the canvas, so a shape reaching past it renders again, as does a fractional move.
+ pub(crate) fn shift_shape(doc: &Document, sh: &mut ShapeLayer, dx: f64, dy: f64) {
+ transform_shape(sh, &Affine::translate(dx, dy));
+ let whole = |v: f64| (v.fract() == 0.0 && v.abs() <= f64::from(i32::MAX)).then_some(v as i32);
+ let canvas = doc.bounds();
+ let inside = sh.cache.as_ref().is_some_and(|c| {
+ let b = photocraft_compose::bounds::content_bounds(c);
+ b.is_empty() || (b.x0 > canvas.x0 && b.y0 > canvas.y0 && b.x1 < canvas.x1 && b.y1 < canvas.y1)
+ });
++ let stroke_pad = sh.stroke.as_ref().filter(|s| s.opacity > 0.0).map_or(0.0, |s| f64::from(s.width.ceil()));
++ let stays_inside = sh.path.control_bounds().is_some_and(|(x0, y0, x1, y1)| {
++ (x0 - stroke_pad) > canvas.x0 as f64
++ && (y0 - stroke_pad) > canvas.y0 as f64
++ && (x1 + stroke_pad) < canvas.x1 as f64
++ && (y1 + stroke_pad) < canvas.y1 as f64
++ });
+ match (&mut sh.cache, whole(dx), whole(dy)) {
- (Some(c), Some(dx), Some(dy)) if inside => *c = crate::layer_multi_cmds::shift_surface(c, dx, dy),
++ (Some(c), Some(dx), Some(dy)) if inside && stays_inside => *c = crate::layer_multi_cmds::shift_surface(c, dx, dy),
+ _ => refresh_shape(doc, sh),
+ }
+ }
+
/// Applies an affine transform to a shape's path, keeping the live shape when the transform
/// is a translation plus axis-aligned scale (otherwise the shape stops being parametric).
pub fn transform_shape(sh: &mut ShapeLayer, a: &Affine) {