From 49170cdf18d5604776fae576cb9fd247d1ed7d36 Mon Sep 17 00:00:00 2001 From: FredApps Date: Wed, 18 Nov 2026 09:43:01 +0100 Subject: [PATCH] fix(psd): surface unsupported modern placements at import boundary --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/psd/src/parse.rs | 15 ++++++--- crates/psd/src/tests/integration.rs | 48 +++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 377d2308f..347669243 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1449,7 +1449,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=e9ec4813311264ee1857079c2edd10a70e2389a6#e9ec4813311264ee1857079c2edd10a70e2389a6" +source = "git+https://github.com/storytold/craft-libs?rev=c9862964df7f73ad37706539a06e346fc4865ca3#c9862964df7f73ad37706539a06e346fc4865ca3" dependencies = [ "encoding_rs", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 4f064474f..551e634fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] } craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } -craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e9ec4813311264ee1857079c2edd10a70e2389a6" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c9862964df7f73ad37706539a06e346fc4865ca3" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "77686f24fe8665c21369f1b593f4d65a973e2a45" } craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } diff --git a/crates/psd/src/parse.rs b/crates/psd/src/parse.rs index 446ecb7af..5bcb38cc0 100644 --- a/crates/psd/src/parse.rs +++ b/crates/psd/src/parse.rs @@ -68,7 +68,7 @@ impl Psd { } if let Some(info) = &file.layer_info { psd.merged_alpha = info.merged_alpha; - psd.layers = info.layers.iter().enumerate().map(|(i, rec)| psd.layer(rec, i, splice)).collect(); + psd.layers = info.layers.iter().enumerate().map(|(i, rec)| psd.layer(rec, i, splice)).collect::>>()?; } for b in &file.global_blocks { if let Some(stored) = b.stored.as_ref().filter(|_| linked(&b.key, 0)) { @@ -81,7 +81,7 @@ impl Psd { /// One layer record of the model, its additional information read into [`Layer`]. /// `splice` maps craft-psd's offsets back into `self.data` (see [`read_file`]). - fn layer(&self, rec: &craft_psd::LayerRecord, index: usize, splice: Splice) -> Layer { + fn layer(&self, rec: &craft_psd::LayerRecord, index: usize, splice: Splice) -> Result { let channels = rec .channels .iter() @@ -111,10 +111,15 @@ impl Psd { } for b in &rec.blocks { l.keys.push(b.key); - // A malformed block only loses that block's information. - let _ = self.layer_block(&mut l, &b.key, &b.data); + // A malformed optional block only loses that block's information. + // Unsupported modern placement geometry must not silently become an + // unwarped layer or fall back to a less expressive PlLd in either order. + match self.layer_block(&mut l, &b.key, &b.data) { + Err(e @ Error::Unsupported(_)) if matches!(&b.key, b"SoLd" | b"SoLE") => return Err(e), + _ => {} + } } - l + Ok(l) } /// The embedded bytes of linked file `uuid`. diff --git a/crates/psd/src/tests/integration.rs b/crates/psd/src/tests/integration.rs index ee40aee16..ca521fc5e 100644 --- a/crates/psd/src/tests/integration.rs +++ b/crates/psd/src/tests/integration.rs @@ -92,3 +92,51 @@ fn a_tiny_file_claiming_a_huge_image_is_refused_within_the_budget() { } } } + +#[test] +fn unsupported_modern_placement_does_not_fall_back_to_legacy_in_either_order() { + use craft_psd::descriptor::{Descriptor, Id, ObjectArray, UnicodeString, Value, VersionedDescriptor}; + let quad = [[0.0, 0.0], [20.0, 0.0], [20.0, 10.0], [0.0, 10.0]]; + let legacy = craft_psd::smart::SmartObject { uuid: "legacy".into(), quad, size: Some([20.0, 10.0]), warp: None }.to_placed_layer(); + let modern = |middle| { + let floats = |values| Value::UnitFloats { unit: *b"#Pxl", values }; + let points = Descriptor::new("rationalPoint") + .with("Hrzn", floats((0..4).flat_map(|_| (0..7).map(|i| i as f64 * 20.0 / 6.0)).collect())) + .with("Vrtc", floats((0..4).flat_map(|j| [j as f64 * 10.0 / 3.0; 7]).collect())); + let envelope = Descriptor::new("customEnvelopeWarp") + .with("quiltSliceX", floats(vec![0.0, middle, 20.0])) + .with("quiltSliceY", floats(vec![0.0, 10.0])) + .with("meshPoints", Value::ObjectArray(ObjectArray { prefix: 28, body: points })); + let warp = Descriptor::new("quiltWarp") + .with("warpStyle", Value::Enumerated { type_id: Id::new("warpStyle"), value: Id::new("warpCustom") }) + .with("customEnvelopeWarp", Value::Descriptor(envelope)); + let descriptor = Descriptor::new("null") + .with("Idnt", Value::Text(UnicodeString::new("modern"))) + .with("Trnf", Value::List(quad.iter().flatten().map(|v| Value::Double(*v)).collect())) + .with("quiltWarp", Value::Descriptor(warp)); + let mut bytes = b"soLD".to_vec(); + bytes.extend_from_slice(&4u32.to_be_bytes()); + bytes.extend_from_slice(&VersionedDescriptor::new(descriptor).to_bytes()); + bytes + }; + for key in [*b"SoLd", *b"SoLE"] { + for legacy_first in [false, true] { + for (bytes, expected) in [(modern(5.0), None), (modern(10.0), Some("modern")), (b"soLD".to_vec(), Some("legacy"))] { + let mut blocks = vec![(key, bytes), (*b"PlLd", legacy.clone()), (*b"luni", vec![0])]; + if legacy_first { + blocks.swap(0, 1); + } + let mut doc = WDoc::new(2, 2); + doc.layers = vec![WLayer::empty("placement", blocks)]; + let result = Psd::parse(write(&doc)); + if let Some(uuid) = expected { + let parsed = result.unwrap(); + assert_eq!(parsed.layers[0].smart_object.as_ref().unwrap().uuid, uuid); + assert_eq!(parsed.layers[0].name, "placement", "malformed optional metadata still recovers"); + } else { + assert!(matches!(result, Err(Error::Unsupported(_))), "unsupported modern placement must be reported"); + } + } + } + } +}