Skip to content

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

fix(psd): bound the base style run a save copies per run

NetterSilvere86e6910ae58
diff --git a/crates/io/tests/psd_kerning.rs b/crates/io/tests/psd_kerning.rs
index 6eeebc43b..ad492fce2 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<u8> {
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<i32>, Vec<String>);
+
/// 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<i32>, Vec<String>)>, E) {
+fn add_type_layer(objects: &[(&str, &str)], kept: &[(&str, i32)], at: usize) -> (Vec<Reopened>, 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<i32>, Vec<String>)], name: &str) -> &'a (String, Option<i32>, Vec<String>) {
+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 6d8fd8f45..aaa901eb2 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<E>, dpi: f32) -> Option<Vec<E>> {
- 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<E>, 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 {