diff --git a/crates/engine/src/cmd/clipboard/resources.rs b/crates/engine/src/cmd/clipboard/resources.rs index fdaeddf39d..f912fce7ae 100644 --- a/crates/engine/src/cmd/clipboard/resources.rs +++ b/crates/engine/src/cmd/clipboard/resources.rs @@ -703,7 +703,7 @@ impl Relink { self.name(Res::ParaStyle, s); } } - for r in [&mut l.values_rows, &mut l.categories_rows, &mut l.legend_rows].into_iter().flatten() { + for r in [&mut l.values_rows, &mut l.categories_rows, &mut l.legend_rows, &mut l.axis_rows, &mut l.ring_rows].into_iter().flatten() { r.paint.fill.iter_mut().chain(r.paint.stroke.iter_mut()).for_each(|p| self.paint(p)); } } diff --git a/crates/engine/src/cmd/graph/series.rs b/crates/engine/src/cmd/graph/series.rs index 3f787513f4..c79f795b50 100644 --- a/crates/engine/src/cmd/graph/series.rs +++ b/crates/engine/src/cmd/graph/series.rs @@ -1492,7 +1492,11 @@ pub(crate) fn capture_line_changes(before: &Document, doc: &mut Document, ids: & let Some(spec) = doc.node_mut(graph).and_then(|n| n.extras_if_any().and_then(|e| e.graph.as_deref_mut())) else { continue }; let slot = line_slot(&mut spec.label_styles, line); let mut rows = slot.clone().unwrap_or_else(|| line.default_rows()); + let was_unpainted = stroke_unpainted(&rows); merge_row_edits(&mut rows, &old, &new); + if old.paint.stroke != new.paint.stroke { + keep_painted_stroke_weight(&mut rows, was_unpainted, &new.paint); + } let kept = (rows != line.default_rows()).then_some(rows); changed |= *slot != kept; *slot = kept; @@ -1547,9 +1551,10 @@ pub(crate) fn capture_line_row_writes(doc: &mut Document, written: &[(NodeId, us let Some(spec) = doc.node_mut(graph).and_then(|n| n.extras_if_any().and_then(|e| e.graph.as_deref_mut())) else { continue }; let slot = line_slot(&mut spec.label_styles, line); let mut rows = slot.clone().unwrap_or_else(|| line.default_rows()); + let was_unpainted = stroke_unpainted(&rows); let k = &mut rows.paint; if on_fill.paint { - k.fill = shown.fill; + k.fill = shown.fill.clone(); } if on_fill.opacity { k.fill_opacity = shown.fill_opacity; @@ -1558,7 +1563,7 @@ pub(crate) fn capture_line_row_writes(doc: &mut Document, written: &[(NodeId, us k.fill_blend = shown.fill_blend; } if on_stroke.paint { - k.stroke = shown.stroke; + k.stroke = shown.stroke.clone(); } if on_stroke.weight { k.stroke_width = shown.stroke_width; @@ -1569,6 +1574,9 @@ pub(crate) fn capture_line_row_writes(doc: &mut Document, written: &[(NodeId, us if on_stroke.blend { k.stroke_blend = shown.stroke_blend; } + if on_stroke.paint { + keep_painted_stroke_weight(&mut rows, was_unpainted, &shown); + } let kept = (rows != line.default_rows()).then_some(rows); changed |= *slot != kept; *slot = kept; @@ -1576,6 +1584,24 @@ pub(crate) fn capture_line_row_writes(doc: &mut Document, written: &[(NodeId, us changed } +/// Give kept line rows whose stroke an edit painted the painted line's stroke weight (`shown`) when the kind kept no +/// stroke paint before the edit (`was_unpainted`) and keeps the zero weight a stroke set to None left. A non-zero kept +/// weight stays, and so do a zero weight kept with a painted stroke and a zero weight the painted line shows. +fn keep_painted_stroke_weight(rows: &mut vectorcraft_doc::LabelRows, was_unpainted: bool, shown: &vectorcraft_doc::SeriesPaint) { + if was_unpainted + && !stroke_unpainted(rows) + && rows.paint.stroke_width.is_some_and(|w| w <= 0.0) + && let Some(w) = shown.stroke_width.filter(|w| *w > 0.0) + { + rows.paint.stroke_width = Some(w); + } +} + +/// Do kept line rows keep no stroke paint? +fn stroke_unpainted(rows: &vectorcraft_doc::LabelRows) -> bool { + rows.paint.stroke.as_ref().is_none_or(Paint::is_none) +} + /// Is `id` a graph line: a path directly in a graph's Axes group? pub(crate) fn is_graph_line(doc: &Document, id: NodeId) -> bool { graph_line(doc, id).is_some() diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index d7000d5c68..83c45ef498 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -7905,6 +7905,28 @@ fn recoloring_a_graph_keeps_its_axis_lines_paint() { assert!(!label_paint.is_empty() && label_paint.iter().all(|l| *l == ("#ffffff".to_string(), true)), "{label_paint:?}"); } +/// Swap Fill and Stroke on an axis line keeps the swapped paint for every axis line and leaves the fill opacity the axis +/// lines keep from another line, though the line swapped still shows 100%. +#[test] +fn swapping_an_axis_lines_paint_keeps_the_fill_opacity_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 lines = line_nodes(&s, id); + let (first, second) = (lines[0].id, lines[1].id); + s.execute("paint.setFill", &json!({"ids": [first.0], "color": "#00ff00"})).unwrap(); + s.execute("transparency.set", &json!({"ids": [first.0], "item": top_row(&s, first, true), "opacity": 40})).unwrap(); + let shown = s.doc().unwrap().doc.node(second).unwrap().appearance.fill().map(|f| f.opacity); + assert!(shown.is_none_or(|o| o == 1.0), "the other lines show 100% until the graph is drawn again: {shown:?}"); + s.execute("paint.swap", &json!({"ids": [second.0]})).unwrap(); + s.execute("graph.setData", &json!({"id": id.0, "csv": ",2024,2025\nQ1,3,2\nQ2,5,4\nQ3,-1,6"})).unwrap(); + for n in line_nodes(&s, id) { + let fill = n.appearance.fill().map(|f| (f.paint.color().map(|c| c.to_hex()), f.opacity)); + assert!(fill.as_ref().is_some_and(|(c, o)| c.as_deref() == Some("#000000") && (*o - 0.4).abs() < 1e-6), "{fill:?}"); + assert!(n.appearance.stroke_paint().is_none(), "the swapped line had no fill, so the lines have no stroke"); + } +} + /// A stroke given the whole graph, or its Axes group, is kept for its axis lines. #[test] fn a_stroke_given_the_whole_graph_or_its_axes_group_is_kept_for_its_axis_lines() { @@ -8047,6 +8069,56 @@ fn an_object_opacity_on_an_axis_line_keeps_the_stroke_its_kind_keeps() { } } +/// A stroke painted on an axis line after another axis line's stroke was set to None is kept for every axis line with +/// the painted line's weight, not the zero weight the unpainted stroke left on its kind. +#[test] +fn a_stroke_painted_after_an_axis_line_lost_its_stroke_keeps_a_weight() { + let cases = ["paint.setStroke", "addon.color.fillToStroke", "appearance.setItem"]; + every_case(cases, |cmd| { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let id = graph(&mut s, "column"); + let lines = line_nodes(&s, id); + let weight = line_look(&lines[1]).1; + assert!(weight > 0.0, "{cmd}: the line has a stroke weight: {weight}"); + s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "none": true})).unwrap(); + let b = lines[1].id.0; + match *cmd { + "paint.setStroke" => s.execute(cmd, &json!({"ids": [b], "color": "#ff0000"})).unwrap(), + "addon.color.fillToStroke" => { + s.execute("paint.setFill", &json!({"ids": [b], "color": "#ff0000"})).unwrap(); + s.execute(cmd, &json!({"ids": [b]})).unwrap() + } + _ => s.execute(cmd, &json!({"ids": [b], "index": top_row(&s, lines[1].id, false), "color": "#ff0000"})).unwrap(), + }; + let looks = redrawn_line_looks(&mut s, id); + assert!(looks.iter().all(|l| l.0 == "#ff0000" && (l.1 - weight).abs() < 1e-9), "{cmd}: {looks:?}"); + }); +} + +/// A zero stroke weight set together with a stroke color on an axis line is kept for every axis line as set, also when +/// a stroke is painted on another axis line before the graph is drawn again. +#[test] +fn a_zero_weight_set_with_a_stroke_color_on_an_axis_line_is_kept() { + every_case([false, true], |repaint| { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let id = graph(&mut s, "column"); + let lines = line_nodes(&s, id); + s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "none": true})).unwrap(); + let at = top_row(&s, lines[1].id, false); + s.execute("appearance.setItem", &json!({"ids": [lines[1].id.0], "index": at, "color": "#ff0000", "weight": 0})).unwrap(); + let color = if *repaint { + s.execute("paint.setStroke", &json!({"ids": [lines[2].id.0], "color": "#0000ff"})).unwrap(); + "#0000ff" + } else { + "#ff0000" + }; + let looks = redrawn_line_looks(&mut s, id); + assert!(looks.iter().all(|l| l.0 == color && l.1 == 0.0), "repaint {repaint}: {looks:?}"); + }); +} + /// A reorder that leaves an axis line's rows as they were keeps what its kind keeps from another line and is no edit. #[test] fn a_reorder_that_changes_nothing_on_an_axis_line_keeps_the_stroke_its_kind_keeps() { diff --git a/crates/engine/src/cmd/paint.rs b/crates/engine/src/cmd/paint.rs index e72b5b76fb..2421b71d77 100644 --- a/crates/engine/src/cmd/paint.rs +++ b/crates/engine/src/cmd/paint.rs @@ -39,7 +39,7 @@ pub fn specs() -> Vec { "Swap Fill and Stroke", [], Some("Shift+X"), - "{ids?|id?} swap the fill and stroke of the selection (type too) and of the defaults. On a path directly in a graph series, the graph also stores the swapped paints and the stroke weight, and on a graph label the swapped paints of its characters for every label of its kind, and on a graph's axis line, tick mark or radar ring its rows for every line of its kind (exceptions: docs/mcp.md)", + "{ids?|id?} swap the fill and stroke of the selection (type too) and of the defaults. On a path directly in a graph series, the graph also stores the swapped paints and the stroke weight, and on a graph label the swapped paints of its characters for every label of its kind, and on a graph's axis line, tick mark or radar ring what the swap changes on its rows for every line of its kind (exceptions: docs/mcp.md)", has_doc, swap ), diff --git a/crates/engine/src/tests/clipboard.rs b/crates/engine/src/tests/clipboard.rs index f041a7426c..ffb842bca8 100644 --- a/crates/engine/src/tests/clipboard.rs +++ b/crates/engine/src/tests/clipboard.rs @@ -468,7 +468,7 @@ fn styles_symbols_and_images_of_the_same_name_are_renamed() { assert_eq!(key(&s, img2), src_key); } -/// A graph whose series fill, value labels and legend rows paint with global swatch "Brand" and +/// A graph whose series fill, value labels, legend rows and axis and ring line rows paint with global swatch "Brand" and /// whose value labels are set in character style "Emphasis" and paragraph style "Body", as a /// later redraw would give them to its marks and labels. fn styled_graph(s: &mut Session) -> NodeId { @@ -484,8 +484,9 @@ fn styled_graph(s: &mut Session) -> NodeId { let l = &mut g.label_styles; l.values = Some(vectorcraft_doc::CharStyle { fill: brand.clone(), style_name: Some("Emphasis".into()), ..Default::default() }); l.values_para = Some(vectorcraft_doc::ParaStyle { style_name: Some("Body".into()), ..Default::default() }); - let rows = vectorcraft_doc::SeriesPaint { stroke: Some(brand), ..Default::default() }; - l.legend_rows = Some(vectorcraft_doc::graph::LabelRows { paint: rows, ..Default::default() }); + let paint = vectorcraft_doc::SeriesPaint { stroke: Some(brand), ..Default::default() }; + let rows = vectorcraft_doc::graph::LabelRows { paint, ..Default::default() }; + (l.legend_rows, l.axis_rows, l.ring_rows) = (Some(rows.clone()), Some(rows.clone()), Some(rows)); id } @@ -515,7 +516,7 @@ fn a_pasted_graph_brings_and_follows_its_label_styles_and_paints() { let d = doc(&s); assert!(d.swatch("Brand").is_some(), "{v}"); assert!(d.char_styles.iter().any(|x| x.name == "Emphasis") && d.para_styles.iter().any(|x| x.name == "Body"), "{v}"); - let brand = vec!["Brand".to_string(); 3]; + let brand = vec!["Brand".to_string(); 5]; assert_eq!(graph_links(&s, ids_of(&v)[0]), (brand.clone(), Some("Emphasis".into()), Some("Body".into()))); // Into a document that gives each name something else: they come in renamed. @@ -524,15 +525,15 @@ fn a_pasted_graph_brings_and_follows_its_label_styles_and_paints() { s.execute("charStyle.new", &json!({"name": "Emphasis", "attrs": {"size": 8.0}})).unwrap(); s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"space_after": 2.0}})).unwrap(); let v = s.execute("edit.pasteInPlace", &json!({"swatchConflict": "add"})).unwrap(); - let brand2 = vec!["Brand 2".to_string(); 3]; + let brand2 = vec!["Brand 2".to_string(); 5]; assert_eq!(graph_links(&s, ids_of(&v)[0]), (brand2, Some("Emphasis 2".into()), Some("Body 2".into())), "{v}"); // Without formatting: the labels' text styles stay behind. new_doc(&mut s); let v = s.execute("edit.pasteWithoutFormatting", &json!({})).unwrap(); assert!(doc(&s).char_styles.is_empty() && doc(&s).para_styles.is_empty(), "{v}"); - // The value labels' red characters stay behind too; the series fill and the legend rows keep Brand. - assert_eq!(graph_links(&s, ids_of(&v)[0]), (vec!["Brand".to_string(); 2], None, None)); + // The value labels' red characters stay behind too; the series fill, the legend rows and the line rows keep Brand. + assert_eq!(graph_links(&s, ids_of(&v)[0]), (vec!["Brand".to_string(); 4], None, None)); } /// Each label of graph `id` (the text of its Axes and Legend groups) with its size and its first paragraph's format. diff --git a/docs/mcp.md b/docs/mcp.md index 74ea95519a..d796deeb79 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -991,7 +991,9 @@ when the line still shows different ones until the graph is drawn again. `paint. `addon.color.fillToStroke` and `addon.color.strokeToFill` also store the paint they write on a line's topmost visible fill or stroke row when the line already showed it. `appearance.setItem` and an `appearance.copyFrom` of some attributes store the paint, stroke weight, and row opacity and blend mode they write to that row in the same way, and -`stroke.set` stores the weight it writes there. An `appearance.copyFrom` that does not apply +`stroke.set` stores the weight it writes there. When a kind keeps no stroke paint and a zero weight (a line's stroke was +set to None), a stroke painted on another line of that kind stores that line's weight for the kind; a zero weight set +together with a stroke color, or kept with a painted stroke, stays as set. 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 even when the line already showed them, and leaves the object opacity and blend mode the kind keeps. Rows