mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:42:44 +00:00
Merge main into fix/graph-label-paint-recolor
This commit is contained in:
@@ -131,7 +131,7 @@ pub(crate) fn paint_targets(doc: &Document, ids: &[NodeId]) -> Vec<NodeId> {
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
for id in ids {
|
||||
// The series' own marks: the art of a marker design inside it keeps the design's paint (painting it would
|
||||
// The series' own marks: the art of a graph design inside it keeps the design's paint (painting it would
|
||||
// last only until the graph is drawn again), so edit the design to change it.
|
||||
match members.get(id) {
|
||||
Some(r) if r.group == *id => {
|
||||
@@ -139,7 +139,7 @@ pub(crate) fn paint_targets(doc: &Document, ids: &[NodeId]) -> Vec<NodeId> {
|
||||
out.extend(group.children().into_iter().flatten().filter(|m| matches!(m.kind, NodeKind::Path { .. })).map(|m| m.id));
|
||||
}
|
||||
}
|
||||
Some(r) if doc.parent_of(*id) != Some(r.group) => {}
|
||||
Some(r) if doc.parent_of(*id) != Some(r.group) || doc.node(*id).is_some_and(|n| !matches!(n.kind, NodeKind::Path { .. })) => {}
|
||||
_ => out.push(*id),
|
||||
}
|
||||
}
|
||||
@@ -157,18 +157,11 @@ pub(crate) fn series_group_marks(doc: &Document, ids: &[NodeId]) -> Vec<NodeId>
|
||||
|
||||
/// [`series_group_marks`] for the series groups `stands` accepts; the others pass through.
|
||||
pub(crate) fn series_group_marks_where(doc: &Document, ids: &[NodeId], stands: impl Fn(&Node) -> bool) -> Vec<NodeId> {
|
||||
// A series group is a child of a graph with one of the graph's series indexes ([`series_members`]). Each id is
|
||||
// looked up through its parent, with no pass over the document: the panels call this while they draw.
|
||||
let is_series_group = |n: &Node| {
|
||||
let Some(index) = n.extras().series_index.and_then(|i| usize::try_from(i).ok()) else { return false };
|
||||
let graph = doc.parent_of(n.id).and_then(|parent| doc.node(parent)).and_then(|parent| parent.extras().graph.as_deref());
|
||||
graph.is_some_and(|spec| index < series_count(spec))
|
||||
};
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut out = Vec::new();
|
||||
for id in ids {
|
||||
match doc.node(*id) {
|
||||
Some(group) if is_series_group(group) && stands(group) => {
|
||||
Some(group) if is_series_group(doc, group) && stands(group) => {
|
||||
let marks: Vec<NodeId> =
|
||||
group.children().into_iter().flatten().filter(|m| matches!(m.kind, NodeKind::Path { .. })).map(|m| m.id).collect();
|
||||
if marks.is_empty() { out.push(*id) } else { out.extend(marks) }
|
||||
@@ -180,6 +173,26 @@ pub(crate) fn series_group_marks_where(doc: &Document, ids: &[NodeId], stands: i
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether `n` is a graph series group: a child of a graph with one of the graph's series indexes ([`series_members`]).
|
||||
/// It is looked up through its parent, with no pass over the document: the panels call this while they draw.
|
||||
fn is_series_group(doc: &Document, n: &Node) -> bool {
|
||||
let Some(index) = n.extras().series_index.and_then(|i| usize::try_from(i).ok()) else { return false };
|
||||
let graph = doc.parent_of(n.id).and_then(|parent| doc.node(parent)).and_then(|parent| parent.extras().graph.as_deref());
|
||||
graph.is_some_and(|spec| index < series_count(spec))
|
||||
}
|
||||
|
||||
/// Whether `id` is the art of a graph design drawn in a graph series (Object › Graph › Column or Marker): an object
|
||||
/// inside a series group other than a path directly in it. The graph draws that art again from the design.
|
||||
pub(crate) fn is_design_art(doc: &Document, id: NodeId) -> bool {
|
||||
let Some(chain) = doc.ancestry(id) else { return false };
|
||||
let Some(at) = chain.iter().position(|a| doc.node(*a).is_some_and(|n| is_series_group(doc, n))) else { return false };
|
||||
match chain.len() - at {
|
||||
1 => false,
|
||||
2 => doc.node(id).is_some_and(|n| !matches!(n.kind, NodeKind::Path { .. })),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// The paint of the topmost visible fill (`true`) or stroke (`false`) of `node`, and a stroke's weight (0 without paint).
|
||||
/// With no such row: no paint.
|
||||
pub(crate) fn shown_paint(node: &Node, fill: bool) -> (Paint, f64) {
|
||||
|
||||
@@ -373,8 +373,8 @@ fn removing_a_series_marks_fill_forgets_its_kept_opacity() {
|
||||
assert_eq!(spec(&s, id).series_paints[0].fill_opacity, None);
|
||||
}
|
||||
|
||||
/// An opacity set on a row added to a graph design's copy inside a series is not kept for the series: the graph draws
|
||||
/// the design again from the design itself.
|
||||
/// A row or a row opacity is not given to a graph design's copy inside a series, and the graph keeps no opacity for the
|
||||
/// series: the graph draws the design again from the design itself.
|
||||
#[test]
|
||||
fn an_opacity_on_a_design_copy_in_a_series_is_not_kept() {
|
||||
let mut s = Session::new();
|
||||
@@ -384,11 +384,143 @@ fn an_opacity_on_a_design_copy_in_a_series_is_not_kept() {
|
||||
s.execute("graph.column", &json!({"id": id.0, "design": "Square"})).unwrap();
|
||||
let copy = series(s.doc().unwrap().doc.node(id).unwrap(), 0).children().unwrap()[0].clone();
|
||||
assert!(matches!(copy.kind, NodeKind::Group { .. }));
|
||||
s.execute("appearance.addFill", &json!({"ids": [copy.id.0]})).unwrap();
|
||||
s.execute("transparency.set", &json!({"ids": [copy.id.0], "item": 0, "opacity": 40})).unwrap();
|
||||
assert!(s.execute("appearance.addFill", &json!({"ids": [copy.id.0]})).is_err());
|
||||
assert!(s.execute("transparency.set", &json!({"ids": [copy.id.0], "item": 0, "opacity": 40})).is_err());
|
||||
assert!(spec(&s, id).series_paints.first().is_none_or(|p| p.fill_opacity.is_none()));
|
||||
}
|
||||
|
||||
/// A column graph whose series 0 draws the design "Square": the graph, series 0's first copy of the design, a path
|
||||
/// inside that copy, and the first mark of series 1, drawn plain.
|
||||
fn design_art() -> (Session, NodeId, NodeId, NodeId, NodeId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
save_design(&mut s, "Square");
|
||||
s.execute("graph.column", &json!({"id": id.0, "seriesIndexes": [0], "design": "Square"})).unwrap();
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
let copy = series(&n, 0).children().unwrap()[0].clone();
|
||||
assert!(matches!(copy.kind, NodeKind::Group { .. }));
|
||||
let mut art = vec![];
|
||||
copy.walk(&mut |m| {
|
||||
if matches!(m.kind, NodeKind::Path { .. }) {
|
||||
art.push(m.id);
|
||||
}
|
||||
});
|
||||
let mark = series(&n, 1).children().unwrap()[0].clone();
|
||||
assert!(matches!(mark.kind, NodeKind::Path { .. }));
|
||||
(s, id, copy.id, art[0], mark.id)
|
||||
}
|
||||
|
||||
/// `cmd` with `p` is refused as an edit of graph design art, and the document keeps its undo history.
|
||||
fn refused_on_design_art(s: &mut Session, cmd: &str, p: serde_json::Value) {
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
let e = s.execute(cmd, &p).unwrap_err().to_string();
|
||||
assert!(e.contains("drawn again from the design"), "{cmd}: {e}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps, "{cmd}");
|
||||
}
|
||||
|
||||
/// The commands that give rows, effects or graphic styles refuse the art of a graph design inside a series, given by
|
||||
/// id (the design's copy or a path inside it) or selected: the graph draws that art again from the design.
|
||||
#[test]
|
||||
fn appearance_commands_refuse_a_graph_designs_art_in_a_series() {
|
||||
let (mut s, _, copy, art, _) = design_art();
|
||||
let (rect, _) = blue_style(&mut s, "Blue", false);
|
||||
let graphic = s.execute("shape.rectangle", &json!({"x": 700, "y": 700, "width": 10, "height": 10})).unwrap()["id"].clone();
|
||||
s.execute("threeD.addGraphic", &json!({"name": "Mark", "ids": [graphic]})).unwrap();
|
||||
let fill = top_row(&s, art, true);
|
||||
for ids in [json!([copy.0]), json!([art.0])] {
|
||||
refused_on_design_art(
|
||||
&mut s,
|
||||
"graphicStyle.addFromLibrary",
|
||||
json!({"library": "painted-sketched", "name": "Wet Ink", "apply": true, "ids": ids}),
|
||||
);
|
||||
for cmd in ["appearance.addFill", "appearance.addStroke", "appearance.clear", "appearance.reduceToBasic", "appearance.showAllHidden"] {
|
||||
refused_on_design_art(&mut s, cmd, json!({"ids": ids}));
|
||||
}
|
||||
refused_on_design_art(&mut s, "appearance.moveItem", json!({"ids": ids, "from": "contents", "to": 0}));
|
||||
refused_on_design_art(&mut s, "effect.apply", json!({"effect": "stylize.dropShadow", "ids": ids}));
|
||||
refused_on_design_art(&mut s, "appearance.addEffect", json!({"effect": "stylize.dropShadow", "ids": ids}));
|
||||
refused_on_design_art(&mut s, "effect.apply", json!({"effect": "threeD.extrude", "item": null, "ids": ids}));
|
||||
refused_on_design_art(&mut s, "effect.move", json!({"ids": ids, "from": 0, "to": 0}));
|
||||
refused_on_design_art(&mut s, "effect.remove", json!({"ids": ids, "index": 0}));
|
||||
refused_on_design_art(&mut s, "threeD.mapGraphic", json!({"ids": ids, "symbol": "Mark"}));
|
||||
refused_on_design_art(&mut s, "graphicStyle.apply", json!({"name": "Blue", "ids": ids}));
|
||||
}
|
||||
for cmd in ["appearance.removeItem", "appearance.duplicateItem"] {
|
||||
refused_on_design_art(&mut s, cmd, json!({"ids": [art.0], "index": fill}));
|
||||
}
|
||||
refused_on_design_art(&mut s, "appearance.setItem", json!({"ids": [art.0], "index": fill, "color": "#00ff00"}));
|
||||
refused_on_design_art(&mut s, "appearance.moveItem", json!({"ids": [art.0], "from": fill, "to": 0}));
|
||||
refused_on_design_art(&mut s, "transparency.set", json!({"ids": [art.0], "item": fill, "opacity": 40}));
|
||||
refused_on_design_art(&mut s, "appearance.copyFrom", json!({"source": rect.0, "ids": [art.0]}));
|
||||
refused_on_design_art(&mut s, "appearance.transfer", json!({"source": rect.0, "target": art.0, "copy": true}));
|
||||
refused_on_design_art(&mut s, "appearance.transfer", json!({"source": art.0, "target": rect.0}));
|
||||
// Selected, and through the contents of the design's copy.
|
||||
s.execute("select.set", &json!({"ids": [art.0]})).unwrap();
|
||||
refused_on_design_art(&mut s, "appearance.addFill", json!({}));
|
||||
refused_on_design_art(&mut s, "graphicStyle.addFromLibrary", json!({"library": "painted-sketched", "name": "Wet Ink", "apply": true}));
|
||||
refused_on_design_art(&mut s, "appearance.addStroke", json!({"ids": [copy.0], "target": "contents"}));
|
||||
// A Pathfinder effect does not group the selected paths of the design's copy.
|
||||
let mut paths = vec![];
|
||||
s.doc().unwrap().doc.node(copy).unwrap().walk(&mut |m| {
|
||||
if matches!(m.kind, NodeKind::Path { .. }) {
|
||||
paths.push(m.id.0);
|
||||
}
|
||||
});
|
||||
assert_eq!(paths.len(), 2);
|
||||
s.execute("select.set", &json!({"ids": paths})).unwrap();
|
||||
refused_on_design_art(&mut s, "effect.apply", json!({"effect": "pathfinder.add"}));
|
||||
// Read as a source, the art gives its look.
|
||||
s.execute("appearance.transfer", &json!({"source": art.0, "target": rect.0, "copy": true})).unwrap();
|
||||
}
|
||||
|
||||
/// Given the art of a graph design in a series with other objects, the commands edit the other objects only.
|
||||
#[test]
|
||||
fn appearance_commands_edit_the_objects_given_with_a_graph_designs_art() {
|
||||
let (mut s, id, copy, art, mark) = design_art();
|
||||
let (rect, _) = blue_style(&mut s, "Blue", false);
|
||||
let rows = |s: &Session, id: NodeId| s.doc().unwrap().doc.node(id).unwrap().appearance.clone();
|
||||
let (copy_was, art_was, mark_was) = (rows(&s, copy), rows(&s, art), rows(&s, mark));
|
||||
s.execute("appearance.addFill", &json!({"ids": [copy.0, mark.0]})).unwrap();
|
||||
assert_eq!((rows(&s, copy), rows(&s, art)), (copy_was.clone(), art_was.clone()));
|
||||
assert_eq!(rows(&s, mark).items.len(), mark_was.items.len() + 1);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("effect.apply", &json!({"effect": "stylize.dropShadow", "ids": [art.0, mark.0]})).unwrap();
|
||||
assert_eq!(rows(&s, art), art_was);
|
||||
assert_eq!(rows(&s, mark).effects.len(), 1);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("appearance.copyFrom", &json!({"source": rect.0, "ids": [art.0, mark.0]})).unwrap();
|
||||
assert_eq!(rows(&s, art), art_was);
|
||||
assert_ne!(rows(&s, mark), mark_was);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
// The graph's contents: series 1's marks take the fill, the design's art does not.
|
||||
s.execute("appearance.addFill", &json!({"ids": [id.0], "target": "contents"})).unwrap();
|
||||
assert_eq!((rows(&s, copy), rows(&s, art)), (copy_was, art_was));
|
||||
assert_eq!(rows(&s, mark).items.len(), mark_was.items.len() + 1);
|
||||
}
|
||||
|
||||
/// The paint commands leave the art of a graph design in a series out, a copy of the design given by id with an item
|
||||
/// included.
|
||||
#[test]
|
||||
fn paint_targets_leave_a_graph_designs_copy_out() {
|
||||
let (s, _, copy, art, mark) = design_art();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert_eq!(super::paint_targets(d, &[copy, art, mark]), [mark]);
|
||||
}
|
||||
|
||||
/// The art of a marker design in a series is refused too.
|
||||
#[test]
|
||||
fn effect_commands_refuse_a_marker_designs_art() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
save_design(&mut s, "Pill");
|
||||
let id = graph(&mut s, "line");
|
||||
s.execute("graph.marker", &json!({"id": id.0, "seriesIndexes": [1], "design": "Pill"})).unwrap();
|
||||
let marker = series(s.doc().unwrap().doc.node(id).unwrap(), 1).children().unwrap()[1].clone();
|
||||
assert!(matches!(marker.kind, NodeKind::Group { .. }), "the design's art");
|
||||
refused_on_design_art(&mut s, "effect.apply", json!({"effect": "stylize.dropShadow", "ids": [marker.id.0]}));
|
||||
}
|
||||
|
||||
/// A series' kept opacity goes on its generated marks only: a column design made of one path keeps its own opacity.
|
||||
#[test]
|
||||
fn a_column_design_keeps_its_own_opacity_under_a_series_opacity() {
|
||||
@@ -1745,8 +1877,8 @@ fn a_paint_on_a_design_copy_or_the_other_kind_of_row_is_not_kept() {
|
||||
s.execute("graph.column", &json!({"id": id.0, "design": "Square"})).unwrap();
|
||||
let copy = series(s.doc().unwrap().doc.node(id).unwrap(), 0).children().unwrap()[0].clone();
|
||||
assert!(matches!(copy.kind, NodeKind::Group { .. }));
|
||||
s.execute("appearance.addFill", &json!({"ids": [copy.id.0]})).unwrap();
|
||||
s.execute("appearance.setItem", &json!({"ids": [copy.id.0], "index": 0, "color": "#0000ff"})).unwrap();
|
||||
assert!(s.execute("appearance.addFill", &json!({"ids": [copy.id.0]})).is_err());
|
||||
assert!(s.execute("appearance.setItem", &json!({"ids": [copy.id.0], "index": 0, "color": "#0000ff"})).is_err());
|
||||
assert!(spec(&s, id).series_paints.first().is_none_or(|p| p.fill.is_none()));
|
||||
// A path inside the design's copy is a member of the series too, and its paint is not kept either.
|
||||
let copy = series(s.doc().unwrap().doc.node(id).unwrap(), 0).children().unwrap()[0].clone();
|
||||
@@ -1757,7 +1889,7 @@ fn a_paint_on_a_design_copy_or_the_other_kind_of_row_is_not_kept() {
|
||||
}
|
||||
});
|
||||
let art = paths[0];
|
||||
s.execute("appearance.setItem", &json!({"ids": [art.0], "index": top_row(&s, art, true), "color": "#00ff00"})).unwrap();
|
||||
assert!(s.execute("appearance.setItem", &json!({"ids": [art.0], "index": top_row(&s, art, true), "color": "#00ff00"})).is_err());
|
||||
assert!(spec(&s, id).series_paints.first().is_none_or(|p| p.fill.is_none()));
|
||||
|
||||
let mut s = Session::new();
|
||||
@@ -6291,3 +6423,66 @@ fn graph_commands_refuse_params_of_the_wrong_type() {
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, null], [2, 3]], "series": null, "transpose": null})).unwrap();
|
||||
assert_eq!(spec(&s).cells(), vec![vec![Some(1.0), None], vec![Some(2.0), Some(3.0)]]);
|
||||
}
|
||||
|
||||
/// A `.ai` in PostScript form whose graph's records read whole (version 7 specification, appendix A)
|
||||
/// opens as a graph whose data can be edited: Graph Data redraws it on its plot. Saving it as `.ai`
|
||||
/// says its data and settings aren't written, in either form.
|
||||
#[test]
|
||||
fn a_graph_opened_from_ai_keeps_its_data_for_editing() {
|
||||
let graph = "%_Gs\n%_100 200 300 100 Gb\n%_5 0 0 14 0 1 0 0 6 44 Gy\n%_10 2 0 0 90 80 0 Gd\n\
|
||||
%_1 () Ga\n%_0 13 0 0 0 0 0 () GA\n%_2 () Ga\n%_0 14 0 0 0 0 0 () GA\n%_4 () Ga\n%_0 14 0 0 0 0 0 () GA\n\
|
||||
%_2 2 1 1 Gz\n%_() Gc (East) Gc (Q1) Gc 5 Gc GC\n\
|
||||
u\nu\n0.6 0 1 0 k\n110 100 m\n110 150 L\n130 150 L\n130 100 L\nf\nU\n%_2 1 0 0 Go\n\
|
||||
u\n0 0 0 1 K\n100 200 m\n100 100 L\n300 100 L\nS\nU\nU\n%_GS\n";
|
||||
let text = format!(
|
||||
"%!PS-Adobe-3.0\n%%Creator: a script\n%%BoundingBox: 0 0 400 400\n%AI5_FileFormat 3\n%AI3_Cropmarks: 0 0 400 400\n%%EndComments\n\
|
||||
%%BeginProlog\n%%EndProlog\n%%BeginSetup\n%%EndSetup\n%AI5_BeginLayer\n1 1 1 1 0 0 0 79 128 255 Lb\n(Graph) Ln\n0 A\n{graph}LB\n\
|
||||
%AI5_EndLayer--\n%%PageTrailer\n%%Trailer\n%%EOF\n"
|
||||
);
|
||||
let dir = vectorcraft_testkit::temp_dir("graph-ai-open");
|
||||
let path = dir.join("graph.ai");
|
||||
std::fs::write(&path, text).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("document.open", &json!({"path": path})).unwrap();
|
||||
let mut found = vec![];
|
||||
s.doc().unwrap().doc.walk(|n| {
|
||||
if n.extras().graph.is_some() {
|
||||
found.push(n.id);
|
||||
}
|
||||
});
|
||||
assert_eq!(found.len(), 1);
|
||||
let id = found[0];
|
||||
let before = spec(&s, id);
|
||||
assert_eq!(
|
||||
(before.series.as_slice(), before.categories.as_slice(), before.rows.as_slice()),
|
||||
(&["East".to_string()][..], &["Q1".to_string()][..], &[vec![5.0]][..])
|
||||
);
|
||||
// The fill of the series' first mark.
|
||||
let series_fill = |s: &Session| {
|
||||
let mut fill = None;
|
||||
s.doc().unwrap().doc.walk(|n| {
|
||||
if n.extras().series_index == Some(0) && fill.is_none() {
|
||||
let first = n.children().and_then(|c| c.first().cloned());
|
||||
fill = first.and_then(|m| m.appearance.items.iter().find(|i| i.is_fill()).map(|i| i.paint().clone()));
|
||||
}
|
||||
});
|
||||
fill
|
||||
};
|
||||
let painted = series_fill(&s);
|
||||
assert!(painted.is_some());
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": ",East\nQ1,8\nQ2,2"})).unwrap();
|
||||
let after = spec(&s, id);
|
||||
assert_eq!(after.rows, [[8.0], [2.0]]);
|
||||
assert_eq!(after.rect, before.rect, "the graph is redrawn on its plot");
|
||||
assert_eq!(series_fill(&s), painted, "the series keeps the file's paint");
|
||||
for (name, version) in [("v7.ai", Some("7")), ("pdf.ai", None)] {
|
||||
let mut p = json!({"path": dir.join(name), "format": "ai"});
|
||||
if let Some(v) = version {
|
||||
p["aiVersion"] = json!(v);
|
||||
}
|
||||
let r = s.execute("file.saveAs", &p).unwrap();
|
||||
let notes = r["warnings"].as_array().cloned().unwrap_or_default();
|
||||
assert!(notes.iter().any(|w| w.as_str().is_some_and(|w| w.starts_with("graphs are written as their art"))), "{name}: {r}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user