psd: share legacy Mac Roman layer-name decoding (#4056)

This commit is contained in:
Terra854
2026-11-21 13:22:30 -05:00
committed by GitHub
parent 951c8b4da7
commit abb7b66de5
6 changed files with 40 additions and 11 deletions
Generated
+1
View File
@@ -2426,6 +2426,7 @@ dependencies = [
name = "effectcraft-psd"
version = "0.24.1"
dependencies = [
"craft-codepage",
"craft-psd",
"miniz_oxide 0.9.1",
]
+1
View File
@@ -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" }
+1
View File
@@ -11,6 +11,7 @@ workspace = true
[dependencies]
craft-psd = { workspace = true }
craft-codepage = { workspace = true }
miniz_oxide = "0.9"
[dev-dependencies]
+1 -6
View File
@@ -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;
+10 -5
View File
@@ -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()
}
+26
View File
@@ -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);
}
}
}