From 2a973becbc8723d2fa7253e5dcbf35d30bfc10ca Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 17:15:50 -0800 Subject: [PATCH] Symbols: Align to Pixel Grid leaves symbol editing layers to their symbol - An instance of a pixel-aligned symbol on another symbol's editing layer is part of that symbol's art, so it is no longer snapped and written into that art. - Turning Align to Pixel Grid on for the symbol being edited now writes its art back and shows it snapped on the editing layer at once, instead of after the next edit. --- crates/engine/src/cmd/object/pixelgrid.rs | 15 ++++++-- crates/engine/src/cmd/symboledit.rs | 3 +- crates/engine/src/tests/pixelalign.rs | 46 +++++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/object/pixelgrid.rs b/crates/engine/src/cmd/object/pixelgrid.rs index 5cfc22b7f8..8d2a344ffe 100644 --- a/crates/engine/src/cmd/object/pixelgrid.rs +++ b/crates/engine/src/cmd/object/pixelgrid.rs @@ -166,8 +166,8 @@ const ACTUAL_SIZE_TOLERANCE: f64 = 1e-9; /// (while it is edited, the art snaps as the editing layer is written back to it). Every instance /// of such a symbol then goes to whole points ([`snap_instance`]), and so does each instance of a /// pixel-aligned symbol in an object the edit changed or added, the art of opacity masks included. -/// An instance in another symbol's art is placed by that symbol's instances and is left as it is. -/// A document without pixel-aligned symbols is not walked. +/// An instance in another symbol's art, the art on the symbol editing layer included, is placed by +/// that symbol's instances and is left as it is. A document without pixel-aligned symbols is not walked. fn refresh_symbols(before: &Document, doc: &mut Document) { let aligned = doc.pixel_aligned_symbols(); if aligned.is_empty() { @@ -193,16 +193,23 @@ fn refresh_symbols(before: &Document, doc: &mut Document) { } } let natural: HashMap = aligned.iter().map(|name| (name.clone(), doc.symbol_natural_xf(name))).collect(); + // The symbol editing layer holds another symbol's art. + let editing = doc.symbol_edit.as_ref().map(|e| e.layer); if every { for layer in doc.layers.iter_mut() { - if holds_instance(layer, &natural) { + if Some(layer.id) != editing && holds_instance(layer, &natural) { snap_instances_in(Arc::make_mut(layer), &natural); } } return; } let (mut ids, mut masked) = (vec![], vec![]); - changed_instances(&before.layers, &doc.layers, &natural, &mut ids, &mut masked); + if let Some(layer) = editing { + let outside = |layers: &[Arc]| layers.iter().filter(|l| l.id != layer).cloned().collect::>(); + changed_instances(&outside(&before.layers), &outside(&doc.layers), &natural, &mut ids, &mut masked); + } else { + changed_instances(&before.layers, &doc.layers, &natural, &mut ids, &mut masked); + } for id in ids { if let Some(n) = doc.node_mut(id) { snap_instance(n, &natural); diff --git a/crates/engine/src/cmd/symboledit.rs b/crates/engine/src/cmd/symboledit.rs index c3fc9a4392..77266b5aaa 100644 --- a/crates/engine/src/cmd/symboledit.rs +++ b/crates/engine/src/cmd/symboledit.rs @@ -495,7 +495,8 @@ pub(crate) fn sync(before: &Document, d: &mut Document) -> Result<()> { let (old, old_group) = match before.symbol_edit.as_ref().filter(|b| b.layer == se.layer) { None => return Ok(()), Some(b) => match art_of(before) { - Some(old) if same(&old) => return Ok(()), + // Turning on Align to Pixel Grid writes the art back too, so the layer shows it snapped. + Some(old) if same(&old) && (before.symbol_pixel_aligned(&se.symbol) || !d.symbol_pixel_aligned(&se.symbol)) => return Ok(()), old => (old.unwrap_or_default(), b.group), }, }; diff --git a/crates/engine/src/tests/pixelalign.rs b/crates/engine/src/tests/pixelalign.rs index 1258ae58da..00b4cce694 100644 --- a/crates/engine/src/tests/pixelalign.rs +++ b/crates/engine/src/tests/pixelalign.rs @@ -1493,3 +1493,49 @@ fn art_edited_in_symbol_editing_mode_stays_on_the_pixel_grid() { let pts = placed_points(&s, inst); assert!(whole(&pts), "{pts:?}"); } + +/// Turning it on while the symbol is open in symbol editing mode shows its art on the pixel grid +/// on the editing layer at once, and its instance stays on it after editing ends. +#[test] +fn turning_it_on_in_symbol_editing_mode_snaps_the_editing_layer() { + let mut s = new_doc(); + path(&mut s, "M10.3 20.6 L40.7 20.6 L40.7 30.3 L10.3 30.3 Z", 2.0); + let inst = s.execute("symbol.new", &json!({"name": "Px"})).unwrap()["id"].as_u64().unwrap(); + s.execute("symbol.edit", &json!({"ids": [inst]})).unwrap(); + let st = s.doc().unwrap(); + let layer = st.doc.symbol_edit.as_ref().unwrap().layer; + let part = st.doc.node(layer).unwrap().children().unwrap()[0].id.0; + let before = points(&s, part); + s.execute("symbol.options", &json!({"name": "Px", "alignToPixelGrid": true})).unwrap(); + let d = &s.doc().unwrap().doc; + let xf = d.symbol_edit.as_ref().unwrap().xf; + let art = &d.symbols.iter().find(|x| x.name == "Px").unwrap().art; + let stored: Vec<_> = art.path_data().unwrap().anchors().map(|(_, _, a)| xf * a.p).map(|p| (p.x, p.y)).collect(); + let shown = points(&s, part); + assert!(shown.iter().zip(&stored).all(|(a, b)| close(*a, *b)) && shown != before, "{shown:?} != {stored:?}"); + s.execute("symbol.exitEditing", &json!({})).unwrap(); + let pts = placed_points(&s, inst); + assert!(whole(&pts), "{pts:?}"); +} + +/// An instance of a pixel-aligned symbol on the editing layer of another symbol is part of that +/// symbol's art, so turning it on leaves the instance and the other symbol's art where they are. +#[test] +fn an_instance_on_another_symbols_editing_layer_is_left_as_it_is() { + let mut s = new_doc(); + path(&mut s, "M10.3 20.6 L40.7 20.6 L40.7 30.3 L10.3 30.3 Z", 2.0); + let a = s.execute("symbol.new", &json!({"name": "Px"})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [a]})).unwrap(); + let b = s.execute("symbol.new", &json!({"name": "Outer"})).unwrap()["id"].as_u64().unwrap(); + s.execute("symbol.edit", &json!({"ids": [b]})).unwrap(); + let st = s.doc().unwrap(); + let layer = st.doc.symbol_edit.as_ref().unwrap().layer; + let inner = st.doc.node(layer).unwrap().children().unwrap()[0].id.0; + let outer_art = st.doc.symbols.iter().find(|x| x.name == "Outer").unwrap().art.clone(); + let shown = instance_xf(&s, inner); + assert!(!whole(&placed_points(&s, inner))); + s.execute("symbol.options", &json!({"name": "Px", "alignToPixelGrid": true})).unwrap(); + assert_eq!(instance_xf(&s, inner), shown); + let d = &s.doc().unwrap().doc; + assert!(std::sync::Arc::ptr_eq(&d.symbols.iter().find(|x| x.name == "Outer").unwrap().art, &outer_art)); +}