Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Integration: keep unreadable canonical type as a type layer (#4771)

David Mann152c9677da7e
diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs
index b9078d823..55a549b40 100644
--- a/crates/io/src/psd_import.rs
+++ b/crates/io/src/psd_import.rs
@@ -4,7 +4,7 @@ use std::sync::Arc;
use photocraft_color::{BlendMode, ColorMode, PixelFormat, SampleType};
use photocraft_doc::{
- AlphaChannel, Document, Effects, FillCache, Group, Layer, LayerContent, LayerMask, ShapeLayer, SmartContentsId, SmartObject, SmartSource,
+ AlphaChannel, Document, Effects, FillCache, Group, Layer, LayerContent, LayerMask, ShapeLayer, SmartContentsId, SmartObject, SmartSource, TextLayer,
};
use photocraft_geom::{Rect, Size, TILE_SIZE};
use photocraft_psd::layer::{CHANNEL_REAL_USER_MASK, CHANNEL_TRANSPARENCY, CHANNEL_USER_MASK};
@@ -362,9 +362,18 @@ impl Ctx<'_> {
}
LayerContent::Adjustment(adj)
} else if let Some(block) = rec.block(b"TySh") {
- // Only canonical, safely decoded type data can become editable text. A retained
- // foreign-order block must never be handed to the big-endian reader (#4758).
- let parsed = (!block.is_foreign_order()).then(|| photocraft_text::psd::text_layer_from_tysh(&block.data, self.dpi)).flatten();
+ // Only canonical type data with a safe transform can become a text layer. A retained
+ // foreign-order block must never be handed to the big-endian reader (#4758). Canonical
+ // data the typed reader can't decode stays a type layer as before (usually without
+ // text), so its TySh is saved back verbatim instead of being dropped with the raster.
+ let parsed = (!block.is_foreign_order())
+ .then(|| {
+ photocraft_text::psd::text_layer_from_tysh(&block.data, self.dpi).or_else(|| {
+ let (text, transform) = blocks::parse_tysh(&block.data).unwrap_or_default();
+ photocraft_text::psd::transform_is_safe(&transform).then(|| TextLayer { text, transform, ..Default::default() })
+ })
+ })
+ .flatten();
if let Some(mut t) = parsed {
if let Some(txt2) = &self.txt2 {
photocraft_text::psd::apply_txt2(&mut t, &block.data, txt2);
@@ -378,7 +387,7 @@ impl Ctx<'_> {
t.psd_raw = principal(b"TySh");
LayerContent::Text(t)
} else {
- self.warn(format!("layer \"{name}\": unreadable or unsafe type geometry; kept as pixels with its original type data"));
+ self.warn(format!("layer \"{name}\": type data in another byte order or with unsafe type geometry; kept as pixels"));
LayerContent::Raster(self.record_surface(rec, &name))
}
} else if let Some(k) = smart_key {
diff --git a/crates/io/tests/tiff_type_geometry.rs b/crates/io/tests/tiff_type_geometry.rs
index a64aeb7c2..8a04c2dc9 100644
--- a/crates/io/tests/tiff_type_geometry.rs
+++ b/crates/io/tests/tiff_type_geometry.rs
@@ -67,3 +67,20 @@ fn foreign_or_unsafe_type_keeps_cached_pixels_without_editable_geometry() {
assert!(opened.layers[0].psd_blocks.iter().any(|(k, data)| k == b"TySh" && **data == raw));
}
}
+
+#[test]
+fn canonical_type_the_typed_reader_rejects_stays_type_and_saves_its_block() {
+ let doc = document();
+ let (mut psd, _) = document_to_psd_with(&doc, &PsdExportOptions::default());
+ let block = psd.layer_info.as_mut().unwrap().layers[0].blocks.iter_mut().find(|b| b.key == *b"TySh").unwrap();
+ // A text descriptor version the typed reader doesn't know; the matrix is ordinary.
+ block.data[52..56].copy_from_slice(&17u32.to_be_bytes());
+ let raw = block.data.clone();
+ let (opened, warnings) = psd_to_document(&psd);
+ assert!(!warnings.iter().any(|w| w.contains("unsafe type geometry")), "{warnings:?}");
+ let LayerContent::Text(t) = &opened.layers[0].content else { panic!("canonical type must stay a type layer") };
+ let LayerContent::Text(before) = &doc.layers[0].content else { panic!("source type") };
+ assert_eq!(t.transform, before.transform);
+ let (saved, _) = document_to_psd_with(&opened, &PsdExportOptions::default());
+ assert_eq!(saved.layers()[0].block(b"TySh").map(|b| &b.data), Some(&raw), "PSD save must keep the type block");
+}
diff --git a/crates/text/src/psd.rs b/crates/text/src/psd.rs
index 864f8697d..cc49b37a3 100644
--- a/crates/text/src/psd.rs
+++ b/crates/text/src/psd.rs
@@ -30,6 +30,12 @@ pub struct TySh {
pub bounds: [f32; 4],
}
+/// Whether a type transform can reach rendering or egui. Pixel coordinates use i32 throughout
+/// the document, so non-finite and unrepresentable affine terms are rejected (#4758).
+pub fn transform_is_safe(t: &Affine) -> bool {
+ t.m.iter().all(|v| v.is_finite() && v.abs() <= f64::from(i32::MAX))
+}
+
/// Parses a `TySh` block.
pub fn parse_tysh(data: &[u8]) -> Option<TySh> {
let f = |at: usize| data.get(at..at + 8).map(|b| f64::from_be_bytes(b.try_into().unwrap_or([0; 8])));
@@ -37,9 +43,7 @@ pub fn parse_tysh(data: &[u8]) -> Option<TySh> {
for (i, v) in m.iter_mut().enumerate() {
*v = f(2 + i * 8)?;
}
- // Pixel coordinates use i32 throughout the document. Reject non-finite and
- // unrepresentable affine terms before they reach rendering or egui (#4758).
- if !m.iter().all(|v| v.is_finite() && v.abs() <= f64::from(i32::MAX)) {
+ if !transform_is_safe(&Affine { m }) {
return None;
}
let (text, used) = VersionedDescriptor::parse_prefix(data.get(52..)?).ok()?;