Skip to content

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

Convert to Smart Object keeps a pattern fill's pattern (#1937)

Nigel Ihza Firdausy54d7b07acfea
diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rs
index 4e589a642..d10b3279d 100644
--- a/crates/engine/src/smart_cmds.rs
+++ b/crates/engine/src/smart_cmds.rs
@@ -597,6 +597,9 @@ pub fn layer_to_smart(doc: &Document, l: &Layer) -> Result<Layer> {
sub.resolution_dpi = doc.resolution_dpi;
sub.icc_profile = doc.icc_profile.clone();
sub.global_light = doc.global_light;
+ // Pattern fills and pattern layer effects reference document-level patterns by ID.
+ // Preserve those resources in the embedded document before rendering or saving it.
+ sub.patterns = doc.patterns.clone();
if any_layer(l, &|x| matches!(x.content, LayerContent::Smart(_))) {
// Nested PSD placed layers find their embedded files here.
sub.metadata.psd_global_blocks = doc.metadata.psd_global_blocks.clone();
diff --git a/crates/engine/src/smart_cmds/tests.rs b/crates/engine/src/smart_cmds/tests.rs
index 915ad2d8a..eebbb7e3e 100644
--- a/crates/engine/src/smart_cmds/tests.rs
+++ b/crates/engine/src/smart_cmds/tests.rs
@@ -115,6 +115,31 @@ fn masked_layer_and_group_convert_within_rounding() {
assert!(max_diff(&flat(&s), &before) <= 1.0 / 255.0 + 1e-6);
}
+#[test]
+fn pattern_fill_converts_to_editable_smart_object_without_losing_its_pattern() {
+ let mut s = session(8);
+ s.execute("layer.newFillLayer.pattern", json!({"pattern": "Bricks"})).unwrap();
+ let before = flat(&s);
+ let pattern_id = s.active().unwrap().doc.patterns[0].id.clone();
+ assert!(before.iter().any(|p| p[3] > 0.0), "the pattern fill is visible");
+
+ convert(&mut s);
+ assert!(max_diff(&flat(&s), &before) < 1e-6, "conversion keeps every pattern pixel");
+ let sm = active_smart(&s);
+ let SmartSource::Embedded { file_name, bytes } = &sm.source else { panic!("not embedded") };
+ let inner = decode_source(file_name, bytes).unwrap();
+ assert!(inner.patterns.iter().any(|p| p.id == pattern_id), "the embedded source keeps the pattern resource");
+
+ // The smart object's source remains live after opening and editing its contents.
+ let child = s.execute("layer.smartObjects.editContents", json!({})).unwrap()["document"].as_u64().unwrap() as usize;
+ assert_eq!(s.active_index(), Some(child));
+ assert!(s.active().unwrap().doc.patterns.iter().any(|p| p.id == pattern_id));
+ s.set_active(0);
+ assert!(max_diff(&flat(&s), &before) < 1e-6);
+ s.undo();
+ assert!(max_diff(&flat(&s), &before) < 1e-6);
+}
+
#[test]
fn empty_layer_cannot_be_converted() {
let mut s = session(8);