From e86e6910ae58d828cdc7b50dbb64dcb02662d50c Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Tue, 20 Oct 2026 20:07:39 +0200 Subject: [PATCH] fix(psd): bound the base style run a save copies per run The base run comes from the file and is cloned once per character run, so it now has to fit the template node budget, and each font is looked up once per name. Also names the reopened-layer tuple in psd_kerning for clippy::type_complexity. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/io/tests/psd_kerning.rs | 7 +++++-- crates/text/src/psd.rs | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/io/tests/psd_kerning.rs b/crates/io/tests/psd_kerning.rs index 6eeebc43b4..ad492fce25 100644 --- a/crates/io/tests/psd_kerning.rs +++ b/crates/io/tests/psd_kerning.rs @@ -192,12 +192,15 @@ fn photoshop_txt2(objects: &[(&str, &str)]) -> Vec { format!("\n\n/98 << /0 7 >> /0 << /1 << /0 [ << /0 << /99 /CoolTypeFont /0 << /0 ({bom}{font}) /2 1 >> >> >> ] >> /8 << /0 [ {frames}] >> >> /1 << /0 << /0 1 >> /1 [ {body}] /2 << /0 7 >> >>").chars().map(|c| c as u32 as u8).collect() } +/// A reopened type layer: its name, `TextIndex` and font features. +type Reopened = (String, Option, Vec); + /// A Photoshop file whose `Txt2` holds `objects` and whose type layers are `kept` (text and /// `TextIndex`, bottom first), with a type layer "Hi" made the way the Type tool makes one /// (`build_tysh`, no index of its own) inserted at `at`, saved and reopened: each layer's /// index and font features (but `calt`, on by default and written on as Photoshop does), and /// "Hi"'s style run in the saved `Txt2`. -fn add_type_layer(objects: &[(&str, &str)], kept: &[(&str, i32)], at: usize) -> (Vec<(String, Option, Vec)>, E) { +fn add_type_layer(objects: &[(&str, &str)], kept: &[(&str, i32)], at: usize) -> (Vec, E) { let mut doc = Document::new("k", photocraft_geom::Size::new(64, 32), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); for (text, index) in kept { let mut t = layer(text, &[(text.len(), Kerning::Metrics, 0.0)]); @@ -231,7 +234,7 @@ fn add_type_layer(objects: &[(&str, &str)], kept: &[(&str, i32)], at: usize) -> } /// The layer `name` in `add_type_layer`'s result. -fn named<'a>(layers: &'a [(String, Option, Vec)], name: &str) -> &'a (String, Option, Vec) { +fn named<'a>(layers: &'a [Reopened], name: &str) -> &'a Reopened { layers.iter().find(|l| l.0 == name).unwrap() } diff --git a/crates/text/src/psd.rs b/crates/text/src/psd.rs index 6d8fd8f454..aaa901eb2d 100644 --- a/crates/text/src/psd.rs +++ b/crates/text/src/psd.rs @@ -944,9 +944,10 @@ fn txt2_mode_runs(layer: &TextLayer) -> Vec<(i64, usize)> { /// a missing font is appended to), size (`/1`), faux bold and italic, leading, scales and tracking /// (`/2`–`/8`, EngineData's order), fill colour (`/53`), the OpenType keys and the auto-kern mode /// (`/11`). Photoshop reads the character style from here rather than from EngineData, and quit -/// on a run holding only the auto-kern mode. `None` when `base` has no style dictionary. +/// on a run holding only the auto-kern mode. `None` when `base` has no style dictionary or is +/// larger than a template may be (it is copied once per run). fn txt2_style_runs(layer: &TextLayer, base: &E, fonts: &mut Vec, dpi: f32) -> Option> { - if !matches!(base.path(&["0", "0", "6"]), Some(E::Dict(_))) { + if !matches!(base.path(&["0", "0", "6"]), Some(E::Dict(_))) || !fits(base, &mut { TEMPLATE_NODES }) { return None; } let k = if dpi.is_finite() && dpi > 0.0 { dpi / 72.0 } else { 1.0 }; @@ -967,9 +968,10 @@ fn txt2_style_runs(layer: &TextLayer, base: &E, fonts: &mut Vec, dpi: f32) -> } let default = CharStyle::default(); let mut out = Vec::with_capacity(spans.len()); + let mut looked_up = std::collections::HashMap::new(); for (ri, n) in spans { let s = runs.get(ri).map_or(&default, |r| &r.style); - let font = txt2_font(fonts, &postscript_for(s)); + let font = *looked_up.entry(postscript_for(s)).or_insert_with_key(|name| txt2_font(fonts, name)); let mut run = base.clone(); let style = run.get_mut("0")?.get_mut("0")?.get_mut("6")?; if let Some(font) = font {