diff --git a/crates/engine/src/cmd/clipboard/resources.rs b/crates/engine/src/cmd/clipboard/resources.rs index 55e9b5061c..c20c763495 100644 --- a/crates/engine/src/cmd/clipboard/resources.rs +++ b/crates/engine/src/cmd/clipboard/resources.rs @@ -1849,15 +1849,17 @@ pub(crate) fn graph_design_library(src: &Document) -> Option { /// brushes and images their art uses, placed as Paste places them ([`Clipboard::import_into`]; a /// global or spot color whose name `d` gives another color is `d`'s). A design `d` has under the /// same name stays as it is, and the art of `src`'s design of that name is not followed. → the -/// names of the designs added: those of `names` `d` lacked, and those their art uses. An error, -/// with `d` unchanged, when `d` would hold more than [`GraphDesign::MAX`] designs. -pub(crate) fn import_graph_designs(src: &Document, d: &mut Document, names: &[String]) -> Result> { +/// names of the designs added ([`Imported::added_designs`]): those of `names` `d` lacked, and those +/// their art uses; with the symbols added and the designs `d` kept that their art draws with +/// ([`Imported::added_symbols`], [`Imported::kept_designs`]). An error, with `d` unchanged, when `d` +/// would hold more than [`GraphDesign::MAX`] designs. +pub(crate) fn import_graph_designs(src: &Document, d: &mut Document, names: &[String]) -> Result { let library = OnceCell::new(); let held = |n: &str| d.graph_designs.iter().any(|x| x.name == n); let followed: Vec = src.graph_designs.iter().filter(|g| names.contains(&g.name) || !held(&g.name)).cloned().collect(); let used = designs_used(&Defs { graph_designs: &followed, ..Defs::of(src, &library) }, names); let clip = Clipboard::with_used(src, &used, &library, vec![]); - Ok(clip.import_used(d, &used, &SwatchChoices::default(), false)?.added_designs) + clip.import_used(d, &used, &SwatchChoices::default(), false) } /// Swatch `w` in `d` ([`existing`]), else added under a free name (swatches and colour groups diff --git a/crates/engine/src/cmd/graph/designs.rs b/crates/engine/src/cmd/graph/designs.rs index d0807df3f1..f0d42808ab 100644 --- a/crates/engine/src/cmd/graph/designs.rs +++ b/crates/engine/src/cmd/graph/designs.rs @@ -60,10 +60,14 @@ pub(super) fn from_library(s: &Session, p: &Value, cmd: &str) -> Result Result<()> { - let added = import_graph_designs(lib, d, names)?; - if !added.is_empty() { - refresh_graphs(d, &added, &[], &BTreeSet::new())?; + let imported = import_graph_designs(lib, d, names)?; + if !imported.kept_designs.is_empty() { + redraw_symbols_with_designs(d, &imported.added_symbols, &imported.kept_designs); + } + if !imported.added_designs.is_empty() { + refresh_graphs(d, &imported.added_designs, &[], &BTreeSet::new())?; } Ok(()) } @@ -93,7 +97,9 @@ fn draws_with(g: &GraphSpec, name: &str) -> bool { let label = g.series.get(if scatter { s * 2 } else { s }).map_or("", String::as_str); g.legend && !g.series.is_empty() && s < entries && !super::labels::blank_label(label) }; - let shown = |s: usize| g.mark_points || in_legend(s); + // Point markers use the same range of designs: a scatter graph plots a pair of series for each. + let plotted = if scatter { nser.div_ceil(2) } else { nser }; + let shown = |s: usize| s < plotted && (g.mark_points || in_legend(s)); let marked = |s: usize| shown(s) && (matches!(g.kind, GraphKind::Scatter | GraphKind::Radar) || g.series_kind(s) == GraphKind::Line); let columnar = |s: usize| matches!(g.series_kind(s), GraphKind::Column | GraphKind::StackedColumn | GraphKind::Bar | GraphKind::StackedBar); g.series_markers.iter().enumerate().any(|(s, m)| m.as_deref() == Some(name) && marked(s)) diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index 0c6b3945b9..13394fc8d0 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -5817,6 +5817,73 @@ fn a_design_brought_in_from_a_library_redraws_the_graphs_in_symbols_that_name_it assert!(paints_with_swatch(series(&graphs[0], 0), "Gold"), "drawn with the library's design"); } +#[test] +fn a_design_brought_in_from_a_library_leaves_a_scatter_graph_that_names_it_for_a_slot_it_does_not_plot() { + let mut s = Session::new(); + let lib = load_coin_library(&mut s); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + // Four data columns plot two series, so marker slot 2 is never drawn. + let id = make(&mut s, "scatter", ",y1,x1,y2,x2\nP,1,10,2,20\nQ,3,11,4,22"); + s.execute("select.set", &json!({"ids": [id.0]})).unwrap(); + assert!(spec(&s, id).mark_points, "Mark Data Points is on"); + s.execute("graph.marker", &json!({"design": "Coin", "library": lib, "seriesIndexes": [2]})).unwrap(); + s.execute("graph.design", &json!({"delete": "Coin"})).unwrap(); + assert_eq!(spec(&s, id).series_markers.get(2).cloned().flatten().as_deref(), Some("Coin")); + // An edit of the graph's art that drawing the graph again would not keep. + let mark = series(s.doc().unwrap().doc.node(id).unwrap(), 0).children().unwrap()[0].id; + s.edit("Name", |d, _| { + if let Some(n) = d.node_mut(mark) { + n.name = Some("Mine".into()); + } + Ok(()) + }) + .unwrap(); + s.execute("graph.design", &json!({"library": lib, "add": "Coin"})).unwrap(); + let names: Vec> = series(s.doc().unwrap().doc.node(id).unwrap(), 0).children().unwrap().iter().map(|n| n.name.clone()).collect(); + assert_eq!(names[0].as_deref(), Some("Mine"), "not drawn again: {names:?}"); +} + +#[test] +fn a_symbol_a_library_design_brings_draws_its_graph_with_the_documents_design_it_keeps() { + // The library's design "A" holds an instance of symbol "Chart", whose graph draws columns with + // the library's "B": a rectangle and an ellipse. + let mut src = Session::new(); + src.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + save_design(&mut src, "B"); + let id = graph(&mut src, "column"); + src.execute("select.set", &json!({"ids": [id.0]})).unwrap(); + src.execute("graph.column", &json!({"design": "B"})).unwrap(); + let instance = src.execute("symbol.new", &json!({"name": "Chart", "ids": [id.0]})).unwrap()["id"].as_u64().unwrap(); + src.execute("select.set", &json!({"ids": [instance]})).unwrap(); + src.execute("graph.design", &json!({"new": "A"})).unwrap(); + let bytes = vectorcraft_format::save_file(&src.doc().unwrap().doc); + let mut s = Session::new(); + let p = json!({"dataBase64": vectorcraft_format::base64_encode(&bytes), "name": "Lib.vectorcraft"}); + let lib = s.execute("swatch.library.load", &p).unwrap()["library"].as_str().unwrap().to_string(); + // The document's own "B" is a single rectangle, and stays. + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let r = s.execute("shape.rectangle", &json!({"x": 600, "y": 600, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [r]})).unwrap(); + s.execute("graph.design", &json!({"new": "B"})).unwrap(); + s.execute("graph.design", &json!({"library": lib, "add": "A"})).unwrap(); + let d = &s.doc().unwrap().doc; + assert_eq!(d.graph_designs.len(), 2, "A came in and B stayed"); + let art = &d.symbols.iter().find(|x| x.name == "Chart").expect("the symbol came in").art; + let mut graphs = vec![]; + art.walk(&mut |m| { + if m.extras().graph.is_some() { + graphs.push(m.clone()); + } + }); + let paths = |n: &vectorcraft_doc::Node| { + let mut count = 0; + n.walk(&mut |m| count += usize::from(matches!(m.kind, NodeKind::Path { .. }))); + count + }; + let columns: Vec = series(&graphs[0], 0).children().unwrap().iter().map(|n| paths(n)).collect(); + assert_eq!(columns, [1, 1, 1, 1], "drawn with the document's B"); +} + #[test] fn a_design_brought_in_from_a_library_redraws_a_graph_in_an_opacity_mask() { let mut s = Session::new(); @@ -6325,6 +6392,16 @@ fn a_swatch_library_file_is_read_no_further_than_the_size_limit() { assert!(e.contains("parsed"), "{e}"); } +#[test] +fn reading_the_head_of_a_swatch_library_file_stops_at_the_limit() { + let dir = vectorcraft_testkit::TempDir::new("graph-design-library-head"); + let path = dir.join("long.gpl"); + std::fs::write(&path, vec![b'a'; 1 << 20]).unwrap(); + // The read the limit check relies on returns the bytes up to one past the limit, not the file. + let head = crate::cmd::fileio::read_file_head(&path.to_string_lossy(), 9).unwrap(); + assert_eq!(head.len(), 9); +} + #[test] fn a_library_design_brings_its_symbols_with_their_symbol_options() { // The source design is an instance of symbol "Corner" with 9-slice scaling, a top-left