diff --git a/crates/doc/src/pattern.rs b/crates/doc/src/pattern.rs index f3e16081386..869576da2f5 100644 --- a/crates/doc/src/pattern.rs +++ b/crates/doc/src/pattern.rs @@ -72,7 +72,9 @@ pub struct Pattern { /// Indexed source stores an RGB surface), so this is the only place the mode survives. `None` /// for a pattern whose source is not a document of a known mode: the built-ins, and patterns /// read from a `.pat` or PSD block, which say what their pixels are but not what they came - /// from. Not part of the id: the same pixels keep one id whatever their source. + /// from. Not part of the id: the same pixels keep one id whatever their source, and the id is + /// how the library and a document dedupe, so defining pixels that are already stored keeps the + /// source mode of the pattern that got there first. pub source_mode: Option, pub surface: Surface, } diff --git a/crates/engine-core/src/preset_store/tests.rs b/crates/engine-core/src/preset_store/tests.rs index c30ae0c32e4..1d1af1e8fce 100644 --- a/crates/engine-core/src/preset_store/tests.rs +++ b/crates/engine-core/src/preset_store/tests.rs @@ -563,6 +563,20 @@ fn patterns_survive_a_restart() { let (u, _) = session(&dir); assert_eq!(user_patterns(&u), [("Toner".to_string(), 8)]); + + // An index written before the source member: the pattern still loads, whole but with no + // source recorded, so nothing written by the older build is lost or refused differently. + let index = dir.0.join(PATTERNS_FILE); + let mut manifest: serde_json::Value = serde_json::from_slice(&std::fs::read(&index).unwrap()).unwrap(); + for entry in manifest["patterns"].as_array_mut().unwrap() { + assert!(entry.as_object_mut().unwrap().remove("source_mode").is_some(), "the writer named the member"); + } + std::fs::write(&index, serde_json::to_vec_pretty(&manifest).unwrap()).unwrap(); + let (older, w) = session(&dir); + assert!(w.iter().all(|w| !w.contains("Toner")), "{w:?}"); + let toner = older.patterns.items.iter().find(|p| p.name == "Toner").cloned().unwrap(); + assert_eq!(toner.source_mode, None, "an older index records no source"); + assert_eq!((toner.width, toner.height), (8, 6), "its pixels and size still load"); } fn define_pattern(s: &mut Session, name: &str, color: &str, width: u32) { diff --git a/crates/engine-paint/src/cmd/retouch_cmds/pattern_stamp_indexed_tests.rs b/crates/engine-paint/src/cmd/retouch_cmds/pattern_stamp_indexed_tests.rs index 58486be91a2..aa2cab91bc8 100644 --- a/crates/engine-paint/src/cmd/retouch_cmds/pattern_stamp_indexed_tests.rs +++ b/crates/engine-paint/src/cmd/retouch_cmds/pattern_stamp_indexed_tests.rs @@ -146,6 +146,35 @@ fn an_indexed_source_pattern_paints_in_other_documents_as_before() { } } +/// The one measured row of #12887 this change knowingly does not keep: the reference refuses an +/// Indexed pattern from ANOTHER image, whose colours are not in the document's colour table, and +/// paints only the one that carries that table. PhotoCraft records the source mode, not the table, +/// so this paints - the deviation tracked as #16642, pinned here exactly as it behaves, with the +/// landed colours shown to be outside the document's own table. +#[test] +fn a_pattern_defined_in_another_indexed_document_paints_today() { + let mut s = crate::session(); + let pat = indexed_doc_and_pattern(&mut s); + // A second Indexed document, cyan over magenta: its own exact palette, which holds none of the + // tile's red, green and blue. + s.execute("file.new", json!({"width": 64, "height": 64, "depth": 8, "mode": "rgb"})).unwrap(); + for (y, color) in [(0, "#00ffff"), (32, "#ff00ff")] { + s.execute("select.rect", json!({"x": 0, "y": y, "width": 64, "height": 32})).unwrap(); + s.execute("edit.fill", json!({"color": color})).unwrap(); + } + s.execute("select.deselect", json!({})).unwrap(); + to_indexed(&mut s); + let table = s.active().unwrap().doc.color_table.clone().expect("an Indexed document has a colour table"); + assert!(!table.colors.contains(&[255, 0, 0]), "the second table holds {table:?}"); + let before = state(&s); + assert_eq!(press_refusal(&s, &stroke(&pat, false)), None, "no refusal for a foreign Indexed source"); + s.execute("paint.patternStamp", stroke(&pat, false)).unwrap_or_else(|e| panic!("foreign Indexed source: {e}")); + let after = state(&s); + assert_eq!(after.1, before.1 + 1, "one history step"); + assert_eq!(after.0[25 * 64 + 5], [1.0, 0.0, 0.0, 1.0], "the tile's red lands, though this table has no red"); + assert_eq!(after.0[25 * 64 + 12], [0.0, 1.0, 0.0, 1.0], "and its green, which this table lacks too"); +} + #[test] fn a_pattern_whose_source_is_not_an_indexed_image_is_refused_in_an_indexed_document() { let mut s = crate::session(); diff --git a/crates/io/src/pattern_map.rs b/crates/io/src/pattern_map.rs index 3989a0b101b..e345c4d0b24 100644 --- a/crates/io/src/pattern_map.rs +++ b/crates/io/src/pattern_map.rs @@ -265,6 +265,8 @@ pub fn export_global_blocks_with_warnings(doc: &Document, warnings: &mut Vec, limit: u64) -> Vec { let raw = &doc.metadata.psd_global_blocks; + // The comparison covers every field, `Pattern::source_mode` included; a pattern this reader + // decoded names no source on either side, so a file's blocks are still reused verbatim. if from_global_blocks(doc) == doc.patterns { return repair_legacy_blocks(raw); }