diff --git a/crates/engine/src/cmd/object/pixelgrid.rs b/crates/engine/src/cmd/object/pixelgrid.rs index a8bb12b429..ba95db0d69 100644 --- a/crates/engine/src/cmd/object/pixelgrid.rs +++ b/crates/engine/src/cmd/object/pixelgrid.rs @@ -358,9 +358,10 @@ fn snap_mask(n: &mut Node) -> bool { /// Move symbol instance `n` to whole points when its transform places its symbol's art at the /// symbol's natural size (`natural`, by symbol name), reflected across one axis or not. A scaled, -/// rotated (a half turn included) or sheared instance stays where it is. +/// rotated (a half turn included) or sheared instance stays where it is. The instance moves as any +/// move moves it ([`Node::transform`]), so the art of a linked opacity mask moves with it. fn snap_instance(n: &mut Node, natural: &HashMap) { - let NodeKind::SymbolInstance { symbol, xf } = &mut n.kind else { return }; + let NodeKind::SymbolInstance { symbol, xf } = &n.kind else { return }; let Some(nat) = natural.get(symbol.as_str()) else { return }; let [a, b, c, d, e, f] = xf.as_coeffs(); let [na, _, _, nd, _, _] = nat.as_coeffs(); @@ -373,7 +374,12 @@ fn snap_instance(n: &mut Node, natural: &HashMap) { return; } let (x, y) = (e.round(), f.round()); - if x != e || y != f { + if x == e && y == f { + return; + } + n.transform(Affine::translate((x - e, y - f)), Scaling::default()); + // Whole points exactly, which adding the offset to the old position need not give. + if let NodeKind::SymbolInstance { xf, .. } = &mut n.kind { *xf = Affine::new([a, b, c, d, x, y]); } } diff --git a/crates/engine/src/tests/pixelalign.rs b/crates/engine/src/tests/pixelalign.rs index b70098d9c9..1258ae58da 100644 --- a/crates/engine/src/tests/pixelalign.rs +++ b/crates/engine/src/tests/pixelalign.rs @@ -1436,6 +1436,40 @@ fn an_instance_in_an_opacity_mask_snaps_with_its_symbol() { assert!(whole(&[(e, f)]), "{e} {f}"); } +/// An instance snapped to whole points takes the art of its linked opacity mask along by the same +/// amount, as a move does; the art of an unlinked mask stays where it is. +#[test] +fn an_instance_snapped_to_the_grid_moves_its_linked_mask() { + 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); + s.execute("symbol.new", &json!({"name": "Px"})).unwrap(); + let place = |s: &mut Session, x: f64, linked: bool| { + let inst = s.execute("symbol.place", &json!({"name": "Px", "x": x, "y": 200.7})).unwrap()["id"].as_u64().unwrap(); + let m = path(s, "M80.25 180.5 L160.25 180.5 L160.25 240.5 L80.25 240.5 Z", 1.0); + let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); + let art = d.remove(NodeId(m)).unwrap(); + let mask = vectorcraft_doc::OpacityMask { linked, ..vectorcraft_doc::OpacityMask::new((*art).clone(), true) }; + d.node_mut(NodeId(inst)).unwrap().extras_mut().mask = Some(Box::new(mask)); + inst + }; + let (linked, unlinked) = (place(&mut s, 100.3, true), place(&mut s, 300.3, false)); + let mask_points = |s: &Session, inst: u64| { + let d = &s.doc().unwrap().doc; + let mask = d.node(NodeId(inst)).unwrap().extras().mask.clone().unwrap(); + mask.art.path_data().unwrap().anchors().map(|(_, _, a)| (a.p.x, a.p.y)).collect::>() + }; + let (xf_before, mask_before, unlinked_before) = (instance_xf(&s, linked), mask_points(&s, linked), mask_points(&s, unlinked)); + s.execute("symbol.options", &json!({"name": "Px", "alignToPixelGrid": true})).unwrap(); + let [.., e0, f0] = xf_before.as_coeffs(); + let [.., e, f] = instance_xf(&s, linked).as_coeffs(); + assert!(whole(&[(e, f)]) && (e, f) != (e0, f0), "{e} {f}"); + let moved: Vec<_> = mask_before.iter().map(|(x, y)| (x + e - e0, y + f - f0)).collect(); + let after = mask_points(&s, linked); + assert!(moved.iter().zip(&after).all(|(a, b)| close(*a, *b)), "{after:?} != {moved:?}"); + assert!(whole(&placed_points(&s, unlinked))); + assert_eq!(mask_points(&s, unlinked), unlinked_before); +} + /// While a pixel-aligned symbol is edited, the art written back from the editing layer is stored /// on the pixel grid, so its instances stay on it. #[test]