Merge pull request #8961 from dotCooCoo/fix/safe-mode-expand-hidden-symbol-art

Expand Appearance: Safe Mode skips art that a group's raster image leaves out
This commit is contained in:
BFlat
2026-11-22 11:37:14 +07:00
committed by GitHub
3 changed files with 179 additions and 11 deletions
+49 -10
View File
@@ -97,7 +97,7 @@ fn effect_specs() -> Vec<CommandSpec> {
"Expand Appearance",
["Object"],
None,
"{ids?|id?: [..], target?} turn each object's appearance into objects: every fill and stroke becomes an object of its own (a stroke stays a stroke, with its weight, dashes, arrowheads and width profile, on its own copy of the path, which object.expand or object.path.outlineStroke outline; brushed strokes become their brush art) grouped in paint order under the object's id and transparency, geometry effects are baked, raster effects become an embedded image (shadows and outer glows below the art; blur, feather and inner glow replace it; an object whose only raster effect is its own Rasterize becomes the image object.rasterize makes with the effect's options), type with effects or fills of its own is outlined; Crop Marks become a group of the object and its marks; Vector Halftone (addon.color.halftone) becomes a group of its dot paths, one per ink; a group's or layer's own fills and strokes become objects among its members, which are expanded too; a symbol instance whose topmost fill or stroke is visible, has an opacity above 0 and has a geometry or raster effect becomes a group of its art, that fill's effects reshaping the art's filled shapes and that stroke's effects reshaping its stroked shapes, as the canvas draws them, its raster effects an image as for other objects; a symbol instance in that art whose topmost fill or stroke has such an effect (its own or one the outer instance passes on) becomes its art and image in turn. An outlined frame of threaded text leaves its thread with the text it shows, and a frame kept as type in a group stays in its thread. A dimension whose art it changes becomes a plain group of its art, except one selected with art it measures, directly or in groups without fills, strokes or effects of their own: its lines and label stay as they are, and if the new art no longer fits its link it stays where it was, no longer linked. Hidden fills, strokes and effects are dropped. One expansion makes at most 1,024 images of raster effects, 256 megapixels in all (VectorCraft's own limits): an object or symbol instance whose image would go past them stays as it is, with its effects, and when nothing else expands that is an error. In Safe Mode, an object with a visible plug-in effect (in it, or in a symbol it places, a plain instance in a group or layer whose raster effect makes an image of it included) is an error and nothing changes. Enabled when a selected or targeted object's appearance isn't basic (`ids` then pick which objects) → {ids, warning? (an image in the art couldn't be decoded, as object.rasterize says; or raster effects were left past the limits), rasterLeft? (how many objects were left so)}",
"{ids?|id?: [..], target?} turn each object's appearance into objects: every fill and stroke becomes an object of its own (a stroke stays a stroke, with its weight, dashes, arrowheads and width profile, on its own copy of the path, which object.expand or object.path.outlineStroke outline; brushed strokes become their brush art) grouped in paint order under the object's id and transparency, geometry effects are baked, raster effects become an embedded image (shadows and outer glows below the art; blur, feather and inner glow replace it; an object whose only raster effect is its own Rasterize becomes the image object.rasterize makes with the effect's options), type with effects or fills of its own is outlined; Crop Marks become a group of the object and its marks; Vector Halftone (addon.color.halftone) becomes a group of its dot paths, one per ink; a group's or layer's own fills and strokes become objects among its members, which are expanded too; a symbol instance whose topmost fill or stroke is visible, has an opacity above 0 and has a geometry or raster effect becomes a group of its art, that fill's effects reshaping the art's filled shapes and that stroke's effects reshaping its stroked shapes, as the canvas draws them, its raster effects an image as for other objects; a symbol instance in that art whose topmost fill or stroke has such an effect (its own or one the outer instance passes on) becomes its art and image in turn. An outlined frame of threaded text leaves its thread with the text it shows, and a frame kept as type in a group stays in its thread. A dimension whose art it changes becomes a plain group of its art, except one selected with art it measures, directly or in groups without fills, strokes or effects of their own: its lines and label stay as they are, and if the new art no longer fits its link it stays where it was, no longer linked. Hidden fills, strokes and effects are dropped. One expansion makes at most 1,024 images of raster effects, 256 megapixels in all (VectorCraft's own limits): an object or symbol instance whose image would go past them stays as it is, with its effects, and when nothing else expands that is an error. In Safe Mode, an object with a visible plug-in effect (in it, or in a symbol it places, a visible plain instance outside template layers in a group or layer whose raster effect makes an image of it included) is an error and nothing changes. Enabled when a selected or targeted object's appearance isn't basic (`ids` then pick which objects) → {ids, warning? (an image in the art couldn't be decoded, as object.rasterize says; or raster effects were left past the limits), rasterLeft? (how many objects were left so)}",
can_expand,
expand_appearance
),
@@ -1616,33 +1616,72 @@ fn expanded_node(
/// Does `n`, an object in it, or the art of a symbol placed in it hold a plug-in effect that
/// expanding it draws: a visible one, the object's own or a visible fill's or stroke's, on an
/// object that expands ([`expandable`]; in a symbol's art, `in_art`, every object does, and so does
/// every object in a group or layer with a raster effect, whose image draws all of it). A symbol's
/// object that expands ([`expandable`]; in a symbol's art, `in_art`, every object does), or that the
/// image of a group or layer with a raster effect draws ([`rasterizes_plugin_effect`]). A symbol's
/// art counts where an instance of it expands. `seen` holds the symbols already looked into.
fn holds_plugin_effect(d: &Document, n: &Node, in_art: bool, seen: &mut std::collections::HashSet<String>) -> bool {
let plugin = |e: &Effect| e.visible && vectorcraft_plugins::effect::plugin_id(&e.id).is_some();
let mut symbols = vec![];
let mut drawn = vec![];
let mut holds = false;
n.walk(&mut |c| {
if !in_art && is_container(c) && !rasterfx::raster_fx(c).is_empty() {
drawn.push(c);
}
if !in_art && !expandable(c) {
return;
}
holds |= c.appearance.effects.iter().any(plugin) || c.appearance.items.iter().any(|i| i.visible() && i.effects().iter().any(plugin));
holds |= has_own_plugin_effect(c);
if let NodeKind::SymbolInstance { symbol, .. } = &c.kind {
symbols.push(symbol.clone());
}
});
holds
|| drawn.into_iter().any(|g| holds_plugin_effect(d, g, true, seen))
|| (!in_art && rasterizes_plugin_effect(d, n, &mut std::collections::HashSet::new()))
|| symbols.into_iter().any(|name| {
seen.insert(name.clone()) && d.symbols.iter().find(|s| s.name == name).is_some_and(|s| holds_plugin_effect(d, &s.art, true, seen))
})
}
/// Does `n` have a visible plug-in effect of its own, or on a visible fill or stroke?
fn has_own_plugin_effect(n: &Node) -> bool {
let plugin = |e: &Effect| e.visible && vectorcraft_plugins::effect::plugin_id(&e.id).is_some();
n.appearance.effects.iter().any(plugin) || n.appearance.items.iter().any(|i| i.visible() && i.effects().iter().any(plugin))
}
/// Does a group or layer in `n` with a raster effect make an image of a visible plug-in effect
/// ([`draws_plugin_effect`])? Expansion makes the image of such a group or layer even when it, or
/// a group around it, is hidden. A group or layer that paints something and whose raster effects
/// are not all shadows and outer glows becomes one image of what it draws, so the groups and layers
/// in it make no images of their own; one that paints nothing makes no image, and its members
/// expand on their own.
fn rasterizes_plugin_effect(d: &Document, n: &Node, seen: &mut std::collections::HashSet<String>) -> bool {
let fx = if is_container(n) { rasterfx::raster_fx(n) } else { vec![] };
if !fx.is_empty() {
if draws_plugin_effect(d, n, seen) {
return true;
}
if !fx.iter().all(effects::RasterFx::is_below) && vectorcraft_render::painted_bounds(d, n).is_some() {
return false;
}
}
n.children().into_iter().flatten().any(|c| rasterizes_plugin_effect(d, c, seen))
}
/// Does `n`, an object the image of `n` draws in it, or the art of a symbol such an instance places,
/// have a visible plug-in effect ([`has_own_plugin_effect`])? The image leaves out a hidden object
/// in `n`, a template layer, a guide or a ruler guide, and everything in it, as the renderer does
/// with templates skipped. `seen` holds the symbols already looked into.
fn draws_plugin_effect(d: &Document, n: &Node, seen: &mut std::collections::HashSet<String>) -> bool {
if has_own_plugin_effect(n) {
return true;
}
if let NodeKind::SymbolInstance { symbol, .. } = &n.kind
&& seen.insert(symbol.clone())
&& d.symbols.iter().find(|s| s.name == *symbol).is_some_and(|s| draws_plugin_effect(d, &s.art, seen))
{
return true;
}
let drawn =
|c: &Node| c.visible && !c.is_ruler_guide() && !matches!(c.kind, NodeKind::Layer { template: true, .. } | NodeKind::Path { guide: true, .. });
n.children().into_iter().flatten().any(|c| drawn(c) && draws_plugin_effect(d, c, seen))
}
fn expand_appearance(s: &mut Session, p: &Value) -> Result<Value> {
let roots = edit_targets(s, p, "effect.expandAppearance")?;
// In Safe Mode a plug-in effect draws nothing: expanding it would drop the effect and keep the
+128
View File
@@ -506,6 +506,134 @@ fn expand_appearance_in_safe_mode_checks_symbol_art_a_group_image_draws() {
refused(&mut s, &json!(outer));
}
/// In Safe Mode, a hidden plain instance draws nothing in the image a group's raster effect makes,
/// so its symbol art's plug-in effect doesn't stop Expand Appearance of the group; shown, it does.
#[test]
fn expand_appearance_in_safe_mode_skips_symbol_art_of_a_hidden_instance_a_group_image_leaves_out() {
let _f = fresh();
let mut s = Session::new();
let fx = wat::parse_str(tpl::returning(&tpl::manifest("test.sm.hid", "effect", "{}"), r#"{"objects":[]}"#)).unwrap();
s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&fx)})).unwrap();
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
s.execute("effect.apply", &json!({"effect": "plugin.test.sm.hid"})).unwrap();
s.execute("symbol.new", &json!({"name": "fx"})).unwrap();
let hide = |s: &mut Session, id: u64| {
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(vectorcraft_engine::doc::NodeId(id)).unwrap().visible = false;
};
let expand = |s: &mut Session, group: u64| {
safe_mode::set_active(Some(safe_mode::Source::Flag));
let r = s.execute("effect.expandAppearance", &json!({"ids": [group]}));
safe_mode::set_active(None);
r
};
// A blurred group of a visible rectangle and an instance, then one of a visible rectangle and a
// group around an instance; the instance, or the group around it, is shown and then hidden.
for wrap in [false, true] {
let rect = s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 20, "height": 20})).unwrap()["id"].clone();
let mut hidden = s.execute("symbol.place", &json!({"name": "fx", "x": 150, "y": 150})).unwrap()["id"].clone();
if wrap {
hidden = s.execute("object.group", &json!({"ids": [hidden]})).unwrap()["id"].clone();
}
let group = s.execute("object.group", &json!({"ids": [rect, hidden]})).unwrap()["id"].clone();
s.execute("effect.apply", &json!({"effect": "blur.gaussian", "ids": [group], "params": {"radius": 2}})).unwrap();
let (hidden, group) = (hidden.as_u64().unwrap(), group.as_u64().unwrap());
let err = expand(&mut s, group).unwrap_err().to_string();
assert!(err.contains("a plug-in effect in the selection can't be expanded in Safe Mode"), "{err}");
hide(&mut s, hidden);
expand(&mut s, group).unwrap();
}
// Expansion still makes the image of a hidden blurred group, or of one in a hidden group, so
// a shown instance in it still stops it.
let plain = s.execute("symbol.place", &json!({"name": "fx", "x": 150, "y": 20})).unwrap()["id"].clone();
let blurred = s.execute("object.group", &json!({"ids": [plain]})).unwrap()["id"].clone();
s.execute("effect.apply", &json!({"effect": "blur.gaussian", "ids": [blurred], "params": {"radius": 2}})).unwrap();
let parent = s.execute("object.group", &json!({"ids": [blurred]})).unwrap()["id"].as_u64().unwrap();
hide(&mut s, parent);
let err = expand(&mut s, parent).unwrap_err().to_string();
assert!(err.contains("a plug-in effect in the selection can't be expanded in Safe Mode"), "{err}");
let blurred = blurred.as_u64().unwrap();
hide(&mut s, blurred);
let err = expand(&mut s, blurred).unwrap_err().to_string();
assert!(err.contains("a plug-in effect in the selection can't be expanded in Safe Mode"), "{err}");
}
/// In Safe Mode, the image a layer's raster effect makes leaves out a template sublayer, so the symbol
/// art of a plain instance in it doesn't stop Expand Appearance of the layer; in a plain sublayer, it does.
#[test]
fn expand_appearance_in_safe_mode_skips_symbol_art_in_a_template_sublayer_a_layer_image_leaves_out() {
let _f = fresh();
let mut s = Session::new();
let fx = wat::parse_str(tpl::returning(&tpl::manifest("test.sm.tpl", "effect", "{}"), r#"{"objects":[]}"#)).unwrap();
s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&fx)})).unwrap();
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
s.execute("effect.apply", &json!({"effect": "plugin.test.sm.tpl"})).unwrap();
s.execute("symbol.new", &json!({"name": "fx"})).unwrap();
let layer = s.execute("layer.new", &json!({})).unwrap()["id"].as_u64().unwrap();
s.execute("layer.setCurrent", &json!({"id": layer})).unwrap();
s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 20, "height": 20})).unwrap();
let instance = s.execute("symbol.place", &json!({"name": "fx", "x": 150, "y": 150})).unwrap()["id"].as_u64().unwrap();
let sub = s.execute("layer.newSublayer", &json!({"parent": layer})).unwrap()["id"].as_u64().unwrap();
s.execute("layer.move", &json!({"ids": [instance], "target": sub})).unwrap();
s.execute("layer.target", &json!({"id": layer})).unwrap();
s.execute("effect.apply", &json!({"effect": "blur.gaussian", "ids": [layer], "item": null, "params": {"radius": 2}})).unwrap();
let blurred = |s: &Session| s.doc().unwrap().doc.node(vectorcraft_engine::doc::NodeId(layer)).unwrap().appearance.effects.len();
assert_eq!(blurred(&s), 1, "the layer has its own Gaussian Blur");
let expand = |s: &mut Session| {
s.execute("layer.target", &json!({"id": layer})).unwrap();
safe_mode::set_active(Some(safe_mode::Source::Flag));
let r = s.execute("effect.expandAppearance", &json!({}));
safe_mode::set_active(None);
r
};
let err = expand(&mut s).unwrap_err().to_string();
assert!(err.contains("a plug-in effect in the selection can't be expanded in Safe Mode"), "{err}");
s.execute("layer.setProps", &json!({"id": sub, "template": true})).unwrap();
expand(&mut s).unwrap();
}
/// In Safe Mode, a group whose Gaussian Blur makes it one image leaves out a hidden blurred group in
/// it, which makes no image of its own, so the symbol art of a plain instance there doesn't stop
/// Expand Appearance. Under a Drop Shadow, whose members expand on their own, the hidden group still
/// makes its image, so it does.
#[test]
fn expand_appearance_in_safe_mode_skips_a_hidden_blurred_group_that_a_blurred_group_image_leaves_out() {
let _f = fresh();
let mut s = Session::new();
let fx = wat::parse_str(tpl::returning(&tpl::manifest("test.sm.nest", "effect", "{}"), r#"{"objects":[]}"#)).unwrap();
s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&fx)})).unwrap();
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
s.execute("effect.apply", &json!({"effect": "plugin.test.sm.nest"})).unwrap();
s.execute("symbol.new", &json!({"name": "fx"})).unwrap();
let expand = |s: &mut Session, group: u64| {
safe_mode::set_active(Some(safe_mode::Source::Flag));
let r = s.execute("effect.expandAppearance", &json!({"ids": [group]}));
safe_mode::set_active(None);
r
};
for (outer, allowed) in [("blur.gaussian", true), ("stylize.dropShadow", false)] {
let rect = s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 20, "height": 20})).unwrap()["id"].clone();
let plain = s.execute("symbol.place", &json!({"name": "fx", "x": 150, "y": 150})).unwrap()["id"].clone();
let inner = s.execute("object.group", &json!({"ids": [plain]})).unwrap()["id"].clone();
s.execute("effect.apply", &json!({"effect": "blur.gaussian", "ids": [inner], "params": {"radius": 2}})).unwrap();
let group = s.execute("object.group", &json!({"ids": [rect, inner]})).unwrap()["id"].as_u64().unwrap();
s.execute("effect.apply", &json!({"effect": outer, "ids": [group]})).unwrap();
let inner = vectorcraft_engine::doc::NodeId(inner.as_u64().unwrap());
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(inner).unwrap().visible = false;
let r = expand(&mut s, group);
if allowed {
r.unwrap();
// The blurred group became its image: the hidden group in it was not expanded on its own.
assert!(s.doc().unwrap().doc.node(inner).is_none(), "the hidden group went into the image");
} else {
let err = r.unwrap_err().to_string();
assert!(err.contains("a plug-in effect in the selection can't be expanded in Safe Mode"), "{outer}: {err}");
}
}
}
/// In Safe Mode, a plug-in effect in the art of a graph design in a series stops Expand Appearance of the graph only
/// when an effect of the series group draws that art with the group.
#[test]
+2 -1
View File
@@ -13369,7 +13369,8 @@ runs: `plugin.install`, `plugin.run`, `plugin.reload` and exporters return an er
object's geometry unchanged while the object keeps the effect: `effect.list` gives that effect `skipped: true`, and the
Appearance panel shows "(Safe Mode)" after its name and the plug-in's id in its tooltip. `effect.expandAppearance` returns an
error and changes nothing when an object it would expand holds such an effect, in it or in a symbol it places. That includes a
plain symbol instance in a group or layer with a raster effect, since the group's image draws the symbol's art. `vectorcraft` rejects an option it does not know (such as
visible plain symbol instance in a group or layer with a raster effect, since the group's image draws the symbol's art. A
hidden instance, or one in a template sublayer, draws nothing in that image, so its symbol's art does not count. `vectorcraft` rejects an option it does not know (such as
`--safe-mod`) instead of opening it as a file; `--` before a file name that starts with `-` opens it. The web build keeps
no installed plug-ins from one page load to the next and has no launch marker, so it never offers the choice. `plugin.safeMode` returns `{active, source, disabled,
pendingItem}`; a command does not turn Safe Mode on or off. `plugin.setEnabled {id | file, enabled}` turns a plug-in on or