diff --git a/crates/svg/src/export/paint.rs b/crates/svg/src/export/paint.rs index 7e67f09cf7..f52a701867 100644 --- a/crates/svg/src/export/paint.rs +++ b/crates/svg/src/export/paint.rs @@ -37,9 +37,25 @@ pub(super) enum StrokePlan { Written(Written), } +/// Visit `n` and the descendants the writer draws ([`Writer::node_body`]): a hidden layer or object +/// and what it holds only with `hidden` (SVG Options › hidden layers kept), and a template layer's +/// art never. +fn walk_drawn<'a>(n: &'a Node, hidden: bool, f: &mut impl FnMut(&'a Node)) { + if !(n.visible || hidden) { + return; + } + f(n); + if matches!(n.kind, NodeKind::Layer { template: true, .. }) { + return; + } + for c in n.children().into_iter().flatten() { + walk_drawn(c, hidden, f); + } +} + impl Writer<'_> { pub(super) fn count_colorized_images(&mut self, art: &Node) { - art.walk(&mut |n| { + walk_drawn(art, self.opts.hidden_layers, &mut |n| { let NodeKind::Image(im) = &n.kind else { return }; let Some(Paint::Solid { color, .. }) = n.extras().colorize.as_deref() else { return }; let shown = self.doc.shown_image(n, im); @@ -176,7 +192,7 @@ impl Writer<'_> { self.count_colorized_images(art); // Each image's count and its first piece, which shows what they all show. let mut seen: HashMap<(String, u32, u32), (usize, vectorcraft_doc::ImageObject)> = HashMap::new(); - art.walk(&mut |c| { + walk_drawn(art, self.opts.hidden_layers, &mut |c| { if let NodeKind::Image(im) = &c.kind { let shown = self.doc.shown_image(c, im); let im = &*shown; diff --git a/crates/svg/tests/it/warp_pieces.rs b/crates/svg/tests/it/warp_pieces.rs index 9c8b3e1760..4d61baee14 100644 --- a/crates/svg/tests/it/warp_pieces.rs +++ b/crates/svg/tests/it/warp_pieces.rs @@ -118,6 +118,49 @@ fn opaque_pixels_stay_opaque_where_warp_pieces_overlap() { } } +/// Pieces the file leaves out share no image: with Images set to Link, the warp pieces of a linked +/// image that are hidden, or sit in hidden clip groups, put no `` naming its file in the +/// defs unless hidden layers are kept. Visible pieces share one. +#[test] +fn hidden_warp_pieces_share_no_image() { + let path = "/away/photo.png"; + let svg = |d: &Document, hidden_layers: bool| { + let opts = ExportOptions { images: vectorcraft_svg::ImageMode::Link, hidden_layers, ..ExportOptions::default() }; + export_full(d, &opts, None).svg + }; + // Every image in `n` linked to `path`. + fn link(n: &mut Node, path: &str) { + if let NodeKind::Image(im) = &mut n.kind { + im.link = Some(vectorcraft_doc::LinkInfo::new(path)); + } + for c in n.children_mut().into_iter().flatten() { + link(std::sync::Arc::make_mut(c), path); + } + } + for masked in [false, true] { + // Hide every image (`ancestor` false), or every clip group holding one (`ancestor` true). + for ancestor in [false, true] { + let mut d = warped_pixels(&|p| p, masked, [255, 0, 0, 255], |_| {}); + let layer = std::sync::Arc::make_mut(&mut d.layers[0]); + let g = std::sync::Arc::make_mut(&mut layer.children_mut().unwrap()[0]); + link(g, path); + let visible = d.clone(); + assert_eq!(svg(&visible, false).matches(path).count(), 1, "masked {masked}: the pieces share one image"); + let layer = std::sync::Arc::make_mut(&mut d.layers[0]); + let g = std::sync::Arc::make_mut(&mut layer.children_mut().unwrap()[0]); + for piece in g.children_mut().unwrap() { + let piece = std::sync::Arc::make_mut(piece); + match piece.children_mut() { + Some(kids) if !ancestor => std::sync::Arc::make_mut(&mut kids[1]).visible = false, + _ => piece.visible = false, + } + } + assert!(!svg(&d, false).contains(path), "masked {masked}, ancestor {ancestor}: {}", svg(&d, false)); + assert_eq!(svg(&d, true).matches(path).count(), 1, "masked {masked}, ancestor {ancestor}"); + } + } +} + /// Pieces of an opaque image are drawn one over another without knockout masks; a colorize paint /// draws a gray image's light pixels partly transparent, so its pieces take them. #[test]