diff --git a/crates/engine/src/cmd/appearance.rs b/crates/engine/src/cmd/appearance.rs index 78a258ac1c..1ce0228b9d 100644 --- a/crates/engine/src/cmd/appearance.rs +++ b/crates/engine/src/cmd/appearance.rs @@ -129,7 +129,7 @@ pub fn specs() -> Vec { "Eyedropper", [], None, - "{source: id, ids?, pickUp?, apply? (trees as eyedropper.setOptions, merged over the Eyedropper Options for this call), reverse?: bool, append?: bool, defaultsOnly?: bool} copy the attributes both picked up and applied (by default the whole appearance stack, opacity and blend mode, and from type to type the character and paragraph attributes using effective character dimensions while preserving target transforms) from `source` to ids (default: highlighted characters while editing type, after Select All in a frame of threaded text the text of the story's frames, leaving out `source`, otherwise the object selection; highlighted characters reject enabled non-neutral object transparency), the fill, stroke and weight also to the paint defaults; `defaultsOnly` updates only paint defaults and cannot be combined with ids, reverse or append; `reverse` (Alt-click) copies from the first selected object (or ids) onto `source` instead, or with nothing selected what the Eyedropper picked up last (a click without Alt); `append` (Shift+Alt-click) adds the source's fills, strokes and effects on top of each target's stack; placed gradients land at the same place relative to each target's bounds (defaults: fitted to new art); with an object of a dynamic symbol instance selected by Direct Selection and no ids, reverse, append or defaultsOnly, that object takes the source's fill and stroke (solid, none, gradient or pattern) for this instance only, as the Eyedropper Options allow, and a source that is not a path, compound path or type, or options that pick up neither color, are refused. On a path directly in a graph series, 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: docs/mcp.md); on a graph label, the graph stores, for every label of its kind, the character attributes the pick gives all of its text, its object opacity and blend mode, and with `append` its topmost visible fill and stroke rows; on a graph's axis line, tick mark or radar ring, its topmost visible fill and stroke rows and object opacity and blend mode for every line of its kind → {ids}", + "{source: id, ids?, pickUp?, apply? (trees as eyedropper.setOptions, merged over the Eyedropper Options for this call), reverse?: bool, append?: bool, defaultsOnly?: bool} copy the attributes both picked up and applied (by default the whole appearance stack, opacity and blend mode, and from type to type the character and paragraph attributes using effective character dimensions while preserving target transforms) from `source` to ids (default: highlighted characters while editing type, after Select All in a frame of threaded text the text of the story's frames, leaving out `source`, otherwise the object selection; highlighted characters reject enabled non-neutral object transparency), the fill, stroke and weight also to the paint defaults; `defaultsOnly` updates only paint defaults and cannot be combined with ids, reverse or append; `reverse` (Alt-click) copies from the first selected object (or ids) onto `source` instead, or with nothing selected what the Eyedropper picked up last (a click without Alt); `append` (Shift+Alt-click) adds the source's fills, strokes and effects on top of each target's stack; placed gradients land at the same place relative to each target's bounds (defaults: fitted to new art); with an object of a dynamic symbol instance selected by Direct Selection and no ids, reverse, append or defaultsOnly, that object takes the source's fill and stroke (solid, none, gradient or pattern) for this instance only, as the Eyedropper Options allow, and a source that is not a path, compound path or type, or options that pick up neither color, are refused. On a path directly in a graph series, 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: docs/mcp.md); on a graph label, the graph stores, for every label of its kind, the character attributes the pick gives all of its text, its object opacity and blend mode, and with `append` its topmost visible fill and stroke rows; on a graph's axis line, tick mark or radar ring, its topmost visible fill and stroke rows (not for a pick of only the object's transparency) and object opacity and blend mode for every line of its kind → {ids}", has_doc, copy_from ), @@ -1069,6 +1069,9 @@ fn set_item(s: &mut Session, p: &Value) -> Result { let per = items_per_target(&before, &ids, &[idx], aligned(p, "appearance.setItem")?); super::overprint::edit_counted(s, "Appearance", |d| { let mut edited = Vec::new(); + // The targets that have the row: an aligned row leaves out the members of a graph's Axes or Legend group that + // lack it, and the graph keeps the rows of the labels and lines it reached only. + let mut reached = Vec::new(); for (id, at) in ids.iter().zip(&per) { let Some(&idx) = at.first() else { continue }; let Some(n) = d.node_mut(*id) else { continue }; @@ -1079,6 +1082,7 @@ fn set_item(s: &mut Session, p: &Value) -> Result { } let bounds = n.geometric_bounds(); let Some(item) = n.appearance.items.get_mut(idx) else { continue }; + reached.push(*id); let (pp, op, bl, vis, bounds) = match item { AppearanceItem::Fill(f) => (&mut f.paint, &mut f.opacity, &mut f.blend, &mut f.visible, bounds), AppearanceItem::Stroke(st) => { @@ -1121,7 +1125,7 @@ fn set_item(s: &mut Session, p: &Value) -> Result { let mut rows = top_paint_changed(&before, d, &ids); rows.extend(painted_rows(d, written)); let painted = super::graph::capture_series_top_paints(d, &rows); - let labels = super::graph::capture_label_rows(d, &ids); + let labels = super::graph::capture_label_rows(d, &reached); Ok(count_changed(d, &before, &ids) + usize::from(kept || painted || labels)) })?; if let Some(pa) = &paint { @@ -1156,8 +1160,13 @@ fn remove_item(s: &mut Session, p: &Value) -> Result { let before = s.doc()?.doc.clone(); let per = items_per_target(&before, &ids, &idx, aligned(p, "appearance.removeItem")?); super::overprint::edit_counted(s, "Remove Item", |d| { + // The targets that had a row to remove, as in `set_item`. + let mut reached = Vec::new(); for (id, idx) in ids.iter().zip(&per) { if let Some(n) = d.node_mut(*id) { + if idx.iter().any(|i| *i < n.appearance.items.len()) { + reached.push(*id); + } let mut idx = idx.clone(); idx.sort_unstable(); // From the top down, so the lower indices stay valid. @@ -1169,7 +1178,7 @@ fn remove_item(s: &mut Session, p: &Value) -> Result { } let kept = super::graph::capture_series_opacity(d, &top_opacity_changed(&before, d, &ids)); let painted = super::graph::capture_series_top_paints(d, &top_paint_changed(&before, d, &ids)); - let labels = super::graph::capture_label_rows(d, &ids); + let labels = super::graph::capture_label_rows(d, &reached); Ok(count_changed(d, &before, &ids) + usize::from(kept || painted || labels)) })?; // The rows removed from the active row's object, which need not come first in `ids`. @@ -1207,6 +1216,8 @@ fn duplicate_item(s: &mut Session, p: &Value) -> Result { // Copies that became the topmost visible row of their kind: the series stores their paint even when the object // showed it. let mut added = Vec::new(); + // The targets that took a copy, as in `set_item`. + let mut reached = Vec::new(); for (id, idx) in ids.iter().zip(&per) { let Some(n) = d.node_mut(*id) else { continue }; let has_contents = n.contents_label().is_some(); @@ -1231,6 +1242,7 @@ fn duplicate_item(s: &mut Session, p: &Value) -> Result { ap.set_contents_at(c); } any = true; + reached.push(*id); continue; } let mut idx = idx.clone(); @@ -1245,6 +1257,9 @@ fn duplicate_item(s: &mut Session, p: &Value) -> Result { added.push((*id, kind)); } any = true; + if reached.last() != Some(id) { + reached.push(*id); + } } } } @@ -1255,7 +1270,7 @@ fn duplicate_item(s: &mut Session, p: &Value) -> Result { let mut rows = top_paint_changed(&before, d, &ids); rows.extend(painted_rows(d, added)); super::graph::capture_series_top_paints(d, &rows); - super::graph::capture_label_rows(d, &ids); + super::graph::capture_label_rows(d, &reached); Ok(()) })?; // Rows at or above each copy move up by one (by the block, for copies placed at `to`). @@ -1697,8 +1712,21 @@ fn copy_from(s: &mut Session, p: &Value) -> Result { }; // `append` adds rows and leaves the characters as they were. let text = !append && super::graph::capture_label_text(d, &targets, kept_text, kept_para)?; - // A graph line is a path: every pick writes its rows. - let lines = super::graph::capture_line_rows(d, &targets); + // A graph line is a path: a pick of fill or stroke attributes writes its rows. A pick of only the object's + // opacity and blend mode leaves the fill and stroke its kind keeps, as `capture_label_object_opacity` keeps them. + let to_rows = append + || fill.color + || fill.transparency + || fill.overprint + || stroke.color + || stroke.transparency + || stroke.overprint + || stroke.weight + || stroke.cap + || stroke.join + || stroke.miter + || stroke.dash; + let lines = to_rows && super::graph::capture_line_rows(d, &targets); Ok(count_changed(d, &before, &targets) + usize::from(kept || painted || object || rows || text || lines)) })?; Ok(finish(s, &targets)) diff --git a/crates/engine/src/cmd/graph/series.rs b/crates/engine/src/cmd/graph/series.rs index 7c3dcf90e6..e9b4b15185 100644 --- a/crates/engine/src/cmd/graph/series.rs +++ b/crates/engine/src/cmd/graph/series.rs @@ -529,16 +529,20 @@ pub(crate) fn capture_label_styles(doc: &mut Document, ids: &[NodeId], change: & } /// Apply `change` to the style graph `graph` keeps for its labels of kind `role` (or the default one). Returns whether -/// the kept style changed. +/// the kept style changed. A kind that keeps no style and that `change` leaves at the default style keeps none. fn store_label_style(doc: &mut Document, graph: NodeId, role: Role, change: &dyn Fn(&mut CharStyle)) -> bool { let Some(spec) = doc.node_mut(graph).and_then(|n| n.extras_if_any().and_then(|e| e.graph.as_deref_mut())) else { return false }; - let mut style = label_style(&spec.label_styles, role); + let was = label_style(&spec.label_styles, role); + let mut style = was.clone(); change(&mut style); let slot = match role { Role::Value => &mut spec.label_styles.values, Role::Category => &mut spec.label_styles.categories, Role::Legend => &mut spec.label_styles.legend, }; + if slot.is_none() && style == was { + return false; + } let changed = slot.as_ref() != Some(&style); *slot = Some(style); changed diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index 967d4dc9f2..5e4d99f7ad 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -6294,6 +6294,83 @@ fn a_row_index_given_the_axes_group_is_a_row_of_its_backmost_member_with_aligned assert!((l.appearance.items[top_row(&s, l.id, false)].opacity() - 0.4).abs() < 1e-6, "the stroke row's opacity is kept"); } +/// An aligned row edit given the Axes group leaves alone what a kind of label keeps from a label without that row: the +/// category labels keep the orange fill row of one of them through an edit of the lines' stroke rows. +#[test] +fn an_aligned_row_edit_given_the_axes_group_keeps_the_rows_of_labels_it_did_not_reach() { + let cases = [("appearance.setItem", json!({"color": "#0000ff"})), ("appearance.duplicateItem", json!({})), ("appearance.removeItem", json!({}))]; + every_case(cases, |(cmd, params)| { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let id = graph(&mut s, "column"); + let c = text_id(&s, id, "Axes", "Q2"); + s.execute("appearance.addFill", &json!({"ids": [c.0]})).unwrap(); + s.execute("appearance.setItem", &json!({"ids": [c.0], "index": 0, "color": "#ff8000"})).unwrap(); + let kept = spec(&s, id).label_styles.categories_rows; + assert!(kept.is_some(), "the category labels keep the orange row"); + let axes = group(s.doc().unwrap().doc.node(id).unwrap(), "Axes").clone(); + let back = axes.children().unwrap()[0].id; + let mut p = params.clone(); + p["ids"] = json!([axes.id.0]); + p["index"] = json!(top_row(&s, back, false)); + p["aligned"] = json!(true); + s.execute(cmd, &p).unwrap(); + assert_eq!(spec(&s, id).label_styles.categories_rows, kept, "{cmd}"); + }); +} + +/// An Eyedropper pick of only the object's opacity on an axis line keeps the stroke its kind keeps from another line, +/// though the line it is given still shows the default stroke. +#[test] +fn an_eyedropper_pick_of_only_the_opacity_keeps_the_stroke_its_kind_keeps() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let id = graph(&mut s, "column"); + let rect = NodeId(s.execute("shape.rectangle", &json!({"x": 600, "y": 600, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.setProps", &json!({"ids": [rect.0], "opacity": 50})).unwrap(); + let lines = line_nodes(&s, id); + s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "color": "#ff0000"})).unwrap(); + let fill = json!({"color": false, "transparency": false, "overprint": false}); + let stroke = json!({"color": false, "transparency": false, "overprint": false, "weight": false, "cap": false, "join": false, "miter": false, "dash": false}); + let opacity_only = json!({"appearance": {"transparency": true, "fill": fill, "stroke": stroke}}); + s.execute("appearance.copyFrom", &json!({"source": rect.0, "ids": [lines[1].id.0], "pickUp": opacity_only})).unwrap(); + let looks = redrawn_line_looks(&mut s, id); + assert!(looks.iter().all(|l| l.0 == "#ff0000" && (l.2 - 0.5).abs() < 1e-6), "{looks:?}"); +} + +/// Default Fill and Stroke on a label or an axis line that already shows the default paint is kept for its kind, in +/// place of the paint the kind keeps from another label or line. +#[test] +fn default_fill_and_stroke_on_a_label_or_line_that_shows_it_is_kept_for_its_kind() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let id = graph(&mut s, "column"); + s.execute("paint.setFill", &json!({"ids": [text_id(&s, id, "Axes", "4").0], "color": "#ff0000"})).unwrap(); + let hex = |st: &CharStyle| st.fill.color().map(|c| c.to_hex()); + assert_eq!(hex(&label_run_style(&s, id, "Axes", "0")).as_deref(), Some("#000000"), "the other labels show black until the graph is drawn again"); + s.execute("paint.default", &json!({"ids": [text_id(&s, id, "Axes", "0").0]})).unwrap(); + label_looks(&mut s, id); + let st = label_run_style(&s, id, "Axes", "4"); + assert_eq!((hex(&st).as_deref(), st.stroke.is_none()), (Some("#000000"), true)); + let lines = line_nodes(&s, id); + s.execute("paint.default", &json!({"ids": [lines[0].id.0]})).unwrap(); + s.execute("paint.setStroke", &json!({"ids": [lines[1].id.0], "color": "#ff0000"})).unwrap(); + s.execute("paint.default", &json!({"ids": [lines[0].id.0]})).unwrap(); + assert!(redrawn_line_looks(&mut s, id).iter().all(|l| *l == ("#000000".into(), 1.0, 1.0)), "the 1 pt black default stroke"); +} + +/// Default Fill and Stroke on a label of a kind that keeps no style, which already shows the default paint, is no edit. +#[test] +fn default_fill_and_stroke_on_a_label_that_shows_it_is_no_edit() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let id = graph(&mut s, "column"); + let undo = s.doc().unwrap().history.undo.len(); + s.execute("paint.default", &json!({"ids": [text_id(&s, id, "Axes", "4").0]})).unwrap(); + assert_eq!(spec(&s, id).label_styles.values, None); + assert_eq!(s.doc().unwrap().history.undo.len(), undo, "nothing changed"); +} + /// Redefine Graphic Style on an Axes group linked to the style with rows of its own (as a file can hold it) restyles and /// links the lines and labels in it, and clears and unlinks the group; Break Link given the group unlinks them. A style /// applied through the group links its lines and labels, and the graph keeps their look, not the link, when it is drawn diff --git a/crates/engine/src/cmd/paint.rs b/crates/engine/src/cmd/paint.rs index 174d259164..db0769d718 100644 --- a/crates/engine/src/cmd/paint.rs +++ b/crates/engine/src/cmd/paint.rs @@ -955,8 +955,14 @@ fn default_paint(s: &mut Session, p: &Value) -> Result { } } let kept = super::appearance::capture_series_writes(&before, d, &ids, (true, true), (true, true), false, false); - let labels = super::graph::capture_label_changes(&before, d, &ids, false); - Ok(super::appearance::count_changed(d, &before, &ids) + usize::from(kept || labels)) + // The graph keeps the default paint for every label and line of a kind it reached, also when one already + // showed it. + let labels = super::graph::capture_label_styles(d, &ids, &|st| { + st.fill = Paint::solid(Color::BLACK); + st.stroke = Paint::None; + }); + let lines = super::graph::capture_line_rows(d, &ids); + Ok(super::appearance::count_changed(d, &before, &ids) + usize::from(kept || labels || lines)) })?; } super::newart::reset(s); diff --git a/docs/mcp.md b/docs/mcp.md index bcbee312fd..dbfe3f4a2a 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -809,7 +809,8 @@ except by `graphicStyle.redefine`, which passes the style to the lines and label holds. An `index`, `indices` or fill or stroke `item` given an Axes or Legend group whose lines and labels have different rows, as the lines and labels of a new graph do, is an error unless `aligned` is true, which makes it a row of the stack of the backmost line or label in the group, and `appearance.moveItem` does not move the rows of such a -group. The graph keeps no graphic style link on its lines and +group. With `aligned`, the graph stores the rows of only the lines and labels that have that row; a kind of label none +of whose labels has it keeps what it stored before. The graph keeps no graphic style link on its lines and labels: they keep the look of a style applied to them, and are unlinked when the graph is drawn again. The commands find these groups by their names, so a renamed Axes or Legend group is edited itself. @@ -856,7 +857,9 @@ placement they set there. A stroke weight and dashes are stored as the label sho scaled. When the characters of labels of one kind change differently, the frontmost label sets what the kind keeps. On a pie with Legends in Wedges whose legend labels have no fill of their own, the legend labels' fill is stored only when every legend label the command reaches ends up with the same fill. Otherwise they are drawn again in black, or white on -a dark wedge. What they change on a graph's axis lines, tick marks and radar rings is stored as described below. +a dark wedge. `paint.default` stores the black fill and no stroke for the kind of each label it reaches, also when the +label already showed them. What they change on a graph's axis lines, tick marks and radar rings is stored as described +below. `text.setFormat` without `start` and `end`, on a graph or on its labels, stores the character and paragraph attributes it sets for each kind of label it reaches (value axis numbers, category labels, legend labels), and every label of that @@ -889,8 +892,9 @@ opacity and blend mode. `paint.setFill`, `paint.setStroke` and the recolor comma graph, its Axes group or a group that holds the graph, so the lines keep the paint those commands give a whole graph. The other commands store them when given the line itself, and the `appearance.*` and graphic style commands and `transparency.set` with an `item` also when given the Axes group, as described above. `transparency.set` without an -`item` and `object.setProps` store only the object opacity and blend mode. `appearance.copyFrom` stores the rows -whatever it picks up, with or without `append`. The recolor commands, `paint.swap` and `paint.default` store a line's rows when they change them. The +`item` and `object.setProps` store only the object opacity and blend mode. `appearance.copyFrom` stores the rows when it +picks up a fill or stroke attribute or adds rows with `append`; a pick of only the object's transparency stores only the +object opacity and blend mode. The recolor commands and `paint.swap` store a line's rows when they change them. The other commands store them even when the line already showed them, except that rows equal to a kind's default stroke (0.5 pt black for axis lines and tick marks, 0.25 pt gray for rings) are not saved. When one command reaches several lines of one kind, the frontmost line in paint order sets what the kind keeps, whatever order the `ids` are in. A gradient is stored without its placement, and