PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Artboards: a board moved past the canvas origin grows the canvas left/up (#3070) (#3073)
Valentin Klein0142b24fcbfa
diff --git a/crates/engine/src/artboard_cmds.rs b/crates/engine/src/artboard_cmds.rsindex b5b4c5024..a83a27ea4 100644--- a/crates/engine/src/artboard_cmds.rs+++ b/crates/engine/src/artboard_cmds.rs@@ -4,8 +4,8 @@//!//! An artboard is a top-level group with [`photocraft_doc::Artboard`] set; the compositor clips//! its children to the board and paints its background. Moving the group (Move tool,-//! `layer.translate`) moves the board with its contents. The canvas grows to the right and-//! bottom to fit new boards, like Photoshop's auto-expanding artboard canvas.+//! `layer.translate`) moves the board with its contents. The canvas grows to fit every board,+//! in every direction, like Photoshop's auto-expanding artboard canvas ([`fit_canvas`]).use photocraft_color::{Color, ColorMode, PixelFormat, SampleType};use photocraft_doc::{Artboard, ArtboardBackground, Document, Layer, LayerContent, LayerId, Size};@@ -181,14 +181,28 @@ pub(crate) fn place_copy(doc: &mut Document, id: LayerId) -> Result<()> {Ok(())}-/// Grow the canvas (right/bottom) so every artboard fits.+/// Grow the canvas so every artboard fits. Photoshop's artboard documents grow in every+/// direction: a board left of or above the origin shifts the whole document (boards, their+/// contents, guides, paths…) right/down by its overhang, then the canvas grows to show it. An+/// axis whose boards span more than the largest canvas, or a shift that would push pixels past+/// the 32-bit range, stays unshifted: the board stays partly off-canvas, as one past the size+/// limit does.pub(crate) fn fit_canvas(doc: &mut Document) {- let (mut w, mut h) = (doc.size.width as i64, doc.size.height as i64);- for (_, _, a) in doc.artboards() {- w = w.max(i64::from(a.rect.x1));- h = h.max(i64::from(a.rect.y1));+ let max = i64::from(crate::document_preset_cmds::MAX_DIMENSION);+ let boards: Vec<Rect> = doc.artboards().iter().map(|b| b.2.rect).collect();+ // The canvas and every board, per axis (`lo` ≤ 0 is the canvas origin or a board past it).+ let span = |edges: &dyn Fn(&Rect) -> (i32, i32), len: u32| {+ boards.iter().map(edges).fold((0, i64::from(len)), |(lo, hi), (a, b)| (lo.min(i64::from(a)), hi.max(i64::from(b))))+ };+ let (x0, x1) = span(&|r| (r.x0, r.x1), doc.size.width);+ let (y0, y1) = span(&|r| (r.y0, r.y1), doc.size.height);+ let shift = |lo: i64, hi: i64| if hi - lo <= max { i32::try_from(-lo).unwrap_or(0) } else { 0 };+ let (mut sx, mut sy) = (shift(x0, x1), shift(y0, y1));+ // `translate_doc` checks before it moves anything, so a refusal leaves the document as is.+ if (sx, sy) != (0, 0) && crate::image_cmds::translate_doc(doc, "artboard", sx, sy).is_err() {+ (sx, sy) = (0, 0);}- let size = Size::new(w.clamp(1, 300_000) as u32, h.clamp(1, 300_000) as u32);+ let size = Size::new((x1 + i64::from(sx)).clamp(1, max) as u32, (y1 + i64::from(sy)).clamp(1, max) as u32);if size != doc.size {doc.size = size;// Shape pixels are cut at the canvas: a board moved past the old edge rendered itsdiff --git a/crates/engine/src/artboard_cmds/tests.rs b/crates/engine/src/artboard_cmds/tests.rsindex 00e1d7b46..6f3de6b11 100644--- a/crates/engine/src/artboard_cmds/tests.rs+++ b/crates/engine/src/artboard_cmds/tests.rs@@ -427,6 +427,36 @@ fn moving_an_artboard_past_the_canvas_keeps_its_shapes_whole() {}}+#[test]+fn moving_an_artboard_left_of_or_above_the_origin_grows_the_canvas_there() {+ // Photoshop grows an artboard document in every direction; the canvas only grew right and+ // down, so a board moved past the origin stayed partly off-canvas. Now everything shifts+ // right/down (boards, contents, guides) and the canvas grows, in the move's undo step.+ for (cmd, p) in [("layer.translate", json!({"dx": -60, "dy": -30})), ("layer.artboard.set", json!({"x": -50, "y": -20}))] {+ let mut s = session(8);+ let gid = board_with_square(&mut s);+ let other = s.execute("layer.new.artboard", json!({"rect": [60, 10, 30, 30]})).unwrap()["layer"].as_u64().unwrap();+ s.edit("guide", |doc, _| {+ doc.guides.vertical.push(70.0);+ Ok(())+ })+ .unwrap();+ s.execute("layer.select", json!({"layer": gid.0})).unwrap();+ s.execute(cmd, p.clone()).unwrap();+ assert_eq!(board_rect(&s, gid.0), Rect::new(0, 0, 40, 40), "{cmd}");+ assert_eq!(board_rect(&s, other), Rect::new(110, 30, 140, 60), "{cmd}: the other board shifts too");+ assert_eq!((doc(&s).size.width, doc(&s).size.height), (150, 100), "{cmd}");+ assert_eq!(doc(&s).guides.vertical, vec![120.0], "{cmd}");+ assert!(red_at(&s, 0, 0) && red_at(&s, 19, 19) && !red_at(&s, 20, 20), "{cmd}: the contents moved with the board");+ // One undo step back.+ s.undo();+ assert_eq!(board_rect(&s, gid.0), Rect::new(10, 10, 50, 50), "{cmd}");+ assert_eq!(board_rect(&s, other), Rect::new(60, 10, 90, 40), "{cmd}");+ assert_eq!((doc(&s).size.width, doc(&s).size.height), (100, 80), "{cmd}");+ assert_eq!(doc(&s).guides.vertical, vec![70.0], "{cmd}");+ }+}+#[test]fn add_beside_places_a_board_on_each_side_and_alt_duplicates_contents() {// #3033: the Artboard tool's "+" buttons. Board 40×40 at (10, 10) holding a red square.diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 0149f17f2..31dc49d09 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -305,7 +305,7 @@ pub fn translate(s: &mut Session, p: &Value) -> Result<Value> {}let moves: Vec<_> = ids.iter().map(|&id| (id, dx, dy)).collect();move_layers(doc, &moves)?;- // A board moved past the right or bottom edge grows the canvas, as a new one does (#1531).+ // A board moved past any canvas edge grows the canvas, as a new one does (#1531, #3070).if ids.iter().any(|id| doc.layer(*id).is_some_and(|l| l.artboard().is_some())) {crate::artboard_cmds::fit_canvas(doc);}