diff --git a/crates/engine/src/cmd/graph.rs b/crates/engine/src/cmd/graph.rs index 50f3333627..3714fef36c 100644 --- a/crates/engine/src/cmd/graph.rs +++ b/crates/engine/src/cmd/graph.rs @@ -46,11 +46,11 @@ use generate::generate; #[cfg(test)] use series::default_series_color; pub(crate) use series::{ - LabelTextChange, capture_label_anti_alias, capture_label_changes, capture_label_fill_stroke_rows, capture_label_object_opacity, - capture_label_paragraphs, capture_label_rows, capture_label_styles, capture_label_text, capture_line_changes, capture_line_fill_stroke_rows, - capture_line_rows, capture_series_object_opacity, capture_series_opacity, capture_series_top_paints, is_design_art, is_graph_line, - label_object_opacity_differs, labels_in, paint_targets, series_group_marks, series_group_marks_where, series_groups_among, series_marks_in, - series_object_opacity_differs, shown_paint, + LabelTextChange, capture_label_added_rows, capture_label_anti_alias, capture_label_changes, capture_label_fill_stroke_rows, + capture_label_object_opacity, capture_label_paragraphs, capture_label_rows, capture_label_styles, capture_label_text, capture_line_changes, + capture_line_fill_stroke_rows, capture_line_rows, capture_series_object_opacity, capture_series_opacity, capture_series_top_paints, + is_design_art, is_graph_line, label_object_opacity_differs, labels_in, paint_targets, series_group_marks, series_group_marks_where, + series_groups_among, series_marks_in, series_object_opacity_differs, shown_paint, }; #[cfg(test)] diff --git a/crates/engine/src/cmd/graph/series.rs b/crates/engine/src/cmd/graph/series.rs index 564f01104f..c6255fa353 100644 --- a/crates/engine/src/cmd/graph/series.rs +++ b/crates/engine/src/cmd/graph/series.rs @@ -825,6 +825,9 @@ enum Keep { Object, /// The fill and stroke rows; the object opacity and blend mode stay as the kind keeps them. Rows, + /// The fill row (`fill`) and the stroke row (`stroke`) of an edit that put new rows of those kinds on top of the stack; + /// the other kind of row and the object opacity and blend mode stay as the kind keeps them. + Added { fill: bool, stroke: bool }, } /// `shown` with the parts of the rows `kept` keeps that `keep` leaves as they are. @@ -841,6 +844,33 @@ fn keep_rows(kept: vectorcraft_doc::LabelRows, shown: vectorcraft_doc::LabelRows (shown.paint.opacity, shown.paint.blend) = (kept.paint.opacity, kept.paint.blend); shown } + Keep::Added { fill: false, stroke: false } => kept, + Keep::Added { fill, stroke } => { + // Whether the kept row of the kind the edit did not add paints below the characters: the kept rows, from the + // bottom, are the stroke and the fill (in the order `stroke_under` gives) and the bottom `below` of them paint + // below the characters. + let other_below = { + let present = |f: bool| if f { kept.paint.fill.is_some() } else { kept.paint.stroke.is_some() }; + let order = if kept.stroke_under { [false, true] } else { [true, false] }; + let at = order.iter().filter(|f| present(**f)).position(|f| *f != fill); + at.is_some_and(|at| at < usize::from(kept.below.unwrap_or(0))) + }; + let (mut rows, s) = (kept, &shown.paint); + if fill { + (rows.paint.fill, rows.paint.fill_opacity, rows.paint.fill_blend) = (s.fill.clone(), s.fill_opacity, s.fill_blend); + } + if stroke { + (rows.paint.stroke, rows.paint.stroke_width, rows.paint.stroke_opacity, rows.paint.stroke_blend) = + (s.stroke.clone(), s.stroke_width, s.stroke_opacity, s.stroke_blend); + } + // The added row paints above the characters and above a kept row of the other kind, which keeps its place. + (rows.below, rows.stroke_under) = match (fill, stroke) { + (true, false) => (other_below.then_some(1), rows.paint.stroke.is_some()), + (false, true) => (other_below.then_some(1), false), + (true, true) | (false, false) => (shown.below, shown.stroke_under), + }; + rows + } } } @@ -974,6 +1004,14 @@ pub(crate) fn capture_label_fill_stroke_rows(doc: &mut Document, ids: &[NodeId]) store_label_rows(doc, ids, Keep::Rows) } +/// [`capture_label_fill_stroke_rows`] for an edit that put a new fill row (`fill`) or stroke row (`stroke`) on top of +/// graph labels and graph lines `ids`: only those kinds of row are kept for every label or line of their kind, and the +/// other kind stays as the kind keeps it, also when a label or line still shows another one until the graph is drawn +/// again. Returns whether what the graph keeps changed. +pub(crate) fn capture_label_added_rows(doc: &mut Document, ids: &[NodeId], (fill, stroke): (bool, bool)) -> bool { + store_label_rows(doc, ids, Keep::Added { fill, stroke }) +} + /// Would [`capture_label_object_opacity`] change what a graph keeps for labels `ids`? Reads the document without copying /// it. pub(crate) fn label_object_opacity_differs(doc: &Document, ids: &[NodeId]) -> bool { diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index 3499a25dff..181913dba5 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -7114,6 +7114,82 @@ fn a_graphic_style_added_to_a_label_or_axis_line_keeps_the_opacity_its_kind_keep } } +/// A graphic style that shows only a fill, added on top of a graph label's or axis line's appearance, keeps its fill for +/// the kind and leaves the stroke the kind keeps from another label or line, though the one it is added to still shows +/// its old stroke. +#[test] +fn a_fill_only_graphic_style_added_to_a_label_or_axis_line_keeps_the_stroke_its_kind_keeps() { + for label in [true, false] { + 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("paint.setFill", &json!({"ids": [rect.0], "color": "#00ff00"})).unwrap(); + s.execute("paint.setStroke", &json!({"ids": [rect.0], "color": "#ff0000"})).unwrap(); + s.execute("appearance.setItem", &json!({"ids": [rect.0], "index": top_row(&s, rect, false), "visible": false})).unwrap(); + s.execute("graphicStyle.new", &json!({"id": rect.0, "name": "Green"})).unwrap(); + let (first, second) = if label { + (text_id(&s, id, "Axes", "Q1"), text_id(&s, id, "Axes", "Q2")) + } else { + let lines = line_nodes(&s, id); + (lines[0].id, lines[1].id) + }; + if label { + s.execute("appearance.addStroke", &json!({"ids": [first.0]})).unwrap(); + let at = top_row(&s, first, false); + s.execute("appearance.setItem", &json!({"ids": [first.0], "index": at, "color": "#0000ff", "weight": 2})).unwrap(); + } else { + s.execute("paint.setStroke", &json!({"ids": [first.0], "color": "#0000ff"})).unwrap(); + assert_eq!(line_look(&line_nodes(&s, id)[1]).0, "#000000", "the other lines show black until the graph is drawn again"); + } + s.execute("graphicStyle.apply", &json!({"name": "Green", "ids": [second.0], "add": true})).unwrap(); + if label { + let looks = redrawn_label_rows(&mut s, id); + for q in ["Q1", "Q2", "Q3"] { + let (rows, _) = rows_of(&looks, q); + assert!(rows.iter().any(|r| r.0 && r.1 == "#00ff00"), "{q}: {rows:?}"); + assert!(rows.iter().any(|r| !r.0 && r.1 == "#0000ff"), "{q}: {rows:?}"); + assert!(!rows.iter().any(|r| r.1 == "#ff0000"), "{q}: {rows:?}"); + } + } else { + let looks = redrawn_line_looks(&mut s, id); + assert!(looks.iter().all(|l| l.0 == "#0000ff"), "{looks:?}"); + let fills: Vec> = line_nodes(&s, id).iter().map(|n| n.appearance.fill_paint().color().map(|c| c.to_hex())).collect(); + assert!(fills.iter().all(|f| f.as_deref() == Some("#00ff00")), "{fills:?}"); + } + } +} + +/// A graphic style that shows only a fill, added on top of a graph label, leaves the stroke its kind keeps below the +/// characters there, and the added fill paints above the characters, though the label it is added to has no row below +/// them. +#[test] +fn a_fill_only_graphic_style_added_to_a_label_keeps_the_kind_stroke_below_the_characters() { + 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("paint.setFill", &json!({"ids": [rect.0], "color": "#00ff00"})).unwrap(); + s.execute("paint.setStroke", &json!({"ids": [rect.0], "color": "#ff0000"})).unwrap(); + s.execute("appearance.setItem", &json!({"ids": [rect.0], "index": top_row(&s, rect, false), "visible": false})).unwrap(); + s.execute("graphicStyle.new", &json!({"id": rect.0, "name": "Green"})).unwrap(); + let (q1, q2) = (text_id(&s, id, "Axes", "Q1"), text_id(&s, id, "Axes", "Q2")); + s.execute("appearance.addStroke", &json!({"ids": [q1.0]})).unwrap(); + let at = top_row(&s, q1, false); + s.execute("appearance.setItem", &json!({"ids": [q1.0], "index": at, "color": "#0000ff", "weight": 2})).unwrap(); + s.execute("appearance.moveItem", &json!({"ids": [q1.0], "from": "contents", "to": at + 1})).unwrap(); + s.execute("graphicStyle.apply", &json!({"name": "Green", "ids": [q2.0], "add": true})).unwrap(); + let looks = redrawn_label_rows(&mut s, id); + let n = s.doc().unwrap().doc.node(id).unwrap().clone(); + for q in ["Q1", "Q2", "Q3"] { + let (rows, _) = rows_of(&looks, q); + assert_eq!(rows.iter().map(|r| (r.0, r.1.as_str())).collect::>(), [(false, "#0000ff"), (true, "#00ff00")], "{q}"); + let label = + group(&n, "Axes").children().unwrap().iter().find(|c| matches!(&c.kind, NodeKind::Text(t) if t.plain_text() == q)).unwrap().clone(); + assert_eq!(label.appearance.contents_at(), 1, "{q}: the stroke paints below the characters and the fill above them"); + } +} + /// An Eyedropper pick with `append` and without the object's transparency on a graph label keeps the object opacity its /// kind keeps from another label, though the label it is applied to still shows 100%. #[test] diff --git a/crates/engine/src/cmd/style.rs b/crates/engine/src/cmd/style.rs index 36716a4905..a5f4e67592 100644 --- a/crates/engine/src/cmd/style.rs +++ b/crates/engine/src/cmd/style.rs @@ -364,6 +364,9 @@ fn style_apply(s: &mut Session, p: &Value) -> Result { let shows = |kind: bool| g.appearance.items.iter().any(|it| it.is_fill() == kind && it.visible()); let kinds = (shows(true), shows(false)); super::appearance::capture_series_writes(&before, d, &ids, kinds, kinds, false, false); + // A label or graph line keeps those kinds of row for its kind. A style added on top leaves the object opacity + // and blend mode, so the kind keeps its own. + super::graph::capture_label_added_rows(d, &ids, kinds); } else { d.graphic_style_id(i); let g = d.graphic_styles[i].clone(); @@ -373,12 +376,7 @@ fn style_apply(s: &mut Session, p: &Value) -> Result { } 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. A style added on top leaves the object opacity and blend - // mode, so the kind keeps its own. - if add { - super::graph::capture_label_fill_stroke_rows(d, &ids); - } else { + // A label or graph line keeps its new rows for its kind. super::graph::capture_label_rows(d, &ids); } Ok(()) diff --git a/docs/mcp.md b/docs/mcp.md index e4385deeda..6af7bf7121 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -895,8 +895,8 @@ their place relative to the Characters row, and the label's object opacity and b label already showed them. `transparency.set` without an `item`, `object.setProps` with an opacity or blend mode, and an `appearance.copyFrom` (Eyedropper) pick of transparency store only the label's object opacity and blend mode; `appearance.copyFrom` with `append` stores the rows, and the object opacity and blend mode only when it also applies -the object's transparency. `graphicStyle.apply` with `add` stores the rows and leaves the object opacity and blend -mode the kind keeps. When the graph is drawn again, every label of that kind gets what is stored, with a placed +the object's transparency. `graphicStyle.apply` with `add` stores only the kinds of row that are visible in the style +and leaves the other kind of row and the object opacity and blend mode the kind keeps. When the graph is drawn again, every label of that kind gets what is stored, with a placed gradient fitted to each label, and the graph saves it as `valuesRows`, `categoriesRows` and `legendRows` in its label styles. Rows below the topmost one of each kind, a row's effects, other stroke options (caps, joins, dashes) and effects applied with `effect.*` are not stored. When labels of one kind are edited together, the last one in the order of the targets (`ids` as given, otherwise paint order) sets what the kind keeps. @@ -919,7 +919,8 @@ line's rows even when the line already showed them. The other commands, an `appe included, store only what they change on a line's rows; the kind keeps its other attributes, also when the line still shows different ones until the graph is drawn again. An `appearance.copyFrom` that does not apply the object's transparency, and `graphicStyle.apply` with `add`, leave the object opacity and blend mode a kind keeps as they are, -also when the line still shows other ones. `paint.default` stores the default fill and stroke rows for a line's kind +also when the line still shows other ones. `graphicStyle.apply` with `add` stores only the kinds of row that are +visible in the style, and the kind keeps its other kind of row. `paint.default` stores the default fill and stroke rows for a line's kind even when the line already showed them, and leaves the object opacity and blend mode the kind keeps. 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