mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Honor the rotation direction in recorded Crop actions (#17690)
This commit is contained in:
1 parent
399a90a007
commit
d4380ddf59
2 files changed
+113
-2
No files matched your search
@@ -0,0 +1,106 @@
|
||||
use craft_psd::atn::{self, AtnAction, AtnEvent, AtnFile, AtnItem};
|
||||
use craft_psd::descriptor::{UnicodeString, Value as D};
|
||||
use photocraft_doc::{LayerContent, LayerId, Size, SmartObject};
|
||||
use photocraft_geom::{Point, Rect};
|
||||
use photocraft_paint::tile::{b64_decode, b64_encode};
|
||||
use serde_json::json;
|
||||
|
||||
use super::{ang, desc, get_num, px};
|
||||
use crate::Session;
|
||||
|
||||
fn document() -> (Session, LayerId, LayerId) {
|
||||
let mut s = crate::session();
|
||||
s.execute("prefs.set", json!({"path": "actions.createSingleHistoryState", "value": false})).unwrap();
|
||||
s.execute("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
s.execute("layer.new.layer", json!({"name": "Embedded square"})).unwrap();
|
||||
s.edit("Paint square", |doc, active| {
|
||||
let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
|
||||
surface.fill_rect(Rect::new(150, 100, 250, 200), &[0.2, 0.4, 0.8, 1.0]);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let converted = s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();
|
||||
let shape = s.execute("shape.create", json!({"kind": "rect", "rect": [250, 20, 140, 70], "fill": "#ff6600"})).unwrap();
|
||||
(s, LayerId(converted["layer"].as_u64().unwrap()), LayerId(shape["layer"].as_u64().unwrap()))
|
||||
}
|
||||
|
||||
fn smart_object(s: &Session, id: LayerId) -> &SmartObject {
|
||||
let LayerContent::Smart(smart) = &s.active().unwrap().doc.layer(id).unwrap().content else { panic!("expected a Smart Object") };
|
||||
smart
|
||||
}
|
||||
|
||||
fn shape_corners(s: &Session, id: LayerId) -> Vec<Point> {
|
||||
let LayerContent::Shape(shape) = &s.active().unwrap().doc.layer(id).unwrap().content else { panic!("expected a shape") };
|
||||
shape.path.subpaths[0].knots.iter().map(|knot| knot.anchor).collect()
|
||||
}
|
||||
|
||||
fn play_atn(s: &mut Session, data: &str) {
|
||||
let loaded = s.execute("actions.load", json!({"data": data})).unwrap();
|
||||
assert_eq!(loaded["unsupported"], json!([]), "{loaded}");
|
||||
let before = s.active().unwrap().history.past_len();
|
||||
let played = s.execute("actions.play", json!({"action": loaded["actions"][0]})).unwrap();
|
||||
assert_eq!(played["ran"], 1, "{played}");
|
||||
assert!(played.get("failed").is_none(), "{played}");
|
||||
let state = s.active().unwrap();
|
||||
assert_eq!(state.history.past_len(), before + 1);
|
||||
assert_eq!(state.history.undo_label(), Some("Crop"));
|
||||
assert_eq!(state.doc.size, Size::new(300, 200));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imported_crop_turns_shape_and_smart_object_clockwise() {
|
||||
let frame = desc("Rctn").with("Top ", px(50.0)).with("Left", px(50.0)).with("Btom", px(250.0)).with("Rght", px(350.0));
|
||||
let crop = desc("null").with("To ", D::Descriptor(frame)).with("Angl", ang(10.0)).with("Dlt ", D::Boolean(false));
|
||||
let file = AtnFile {
|
||||
name: UnicodeString::new_nul("Crops"),
|
||||
actions: vec![AtnAction {
|
||||
name: UnicodeString::new_nul("Turn and crop"),
|
||||
items: vec![AtnItem { event: AtnEvent::Code(*b"Crop"), dictionary: b"Crop".to_vec(), descriptor: Some(crop), ..Default::default() }],
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
};
|
||||
let (mut s, smart, shape) = document();
|
||||
let origin = smart_object(&s, smart).source.clone();
|
||||
play_atn(&mut s, &b64_encode(&atn::write(&file).unwrap()));
|
||||
|
||||
let placed = smart_object(&s, smart);
|
||||
assert_eq!(placed.source, origin);
|
||||
let corners = [(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)].map(|(x, y)| placed.transform.apply(Point::new(x, y)));
|
||||
for (point, (x, y)) in corners.into_iter().zip([(109.4, 42.1), (207.9, 59.4), (190.6, 157.9), (92.1, 140.6)]) {
|
||||
assert!((point.x - x).abs() <= 1.0 && (point.y - y).abs() <= 1.0, "{point:?} expected ({x}, {y})");
|
||||
}
|
||||
|
||||
let corners = shape_corners(&s, shape);
|
||||
let bounds = [
|
||||
corners.iter().map(|p| p.x).fold(f64::INFINITY, f64::min),
|
||||
corners.iter().map(|p| p.y).fold(f64::INFINITY, f64::min),
|
||||
corners.iter().map(|p| p.x).fold(f64::NEG_INFINITY, f64::max),
|
||||
corners.iter().map(|p| p.y).fold(f64::NEG_INFINITY, f64::max),
|
||||
];
|
||||
for (edge, expected) in bounds.into_iter().zip([209.0, -20.0, 360.0, 74.0]) {
|
||||
assert!((edge - expected).abs() <= 1.0, "{bounds:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recorded_crop_exports_a_canvas_angle_and_replays_the_same_geometry() {
|
||||
for angle in [-10.0, 6.5] {
|
||||
let (mut recorded, smart, shape) = document();
|
||||
recorded.execute("actions.record", json!({"name": "Turn and crop"})).unwrap();
|
||||
recorded.execute("image.crop", json!({"x": 50, "y": 50, "width": 300, "height": 200, "angle": angle, "deleteCroppedPixels": false})).unwrap();
|
||||
recorded.execute("actions.stop", json!({})).unwrap();
|
||||
let saved = recorded.execute("actions.save", json!({"action": "Turn and crop"})).unwrap();
|
||||
assert_eq!(saved["skipped"], json!([]));
|
||||
let data = saved["data"].as_str().unwrap();
|
||||
let file = atn::parse(&b64_decode(data).unwrap()).unwrap();
|
||||
let [step] = file.actions[0].items.as_slice() else { panic!("expected a single Crop step") };
|
||||
assert_eq!(step.event.id(), "Crop");
|
||||
assert_eq!(get_num(step.descriptor.as_ref().unwrap(), "Angl"), Some(-angle));
|
||||
|
||||
let (mut replayed, replayed_smart, replayed_shape) = document();
|
||||
play_atn(&mut replayed, data);
|
||||
assert_eq!(smart_object(&replayed, replayed_smart).transform, smart_object(&recorded, smart).transform);
|
||||
assert_eq!(shape_corners(&replayed, replayed_shape), shape_corners(&recorded, shape));
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,10 @@ use super::fields::{Mapped, none, step, steps};
|
||||
use super::tables::{ALIGN, EXTENSION, INTENT, INTERPOLATION, MODES, PROFILES, PURGE, SAVE_FORMATS, lookup, lookup_back};
|
||||
use super::{D, Descriptor, Id, Map, ReferenceItem, STOP_MESSAGE, UnicodeString, Value, json};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "crop_angle_sign_tests.rs"]
|
||||
mod crop_angle_sign_tests;
|
||||
|
||||
/// `placeEvent`: File › Place Embedded / Linked, with the file and the recorded scale.
|
||||
pub(super) fn place_from(d: &Descriptor) -> Mapped {
|
||||
let Some(path) = d.get("null").and_then(path_text) else { return Mapped::Unsupported { harmless: false } };
|
||||
@@ -631,7 +635,8 @@ pub(super) fn crop_from(d: &Descriptor) -> Mapped {
|
||||
p.insert("height".into(), (bottom - top).round().into());
|
||||
}
|
||||
if let Some(a) = get_num(d, "Angl") {
|
||||
p.insert("angle".into(), a.into());
|
||||
// ATN records the canvas turn. PhotoCraft's Crop angle describes the frame's turn.
|
||||
p.insert("angle".into(), (-a).into());
|
||||
}
|
||||
if let Some(del) = get_bool(d, "Dlt ") {
|
||||
p.insert("deleteCroppedPixels".into(), del.into());
|
||||
@@ -650,7 +655,7 @@ pub(super) fn crop_to(_: &str, p: &Value) -> Option<Descriptor> {
|
||||
d = d.with("To ", D::Descriptor(desc("Rctn").with("Top ", px(y)).with("Left", px(x)).with("Btom", px(y + h)).with("Rght", px(x + w))));
|
||||
}
|
||||
if let Some(a) = jnum(p, "angle") {
|
||||
d = d.with("Angl", ang(a));
|
||||
d = d.with("Angl", ang(-a));
|
||||
}
|
||||
if let Some(del) = jbool(p, "deleteCroppedPixels") {
|
||||
d = d.with("Dlt ", D::Boolean(del));
|
||||
|
||||
Reference in new issue
Block a user