From 7bb971f77cb6521382b8580271342f0cc8b112e2 Mon Sep 17 00:00:00 2001 From: Rage Date: Thu, 8 Oct 2026 09:51:55 -0500 Subject: [PATCH] Fix PSD text fill after edits in Photopea --- ATTRIBUTION.md | 1 + crates/io/examples/psd_text_fill.rs | 28 +++++++++++ crates/text/src/psd.rs | 2 + crates/text/src/tests.rs | 69 +++++++++++++++++++++++++++ docs/images/psd-text-fill-after.png | Bin 0 -> 3864 bytes docs/images/psd-text-fill-before.png | Bin 0 -> 1232 bytes 6 files changed, 100 insertions(+) create mode 100644 crates/io/examples/psd_text_fill.rs create mode 100644 docs/images/psd-text-fill-after.png create mode 100644 docs/images/psd-text-fill-before.png diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index c6c4a0858b..4d61c087c9 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -50,6 +50,7 @@ The other built-in ICC profiles and the generated LUT looks are produced by code | `docs/brand/` (all files) | ArtCraft name, wordmark and mark | ArtCraft Team | getartcraft.com | Not open source; trademarks of the ArtCraft Team, [`docs/brand/LICENSE-brand.txt`](docs/brand/LICENSE-brand.txt) | | `docs/images/photocraft-*.jpg` | PhotoCraft screenshots | PhotoCraft contributors (UI) | Rendered offscreen with the `snapshot` example | MIT OR Apache-2.0 (UI); the artwork in each is public domain, listed below | | `docs/images/preferences-apply-*.png` | Preferences before and after adding Apply (no artwork) | PhotoCraft contributors | PhotoCraft control-channel capture and offscreen `snapshot` example | MIT OR Apache-2.0 | +| `docs/images/psd-text-fill-*.png` | Live text before and after the PSD fill fix | @ragedotdev | Photopea canvas captures of original synthetic text from `crates/io/examples/psd_text_fill.rs`; cropped to the document, with no Photopea UI assets | MIT OR Apache-2.0 | Artwork shown in the screenshots (all public domain, via Wikimedia Commons; details in [`docs/images/SOURCES.md`](docs/images/SOURCES.md)): diff --git a/crates/io/examples/psd_text_fill.rs b/crates/io/examples/psd_text_fill.rs new file mode 100644 index 0000000000..eaf73526ef --- /dev/null +++ b/crates/io/examples/psd_text_fill.rs @@ -0,0 +1,28 @@ +//! Write a small PSD for a live-text edit check in another editor. +//! `cargo run -p photocraft-io --example psd_text_fill -- /tmp/text-fill.psd` + +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::text::{CharStyle, TextRun}; +use photocraft_doc::{Document, Layer, LayerContent, Size, TextLayer}; +use photocraft_geom::Affine; + +fn main() -> Result<(), Box> { + let path = std::env::args_os().nth(1).ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "usage: psd_text_fill "))?; + let mut doc = Document::new("Text fill", Size::new(640, 240), ColorMode::Rgb, SampleType::U8); + let text = "Editable text"; + let mut layer = TextLayer { + text: text.into(), + runs: vec![TextRun { + len: text.len(), + style: CharStyle { font_family: "Inter".into(), size_pt: 48.0, color: Color::rgb(0.1, 0.2, 0.5), faux_bold: true, ..Default::default() }, + }], + transform: Affine { m: [1.2, 0.0, 0.0, 1.4, 30.0, 150.0] }, + ..Default::default() + }; + layer.sync_summary(); + photocraft_text::TextEngine::new().render_layer(&mut layer, doc.resolution_dpi, doc.pixel_format()); + doc.layers.push(Layer::new("Editable text", LayerContent::Text(layer))); + let output = photocraft_io::export(&doc, "text-fill.psd", &Default::default())?; + std::fs::write(path, output.bytes)?; + Ok(()) +} diff --git a/crates/text/src/psd.rs b/crates/text/src/psd.rs index 1f32fbefa8..cbf7d65d8c 100644 --- a/crates/text/src/psd.rs +++ b/crates/text/src/psd.rs @@ -594,6 +594,8 @@ pub(crate) fn style_sheet_data(s: &CharStyle, font: usize, k: f32) -> E { ("Strikethrough".into(), E::Bool(s.strikethrough)), ("Ligatures".into(), E::Bool(s.ligatures)), ("DLigatures".into(), E::Bool(s.discretionary_ligatures)), + // Photopea lays out edited text without painting it unless its fill is enabled. + ("FillFlag".into(), E::Bool(true)), ("FillColor".into(), E::Dict(vec![("Type".into(), E::Int(color_type)), ("Values".into(), E::Array(values))])), ]; dict.append(&mut opentype); diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index 5b107c7934..423629a684 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -545,6 +545,75 @@ fn txt2_carries_optical_kerning() { assert!(l.char_runs().iter().all(|r| r.style.kerning == Kerning::Optical)); } +/// Photopea needs an enabled fill, not just FillColor, to paint text after an edit. Inspect the +/// serialized TySh rather than our renderer, which does not consume the PSD fill flag. +#[test] +fn psd_text_fill_is_enabled_in_runs_and_new_default_styles() { + let mut t = point("AB", 100.0); + t.runs = vec![ + TextRun { len: 1, style: CharStyle { color: Color::rgb(0.2, 0.4, 0.6), ..Default::default() } }, + TextRun { len: 1, style: CharStyle { faux_bold: true, size_pt: 100.0, ..Default::default() } }, + ]; + for shape in [TextShape::Point, TextShape::Box { x: 0.0, y: 0.0, width: 300.0, height: 200.0 }] { + t.shape = shape; + let tysh = crate::psd::parse_tysh(&crate::psd::build_tysh(&t, 72.0, None)).unwrap(); + let data = crate::psd::engine_data(&tysh.text).unwrap(); + let runs = data.path(&["EngineDict", "StyleRun", "RunArray"]).unwrap().as_array().unwrap(); + assert_eq!(runs.len(), 2); + for run in runs { + assert_eq!(run.path(&["StyleSheet", "StyleSheetData", "FillFlag"]).and_then(crate::engine_data::Value::as_bool), Some(true)); + } + for key in ["ResourceDict", "DocumentResources"] { + let sheets = data.path(&[key, "StyleSheetSet"]).unwrap().as_array().unwrap(); + assert_eq!(sheets[0].path(&["StyleSheetData", "FillFlag"]).and_then(crate::engine_data::Value::as_bool), Some(true)); + } + let back = crate::psd::text_layer_from_tysh(&crate::psd::write_tysh(&tysh), 72.0).unwrap(); + assert_eq!(back.text, t.text); + assert_eq!(back.runs.len(), t.runs.len()); + for (actual, expected) in back.runs.iter().zip(&t.runs) { + assert_eq!(actual.style.color, expected.style.color); + assert_eq!(actual.style.size_pt, expected.style.size_pt); + assert_eq!(actual.style.faux_bold, expected.style.faux_bold); + } + } +} + +#[test] +fn psd_text_fill_is_enabled_after_editing_a_legacy_export() { + use crate::engine_data::Value as E; + // A legacy PhotoCraft export has a normal style and character runs without FillFlag. + let mut t = point("Before", 40.0); + let mut legacy = crate::psd::parse_tysh(&crate::psd::build_tysh(&t, 72.0, None)).unwrap(); + fn remove_fill_flags(value: &mut E) { + match value { + E::Dict(items) => { + items.retain(|(key, _)| key != "FillFlag"); + for (_, value) in items { + remove_fill_flags(value); + } + } + E::Array(items) => items.iter_mut().for_each(remove_fill_flags), + _ => {} + } + } + let mut data = crate::psd::engine_data(&legacy.text).unwrap(); + remove_fill_flags(&mut data); + for (key, value) in &mut legacy.text.items { + if key.as_bytes() == b"EngineData" { + *value = photocraft_psd::descriptor::Value::RawData(crate::engine_data::write(&data)); + } + } + t.psd_raw = Some(crate::psd::write_tysh(&legacy).into()); + t.text = "After".into(); + let tysh = crate::psd::parse_tysh(&crate::psd::build_tysh(&t, 72.0, None)).unwrap(); + let data = crate::psd::engine_data(&tysh.text).unwrap(); + let runs = data.path(&["EngineDict", "StyleRun", "RunArray"]).unwrap().as_array().unwrap(); + assert!(!runs.is_empty()); + for run in runs { + assert_eq!(run.path(&["StyleSheet", "StyleSheetData", "FillFlag"]).and_then(E::as_bool), Some(true)); + } +} + /// EngineData pair fields: manual kerning and "no automatic kerning" round-trip exactly, in /// Photoshop's form (see `psd::pair_runs`). #[test] diff --git a/docs/images/psd-text-fill-after.png b/docs/images/psd-text-fill-after.png new file mode 100644 index 0000000000000000000000000000000000000000..26b665e7e9fe58f75c2e6c238246f930e9578277 GIT binary patch literal 3864 zcmeHKX;2f$whm;#F)qv?0)quOqYNUlsSq55pvWMyghdfV2#NzDLXa&95C|&zZ~+Ao zWc7i-h%5rKCL{>5?;wx>Awq~r$i|igLY5cb)P41)UcG;}>i)UsM|W5CIbV03I_K;D zQr%n~_JFiN003Z*0Tf)=-~adB#tObKIw@?pQp?vE3icV=EQ+Cou@5Vq;ix&%M);teQ^FTa0jF0>fJJ@ z#FbG){+^+}*@r6H3BqZ0@fYsn*(RQ&(iTZbM)O0VWYhxgZS8=K^fqbXWnlRNQ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