Frames: Convert to Frame resizes around the layer's centre (#17325)

A new width or height moves the frame's origin by half the change, so
the frame keeps the layer's centre as in the reference.
This commit is contained in:
qvest-ssels committed 2026-11-29 00:41:16 +01:00
1 parent d3b040392b
commit 82304e178e
3 files changed
+63 -2

No files matched your search

+6 -1
View File
@@ -235,7 +235,10 @@ fn convert(s: &mut Session, p: &Value) -> Result<Value> {
Some(v) => v.as_f64().filter(|v| v.is_finite() && (1.0..=300_000.0).contains(v)).ok_or_else(|| bad(format!("{k} must be 1..=300000"))),
}
};
f.transform.m = [n("width", a)?, b, c, n("height", d)?, x, y];
// The frame keeps the layer's centre, as in the reference (#17325): a new width or height moves the
// origin by half the change. `normalized` gives an axis-aligned transform, so `b` and `c` are 0.
let (w, h) = (n("width", a)?, n("height", d)?);
f.transform.m = [w, b, c, h, x + (a - w) / 2.0, y + (d - h) / 2.0];
}
if !f.valid() {
return Err(bad("invalid converted frame"));
@@ -396,4 +399,6 @@ pub fn specs() -> Vec<CommandSpec> {
#[cfg(test)]
mod content_name_tests;
#[cfg(test)]
mod convert_resize_tests;
#[cfg(test)]
mod tests;
@@ -0,0 +1,55 @@
//! #17325: Convert to Frame with a new width or height resizes the frame around the layer's centre, as the reference
//! application does; without either the frame keeps the layer's bounds.
use super::*;
/// A 400 × 200 document with a shape layer "Rect" covering 200..280 × 60..120.
fn rect_layer() -> (Session, u64) {
let mut s = crate::session();
s.execute("file.new", json!({"width": 400, "height": 200})).unwrap();
let r = s.execute("shape.create", json!({"kind": "rect", "rect": [200, 60, 80, 60], "fill": "#e07020"})).unwrap();
let id = r["layer"].as_u64().unwrap();
s.execute("layer.setProps", json!({"layer": id, "name": "Rect"})).unwrap();
(s, id)
}
/// The frame's rect as (x0, x1, y0, y1).
fn frame_rect(s: &Session, id: u64) -> (f64, f64, f64, f64) {
let l = s.active().unwrap().doc.layer(LayerId(id)).unwrap();
let [w, _, _, h, x, y] = l.frame.as_ref().expect("a frame").transform.m;
(x, x + w, y, y + h)
}
#[test]
fn the_layer_has_the_measured_bounds() {
let (mut s, id) = rect_layer();
s.execute("layer.frame.convert", json!({"layer": id})).unwrap();
assert_eq!(frame_rect(&s, id), (200.0, 280.0, 60.0, 120.0), "without width or height the frame keeps the layer bounds");
}
#[test]
fn a_new_width_grows_around_the_centre() {
let (mut s, id) = rect_layer();
let r = s.execute("layer.frame.convert", json!({"layer": id, "name": "MyFrame", "width": 160})).unwrap();
assert_eq!(r["name"], "MyFrame");
assert_eq!(frame_rect(&s, id), (160.0, 320.0, 60.0, 120.0));
}
#[test]
fn a_new_height_grows_around_the_centre() {
let (mut s, id) = rect_layer();
s.execute("layer.frame.convert", json!({"layer": id, "height": 100})).unwrap();
assert_eq!(frame_rect(&s, id), (200.0, 280.0, 40.0, 140.0));
}
#[test]
fn both_shrink_around_the_centre_and_the_mask_follows() {
let (mut s, id) = rect_layer();
s.execute("layer.frame.convert", json!({"layer": id, "width": 40, "height": 20})).unwrap();
assert_eq!(frame_rect(&s, id), (220.0, 260.0, 80.0, 100.0));
let l = s.active().unwrap().doc.layer(LayerId(id)).unwrap();
let path = &l.vector_mask.as_ref().unwrap().path;
let coverage = |x: i32, y: i32| photocraft_vector::path_coverage(path, photocraft_geom::Rect::new(x, y, x + 1, y + 1))[0];
assert_eq!(coverage(240, 90), 1.0, "the centre is inside");
assert_eq!(coverage(210, 90), 0.0, "the old left edge is outside");
}
@@ -247,7 +247,8 @@ fn converting_a_shape_uses_its_outline_and_leaves_an_empty_frame() {
let l = s.active().unwrap().doc.layer(LayerId(id)).unwrap();
assert_eq!(l.name, "Portrait");
assert!(l.children().unwrap().is_empty());
assert_eq!(l.frame.as_ref().unwrap().transform.m, [20., 0., 0., 25., 10., 15.]);
// The ellipse's 40 × 30 box at (10, 15) resized to 20 × 25 around its centre (30, 30) (#17325).
assert_eq!(l.frame.as_ref().unwrap().transform.m, [20., 0., 0., 25., 20., 17.5]);
assert_eq!(TestMask(l.vector_mask.as_ref().unwrap().path.clone()).value(10, 15), 0.0);
assert!(s.undo());
assert!(matches!(s.active().unwrap().doc.layer(LayerId(id)).unwrap().content, LayerContent::Shape(_)));