PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(io): a fresh Metrics type layer writes no empty Txt2 stub (#3514) (#3529)
Scott C Hughesee05bc833bac
diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rsindex bbbb11992..771ebcb78 100644--- a/crates/io/src/psd_export.rs+++ b/crates/io/src/psd_export.rs@@ -1510,7 +1510,12 @@ fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32, c}).collect();let previous = (!text_layers.is_empty()).then(|| preserved.iter().find(|(_, key, _)| key == b"Txt2").map(|(_, _, data)| data.to_vec())).flatten();- let txt2 = (!text_layers.is_empty()).then(|| photocraft_text::psd::build_txt2(&text_layers, previous.as_deref()));+ // A fresh document of ordinary (Metrics) text has nothing to put in `Txt2`, and Photoshop never+ // writes its empty `<< /0 << >> >>` stub — readers such as Photopea reject it (#3514). Keep the+ // block when the file had one (round-trip its pen positions/extras) or a layer is Optical (the+ // one mode EngineData can't carry; #1348).+ let txt2 =+ (previous.is_some() || photocraft_text::psd::needs_txt2(&text_layers)).then(|| photocraft_text::psd::build_txt2(&text_layers, previous.as_deref()));for (sig, key, data) in preserved {if txt2.is_some() && key == *b"Txt2" {continue;diff --git a/crates/io/tests/psd_kerning.rs b/crates/io/tests/psd_kerning.rsindex c3c4a09f3..b75a6542a 100644--- a/crates/io/tests/psd_kerning.rs+++ b/crates/io/tests/psd_kerning.rs@@ -101,6 +101,25 @@ fn optical_kerning_round_trips_through_psd() {}}+/// #3514: a fresh document of ordinary Metrics type writes no document `Txt2` block — Photoshop+/// never writes its empty `<< /0 << >> >>` stub, and readers such as Photopea reject it — while an+/// Optical layer still does, because its mode lives only in `Txt2`.+#[test]+fn fresh_metrics_text_writes_no_txt2_block() {+ use photocraft_doc::text::Kerning;+ let saves_txt2 = |kerning: Kerning| {+ let t = layer("AV", &[(2, kerning, 0.0)]);+ let mut doc = Document::new("k", photocraft_geom::Size::new(64, 32), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8);+ let mut r = t.clone();+ photocraft_text::TextEngine::new().render_layer(&mut r, doc.resolution_dpi, doc.pixel_format());+ doc.layers.push(Layer::new("t", LayerContent::Text(r)));+ let bytes = photocraft_io::export(&doc, "k.psd", &Default::default()).unwrap().bytes;+ photocraft_psd::PsdFile::from_bytes(&bytes).unwrap().global_blocks.iter().any(|b| &b.key == b"Txt2")+ };+ assert!(!saves_txt2(Kerning::Metrics), "a plain Metrics layer must not write the empty Txt2 stub");+ assert!(saves_txt2(Kerning::Optical), "Optical lives only in Txt2");+}+/// Two imported layers can name the same Txt2 object; export must update the second/// preserved TySh pointer so each layer restores its own automatic kerning mode.#[test]diff --git a/crates/text/src/psd.rs b/crates/text/src/psd.rsindex 85ce2f6a0..c6724bf2c 100644--- a/crates/text/src/psd.rs+++ b/crates/text/src/psd.rs@@ -743,6 +743,15 @@ fn txt2_mode_runs(layer: &TextLayer) -> Vec<(i64, usize)> {/// `i32::MAX`, and sizing the array by it would make every save allocate gigabytes.pub const MAX_TEXT_INDEX: i32 = 0xFFFF;+/// Whether the document `Txt2` block is needed for `objects`: only a non-Metrics auto-kern mode+/// (`Optical`) is exclusive to `Txt2` — EngineData's `AutoKerning` cannot distinguish Metrics from+/// Optical — so a fresh document of ordinary Metrics text has nothing to put in the block.+/// Writing Photoshop's empty `<< /0 << >> >>` stub anyway makes some readers (Photopea) fail+/// (#3514); the export omits the block for such a document.+pub fn needs_txt2(objects: &[(i32, &TextLayer)]) -> bool {+ objects.iter().any(|(_, l)| l.char_runs().iter().any(|r| r.style.kerning == Kerning::Optical))+}+/// The document's `Txt2` block for `objects`: `(TextIndex, layer)` pairs, one text object each/// at the slot its `TextIndex` names (see [`set_text_index`] and [`text_index`]; unused slots are/// empty dictionaries), carrying the automatic kerning mode of every pair (`/11`: 0 manual,diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rsindex 986211bcb..fa0a626fe 100644--- a/crates/text/src/tests.rs+++ b/crates/text/src/tests.rs@@ -735,6 +735,22 @@ fn build_txt2_round_trips_kerning_modes() {assert_eq!(modes, vec![(1, Kerning::Off), (1, Kerning::Metrics)]);}+/// #3514: the document `Txt2` is only needed for a non-Metrics (`Optical`) auto-kern mode. A fresh+/// document of ordinary Metrics (or EngineData-carried Off) text must not write Photoshop's empty+/// stub, which some readers (Photopea) reject.+#[test]+fn needs_txt2_only_for_optical() {+ use photocraft_doc::text::Kerning;+ let style = |kerning: Kerning| CharStyle { kerning, ..Default::default() };+ let metrics = runs_of("AV", &[(2, style(Kerning::Metrics))]);+ assert!(!crate::psd::needs_txt2(&[(0, &metrics)]));+ let off = runs_of("AV", &[(2, style(Kerning::Off))]);+ assert!(!crate::psd::needs_txt2(&[(0, &off)]), "Off lives in EngineData, not Txt2");+ let optical = runs_of("AV", &[(1, style(Kerning::Optical)), (1, CharStyle::default())]);+ assert!(crate::psd::needs_txt2(&[(0, &optical)]));+ assert!(!crate::psd::needs_txt2(&[]));+}+/// A kept `Txt2` object — same text, so its extras are still true — keeps everything the file/// held beyond the regenerated keys (Photoshop stores glyph pen positions under `/21 /1`), and a/// changed text drops those extras with the stale runs. The block's own extras always survive.