mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 08:26:04 +00:00
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:
@@ -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 {
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user