Graphs: a style added to a label or axis line keeps only the rows it shows

graphicStyle.apply with add stored both the fill and stroke rows a graph
label or line showed for its kind. A fill-only style added to one line
copied that line's old stroke over the stroke another line had set, and
the next redraw lost it. The kind now keeps only the kinds of row visible
in the style, with the other kind of row kept in its place relative to
the characters.
This commit is contained in:
dotCooCoo
2026-11-14 21:38:42 -08:00
parent 0b42788d6b
commit 021e4dc4ca
5 changed files with 127 additions and 14 deletions
+5 -5
View File
@@ -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)]
+38
View File
@@ -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 {
+76
View File
@@ -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<Option<String>> = 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::<Vec<_>>(), [(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]
+4 -6
View File
@@ -364,6 +364,9 @@ fn style_apply(s: &mut Session, p: &Value) -> Result<Value> {
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<Value> {
}
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(())