fix(vector): pivot explicit path transforms on target layer (#3062) (#3183)

* fix(vector): pivot explicit path transforms on target layer (#3062)

* cargo fmt

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Brandon Thomas <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-11 04:23:08 -04:00
1 parent 233237e9dc
commit a9aabcdfde
2 files changed
+44 -1

No files matched your search

+15 -1
View File
@@ -892,7 +892,21 @@ fn path_transform(s: &mut Session, p: &Value) -> Result<Value> {
let (tx, ty) = (number("translateX", 0.0)?, number("translateY", 0.0)?);
let (sx, sy) = (number("scaleX", 1.0)?, number("scaleY", 1.0)?);
let angle = number("angle", 0.0)?;
let path = resolve_path(s, Some(&name))?;
// An explicit `layer` parameter must determine both the transformed path and its
// pivot. resolve_path("layer") otherwise reads the currently active layer (#3062).
let path = if let Some(id) = layer_id {
let layer = s.active().and_then(|st| st.doc.layer(id)).ok_or(EngineError::NoLayer(id))?;
match &layer.content {
LayerContent::Shape(shape) => shape.path.clone(),
_ => layer
.vector_mask
.as_ref()
.map(|mask| mask.path.clone())
.ok_or_else(|| EngineError::Other("target layer has no shape path or vector mask".into()))?,
}
} else {
resolve_path(s, Some(&name))?
};
let (x0, y0, x1, y1) = path.control_bounds().ok_or_else(|| bad(CMD, "path has no points to transform"))?;
let (cx, cy) = ((x0 + x1) * 0.5, (y0 + y1) * 0.5);
Affine::translate(-cx, -cy)
@@ -0,0 +1,29 @@
use photocraft_engine::Session;
use serde_json::json;
#[test]
fn numeric_path_transform_pivots_on_explicit_target_not_active_layer() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 128, "height": 128, "background": "transparent"})).unwrap();
let target = s.execute("shape.create", json!({"kind": "rect", "rect": [10, 10, 10, 10]})).unwrap()["layer"].as_u64().unwrap();
let active = s.execute("shape.create", json!({"kind": "rect", "rect": [70, 70, 10, 10]})).unwrap()["layer"].as_u64().unwrap();
let other_before = s.execute("shape.info", json!({"layer": active})).unwrap();
let result = s.execute("path.transform", json!({"name": "layer", "layer": target, "scaleX": 2, "scaleY": 2})).unwrap();
assert_eq!(result["matrix"], json!([2.0, 0.0, 0.0, 2.0, -15.0, -15.0]));
let after = s.execute("shape.info", json!({"layer": target})).unwrap();
assert_eq!(after["bounds"], json!([5, 5, 20, 20]));
assert_eq!(s.execute("shape.info", json!({"layer": active})).unwrap(), other_before);
}
#[test]
fn explicit_path_matrix_is_not_recentered() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 128, "height": 128})).unwrap();
let target = s.execute("shape.create", json!({"kind": "rect", "rect": [10, 10, 10, 10]})).unwrap()["layer"].as_u64().unwrap();
s.execute("shape.create", json!({"kind": "rect", "rect": [70, 70, 10, 10]})).unwrap();
let result = s.execute("path.transform", json!({"name": "layer", "layer": target, "matrix": [2, 0, 0, 2, -15, -15]})).unwrap();
assert_eq!(result["matrix"], json!([2.0, 0.0, 0.0, 2.0, -15.0, -15.0]));
let info = s.execute("shape.info", json!({"layer": target})).unwrap();
assert_eq!(info["bounds"], json!([5, 5, 20, 20]));
}