mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 14:17:14 +00:00
Graphs: proxy recolors keep a group's own paint and spread checks on design art alone
paint.invert and paint.complement without an item recolor a given group's own fills and strokes with the objects inside it again, except with target "contents", and leave the art of graph designs inside it as it is. A given member of a compound path stands for the compound path. paint.editGradient with a spread or a negative aspect for design art alone no longer checks it against the kind of the default gradient, so it changes nothing instead of failing. A bad value is still an error.
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 {
|
||||
|
||||
@@ -389,7 +389,7 @@ pub(crate) fn items_per_target(d: &vectorcraft_doc::Document, ids: &[NodeId], id
|
||||
}
|
||||
|
||||
/// 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() {
|
||||
|
||||
@@ -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) })
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -588,6 +588,48 @@ fn paint_edits_of_design_art_alone_leave_the_defaults() {
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
/// `transparency.set` without `item` refuses the art of a graph design in a series, and given it with other objects
|
||||
/// sets only theirs.
|
||||
#[test]
|
||||
|
||||
@@ -1107,14 +1107,31 @@ fn proxy_recolor(s: &mut Session, p: &Value, cmd: &str, label: &str, f: &dyn Fn(
|
||||
(json!({ "changed": changed }), Some(index))
|
||||
}
|
||||
// The proxy recolors its paints, including a colorized image's fill, without changing image pixels or pattern tiles.
|
||||
// Design art inside a given graph or series group is left out too.
|
||||
// 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 leaves = targets;
|
||||
if leaves.is_empty() {
|
||||
let contents = super::appearance::targets_contents(p)?;
|
||||
let mut roots = vec![];
|
||||
for id in &ids {
|
||||
let group = !contents
|
||||
&& s.doc()?.doc.node(*id).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 q = json!({"fill": !stroke, "stroke": stroke, "includeImages": false, "includePatterns": false, "ids": leaves.iter().map(|i| i.0).collect::<Vec<_>>()});
|
||||
(super::colorcmds::recolor(s, &q, cmd, label, f)?, None)
|
||||
let art = super::appearance::design_art_in(d, &roots).into_iter().collect();
|
||||
let q = json!({"fill": !stroke, "stroke": stroke, "includeImages": false, "includePatterns": false, "ids": roots.iter().map(|i| i.0).collect::<Vec<_>>()});
|
||||
(super::colorcmds::recolor_except(s, &q, cmd, label, f, art)?, None)
|
||||
}
|
||||
};
|
||||
let shown_ids = if shown_item.is_some() { ids } else { super::appearance::drop_design_art(&s.doc()?.doc, leaf_targets(s, &ids)?) };
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user