PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fix PSD text fill after edits in Photopea
Rage7bb971f77cb6
diff --git a/ATTRIBUTION.md b/ATTRIBUTION.mdindex c6c4a0858..4d61c087c 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.rsnew file mode 100644index 000000000..eaf73526e--- /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<dyn std::error::Error>> {+ let path = std::env::args_os().nth(1).ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "usage: psd_text_fill <output.psd>"))?;+ 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.rsindex 1f32fbefa..cbf7d65d8 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.rsindex 5b107c793..423629a68 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.pngnew file mode 100644index 000000000..26b665e7eBinary files /dev/null and b/docs/images/psd-text-fill-after.png differdiff --git a/docs/images/psd-text-fill-before.png b/docs/images/psd-text-fill-before.pngnew file mode 100644index 000000000..3af8e749eBinary files /dev/null and b/docs/images/psd-text-fill-before.png differ