From 1130c8a66c6fa4db591b927354e6b2a258bc69fe Mon Sep 17 00:00:00 2001 From: yoriswind Date: Sun, 1 Nov 2026 13:53:09 -0800 Subject: [PATCH] feat(engine): map Type to Shape and radius-only Shape Blur ATN --- crates/engine/src/cmd/atn_cmds/events.rs | 24 +++ crates/engine/src/cmd/atn_cmds/steps_make.rs | 10 ++ crates/engine/src/cmd/atn_cmds/tests.rs | 3 + crates/engine/tests/it/atn_newer_events.rs | 153 ++++++++++++++++++- docs/atn.md | 17 +++ 5 files changed, 205 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/atn_cmds/events.rs b/crates/engine/src/cmd/atn_cmds/events.rs index cca7d2d69c..55b0e2966b 100644 --- a/crates/engine/src/cmd/atn_cmds/events.rs +++ b/crates/engine/src/cmd/atn_cmds/events.rs @@ -213,6 +213,28 @@ fn target_only_to(_: &str, p: &Value) -> Option { target_descriptor(p) } +const SHAPE_BLUR: &[F] = &[F("Rds ", "radius", Kind::Px)]; + +fn shape_blur_from(d: &Descriptor) -> Mapped { + let unsupported = Mapped::Unsupported { harmless: false }; + let Some(p) = fields_from(d, SHAPE_BLUR) else { return unsupported }; + let p = Value::Object(p); + if shape_blur_to("filter.blur.shapeBlur", &p).is_some_and(|expected| d.items == expected.items) { step("filter.blur.shapeBlur", p) } else { unsupported } +} + +fn shape_blur_to(_: &str, p: &Value) -> Option { + if !params(p, &["radius"]) { + return None; + } + if let Some(radius) = p.get("radius") { + let radius = radius.as_f64()?; + if !radius.is_finite() || !(5.0..=1000.0).contains(&radius) { + return None; + } + } + fields_to(p, SHAPE_BLUR, desc("null")) +} + // Recorded Link and Unlink need explicit state: replaying the UI toggle can // perform the opposite operation. Target matching follows #10628's adapter. fn link_from(d: &Descriptor, linked: bool) -> Mapped { @@ -310,6 +332,7 @@ pub(super) const EVENTS: &[Event] = &[ ev!("MtnB", "motionBlur", "Motion Blur", "filter.blur.motionBlur", &[F("Angl", "angle", Kind::Ang), F("Dstn", "distance", Kind::Px)]), ev!("", "boxblur", "Box Blur", "filter.blur.boxBlur", &[F("Rds ", "radius", Kind::Px)]), ev!("", "surfaceBlur", "Surface Blur", "filter.blur.surfaceBlur", &[F("Rds ", "radius", Kind::Px), F("Thsh", "threshold", Kind::Long)]), + Event { code: "", name: "shapeBlur", label: "Shape Blur", ids: &["filter.blur.shapeBlur"], from: shape_blur_from, to: shape_blur_to }, ev!( "SmrB", "smartBlur", @@ -467,6 +490,7 @@ pub(super) const EVENTS: &[Event] = &[ name: "make", label: "Make", ids: &[ + "type.convertToShape", "layer.new.layer", "layer.new.group", "layer.groupLayers", diff --git a/crates/engine/src/cmd/atn_cmds/steps_make.rs b/crates/engine/src/cmd/atn_cmds/steps_make.rs index 2fce72c36d..098bd6ab9a 100644 --- a/crates/engine/src/cmd/atn_cmds/steps_make.rs +++ b/crates/engine/src/cmd/atn_cmds/steps_make.rs @@ -139,7 +139,14 @@ fn new_document_from_descriptor(d: &Descriptor) -> Option { /// New Snapshot's From: the full document, the layers merged, or the current layer. const SNAPSHOT_FROM: &[(&str, &str)] = &[("FllD", "fullDocument"), ("MrgL", "mergedLayers"), ("CrnL", "currentLayer")]; +fn type_shape_descriptor() -> Descriptor { + desc("null").with("null", class_ref("contentLayer")).with("Usng", target_ref("textLayer")) +} + pub(super) fn make_from(d: &Descriptor) -> Mapped { + if d.items == type_shape_descriptor().items { + return none("type.convertToShape"); + } if let Some(mapped) = duplicate_layer_from_descriptor(d) { return mapped; } @@ -224,6 +231,9 @@ pub(super) fn make_from(d: &Descriptor) -> Mapped { } pub(super) fn make_to(id: &str, p: &Value) -> Option { + if id == "type.convertToShape" { + return p.as_object().filter(|p| p.is_empty()).map(|_| type_shape_descriptor()); + } let with_name = |cls: &str| { let mut u = desc(cls); if let Some(n) = jstr(p, "name") { diff --git a/crates/engine/src/cmd/atn_cmds/tests.rs b/crates/engine/src/cmd/atn_cmds/tests.rs index 965399241c..757df349a0 100644 --- a/crates/engine/src/cmd/atn_cmds/tests.rs +++ b/crates/engine/src/cmd/atn_cmds/tests.rs @@ -546,6 +546,9 @@ fn every_mapped_command_exports_and_imports_back() { ("layer.duplicate", json!({"layer": "Shadow", "document": "new", "documentName": "Doc"})), ("image.duplicate", json!({"mergedOnly": true})), ("layer.new.layer", json!({"name": "Ink", "opacity": 0.5, "mode": "Multiply"})), + ("type.convertToShape", json!({})), + ("filter.blur.shapeBlur", json!({})), + ("filter.blur.shapeBlur", json!({"radius": 10})), ("layer.new.group", json!({"name": "G"})), ("layer.groupLayers", json!({})), ("layer.linkLayers", json!({"linked": true})), diff --git a/crates/engine/tests/it/atn_newer_events.rs b/crates/engine/tests/it/atn_newer_events.rs index 70db016186..41f5ed7525 100644 --- a/crates/engine/tests/it/atn_newer_events.rs +++ b/crates/engine/tests/it/atn_newer_events.rs @@ -36,10 +36,14 @@ fn target() -> D { } fn target_class(class_id: &str) -> D { + ordinal_target(class_id, "Trgt") +} + +fn ordinal_target(class_id: &str, ordinal: &str) -> D { D::Reference(vec![ReferenceItem::Enumerated { class: Class { name: UnicodeString::new_nul(""), class_id: Id::new(class_id) }, type_id: Id::new("Ordn"), - value: Id::new("Trgt"), + value: Id::new(ordinal), }]) } @@ -49,7 +53,14 @@ fn file(event: &str, descriptor: Descriptor) -> Vec { expanded: false, actions: vec![AtnAction { name: UnicodeString::new_nul("A"), - items: vec![AtnItem { event: AtnEvent::name(event), descriptor: Some(descriptor), ..Default::default() }], + items: vec![AtnItem { + event: match event.as_bytes() { + [a, b, c, d] => AtnEvent::Code([*a, *b, *c, *d]), + _ => AtnEvent::name(event), + }, + descriptor: Some(descriptor), + ..Default::default() + }], ..Default::default() }], warnings: vec![], @@ -67,6 +78,13 @@ fn play(s: &mut Session) { assert_eq!(report["ran"], 1, "{report}"); } +fn saved_item(s: &mut Session) -> AtnItem { + let saved = s.execute("actions.save", json!({"set": "Links"})).unwrap(); + assert_eq!(saved["skipped"], json!([])); + let bytes = photocraft_engine::paint::tile::b64_decode(saved["data"].as_str().unwrap()).unwrap(); + atn::parse(&bytes).unwrap().actions[0].items[0].clone() +} + #[test] fn explicit_links_are_idempotent_and_omitted_state_still_toggles() { let mut s = fixture(&[None, None], &[0, 1]); @@ -459,6 +477,137 @@ fn measurement_event_records_actual_selected_area() { } } +fn class_reference(class_id: &str) -> D { + D::Reference(vec![ReferenceItem::Class(Class { name: UnicodeString::new_nul(""), class_id: Id::new(class_id) })]) +} + +fn make_type_descriptor() -> Descriptor { + Descriptor::new("Mk ").with("null", class_reference("contentLayer")).with("Usng", target_class("textLayer")) +} + +#[test] +fn make_type_shape_roundtrips_and_converts_actual_glyphs_with_undo() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 128, "height": 80, "background": "white"})).unwrap(); + let id = LayerId(s.execute("type.create", json!({"text": "oil", "size": 24, "x": 8, "y": 48})).unwrap()["layer"].as_u64().unwrap()); + Arc::make_mut(&mut s.active_mut().unwrap().doc).layer_mut(id).unwrap().psd_blocks.push((*b"TySh", Arc::new(vec![1, 2, 3]))); + let before = s.active().unwrap().doc.clone(); + assert_eq!(load(&mut s, &file("Mk ", make_type_descriptor()))["unsupported"], json!([])); + assert_eq!(s.actions.list[0].steps, [("type.convertToShape".into(), json!({}))]); + let saved = saved_item(&mut s); + assert_eq!(saved.event, AtnEvent::Code(*b"Mk ")); + let mut canonical = make_type_descriptor(); + canonical.class_id = Id::new("null"); + assert_eq!(saved.descriptor, Some(canonical.clone())); + assert_eq!(load(&mut s, &file("Mk ", canonical))["unsupported"], json!([])); + play(&mut s); + let layer = s.active().unwrap().doc.layer(id).unwrap(); + let LayerContent::Shape(shape) = &layer.content else { panic!("Type was not converted to Shape") }; + assert!(!shape.path.subpaths.is_empty()); + assert!(!layer.psd_blocks.iter().any(|(key, _)| key == b"TySh")); + assert!(s.undo()); + assert_eq!(s.active().unwrap().doc, before); +} + +#[test] +fn make_type_shape_keeps_wrong_references_and_preserves_fill_mappings() { + for descriptor in [ + Descriptor::new("null").with("null", class_reference("contentLayer")), + Descriptor::new("null").with("null", class_reference("contentLayer")).with("Usng", target_class("Lyr ")), + Descriptor::new("null").with("null", class_reference("contentLayer")).with("Usng", ordinal_target("textLayer", "Frnt")), + make_type_descriptor().with("extra", D::Boolean(true)), + make_type_descriptor().with("Usng", target_class("textLayer")), + ] { + let mut s = Session::new(); + assert_eq!(load(&mut s, &file("Mk ", descriptor.clone()))["unsupported"].as_array().unwrap().len(), 1); + assert_eq!(saved_item(&mut s).descriptor, Some(descriptor)); + } + for (id, p) in [("layer.newFillLayer.solidColor", json!({"color": "#ff0000"})), ("layer.newFillLayer.pattern", json!({"pattern": "Grid"}))] { + let action = Action::new("A", vec![(id.into(), p)]); + let (bytes, skipped) = atn_cmds::export("Links", &[&action], &[], false).unwrap(); + assert!(skipped.is_empty()); + let mut s = Session::new(); + assert_eq!(load(&mut s, &bytes)["unsupported"], json!([])); + assert_eq!(s.actions.list[0].steps[0].0, id); + } + let action = Action::new("A", vec![("type.convertToShape".into(), json!({"layer": 1}))]); + assert!(atn_cmds::export("Links", &[&action], &[], false).is_err()); +} + +fn radius_descriptor(radius: f64) -> Descriptor { + Descriptor::new("shapeBlur").with("Rds ", D::UnitFloat { unit: *b"#Pxl", value: radius }) +} + +fn impulse_fixture() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 9, "height": 9, "background": "transparent"})).unwrap(); + s.edit("impulse fixture", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(photocraft_doc::Rect::new(4, 4, 5, 5), &[1.0, 1.0, 1.0, 1.0]); + Ok(()) + }) + .unwrap(); + s +} + +#[test] +fn radius_only_shape_blur_roundtrips_and_changes_pixels_including_default() { + for radius in [None, Some(5.0)] { + let mut s = impulse_fixture(); + let d = radius.map_or_else(|| Descriptor::new("shapeBlur"), radius_descriptor); + assert_eq!(load(&mut s, &file("shapeBlur", d.clone()))["unsupported"], json!([])); + let saved = saved_item(&mut s); + assert_eq!(saved.event, AtnEvent::name("shapeBlur")); + let mut canonical = d; + canonical.class_id = Id::new("null"); + assert_eq!(saved.descriptor, Some(canonical.clone())); + assert_eq!(load(&mut s, &file("shapeBlur", canonical))["unsupported"], json!([])); + let before = s.active().unwrap().doc.clone(); + let pixels = photocraft_compose::flatten(&before).px; + play(&mut s); + let blurred = photocraft_compose::flatten(&s.active().unwrap().doc).px; + assert_ne!(blurred, pixels); + let mut direct = impulse_fixture(); + direct.execute("filter.blur.shapeBlur", json!({"radius": radius.unwrap_or(10.0)})).unwrap(); + assert_eq!(blurred, photocraft_compose::flatten(&direct.active().unwrap().doc).px); + assert!(s.undo()); + assert_eq!(s.active().unwrap().doc, before); + } +} + +#[test] +fn shape_blur_radius_boundaries_keep_their_value_without_running_large_kernels() { + for radius in [5.0, 10.0, 1000.0] { + let mut s = Session::new(); + let mut canonical = radius_descriptor(radius); + assert_eq!(load(&mut s, &file("shapeBlur", canonical.clone()))["unsupported"], json!([])); + assert_eq!(s.actions.list[0].steps[0].1["radius"].as_f64(), Some(radius)); + canonical.class_id = Id::new("null"); + assert_eq!(saved_item(&mut s).descriptor, Some(canonical)); + } +} + +#[test] +fn shape_blur_keeps_custom_assets_and_invalid_radius_forms_whole() { + let custom = Descriptor::new("customShape").with("Nm ", D::Text(UnicodeString::new_nul("Star"))).with("Idnt", D::Text(UnicodeString::new_nul("asset-id"))); + for descriptor in [ + radius_descriptor(4.0), + radius_descriptor(1001.0), + radius_descriptor(f64::NAN), + radius_descriptor(f64::INFINITY), + Descriptor::new("shapeBlur").with("Rds ", D::UnitFloat { unit: *b"#Prc", value: 10.0 }), + radius_descriptor(10.0).with("customShape", D::Descriptor(custom)).with("keyOriginType", D::Integer(9)), + radius_descriptor(10.0).with("extra", D::Boolean(true)), + ] { + let mut s = Session::new(); + assert_eq!(load(&mut s, &file("shapeBlur", descriptor.clone()))["unsupported"].as_array().unwrap().len(), 1); + assert_eq!(saved_item(&mut s).descriptor.unwrap().to_bytes(), descriptor.to_bytes()); + } + for p in [json!({"radius": 0}), json!({"radius": 1001}), json!({"radius": null}), json!({"radius": 10, "shape": "circle"}), json!({"target": "mask"})] { + let action = Action::new("A", vec![("filter.blur.shapeBlur".into(), p)]); + assert!(atn_cmds::export("Links", &[&action], &[], false).is_err()); + } +} + #[test] fn link_descriptor_headers_normalize_without_changing_meaningful_keys() { for event in ["linkSelectedLayers", "unlinkSelectedLayers"] { diff --git a/docs/atn.md b/docs/atn.md index 2c1e631bec..98a94434a5 100644 --- a/docs/atn.md +++ b/docs/atn.md @@ -44,6 +44,23 @@ Isolate Layers toggles the panel view. Type defaults are saved in the current session. Record Measurements uses the current selection/ruler/count and data point configuration. +## Type to Shape and radius-only Shape Blur + +| Photoshop form | PhotoCraft command | +| --- | --- | +| Make `contentLayer`, `Usng` a `textLayer` / `Ordn.Trgt` reference | `type.convertToShape` | +| `shapeBlur`, optional `Rds ` in pixels, 5–1000 | `filter.blur.shapeBlur` | + +Convert Type to Shape retains the Make event and its two references; it does not +export a separate `convertToShape` event. Other Make forms, including Solid Color +and Pattern fill layers, retain their existing mappings. + +Shape Blur maps the issue's radius-only form through the existing filter. An +omitted radius uses the native factory value of 10. Later dialog forms containing +`customShape` or `keyOriginType` stay complete kept steps. Native explicit shape +choices cannot be exported into a radius-only descriptor. No custom shape asset +lookup or new blur kernel is supplied by this mapping. + ## Smart Object file operations | Photoshop event | Meaningful descriptor keys | PhotoCraft command |