PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Moving a shape keeps its rendering: Artboards to Files matches Photoshop
Fabian Ghenofc17cc118f550
diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 5db8f5270..75ddffda9 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -345,19 +345,7 @@ fn shift_shown(doc: &Document, l: &mut Layer, dx: i32, dy: i32) {*c = translate_surface(c, dx, dy);}}- LayerContent::Shape(sh) => {- // The rendered shape is cut at the canvas: one that reaches past it renders again.- 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)- });- crate::vector_cmds::transform_shape(sh, &a);- match &mut sh.cache {- Some(c) if inside => *c = translate_surface(c, dx, dy),- _ => crate::vector_cmds::refresh_shape(doc, sh),- }- }+ LayerContent::Shape(sh) => crate::vector_cmds::shift_shape(doc, sh, f64::from(dx), f64::from(dy)),LayerContent::Smart(sm) => crate::smart_cmds::shift_smart(sm, dx, dy),LayerContent::Group(g) => {if let Some(ab) = &mut g.artboard {diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rsindex fda3ba8b4..6b44efd56 100644--- a/crates/engine/src/vector_cmds.rs+++ b/crates/engine/src/vector_cmds.rs@@ -481,10 +481,7 @@ pub fn translate_vectors(doc: &Document, l: &mut Layer, dx: f64, dy: f64) {vm.path = vm.path.transform(&a);}match &mut l.content {- LayerContent::Shape(sh) => {- transform_shape(sh, &a);- refresh_shape(doc, sh);- }+ LayerContent::Shape(sh) => shift_shape(doc, sh, dx, dy),LayerContent::Group(g) => {for c in &mut g.children {translate_vectors(doc, c, dx, dy);@@ -494,6 +491,23 @@ pub fn translate_vectors(doc: &Document, l: &mut Layer, dx: f64, dy: f64) {}}+/// 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)+ });+ 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),+ _ => 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) {diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rsindex 6f41eb03c..5346f49df 100644--- a/crates/engine/src/vector_cmds/tests.rs+++ b/crates/engine/src/vector_cmds/tests.rs@@ -666,3 +666,71 @@ fn shape_info_bounds_of_curved_shapes_are_exact() {}}}++/// A shape layer as a PSD import leaves it: Photoshop's rendering as the cache (deliberately not+/// what PhotoCraft would draw, a block of another colour, so a re-render shows).+fn add_imported_shape(s: &mut Session, r: Rect) -> LayerId {+ s.edit("shape", |doc, active| {+ let fmt = doc.pixel_format();+ let path = photocraft_vector::shapes::rect(f64::from(r.x0), f64::from(r.y0), f64::from(r.width()), f64::from(r.height()));+ let mut cache = photocraft_raster::Surface::new(fmt);+ cache.fill_rect(r, &photocraft_raster::from_rgba(&fmt, [0.9, 0.5, 0.1, 1.0]));+ let sh = ShapeLayer { path, fill: Some(photocraft_doc::Fill::Solid(photocraft_color::Color::BLACK)), cache: Some(cache), ..Default::default() };+ let id = doc.insert_above(None, Layer::new("Rectangle 1", LayerContent::Shape(sh)));+ *active = Some(id);+ Ok(id)+ })+ .unwrap()+}++fn shape_layer(doc: &Document, id: LayerId) -> ShapeLayer {+ match &doc.layer(id).unwrap().content {+ LayerContent::Shape(sh) => sh.clone(),+ other => panic!("not a shape layer: {}", other.kind_name()),+ }+}++fn orange_at(sh: &ShapeLayer, x: i32, y: i32) -> bool {+ let c = sh.cache.as_ref().unwrap();+ let px = photocraft_raster::to_rgba(&c.format(), &c.pixel(x, y));+ (px[0] - 0.9).abs() < 0.01 && (px[1] - 0.5).abs() < 0.01+}++#[test]+fn moving_and_exporting_imported_shapes_keeps_their_rendering() {+ for depth in [8, 16, 32] {+ let mut s = session(100, 80, depth);+ let id = add_imported_shape(&mut s, Rect::new(30, 20, 60, 40));+ // The Move tool: the same pixels, moved; the path moves with them.+ s.execute("layer.translate", json!({"layer": id.0, "dx": 5, "dy": -3})).unwrap();+ let sh = shape_layer(doc(&s), id);+ assert_eq!(sh.cache.as_ref().unwrap().content_bounds(), Rect::new(35, 17, 65, 37), "depth {depth}");+ assert!(orange_at(&sh, 40, 20), "depth {depth}: not re-rendered");+ assert_eq!(sh.path.control_bounds().map(|b| (b.0.round(), b.1.round())), Some((35.0, 17.0)), "the path moves too");+ // Artboards to Files moves the board to the origin the same way.+ s.execute("layer.new.artboard", json!({"rect": [20, 10, 60, 40]})).unwrap();+ let board = doc(&s).artboards()[0].0;+ s.edit("into board", |doc, _| {+ let l = doc.remove(id).unwrap();+ let Some(LayerContent::Group(g)) = doc.layer_mut(board).map(|b| &mut b.content) else { panic!("artboard group") };+ g.children.push(l);+ Ok(())+ })+ .unwrap();+ let one = crate::artboard_cmds::artboard_document(doc(&s), board).unwrap().unwrap();+ let sh = shape_layer(&one, id);+ assert_eq!(sh.cache.as_ref().unwrap().content_bounds(), Rect::new(15, 7, 45, 27), "depth {depth}");+ assert!(orange_at(&sh, 20, 10), "depth {depth}: exported as rendered");+ }+}++#[test]+fn a_shape_reaching_past_the_canvas_renders_again_when_moved() {+ let mut s = session(100, 80, 8);+ // Cut at the canvas's right edge (100 px), so its pixels can't simply be shifted back in.+ let id = add_imported_shape(&mut s, Rect::new(80, 20, 120, 40));+ s.execute("layer.translate", json!({"layer": id.0, "dx": -30, "dy": 0})).unwrap();+ let sh = shape_layer(doc(&s), id);+ assert!(!orange_at(&sh, 60, 30), "re-rendered from the path (black fill)");+ assert_eq!(sh.cache.as_ref().unwrap().content_bounds(), Rect::new(50, 20, 90, 40), "the whole shape shows");+}