mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
fix(io): preserve TIFF scale resource Pascal names
This commit is contained in:
1 parent
663710bb86
commit
b37ff7bc5f
4 files changed
+88
-2
No files matched your search
Generated
+1
@@ -6939,6 +6939,7 @@ dependencies = [
|
||||
"craft-xcf",
|
||||
"craft-xml",
|
||||
"crc32fast",
|
||||
"encoding_rs",
|
||||
"flate2",
|
||||
"kurbo 0.13.1",
|
||||
"lopdf",
|
||||
|
||||
@@ -21,6 +21,8 @@ testgen = []
|
||||
pdf-editable = ["dep:kurbo"]
|
||||
|
||||
[dependencies]
|
||||
# Photoshop image-resource Pascal names use MacRoman; retaining them must not pass through UTF-8.
|
||||
encoding_rs = { workspace = true }
|
||||
photocraft-geom = { workspace = true }
|
||||
photocraft-color = { workspace = true }
|
||||
craft-cms = { workspace = true }
|
||||
|
||||
@@ -319,7 +319,10 @@ pub(crate) fn import_scale_resource(doc: &mut Document, resource: Option<ImageRe
|
||||
doc.measurement.scale = scale;
|
||||
}
|
||||
doc.metadata.psd_resources.retain(|(id, _, _)| *id != MEASUREMENT_SCALE);
|
||||
doc.metadata.psd_resources.push((MEASUREMENT_SCALE, String::from_utf8_lossy(&resource.name).into_owned(), Arc::new(resource.data)));
|
||||
// Image-resource names are legacy Pascal bytes, separate from the descriptor's UTF-16 logical units.
|
||||
// MacRoman is reversible for every byte; lossy UTF-8 would collapse distinct names to the replacement character.
|
||||
let name = encoding_rs::MACINTOSH.decode_without_bom_handling(&resource.name).0.into_owned();
|
||||
doc.metadata.psd_resources.push((MEASUREMENT_SCALE, name, Arc::new(resource.data)));
|
||||
}
|
||||
|
||||
/// Merge the scale into TIFF metadata without replacing channel or other image resources.
|
||||
@@ -328,7 +331,10 @@ pub(crate) fn write_tiff_scale(doc: &Document, bytes: &mut Option<Vec<u8>>) -> R
|
||||
let scale = fresh_scale_resource(doc).map(|data| ImageResource::new(MEASUREMENT_SCALE, data)).or_else(|| {
|
||||
doc.metadata.psd_resources.iter().find(|(id, _, _)| *id == MEASUREMENT_SCALE).filter(|_| keep_raw_scale(doc)).map(|(id, name, data)| {
|
||||
let mut resource = ImageResource::new(*id, data.to_vec());
|
||||
resource.name = crate::psd_export::legacy_name(name);
|
||||
let (encoded, _, unmappable) = encoding_rs::MACINTOSH.encode(name);
|
||||
// Imported MacRoman names always encode exactly. Keep the existing fallback for a caller-supplied name
|
||||
// outside that legacy alphabet rather than emitting the encoder's HTML character references.
|
||||
resource.name = if unmappable { crate::psd_export::legacy_name(name) } else { encoded.into_owned() };
|
||||
resource
|
||||
})
|
||||
});
|
||||
@@ -592,6 +598,50 @@ mod tiff_scale_tests {
|
||||
assert_eq!(bytes, Some(original));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retained_tiff_scale_pascal_names_roundtrip_every_byte_without_utf8_guessing() {
|
||||
let scale = MeasurementScale { pixel_length: 100.0, logical_length: 2.0, units: "cm".into() };
|
||||
let names = [
|
||||
Vec::new(),
|
||||
b"Scale".to_vec(),
|
||||
vec![0xe9],
|
||||
vec![0xff],
|
||||
vec![0x8e], // MacRoman e-acute, not a malformed Unicode descriptor string.
|
||||
vec![0xc3, 0xa9], // Valid UTF-8-looking bytes are still a legacy Pascal name.
|
||||
vec![0xef, 0xbb, 0xbf], // A BOM-looking prefix is also three ordinary name bytes.
|
||||
(0x80..=0xff).collect(),
|
||||
(0..=0xfe).collect(), // The maximum Pascal name length, including control bytes.
|
||||
];
|
||||
let mut decoded_names = std::collections::HashSet::new();
|
||||
for name in names {
|
||||
for data in [write_scale_resource(&scale), vec![0, 0, 0, 16, 1]] {
|
||||
let mut resource = ImageResource::new(MEASUREMENT_SCALE, data.clone());
|
||||
resource.name = name.clone();
|
||||
let original = craft_psd::tiff::resources_to_bytes(&[resource]).unwrap();
|
||||
let mut bytes = original.clone();
|
||||
for _ in 0..3 {
|
||||
let mut doc = Document::new("Scale", photocraft_doc::Size::new(3, 2), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8);
|
||||
import_scale_resource(&mut doc, tiff_scale_resource(Some(&bytes)));
|
||||
let stored = &doc.metadata.psd_resources[0].1;
|
||||
if name == [0x8e] {
|
||||
assert_eq!(stored, "é");
|
||||
}
|
||||
if name == b"Scale" {
|
||||
assert_eq!(stored, "Scale");
|
||||
}
|
||||
if name == [0xe9] || name == [0xff] {
|
||||
decoded_names.insert(stored.clone());
|
||||
}
|
||||
let mut written = None;
|
||||
write_tiff_scale(&doc, &mut written).unwrap();
|
||||
bytes = written.expect("a retained scale resource");
|
||||
assert_eq!(bytes, original, "the Pascal name, padding and opaque descriptor remain byte-exact: {name:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(decoded_names.len(), 2, "distinct non-UTF-8 names do not collapse together");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merging_tiff_scale_refuses_a_damaged_resource_bundle_without_replacing_it() {
|
||||
let mut doc = Document::new("Scale", photocraft_doc::Size::new(3, 2), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8);
|
||||
|
||||
@@ -144,3 +144,36 @@ fn a_damaged_layer_tag_still_restores_the_composites_measurement_scale() {
|
||||
assert!(!back.warnings.is_empty());
|
||||
assert_eq!(back.document.measurement.scale, scale());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flat_tiff_preserves_raw_pascal_scale_names_through_both_import_paths() {
|
||||
let raw = annotations_map::write_scale_resource(&scale());
|
||||
for extras in [false, true] {
|
||||
let mut doc = document(ColorMode::Rgb, SampleType::U8, false);
|
||||
if extras {
|
||||
let mut surface = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::U8, false));
|
||||
surface.fill_rect(doc.bounds(), &[0.5]);
|
||||
doc.channels.push(AlphaChannel::new("Selection", surface));
|
||||
}
|
||||
for name in [b"scale label".to_vec(), vec![0xe9], vec![0xff], vec![0xc3, 0xa9], (0x80..=0xff).collect()] {
|
||||
let mut image = photocraft_codecs::decode(&tiff(&doc, false)).unwrap();
|
||||
let mut res = image.meta.photoshop_resources.as_deref().map(|bytes| resources_from_bytes(bytes).unwrap().0).unwrap_or_default();
|
||||
res.retain(|resource| resource.id != SCALE);
|
||||
let mut scale_resource = ImageResource::new(SCALE, raw.clone());
|
||||
scale_resource.name = name.clone();
|
||||
res.push(scale_resource);
|
||||
image.meta.photoshop_resources = Some(resources_to_bytes(&res).unwrap());
|
||||
let mut bytes = photocraft_codecs::encode(&image, photocraft_codecs::Format::Tiff, &Default::default()).unwrap();
|
||||
for _ in 0..3 {
|
||||
let back = import("scale.tif", &bytes).unwrap().document;
|
||||
assert_eq!(back.measurement.scale, scale());
|
||||
assert_eq!(back.channels.len(), usize::from(extras));
|
||||
bytes = tiff(&back, false);
|
||||
let res = resources(&bytes);
|
||||
let scale_resource = res.iter().find(|resource| resource.id == SCALE).unwrap();
|
||||
assert_eq!(scale_resource.name, name, "extras={extras}: names are legacy bytes, even when they resemble UTF-8");
|
||||
assert_eq!(scale_resource.data, raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user