mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 13:08:32 +00:00
Merge pull request #7368 from dotCooCoo/fix/graph-design-art-paint
Graphs: paint, stroke and transparency commands leave a design's art in a series out
This commit is contained in:
@@ -16,6 +16,11 @@ pub trait ColorVisitor {
|
||||
fn color(&mut self, c: Color) -> Color;
|
||||
/// The key of embedded image `key` recoloured, `None` when it stays as it is.
|
||||
fn image(&mut self, key: &str) -> Option<String>;
|
||||
/// Whether the pass recolors child `n` of a container: false leaves `n` and everything inside
|
||||
/// it as they are.
|
||||
fn enters(&mut self, _n: &Node) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// What a recolouring pass reaches: fills (including colorized image fills and gradient-mesh
|
||||
@@ -82,6 +87,9 @@ pub fn recolor_node(n: &mut Node, reach: Reach, v: &mut dyn ColorVisitor) -> usi
|
||||
}
|
||||
_ => {
|
||||
for c in n.children_mut().into_iter().flatten() {
|
||||
if !v.enters(c) {
|
||||
continue;
|
||||
}
|
||||
let mut m = (**c).clone();
|
||||
let k = recolor_node(&mut m, reach, v);
|
||||
if k > 0 {
|
||||
|
||||
@@ -446,7 +446,7 @@ pub(crate) fn rows_per_target(d: &Document, of: Option<NodeId>, ids: &[NodeId],
|
||||
}
|
||||
|
||||
/// Whether the `target` param asks for the contents of groups and layers (see the module docs).
|
||||
fn targets_contents(p: &Value) -> Result<bool> {
|
||||
pub(crate) fn targets_contents(p: &Value) -> Result<bool> {
|
||||
match p.get("target") {
|
||||
None | Some(Value::Null) => Ok(false),
|
||||
Some(v) => match v.as_str() {
|
||||
@@ -508,6 +508,20 @@ pub(crate) fn drop_design_art(d: &Document, mut ids: Vec<NodeId>) -> Vec<NodeId>
|
||||
ids
|
||||
}
|
||||
|
||||
/// [`drop_design_art`] for an edit of the `item` rows of `ids` (`fill`: fill rows): an explicit `item` that a design's
|
||||
/// art has no row of that kind for is still an error of `cmd`, as on any other object.
|
||||
pub(crate) fn drop_design_art_rows(s: &Session, ids: Vec<NodeId>, item: ItemTarget, fill: bool, cmd: &str) -> Result<Vec<NodeId>> {
|
||||
let d = &s.doc()?.doc;
|
||||
for (id, it) in ids.iter().zip(item.per_target(s, &ids)) {
|
||||
if let (Some(it), Some(n)) = (it, d.node(*id))
|
||||
&& super::graph::is_design_art(d, *id)
|
||||
{
|
||||
it.resolve(&n.appearance, fill, cmd)?;
|
||||
}
|
||||
}
|
||||
Ok(drop_design_art(d, ids))
|
||||
}
|
||||
|
||||
/// The outermost art of graph designs drawn in graph series inside `roots`: the objects directly in a series group that
|
||||
/// [`super::graph::is_design_art`] accepts.
|
||||
pub(crate) fn design_art_in(d: &Document, roots: &[NodeId]) -> Vec<NodeId> {
|
||||
@@ -720,7 +734,10 @@ pub(crate) fn edits_stroke(s: &Session, p: &Value, item: ItemTarget, default: bo
|
||||
let holder = match reference_of(s, p, item, cmd)? {
|
||||
Some(of) => Some(of),
|
||||
None => {
|
||||
let ids = drop_design_art(d, appearance_targets(s, p)?);
|
||||
let all = appearance_targets(s, p)?;
|
||||
// The art of a graph design is left out, unless it is all there is: the item is then a row of that art.
|
||||
let kept = drop_design_art(d, all.clone());
|
||||
let ids = if kept.is_empty() { all } else { kept };
|
||||
ids.get(item.anchor(s, &ids)).copied()
|
||||
}
|
||||
};
|
||||
|
||||
@@ -221,6 +221,8 @@ pub(crate) struct Recolor<'a> {
|
||||
patterns: HashMap<String, Option<String>>,
|
||||
new_images: Vec<(String, ImageBlob)>,
|
||||
new_patterns: Vec<PatternDef>,
|
||||
/// Objects inside the recolored ones that keep their colors, with everything inside them.
|
||||
skip: HashSet<NodeId>,
|
||||
changed: usize,
|
||||
}
|
||||
|
||||
@@ -236,6 +238,7 @@ impl<'a> Recolor<'a> {
|
||||
patterns: HashMap::new(),
|
||||
new_images: vec![],
|
||||
new_patterns: vec![],
|
||||
skip: HashSet::new(),
|
||||
changed: 0,
|
||||
}
|
||||
}
|
||||
@@ -254,7 +257,7 @@ impl<'a> Recolor<'a> {
|
||||
|
||||
/// A pass that maps colors as this one does, with nothing recolored yet.
|
||||
fn again(&self) -> Self {
|
||||
Self { links: self.links, gray_images: self.gray_images, remake: self.remake, ..Self::new(self.scope, self.f) }
|
||||
Self { links: self.links, gray_images: self.gray_images, remake: self.remake, skip: self.skip.clone(), ..Self::new(self.scope, self.f) }
|
||||
}
|
||||
|
||||
/// The entries of `objects` (ids, each with a flag) whose object this pass would change, other
|
||||
@@ -440,12 +443,20 @@ impl ColorVisitor for Visit<'_, '_> {
|
||||
fn image(&mut self, key: &str) -> Option<String> {
|
||||
self.pass.image(self.d, key)
|
||||
}
|
||||
fn enters(&mut self, n: &Node) -> bool {
|
||||
!self.pass.skip.contains(&n.id)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn recolor(s: &mut Session, p: &Value, cmd: &str, label: &str, f: &dyn Fn(Color) -> Color) -> Result<Value> {
|
||||
run_pass(s, p, cmd, label, Recolor::new(Scope::of(p, cmd)?, f))
|
||||
}
|
||||
|
||||
/// [`recolor`] that leaves the objects `skip` inside the recolored ones, and everything inside them, as they are.
|
||||
pub(crate) fn recolor_except(s: &mut Session, p: &Value, cmd: &str, label: &str, f: &dyn Fn(Color) -> Color, skip: HashSet<NodeId>) -> Result<Value> {
|
||||
run_pass(s, p, cmd, label, Recolor { skip, ..Recolor::new(Scope::of(p, cmd)?, f) })
|
||||
}
|
||||
|
||||
/// [`recolor`] where `f` gives gray colors, so embedded images are stored with one gray channel.
|
||||
fn recolor_gray(s: &mut Session, p: &Value, cmd: &str, label: &str, f: &dyn Fn(Color) -> Color) -> Result<Value> {
|
||||
run_pass(s, p, cmd, label, Recolor { gray_images: true, ..Recolor::new(Scope::of(p, cmd)?, f) })
|
||||
|
||||
@@ -978,15 +978,18 @@ fn raster_image(d: &mut Document, n: &Node) -> Option<(Node, bool)> {
|
||||
/// Expand the pieces of `m`, art evaluated from the object of the same id (its pieces share that
|
||||
/// id): those made from its own fills and strokes, or with `all` every member. The other members
|
||||
/// that repeat an id ([`repeats_an_id`]) and hold a random Transform effect ([`has_seeded`]) are
|
||||
/// expanded as well ([`expand_in_place`]). Then the members get ids of their own. Random in a
|
||||
/// Transform effect takes its seed from the id, and the canvas draws the pieces with the ids they
|
||||
/// have before that. Returns what each of those other members' expansions lists, by the member's
|
||||
/// new id.
|
||||
/// expanded as well ([`expand_in_place`]), except the objects `skip` lists. Then the members get ids
|
||||
/// of their own. Random in a Transform effect takes its seed from the id, and the canvas draws the
|
||||
/// pieces with the ids they have before that. `kept` gets the new id of each object in them that
|
||||
/// had an id `skip` lists before. Returns what each of those other members' expansions lists, by
|
||||
/// the member's new id.
|
||||
fn expand_pieces(
|
||||
d: &mut Document,
|
||||
m: &mut Node,
|
||||
all: bool,
|
||||
brushes: &[vectorcraft_brush::Brush],
|
||||
skip: &[NodeId],
|
||||
kept: &mut Vec<NodeId>,
|
||||
stroke_art: effects::StrokeArt,
|
||||
) -> std::collections::HashMap<NodeId, Vec<Listed>> {
|
||||
let id = m.id;
|
||||
@@ -1002,9 +1005,23 @@ fn expand_pieces(
|
||||
if all || c.id == id {
|
||||
effects::expand_art(d, c, stroke_art);
|
||||
} else if has_seeded(c) && repeats_an_id(c, &seen) {
|
||||
listed = Some(expand_in_place(d, c, brushes));
|
||||
listed = Some(expand_in_place(d, c, brushes, skip));
|
||||
}
|
||||
let mut was = vec![];
|
||||
if !skip.is_empty() {
|
||||
c.walk(&mut |n| was.push(n.id));
|
||||
}
|
||||
fresh_ids_listed(d, c, &mut seen, listed.as_deref_mut().unwrap_or_default());
|
||||
if !was.is_empty() {
|
||||
// A copy of an object `skip` lists is left as it is too.
|
||||
let mut at = 0;
|
||||
c.walk(&mut |n| {
|
||||
if was.get(at).is_some_and(|old| *old != n.id && skip.contains(old)) {
|
||||
kept.push(n.id);
|
||||
}
|
||||
at += 1;
|
||||
});
|
||||
}
|
||||
if let Some(listed) = listed {
|
||||
expanded.insert(c.id, listed);
|
||||
}
|
||||
@@ -1058,9 +1075,9 @@ fn repeats_an_id(n: &Node, seen: &std::collections::HashSet<NodeId>) -> bool {
|
||||
type Listed = (NodeId, usize);
|
||||
|
||||
/// [`expanded_node`] of `c` in place. Returns what the expansion lists, by places in the walk of `c`.
|
||||
fn expand_in_place(d: &mut Document, c: &mut Node, brushes: &[vectorcraft_brush::Brush]) -> Vec<Listed> {
|
||||
fn expand_in_place(d: &mut Document, c: &mut Node, brushes: &[vectorcraft_brush::Brush], skip: &[NodeId]) -> Vec<Listed> {
|
||||
let mut listed = vec![];
|
||||
if let Some(e) = expanded_node(d, c.clone(), brushes, &mut listed) {
|
||||
if let Some(e) = expanded_node(d, c.clone(), brushes, skip, &mut listed) {
|
||||
*c = e;
|
||||
}
|
||||
listed
|
||||
@@ -1084,9 +1101,16 @@ fn fresh_ids_listed(d: &mut Document, c: &mut Node, seen: &mut std::collections:
|
||||
/// geometry effects are baked, raster effects become an embedded image, and the object keeps its
|
||||
/// transparency.
|
||||
pub(crate) fn expand_node(d: &mut Document, id: NodeId, brushes: &[vectorcraft_brush::Brush], out: &mut Vec<NodeId>) {
|
||||
expand_node_except(d, id, brushes, &[], out);
|
||||
}
|
||||
|
||||
/// [`expand_node`], leaving the objects `skip` lists, and what they hold, as they are. A geometry
|
||||
/// effect of a group or layer around one still reshapes it with the rest of its members, and the
|
||||
/// copies of it that such an effect makes are left as they are too.
|
||||
fn expand_node_except(d: &mut Document, id: NodeId, brushes: &[vectorcraft_brush::Brush], skip: &[NodeId], out: &mut Vec<NodeId>) {
|
||||
let Some(n) = d.node(id).cloned() else { return };
|
||||
let mut listed = vec![];
|
||||
let m = expanded_node(d, n, brushes, &mut listed);
|
||||
let m = expanded_node(d, n, brushes, skip, &mut listed);
|
||||
out.extend(listed.into_iter().map(|(id, _)| id));
|
||||
if let Some(mut m) = m {
|
||||
// Art made from type takes its emoji's pixels into the document.
|
||||
@@ -1099,10 +1123,10 @@ pub(crate) fn expand_node(d: &mut Document, id: NodeId, brushes: &[vectorcraft_b
|
||||
|
||||
/// [`expand_node`] of `n`, which need not be in `d`: `n` with its appearance expanded, `None` when
|
||||
/// it has nothing to expand. `out` gets the objects whose appearance expands, by places in the walk
|
||||
/// of the expanded `n`, `n`'s first when its own does.
|
||||
fn expanded_node(d: &mut Document, mut n: Node, brushes: &[vectorcraft_brush::Brush], out: &mut Vec<Listed>) -> Option<Node> {
|
||||
/// of the expanded `n`, `n`'s first when its own does. The objects `skip` lists are not expanded.
|
||||
fn expanded_node(d: &mut Document, mut n: Node, brushes: &[vectorcraft_brush::Brush], skip: &[NodeId], out: &mut Vec<Listed>) -> Option<Node> {
|
||||
let id = n.id;
|
||||
if !expandable(&n) {
|
||||
if skip.contains(&id) || !expandable(&n) {
|
||||
return None;
|
||||
}
|
||||
// Colour adjustments: the colours inside become the adjusted ones (an embedded image a
|
||||
@@ -1139,7 +1163,7 @@ fn expanded_node(d: &mut Document, mut n: Node, brushes: &[vectorcraft_brush::Br
|
||||
for (i, c) in m.children_mut().into_iter().flatten().enumerate() {
|
||||
let c = Arc::make_mut(c);
|
||||
if i == 0 {
|
||||
expanded = expand_in_place(d, c, brushes);
|
||||
expanded = expand_in_place(d, c, brushes, skip);
|
||||
fresh_ids_listed(d, c, &mut seen, &mut expanded);
|
||||
} else {
|
||||
effects::fresh_ids(d, c, &mut seen);
|
||||
@@ -1169,6 +1193,8 @@ fn expanded_node(d: &mut Document, mut n: Node, brushes: &[vectorcraft_brush::Br
|
||||
|d: &mut Document, path: &PathData, rule: FillRule, st: &StrokeLayer| super::pathops::outlined_stroke(d, brushes, path, rule, st);
|
||||
// Members of a group or layer that `expand_pieces` expanded, by id, with what their expansion lists.
|
||||
let mut members = std::collections::HashMap::new();
|
||||
// New ids of the copies of objects `skip` lists among those members.
|
||||
let mut kept = vec![];
|
||||
let expanded = match image {
|
||||
// Blur, feather and inner glow change the object itself: it becomes the image (a layer
|
||||
// keeps it as its only member).
|
||||
@@ -1180,7 +1206,7 @@ fn expanded_node(d: &mut Document, mut n: Node, brushes: &[vectorcraft_brush::Br
|
||||
} else if container {
|
||||
// A group's or layer's own fills and strokes become art among its members.
|
||||
effects::evaluate_container(Some(&*d), &v).map(|mut m| {
|
||||
members = expand_pieces(d, &mut m, false, brushes, &mut stroke_art);
|
||||
members = expand_pieces(d, &mut m, false, brushes, skip, &mut kept, &mut stroke_art);
|
||||
m
|
||||
})
|
||||
} else if matches!(n.kind, NodeKind::Path { .. } | NodeKind::Compound { .. })
|
||||
@@ -1201,7 +1227,7 @@ fn expanded_node(d: &mut Document, mut n: Node, brushes: &[vectorcraft_brush::Br
|
||||
effects::expand_outlined(Some(&*d), &v, symbol.as_ref()).map(|mut m| {
|
||||
let mut budget = vectorcraft_doc::MAX_SYMBOL_NODES;
|
||||
expand_nested_instances(d, &mut m, 1, &mut budget);
|
||||
expand_pieces(d, &mut m, true, brushes, &mut stroke_art);
|
||||
expand_pieces(d, &mut m, true, brushes, skip, &mut kept, &mut stroke_art);
|
||||
m
|
||||
})
|
||||
};
|
||||
@@ -1223,6 +1249,7 @@ fn expanded_node(d: &mut Document, mut n: Node, brushes: &[vectorcraft_brush::Br
|
||||
}
|
||||
}
|
||||
if container {
|
||||
let skip = if kept.is_empty() { std::borrow::Cow::Borrowed(skip) } else { std::borrow::Cow::Owned([skip, &kept].concat()) };
|
||||
// The place of each member in the walk of `n`: after `n` and the members before it.
|
||||
let mut at = 1;
|
||||
for c in n.children_mut().into_iter().flatten() {
|
||||
@@ -1235,7 +1262,7 @@ fn expanded_node(d: &mut Document, mut n: Node, brushes: &[vectorcraft_brush::Br
|
||||
match members.remove(&c.id) {
|
||||
Some(listed) => out.extend(listed),
|
||||
None => {
|
||||
if let Some(e) = expanded_node(d, (**c).clone(), brushes, out) {
|
||||
if let Some(e) = expanded_node(d, (**c).clone(), brushes, &skip, out) {
|
||||
*c = Arc::new(e);
|
||||
changed = true;
|
||||
}
|
||||
@@ -1280,9 +1307,20 @@ fn expand_appearance(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// geometry it hasn't changed.
|
||||
if vectorcraft_plugins::safe_mode::active() {
|
||||
// A dimension selected with the art it measures is left as it is, and so is the art of a graph design in a
|
||||
// series.
|
||||
// series, unless an effect of a group around it, the selected graph or series group or one inside it, draws it
|
||||
// with that group: an effect of the group's own, or a raster effect of one of its fills or strokes (the group
|
||||
// then becomes an image of all of it).
|
||||
let mut d = super::dimension::without_kept(&s.doc()?.doc, &roots);
|
||||
let designs = super::appearance::design_art_in(&d, &roots);
|
||||
let draws_members = |g: &Node| g.appearance.effects.iter().any(|e| e.visible) || !rasterfx::raster_fx(g).is_empty();
|
||||
let designs: Vec<NodeId> = super::appearance::design_art_in(&d, &roots)
|
||||
.into_iter()
|
||||
.filter(|art| {
|
||||
!d.ancestry(*art).is_some_and(|up| {
|
||||
let from = up.iter().position(|a| roots.contains(a)).unwrap_or(up.len());
|
||||
up.get(from..up.len().saturating_sub(1)).is_some_and(|around| around.iter().filter_map(|a| d.node(*a)).any(draws_members))
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
if !designs.is_empty() {
|
||||
let view = d.to_mut();
|
||||
for id in designs {
|
||||
@@ -1313,16 +1351,7 @@ fn expand_appearance(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut out = vec![];
|
||||
let threaded = super::threads::frames(d);
|
||||
for id in &roots {
|
||||
expand_node(d, *id, &brushes, &mut out);
|
||||
}
|
||||
for id in &designs {
|
||||
if let (Some(was), Some(slot)) = (before.node(*id), d.node_mut(*id)) {
|
||||
*slot = was.clone();
|
||||
}
|
||||
}
|
||||
if !designs.is_empty() {
|
||||
// A mark that expands into a group stays listed: only what lies in the restored art is left out.
|
||||
out.retain(|id| d.node(*id).is_some() && !d.ancestry(*id).is_some_and(|up| up.iter().any(|a| designs.contains(a))));
|
||||
expand_node_except(d, *id, &brushes, &designs, &mut out);
|
||||
}
|
||||
super::dimension::put_back(d, lifted);
|
||||
// A dimension whose source was replaced stays where it was, no longer linked.
|
||||
|
||||
@@ -534,7 +534,7 @@ fn edit_gradient(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Objects were given or selected, even when all of them are graph design art left out below: the stroke mode is then
|
||||
// not one for new art.
|
||||
let selected = !ids.is_empty();
|
||||
let ids = super::appearance::drop_design_art(&s.doc()?.doc, ids);
|
||||
let ids = super::appearance::drop_design_art_rows(s, ids, item, !stroke, C)?;
|
||||
// How the gradient lies on a stroke (the panel's Stroke buttons).
|
||||
let mode = match str_param(p, "strokeMode") {
|
||||
None => None,
|
||||
@@ -558,7 +558,7 @@ fn edit_gradient(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
_ => None,
|
||||
});
|
||||
let for_default = match p.get("aspect").and_then(Value::as_f64) {
|
||||
Some(a) if a < 0.0 && !ids.is_empty() && default_kind != Some(GradientKind::Conic) => {
|
||||
Some(a) if a < 0.0 && selected && default_kind != Some(GradientKind::Conic) => {
|
||||
let mut q = p.clone();
|
||||
q["aspect"] = json!(-a);
|
||||
std::borrow::Cow::Owned(q)
|
||||
@@ -568,7 +568,7 @@ fn edit_gradient(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// A spread for selected gradients leaves a freeform default (which has no ends) as it is.
|
||||
let spreads = matches!(spread_param(p), Ok(Some(sp)) if !sp.is_pad());
|
||||
let for_default = match for_default {
|
||||
q if spreads && !ids.is_empty() && default_kind == Some(GradientKind::Freeform) => {
|
||||
q if spreads && selected && default_kind == Some(GradientKind::Freeform) => {
|
||||
let mut q = q.into_owned();
|
||||
if let Some(o) = q.as_object_mut() {
|
||||
o.remove("spread");
|
||||
@@ -579,8 +579,14 @@ fn edit_gradient(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
let new_default = apply_gradient_edit_in_model(&default_paint, &with_stored_stops(&for_default, &default_paint, &stored), None, &|_| true, model)
|
||||
.map_err(|e| bad(C, e))?;
|
||||
let freeform = str_param(p, "kind").and_then(GradientKind::parse) == Some(GradientKind::Freeform);
|
||||
let of = super::appearance::reference_of(s, p, item, C)?;
|
||||
// Only design art was given or selected: nothing changes, the defaults and the remembered paint included.
|
||||
if selected && ids.is_empty() {
|
||||
// An aligned `item` of the art alone is still checked: a row the art lacks, or that no other object has, is an error.
|
||||
edit_items(s, &ids, item, of, C, "Gradient", !stroke, |_, _| Ok(()))?;
|
||||
return Ok(json!({"ids": []}));
|
||||
}
|
||||
let freeform = str_param(p, "kind").and_then(GradientKind::parse) == Some(GradientKind::Freeform);
|
||||
let shown_at = super::appearance::first_edited(s, item, of, &ids);
|
||||
// The objects returned: with a design's art, those with a row equal to the art's.
|
||||
let edited = edit_items(s, &ids, item, of, C, "Gradient", !stroke, |n, index| {
|
||||
@@ -770,7 +776,7 @@ fn set_gradient_geom(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if targets.is_empty() {
|
||||
return Err(EngineError::Other("nothing selected".into()));
|
||||
}
|
||||
let targets = super::appearance::drop_design_art(&s.doc()?.doc, targets);
|
||||
let targets = super::appearance::drop_design_art_rows(s, targets, item, !stroke, C)?;
|
||||
let model = super::paint::new_color_model(s, &Value::Null);
|
||||
let of = super::appearance::reference_of(s, p, item, C)?;
|
||||
// The objects returned: with a design's art, those with a row equal to the art's.
|
||||
|
||||
@@ -454,7 +454,7 @@ fn an_edit_of_a_graph_designs_art_is_lost_when_the_graph_is_drawn_again() {
|
||||
/// it, and edit the other objects they are given.
|
||||
#[test]
|
||||
fn paint_commands_leave_a_graph_designs_art_out() {
|
||||
let (mut s, _, copy, art, mark) = design_art();
|
||||
let (mut s, graph, copy, art, mark) = design_art();
|
||||
let fill = top_row(&s, art, true);
|
||||
let commands = [
|
||||
("stroke.set", json!({"weight": 5})),
|
||||
@@ -466,6 +466,7 @@ fn paint_commands_leave_a_graph_designs_art_out() {
|
||||
("paint.swap", json!({})),
|
||||
("paint.default", json!({})),
|
||||
("paint.invert", json!({"item": fill, "stroke": false})),
|
||||
("paint.invert", json!({})),
|
||||
];
|
||||
let (copy_was, art_was) = (look(&s, copy), look(&s, art));
|
||||
for (cmd, p) in &commands {
|
||||
@@ -489,6 +490,11 @@ fn paint_commands_leave_a_graph_designs_art_out() {
|
||||
s.execute("select.set", &json!({"ids": [art.0]})).unwrap();
|
||||
s.execute("stroke.set", &json!({"weight": 5})).unwrap();
|
||||
assert_eq!(look(&s, art), art_was);
|
||||
// Given the whole graph, Invert recolors the marks and leaves the art.
|
||||
let mark_was = look(&s, mark);
|
||||
s.execute("paint.invert", &json!({"ids": [graph.0]})).unwrap();
|
||||
assert_eq!(look(&s, art), art_was);
|
||||
assert_ne!(look(&s, mark), mark_was);
|
||||
// A freeform gradient point is refused on the art alone.
|
||||
refused_on_design_art(&mut s, "paint.freeform.addPoint", json!({"ids": [art.0], "at": [0, 0]}));
|
||||
}
|
||||
@@ -517,6 +523,182 @@ fn stroke_edits_of_selected_design_art_leave_the_new_art_stroke() {
|
||||
assert_eq!(s.new_art_stroke().gradient_mode, mode, "paint.editGradient");
|
||||
}
|
||||
|
||||
/// With only the art of a graph design given or selected, the gradient, swap, default and recolor commands leave the
|
||||
/// paint for new art and the remembered colors as they are.
|
||||
#[test]
|
||||
fn paint_edits_of_design_art_alone_leave_the_defaults() {
|
||||
let (mut s, _, _, art, _) = design_art();
|
||||
let fill = top_row(&s, art, true);
|
||||
s.execute("select.set", &json!({"ids": []})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap();
|
||||
s.execute("stroke.set", &json!({"weight": 3})).unwrap();
|
||||
let defaults = |s: &Session| format!("{:?}", (&s.paint, s.last_solid, &s.last_gradient, &s.recent_colors));
|
||||
let commands = [
|
||||
("paint.editGradient", json!({"kind": "linear"})),
|
||||
("paint.editGradient", json!({"kind": "radial", "stroke": true})),
|
||||
("paint.swap", json!({})),
|
||||
("paint.default", json!({})),
|
||||
("paint.invert", json!({"item": fill, "stroke": false})),
|
||||
("paint.complement", json!({"item": fill, "stroke": false})),
|
||||
("paint.invert", json!({})),
|
||||
("paint.complement", json!({"stroke": true})),
|
||||
("stroke.set", json!({"weight": 7})),
|
||||
("stroke.weightUp", json!({})),
|
||||
];
|
||||
for (cmd, p) in &commands {
|
||||
let was = defaults(&s);
|
||||
let mut q = p.clone();
|
||||
q["ids"] = json!([art.0]);
|
||||
s.execute(cmd, &q).unwrap();
|
||||
assert_eq!(defaults(&s), was, "{cmd}, given");
|
||||
// Selecting the art makes the defaults take its paint.
|
||||
s.execute("select.set", &json!({"ids": [art.0]})).unwrap();
|
||||
let was = defaults(&s);
|
||||
s.execute(cmd, p).unwrap();
|
||||
assert_eq!(defaults(&s), was, "{cmd}, selected");
|
||||
s.execute("select.set", &json!({"ids": []})).unwrap();
|
||||
}
|
||||
// A bad `item`, `aligned` or `target` is still an error with only design art given.
|
||||
for bad in [
|
||||
json!({"ids": [art.0], "item": "top"}),
|
||||
json!({"ids": [art.0], "item": fill, "aligned": "yes"}),
|
||||
json!({"ids": [art.0], "item": fill, "target": "nowhere"}),
|
||||
json!({"ids": [art.0], "target": "nowhere"}),
|
||||
] {
|
||||
assert!(s.execute("paint.invert", &bad).is_err(), "{bad}");
|
||||
assert!(s.execute("paint.complement", &bad).is_err(), "{bad}");
|
||||
}
|
||||
// So is an `item` the art has no row for, in each command that leaves the art out.
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
for (cmd, p) in [
|
||||
("paint.invert", json!({})),
|
||||
("stroke.set", json!({"weight": 2})),
|
||||
("stroke.weightUp", json!({})),
|
||||
("stroke.setAdvanced", json!({"swapArrows": true})),
|
||||
("paint.editGradient", json!({"kind": "linear"})),
|
||||
("paint.setGradientGeom", json!({"start": [0, 0], "end": [50, 50]})),
|
||||
("object.setOverprint", json!({"fill": true})),
|
||||
] {
|
||||
let mut q = p.clone();
|
||||
q["ids"] = json!([art.0]);
|
||||
q["item"] = json!(99);
|
||||
assert!(s.execute(cmd, &q).is_err(), "{cmd}");
|
||||
}
|
||||
// The art's stroke row named without `stroke` is a stroke row: the gradient commands succeed and change nothing.
|
||||
let stroke_row = top_row(&s, art, false);
|
||||
for p in [json!({"kind": "linear"}), json!({"start": [0, 0], "end": [50, 50]})] {
|
||||
let cmd = if p.get("kind").is_some() { "paint.editGradient" } else { "paint.setGradientGeom" };
|
||||
let mut q = p.clone();
|
||||
q["ids"] = json!([art.0]);
|
||||
q["item"] = json!(stroke_row);
|
||||
s.execute(cmd, &q).unwrap();
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps);
|
||||
// With nothing selected, the swap still swaps the defaults.
|
||||
let was = defaults(&s);
|
||||
s.execute("paint.swap", &json!({})).unwrap();
|
||||
assert_ne!(defaults(&s), was);
|
||||
}
|
||||
|
||||
/// A spread or a negative aspect for the art of a graph design alone changes nothing, whatever kind of gradient the
|
||||
/// default fill is; a bad value is still an error.
|
||||
#[test]
|
||||
fn gradient_edits_of_design_art_alone_skip_the_default_kind_checks() {
|
||||
let (mut s, _, _, art, _) = design_art();
|
||||
for (default, p) in [("freeform", json!({"spread": "repeat"})), ("linear", json!({"aspect": -50}))] {
|
||||
s.execute("select.set", &json!({"ids": []})).unwrap();
|
||||
s.execute("paint.editGradient", &json!({"kind": default})).unwrap();
|
||||
let (was, art_was, steps) = (format!("{:?}", s.paint), look(&s, art), s.doc().unwrap().history.undo.len());
|
||||
let mut q = p.clone();
|
||||
q["ids"] = json!([art.0]);
|
||||
s.execute("paint.editGradient", &q).unwrap_or_else(|e| panic!("{q}: {e}"));
|
||||
assert_eq!(format!("{:?}", s.paint), was, "{default}");
|
||||
assert_eq!(look(&s, art), art_was, "{default}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps, "{default}");
|
||||
}
|
||||
assert!(s.execute("paint.editGradient", &json!({"ids": [art.0], "spread": "sideways"})).is_err());
|
||||
}
|
||||
|
||||
/// Invert given a group around a graph recolors the group's own fill and the series marks, and leaves the art of a
|
||||
/// graph design as it is. With `target: "contents"` the group's own fill stays as it is.
|
||||
#[test]
|
||||
fn invert_of_a_group_recolors_its_own_fill_and_leaves_design_art() {
|
||||
let (mut s, graph, _, art, mark) = design_art();
|
||||
s.execute("select.set", &json!({"ids": [graph.0]})).unwrap();
|
||||
let group = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("appearance.addFill", &json!({"ids": [group.0]})).unwrap();
|
||||
let row = top_row(&s, group, true);
|
||||
s.execute("paint.setFill", &json!({"ids": [group.0], "item": row, "color": "#ff0000"})).unwrap();
|
||||
let own = |s: &Session| format!("{:?}", s.doc().unwrap().doc.node(group).unwrap().appearance.items);
|
||||
let (own_was, art_was, mark_was) = (own(&s), look(&s, art), look(&s, mark));
|
||||
s.execute("paint.invert", &json!({"ids": [group.0]})).unwrap();
|
||||
assert_ne!(own(&s), own_was, "the group's own fill");
|
||||
assert_ne!(look(&s, mark), mark_was);
|
||||
assert_eq!(look(&s, art), art_was);
|
||||
// The color remembered is the group's own new fill, which the Fill proxy shows.
|
||||
let Some(Paint::Solid { color, .. }) = s.doc().unwrap().doc.node(group).unwrap().proxy_paint_only(false, None).cloned() else {
|
||||
panic!("the group's fill is not a solid color")
|
||||
};
|
||||
assert_eq!((s.last_solid, s.recent_colors.first().copied()), (color, Some(color)));
|
||||
let (own_was, mark_was) = (own(&s), look(&s, mark));
|
||||
s.execute("paint.invert", &json!({"ids": [group.0], "target": "contents"})).unwrap();
|
||||
assert_eq!(own(&s), own_was, "target: contents");
|
||||
assert_ne!(look(&s, mark), mark_was);
|
||||
assert_eq!(look(&s, art), art_was);
|
||||
}
|
||||
|
||||
/// Invert and Complement given a series group that holds only a graph design's art recolor the group's own fill and
|
||||
/// remember that color, as the last color and the newest recent color. With several such groups the color remembered
|
||||
/// is that of the first group whose fill changed.
|
||||
#[test]
|
||||
fn recoloring_a_group_of_design_art_alone_remembers_its_own_fill() {
|
||||
// A fill of its own for `group`, painted `hex` (the color of a rectangle painted with it).
|
||||
let paint_own = |s: &mut Session, group: NodeId, hex: &str| {
|
||||
s.execute("appearance.addFill", &json!({"ids": [group.0]})).unwrap();
|
||||
let t = NodeId(s.execute("shape.rectangle", &json!({"x": 700, "y": 700, "width": 5, "height": 5})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("paint.setFill", &json!({"ids": [t.0], "color": hex})).unwrap();
|
||||
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
let paint = d.node(t).unwrap().proxy_paint_only(false, None).unwrap().clone();
|
||||
d.node_mut(group).unwrap().appearance.fill_at_mut(None).unwrap().paint = paint;
|
||||
};
|
||||
// Series 0's group with its own fill painted `hex`.
|
||||
let painted_series = |hex: &str| {
|
||||
let (mut s, graph, _, art, _) = design_art();
|
||||
let group = series(s.doc().unwrap().doc.node(graph).unwrap(), 0).id;
|
||||
paint_own(&mut s, group, hex);
|
||||
(s, group, art)
|
||||
};
|
||||
for cmd in ["paint.invert", "paint.complement"] {
|
||||
let (mut s, group, art) = painted_series("#ff0000");
|
||||
let fill = |s: &Session| s.doc().unwrap().doc.node(group).unwrap().proxy_paint_only(false, None).unwrap().clone();
|
||||
let (art_was, fill_was) = (look(&s, art), fill(&s));
|
||||
s.execute(cmd, &json!({"ids": [group.0]})).unwrap();
|
||||
assert_eq!(look(&s, art), art_was, "{cmd}");
|
||||
assert_ne!(fill(&s), fill_was, "{cmd}: the group's fill is recolored");
|
||||
let Paint::Solid { color, .. } = fill(&s) else { panic!("{cmd}: the group's fill is not a solid color") };
|
||||
assert_eq!((s.last_solid, s.recent_colors.first().copied()), (color, Some(color)), "{cmd}");
|
||||
}
|
||||
// A gray fill is its own complement: nothing changes, and the remembered colors stay as they were.
|
||||
let (mut s, group, _) = painted_series("#808080");
|
||||
s.execute("select.set", &json!({"ids": []})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"color": "#00ff00"})).unwrap();
|
||||
let remembered = |s: &Session| format!("{:?}", (s.last_solid, &s.recent_colors));
|
||||
let was = remembered(&s);
|
||||
assert_eq!(s.execute("paint.complement", &json!({"ids": [group.0]})).unwrap()["changed"], json!(0));
|
||||
assert_eq!(remembered(&s), was);
|
||||
// Both series groups hold only the design's art: a gray one first and a red one, given together.
|
||||
let (mut s, graph, _, _, _) = design_art();
|
||||
s.execute("graph.column", &json!({"id": graph.0, "seriesIndexes": [1], "design": "Square"})).unwrap();
|
||||
let groups = [0, 1].map(|i| series(s.doc().unwrap().doc.node(graph).unwrap(), i).id);
|
||||
paint_own(&mut s, groups[0], "#808080");
|
||||
paint_own(&mut s, groups[1], "#ff0000");
|
||||
s.execute("paint.complement", &json!({"ids": [groups[0].0, groups[1].0]})).unwrap();
|
||||
let Some(Paint::Solid { color, .. }) = s.doc().unwrap().doc.node(groups[1]).unwrap().proxy_paint_only(false, None).cloned() else {
|
||||
panic!("the red group's fill is not a solid color")
|
||||
};
|
||||
assert_eq!((s.last_solid, s.recent_colors.first().copied()), (color, Some(color)));
|
||||
}
|
||||
|
||||
/// `transparency.set` without `item` refuses the art of a graph design in a series, and given it with other objects
|
||||
/// sets only theirs.
|
||||
#[test]
|
||||
@@ -596,6 +778,81 @@ fn expand_appearance_leaves_a_graph_designs_art() {
|
||||
assert!(s.doc().unwrap().doc.node(NodeId(other)).is_none_or(|m| m.appearance.effects.is_empty()));
|
||||
}
|
||||
|
||||
/// `effect` with `params`, as `effect.apply` gives it to an object.
|
||||
fn an_effect(s: &mut Session, effect: &str, params: serde_json::Value) -> vectorcraft_doc::Effect {
|
||||
let r = s.execute("shape.rectangle", &json!({"x": 700, "y": 20, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("effect.apply", &json!({"effect": effect, "ids": [r], "params": params})).unwrap();
|
||||
s.doc().unwrap().doc.node(NodeId(r)).unwrap().appearance.effects[0].clone()
|
||||
}
|
||||
|
||||
/// Expand Appearance of a graph neither lists the art of a design in a series whose own effect it leaves, nor adds an
|
||||
/// image of that effect to the document.
|
||||
#[test]
|
||||
fn expand_appearance_lists_no_design_art_and_rasterizes_none() {
|
||||
let (mut s, id, copy, _, mark) = design_art();
|
||||
let shadow = an_effect(&mut s, "stylize.dropShadow", json!({}));
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(copy).unwrap().appearance.effects.push(shadow);
|
||||
s.execute("effect.apply", &json!({"effect": "stylize.dropShadow", "ids": [mark.0]})).unwrap();
|
||||
let copy_was = look(&s, copy);
|
||||
let images = |s: &Session| s.doc().unwrap().doc.images.len();
|
||||
let before = images(&s);
|
||||
s.execute("select.set", &json!({"ids": [mark.0]})).unwrap();
|
||||
s.execute("effect.expandAppearance", &json!({})).unwrap();
|
||||
let per_mark = images(&s) - before;
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
let expanded = s.execute("effect.expandAppearance", &json!({})).unwrap();
|
||||
assert_eq!(expanded["ids"], json!([mark.0]));
|
||||
assert_eq!(look(&s, copy), copy_was);
|
||||
assert_eq!(images(&s) - before, per_mark, "only the mark's shadow becomes an image");
|
||||
}
|
||||
|
||||
/// A geometry effect of a series group that Expand Appearance bakes moves the art of a design in that series with the
|
||||
/// marks.
|
||||
#[test]
|
||||
fn expand_appearance_moves_design_art_with_its_series_groups_effect() {
|
||||
let (mut s, id, copy, _, _) = design_art();
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
let group = series(&n, 0).id;
|
||||
let shift = an_effect(&mut s, "distort.transform", json!({"moveH": 50}));
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(group).unwrap().appearance.effects.push(shift);
|
||||
let left = |s: &Session| {
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
series(&n, 0).children().unwrap()[0].geometric_bounds().unwrap().x0
|
||||
};
|
||||
let was = left(&s);
|
||||
assert_eq!(series(&n, 0).children().unwrap()[0].id, copy);
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("effect.expandAppearance", &json!({})).unwrap();
|
||||
assert!((left(&s) - was - 50.0).abs() < 1e-6, "{} then {}", was, left(&s));
|
||||
}
|
||||
|
||||
/// The copies that a Transform effect of a series group makes of a design's art keep their own effects when Expand
|
||||
/// Appearance bakes the series group's effect, as the art they copy does.
|
||||
#[test]
|
||||
fn expand_appearance_leaves_the_effects_of_design_art_copies() {
|
||||
let (mut s, id, copy, _, _) = design_art();
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
let group = series(&n, 0).id;
|
||||
let shadow = an_effect(&mut s, "stylize.dropShadow", json!({}));
|
||||
let copies = an_effect(&mut s, "distort.transform", json!({"moveH": 50, "copies": 2}));
|
||||
let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
doc.node_mut(copy).unwrap().appearance.effects.push(shadow);
|
||||
doc.node_mut(group).unwrap().appearance.effects.push(copies);
|
||||
let shadows = |s: &Session| {
|
||||
let mut count = 0;
|
||||
s.doc().unwrap().doc.node(id).unwrap().walk(&mut |m| count += m.appearance.effects.iter().filter(|e| e.id == "stylize.dropShadow").count());
|
||||
count
|
||||
};
|
||||
let images = |s: &Session| s.doc().unwrap().doc.images.len();
|
||||
let before = images(&s);
|
||||
assert_eq!(shadows(&s), 1);
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("effect.expandAppearance", &json!({})).unwrap();
|
||||
assert_eq!(shadows(&s), 3, "the art and its two copies keep their shadows");
|
||||
assert_eq!(images(&s), before, "no shadow becomes an image");
|
||||
}
|
||||
|
||||
/// The commands that give rows, effects or graphic styles refuse the art of a graph design inside a series, given by
|
||||
/// id (the design's copy or a path inside it) or selected: the graph draws that art again from the design.
|
||||
#[test]
|
||||
|
||||
@@ -126,14 +126,18 @@ fn kinds(p: &Value, cmd: &str, keys: [&str; 2]) -> Result<Vec<(bool, bool)>> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Each kind's item target and objects: (fill?, item, ids).
|
||||
fn aims(s: &Session, p: &Value, cmd: &str, kinds: impl IntoIterator<Item = bool>) -> Result<Vec<(bool, ItemTarget, Vec<NodeId>)>> {
|
||||
/// Each kind's item target and objects: (fill?, item, ids). With `edit`, the art of graph designs drawn in graph series
|
||||
/// is left out, and an explicit item that the art left out has no row of that kind for is an error, as when the edit
|
||||
/// resolves it on the other objects. Without it, the art is read with the other objects.
|
||||
fn aims(s: &Session, p: &Value, cmd: &str, kinds: impl IntoIterator<Item = bool>, edit: bool) -> Result<Vec<(bool, ItemTarget, Vec<NodeId>)>> {
|
||||
let item = item_target(s, p, cmd)?;
|
||||
kinds
|
||||
.into_iter()
|
||||
.map(|fill| {
|
||||
let it = item.of_kind(s, fill);
|
||||
Ok((fill, it, it.targets(s, p)?))
|
||||
let ids = it.targets(s, p)?;
|
||||
let ids = if edit { super::appearance::drop_design_art_rows(s, ids, it, fill, cmd)? } else { ids };
|
||||
Ok((fill, it, ids))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -159,12 +163,7 @@ pub(super) type OverprintJob = ((bool, ItemTarget, Vec<NodeId>), bool);
|
||||
/// `object.setOverprint` aims, leaving out the art of graph designs drawn in graph series.
|
||||
pub(super) fn overprint_jobs(s: &Session, p: &Value, cmd: &str, keys: [&str; 2]) -> Result<Vec<OverprintJob>> {
|
||||
let kinds = kinds(p, cmd, keys)?;
|
||||
let d = &s.doc()?.doc;
|
||||
Ok(aims(s, p, cmd, kinds.iter().map(|k| k.0))?
|
||||
.into_iter()
|
||||
.map(|(fill, it, ids)| (fill, it, super::appearance::drop_design_art(d, ids)))
|
||||
.zip(kinds.iter().map(|k| k.1))
|
||||
.collect())
|
||||
Ok(aims(s, p, cmd, kinds.iter().map(|k| k.0), true)?.into_iter().zip(kinds.iter().map(|k| k.1)).collect())
|
||||
}
|
||||
|
||||
/// Apply `jobs` to `d`, adding the objects changed to `changed`.
|
||||
@@ -203,7 +202,7 @@ fn attributes(s: &Session, p: &Value) -> Result<AttributesInfo> {
|
||||
let d = &s.doc()?.doc;
|
||||
let mut info = AttributesInfo { ids: targets(s, p)?, recent_urls: s.recent_urls.clone(), ..Default::default() };
|
||||
super::attributes::fill_info(d, &mut info);
|
||||
for (fill, item, ids) in aims(s, p, C, [true, false])? {
|
||||
for (fill, item, ids) in aims(s, p, C, [true, false], false)? {
|
||||
let mut all =
|
||||
ids.iter().filter_map(|id| d.node(*id)).filter_map(|n| Some(flags(n, fill, item.resolve(&n.appearance, fill, C).ok()?))).flatten();
|
||||
let same = all.next().and_then(|first| all.all(|b| b == first).then_some(first));
|
||||
|
||||
@@ -96,7 +96,7 @@ pub fn proxy_specs() -> Vec<CommandSpec> {
|
||||
"Invert",
|
||||
[],
|
||||
None,
|
||||
"{stroke?: bool (default: the active proxy), ids?|id?, item?: appearance item index|null, aligned?: bool (as paint.setFill), target?: \"object\"|\"contents\"} invert the active proxy's colours of the selection (or ids; groups recolour their contents) keeping each colour's model, as edit.colors.invert does; the Appearance panel's active fill/stroke item (appearance.setActiveItem) is inverted instead when it is of that kind and no ids are given; with an object of a dynamic symbol instance selected by Direct Selection and no ids, id or item, that object's solid color changes for this instance only, and a gradient or pattern, or type whose characters differ in color, is refused; with nothing selected, invert the default. On a path directly in a graph series, the graph also stores the new color of the topmost visible fill or stroke, and on a graph label the new color of its characters and rows for every label of its kind (docs/mcp.md); no undo step when no color changes → {changed}",
|
||||
"{stroke?: bool (default: the active proxy), ids?|id?, item?: appearance item index|null, aligned?: bool (as paint.setFill), target?: \"object\"|\"contents\"} invert the active proxy's colours of the selection (or ids; groups recolour their contents and, except with target \"contents\", their own fills and strokes; a compound path's member stands for its compound path) keeping each colour's model, as edit.colors.invert does; the Appearance panel's active fill/stroke item (appearance.setActiveItem) is inverted instead when it is of that kind and no ids are given; with an object of a dynamic symbol instance selected by Direct Selection and no ids, id or item, that object's solid color changes for this instance only, and a gradient or pattern, or type whose characters differ in color, is refused; with nothing selected, invert the default. On a path directly in a graph series, the graph also stores the new color of the topmost visible fill or stroke, and on a graph label the new color of its characters and rows for every label of its kind (docs/mcp.md); no undo step when no color changes → {changed}",
|
||||
has_doc,
|
||||
|s, p| proxy_recolor(s, p, "paint.invert", "Invert", &super::colorcmds::invert)
|
||||
),
|
||||
@@ -982,7 +982,13 @@ pub(super) fn restore_unpainted_stroke(ap: &mut Appearance, index: Option<usize>
|
||||
}
|
||||
|
||||
fn swap(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let ids = super::appearance::drop_design_art(&s.doc()?.doc, paint_targets(s, p)?);
|
||||
let targets = paint_targets(s, p)?;
|
||||
let given = !targets.is_empty();
|
||||
let ids = super::appearance::drop_design_art(&s.doc()?.doc, targets);
|
||||
// Only design art was given or selected: nothing changes, the defaults included.
|
||||
if given && ids.is_empty() {
|
||||
return ok();
|
||||
}
|
||||
if !ids.is_empty() {
|
||||
// When every object comes out as it was, the document and its undo history stay as they are.
|
||||
let before = s.doc()?.doc.clone();
|
||||
@@ -1017,7 +1023,13 @@ fn swap(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
|
||||
fn default_paint(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let ids = super::appearance::drop_design_art(&s.doc()?.doc, paint_targets(s, p)?);
|
||||
let targets = paint_targets(s, p)?;
|
||||
let given = !targets.is_empty();
|
||||
let ids = super::appearance::drop_design_art(&s.doc()?.doc, targets);
|
||||
// Only design art was given or selected: nothing changes, the defaults included.
|
||||
if given && ids.is_empty() {
|
||||
return ok();
|
||||
}
|
||||
if !ids.is_empty() {
|
||||
// When every object comes out as it was, the document and its undo history stay as they are.
|
||||
let before = s.doc()?.doc.clone();
|
||||
@@ -1086,10 +1098,26 @@ fn proxy_recolor(s: &mut Session, p: &Value, cmd: &str, label: &str, f: &dyn Fn(
|
||||
return Ok(json!({ "changed": changed as usize }));
|
||||
}
|
||||
let item = item_target(s, p, cmd)?.of_kind(s, !stroke);
|
||||
// Read before the check below, so that a bad param (such as `target` or `aligned`, or an explicit `item` the art has
|
||||
// no row for) is still an error.
|
||||
let targets = super::appearance::drop_design_art_rows(s, item.targets(s, p)?, item, !stroke, cmd)?;
|
||||
let of = match item {
|
||||
ItemTarget::Item { .. } => super::appearance::reference_of(s, p, item, cmd)?,
|
||||
ItemTarget::Top => None,
|
||||
};
|
||||
// Read before the check below too: `targets` reads no `target` for the topmost fill or stroke.
|
||||
let contents = super::appearance::targets_contents(p)?;
|
||||
let ids = super::appearance::drop_design_art(&s.doc()?.doc, ids);
|
||||
// Only design art was given or selected: nothing changes, the defaults and remembered colors included.
|
||||
if ids.is_empty() {
|
||||
return Ok(json!({ "changed": 0 }));
|
||||
}
|
||||
let (r, shown_at) = match item {
|
||||
ItemTarget::Item { .. } => {
|
||||
let targets = super::appearance::drop_design_art(&s.doc()?.doc, item.targets(s, p)?);
|
||||
let of = super::appearance::reference_of(s, p, item, cmd)?;
|
||||
// Only design art was given or selected: nothing changes, the remembered colors included.
|
||||
if targets.is_empty() {
|
||||
return Ok(json!({ "changed": 0 }));
|
||||
}
|
||||
// The row shown is the one edited in the first object that has it: an aligned row maps to each object's equal row.
|
||||
let shown_at = super::appearance::first_edited(s, item, of, &targets).and_then(|(id, it)| match it {
|
||||
ItemTarget::Item { index, .. } => Some((id, Some(index))),
|
||||
@@ -1105,9 +1133,56 @@ fn proxy_recolor(s: &mut Session, p: &Value, cmd: &str, label: &str, f: &dyn Fn(
|
||||
(json!({ "changed": changed }), shown_at)
|
||||
}
|
||||
// The proxy recolors its paints, including a colorized image's fill, without changing image pixels or pattern tiles.
|
||||
// A given group's own fills and strokes change with the objects inside it, except with `target: "contents"`; other
|
||||
// objects change as their paint targets do (a compound path's member stands for the compound path). Design art
|
||||
// inside a given graph or series group is left out.
|
||||
ItemTarget::Top => {
|
||||
let q = json!({"fill": !stroke, "stroke": stroke, "includeImages": false, "includePatterns": false, "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()});
|
||||
(super::colorcmds::recolor(s, &q, cmd, label, f)?, leaf_targets(s, &ids)?.first().map(|id| (*id, None)))
|
||||
let mut roots = vec![];
|
||||
for id in &ids {
|
||||
let n = s.doc()?.doc.node(*id);
|
||||
let group =
|
||||
!contents && n.is_some_and(|n| matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }) && !n.is_compound_shape());
|
||||
if group {
|
||||
roots.push(*id);
|
||||
} else {
|
||||
roots.extend(leaf_targets(s, &[*id])?);
|
||||
}
|
||||
}
|
||||
let d = &s.doc()?.doc;
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
roots.retain(|id| seen.insert(*id));
|
||||
let roots = super::appearance::drop_design_art(d, roots);
|
||||
if roots.is_empty() {
|
||||
return Ok(json!({ "changed": 0 }));
|
||||
}
|
||||
let art = super::appearance::design_art_in(d, &roots).into_iter().collect();
|
||||
// The fill or stroke the proxy shows of each group among the roots, before the edit.
|
||||
let own = |d: &Document, id: NodeId| {
|
||||
d.node(id)
|
||||
.filter(|n| matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }))
|
||||
.and_then(|n| n.proxy_paint_only(stroke, None))
|
||||
.cloned()
|
||||
};
|
||||
let own_before: std::collections::HashMap<NodeId, Paint> = roots.iter().filter_map(|id| Some((*id, own(d, *id)?))).collect();
|
||||
let q = json!({"fill": !stroke, "stroke": stroke, "includeImages": false, "includePatterns": false, "ids": roots.iter().map(|i| i.0).collect::<Vec<_>>()});
|
||||
let r = super::colorcmds::recolor_except(s, &q, cmd, label, f, art)?;
|
||||
// As the proxy shows a group: its own fill or stroke when the edit changed it, else the first object inside it
|
||||
// that is not design art (which keeps its paint). The first object given that has either shows it.
|
||||
let d = &s.doc()?.doc;
|
||||
let mut shown_at = None;
|
||||
for id in &ids {
|
||||
if let Some(was) = own_before.get(id)
|
||||
&& own(d, *id).as_ref() != Some(was)
|
||||
{
|
||||
shown_at = Some((*id, None));
|
||||
break;
|
||||
}
|
||||
if let Some(leaf) = super::appearance::drop_design_art(d, leaf_targets(s, &[*id])?).first() {
|
||||
shown_at = Some((*leaf, None));
|
||||
break;
|
||||
}
|
||||
}
|
||||
(r, shown_at)
|
||||
}
|
||||
};
|
||||
// The paint remembered is one the edit recolored: a graph design's art that the item edit leaves out gives none.
|
||||
|
||||
@@ -234,8 +234,11 @@ fn stroke_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let item = item_target(s, p, C)?.of_kind(s, false);
|
||||
let mut ids = if item == ItemTarget::Top { own_stroke_or_leaves(s, p)? } else { item.targets(s, p)? };
|
||||
let new_art = no_targets(&ids, p);
|
||||
let given = !ids.is_empty();
|
||||
// The art of a graph design in a series keeps the design's stroke.
|
||||
ids = super::appearance::drop_design_art(&s.doc()?.doc, ids);
|
||||
ids = super::appearance::drop_design_art_rows(s, ids, item, false, C)?;
|
||||
// With only design art, the weight for new art stays as it is too.
|
||||
let only_design_art = given && ids.is_empty();
|
||||
if item == ItemTarget::Top {
|
||||
// Images and symbol instances take no stroke from the panel.
|
||||
let d = &s.doc()?.doc;
|
||||
@@ -329,7 +332,9 @@ fn stroke_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
})?;
|
||||
// Commit the new-art template only after the complete request is validated
|
||||
// and the selected artwork edit has succeeded.
|
||||
if let Some(w) = change.weight {
|
||||
if let Some(w) = change.weight
|
||||
&& !only_design_art
|
||||
{
|
||||
s.paint.stroke_width = w;
|
||||
}
|
||||
if new_art && let Some(st) = s.new_art_stroke_mut() {
|
||||
@@ -372,7 +377,7 @@ fn stroke_step(s: &mut Session, p: &Value, up: bool) -> Result<Value> {
|
||||
}
|
||||
return Ok(json!({"objects": 0}));
|
||||
}
|
||||
let ids = super::appearance::drop_design_art(&s.doc()?.doc, ids);
|
||||
let ids = super::appearance::drop_design_art_rows(s, ids, item, false, c)?;
|
||||
let label = if up { "Stroke Weight Up" } else { "Stroke Weight Down" };
|
||||
edit_items(s, &ids, item, None, c, label, false, |n, index| {
|
||||
if index.is_none()
|
||||
@@ -517,7 +522,7 @@ fn stroke_advanced(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let ids = item.targets(s, p)?;
|
||||
let new_art = no_targets(&ids, p);
|
||||
// The art of a graph design in a series keeps the design's stroke.
|
||||
let ids = super::appearance::drop_design_art(&s.doc()?.doc, ids);
|
||||
let ids = super::appearance::drop_design_art_rows(s, ids, item, false, C)?;
|
||||
let arrows_and_profile = |st: &mut StrokeLayer| {
|
||||
if let Some(sc) = scale {
|
||||
st.arrow_scale = sc;
|
||||
|
||||
@@ -706,3 +706,16 @@ fn a_marker_designs_art_in_a_series_keeps_its_paint() {
|
||||
s.execute("addon.color.fillToStroke", &json!({"ids": [series.id.0]})).unwrap();
|
||||
assert_eq!(design_strokes(&s), before, "the design's art keeps its paint");
|
||||
}
|
||||
|
||||
/// Invert given a member of a compound path inverts the compound path's fill, which the member stands for.
|
||||
#[test]
|
||||
fn invert_of_a_compound_member_inverts_the_compound_path() {
|
||||
let mut s = session();
|
||||
let (a, b) = (rect(&mut s), rect(&mut s));
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
|
||||
let comp = NodeId(s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("paint.setFill", &json!({"ids": [comp.0], "color": "#ff0000"})).unwrap();
|
||||
let member = node(&s, comp).children().into_iter().flatten().next().unwrap().id;
|
||||
s.execute("paint.invert", &json!({"ids": [member.0]})).unwrap();
|
||||
assert_eq!(node(&s, comp).appearance.fill_paint().color().unwrap().to_hex(), "#00ffff");
|
||||
}
|
||||
|
||||
@@ -463,6 +463,69 @@ fn expand_appearance_keeps_a_plug_in_effect_safe_mode_skips() {
|
||||
s.execute("effect.expandAppearance", &json!({})).unwrap();
|
||||
}
|
||||
|
||||
/// 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]
|
||||
fn expand_appearance_in_safe_mode_checks_design_art_its_series_group_draws() {
|
||||
let _f = fresh();
|
||||
let mut s = Session::new();
|
||||
let fx = wat::parse_str(tpl::returning(&tpl::manifest("test.sm.graph", "effect", "{}"), r#"{"objects":[]}"#)).unwrap();
|
||||
s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&fx)})).unwrap();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let csv = ",2024,2025\nQ1,3,2\nQ2,5,4";
|
||||
let graph = s.execute("graph.create", &json!({"type": "column", "x": 100, "y": 100, "width": 300, "height": 200, "csv": csv})).unwrap()["id"]
|
||||
.as_u64()
|
||||
.unwrap();
|
||||
let r = s.execute("shape.rectangle", &json!({"x": 600, "y": 600, "width": 10, "height": 10})).unwrap()["id"].clone();
|
||||
s.execute("effect.apply", &json!({"effect": "plugin.test.sm.graph", "ids": [r]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [r]})).unwrap();
|
||||
s.execute("graph.design", &json!({"new": "Plug"})).unwrap();
|
||||
s.execute("graph.column", &json!({"id": graph, "seriesIndexes": [0], "design": "Plug"})).unwrap();
|
||||
let series = |s: &Session, index: u32| {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let n = d.node(vectorcraft_engine::doc::NodeId(graph)).unwrap();
|
||||
n.children().unwrap().iter().find(|c| c.extras().series_index == Some(index)).unwrap().id
|
||||
};
|
||||
let (art_series, other_series) = (series(&s, 0), series(&s, 1));
|
||||
// A mark in the other series has an effect to expand.
|
||||
let mark = s.doc().unwrap().doc.node(other_series).unwrap().children().unwrap()[0].id;
|
||||
s.execute("effect.apply", &json!({"effect": "distort.puckerBloat", "ids": [mark.0]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [graph]})).unwrap();
|
||||
safe_mode::set_active(Some(safe_mode::Source::Flag));
|
||||
s.execute("effect.expandAppearance", &json!({})).unwrap();
|
||||
safe_mode::set_active(None);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
// With a Transform effect on the art's series group, expanding the group draws the art's plug-in effect.
|
||||
let shift = {
|
||||
let t = s.execute("shape.rectangle", &json!({"x": 700, "y": 20, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("effect.apply", &json!({"effect": "distort.transform", "ids": [t], "params": {"moveH": 5}})).unwrap();
|
||||
s.doc().unwrap().doc.node(vectorcraft_engine::doc::NodeId(t)).unwrap().appearance.effects[0].clone()
|
||||
};
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(art_series).unwrap().appearance.effects.push(shift);
|
||||
s.execute("select.set", &json!({"ids": [graph]})).unwrap();
|
||||
safe_mode::set_active(Some(safe_mode::Source::Flag));
|
||||
let err = s.execute("effect.expandAppearance", &json!({})).unwrap_err().to_string();
|
||||
safe_mode::set_active(None);
|
||||
assert!(err.contains("a plug-in effect in the selection can't be expanded in Safe Mode"), "{err}");
|
||||
// With a Gaussian Blur on a fill of the art's series group instead, expanding the group makes an image of it, art included.
|
||||
let blurred_fill = {
|
||||
let t = s.execute("shape.rectangle", &json!({"x": 700, "y": 40, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("effect.apply", &json!({"effect": "blur.gaussian", "ids": [t], "item": 0, "params": {"radius": 2}})).unwrap();
|
||||
s.doc().unwrap().doc.node(vectorcraft_engine::doc::NodeId(t)).unwrap().appearance.items[0].clone()
|
||||
};
|
||||
{
|
||||
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
let g = d.node_mut(art_series).unwrap();
|
||||
g.appearance.effects.clear();
|
||||
g.appearance.items.push(blurred_fill);
|
||||
}
|
||||
s.execute("select.set", &json!({"ids": [graph]})).unwrap();
|
||||
safe_mode::set_active(Some(safe_mode::Source::Flag));
|
||||
let err = s.execute("effect.expandAppearance", &json!({})).unwrap_err().to_string();
|
||||
safe_mode::set_active(None);
|
||||
assert!(err.contains("a plug-in effect in the selection can't be expanded in Safe Mode"), "{err}");
|
||||
}
|
||||
|
||||
/// In Safe Mode, a plug-in effect on a dimension selected with the art it measures does not stop
|
||||
/// Expand Appearance: the dimension is left as it is, its effect included, and the art expands.
|
||||
#[test]
|
||||
|
||||
+11
-7
@@ -631,7 +631,9 @@ For `draw_shape` and `draw_path`, omitted paint fields inherit the current [new-
|
||||
For a filled shape or path without an outline, pass an explicit `fill` and `stroke: "none"`.
|
||||
Lines start with no fill and a visible stroke.
|
||||
The Fill/Stroke proxy commands run through `run_command`: `paint.invert` and `paint.complement` recolor the active
|
||||
proxy, including a grayscale image's colorized fill, while keeping each color's model. `paint.lastColor` /
|
||||
proxy, including a grayscale image's colorized fill, while keeping each color's model. Given a group without an
|
||||
Appearance item, they recolor the active proxy's fills or strokes of the objects inside it and of the group itself;
|
||||
`target: "contents"` leaves the group's own fills or strokes as they are. `paint.lastColor` /
|
||||
`paint.lastGradient` re-apply the last solid color or gradient, and `paint.recent` returns the recent colors that every
|
||||
paint command (and the eyedropper) feeds: up to 30, newest first and each once, kept across documents for the session.
|
||||
`paint.proxies` returns what the proxies show: the fill and stroke, which one is active, and whether the selected
|
||||
@@ -852,15 +854,17 @@ inside a series group other than a path directly in it, a legend swatch's includ
|
||||
reached through `target: "contents"`. These commands leave that art out, edit the other objects they are given, and
|
||||
are an error when all their objects are design art: the `appearance.*` commands listed above, `effect.apply`,
|
||||
`appearance.addEffect` and the other `effect.*` commands that edit, move or remove an effect,
|
||||
`effect.expandAppearance` (which also leaves the art inside a selected graph or series group as it was, unless an
|
||||
effect of that group turns the whole group into other art, and is disabled when only design art is selected or
|
||||
given), `threeD.mapGraphic`,
|
||||
`effect.expandAppearance` (which also leaves the art inside a selected graph or series group as it was, except that
|
||||
an effect of that group which reshapes or replaces the whole group bakes into that art too, and is disabled when only
|
||||
design art is selected or given), `threeD.mapGraphic`,
|
||||
`graphicStyle.apply`, `graphicStyle.addFromLibrary` with `apply`, `graphicStyle.breakLink`, `transparency.set`, the
|
||||
`paint.freeform.*` commands that edit points and lines, and `appearance.copyFrom` as the target.
|
||||
`appearance.transfer` with design art as its `target`, or as its `source` without `copy`, is an error.
|
||||
`paint.setFill`, `paint.setStroke`, `paint.swap`, `paint.default`, `paint.invert` and `paint.complement` with an
|
||||
`item`, `stroke.set`, `stroke.weightUp`, `stroke.weightDown`, `stroke.setAdvanced`, `paint.editGradient`,
|
||||
`paint.setGradientGeom` and `object.setOverprint` leave the art out without an error. When the first selected object is design art, the commands
|
||||
`paint.setFill`, `paint.setStroke`, `paint.swap`, `paint.default`, `paint.invert`, `paint.complement`,
|
||||
`stroke.set`, `stroke.weightUp`, `stroke.weightDown`, `stroke.setAdvanced`, `paint.editGradient`,
|
||||
`paint.setGradientGeom` and `object.setOverprint` leave the art out without an error. When all their objects are design art,
|
||||
`paint.editGradient`, `paint.swap`, `paint.default`, `paint.invert`, `paint.complement` and `stroke.set` also leave
|
||||
the paint and stroke weight for new art and the remembered colors as they are. When the first selected object is design art, the commands
|
||||
do not use the Appearance panel's active row. When the first object given is design art, rows and effects given with
|
||||
`aligned: true` are rows of its stack in `appearance.setItem`, `appearance.removeItem`, `appearance.duplicateItem`,
|
||||
`transparency.set` with an `item`, `effect.apply`, `appearance.addEffect` and the other `effect.*` commands that edit,
|
||||
|
||||
Reference in New Issue
Block a user