From 9ab6fb056c474581666eca9d09b294c37680f5ba Mon Sep 17 00:00:00 2001 From: DuBearte <130190089+DuBearte@users.noreply.github.com> Date: Sat, 7 Nov 2026 02:27:54 -0500 Subject: [PATCH 1/3] fix(engine): a rectangular Perspective Crop keeps shape and fill layers (#11800) image.perspectiveCrop rasterized every shape and fill layer first. With a rectangular quad the crop's map is affine, which shapes and fills already follow exactly (transform_geometry, then the shape refresh), so they now stay shape and fill layers and a shape moves with the content. Any other quad still rasterizes them, as do type and smart object layers. Only the layers that must be are rasterized, in the same one undo step. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd.rs | 2 + .../engine/src/cmd/perspective_crop_cmds.rs | 40 +++++++++--- .../perspective_crop_vector_layers_tests.rs | 64 +++++++++++++++++++ 3 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 crates/engine/src/cmd/perspective_crop_vector_layers_tests.rs diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 4c911e6be0..426287bfdb 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -119,6 +119,8 @@ pub(super) mod path_edit_cmds; pub mod pattern_cmds; pub mod pattern_fill_cmds; pub mod perspective_crop_cmds; +#[cfg(test)] +mod perspective_crop_vector_layers_tests; pub mod photo_cmds; pub mod pick_cmds; pub mod plugin_cmds; diff --git a/crates/engine/src/cmd/perspective_crop_cmds.rs b/crates/engine/src/cmd/perspective_crop_cmds.rs index fbff5c6e22..5ff3ea2be1 100644 --- a/crates/engine/src/cmd/perspective_crop_cmds.rs +++ b/crates/engine/src/cmd/perspective_crop_cmds.rs @@ -8,9 +8,11 @@ //! Background over the background colour where the quad reaches past the image, and pixels outside //! the quad are deleted (Photoshop's Perspective Crop has no Delete Cropped Pixels option). //! -//! Type, shape, fill and smart object layers are rasterized first: a projective map has no -//! editable equivalent for them. Vector masks, paths, notes, slices and guides follow the affine -//! map closest to the homography (exact when the quad is a parallelogram). +//! Type and smart object layers are rasterized first: a projective map has no editable equivalent +//! for them. Shape and fill layers stay shape and fill layers when the quad is a rectangle, whose +//! map is affine (#11800), and are rasterized for any other quad. Vector masks, paths, shapes, +//! notes, slices and guides follow the affine map closest to the homography (exact when the quad +//! is a parallelogram). use photocraft_algo::transform::{Homography, Interp, convex_quad, quad_size, quad_to_rect, warp_surface_clipped}; use photocraft_color::PixelFormat; @@ -107,6 +109,16 @@ fn interpolation(p: &Value) -> Result { } } +/// Whether the quad (TL, TR, BR, BL) is a rectangle, at any angle: its map is then affine, so +/// shape and fill layers can follow it as they are. +fn is_rectangle(q: &[[f64; 2]; 4]) -> bool { + let [tl, tr, br, bl] = *q; + let (u, v) = ([tr[0] - tl[0], tr[1] - tl[1]], [bl[0] - tl[0], bl[1] - tl[1]]); + let scale = (u[0].hypot(u[1]) * v[0].hypot(v[1])).max(1e-9); + let parallelogram = (tl[0] + br[0] - tr[0] - bl[0]).abs() < 1e-6 && (tl[1] + br[1] - tr[1] - bl[1]).abs() < 1e-6; + parallelogram && ((u[0] * v[0] + u[1] * v[1]) / scale).abs() < 1e-9 +} + /// The affine map closest to the crop for geometry that can't take a homography (vector masks, /// paths, notes, guides): the quad's centroid goes to the output's centre and its mean horizontal /// and vertical edges to the output's sides. Exact when the quad is a parallelogram. @@ -221,11 +233,19 @@ fn perspective_crop(s: &mut Session, p: &Value) -> Result { let interp = interpolation(p)?; let d = s.active().ok_or(EngineError::NoDocument)?; crate::image_cmds::check_resample_budget(CMD, &d.doc, w, h, "")?; - let rasterize = d + // Shape and fill layers follow a rectangle's affine map as they are (#11800). + let keeps_vectors = is_rectangle(&q); + let rasterize: Vec = d .doc .walk() .iter() - .any(|(_, _, l)| matches!(l.content, LayerContent::Text(_) | LayerContent::Shape(_) | LayerContent::Fill(_) | LayerContent::Smart(_))); + .filter(|(_, _, l)| match l.content { + LayerContent::Text(_) | LayerContent::Smart(_) => true, + LayerContent::Shape(_) | LayerContent::Fill(_) => !keeps_vectors, + _ => false, + }) + .map(|(_, _, l)| l.id) + .collect(); let map = quad_to_rect(&q, f64::from(w), f64::from(h)).ok_or_else(|| bad("the corners don't define a perspective"))?; let bg = s.tools.background; // The Color Sampler points follow their pixels through the homography, which the edit's @@ -233,8 +253,12 @@ fn perspective_crop(s: &mut Session, p: &Value) -> Result { let samplers = s.active().map(|d| d.color_samplers.clone()).unwrap_or_default(); let removed = s.samplers_removed; crate::analysis_cmds::compound(s, "Perspective Crop", |s| { - if rasterize { - crate::extra_cmds::rasterize_all(s)?; + if !rasterize.is_empty() { + // One history entry for every layer, as Rasterize All Layers makes. + let key = crate::extra_cmds::step_key(s, "rasterizeAll"); + for id in rasterize { + crate::extra_cmds::rasterize_one(s, id, None, &key)?; + } } s.edit("Perspective Crop", |doc, _| { apply(doc, &q, &map, (w, h), interp, bg)?; @@ -259,7 +283,7 @@ pub fn specs() -> Vec { label: "Perspective Crop", menu: &[], shortcut: None, - params: r##"{"corners":[[x,y],[x,y],[x,y],[x,y]] (top-left, top-right, bottom-right, bottom-left; a convex quad, document px),"width":px,"height":px,"resolution":ppi,"interpolation":"bicubic|bilinear|nearest"="bicubic"} → {width,height,resolution,corners} (the quad is mapped onto an upright width x height canvas, all layers, one undo step; no width/height: the quad's mean side lengths, one alone keeps its proportions; type, shape, fill and smart object layers are rasterized)"##, + params: r##"{"corners":[[x,y],[x,y],[x,y],[x,y]] (top-left, top-right, bottom-right, bottom-left; a convex quad, document px),"width":px,"height":px,"resolution":ppi,"interpolation":"bicubic|bilinear|nearest"="bicubic"} → {width,height,resolution,corners} (the quad is mapped onto an upright width x height canvas, all layers, one undo step; no width/height: the quad's mean side lengths, one alone keeps its proportions; type and smart object layers are rasterized, and shape and fill layers too unless the quad is a rectangle)"##, enabled: has_doc, run: perspective_crop, journal: true, diff --git a/crates/engine/src/cmd/perspective_crop_vector_layers_tests.rs b/crates/engine/src/cmd/perspective_crop_vector_layers_tests.rs new file mode 100644 index 0000000000..8e9702f65e --- /dev/null +++ b/crates/engine/src/cmd/perspective_crop_vector_layers_tests.rs @@ -0,0 +1,64 @@ +//! #11800: a Perspective Crop with a rectangular quad keeps shape and Color Fill layers as they +//! are (a shape moves with the content), in one undo step; any other quad still rasterizes them. + +use photocraft_doc::{LayerContent, LayerId}; +use serde_json::json; + +use crate::Session; + +/// 800 × 500 with a Levels adjustment, a Color Fill and a rectangle shape at 200,300–350,400. +fn doc() -> (Session, LayerId, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 800, "height": 500})).unwrap(); + s.execute("layer.newAdjustmentLayer.levels", json!({})).unwrap(); + s.execute("layer.newFillLayer.solidColor", json!({"color": "#3366cc"})).unwrap(); + let fill = s.active().unwrap().active_layer.unwrap(); + s.execute("shape.create", json!({"kind": "rect", "rect": [200, 300, 150, 100], "fill": "#cc3333"})).unwrap(); + let shape = s.active().unwrap().active_layer.unwrap(); + (s, fill, shape) +} + +fn crop(s: &mut Session, corners: serde_json::Value) { + s.execute("image.perspectiveCrop", json!({"corners": corners, "width": 602, "height": 402})).unwrap(); +} + +fn content(s: &Session, id: LayerId) -> LayerContent { + s.active().unwrap().doc.layer(id).unwrap().content.clone() +} + +fn shape_bounds(s: &Session, id: LayerId) -> (f64, f64, f64, f64) { + match content(s, id) { + LayerContent::Shape(sh) => sh.path.control_bounds().expect("a path"), + other => panic!("not a shape: {other:?}"), + } +} + +#[test] +fn a_rectangular_crop_keeps_shape_and_fill_layers() { + let (mut s, fill, shape) = doc(); + let before = s.active().unwrap().history.past_len(); + crop(&mut s, json!([[99, 49], [701, 49], [701, 451], [99, 451]])); + assert_eq!(s.active().unwrap().history.past_len(), before + 1, "one step"); + assert!(matches!(content(&s, fill), LayerContent::Fill(_)), "still a fill layer"); + let (x0, y0, x1, y1) = shape_bounds(&s, shape); + for (got, want) in [(x0, 101.0), (y0, 251.0), (x1, 251.0), (y1, 351.0)] { + assert!((got - want).abs() <= 1.0, "shape bounds {:?}", (x0, y0, x1, y1)); + } + // The Color Fill still covers the new canvas. + let d = &s.active().unwrap().doc; + assert_eq!((d.size.width, d.size.height), (602, 402)); + let px = photocraft_compose::render(d, photocraft_geom::Rect::new(5, 5, 6, 6)).into_px(); + assert!((px[0][2] - 0.8).abs() < 0.02, "the fill's colour: {:?}", px[0]); + // One undo restores the layers as they were. + s.undo(); + assert!(matches!(content(&s, fill), LayerContent::Fill(_))); + assert_eq!(shape_bounds(&s, shape), (200.0, 300.0, 350.0, 400.0)); +} + +#[test] +fn a_skewed_crop_still_rasterizes_them() { + let (mut s, fill, shape) = doc(); + crop(&mut s, json!([[120, 49], [701, 60], [680, 451], [99, 430]])); + assert!(matches!(content(&s, fill), LayerContent::Raster(_))); + assert!(matches!(content(&s, shape), LayerContent::Raster(_))); +} From 1db2f861f222ebf5fe04adb246190839877e55e2 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 8 Nov 2026 20:47:55 +0100 Subject: [PATCH 2/3] Perspective Crop #11855: rounding-safe rectangle test, stroke scaling, vector masks on skewed quads Fix-up for #11855 (review threads): - is_rectangle allowed no rounding in its right-angle test, so a small rectangle far from the origin (corners near 1e7) counted as skewed and rasterized its shape and fill layers. The tolerance now grows with the corners' f64 rounding. - A kept shape's stroke width now scales with the mean of the crop's two axis scales, as Image Size does (#8341); before, a crop to a larger output left the strokes at their old width. - Skewed quads go back to main's Rasterize All Layers, which also turns raster layers' vector masks into pixel masks, so they follow the homography with the pixels instead of the affine approximation. - Tests for each, plus a rotated rectangle, a type layer next to kept shape and fill layers, one-step undo on the skewed crop, and the fill checked at the canvas corners. Module and params docs say all this. Co-Authored-By: DuBearte Co-Authored-By: Claude Opus 5.5 --- .../engine/src/cmd/perspective_crop_cmds.rs | 55 +++++++++-- .../perspective_crop_vector_layers_tests.rs | 97 ++++++++++++++++++- 2 files changed, 139 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/cmd/perspective_crop_cmds.rs b/crates/engine/src/cmd/perspective_crop_cmds.rs index 5ff3ea2be1..5635b696ab 100644 --- a/crates/engine/src/cmd/perspective_crop_cmds.rs +++ b/crates/engine/src/cmd/perspective_crop_cmds.rs @@ -9,10 +9,12 @@ //! the quad are deleted (Photoshop's Perspective Crop has no Delete Cropped Pixels option). //! //! Type and smart object layers are rasterized first: a projective map has no editable equivalent -//! for them. Shape and fill layers stay shape and fill layers when the quad is a rectangle, whose -//! map is affine (#11800), and are rasterized for any other quad. Vector masks, paths, shapes, -//! notes, slices and guides follow the affine map closest to the homography (exact when the quad -//! is a parallelogram). +//! for them. Shape and fill layers stay shape and fill layers when the quad is a rectangle at any +//! angle, whose map is affine (#11800); a kept shape's stroke width scales with the mean of the +//! two axis scales, as in Image Size. Any other quad rasterizes every layer's vector data first, +//! vector masks included, as Rasterize All Layers does. Vector masks that are left, paths, notes, +//! slices and guides follow the affine map closest to the homography (exact when the quad is a +//! parallelogram). use photocraft_algo::transform::{Homography, Interp, convex_quad, quad_size, quad_to_rect, warp_surface_clipped}; use photocraft_color::PixelFormat; @@ -110,13 +112,40 @@ fn interpolation(p: &Value) -> Result { } /// Whether the quad (TL, TR, BR, BL) is a rectangle, at any angle: its map is then affine, so -/// shape and fill layers can follow it as they are. +/// shape and fill layers can follow it as they are. The right-angle test allows for the rounding +/// of the corners to f64, which far from the origin is larger than the angle of a short side. fn is_rectangle(q: &[[f64; 2]; 4]) -> bool { let [tl, tr, br, bl] = *q; let (u, v) = ([tr[0] - tl[0], tr[1] - tl[1]], [bl[0] - tl[0], bl[1] - tl[1]]); - let scale = (u[0].hypot(u[1]) * v[0].hypot(v[1])).max(1e-9); + let (lu, lv) = (u[0].hypot(u[1]).max(1e-9), v[0].hypot(v[1]).max(1e-9)); let parallelogram = (tl[0] + br[0] - tr[0] - bl[0]).abs() < 1e-6 && (tl[1] + br[1] - tr[1] - bl[1]).abs() < 1e-6; - parallelogram && ((u[0] * v[0] + u[1] * v[1]) / scale).abs() < 1e-9 + // Each corner is off by up to half an ulp of the largest coordinate. + let ulp = q.iter().flatten().fold(1.0_f64, |m, c| m.max(c.abs())) * f64::EPSILON; + let tolerance = 1e-9 + 4.0 * ulp * (1.0 / lu + 1.0 / lv); + parallelogram && ((u[0] * v[0] + u[1] * v[1]) / (lu * lv)).abs() < tolerance +} + +/// The mean of a rectangle crop's two axis scales: what a kept shape's stroke width is multiplied +/// by, as Image Size does with its mean scale (#8341). +fn stroke_scale(q: &[[f64; 2]; 4], w: f64, h: f64) -> f64 { + let [tl, tr, _, bl] = *q; + let (lu, lv) = ((tr[0] - tl[0]).hypot(tr[1] - tl[1]), (bl[0] - tl[0]).hypot(bl[1] - tl[1])); + if lu > 0.0 && lv > 0.0 { (w / lu + h / lv) / 2.0 } else { 1.0 } +} + +/// Multiplies the stroke width of every shape layer (in groups too) by `k`. +fn scale_strokes(layers: &mut [Layer], k: f64) { + for l in layers { + match &mut l.content { + LayerContent::Group(g) => scale_strokes(&mut g.children, k), + LayerContent::Shape(sh) => { + if let Some(st) = &mut sh.stroke { + st.width = (f64::from(st.width) * k).clamp(0.0, f64::from(f32::MAX)) as f32; + } + } + _ => {} + } + } } /// The affine map closest to the crop for geometry that can't take a homography (vector masks, @@ -209,6 +238,8 @@ fn apply(doc: &mut Document, q: &[[f64; 2]; 4], h: &Homography, size: (u32, u32) } doc.selection = None; crate::canvas_geom::transform_geometry(doc, &nearest_affine(q, f64::from(w), f64::from(ht))); + // Only a rectangle crop keeps shape layers (#11800); every other quad rasterized them. + scale_strokes(&mut doc.layers, stroke_scale(q, f64::from(w), f64::from(ht))); doc.size = Size::new(w, ht); crate::canvas_geom::refresh(doc, crate::canvas_geom::Refresh::Shapes); Ok(()) @@ -233,7 +264,9 @@ fn perspective_crop(s: &mut Session, p: &Value) -> Result { let interp = interpolation(p)?; let d = s.active().ok_or(EngineError::NoDocument)?; crate::image_cmds::check_resample_budget(CMD, &d.doc, w, h, "")?; - // Shape and fill layers follow a rectangle's affine map as they are (#11800). + // Shape and fill layers follow a rectangle's affine map as they are (#11800): only type and + // smart object layers are rasterized then. Any other quad keeps today's Rasterize All Layers, + // which also turns the vector masks into pixel masks, so they follow the homography too. let keeps_vectors = is_rectangle(&q); let rasterize: Vec = d .doc @@ -253,7 +286,9 @@ fn perspective_crop(s: &mut Session, p: &Value) -> Result { let samplers = s.active().map(|d| d.color_samplers.clone()).unwrap_or_default(); let removed = s.samplers_removed; crate::analysis_cmds::compound(s, "Perspective Crop", |s| { - if !rasterize.is_empty() { + if !rasterize.is_empty() && !keeps_vectors { + crate::extra_cmds::rasterize_all(s)?; + } else if !rasterize.is_empty() { // One history entry for every layer, as Rasterize All Layers makes. let key = crate::extra_cmds::step_key(s, "rasterizeAll"); for id in rasterize { @@ -283,7 +318,7 @@ pub fn specs() -> Vec { label: "Perspective Crop", menu: &[], shortcut: None, - params: r##"{"corners":[[x,y],[x,y],[x,y],[x,y]] (top-left, top-right, bottom-right, bottom-left; a convex quad, document px),"width":px,"height":px,"resolution":ppi,"interpolation":"bicubic|bilinear|nearest"="bicubic"} → {width,height,resolution,corners} (the quad is mapped onto an upright width x height canvas, all layers, one undo step; no width/height: the quad's mean side lengths, one alone keeps its proportions; type and smart object layers are rasterized, and shape and fill layers too unless the quad is a rectangle)"##, + params: r##"{"corners":[[x,y],[x,y],[x,y],[x,y]] (top-left, top-right, bottom-right, bottom-left; a convex quad, document px),"width":px,"height":px,"resolution":ppi,"interpolation":"bicubic|bilinear|nearest"="bicubic"} → {width,height,resolution,corners} (the quad is mapped onto an upright width x height canvas, all layers, one undo step; no width/height: the quad's mean side lengths, one alone keeps its proportions; type and smart object layers are rasterized; shape and fill layers are kept when the quad is a rectangle at any angle, their stroke widths scaled by the mean axis scale, and otherwise every layer's vector data is rasterized, vector masks included)"##, enabled: has_doc, run: perspective_crop, journal: true, diff --git a/crates/engine/src/cmd/perspective_crop_vector_layers_tests.rs b/crates/engine/src/cmd/perspective_crop_vector_layers_tests.rs index 8e9702f65e..bd0310c4f2 100644 --- a/crates/engine/src/cmd/perspective_crop_vector_layers_tests.rs +++ b/crates/engine/src/cmd/perspective_crop_vector_layers_tests.rs @@ -22,6 +22,14 @@ fn crop(s: &mut Session, corners: serde_json::Value) { s.execute("image.perspectiveCrop", json!({"corners": corners, "width": 602, "height": 402})).unwrap(); } +fn crop_to(s: &mut Session, corners: serde_json::Value, w: u32, h: u32) { + s.execute("image.perspectiveCrop", json!({"corners": corners, "width": w, "height": h})).unwrap(); +} + +fn past(s: &Session) -> usize { + s.active().unwrap().history.past_len() +} + fn content(s: &Session, id: LayerId) -> LayerContent { s.active().unwrap().doc.layer(id).unwrap().content.clone() } @@ -44,11 +52,13 @@ fn a_rectangular_crop_keeps_shape_and_fill_layers() { for (got, want) in [(x0, 101.0), (y0, 251.0), (x1, 251.0), (y1, 351.0)] { assert!((got - want).abs() <= 1.0, "shape bounds {:?}", (x0, y0, x1, y1)); } - // The Color Fill still covers the new canvas. + // The Color Fill still covers the new canvas, to its corners. let d = &s.active().unwrap().doc; assert_eq!((d.size.width, d.size.height), (602, 402)); - let px = photocraft_compose::render(d, photocraft_geom::Rect::new(5, 5, 6, 6)).into_px(); - assert!((px[0][2] - 0.8).abs() < 0.02, "the fill's colour: {:?}", px[0]); + for (x, y) in [(0, 0), (601, 0), (0, 401), (601, 401), (5, 5), (300, 20)] { + let px = photocraft_compose::render(d, photocraft_geom::Rect::new(x, y, x + 1, y + 1)).into_px(); + assert!((px[0][2] - 0.8).abs() < 0.02, "the fill's colour at ({x}, {y}): {:?}", px[0]); + } // One undo restores the layers as they were. s.undo(); assert!(matches!(content(&s, fill), LayerContent::Fill(_))); @@ -58,7 +68,88 @@ fn a_rectangular_crop_keeps_shape_and_fill_layers() { #[test] fn a_skewed_crop_still_rasterizes_them() { let (mut s, fill, shape) = doc(); + let before = past(&s); crop(&mut s, json!([[120, 49], [701, 60], [680, 451], [99, 430]])); assert!(matches!(content(&s, fill), LayerContent::Raster(_))); assert!(matches!(content(&s, shape), LayerContent::Raster(_))); + assert_eq!(past(&s), before + 1, "one step"); + s.undo(); + assert!(matches!(content(&s, fill), LayerContent::Fill(_))); + assert_eq!(shape_bounds(&s, shape), (200.0, 300.0, 350.0, 400.0)); +} + +/// A rectangle at an angle maps affinely too: the shape stays a shape, its corners taken where the +/// rotated frame's axes take them. +#[test] +fn a_rotated_rectangle_keeps_shape_and_fill_layers() { + let (mut s, fill, shape) = doc(); + // u = (400, 200), v = (-100, 200): perpendicular, |u| = 447.2, |v| = 223.6. + let (tl, u, v) = ([200.0, 50.0], [400.0, 200.0], [-100.0, 200.0]); + crop_to(&mut s, json!([tl, [600, 250], [500, 450], [100, 250]]), 447, 224); + assert!(matches!(content(&s, fill), LayerContent::Fill(_)), "still a fill layer"); + let (lu, lv) = (f64::hypot(u[0], u[1]), f64::hypot(v[0], v[1])); + let map = |p: [f64; 2]| { + let d = [p[0] - tl[0], p[1] - tl[1]]; + ((d[0] * u[0] + d[1] * u[1]) / lu * 447.0 / lu, (d[0] * v[0] + d[1] * v[1]) / lv * 224.0 / lv) + }; + let pts = [[200.0, 300.0], [350.0, 300.0], [350.0, 400.0], [200.0, 400.0]].map(map); + let want = pts.iter().fold((f64::MAX, f64::MAX, f64::MIN, f64::MIN), |b, p| (b.0.min(p.0), b.1.min(p.1), b.2.max(p.0), b.3.max(p.1))); + let got = shape_bounds(&s, shape); + for (g, w) in [(got.0, want.0), (got.1, want.1), (got.2, want.2), (got.3, want.3)] { + assert!((g - w).abs() <= 1.0, "shape bounds {got:?}, expected {want:?}"); + } +} + +/// A small rectangle far from the origin still counts as one: the corners' rounding to f64 is +/// larger there than its sides' right angle allows without a scale-aware tolerance. +#[test] +fn a_rectangle_far_from_the_origin_keeps_the_fill_layer() { + let (mut s, fill, _) = doc(); + let q = json!([[9999980.266, 9999980.347], [9999980.826, 9999980.787], [9999979.066, 9999983.027], [9999978.506, 9999982.587]]); + s.execute("image.perspectiveCrop", json!({"corners": q})).unwrap(); + assert!(matches!(content(&s, fill), LayerContent::Fill(_)), "still a fill layer"); +} + +/// A type layer is rasterized by a rectangle crop; the shape and fill layers beside it are kept, +/// and it all stays one step. +#[test] +fn a_rectangular_crop_rasterizes_only_the_type_layer() { + let (mut s, fill, shape) = doc(); + let text = LayerId(s.execute("type.create", json!({"x": 300, "y": 100, "text": "A", "size": 24})).unwrap()["layer"].as_u64().unwrap()); + let before = past(&s); + crop(&mut s, json!([[99, 49], [701, 49], [701, 451], [99, 451]])); + assert!(matches!(content(&s, text), LayerContent::Raster(_)), "type rasterized"); + assert!(matches!(content(&s, fill), LayerContent::Fill(_)), "still a fill layer"); + assert!(matches!(content(&s, shape), LayerContent::Shape(_)), "still a shape layer"); + assert_eq!(past(&s), before + 1, "one step"); + s.undo(); + assert!(matches!(content(&s, text), LayerContent::Text(_))); +} + +/// A kept shape's stroke scales with the crop, as in Image Size: by the mean of the axis scales. +#[test] +fn a_scaled_rectangular_crop_scales_the_stroke_width() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 800, "height": 500})).unwrap(); + let id = LayerId(s.execute("shape.create", json!({"rect": [200, 300, 150, 100], "stroke": {"width": 4}})).unwrap()["layer"].as_u64().unwrap()); + // 602 x 402 onto 1204 x 402: x2 across, x1 down, mean 1.5. + crop_to(&mut s, json!([[99, 49], [701, 49], [701, 451], [99, 451]]), 1204, 402); + let LayerContent::Shape(sh) = content(&s, id) else { panic!("not a shape") }; + let w = sh.stroke.expect("a stroke").width; + assert!((w - 6.0).abs() < 1e-3, "stroke width {w}"); +} + +/// A skewed crop with a shape layer rasterizes every layer's vector data, as Rasterize All Layers +/// does: a raster layer's vector mask becomes a pixel mask, so it follows the homography with the +/// pixels instead of an affine approximation. +#[test] +fn a_skewed_crop_rasterizes_vector_masks_too() { + let (mut s, _, _) = doc(); + s.execute("layer.new.layer", json!({})).unwrap(); + let raster = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.vectorMask.revealAll", json!({})).unwrap(); + assert!(s.active().unwrap().doc.layer(raster).unwrap().vector_mask.is_some()); + crop(&mut s, json!([[120, 49], [701, 60], [680, 451], [99, 430]])); + let l = s.active().unwrap().doc.layer(raster).unwrap(); + assert!(l.vector_mask.is_none() && l.mask.is_some(), "the vector mask became a pixel mask"); } From 042590eb7c052e9150706ac0e517e733e07df329 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 29 Nov 2026 23:12:45 +0100 Subject: [PATCH 3/3] test(engine-core): build the Perspective Crop vector layer tests on an installed session Co-Authored-By: Claude Opus 5.5 --- .../src/cmd/perspective_crop_vector_layers_tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/engine-core/src/cmd/perspective_crop_vector_layers_tests.rs b/crates/engine-core/src/cmd/perspective_crop_vector_layers_tests.rs index f10568a251..137b431a61 100644 --- a/crates/engine-core/src/cmd/perspective_crop_vector_layers_tests.rs +++ b/crates/engine-core/src/cmd/perspective_crop_vector_layers_tests.rs @@ -8,7 +8,7 @@ use crate::Session; /// 800 × 500 with a Levels adjustment, a Color Fill and a rectangle shape at 200,300–350,400. fn doc() -> (Session, LayerId, LayerId) { - let mut s = Session::new(); + let mut s = crate::session(); s.execute("file.new", json!({"width": 800, "height": 500})).unwrap(); s.execute("layer.newAdjustmentLayer.levels", json!({})).unwrap(); s.execute("layer.newFillLayer.solidColor", json!({"color": "#3366cc"})).unwrap(); @@ -143,7 +143,7 @@ fn a_thin_sheared_quad_is_not_a_rectangle() { /// A kept shape's stroke scales with the crop, as in Image Size: by the mean of the axis scales. #[test] fn a_scaled_rectangular_crop_scales_the_stroke_width() { - let mut s = Session::new(); + let mut s = crate::session(); s.execute("file.new", json!({"width": 800, "height": 500})).unwrap(); let id = LayerId(s.execute("shape.create", json!({"rect": [200, 300, 150, 100], "stroke": {"width": 4}})).unwrap()["layer"].as_u64().unwrap()); // 602 x 402 onto 1204 x 402: x2 across, x1 down, mean 1.5.