mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 14:17:14 +00:00
SVG: hidden pieces of a cut image put no shared image in the defs
The pieces a Warp effect cuts an image into share one <image> in the defs. The writer counted every piece, hidden ones included, so pieces the file leaves out (hidden, or in a hidden clip group) still put an <image> into the defs, with the linked file's path under Images: Link. A confined caller's read-root check looks only at the art the file draws and did not refuse it. Image sharing and the colorized-image counts now walk only the art the writer draws: hidden layers and objects only with hidden layers kept, and never a template layer's art.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 `<image>` 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]
|
||||
|
||||
Reference in New Issue
Block a user