From abb7b66de53fd1d9e42f790a3616a477bcbeeb50 Mon Sep 17 00:00:00 2001 From: Terra854 Date: Sun, 22 Nov 2026 02:22:30 +0800 Subject: [PATCH] psd: share legacy Mac Roman layer-name decoding (#4056) --- Cargo.lock | 1 + Cargo.toml | 1 + crates/psd/Cargo.toml | 1 + crates/psd/src/parse.rs | 7 +------ crates/psd/src/read.rs | 15 ++++++++++----- crates/psd/src/tests/unicode_name.rs | 26 ++++++++++++++++++++++++++ 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 48e59a0b4..68eb957f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2426,6 +2426,7 @@ dependencies = [ name = "effectcraft-psd" version = "0.24.1" dependencies = [ + "craft-codepage", "craft-psd", "miniz_oxide 0.9.1", ] diff --git a/Cargo.toml b/Cargo.toml index 3301c483e..011553b8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "3b8e426a260695af840a2cd79a064d40778f939e" } 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 = "08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c" } +craft-codepage = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "3dff4793f1a5bd1f99a4fe3280d48f515158dff3" } 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/Cargo.toml b/crates/psd/Cargo.toml index 818331cce..aa9e77e07 100644 --- a/crates/psd/Cargo.toml +++ b/crates/psd/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] craft-psd = { workspace = true } +craft-codepage = { workspace = true } miniz_oxide = "0.9" [dev-dependencies] diff --git a/crates/psd/src/parse.rs b/crates/psd/src/parse.rs index 2f1ccd2b3..b5a69bb4f 100644 --- a/crates/psd/src/parse.rs +++ b/crates/psd/src/parse.rs @@ -102,7 +102,7 @@ impl Psd { hidden: rec.flags.hidden(), protect_transparency: rec.flags.transparency_protected(), channels, - name: mac_roman_string(&rec.name), + name: crate::read::decode_mac_roman(&rec.name), ..Default::default() }; if let craft_psd::MaskData::Mask(m) = &rec.mask { @@ -489,11 +489,6 @@ fn error(e: &craft_psd::PsdError) -> Error { } } -/// A Pascal string's bytes in the system (Mac Roman) encoding; ASCII covers the common case. -fn mac_roman_string(b: &[u8]) -> String { - b.iter().map(|&b| if b < 0x80 { b as char } else { super::read::mac_roman(b) }).collect() -} - /// App representation only; binary field framing belongs to craft-psd. fn from_shared_adjustment(value: craft_psd::adjust::Adjustment) -> Adjustment { use craft_psd::adjust::Adjustment as Shared; diff --git a/crates/psd/src/read.rs b/crates/psd/src/read.rs index 307998066..158675d89 100644 --- a/crates/psd/src/read.rs +++ b/crates/psd/src/read.rs @@ -80,12 +80,17 @@ impl<'a> Reader<'a> { let padded = total.div_ceil(pad) * pad; self.skip(padded - total)?; // Pascal strings are in the system (Mac Roman) encoding; ASCII covers the common case. - Ok(s.iter().map(|&b| if b < 0x80 { b as char } else { mac_roman(b) }).collect()) + Ok(decode_mac_roman(s)) } } -/// Mac OS Roman upper half (0x80..=0xFF), as published by the Unicode Consortium mapping table. -pub(crate) fn mac_roman(b: u8) -> char { - const T: &str = "ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø¿¡¬√ƒ≈∆«»… ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔ\u{F8FF}ÒÚÛÙıˆ˜¯˘˙˚¸˝˛ˇ"; - T.chars().nth((b - 0x80) as usize).unwrap_or('?') +/// Decode the legacy Pascal names in PSD and PSB. +pub(crate) fn decode_mac_roman(bytes: &[u8]) -> String { + bytes + .iter() + .map(|&byte| { + // Keep the ordinary space EffectCraft used for this byte in legacy Pascal names. + if byte == 0xca { ' ' } else { craft_codepage::windows_byte(craft_codepage::MAC_ROMAN_CODE_PAGE, byte) } + }) + .collect() } diff --git a/crates/psd/src/tests/unicode_name.rs b/crates/psd/src/tests/unicode_name.rs index 8785b1a49..a5616099c 100644 --- a/crates/psd/src/tests/unicode_name.rs +++ b/crates/psd/src/tests/unicode_name.rs @@ -84,3 +84,29 @@ fn missing_empty_or_malformed_unicode_name_keeps_pascal_name() { assert_eq!(Psd::parse(file.to_bytes().unwrap()).unwrap().layers[0].name, "Pascal"); } } + +#[test] +fn pascal_layer_name_keeps_legacy_mac_roman_and_unicode_precedence() { + let mut doc = WDoc::new(1, 1); + doc.rle = false; // The raw pixels have the same layout in PSD and PSB. + doc.layers = vec![WLayer::empty("Fallback", vec![])]; + let bytes = write(&doc); + for version in [craft_psd::Version::Psd, craft_psd::Version::Psb] { + let mut file = craft_psd::PsdFile::from_bytes(&bytes).unwrap(); + file.header.version = version; + file.layer_info.as_mut().unwrap().layers[0].name = b"Caf\x8e\xca\xdb".to_vec(); + + for (unicode, expected) in + [(None, "Caf\u{e9} \u{20ac}"), (Some(vec![0, 0, 0, 1, 0]), "Caf\u{e9} \u{20ac}"), (Some(record(&[0x0041, 0x00a0, 0x0042])), "A\u{a0}B")] + { + let layer = &mut file.layer_info.as_mut().unwrap().layers[0]; + layer.blocks.retain(|b| b.key != *b"luni"); + if let Some(data) = unicode { + layer.blocks.push(craft_psd::tagged::TaggedBlock::new(*b"luni", data)); + } + let parsed = Psd::parse(file.to_bytes().unwrap()).unwrap(); + assert_eq!(parsed.psb, version.is_psb()); + assert_eq!(parsed.layers[0].name, expected); + } + } +}