Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Actions: an imported Crop step in a unit other than pixels is not applied as pixels (#7084)

David Mann4fc492d2ca6a
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index ebeda4868..96b39d8eb 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -2344,7 +2344,13 @@ fn image_size_to(_: &str, p: &Value) -> Option<Descriptor> {
fn crop_from(d: &Descriptor) -> Mapped {
let mut p = Map::new();
if let Some(t) = get_obj(d, "To ") {
- let (Some(top), Some(left), Some(bottom), Some(right)) = (get_num(t, "Top "), get_num(t, "Left"), get_num(t, "Btom"), get_num(t, "Rght")) else {
+ // Pixels only, like Canvas Size: a side in percent or points needs the document's size
+ // or resolution and must not be played as pixels.
+ let side = |k: &str| match t.get(k) {
+ Some(D::UnitFloat { unit: PX, value }) if value.is_finite() => Some(*value),
+ _ => None,
+ };
+ let (Some(top), Some(left), Some(bottom), Some(right)) = (side("Top "), side("Left"), side("Btom"), side("Rght")) else {
return Mapped::Unsupported { harmless: false };
};
p.insert("x".into(), left.round().into());
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index 871300372..45e4962bd 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -479,6 +479,20 @@ fn unsupported_shapes_become_photoshop_steps_not_errors() {
assert!(imp.actions[0].disabled.is_empty(), "steps with a document effect stay on so playback reports them");
}
+#[test]
+fn crop_sides_in_other_units_are_refused_not_played_as_pixels() {
+ let rect = |unit: [u8; 4]| {
+ let side = |v: f64| D::UnitFloat { unit, value: v };
+ desc("null")
+ .with("To ", D::Descriptor(desc("Rctn").with("Top ", side(10.0)).with("Left", side(10.0)).with("Btom", side(90.0)).with("Rght", side(90.0))))
+ };
+ for unit in [PCT, *b"#Rlt"] {
+ assert!(matches!(crop_from(&rect(unit)), Mapped::Unsupported { harmless: false }), "{}", String::from_utf8_lossy(&unit));
+ }
+ let Mapped::Steps(steps) = crop_from(&rect(PX)) else { panic!("a crop in pixels maps") };
+ assert_eq!(steps, [("image.crop".to_string(), json!({"x": 10.0, "y": 10.0, "width": 80.0, "height": 80.0}))]);
+}
+
#[test]
fn step_commands_and_bad_params_fail_gracefully() {
let mut s = Session::new();