diff --git a/crates/engine/src/cmd/image_cmds.rs b/crates/engine/src/cmd/image_cmds.rs index cd5af85152..3be558402d 100644 --- a/crates/engine/src/cmd/image_cmds.rs +++ b/crates/engine/src/cmd/image_cmds.rs @@ -439,6 +439,27 @@ fn delete_cropped_pixels(layers: &mut [Layer], r: Rect) { }); } +/// The document's only artboard, when it covers exactly the canvas. The Crop tool frames that +/// board, so its straight crop fits the board to the new canvas, as the reference application's +/// Crop tool resizes the artboard to the frame. Several boards, or one that covers only part of +/// the canvas, keep their own layout. +fn canvas_artboard(doc: &Document) -> Option { + match doc.artboards().as_slice() { + [(id, _, board)] if board.rect == doc.bounds() => Some(*id), + _ => None, + } +} + +/// Fits the board [`canvas_artboard`] found before the crop to the new canvas; the size preset it +/// was made from no longer describes it. +fn fit_canvas_artboard(doc: &mut Document, id: Option) { + let bounds = doc.bounds(); + if let Some(board) = id.and_then(|id| doc.layer_mut(id)).and_then(|l| l.artboard_mut()) { + board.rect = bounds; + board.preset.clear(); + } +} + /// Crops the document to `r` (in current document coordinates). /// A frame that extends past the canvas fills the Background's new area with `extension` (the /// background colour, as Photoshop's Crop tool does); other layers get transparent pixels there. @@ -685,12 +706,17 @@ fn crop_with(s: &mut Session, p: &Value, keep_background: bool) -> Result } s.set_sampler_map(target.sampler_map(r)); let extension = s.tools.background; + // Only the Crop tool's frame (an explicit rectangle) resizes a canvas-sized artboard; a + // selection crop leaves boards as they are. + let tool_frame = explicit.is_some(); s.edit("Crop", |doc, active| { let original = doc.size; + let board = if tool_frame { canvas_artboard(doc) } else { None }; if unlock { unlock_background_for_crop(doc); } crop_doc(doc, "image.crop", r, delete, extension)?; + fit_canvas_artboard(doc, board); // A straight crop fills all of the area past the old canvas, pixels an earlier Delete Cropped // Pixels off crop hid there included, as Photoshop does; only a turned frame keeps them (#8060). if content_aware { diff --git a/crates/engine/tests/it/canvas_artboard_crop.rs b/crates/engine/tests/it/canvas_artboard_crop.rs new file mode 100644 index 0000000000..7210fbaec5 --- /dev/null +++ b/crates/engine/tests/it/canvas_artboard_crop.rs @@ -0,0 +1,104 @@ +//! The Crop tool's straight crop of a document whose only artboard covers the whole canvas fits +//! that board to the frame, as the reference application's Crop tool resizes the artboard (#8111). +//! Partial and several boards keep their layout; Image > Crop with a selection and Image > Trim +//! leave boards alone (the reference application's Image > Crop does nothing in an artboard +//! document and its Trim keeps the board's size). + +use photocraft_doc::{Artboard, ArtboardBackground, ColorMode, Document, Group, Layer, LayerContent, Rect, SampleType, Size}; +use photocraft_engine::Session; +use serde_json::json; + +fn board_layer(board: Artboard, children: Vec) -> Layer { + Layer::new("Page", LayerContent::Group(Group { children, expanded: true, artboard: Some(board), frame_stroke: None })) +} + +/// A 100 x 80 document with one artboard per rect, each carrying a size preset. +fn fixture(rects: &[Rect]) -> Session { + let mut doc = Document::new("Synthetic canvas", Size::new(100, 80), ColorMode::Rgb, SampleType::U8); + for rect in rects { + let mut board = Artboard::new(*rect); + board.preset = "original preset".into(); + doc.layers.push(board_layer(board, vec![])); + } + let mut session = Session::new(); + session.add_document(doc, None); + session +} + +/// A sole canvas board follows the frame in both crop modes, with exact undo and redo. +#[test] +fn canvas_artboard_follows_the_crop_frame_with_undo_and_redo() { + for delete in [false, true] { + let mut s = fixture(&[Rect::new(0, 0, 100, 80)]); + let before = s.active().unwrap().doc.clone(); + s.execute("image.crop", json!({"x":20,"y":10,"width":60,"height":40,"deleteCroppedPixels":delete})).unwrap(); + let doc = &s.active().unwrap().doc; + assert_eq!(doc.size, Size::new(60, 40)); + assert_eq!(doc.artboards()[0].2.rect, doc.bounds(), "delete {delete}"); + assert!(doc.artboards()[0].2.preset.is_empty()); + assert!(s.undo()); + assert_eq!(s.active().unwrap().doc, before); + assert!(s.redo()); + assert_eq!(s.active().unwrap().doc.artboards()[0].2.rect, Rect::new(0, 0, 60, 40)); + } +} + +/// A crop frame reaching past the canvas grows the canvas board with it. +#[test] +fn extending_crop_grows_the_canvas_artboard() { + for delete in [false, true] { + let mut s = fixture(&[Rect::new(0, 0, 100, 80)]); + s.execute("image.crop", json!({"x":-10,"y":-5,"width":130,"height":95,"deleteCroppedPixels":delete})).unwrap(); + let doc = &s.active().unwrap().doc; + assert_eq!(doc.bounds(), Rect::new(0, 0, 130, 95)); + assert_eq!(doc.artboards()[0].2.rect, doc.bounds(), "delete {delete}"); + } +} + +/// Independent boards keep their layout and preset when the canvas origin moves. +#[test] +fn crop_preserves_partial_and_multiple_artboard_layouts_and_presets() { + for rects in [vec![Rect::new(10, 10, 50, 50)], vec![Rect::new(0, 0, 100, 80), Rect::new(60, 10, 90, 50)]] { + let mut s = fixture(&rects); + s.execute("image.crop", json!({"x":20,"y":10,"width":60,"height":40,"deleteCroppedPixels":false})).unwrap(); + for ((_, _, board), original) in s.active().unwrap().doc.artboards().iter().zip(rects) { + assert_eq!(board.rect, original.translate(-20, -10)); + assert_eq!(board.preset, "original preset"); + } + } +} + +/// Image > Crop with a selection and Image > Trim keep the board's size: only the canvas moves. +#[test] +fn selection_crop_and_trim_leave_the_canvas_artboard_alone() { + let mut s = fixture(&[Rect::new(0, 0, 100, 80)]); + s.execute("select.rect", json!({"x":20,"y":10,"width":60,"height":40})).unwrap(); + s.execute("image.crop", json!({})).unwrap(); + let doc = &s.active().unwrap().doc; + assert_eq!(doc.bounds(), Rect::new(0, 0, 60, 40)); + assert_eq!(doc.artboards()[0].2.rect, Rect::new(-20, -10, 80, 70)); + assert_eq!(doc.artboards()[0].2.preset, "original preset"); + + let mut doc = Document::new("Trim", Size::new(100, 80), ColorMode::Rgb, SampleType::U8); + let mut board = Artboard::new(doc.bounds()); + board.background = ArtboardBackground::Transparent; + let mut surface = photocraft_raster::Surface::new(photocraft_color::PixelFormat::RGBA8); + surface.write_region(Rect::new(20, 10, 80, 50), &[1.0, 0.0, 0.0, 1.0].repeat(60 * 40)); + doc.layers.push(board_layer(board, vec![Layer::new("Paint", LayerContent::Raster(surface))])); + let mut s = Session::new(); + s.add_document(doc, None); + s.execute("image.trim", json!({"basedOn":"transparent"})).unwrap(); + assert_eq!(s.active().unwrap().doc.bounds(), Rect::new(0, 0, 60, 40)); + assert_eq!(s.active().unwrap().doc.artboards()[0].2.rect, Rect::new(-20, -10, 80, 70)); +} + +/// Resampling to the crop's target size keeps the canvas board fitted to the final canvas. +#[test] +fn crop_target_resampling_keeps_the_canvas_board_fitted() { + let mut s = fixture(&[Rect::new(0, 0, 100, 80)]); + s.execute("image.crop", json!({"x":20,"y":10,"width":60,"height":40,"targetWidth":30,"targetHeight":20,"resolution":144})).unwrap(); + let doc = &s.active().unwrap().doc; + assert_eq!(doc.bounds(), Rect::new(0, 0, 30, 20)); + assert_eq!(doc.artboards()[0].2.rect, doc.bounds()); + assert_eq!(doc.resolution_dpi, 144.0); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 4e4a83b874..8edc7f7082 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -21,6 +21,7 @@ mod background_layer_consistency; mod brush_smoothing; mod camera_raw_exposure_clamp; mod camera_raw_noise_clamp; +mod canvas_artboard_crop; mod clipping_semantics; mod close_document; mod command_lookup;