Live Paint: graphic styles on a group's contents give fills to its faces and strokes to its edges

Follows #8765. graphicStyle.apply with target "contents" gave every face
and edge of a Live Paint group the whole style, so faces drew the style's
strokes along their outlines and edges drew its fills. Each face now takes
only the style's fills and each edge only its strokes, with or without
add, and neither is linked to the style. An add that gives a face or edge
nothing leaves it and its link as they are, and an apply that changes
nothing records no undo step.

appearance.clear on the contents leaves a face one empty fill and an edge
one empty stroke, and appearance.reduceToBasic keeps only a face's fill
and only an edge's stroke.

Docs: docs/mcp.md, params text, docs/target-app-parity.md.
This commit is contained in:
dotCooCoo committed 2026-11-20 06:26:51 -08:00
1 parent 291ea9fd00
commit f9c00a5ae5
5 files changed
+143 -9

No files matched your search

+30 -2
View File
@@ -67,7 +67,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Clear Appearance",
["Window", "Appearance"],
None,
"{ids?|id?, target?} leave each object one empty fill and stroke (None; a group none of its own) and reset its opacity and blend mode. On a path directly in a graph series (a series group's id stands for those paths and the group), the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke and the object's opacity and blend mode (exceptions, and the art of a graph design in a series, which is left out: docs/mcp.md); on a graph label, or a graph's axis line, tick mark or radar ring (a graph's Axes group stands for the lines and labels directly in it, its Legend group for the labels), the graph stores that object's topmost visible fill and stroke rows and object opacity and blend mode for every label or line of its kind",
"{ids?|id?, target?} leave each object one empty fill and stroke (None; a group none of its own; in the contents of a Live Paint group, a face one empty fill and an edge one empty stroke) and reset its opacity and blend mode. On a path directly in a graph series (a series group's id stands for those paths and the group), the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke and the object's opacity and blend mode (exceptions, and the art of a graph design in a series, which is left out: docs/mcp.md); on a graph label, or a graph's axis line, tick mark or radar ring (a graph's Axes group stands for the lines and labels directly in it, its Legend group for the labels), the graph stores that object's topmost visible fill and stroke rows and object opacity and blend mode for every label or line of its kind",
has_doc,
clear_appearance
),
@@ -76,7 +76,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Reduce to Basic Appearance",
["Window", "Appearance"],
None,
"{ids?|id?, target?} keep only one visible fill and stroke (without ids: the Appearance panel's active row in place of the topmost one of its kind; else the topmost), without their own opacity, blend mode or effects (a group keeps no rows it lacked), and drop the object's effects. On a path directly in a graph series (a series group's id stands for those paths and the group), the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke (exceptions, and the art of a graph design in a series, which is left out: docs/mcp.md); on a graph label, or a graph's axis line, tick mark or radar ring (a graph's Axes group stands for the lines and labels directly in it, its Legend group for the labels), the graph stores that object's topmost visible fill and stroke rows and object opacity and blend mode for every label or line of its kind (of a line, only what the edit changes)",
"{ids?|id?, target?} keep only one visible fill and stroke (without ids: the Appearance panel's active row in place of the topmost one of its kind; else the topmost), without their own opacity, blend mode or effects (a group keeps no rows it lacked; in the contents of a Live Paint group, a face keeps only the fill and an edge only the stroke), and drop the object's effects. On a path directly in a graph series (a series group's id stands for those paths and the group), the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke (exceptions, and the art of a graph design in a series, which is left out: docs/mcp.md); on a graph label, or a graph's axis line, tick mark or radar ring (a graph's Axes group stands for the lines and labels directly in it, its Legend group for the labels), the graph stores that object's topmost visible fill and stroke rows and object opacity and blend mode for every label or line of its kind (of a line, only what the edit changes)",
has_doc,
reduce_basic
),
@@ -529,6 +529,24 @@ pub(crate) fn contents_skipped(s: &Session, p: &Value, fill: bool) -> Result<Has
Ok(super::paint::live_paint_parts_skipped(&s.doc()?.doc, &appearance_roots(s, p)?, fill))
}
/// The faces and the edges of the Live Paint groups whose contents `p` targets (`target: "contents"`). A command that
/// gives them a whole appearance gives a face only its fill rows and an edge only its stroke rows
/// ([`keep_rows_of_kind`]). Both are empty without `target: "contents"`.
pub(crate) fn contents_parts(s: &Session, p: &Value) -> Result<(HashSet<NodeId>, HashSet<NodeId>)> {
if !targets_contents(p)? {
return Ok((HashSet::new(), HashSet::new()));
}
let (faces, edges, _) = super::paint::live_paint_split(&s.doc()?.doc, &appearance_roots(s, p)?);
Ok((faces, edges))
}
/// Remove from `n` its stroke rows (`fill`) or its fill rows: a Live Paint face draws fills and an edge strokes.
pub(crate) fn keep_rows_of_kind(n: &mut Node, fill: bool) {
if n.appearance.items.iter().any(|it| it.is_fill() != fill) {
n.appearance.items.retain(|it| it.is_fill() == fill);
}
}
/// The refusal of an appearance edit whose targets are all the art of graph designs drawn in graph series.
const DESIGN_ART: &str = "the art of a graph design in a graph series is drawn again from the design: edit the design to change it";
@@ -1044,12 +1062,17 @@ fn add_item(s: &mut Session, p: &Value, fill: bool) -> Result<Value> {
fn clear_appearance(s: &mut Session, p: &Value) -> Result<Value> {
let ids = require_whole_targets(s, p, "appearance.clear")?;
let (faces, edges) = contents_parts(s, p)?;
// When every object is cleared already, the document and its undo history stay as they are.
let before = s.doc()?.doc.clone();
super::overprint::edit_counted(s, "Clear Appearance", |d| {
for id in &ids {
if let Some(n) = d.node_mut(*id) {
clear(n);
// A Live Paint group's face keeps an empty fill and its edge an empty stroke.
if faces.contains(id) || edges.contains(id) {
keep_rows_of_kind(n, faces.contains(id));
}
super::object::snap_if_aligned(n);
}
}
@@ -1284,6 +1307,7 @@ fn is_group(n: &Node) -> bool {
fn reduce_basic(s: &mut Session, p: &Value) -> Result<Value> {
let ids = require_whole_targets(s, p, "appearance.reduceToBasic")?;
let (faces, edges) = contents_parts(s, p)?;
// When every object comes out as it was, the document and its undo history stay as they are.
let before = s.doc()?.doc.clone();
// The Appearance panel's active row (a row of the first selected object's stack, and in each
@@ -1322,6 +1346,10 @@ fn reduce_basic(s: &mut Session, p: &Value) -> Result<Value> {
st.blend = BlendMode::Normal;
n.appearance.items.push(AppearanceItem::Stroke(st));
}
// A Live Paint group's face keeps only its fill and its edge only its stroke.
if faces.contains(id) || edges.contains(id) {
keep_rows_of_kind(n, faces.contains(id));
}
super::object::snap_if_aligned(n);
}
}
+19 -4
View File
@@ -31,7 +31,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Apply Graphic Style",
["Window", "Graphic Styles"],
None,
"{name, add?: bool, ids?|id? (layers too), target?: \"object\"|\"contents\"} give the objects (default: the selection; a group or layer itself, its fills and strokes painting its members and its effects applying to them as one piece; target contents: the objects inside instead) the style's appearance, opacity, blend mode, isolate and knockout, and link them to it (placed gradients land at the same place relative to each object's bounds); add: true (Alt-click) adds its fills, strokes and effects on top of the existing appearance instead and unlinks. On a path directly in a graph series (a series group's id stands for those paths), the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke (with `add`: of the kinds of row the style shows) and, without `add`, the object's opacity and blend mode (exceptions, and the art of a graph design in a series, which is left out: docs/mcp.md); on a graph label, or a graph's axis line, tick mark or radar ring (a graph's Axes group stands for the lines and labels directly in it, its Legend group for the labels), the graph stores that object's topmost visible fill and stroke rows (with `add`: of the kinds of row the style shows) and, without `add`, its object opacity and blend mode for every label or line of its kind. With nothing selected (and no ids) the next object drawn takes the style instead (see appearance.newArt) → {newArt: true}",
"{name, add?: bool, ids?|id? (layers too), target?: \"object\"|\"contents\"} give the objects (default: the selection; a group or layer itself, its fills and strokes painting its members and its effects applying to them as one piece; target contents: the objects inside instead) the style's appearance, opacity, blend mode, isolate and knockout, and link them to it (placed gradients land at the same place relative to each object's bounds); add: true (Alt-click) adds its fills, strokes and effects on top of the existing appearance instead and unlinks. In the contents of a Live Paint group, each face takes only the style's fills and each edge only its strokes, and neither is linked. On a path directly in a graph series (a series group's id stands for those paths), the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke (with `add`: of the kinds of row the style shows) and, without `add`, the object's opacity and blend mode (exceptions, and the art of a graph design in a series, which is left out: docs/mcp.md); on a graph label, or a graph's axis line, tick mark or radar ring (a graph's Axes group stands for the lines and labels directly in it, its Legend group for the labels), the graph stores that object's topmost visible fill and stroke rows (with `add`: of the kinds of row the style shows) and, without `add`, its object opacity and blend mode for every label or line of its kind. With nothing selected (and no ids) the next object drawn takes the style instead (see appearance.newArt) → {newArt: true}",
has_doc,
style_apply
),
@@ -351,6 +351,9 @@ fn style_apply(s: &mut Session, p: &Value) -> Result<Value> {
// The series groups their paths stand for: a style that replaces the appearance clears what they hold themselves,
// and a style added on top unlinks them.
let groups = super::appearance::stood_for(&own, &ids);
// In the contents of a Live Paint group, a face takes the style's fills and an edge its strokes.
let (faces, edges) = super::appearance::contents_parts(s, p)?;
let part = |id: &NodeId| (faces.contains(id) || edges.contains(id)).then(|| faces.contains(id));
if ids.is_empty() {
if p.get("ids").is_some() || p.get("id").is_some() {
return Err(bad("graphicStyle.apply", "no such objects"));
@@ -361,13 +364,20 @@ fn style_apply(s: &mut Session, p: &Value) -> Result<Value> {
return Ok(json!({ "newArt": true }));
}
let before = s.doc()?.doc.clone();
s.edit("Apply Graphic Style", |d, _| {
// An apply that changes nothing (a fill-only style added to edges only) records no undo step.
super::overprint::edit_counted(s, "Apply Graphic Style", |d| {
if add {
let g = d.graphic_styles[i].clone();
for id in &ids {
if let Some(n) = d.node_mut(*id) {
let ap = g.appearance_on(n).into_owned();
n.appearance.items.extend(ap.items);
let kind = part(id);
let rows: Vec<_> = ap.items.into_iter().filter(|it| kind.is_none_or(|fill| it.is_fill() == fill)).collect();
// A face or edge the style adds nothing to keeps its appearance and its link.
if kind.is_some() && rows.is_empty() && ap.effects.is_empty() {
continue;
}
n.appearance.items.extend(rows);
n.appearance.effects.extend(ap.effects);
n.extras_mut().graphic_style = None;
}
@@ -387,13 +397,18 @@ fn style_apply(s: &mut Session, p: &Value) -> Result<Value> {
let mut shared = None;
for id in &ids {
apply_style(d, *id, &g, &mut shared, chars);
// A face or edge that keeps only the rows of its kind no longer has the style's look, so it is not linked.
if let (Some(fill), Some(n)) = (part(id), d.node_mut(*id)) {
super::appearance::keep_rows_of_kind(n, fill);
n.extras_mut().graphic_style = None;
}
}
super::appearance::clear_stood_for(d, &groups);
super::appearance::capture_series_writes(&before, d, &ids, (true, true), (true, true), false, true);
// A label or graph line keeps its new rows for its kind.
super::graph::capture_label_rows(d, &ids);
}
Ok(())
Ok(usize::from(*d != *before))
})?;
ok()
}
+87
View File
@@ -4630,6 +4630,93 @@ fn live_paint_group_with_only_source_paths_ignores_add_fill_and_add_stroke_of_it
assert!(s.execute("appearance.addFill", &json!({"target": "contents"})).is_err(), "no selection is still refused");
}
/// Whether every face of Live Paint group `g` holds only fill rows and every edge only stroke rows.
fn parts_hold_their_kind(s: &Session, g: NodeId) -> bool {
let (faces, edges) = part_looks(s, g);
faces.iter().all(|a| a.items.iter().all(|it| it.is_fill())) && edges.iter().all(|a| a.items.iter().all(|it| !it.is_fill()))
}
/// A graphic style applied to the contents of a Live Paint group, replacing their appearance or added to it, gives
/// the faces its fills and the edges its strokes, and the faces and edges are not linked to it.
#[test]
fn live_paint_group_contents_take_a_graphic_style_by_kind() {
let red = Paint::solid(Color::from_hex("#ff0000").unwrap());
let blue = Paint::solid(Color::from_hex("#0000ff").unwrap());
for add in [false, true] {
let mut s = session();
let g = live(&mut s);
let r = rect(&mut s, 400.0, 0.0, 50.0, 50.0);
s.execute("select.set", &json!({"ids": [r.0]})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap();
s.execute("paint.setStroke", &json!({"color": "#0000ff"})).unwrap();
s.execute("graphicStyle.new", &json!({"name": "Both", "id": r.0})).unwrap();
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("graphicStyle.apply", &json!({"name": "Both", "target": "contents", "add": add})).unwrap();
assert!(parts_hold_their_kind(&s, g), "add {add}: faces hold fills and edges strokes");
let (faces, edges) = part_looks(&s, g);
assert!(faces.iter().all(|a| a.items.iter().any(|it| *it.paint() == red)), "add {add}: each face takes the style's fill");
assert!(edges.iter().all(|a| a.items.iter().any(|it| *it.paint() == blue)), "add {add}: each edge takes the style's stroke");
let n = node(&s, g);
let mut parts = vectorcraft_tools::builder::faces(&n).into_iter().chain(vectorcraft_tools::builder::edges(&n));
assert!(parts.all(|p| p.extras().graphic_style.is_none()), "add {add}: no face or edge is linked");
}
}
/// A fill-only graphic style added to the contents of a Live Paint group without faces changes nothing and records no
/// undo step.
#[test]
fn live_paint_group_contents_without_faces_take_no_fill_only_style() {
let mut s = session();
let g = live(&mut s);
let faces: Vec<NodeId> = vectorcraft_tools::builder::faces(&node(&s, g)).iter().map(|p| p.id).collect();
s.edit("t", |d, _| {
d.children_mut(Some(g)).unwrap().retain(|c| !faces.contains(&c.id));
Ok(())
})
.unwrap();
let r = rect(&mut s, 400.0, 0.0, 50.0, 50.0);
s.edit("t", |d, _| {
let fill = AppearanceItem::Fill(FillLayer::new(Paint::solid(Color::from_hex("#ff0000").unwrap())));
d.node_mut(r).unwrap().appearance = vectorcraft_doc::Appearance { items: vec![fill], ..Default::default() }.into();
Ok(())
})
.unwrap();
s.execute("graphicStyle.new", &json!({"name": "Fill", "id": r.0})).unwrap();
// The edges are linked to a style of their own, which they keep.
let edge = vectorcraft_tools::builder::edges(&node(&s, g))[0].id;
s.execute("graphicStyle.new", &json!({"name": "Edge", "id": edge.0})).unwrap();
assert!(node(&s, g).children().unwrap().iter().any(|c| c.extras().graphic_style.is_some()), "an edge is linked");
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
let (before, undo) = (node(&s, g), s.doc().unwrap().history.undo.len());
s.execute("graphicStyle.apply", &json!({"name": "Fill", "target": "contents", "add": true})).unwrap();
assert_eq!(node(&s, g), before, "the edges keep their rows and their link");
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step");
}
/// Clear Appearance and Reduce to Basic Appearance on the contents of a Live Paint group leave each face with fill
/// rows only and each edge with stroke rows only.
#[test]
fn live_paint_group_contents_clear_and_reduce_keep_each_part_to_its_kind() {
for cmd in ["appearance.clear", "appearance.reduceToBasic"] {
let mut s = session();
let g = live(&mut s);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ff00ff"})).unwrap();
s.execute("paint.setStroke", &json!({"color": "#0000ff"})).unwrap();
// A face and an edge given rows of the other kind through their own ids.
let n = node(&s, g);
let (face, edge) = (vectorcraft_tools::builder::faces(&n)[0].id, vectorcraft_tools::builder::edges(&n)[0].id);
s.execute("appearance.addStroke", &json!({"ids": [face.0]})).unwrap();
s.execute("appearance.addFill", &json!({"ids": [edge.0]})).unwrap();
assert!(!parts_hold_their_kind(&s, g), "{cmd}: the face and the edge hold rows of the other kind");
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute(cmd, &json!({"target": "contents"})).unwrap();
assert!(parts_hold_their_kind(&s, g), "{cmd}: faces hold fills and edges strokes");
let (faces, edges) = part_looks(&s, g);
assert!(faces.iter().chain(&edges).all(|a| !a.items.is_empty()), "{cmd}: each face and edge keeps a row");
}
}
/// A selected Live Paint group with no part of the edited kind is still a selection: a stroke edit
/// of a group without edges, or a fill gradient edit of a group without faces, changes nothing and
/// leaves the settings for new art as they are.