//! Brushes and symbols: commands, tools, rendering and persistence. use serde_json::{Value, json}; use vectorcraft_doc::{AngleProfile, Document, Node, NodeKind, PressureProfile}; use vectorcraft_geom::Rect; use vectorcraft_tools::{Mods, PenPose, PointerEvent, PointerKind}; use crate::*; fn session() -> Session { let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); s } fn rect(s: &mut Session, x: f64, y: f64, w: f64, h: f64) -> NodeId { let r = s.execute("shape.rectangle", &json!({"x": x, "y": y, "width": w, "height": h})).unwrap(); NodeId(r["id"].as_u64().unwrap()) } fn line(s: &mut Session) -> NodeId { let r = s.execute("shape.line", &json!({"x1": 50, "y1": 100, "x2": 250, "y2": 100})); match r { Ok(v) if v["id"].is_u64() => NodeId(v["id"].as_u64().unwrap()), _ => { let r = s.execute("path.freehand", &json!({"points": [[50, 100], [150, 100], [250, 100]]})).unwrap(); NodeId(r["id"].as_u64().unwrap()) } } } fn node(s: &Session, id: NodeId) -> Node { s.doc().unwrap().doc.node(id).cloned().unwrap() } fn brush_of(s: &Session, id: NodeId) -> Option { node(s, id).appearance.stroke().and_then(|st| st.brush.clone()) } fn select(s: &mut Session, ids: &[NodeId]) { let ids = ids.to_vec(); s.select(|_, sel| sel.set(ids)).unwrap(); } fn close(a: Rect, b: Rect, tol: f64) -> bool { (a.x0 - b.x0).abs() < tol && (a.y0 - b.y0).abs() < tol && (a.x1 - b.x1).abs() < tol && (a.y1 - b.y1).abs() < tol } fn bounds(s: &Session, id: NodeId) -> Rect { node(s, id).geometric_bounds().unwrap() } // ---------- brushes ---------- #[test] fn brush_list_has_the_default_library() { let mut s = session(); let l = s.execute("brush.list", &json!({})).unwrap(); let names: Vec<&str> = l["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap()).collect(); for n in ["3 pt. Round", "6 pt. Flat", "Charcoal", "Arrow", "Dots", "Chain", "Bristle Round"] { assert!(names.contains(&n), "{n} in {names:?}"); } let def = s.execute("brush.get", &json!({"name": "6 pt. Flat"})).unwrap(); assert_eq!(def["type"], "calligraphic"); assert_eq!(def["angle"], json!(45.0)); } #[test] fn apply_and_remove_brush_with_undo() { let mut s = session(); let a = rect(&mut s, 10.0, 10.0, 50.0, 50.0); s.execute("brush.apply", &json!({"name": "Charcoal"})).unwrap(); assert_eq!(brush_of(&s, a).as_deref(), Some("Charcoal")); assert_eq!(s.execute("brush.list", &json!({})).unwrap()["current"], "Charcoal"); s.execute("brush.remove", &json!({})).unwrap(); assert_eq!(brush_of(&s, a), None); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(brush_of(&s, a).as_deref(), Some("Charcoal")); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(brush_of(&s, a), None); assert!(s.execute("brush.apply", &json!({"name": "Nope"})).is_err()); } #[test] fn apply_adds_a_stroke_when_missing_and_reaches_into_groups() { let mut s = session(); let a = rect(&mut s, 10.0, 10.0, 50.0, 50.0); let b = rect(&mut s, 80.0, 10.0, 50.0, 50.0); s.execute("paint.setStroke", &json!({"color": null, "ids": [a.0]})).ok(); select(&mut s, &[a, b]); s.execute("object.group", &json!({})).unwrap(); s.execute("brush.apply", &json!({"name": "Dots"})).unwrap(); assert_eq!(brush_of(&s, a).as_deref(), Some("Dots")); assert_eq!(brush_of(&s, b).as_deref(), Some("Dots")); assert!(!node(&s, a).appearance.stroke_paint().is_none()); } #[test] fn new_brushes_from_params_and_selection() { let mut s = session(); let r = s.execute("brush.new", &json!({"type": "calligraphic", "name": "Nib", "params": {"angle": 20, "roundness": 30, "size": 8}})).unwrap(); assert_eq!(r["name"], "Nib"); assert_eq!(s.execute("brush.get", &json!({"name": "Nib"})).unwrap()["size"], json!(8.0)); // Art brushes need art. s.select(|_, sel| sel.clear()).unwrap(); assert!(s.execute("brush.new", &json!({"type": "art"})).is_err()); let a = rect(&mut s, 0.0, 0.0, 40.0, 8.0); let r = s.execute("brush.new", &json!({"type": "art", "name": "Bar"})).unwrap(); assert_eq!(r["name"], "Bar"); let r = s.execute("brush.new", &json!({"type": "pattern"})).unwrap(); assert_eq!(r["name"], "New Pattern Brush"); let def = s.execute("brush.get", &json!({"name": "New Pattern Brush"})).unwrap(); assert!(def["side"].is_object()); // The new art brush draws the rectangle along another path. let l = line(&mut s); s.execute("brush.apply", &json!({"name": "Bar", "ids": [l.0]})).unwrap(); let g = s.execute("object.expandBrush", &json!({"ids": [l.0]})).unwrap(); let gid = NodeId(g["ids"][0].as_u64().unwrap()); let b = bounds(&s, gid); assert!((b.width() - 200.0).abs() < 0.5 && (b.height() - 8.0).abs() < 0.5, "{b:?}"); let _ = a; // Undo removes the brush again; the library was not there before. assert!(s.execute("brush.new", &json!({"type": "nope"})).is_err()); } /// The brush of every stroke in symbol "Mark", graph design "Bar", pattern `pattern`, graphic style /// "Linked", the opacity mask in the layers and the art of brush "Holder", each as ": /// " ("-" for a stroke without one). fn brushes_outside_the_layers(s: &Session, pattern: &str) -> Vec { let d = &s.doc().unwrap().doc; let mut out = vec![]; let mut items = |place: &str, items: &[vectorcraft_doc::AppearanceItem]| { for it in items { if let vectorcraft_doc::AppearanceItem::Stroke(st) = it { out.push(format!("{place}: {}", st.brush.as_deref().unwrap_or("-"))); } } }; let mut tree = |place: &str, n: &Node| n.walk(&mut |m| items(place, &m.appearance.items)); tree("symbol", &d.symbols.iter().find(|x| x.name == "Mark").unwrap().art); tree("design", &d.graph_designs.iter().find(|x| x.name == "Bar").unwrap().art); d.pattern(pattern).unwrap().art.iter().for_each(|n| tree("pattern", n)); let mut masks = vec![]; d.walk(|n| masks.extend(n.extras().mask.as_deref().map(|m| m.art.clone()))); masks.iter().for_each(|n| tree("mask", n)); let holder = vectorcraft_brush::find(d, "Holder").unwrap(); holder.art().into_iter().for_each(|n| tree("brush art", n)); items("style", &d.graphic_styles.iter().find(|x| x.name == "Linked").unwrap().appearance.items); out } /// Renaming a brush in Brush Options and deleting it reach every stroke that paints with it, not /// only those in the layers: strokes in symbols, graph designs, pattern tiles, graphic styles, /// opacity masks and the art of other brushes are renamed, or become plain strokes, in the same /// undo step. #[test] fn brush_rename_and_delete_reach_strokes_outside_the_layers() { let mut s = session(); let brushed = |s: &mut Session| { let id = rect(s, 10.0, 10.0, 40.0, 20.0); s.execute("brush.apply", &json!({"name": "Charcoal", "ids": [id.0]})).unwrap(); id }; let clear = |s: &mut Session, id: NodeId| { if s.doc().unwrap().doc.node(id).is_some() { s.execute("edit.clear", &json!({"ids": [id.0]})).unwrap(); } }; brushed(&mut s); let sym = s.execute("symbol.new", &json!({"name": "Mark"})).unwrap(); clear(&mut s, NodeId(sym["id"].as_u64().unwrap())); let id = brushed(&mut s); s.execute("graph.design", &json!({"new": "Bar"})).unwrap(); clear(&mut s, id); let id = brushed(&mut s); let pattern = s.execute("object.pattern.make", &json!({"edit": false})).unwrap()["name"].as_str().unwrap().to_string(); clear(&mut s, id); let id = brushed(&mut s); s.execute("graphicStyle.new", &json!({"name": "Linked", "id": id.0})).unwrap(); clear(&mut s, id); let id = brushed(&mut s); s.execute("brush.new", &json!({"type": "art", "name": "Holder"})).unwrap(); clear(&mut s, id); let masked = rect(&mut s, 100.0, 100.0, 60.0, 60.0); let mask = brushed(&mut s); select(&mut s, &[masked, mask]); s.execute("transparency.makeOpacityMask", &json!({})).unwrap(); let a = brushed(&mut s); let plain = rect(&mut s, 200.0, 10.0, 40.0, 20.0); let shared = |s: &Session| s.doc().unwrap().doc.layers[0].children().unwrap().iter().find(|c| c.id == plain).unwrap().clone(); let plain_before = shared(&s); let before = brushes_outside_the_layers(&s, &pattern); for place in ["symbol", "design", "pattern", "mask", "brush art", "style"] { assert!(before.contains(&format!("{place}: Charcoal")), "{place} in {before:?}"); } let with = |to: &str| -> Vec { before.iter().map(|e| e.replace(": Charcoal", &format!(": {to}"))).collect() }; let undo = s.doc().unwrap().history.undo.len(); s.execute("brush.options", &json!({"name": "Charcoal", "newName": "Chalk"})).unwrap(); assert_eq!((brush_of(&s, a).as_deref(), brushes_outside_the_layers(&s, &pattern)), (Some("Chalk"), with("Chalk"))); assert!(std::sync::Arc::ptr_eq(&shared(&s), &plain_before), "an object without the brush is not copied"); assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!((brush_of(&s, a).as_deref(), brushes_outside_the_layers(&s, &pattern)), (Some("Charcoal"), before.clone())); s.execute("brush.delete", &json!({"name": "Charcoal"})).unwrap(); assert_eq!((brush_of(&s, a), brushes_outside_the_layers(&s, &pattern)), (None, with("-"))); assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1); // A brush made later with the deleted brush's name does not paint the strokes that used it. s.execute("brush.new", &json!({"type": "calligraphic", "name": "Charcoal"})).unwrap(); assert_eq!(brushes_outside_the_layers(&s, &pattern), with("-")); s.execute("edit.undo", &json!({})).unwrap(); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!((brush_of(&s, a).as_deref(), brushes_outside_the_layers(&s, &pattern)), (Some("Charcoal"), before)); } /// A brush renamed while a pattern is being edited keeps its new name in the tile that pattern /// editing's Cancel puts back. #[test] fn brush_rename_reaches_the_tile_that_pattern_cancel_puts_back() { let mut s = session(); let id = rect(&mut s, 10.0, 10.0, 40.0, 20.0); s.execute("brush.apply", &json!({"name": "Charcoal", "ids": [id.0]})).unwrap(); let pattern = s.execute("object.pattern.make", &json!({"edit": false})).unwrap()["name"].as_str().unwrap().to_string(); s.execute("object.pattern.edit", &json!({"name": pattern})).unwrap(); s.execute("brush.options", &json!({"name": "Charcoal", "newName": "Chalk"})).unwrap(); s.execute("object.pattern.cancel", &json!({})).unwrap(); let mut brushes = vec![]; for n in &s.doc().unwrap().doc.pattern(&pattern).unwrap().art { n.walk(&mut |m| brushes.extend(m.appearance.stroke().and_then(|st| st.brush.clone()))); } assert_eq!(brushes, ["Chalk"]); } /// `brush.selectionArt` gives the art `brush.new` takes: the selected object, a group of the selected /// objects, the object of `ids`, or null when nothing is selected. It changes nothing. #[test] fn selection_art_is_the_art_new_brush_takes() { let mut s = session(); let art = |s: &mut Session, p: Value| s.execute("brush.selectionArt", &p).unwrap()["art"].clone(); s.select(|_, sel| sel.clear()).unwrap(); assert_eq!(art(&mut s, json!({})), Value::Null); let a = rect(&mut s, 0.0, 0.0, 40.0, 8.0); let b = rect(&mut s, 0.0, 20.0, 40.0, 8.0); let steps = s.doc().unwrap().history.undo.len(); select(&mut s, &[a, b]); let group: Node = serde_json::from_value(art(&mut s, json!({}))).unwrap(); assert!(matches!(&group.kind, NodeKind::Group { children, .. } if children.len() == 2), "{group:?}"); let one: Node = serde_json::from_value(art(&mut s, json!({"ids": [a.0]}))).unwrap(); assert_eq!(one.geometric_bounds(), Some(bounds(&s, a))); // The art brush `brush.new` makes from the same selection holds the same art. s.execute("brush.new", &json!({"type": "art", "name": "Two"})).unwrap(); let made: Node = serde_json::from_value(s.execute("brush.get", &json!({"name": "Two"})).unwrap()["art"].clone()).unwrap(); assert_eq!(made.geometric_bounds(), group.geometric_bounds()); assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "only brush.new is an undo step"); } fn undo_steps(s: &Session) -> usize { s.doc().unwrap().history.undo.len() } fn selected(s: &Session) -> Vec { s.doc().unwrap().selection.objects.clone() } /// The JSON of object `id`, as `brush.options` params take art. fn art_json(s: &mut Session, id: NodeId) -> Value { s.execute("brush.selectionArt", &json!({"ids": [id.0]})).unwrap()["art"].clone() } /// `brush.placeArt` places a copy of an art or scatter brush's art centered where it is dropped /// (by default on the first artboard's center), selected, in one undo step; brushes without art, /// unknown brushes and a position that is not a number are refused. #[test] fn place_art_puts_a_copy_of_the_brush_art_where_it_is_dropped() { let mut s = session(); let r = rect(&mut s, 10.0, 20.0, 40.0, 8.0); s.execute("brush.new", &json!({"type": "art", "name": "Bar", "ids": [r.0]})).unwrap(); let steps = undo_steps(&s); let id = NodeId(s.execute("brush.placeArt", &json!({"name": "Bar", "x": 100, "y": 80})).unwrap()["id"].as_u64().unwrap()); assert_eq!(undo_steps(&s), steps + 1); assert_eq!(selected(&s), vec![id]); assert_ne!(id, r, "a copy, not the art the brush was made from"); assert!(close(bounds(&s, id), Rect::new(80.0, 76.0, 120.0, 84.0), 1e-6), "{:?}", bounds(&s, id)); // Without a position: the first artboard's center (the document is 400 × 300). let scatter = NodeId(s.execute("brush.placeArt", &json!({"name": "Dots"})).unwrap()["id"].as_u64().unwrap()); let c = bounds(&s, scatter).center(); assert!((c.x - 200.0).abs() < 1e-6 && (c.y - 150.0).abs() < 1e-6, "{c:?}"); let steps = undo_steps(&s); for (p, msg) in [ (json!({"name": "3 pt. Round"}), "`3 pt. Round` has no art (only scatter, art and pattern brushes do)"), (json!({"name": "Bristle Round"}), "`Bristle Round` has no art (only scatter, art and pattern brushes do)"), (json!({"name": "Nope"}), "no brush named `Nope`"), ] { let e = s.execute("brush.placeArt", &p).unwrap_err(); assert!(e.to_string().contains(msg), "{p}: {e}"); } assert!(s.execute("brush.placeArt", &json!({"name": "Bar", "x": "100"})).is_err()); assert_eq!(undo_steps(&s), steps, "a refused call adds no undo step"); } /// Brush art holding an instance of a symbol whose art lies away from the symbol's origin (as /// imported symbols can) is centered by that art, not by the box an instance has without its /// document. #[test] fn placed_art_with_a_symbol_instance_is_centered_by_the_symbols_art() { let mut s = session(); let r = rect(&mut s, 300.0, 10.0, 100.0, 40.0); let art = node(&s, r); s.execute("edit.clear", &json!({"ids": [r.0]})).unwrap(); let st = s.doc_mut().unwrap(); let d = std::sync::Arc::make_mut(&mut st.doc); d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Off".into(), art: std::sync::Arc::new(art) }); let layer = d.layers[0].id; let inst = d.alloc_id(); let xf = vectorcraft_geom::Affine::translate((50.0, 50.0)); d.insert(Some(layer), usize::MAX, Node::new(inst, NodeKind::SymbolInstance { symbol: "Off".into(), xf })).unwrap(); let inst = inst.0; s.execute("brush.new", &json!({"type": "art", "name": "Stamp", "ids": [inst]})).unwrap(); let id = NodeId(s.execute("brush.placeArt", &json!({"name": "Stamp", "x": 200, "y": 150})).unwrap()["id"].as_u64().unwrap()); let st = s.doc().unwrap(); let b = st.doc.bounds_of(&[id], false).unwrap(); assert!((b.center().x - 200.0).abs() < 1e-6 && (b.center().y - 150.0).abs() < 1e-6, "{b:?}"); assert!((b.width() - 100.0).abs() < 1e-6, "{b:?}"); } /// A pattern brush with side, outer corner and start tiles, the side tile with a repeat frame /// wider than its art: → its name. fn framed_pattern_brush(s: &mut Session) -> String { let side = rect(s, 0.0, 0.0, 20.0, 10.0); let corner = rect(s, 0.0, 40.0, 10.0, 10.0); let start = rect(s, 0.0, 80.0, 6.0, 10.0); let name = s.execute("brush.new", &json!({"type": "pattern", "name": "Rail", "ids": [side.0]})).unwrap()["name"].as_str().unwrap().to_string(); let params = json!({ "side": art_json(s, side), "side_frame": Rect::new(-2.0, 0.0, 22.0, 10.0), "outer_corner": art_json(s, corner), "start": art_json(s, start), }); s.execute("brush.options", &json!({"name": name, "params": params})).unwrap(); name } /// `brush.placeArt` lays a pattern brush's tiles out left to right, each in a group named for its /// tile, with an unpainted rectangle at the back of a tile with a repeat frame marking that frame. #[test] fn pattern_brushes_place_their_tiles_in_a_row() { let mut s = session(); let name = framed_pattern_brush(&mut s); let id = NodeId(s.execute("brush.placeArt", &json!({"name": name, "x": 200, "y": 150})).unwrap()["id"].as_u64().unwrap()); let placed = node(&s, id); let tiles: Vec = placed.children().unwrap().iter().map(|c| (**c).clone()).collect(); let names: Vec<&str> = tiles.iter().map(|t| t.name.as_deref().unwrap_or("")).collect(); assert_eq!(names, ["Side Tile", "Outer Corner Tile", "Start Tile"]); let boxes: Vec = tiles.iter().map(|t| t.geometric_bounds().unwrap()).collect(); assert!(boxes.windows(2).all(|w| (w[1].x0 - w[0].x1 - 10.0).abs() < 1e-6), "10 pt apart, left to right: {boxes:?}"); // The side tile: the frame's rectangle, unpainted, behind the art. let side = tiles[0].children().unwrap(); assert_eq!(side.len(), 2); let marker = &side[0]; assert!(marker.appearance.items.iter().all(|i| match i { vectorcraft_doc::AppearanceItem::Fill(f) => f.paint.is_none(), vectorcraft_doc::AppearanceItem::Stroke(st) => st.paint.is_none(), })); let frame = marker.geometric_bounds().unwrap(); assert!((frame.width() - 24.0).abs() < 1e-6 && (frame.height() - 10.0).abs() < 1e-6, "{frame:?}"); assert_eq!(tiles[1].children().unwrap().len(), 1, "no frame, no rectangle"); } /// `brush.replaceArt` reads the tiles `brush.placeArt` places back tile by tile, frames included: /// every tile and the frame keep their sizes, and the frame keeps its place around the art. #[test] fn placed_pattern_tiles_go_back_into_the_brush_tile_by_tile() { let mut s = session(); let name = framed_pattern_brush(&mut s); let before = s.execute("brush.get", &json!({"name": name})).unwrap(); let id = NodeId(s.execute("brush.placeArt", &json!({"name": name, "x": 200, "y": 150})).unwrap()["id"].as_u64().unwrap()); let steps = undo_steps(&s); s.execute("brush.replaceArt", &json!({"name": name, "ids": [id.0]})).unwrap(); assert_eq!(undo_steps(&s), steps + 1); let after = s.execute("brush.get", &json!({"name": name})).unwrap(); let size = |v: &Value| { let n: Node = serde_json::from_value(v.clone()).unwrap(); let b = n.geometric_bounds().unwrap(); (b.width(), b.height()) }; for key in ["side", "outer_corner", "start"] { let (a, b) = (size(&before[key]), size(&after[key])); assert!((a.0 - b.0).abs() < 1e-6 && (a.1 - b.1).abs() < 1e-6, "{key}: {a:?} → {b:?}"); } assert!(after.get("end").is_none_or(Value::is_null) && after.get("inner_corner").is_none_or(Value::is_null)); let frame: Rect = serde_json::from_value(after["side_frame"].clone()).unwrap(); let art: Node = serde_json::from_value(after["side"].clone()).unwrap(); let art = art.geometric_bounds().unwrap(); assert!((frame.width() - 24.0).abs() < 1e-6 && (art.x0 - frame.x0 - 2.0).abs() < 1e-6, "{frame:?} around {art:?}"); assert!(after["outer_corner_frame"].is_null() || after.get("outer_corner_frame").is_none()); } /// Art that is not the tiles `brush.placeArt` places becomes a pattern brush's side tile, and the /// other tiles stay; one tile group alone replaces only that tile. #[test] fn other_art_replaces_the_side_tile_of_a_pattern_brush() { let mut s = session(); let name = framed_pattern_brush(&mut s); let before = s.execute("brush.get", &json!({"name": name})).unwrap(); let r = rect(&mut s, 100.0, 100.0, 30.0, 12.0); s.execute("brush.replaceArt", &json!({"name": name, "ids": [r.0]})).unwrap(); let after = s.execute("brush.get", &json!({"name": name})).unwrap(); let side: Node = serde_json::from_value(after["side"].clone()).unwrap(); assert!(close(side.geometric_bounds().unwrap(), Rect::new(100.0, 100.0, 130.0, 112.0), 1e-6)); assert!(after.get("side_frame").is_none_or(Value::is_null), "the old frame goes with the old art"); assert_eq!((&after["outer_corner"], &after["start"]), (&before["outer_corner"], &before["start"])); // One placed tile, its group dragged back alone: only the start tile changes. let id = NodeId(s.execute("brush.placeArt", &json!({"name": name})).unwrap()["id"].as_u64().unwrap()); let start = node(&s, id).children().unwrap()[2].clone(); assert_eq!(start.name.as_deref(), Some("Start Tile")); s.execute("brush.replaceArt", &json!({"name": name, "ids": [start.id.0]})).unwrap(); let last = s.execute("brush.get", &json!({"name": name})).unwrap(); assert_eq!((&last["side"], &last["outer_corner"]), (&after["side"], &after["outer_corner"])); let art: Node = serde_json::from_value(last["start"].clone()).unwrap(); assert!(close(art.geometric_bounds().unwrap(), start.geometric_bounds().unwrap(), 1e-6), "the start tile is the placed one"); assert!(art.name.is_none(), "the tile's name stays on the canvas"); } /// `brush.replaceArt` gives a brush new art in one undo step: strokes that use it redraw with it, /// or with `applyToStrokes: false` keep the brush as it was while a copy with the new art becomes /// the current brush. A brush no stroke uses changes in place either way. #[test] fn replace_art_applies_to_strokes_or_leaves_them_on_the_old_brush() { let mut s = session(); let r = rect(&mut s, 10.0, 200.0, 40.0, 8.0); s.execute("brush.new", &json!({"type": "art", "name": "Bar", "ids": [r.0]})).unwrap(); let path = line(&mut s); s.execute("brush.apply", &json!({"name": "Bar", "ids": [path.0]})).unwrap(); let thin = brushed_bounds(&s, path).height(); // A path painted with a brush made from the taller art: what the stroke should draw. let tall = rect(&mut s, 10.0, 220.0, 40.0, 20.0); s.execute("brush.new", &json!({"type": "art", "name": "Tall", "ids": [tall.0]})).unwrap(); let reference = line(&mut s); s.execute("brush.apply", &json!({"name": "Tall", "ids": [reference.0]})).unwrap(); let want = brushed_bounds(&s, reference).height(); assert!(want > thin * 2.0, "{want} against {thin}"); // Apply to Strokes: the stroke redraws with the taller art. let steps = undo_steps(&s); let r = s.execute("brush.replaceArt", &json!({"name": "Bar", "ids": [tall.0]})).unwrap(); assert_eq!(r["name"], "Bar"); assert_eq!(undo_steps(&s), steps + 1); assert!((brushed_bounds(&s, path).height() - want).abs() < 1e-6); s.execute("edit.undo", &json!({})).unwrap(); assert!((brushed_bounds(&s, path).height() - thin).abs() < 1e-9, "undo puts the old art back"); s.execute("edit.redo", &json!({})).unwrap(); // Leave Strokes: the stroke keeps Bar as it was; Bar copy has the new art, after Bar, current. let short = rect(&mut s, 10.0, 260.0, 40.0, 4.0); let bar = s.execute("brush.get", &json!({"name": "Bar"})).unwrap(); let r = s.execute("brush.replaceArt", &json!({"name": "Bar", "ids": [short.0], "applyToStrokes": false})).unwrap(); assert_eq!(r["name"], "Bar copy"); assert_eq!(brush_of(&s, path).as_deref(), Some("Bar")); assert_eq!(s.execute("brush.get", &json!({"name": "Bar"})).unwrap(), bar); assert!((brushed_bounds(&s, path).height() - want).abs() < 1e-6); let copy: Node = serde_json::from_value(s.execute("brush.get", &json!({"name": "Bar copy"})).unwrap()["art"].clone()).unwrap(); assert!((copy.geometric_bounds().unwrap().height() - 4.0).abs() < 1e-6); let list = s.execute("brush.list", &json!({})).unwrap(); assert_eq!(list["current"], "Bar copy"); let names: Vec<&str> = list["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap()).collect(); let at = names.iter().position(|n| *n == "Bar").unwrap(); assert_eq!(names.get(at + 1), Some(&"Bar copy")); // No stroke uses Bar copy: its art changes in place, with or without applying. s.execute("brush.replaceArt", &json!({"name": "Bar copy", "ids": [tall.0], "applyToStrokes": false})).unwrap(); assert!(s.execute("brush.get", &json!({"name": "Bar copy 2"})).is_err()); } /// `brush.replaceArt` refuses a brush without art, an unknown brush, an empty selection, art with no /// width and an `applyToStrokes` that is not a boolean, and changes nothing then. #[test] fn replace_art_refuses_what_it_cannot_take() { let mut s = session(); let r = rect(&mut s, 10.0, 20.0, 40.0, 8.0); let flat = s.execute("shape.line", &json!({"x1": 50, "y1": 100, "x2": 50, "y2": 160})).unwrap()["id"].as_u64().unwrap(); let lib = s.execute("brush.list", &json!({})).unwrap(); let steps = undo_steps(&s); for (p, msg) in [ (json!({"name": "3 pt. Round", "ids": [r.0]}), "`3 pt. Round` has no art (only scatter, art and pattern brushes do)"), (json!({"name": "Bristle Round", "ids": [r.0]}), "`Bristle Round` has no art (only scatter, art and pattern brushes do)"), (json!({"name": "Nope", "ids": [r.0]}), "no brush named `Nope`"), (json!({"name": "Arrow", "ids": [flat]}), "the selected art has no width"), (json!({"name": "Chain", "ids": [flat]}), "the selected art has no width"), (json!({"name": "Arrow", "ids": [r.0], "applyToStrokes": "no"}), "`applyToStrokes` must be true or false"), ] { let e = s.execute("brush.replaceArt", &p).unwrap_err(); assert!(e.to_string().contains(msg), "{p}: {e}"); } s.select(|_, sel| sel.clear()).unwrap(); let e = s.execute("brush.replaceArt", &json!({"name": "Arrow"})).unwrap_err(); assert!(e.to_string().contains("nothing selected"), "{e}"); assert_eq!(undo_steps(&s), steps); assert_eq!(s.execute("brush.list", &json!({})).unwrap(), lib); } /// An instance of a symbol whose art has no width is art with no width for `brush.new` and /// `brush.replaceArt`, although an instance's box without its document has width. #[test] fn a_symbol_whose_art_has_no_width_makes_no_brush_art() { let mut s = session(); let flat = NodeId(s.execute("shape.line", &json!({"x1": 50, "y1": 100, "x2": 50, "y2": 160})).unwrap()["id"].as_u64().unwrap()); let art = node(&s, flat); s.execute("edit.clear", &json!({"ids": [flat.0]})).unwrap(); let st = s.doc_mut().unwrap(); let d = std::sync::Arc::make_mut(&mut st.doc); d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Flat".into(), art: std::sync::Arc::new(art) }); let layer = d.layers[0].id; let inst = d.alloc_id(); d.insert(Some(layer), usize::MAX, Node::new(inst, NodeKind::SymbolInstance { symbol: "Flat".into(), xf: vectorcraft_geom::Affine::IDENTITY })) .unwrap(); assert!(node(&s, inst).geometric_bounds().is_some_and(|b| b.width() > 1.0), "the box an instance has without its document"); for (cmd, p) in [("brush.new", json!({"type": "art", "ids": [inst.0]})), ("brush.replaceArt", json!({"name": "Arrow", "ids": [inst.0]}))] { let e = s.execute(cmd, &p).unwrap_err(); assert!(e.to_string().contains("the selected art has no width"), "{cmd}: {e}"); } } /// `brush.replaceArt` checks only the art it is given: a tile the pattern brush already has that /// has no width does not stop another tile from being replaced. #[test] fn replace_art_checks_only_the_tiles_it_is_given() { let mut s = session(); let flat = NodeId(s.execute("shape.line", &json!({"x1": 50, "y1": 100, "x2": 50, "y2": 160})).unwrap()["id"].as_u64().unwrap()); let end = art_json(&mut s, flat); s.execute("brush.options", &json!({"name": "Chain", "params": {"end": end}})).unwrap(); let r = rect(&mut s, 10.0, 20.0, 40.0, 8.0); s.execute("brush.replaceArt", &json!({"name": "Chain", "ids": [r.0]})).unwrap(); let side: Node = serde_json::from_value(s.execute("brush.get", &json!({"name": "Chain"})).unwrap()["side"].clone()).unwrap(); assert!(close(side.geometric_bounds().unwrap(), bounds(&s, r), 1e-6)); } /// Tile `key` (`side`, `start`…) of pattern brush `name`. fn tile_art(s: &mut Session, name: &str, key: &str) -> Node { serde_json::from_value(s.execute("brush.get", &json!({"name": name})).unwrap()[key].clone()).unwrap() } /// A pattern brush tile placed with `brush.placeArt` and given back with `brush.replaceArt` is the /// art it was, not that art inside the tile's group, however many times it goes out and back. #[test] fn pattern_tiles_go_out_and_back_without_gaining_a_group() { let mut s = session(); let name = framed_pattern_brush(&mut s); let size = |n: &Node| { let b = n.geometric_bounds().unwrap(); (b.width(), b.height()) }; let first = tile_art(&mut s, &name, "side"); assert!(matches!(first.kind, NodeKind::Path { .. }), "{:?}", first.kind); for round in 1..=2 { let id = s.execute("brush.placeArt", &json!({"name": name})).unwrap()["id"].as_u64().unwrap(); s.execute("brush.replaceArt", &json!({"name": name, "ids": [id]})).unwrap(); let side = tile_art(&mut s, &name, "side"); assert!(matches!(side.kind, NodeKind::Path { .. }), "round {round}: {:?}", side.kind); let (a, b) = (size(&first), size(&side)); assert!((a.0 - b.0).abs() < 1e-6 && (a.1 - b.1).abs() < 1e-6, "round {round}: {a:?} → {b:?}"); } } /// A tile group whose art was deleted, leaving only the rectangle that marks its frame, is art with /// no width: the brush keeps its tile. #[test] fn a_tile_group_holding_only_its_frame_is_refused() { let mut s = session(); let name = framed_pattern_brush(&mut s); let before = s.execute("brush.get", &json!({"name": name})).unwrap(); let id = NodeId(s.execute("brush.placeArt", &json!({"name": name})).unwrap()["id"].as_u64().unwrap()); let side = node(&s, id).children().unwrap()[0].clone(); assert_eq!(side.name.as_deref(), Some("Side Tile")); let art = side.children().unwrap()[1].id; s.execute("edit.clear", &json!({"ids": [art.0]})).unwrap(); let steps = undo_steps(&s); let e = s.execute("brush.replaceArt", &json!({"name": name, "ids": [side.id.0]})).unwrap_err(); assert!(e.to_string().contains("the selected art has no width"), "{e}"); assert_eq!(undo_steps(&s), steps); assert_eq!(s.execute("brush.get", &json!({"name": name})).unwrap(), before); } /// Two groups named for the same tile: the frontmost one is the tile's art. #[test] fn the_frontmost_group_named_for_a_tile_is_that_tile() { let mut s = session(); let name = framed_pattern_brush(&mut s); let back = rect(&mut s, 10.0, 10.0, 20.0, 10.0); let front = rect(&mut s, 10.0, 40.0, 30.0, 10.0); let (back, front) = (node(&s, back), node(&s, front)); s.execute("edit.clear", &json!({"ids": [back.id.0, front.id.0]})).unwrap(); let st = s.doc_mut().unwrap(); let d = std::sync::Arc::make_mut(&mut st.doc); let tile = |d: &mut Document, n: Node| { let mut g = Node::group(d.alloc_id(), vec![std::sync::Arc::new(n)]); g.name = Some("Start Tile".into()); std::sync::Arc::new(g) }; let tiles = vec![tile(d, back), tile(d, front)]; let both = Node::group(d.alloc_id(), tiles); let both_id = both.id; let layer = d.layers[0].id; d.insert(Some(layer), usize::MAX, both).unwrap(); s.execute("brush.replaceArt", &json!({"name": name, "ids": [both_id.0]})).unwrap(); let start = tile_art(&mut s, &name, "start"); assert!((start.geometric_bounds().unwrap().width() - 30.0).abs() < 1e-6, "{:?}", start.geometric_bounds()); } /// Type is art for `brush.replaceArt` as it is for `brush.new`: taken as it is. #[test] fn replace_art_takes_type_as_brush_new_does() { let mut s = session(); let t = s.execute("text.create", &json!({"x": 20, "y": 60, "text": "Hi", "size": 24})).unwrap()["id"].as_u64().unwrap(); let want = art_json(&mut s, NodeId(t)); s.execute("brush.replaceArt", &json!({"name": "Arrow", "ids": [t]})).unwrap(); assert_eq!(s.execute("brush.get", &json!({"name": "Arrow"})).unwrap()["art"], want); let made = s.execute("brush.new", &json!({"type": "art", "ids": [t]})).unwrap()["name"].as_str().unwrap().to_string(); assert_eq!(s.execute("brush.get", &json!({"name": made})).unwrap()["art"], want); } #[test] fn brush_options_rename_duplicate_and_delete_update_strokes() { let mut s = session(); let a = rect(&mut s, 10.0, 10.0, 50.0, 50.0); s.execute("brush.apply", &json!({"name": "3 pt. Round"})).unwrap(); s.execute("brush.options", &json!({"name": "3 pt. Round", "params": {"size": 5}, "newName": "5 pt. Round"})).unwrap(); assert_eq!(brush_of(&s, a).as_deref(), Some("5 pt. Round")); assert_eq!(s.execute("brush.get", &json!({"name": "5 pt. Round"})).unwrap()["size"], json!(5.0)); assert_eq!(s.execute("brush.list", &json!({})).unwrap()["current"], "5 pt. Round"); let d = s.execute("brush.duplicate", &json!({"name": "5 pt. Round"})).unwrap(); assert_eq!(d["name"], "5 pt. Round copy"); s.execute("brush.delete", &json!({"name": "5 pt. Round"})).unwrap(); assert_eq!(brush_of(&s, a), None); assert!(s.execute("brush.get", &json!({"name": "5 pt. Round"})).is_err()); assert!(s.execute("brush.delete", &json!({"name": "5 pt. Round"})).is_err()); } /// A document whose brushes repeat a name opens with the later brush numbered: strokes of the name /// keep the first brush, and renaming or deleting one brush leaves the other and its strokes alone. #[test] fn a_repeated_brush_name_is_numbered_when_the_document_opens() { use vectorcraft_brush::{Brush, BrushKind, Calligraphic}; let nib = |size: f64| Brush { name: "Nib".into(), kind: BrushKind::Calligraphic(Calligraphic { size, ..Default::default() }) }; let mut s = session(); vectorcraft_brush::store(std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc), &[nib(1.0), nib(2.0)]); let a = rect(&mut s, 10.0, 10.0, 50.0, 50.0); s.execute("brush.apply", &json!({"name": "Nib", "ids": [a.0]})).unwrap(); let saved = s.execute("document.serialize", &json!({})).unwrap()["dataBase64"].clone(); let mut o = Session::new(); o.execute("document.open", &json!({"name": "a.vectorcraft", "dataBase64": saved})).unwrap(); let names = |o: &mut Session| -> Vec { let list = o.execute("brush.list", &json!({})).unwrap(); list["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap().to_string()).collect() }; assert_eq!(names(&mut o), ["Nib", "Nib 2"]); assert_eq!(brush_of(&o, a).as_deref(), Some("Nib")); assert_eq!(o.execute("brush.get", &json!({"name": "Nib"})).unwrap()["size"], json!(1.0)); assert_eq!(o.execute("brush.get", &json!({"name": "Nib 2"})).unwrap()["size"], json!(2.0)); let b = rect(&mut o, 80.0, 10.0, 50.0, 50.0); o.execute("brush.apply", &json!({"name": "Nib 2", "ids": [b.0]})).unwrap(); o.execute("brush.options", &json!({"name": "Nib", "newName": "Pen"})).unwrap(); assert_eq!(names(&mut o), ["Pen", "Nib 2"]); assert_eq!((brush_of(&o, a).as_deref(), brush_of(&o, b).as_deref()), (Some("Pen"), Some("Nib 2"))); o.execute("brush.delete", &json!({"name": "Nib 2"})).unwrap(); assert_eq!(names(&mut o), ["Pen"]); assert_eq!((brush_of(&o, a).as_deref(), brush_of(&o, b)), (Some("Pen"), None)); } /// `brush.move`, which the Brushes panel runs when a brush is dragged, reorders brushes among those of /// their type in one undo step and refuses to move one into another type. #[test] fn brush_move_reorders_brushes_within_their_type() { let mut s = session(); let names = |s: &mut Session, ty: &str| -> Vec { let list = s.execute("brush.list", &json!({})).unwrap(); let of_type = list["brushes"].as_array().unwrap().iter().filter(|b| ty.is_empty() || b["type"] == ty); of_type.map(|b| b["name"].as_str().unwrap().to_string()).collect() }; let undo = |s: &Session| s.doc().unwrap().history.undo.len(); let before = names(&mut s, ""); assert_eq!(names(&mut s, "calligraphic"), ["3 pt. Round", "6 pt. Flat", "10 pt. Oval"]); let steps = undo(&s); // To index 0 of the calligraphic brushes: before the first; the other types stay where they are. let r = s.execute("brush.move", &json!({"name": "10 pt. Oval", "to": 0})).unwrap(); assert_eq!(r["moved"], json!(1)); assert_eq!(names(&mut s, "")[..3], ["10 pt. Oval", "3 pt. Round", "6 pt. Flat"]); assert_eq!(names(&mut s, "")[3..], before[3..]); assert_eq!(undo(&s), steps + 1); // Several go in the order given, as one step. s.execute("brush.move", &json!({"names": ["6 pt. Flat", "3 pt. Round"], "to": 0, "type": "calligraphic"})).unwrap(); assert_eq!(names(&mut s, "calligraphic"), ["6 pt. Flat", "3 pt. Round", "10 pt. Oval"]); assert_eq!(undo(&s), steps + 2); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(names(&mut s, "calligraphic"), ["10 pt. Oval", "3 pt. Round", "6 pt. Flat"]); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(names(&mut s, ""), before); // Into the scatter brushes, with a scatter brush, or with a `to` that isn't a whole number: an // error, and nothing moves. for (p, why) in [ (json!({"name": "3 pt. Round", "type": "scatter"}), "brushes move only within their type"), (json!({"names": ["3 pt. Round", "Dots"], "to": 0}), "brushes move only within their type"), (json!({"name": "3 pt. Round", "to": "1"}), "`to` must be a number, not \"1\""), (json!({"name": "3 pt. Round", "to": 1.5}), "`to` must be a whole number"), (json!({"name": "Nope", "to": 0}), "no brush named `Nope`"), ] { let r = s.execute("brush.move", &p); assert!(matches!(&r, Err(EngineError::BadParams { msg, .. }) if msg == why), "{p}: {r:?}"); } assert_eq!((names(&mut s, ""), undo(&s)), (before.clone(), steps)); // A move that keeps the order records no step. s.execute("brush.move", &json!({"name": "3 pt. Round", "to": 0})).unwrap(); assert_eq!((names(&mut s, ""), undo(&s)), (before.clone(), steps)); // Without `to`, or past the last one: after the last brush of the type, even one listed after // brushes of other types. s.execute("brush.new", &json!({"type": "calligraphic", "name": "Quill"})).unwrap(); s.execute("brush.move", &json!({"name": "3 pt. Round"})).unwrap(); assert_eq!(names(&mut s, "calligraphic"), ["6 pt. Flat", "10 pt. Oval", "Quill", "3 pt. Round"]); assert_eq!(names(&mut s, "").last().map(String::as_str), Some("3 pt. Round")); s.execute("brush.move", &json!({"name": "6 pt. Flat", "to": 99})).unwrap(); assert_eq!(names(&mut s, "calligraphic"), ["10 pt. Oval", "Quill", "3 pt. Round", "6 pt. Flat"]); s.execute("brush.move", &json!({"name": "Leaves", "to": 1})).unwrap(); assert_eq!(names(&mut s, "scatter"), ["Dots", "Leaves", "Confetti"]); } /// New Brush, Duplicate Brush and Brush Options trim the name they are given, as the Brush Options /// dialog does; the trimmed name is the one compared with the brushes there are, and a blank name /// gives the name the command gives when there is none. #[test] fn new_duplicate_and_renamed_brushes_trim_their_names() { let mut s = session(); let new = |s: &mut Session, name: &str| s.execute("brush.new", &json!({"type": "calligraphic", "name": name})).unwrap()["name"].clone(); assert_eq!(new(&mut s, " Nib \t"), "Nib"); assert_eq!(new(&mut s, " Nib"), "Nib 2"); assert_eq!(new(&mut s, " \t "), "New Calligraphic Brush"); assert_eq!(s.execute("brush.duplicate", &json!({"name": "Nib", "newName": " Quill\t"})).unwrap()["name"], "Quill"); assert_eq!(s.execute("brush.duplicate", &json!({"name": "Nib", "newName": " "})).unwrap()["name"], "Nib copy"); assert_eq!(s.execute("brush.options", &json!({"name": "Nib 2", "newName": " Pen "})).unwrap()["name"], "Pen"); assert_eq!(s.execute("brush.options", &json!({"name": "Pen", "newName": " "})).unwrap()["name"], "Pen"); let e = s.execute("brush.options", &json!({"name": "Pen", "newName": " Quill"})).unwrap_err().to_string(); assert!(e.contains("a brush named `Quill` exists"), "{e}"); // Duplicate Brush names the copy of a brush whose saved name has white space around it, as a // saved file can hold, from the trimmed name (" Old " gives "Old copy"). let mut lib = vectorcraft_brush::library(&s.doc().unwrap().doc); lib.iter_mut().filter(|b| b.name == "Pen").for_each(|b| b.name = " Old ".into()); vectorcraft_brush::store(std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc), &lib); assert_eq!(s.execute("brush.duplicate", &json!({"name": " Old "})).unwrap()["name"], "Old copy"); let names: Vec = vectorcraft_brush::library(&s.doc().unwrap().doc).into_iter().map(|b| b.name).collect(); assert_eq!(names[names.len() - 6..], ["Nib", " Old ", "New Calligraphic Brush", "Quill", "Nib copy", "Old copy"]); } /// New Brush and Bristle Brush Options take a bristle brush name of at most 31 characters, counted /// in Unicode characters (31 é's fit). A longer name that a document already holds is kept, and the /// other brush types take a name of any length. #[test] fn bristle_brush_names_have_at_most_31_characters() { let mut s = session(); let library = |s: &Session| vectorcraft_brush::library(&s.doc().unwrap().doc); let refused = |s: &mut Session, cmd: &str, p: Value| s.execute(cmd, &p).unwrap_err().to_string(); let (long, fits) = ("b".repeat(32), "é".repeat(31)); let e = refused(&mut s, "brush.new", json!({"type": "bristle", "name": long})); assert!(e.contains("a bristle brush name can have at most 31 characters: `bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb` has 32"), "{e}"); assert!(!library(&s).iter().any(|b| b.name == long)); assert_eq!(s.execute("brush.new", &json!({"type": "bristle", "name": fits})).unwrap()["name"], fits.as_str()); // A taken name is numbered, and the numbered name is the one counted. let e = refused(&mut s, "brush.new", json!({"type": "bristle", "name": fits})); assert!(e.contains(&format!("`{fits} 2` has 33")), "{e}"); let e = refused(&mut s, "brush.options", json!({"name": "Bristle Round", "newName": long})); assert!(e.contains("at most 31 characters"), "{e}"); let short = "c".repeat(31); assert_eq!(s.execute("brush.options", &json!({"name": "Bristle Round", "newName": short})).unwrap()["name"], short.as_str()); // Calligraphic brushes take a longer name. assert_eq!(s.execute("brush.new", &json!({"type": "calligraphic", "name": long})).unwrap()["name"], long.as_str()); let longer = format!("{long}x"); assert_eq!(s.execute("brush.options", &json!({"name": long, "newName": longer})).unwrap()["name"], longer.as_str()); // Bristle brushes that already have longer names, one of them with white space around it, as a // saved file can hold. let (old, spaced) = ("d".repeat(40), format!(" {} ", "e".repeat(40))); let mut lib = library(&s); lib.iter_mut().filter(|b| b.name == short).for_each(|b| b.name = old.clone()); lib.iter_mut().filter(|b| b.name == fits).for_each(|b| b.name = spaced.clone()); vectorcraft_brush::store(std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc), &lib); let l = line(&mut s); s.execute("brush.apply", &json!({"name": old, "ids": [l.0]})).unwrap(); // Brush Options without a new name, with the same name, or with the name only trimmed keep them. s.execute("brush.options", &json!({"name": old, "params": {"size": 9}})).unwrap(); assert_eq!(s.execute("brush.options", &json!({"name": old, "newName": old, "params": {"density": 20}})).unwrap()["name"], old.as_str()); assert_eq!(s.execute("brush.options", &json!({"name": spaced, "newName": spaced})).unwrap()["name"], spaced.trim()); assert_eq!(s.execute("brush.get", &json!({"name": old})).unwrap()["size"], json!(9.0)); assert_eq!(s.execute("brush.duplicate", &json!({"name": old})).unwrap()["name"], format!("{old} copy")); // Saved and opened again, the brush and the stroke painted with it keep the name. let back = vectorcraft_format::load(&vectorcraft_format::save(&s.doc().unwrap().doc, false)).unwrap(); assert!(vectorcraft_brush::find(&back, &old).is_some_and(|b| b.kind.type_id() == "bristle")); assert_eq!(back.node(l).unwrap().appearance.stroke().unwrap().brush.as_deref(), Some(old.as_str())); let mut s2 = Session::new(); s2.add_document(back, None); assert_eq!(s2.execute("brush.options", &json!({"name": old, "params": {"size": 4}})).unwrap()["name"], old.as_str()); } /// The Brushes panel's multi-selection: `names` deletes or duplicates several brushes in one undo /// step, and an unknown name changes nothing. #[test] fn brushes_delete_and_duplicate_by_names() { let mut s = session(); let a = rect(&mut s, 10.0, 10.0, 50.0, 50.0); s.execute("brush.apply", &json!({"name": "3 pt. Round"})).unwrap(); let b = rect(&mut s, 100.0, 10.0, 50.0, 50.0); s.execute("brush.apply", &json!({"name": "6 pt. Flat"})).unwrap(); let names = |s: &mut Session| -> Vec { let list = s.execute("brush.list", &json!({})).unwrap(); list["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap().to_string()).collect() }; let before = names(&mut s); let undo = s.doc().unwrap().history.undo.len(); // Copies of both, after the last brush. let d = s.execute("brush.duplicate", &json!({"names": ["3 pt. Round", "6 pt. Flat"]})).unwrap(); assert_eq!(d["names"], json!(["3 pt. Round copy", "6 pt. Flat copy"])); assert_eq!(names(&mut s)[before.len()..], ["3 pt. Round copy", "6 pt. Flat copy"]); assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(names(&mut s), before); // A name that isn't a brush: an error, and nothing is deleted. assert!(s.execute("brush.delete", &json!({"names": ["3 pt. Round", "Nope"]})).is_err()); assert!(s.execute("brush.duplicate", &json!({"names": ["3 pt. Round", "Nope"]})).is_err()); assert_eq!((names(&mut s), brush_of(&s, a).as_deref()), (before.clone(), Some("3 pt. Round"))); assert_eq!(s.doc().unwrap().history.undo.len(), undo); // Both go in one step; the strokes that used them become plain strokes. s.execute("brush.delete", &json!({"names": ["3 pt. Round", "6 pt. Flat"]})).unwrap(); assert_eq!((brush_of(&s, a), brush_of(&s, b)), (None, None)); assert!(!names(&mut s).iter().any(|n| n == "3 pt. Round" || n == "6 pt. Flat")); assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!((names(&mut s), brush_of(&s, b).as_deref()), (before, Some("6 pt. Flat"))); } fn unused(s: &mut Session) -> Vec { serde_json::from_value(s.execute("brush.unused", &json!({})).unwrap()["names"].clone()).unwrap() } /// Select All Unused: `brush.unused` lists the brushes no stroke paints with, in library order, /// without an undo step. #[test] fn brush_unused_lists_the_brushes_no_stroke_paints_with() { let mut s = session(); let mut lib = vectorcraft_brush::library(&s.doc().unwrap().doc); lib.retain(|b| ["3 pt. Round", "6 pt. Flat", "Charcoal"].contains(&b.name.as_str())); vectorcraft_brush::store(std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc), &lib); let a = rect(&mut s, 10.0, 10.0, 50.0, 50.0); s.execute("brush.apply", &json!({"name": "6 pt. Flat"})).unwrap(); let undo = s.doc().unwrap().history.undo.len(); assert_eq!(unused(&mut s), ["3 pt. Round", "Charcoal"]); assert_eq!(s.doc().unwrap().history.undo.len(), undo); s.execute("brush.remove", &json!({"ids": [a.0]})).unwrap(); assert_eq!(unused(&mut s), ["3 pt. Round", "6 pt. Flat", "Charcoal"]); } /// A stroke in the tile that pattern editing's Cancel puts back counts as a use, as `brush.delete` /// clears it there. #[test] fn brush_unused_counts_pattern_edit_originals() { let mut s = session(); let id = rect(&mut s, 10.0, 10.0, 40.0, 20.0); s.execute("brush.apply", &json!({"name": "6 pt. Flat", "ids": [id.0]})).unwrap(); let name = s.execute("object.pattern.make", &json!({"ids": [id.0], "edit": false})).unwrap()["name"].clone(); s.execute("brush.remove", &json!({"ids": [id.0]})).unwrap(); s.execute("object.pattern.edit", &json!({ "name": name })).unwrap(); // The tile's copies in the editing layer are selected; only the tile kept for Cancel paints with the brush. s.execute("brush.remove", &json!({})).unwrap(); let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); let original = d.pattern_edit.as_ref().and_then(|pe| pe.original.as_ref()).expect("pattern edit original"); assert_eq!(original.art.first().and_then(|n| n.appearance.stroke()).and_then(|k| k.brush.as_deref()), Some("6 pt. Flat")); for pattern in &mut d.patterns { for art in &mut pattern.art { if let Some(stroke) = std::sync::Arc::make_mut(art).appearance.stroke_mut() { stroke.brush = None; } } } assert!(!unused(&mut s).contains(&"6 pt. Flat".to_string())); } /// A brush counts as used when a stroke paints with it in an opacity mask, a symbol, a graph /// design, a pattern tile, a graphic style, or the art of a brush that is used. The art of a brush /// that is not used, its own strokes included, does not count. #[test] fn brush_unused_counts_strokes_in_masks_symbols_designs_patterns_styles_and_used_brush_art() { let mut s = session(); // Makes a rectangle painted with `brush` and leaves it selected; the test clears it once it // has been used. let brushed = |s: &mut Session, brush: &str| { let id = rect(s, 10.0, 10.0, 40.0, 20.0); s.execute("brush.apply", &json!({"name": brush, "ids": [id.0]})).unwrap(); id }; let clear = |s: &mut Session, id: NodeId| { s.execute("edit.clear", &json!({"ids": [id.0]})).unwrap(); }; brushed(&mut s, "Charcoal"); let sym = s.execute("symbol.new", &json!({"name": "Mark"})).unwrap(); clear(&mut s, NodeId(sym["id"].as_u64().unwrap())); let id2 = brushed(&mut s, "Dots"); s.execute("graph.design", &json!({"new": "Bar"})).unwrap(); clear(&mut s, id2); let id3 = brushed(&mut s, "Arrow"); s.execute("object.pattern.make", &json!({"edit": false})).unwrap(); if s.doc().unwrap().doc.node(id3).is_some() { clear(&mut s, id3); } let id4 = brushed(&mut s, "Chain"); s.execute("graphicStyle.new", &json!({"name": "Linked", "id": id4.0})).unwrap(); clear(&mut s, id4); let masked = rect(&mut s, 100.0, 100.0, 60.0, 60.0); let mask = brushed(&mut s, "6 pt. Flat"); select(&mut s, &[masked, mask]); s.execute("transparency.makeOpacityMask", &json!({})).unwrap(); // "Holder" is used, and its art paints with "10 pt. Oval"; "Idle" is not, and its art paints // with "Bristle Round". "Loop" paints its own art with itself and is not used anywhere else. let id5 = brushed(&mut s, "10 pt. Oval"); s.execute("brush.new", &json!({"type": "art", "name": "Holder"})).unwrap(); clear(&mut s, id5); let l = line(&mut s); s.execute("brush.apply", &json!({"name": "Holder", "ids": [l.0]})).unwrap(); let id6 = brushed(&mut s, "Bristle Round"); s.execute("brush.new", &json!({"type": "art", "name": "Idle"})).unwrap(); clear(&mut s, id6); let id7 = rect(&mut s, 10.0, 10.0, 40.0, 20.0); s.execute("brush.new", &json!({"type": "art", "name": "Loop"})).unwrap(); s.execute("brush.apply", &json!({"name": "Loop", "ids": [id7.0]})).unwrap(); let art = serde_json::to_value(node(&s, id7)).unwrap(); s.execute("brush.options", &json!({"name": "Loop", "params": {"art": art}})).unwrap(); clear(&mut s, id7); let left = unused(&mut s); for used in ["Charcoal", "Dots", "Arrow", "Chain", "6 pt. Flat", "Holder", "10 pt. Oval"] { assert!(!left.iter().any(|n| n == used), "{used} in {left:?}"); } for name in ["Idle", "Bristle Round", "Loop", "3 pt. Round"] { assert!(left.iter().any(|n| n == name), "{name} not in {left:?}"); } } #[test] fn expand_brush_replaces_path_with_art_group() { let mut s = session(); let a = rect(&mut s, 100.0, 100.0, 100.0, 100.0); s.execute("brush.apply", &json!({"name": "10 pt. Oval"})).unwrap(); let r = s.execute("object.expandBrush", &json!({})).unwrap(); let g = NodeId(r["ids"][0].as_u64().unwrap()); assert!(s.doc().unwrap().doc.node(a).is_none()); let gn = node(&s, g); assert!(matches!(gn.kind, NodeKind::Group { .. })); // White fill kept + the black nib outline. assert_eq!(gn.children().unwrap().len(), 2); let b = bounds(&s, g); assert!(b.width() > 104.0 && b.width() < 112.0, "{b:?}"); assert_eq!(s.doc().unwrap().selection.objects, vec![g]); assert!(s.execute("object.expandBrush", &json!({})).is_err(), "nothing brushed left"); } #[test] fn brush_freehand_paints_with_a_brush_in_one_step() { let mut s = session(); let n = s.doc().unwrap().history.undo.len(); s.begin_interaction("Paintbrush").unwrap(); s.preview("brush.freehand", &json!({"points": [[10, 10], [60, 40], [120, 10]], "style": "brush", "brush": "Charcoal"})).unwrap(); s.commit_interaction().unwrap(); assert_eq!(s.doc().unwrap().history.undo.len(), n + 1); let id = s.doc().unwrap().selection.objects[0]; assert_eq!(brush_of(&s, id).as_deref(), Some("Charcoal")); } /// `brush.freehand` refuses a `brush` that is not a string or names no brush in the document, /// and a `style` other than "pencil" or "brush", before it draws: the document, the selection /// and the undo history stay as they were. A `brush` left out or null draws the plain path. #[test] fn brush_freehand_refuses_a_brush_or_style_it_cannot_use() { let mut s = session(); let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0); select(&mut s, &[a]); let points = json!([[10, 10], [60, 40], [120, 10]]); for (extra, words) in [ (json!({"brush": "Nope"}), "no brush named `Nope`"), (json!({"brush": ""}), "no brush named ``"), (json!({"brush": 5}), "`brush` must be a string"), (json!({"brush": {"name": "Charcoal"}}), "`brush` must be a string"), (json!({"brush": true}), "`brush` must be a string"), (json!({"brush": "Charcoal", "style": "marker"}), "`style` must be \"pencil\" or \"brush\", not \"marker\""), (json!({"brush": "Charcoal", "style": 1}), "`style` must be \"pencil\" or \"brush\", not 1"), ] { let mut p = extra.clone(); p["points"] = points.clone(); let (before, undo) = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len()); let e = s.execute("brush.freehand", &p).expect_err(&format!("{extra} is refused")); assert!(e.to_string().contains(words), "{extra}: {e}"); assert!(*s.doc().unwrap().doc == *before, "{extra} leaves the document"); assert_eq!(s.doc().unwrap().selection.objects, [a], "{extra} leaves the selection"); assert_eq!(s.doc().unwrap().history.undo.len(), undo, "{extra} records no undo step"); } for extra in [json!({}), json!({"brush": null, "style": null})] { let mut p = extra.clone(); p["points"] = points.clone(); let id = NodeId(s.execute("brush.freehand", &p).unwrap()["id"].as_u64().unwrap()); assert_eq!(brush_of(&s, id), None, "{extra} draws the plain path"); } for style in ["pencil", "brush"] { let r = s.execute("brush.freehand", &json!({"points": points, "style": style, "brush": "Charcoal"})).unwrap(); assert!(r["id"].is_u64(), "{style}: {r}"); } } /// A `brush.freehand` call outside an interaction draws the path and paints it with the brush in /// one undo step: one Undo removes the path and gives back the earlier selection, and one Redo /// brings back the brushed path. Continuing a path with a brush is one step too. #[test] fn brush_freehand_direct_call_has_one_undo_step() { let mut s = session(); let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0); select(&mut s, &[a]); let before = s.doc().unwrap().doc.clone(); let undo = s.doc().unwrap().history.undo.len(); let r = s.execute("brush.freehand", &json!({"points": [[10, 10], [60, 40], [120, 10]], "style": "brush", "brush": "Charcoal"})).unwrap(); let id = NodeId(r["id"].as_u64().unwrap()); assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1, "one undo step"); assert_eq!(brush_of(&s, id).as_deref(), Some("Charcoal")); s.execute("edit.undo", &json!({})).unwrap(); assert!(*s.doc().unwrap().doc == *before, "one Undo removes the brushed path"); assert_eq!(selected(&s), [a], "Undo gives back the earlier selection"); s.execute("edit.redo", &json!({})).unwrap(); assert_eq!(brush_of(&s, id).as_deref(), Some("Charcoal"), "Redo brings back the brush"); assert_eq!(selected(&s), [id]); let undo = s.doc().unwrap().history.undo.len(); let p = json!({"points": [[120, 10], [160, 60], [200, 10]], "extend": {"id": id.0, "end": "end"}, "brush": "Dots"}); assert_eq!(s.execute("brush.freehand", &p).unwrap()["id"], json!(id.0)); assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1, "continuing is one undo step"); assert_eq!(brush_of(&s, id).as_deref(), Some("Dots")); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(brush_of(&s, id).as_deref(), Some("Charcoal"), "one Undo takes back the stroke and its brush"); let undo = s.doc().unwrap().history.undo.len(); s.execute("brush.freehand", &json!({"points": [[10, 100], [60, 140], [120, 100]], "style": "pencil", "brush": "Charcoal"})).unwrap(); assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1); assert_eq!(s.doc().unwrap().history.undo.last().map(|e| e.label.as_str()), Some("Paintbrush"), "a stroke with a brush is a Paintbrush step"); } /// A `brush.freehand` call that continues a path by one point changes only the path's brush, and /// applies `keepSelected` as a longer continuation does: off, the path is deselected while the /// rest of the selection stays; on, the path is selected. A call that leaves the brush as it is /// changes neither the selection nor the undo history. #[test] fn brush_freehand_continued_by_one_point_follows_keep_selected() { let mut s = session(); let r = s.execute("brush.freehand", &json!({"points": [[10, 10], [60, 40], [120, 10]], "style": "brush", "brush": "Charcoal"})).unwrap(); let p = NodeId(r["id"].as_u64().unwrap()); let a = rect(&mut s, 200.0, 200.0, 10.0, 10.0); let extend = json!({"id": p.0, "end": "end"}); select(&mut s, &[p, a]); let off = json!({"points": [[120, 10]], "extend": extend, "brush": "Dots", "keepSelected": false}); assert_eq!(s.execute("brush.freehand", &off).unwrap()["id"], json!(p.0)); assert_eq!(brush_of(&s, p).as_deref(), Some("Dots")); assert_eq!(selected(&s), [a], "Keep selected off: the path is deselected and the rest stays"); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(brush_of(&s, p).as_deref(), Some("Charcoal"), "one Undo takes back the brush"); assert_eq!(selected(&s), [p, a], "Undo gives back the earlier selection"); select(&mut s, &[a]); let on = json!({"points": [[120, 10]], "extend": extend, "brush": "Dots"}); s.execute("brush.freehand", &on).unwrap(); assert_eq!(brush_of(&s, p).as_deref(), Some("Dots")); assert_eq!(selected(&s), [p], "Keep selected on: the continued path is selected"); select(&mut s, &[a]); let undo = s.doc().unwrap().history.undo.len(); s.execute("brush.freehand", &on).unwrap(); assert_eq!(selected(&s), [a], "the same brush again: the selection stays"); assert_eq!(s.doc().unwrap().history.undo.len(), undo, "the same brush again: no undo step"); } /// `brush.freehand` refuses a brush for a path with no stroke (a continued path, a redrawn one /// and a new filled pencil path while the stroke is None) instead of reporting it painted: the /// document, the selection and the undo history stay as they were. Without a brush the path is /// drawn as `path.freehand` draws it. #[test] fn brush_freehand_refuses_a_brush_on_a_path_without_a_stroke() { let mut s = session(); s.execute("select.none", &json!({})).unwrap(); s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap(); s.execute("paint.setStroke", &json!({"none": true})).unwrap(); let drawn = json!([[10, 10], [60, 20], [90, 80], [20, 90]]); let p = NodeId(s.execute("path.freehand", &json!({"points": drawn, "fill": true})).unwrap()["id"].as_u64().unwrap()); assert!(node(&s, p).appearance.stroke_paint().is_none(), "the path has no stroke"); // The redraw below replaces a section of the path rather than drawing a new one. let redraw = json!({"points": [[10, 10], [40, 30], [60, 20]], "redraw": {"id": p.0}}); assert_eq!(s.execute("path.freehand", &redraw).unwrap()["id"], json!(p.0)); s.execute("edit.undo", &json!({})).unwrap(); select(&mut s, &[p]); let extend = json!({"id": p.0, "end": "end"}); for (what, mut params) in [ ("continued", json!({"points": [[20, 90], [0, 120], [-10, 150]], "extend": extend})), ("continued by one point", json!({"points": [[20, 90]], "extend": extend})), ("redrawn", redraw.clone()), ("new filled pencil", json!({"points": [[100, 100], [150, 120], [180, 180]], "style": "pencil", "fill": true})), ] { params["brush"] = json!("Charcoal"); let (before, undo) = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len()); let e = s.execute("brush.freehand", ¶ms).expect_err(&format!("{what}: refused")); assert_eq!(e.to_string(), "invalid parameters for `brush.freehand`: cannot apply brush `Charcoal`: path has no stroke", "{what}"); assert!(*s.doc().unwrap().doc == *before, "{what}: the document stays"); assert_eq!(selected(&s), [p], "{what}: the selection stays"); assert_eq!(s.doc().unwrap().history.undo.len(), undo, "{what}: no undo step"); } let r = s.execute("brush.freehand", &json!({"points": [[20, 90], [0, 120], [-10, 150]], "extend": extend, "brush": null})).unwrap(); assert_eq!(r["id"], json!(p.0)); assert_eq!(brush_of(&s, p), None, "without a brush the path is continued as it is"); } /// `brush.freehand` takes the keys of `path.freehand` and `brush`, and refuses any other key with /// the nearest key it takes, before it draws. #[test] fn brush_freehand_refuses_unknown_keys() { let spec = crate::cmd::find_command("brush.freehand").unwrap(); let mut keys = crate::cmd::params::accepted_keys(spec.params).expect("the description names every key"); keys.sort_unstable(); assert_eq!(keys, ["brush", "closed", "connect", "extend", "fidelity", "fill", "fit", "keepSelected", "points", "redraw", "style"]); let mut s = session(); let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0); select(&mut s, &[a]); let points = json!([[10, 10], [60, 40], [120, 10]]); let (before, undo) = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len()); let e = s.execute("brush.freehand", &json!({"points": points, "brsuh": "Charcoal"})).expect_err("a misspelled key is refused"); assert!(e.to_string().contains("unknown parameter `brsuh`; did you mean `brush`?"), "{e}"); assert!(*s.doc().unwrap().doc == *before, "the document stays"); assert_eq!(selected(&s), [a], "the selection stays"); assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step"); let every = json!({ "points": points, "fidelity": 2.0, "fit": true, "closed": false, "style": "brush", "fill": false, "keepSelected": true, "extend": null, "connect": null, "redraw": null, "brush": "Charcoal" }); let id = NodeId(s.execute("brush.freehand", &every).unwrap()["id"].as_u64().unwrap()); assert_eq!(brush_of(&s, id).as_deref(), Some("Charcoal")); } /// The Paintbrush skips a brush deleted after it was chosen: a deleted tool brush gives way to /// the document's current brush, and with neither the stroke is drawn without a brush. #[test] fn paintbrush_tool_skips_a_deleted_brush() { let mut s = session(); s.execute("brush.setCurrent", &json!({"name": "Arrow"})).unwrap(); let v = ViewInfo::default(); s.select_tool("paintbrush", v).unwrap(); s.set_tool_option("brush", &json!("Dots")); s.execute("brush.delete", &json!({"name": "Dots"})).unwrap(); let stroke = |s: &mut Session, y: f64| { for (k, x) in [(PointerKind::Down, 10.0), (PointerKind::Drag, 50.0), (PointerKind::Drag, 90.0), (PointerKind::Up, 130.0)] { s.pointer(&PointerEvent::new(k, x, y), v).unwrap(); } s.doc().unwrap().selection.objects[0] }; let id = stroke(&mut s, 10.0); assert_eq!(brush_of(&s, id).as_deref(), Some("Arrow")); s.execute("brush.delete", &json!({"name": "Arrow"})).unwrap(); assert_eq!(s.doc().unwrap().doc.unknown.get("currentBrush"), None); let id = stroke(&mut s, 110.0); assert_eq!(brush_of(&s, id), None); } #[test] fn paintbrush_tool_uses_the_current_brush() { let mut s = session(); s.execute("brush.setCurrent", &json!({"name": "Arrow"})).unwrap(); let v = ViewInfo::default(); s.select_tool("paintbrush", v).unwrap(); for (k, x, y) in [(PointerKind::Down, 10.0, 10.0), (PointerKind::Drag, 50.0, 30.0), (PointerKind::Drag, 90.0, 20.0), (PointerKind::Up, 130.0, 10.0)] { s.pointer(&PointerEvent::new(k, x, y), v).unwrap(); } let id = s.doc().unwrap().selection.objects[0]; assert_eq!(brush_of(&s, id).as_deref(), Some("Arrow")); // A tool option overrides the document's current brush. s.set_tool_option("brush", &json!("Dots")); for (k, x, y) in [(PointerKind::Down, 10.0, 110.0), (PointerKind::Drag, 50.0, 130.0), (PointerKind::Up, 130.0, 110.0)] { s.pointer(&PointerEvent::new(k, x, y), v).unwrap(); } let id = s.doc().unwrap().selection.objects[0]; assert_eq!(brush_of(&s, id).as_deref(), Some("Dots")); assert!(s.execute("brush.setCurrent", &json!({"name": "Nope"})).is_err()); } /// A Size: Pressure variant of "3 pt. Round" (3 ± 3 pt). fn pressure_round(s: &mut Session) { let params = json!({"variation": [0, 0, 3], "modes": ["fixed", "fixed", "pressure"]}); s.execute("brush.options", &json!({"name": "3 pt. Round", "params": params})).unwrap(); } fn pressure_of(s: &Session, id: NodeId) -> Option { node(s, id).appearance.stroke().and_then(|st| st.pressure.clone()) } /// #852: the Paintbrush records the pen pressure on the stroke, the brush draws it, and it lasts /// through saving and every export; a mouse stroke records none. #[test] fn paintbrush_records_pen_pressure_on_the_stroke() { let mut s = session(); pressure_round(&mut s); s.execute("brush.setCurrent", &json!({"name": "3 pt. Round"})).unwrap(); let v = ViewInfo::default(); s.select_tool("paintbrush", v).unwrap(); let strokes = |s: &mut Session, y: f64, pressure: [f32; 4]| { let kinds = [PointerKind::Down, PointerKind::Drag, PointerKind::Drag, PointerKind::Up]; for ((k, x), p) in kinds.into_iter().zip([20.0, 120.0, 220.0, 320.0]).zip(pressure) { s.pointer(&PointerEvent { pressure: p, ..PointerEvent::new(k, x, y) }, v).unwrap(); } let id = s.doc().unwrap().selection.objects[0]; // Deselected, so the next stroke starts a path of its own. select(s, &[]); id }; let pen = strokes(&mut s, 60.0, [0.0, 0.4, 0.8, 1.0]); let p = pressure_of(&s, pen).expect("pressure recorded"); assert!(p.at(0.0) < 0.05 && p.at(1.0) > 0.95 && (p.at(0.5) - 0.6).abs() < 0.05, "{p:?}"); let mouse = strokes(&mut s, 200.0, [1.0; 4]); assert_eq!(pressure_of(&s, mouse), None, "a mouse presses fully: nothing to record"); // Drawn: thin where the pen pressed lightly, 6 pt where it pressed fully. let doc: Document = (*s.doc().unwrap().doc).clone(); let art = |d: &Document, id: NodeId| vectorcraft_brush::node_pieces(d, d.node(id).unwrap()).unwrap(); let heavy_end = vectorcraft_brush::pieces_bounds(&art(&doc, pen)).unwrap(); assert!((heavy_end.height() - 6.0).abs() < 0.3, "{heavy_end:?}"); let even = vectorcraft_brush::pieces_bounds(&art(&doc, mouse)).unwrap(); assert!((even.height() - 3.0).abs() < 0.1, "no pressure draws the size itself: {even:?}"); // Saved and reopened as it was. let back = vectorcraft_format::load(&vectorcraft_format::save(&doc, false)).unwrap(); assert_eq!(back.node(pen).unwrap().appearance.stroke().unwrap().pressure, Some(p)); // SVG and PDF draw the brush through the same stroke code: without the pressure they differ. let mut flat = doc.clone(); flat.node_mut(pen).unwrap().appearance.stroke_mut().unwrap().pressure = None; assert_ne!(vectorcraft_svg::export(&doc, &Default::default()), vectorcraft_svg::export(&flat, &Default::default())); let pdf = |d: &Document| { vectorcraft_pdf::export(d, &vectorcraft_pdf::PdfOptions { created: Some(0), ..vectorcraft_pdf::PdfOptions::uncompressed() }).unwrap() }; assert_ne!(pdf(&doc), pdf(&flat)); } /// Continuing a pressure stroke keeps its pressure where it was and adds the new stroke's after it. #[test] fn continuing_a_stroke_joins_its_pressure() { let mut s = session(); let r = s.execute("path.freehand", &json!({"points": [[0, 50, 0], [100, 50, 1]], "style": "brush"})).unwrap(); let id = NodeId(r["id"].as_u64().unwrap()); s.execute("path.freehand", &json!({"points": [[100, 50, 1], [200, 50, 0]], "style": "brush", "extend": {"id": id.0, "end": "end"}})).unwrap(); let p = pressure_of(&s, id).unwrap(); assert!(p.at(0.0) < 0.05 && (p.at(0.5) - 1.0).abs() < 0.05 && p.at(1.0) < 0.05, "{p:?}"); // Continued at its start: the new stroke comes first, running backwards from where it joined. let r = s.execute("path.freehand", &json!({"points": [[0, 150], [100, 150]], "style": "brush"})).unwrap(); let id = NodeId(r["id"].as_u64().unwrap()); s.execute("path.freehand", &json!({"points": [[0, 150, 1], [-100, 150, 0]], "style": "brush", "extend": {"id": id.0, "end": "start"}})).unwrap(); let p = pressure_of(&s, id).unwrap(); assert!(p.at(0.0) < 0.05 && (p.at(0.5) - 1.0).abs() < 0.05, "{p:?}"); assert_eq!(p.at(1.0), vectorcraft_doc::PressureProfile::MID, "the old part had no pressure"); } /// The pen's tilt, bearing and barrel rotation recorded on the stroke `id`. fn pose_of(s: &Session, id: NodeId) -> (Option, Option, Option) { let st = node(s, id).appearance.stroke().cloned().unwrap(); (st.tilt, st.bearing, st.rotation) } /// #764: `path.freehand` points carry the pen's tilt and barrel rotation; the stroke keeps how far /// the pen leaned (0 upright to 1 flat), which way (counter-clockwise on the page) and how far its /// barrel turned (counter-clockwise), and a continued stroke joins them. #[test] fn freehand_points_record_the_pens_tilt_bearing_and_rotation() { let mut s = session(); let draw = |s: &mut Session, points: Value| { NodeId(s.execute("path.freehand", &json!({"points": points, "style": "brush"})).unwrap()["id"].as_u64().unwrap()) }; // Leaning 30° to the right, the barrel turned 90° clockwise. let id = draw(&mut s, json!([[0, 50, 0.5, 30, 0, 90], [100, 50, 0.5, 30, 0, 90]])); let (tilt, bearing, rotation) = pose_of(&s, id); assert!((tilt.unwrap().at(0.5) - 1.0 / 3.0).abs() < 1e-3); assert!(bearing.unwrap().at(0.5).abs() < 1e-6); assert!((rotation.unwrap().at(0.5) + 90.0).abs() < 1e-6, "counter-clockwise"); // Leaning down the page (towards the user) is −90°; no rotation axis, no rotation. let id = draw(&mut s, json!([[0, 150, 0.5, 0, 30, null], {"x": 100, "y": 150, "pressure": 0.5, "tiltY": 30}])); let (_, bearing, rotation) = pose_of(&s, id); assert!((bearing.unwrap().at(0.5) + 90.0).abs() < 1e-6); assert_eq!(rotation, None); // Upright, the pen leans no way: the bearing it leaned before. let id = draw(&mut s, json!([[0, 250, 0.5, 0, -40], [50, 250, 0.5, 0.2, 0.2], [100, 250, 0.5, 0, 0]])); let (tilt, bearing, _) = pose_of(&s, id); let bearing = bearing.unwrap(); assert!((bearing.at(1.0) - 90.0).abs() < 1e-6 && tilt.unwrap().at(1.0) < 1e-6, "{bearing:?}"); // Pressure alone, or nothing: no pen pose. let id = draw(&mut s, json!([[0, 350, 0.5], [100, 350, 0.5]])); assert_eq!(pose_of(&s, id), (None, None, None)); // Continued at its end: the old part keeps its rotation, the new part has none (unturned). let id = draw(&mut s, json!([[0, 450, 0.5, 30, 0, 90], [100, 450, 0.5, 30, 0, 90]])); s.execute("path.freehand", &json!({"points": [[100, 450], [200, 450]], "style": "brush", "extend": {"id": id.0, "end": "end"}})).unwrap(); let (tilt, _, rotation) = pose_of(&s, id); let rotation = rotation.unwrap(); assert!((rotation.at(0.25) + 90.0).abs() < 1e-6 && rotation.at(0.9).abs() < 1e-6, "{rotation:?}"); assert_eq!(tilt.unwrap().at(0.9), vectorcraft_doc::PressureProfile::MID, "half way over without one"); } /// #764: a stroke that connects two paths joins the pen's tilt, bearing and rotation of both, the /// connected path's after the stroke's. #[test] fn connecting_two_strokes_joins_their_pens_rotation() { let mut s = session(); let draw = |s: &mut Session, points: Value| { NodeId(s.execute("path.freehand", &json!({"points": points, "style": "brush"})).unwrap()["id"].as_u64().unwrap()) }; let a = draw(&mut s, json!([[0, 50, 0.5, 30, 0, 90], [100, 50, 0.5, 30, 0, 90]])); let b = draw(&mut s, json!([[200, 50, 0.5, 30, 0, 45], [300, 50, 0.5, 30, 0, 45]])); let connect = json!({"points": [[100, 50], [200, 50]], "style": "brush", "extend": {"id": a.0, "end": "end"}, "connect": {"id": b.0, "end": "start"}}); s.execute("path.freehand", &connect).unwrap(); assert!(s.doc().unwrap().doc.node(b).is_none(), "joined on"); let (_, _, rotation) = pose_of(&s, a); let rotation = rotation.unwrap(); assert!((rotation.at(0.1) + 90.0).abs() < 1e-6, "{rotation:?}"); assert!(rotation.at(0.5).abs() < 1e-6, "the stroke is unturned: {rotation:?}"); assert!((rotation.at(0.9) + 45.0).abs() < 1e-6, "the connected path's: {rotation:?}"); } /// #764: the Paintbrush records a pen's tilt, bearing and rotation on the stroke, a brush with /// Roundness on Tilt draws it, and it lasts through saving; a mouse stroke records none. #[test] fn paintbrush_records_the_pens_tilt_on_the_stroke() { let mut s = session(); let params = json!({"angle": 0, "roundness": 50, "size": 10, "variation": [0, 50, 0], "modes": ["fixed", "tilt", "fixed"]}); s.execute("brush.options", &json!({"name": "3 pt. Round", "params": params})).unwrap(); s.execute("brush.setCurrent", &json!({"name": "3 pt. Round"})).unwrap(); let v = ViewInfo::default(); s.select_tool("paintbrush", v).unwrap(); let strokes = |s: &mut Session, y: f64, pen: Option| { for (k, x) in [(PointerKind::Down, 20.0), (PointerKind::Drag, 120.0), (PointerKind::Drag, 220.0), (PointerKind::Up, 320.0)] { s.pointer(&PointerEvent { pen, ..PointerEvent::new(k, x, y) }, v).unwrap(); } let id = s.doc().unwrap().selection.objects[0]; select(s, &[]); id }; let pen = strokes(&mut s, 60.0, Some(PenPose { tilt: Some([0.0, 0.0]), rotation: Some(30.0) })); let (tilt, bearing, rotation) = pose_of(&s, pen); assert_eq!(tilt.as_ref().map(|t| t.at(0.5)), Some(0.0), "upright"); assert!(bearing.is_some() && (rotation.unwrap().at(0.5) + 30.0).abs() < 1e-6); let mouse = strokes(&mut s, 200.0, None); assert_eq!(pose_of(&s, mouse), (None, None, None), "a mouse reads no tilt"); // Drawn: upright, the nib is round (10 pt); without a tilt, roundness 50 (5 pt). assert!((brushed_bounds(&s, pen).height() - 10.0).abs() < 0.1, "{:?}", brushed_bounds(&s, pen)); assert!((brushed_bounds(&s, mouse).height() - 5.0).abs() < 0.1, "{:?}", brushed_bounds(&s, mouse)); // Saved and reopened as it was. let doc: Document = (*s.doc().unwrap().doc).clone(); let back = vectorcraft_format::load(&vectorcraft_format::save(&doc, false)).unwrap(); assert_eq!(back.node(pen).unwrap().appearance.stroke(), doc.node(pen).unwrap().appearance.stroke()); } /// Pen pressure belongs to its path: new art, graphic styles and Brush Options' edits leave it. #[test] fn pressure_stays_with_its_own_path() { let mut s = session(); s.execute("appearance.setNewArtBasic", &json!({"on": false})).unwrap(); let points = json!([[0, 50, 0.2, 30, 10, 45], [100, 50, 0.9, 20, 0, 90]]); let r = s.execute("brush.freehand", &json!({"points": points, "style": "brush", "brush": "3 pt. Round"})).unwrap(); let id = NodeId(r["id"].as_u64().unwrap()); assert!(pressure_of(&s, id).is_some()); assert!(matches!(pose_of(&s, id), (Some(_), Some(_), Some(_)))); select(&mut s, &[id]); s.execute("graphicStyle.new", &json!({"name": "Inked"})).unwrap(); let style = s.doc().unwrap().doc.graphic_style("Inked").unwrap().appearance.clone(); assert!(style.stroke().is_some_and(|st| st.pressure.is_none() && st.tilt.is_none() && st.bearing.is_none() && st.rotation.is_none())); let other = rect(&mut s, 10.0, 100.0, 50.0, 50.0); assert_eq!(pressure_of(&s, other), None, "new art takes the look, not the pressure"); assert_eq!(pose_of(&s, other), (None, None, None), "nor the tilt, bearing or rotation"); pressure_round(&mut s); assert!(pressure_of(&s, id).is_some(), "editing the brush keeps the strokes' pressure"); } /// Brush Options sets the variation modes; bad modes are refused, never a panic. #[test] fn brush_options_set_variation_modes() { let mut s = session(); pressure_round(&mut s); let b = s.execute("brush.get", &json!({"name": "3 pt. Round"})).unwrap(); assert_eq!(b["modes"], json!(["fixed", "fixed", "pressure"])); assert_eq!(b["variation"], json!([0.0, 0.0, 3.0])); for bad in [json!({"modes": ["fixed", "fixed", "wobble"]}), json!({"modes": "pressure"}), json!({"variation": "x"}), json!({"modes": [1, 2, 3]})] { assert!(s.execute("brush.options", &json!({"name": "3 pt. Round", "params": bad})).is_err(), "{bad}"); } assert_eq!(s.execute("brush.get", &json!({"name": "3 pt. Round"})).unwrap(), b, "a refused edit changes nothing"); // #764: Tilt, Bearing and Rotation, on a Calligraphic and a Scatter brush. let modes = json!(["tilt", "bearing", "rotation"]); s.execute("brush.options", &json!({"name": "3 pt. Round", "params": {"modes": modes}})).unwrap(); assert_eq!(s.execute("brush.get", &json!({"name": "3 pt. Round"})).unwrap()["modes"], modes); let modes = json!(["tilt", "bearing", "rotation", "pressure"]); s.execute("brush.options", &json!({"name": "Dots", "params": {"modes": modes}})).unwrap(); assert_eq!(s.execute("brush.get", &json!({"name": "Dots"})).unwrap()["modes"], modes); } /// The bounds of the brush art `id`'s strokes draw. fn brushed_bounds(s: &Session, id: NodeId) -> Rect { let st = s.doc().unwrap(); let pieces = vectorcraft_brush::node_pieces(&st.doc, &node(s, id)).expect("brushed"); vectorcraft_brush::pieces_bounds(&pieces).expect("draws") } /// Scatter, Art and Pattern Brush Options: the params reach the brush and every stroke painted /// with it redraws; values out of range are clamped, unknown choices refused. #[test] fn scatter_art_and_pattern_brush_options_update_strokes() { let mut s = session(); // A rectangle as an art brush (straight edges: no flattening between sizes). let r = rect(&mut s, 10.0, 200.0, 40.0, 8.0); let bar = s.execute("brush.new", &json!({"type": "art", "name": "Bar", "ids": [r.0]})).unwrap()["name"].as_str().unwrap().to_string(); let id = line(&mut s); select(&mut s, &[id]); let edit = |s: &mut Session, name: &str, params: Value| -> f64 { s.execute("brush.apply", &json!({ "name": name })).unwrap(); let before = brushed_bounds(s, id).height(); s.execute("brush.options", &json!({ "name": name, "params": params })).unwrap(); brushed_bounds(s, id).height() / before }; // Art: Width 300 % draws the art three times as wide across the path. let art = json!({"width": 300, "direction": "rightToLeft", "flip_across": true}); assert!((edit(&mut s, &bar, art) - 3.0).abs() < 1e-6); s.execute("brush.options", &json!({"name": bar, "params": {"scale": {"mode": "betweenGuides", "start": 0.2, "end": 0.6}}})).unwrap(); let b = s.execute("brush.get", &json!({ "name": bar })).unwrap(); assert_eq!( (&b["direction"], &b["flip_across"], &b["scale"]), (&json!("rightToLeft"), &json!(true), &json!({"mode": "betweenGuides", "start": 0.2, "end": 0.6})) ); // Pattern: Scale 200 % doubles the tiles. assert!((edit(&mut s, "Stitches", json!({"scale": 200, "spacing": 50, "fit": "addSpace", "flip_along": true})) - 2.0).abs() < 0.05); let b = s.execute("brush.get", &json!({"name": "Stitches"})).unwrap(); assert_eq!((&b["spacing"], &b["fit"]), (&json!(50.0), &json!("addSpace"))); // Scatter: a fixed Size of 200 % doubles the dots; the modes and Hue Shift are kept. let key = json!({"model": "rgb", "r": 0.0, "g": 0.0, "b": 0.0}); let scatter = json!({"size": [200, 50], "modes": ["fixed", "fixed", "fixed", "random"], "rotation_relative_to_path": true, "colorization": {"method": "hueShift", "key": key}}); assert!((edit(&mut s, "Dots", scatter) - 2.0).abs() < 0.05); let b = s.execute("brush.get", &json!({"name": "Dots"})).unwrap(); assert_eq!( (&b["modes"], &b["colorization"]["method"], &b["colorization"]["key"]), (&json!(["fixed", "fixed", "fixed", "random"]), &json!("hueShift"), &key) ); // Out of range: clamped. Unknown choices: refused, nothing changes. let junk = json!({"size": [1e308, -1e308], "spacing": [0, 1e308], "rotation": [720, -1e9], "scatter": [-1e308, 5]}); s.execute("brush.options", &json!({"name": "Dots", "params": junk})).unwrap(); let b = s.execute("brush.get", &json!({"name": "Dots"})).unwrap(); assert_eq!( (&b["size"], &b["spacing"], &b["rotation"], &b["scatter"]), (&json!([10000.0, 1.0]), &json!([1.0, 10000.0]), &json!([180.0, -180.0]), &json!([-1000.0, 5.0])) ); assert!(brushed_bounds(&s, id).is_finite()); for (name, bad) in [ ("Dots", json!({"modes": ["fixed", "wobble", "fixed", "fixed"]})), ("Dots", json!({"modes": ["fixed"]})), ("Dots", json!({"colorization": {"method": "sparkle"}})), ("Arrow", json!({"direction": "diagonal"})), ("Arrow", json!({"width": "wide"})), ("Chain", json!({"fit": "squeeze"})), ] { let before = s.execute("brush.get", &json!({ "name": name })).unwrap(); assert!(s.execute("brush.options", &json!({ "name": name, "params": bad })).is_err(), "{bad}"); assert_eq!(s.execute("brush.get", &json!({ "name": name })).unwrap(), before); } s.execute("brush.options", &json!({"name": "Chain", "params": {"scale": 1e308, "spacing": -1e308}})).unwrap(); let b = s.execute("brush.get", &json!({"name": "Chain"})).unwrap(); assert_eq!((&b["scale"], &b["spacing"]), (&json!(10000.0), &json!(0.0))); } /// Hue Shift given without a key color takes the color the art's fills and strokes use most, in /// New Brush and in Brush Options, and a Brush Options edit that gives only the method keeps the /// brush's key color. #[test] fn hue_shift_without_a_key_takes_the_most_used_art_color() { let mut s = session(); let paint = |s: &mut Session, id: NodeId, fill: &str, stroke: Option<&str>| { s.execute("paint.setFill", &json!({"color": fill, "ids": [id.0]})).unwrap(); match stroke { Some(c) => s.execute("paint.setStroke", &json!({"color": c, "ids": [id.0]})).unwrap(), None => s.execute("paint.setStroke", &json!({"none": true, "ids": [id.0]})).unwrap(), }; }; // Blue comes first; red paints two fills and a stroke. let a = rect(&mut s, 10.0, 200.0, 20.0, 8.0); paint(&mut s, a, "#0000ff", None); let b = rect(&mut s, 40.0, 200.0, 20.0, 8.0); paint(&mut s, b, "#ff0000", Some("#ff0000")); let c = rect(&mut s, 70.0, 200.0, 20.0, 8.0); paint(&mut s, c, "#ff0000", None); let color = |r: f32, g: f32, b: f32| serde_json::to_value(vectorcraft_color::Color::rgb(r, g, b)).unwrap(); let (red, green) = (color(1.0, 0.0, 0.0), color(0.0, 1.0, 0.0)); let hue_shift = json!({"colorization": {"method": "hueShift"}}); let r = s.execute("brush.new", &json!({"type": "scatter", "name": "Key", "ids": [a.0, b.0, c.0], "params": hue_shift})).unwrap(); assert_eq!(r["name"], "Key"); let colorization = |s: &mut Session| s.execute("brush.get", &json!({"name": "Key"})).unwrap()["colorization"].clone(); assert_eq!(colorization(&mut s), json!({"method": "hueShift", "key": red})); let options = |s: &mut Session, params: Value| s.execute("brush.options", &json!({"name": "Key", "params": params})).unwrap(); // Brush Options switches from Tints to Hue Shift without a key. options(&mut s, json!({"colorization": {"method": "tints"}})); assert_eq!(colorization(&mut s), json!({"method": "tints"})); options(&mut s, hue_shift.clone()); assert_eq!(colorization(&mut s), json!({"method": "hueShift", "key": red})); // A key color that was set stays when an edit gives only the method. options(&mut s, json!({"colorization": {"method": "hueShift", "key": green}})); options(&mut s, hue_shift.clone()); assert_eq!(colorization(&mut s), json!({"method": "hueShift", "key": green})); // Art with no solid color takes black. let d = rect(&mut s, 100.0, 200.0, 20.0, 8.0); s.execute("paint.setFill", &json!({"gradient": {"kind": "linear"}, "ids": [d.0]})).unwrap(); s.execute("paint.setStroke", &json!({"none": true, "ids": [d.0]})).unwrap(); let r = s.execute("brush.new", &json!({"type": "art", "name": "Plain", "ids": [d.0], "params": hue_shift})).unwrap(); assert_eq!(r["name"], "Plain"); let b = s.execute("brush.get", &json!({"name": "Plain"})).unwrap(); assert_eq!(b["colorization"], json!({"method": "hueShift", "key": color(0.0, 0.0, 0.0)})); } /// Brush Options merges an art brush's `scale` field by field: an edit that gives only the start /// guide keeps the end guide. #[test] fn art_brush_options_keep_the_scale_fields_an_edit_leaves_out() { let mut s = session(); let r = rect(&mut s, 10.0, 200.0, 40.0, 8.0); s.execute("brush.new", &json!({"type": "art", "name": "Bar", "ids": [r.0]})).unwrap(); s.execute("brush.options", &json!({"name": "Bar", "params": {"scale": {"mode": "betweenGuides", "start": 0.2, "end": 0.6}}})).unwrap(); s.execute("brush.options", &json!({"name": "Bar", "params": {"scale": {"mode": "betweenGuides", "start": 0.3}}})).unwrap(); let b = s.execute("brush.get", &json!({"name": "Bar"})).unwrap(); assert_eq!(b["scale"], json!({"mode": "betweenGuides", "start": 0.3, "end": 0.6})); // A scale that becomes a plain mode drops the guides. s.execute("brush.options", &json!({"name": "Bar", "params": {"scale": {"mode": "stretch"}}})).unwrap(); assert_eq!(s.execute("brush.get", &json!({"name": "Bar"})).unwrap()["scale"], json!({"mode": "stretch"})); } #[test] fn pattern_tile_choices_copy_whole_swatch_art_and_reject_invalid_side() { use std::sync::Arc; use vectorcraft_doc::{PatternDef, StrokeLayer}; use vectorcraft_geom::BezPath; let mut s = session(); s.edit("Pattern", |d, _| { let art = Node::path(d.alloc_id(), vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 30.0, 4.0)), vectorcraft_doc::Appearance::default_art()); d.patterns.push(PatternDef { tile: Rect::new(0.0, 0.0, 10.0, 4.0), ..PatternDef::new("Wide", vec![Arc::new(art)]) }); Ok(()) }) .unwrap(); let side = s.execute("brush.get", &json!({"name":"Diamonds"})).unwrap()["side"].clone(); s.execute("brush.new", &json!({"type":"pattern", "name":"Plain Tiles", "params":{"side":side}})).unwrap(); let original = s.execute("brush.get", &json!({"name":"Plain Tiles"})).unwrap(); let steps = s.doc().unwrap().history.undo.len(); for choice in ["none", "Missing"] { let error = s.execute("brush.options", &json!({"name":"Plain Tiles", "params":{"side_tile":choice}})).unwrap_err().to_string(); assert!(error.contains(if choice == "none" { "You must choose a side pattern." } else { "no pattern named" }), "{error}"); assert_eq!(s.doc().unwrap().history.undo.len(), steps); assert_eq!(s.execute("brush.get", &json!({"name":"Plain Tiles"})).unwrap(), original); } s.execute("brush.options", &json!({"name":"Plain Tiles", "params":{"side_tile":"Wide", "fit":"addSpace"}})).unwrap(); let saved = s.execute("brush.get", &json!({"name":"Plain Tiles"})).unwrap(); assert_eq!(saved["side_frame"], json!(Rect::new(0.0, 0.0, 10.0, 4.0))); assert_eq!(saved["side_swatch"], "Wide"); let brush: vectorcraft_brush::Brush = serde_json::from_value(saved).unwrap(); let mut path = BezPath::new(); path.move_to((0.0, 0.0)); path.line_to((100.0, 0.0)); let pieces = vectorcraft_brush::stroke_pieces( &s.doc().unwrap().doc, &brush, &path, &StrokeLayer::new(vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK), 1.0), ); assert_eq!(pieces.len(), 10, "10 pt tile frame repeats ten times along 100 pt"); assert!(pieces[0].geometric_bounds().unwrap().width() >= 29.9, "whole 30 pt art is copied"); s.execute("brush.options", &json!({"name":"Plain Tiles", "params":{"inner_corner_tile":"Wide", "outer_corner_tile":"none"}})).unwrap(); let corners: vectorcraft_brush::Brush = serde_json::from_value(s.execute("brush.get", &json!({"name":"Plain Tiles"})).unwrap()).unwrap(); let mut fallback = corners.clone(); if let (vectorcraft_brush::BrushKind::Pattern(actual), vectorcraft_brush::BrushKind::Pattern(legacy)) = (&corners.kind, &mut fallback.kind) { assert_eq!(actual.outer_source, Some(vectorcraft_brush::CornerSource::None)); assert!(actual.inner_corner.is_some()); legacy.outer_source = None; } let square = |clockwise: bool| { let mut p = BezPath::new(); p.move_to((0.0, 0.0)); if clockwise { for point in [(100.0, 0.0), (100.0, 100.0), (0.0, 100.0)] { p.line_to(point); } } else { for point in [(0.0, 100.0), (100.0, 100.0), (100.0, 0.0)] { p.line_to(point); } } p.close_path(); p }; let stroke = StrokeLayer::new(vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK), 1.0); assert!( [false, true].into_iter().any(|clockwise| { let path = square(clockwise); vectorcraft_brush::stroke_pieces(&s.doc().unwrap().doc, &corners, &path, &stroke) != vectorcraft_brush::stroke_pieces(&s.doc().unwrap().doc, &fallback, &path, &stroke) }), "explicit outer None omits the inner corner art on outer turns" ); let borrowed_art = s.execute("brush.get", &json!({"name":"Plain Tiles"})).unwrap()["inner_corner"].clone(); s.execute("brush.new", &json!({"type":"pattern", "name":"Borrowed Corner", "params":{"side":original["side"], "inner_corner":borrowed_art}})) .unwrap(); s.execute("brush.options", &json!({"name":"Borrowed Corner", "params":{"outer_corner_tile":"original", "inner_corner_tile":"none"}})).unwrap(); let borrowed = s.execute("brush.get", &json!({"name":"Borrowed Corner"})).unwrap(); assert!(borrowed["outer_corner"].is_object()); assert_eq!(borrowed["inner_source"], "none"); s.edit("Delete pattern", |d, _| { d.patterns.clear(); Ok(()) }) .unwrap(); let after: vectorcraft_brush::Brush = serde_json::from_value(s.execute("brush.get", &json!({"name":"Plain Tiles"})).unwrap()).unwrap(); assert_eq!( vectorcraft_brush::stroke_pieces( &s.doc().unwrap().doc, &after, &path, &StrokeLayer::new(vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK), 1.0) ) .len(), 10 ); let copied = s.execute("brush.get", &json!({"name":"Plain Tiles"})).unwrap()["side"].clone(); s.execute("brush.options", &json!({"name":"Plain Tiles", "params":{"side_tile":"original"}})).unwrap(); let original_choice = s.execute("brush.get", &json!({"name":"Plain Tiles"})).unwrap(); assert_eq!(original_choice["side"], copied); assert!(original_choice.get("side_swatch").is_none()); } #[test] fn pattern_original_corner_keeps_borrowed_art_and_frame_when_source_is_cleared() { let mut s = session(); let chain = s.execute("brush.get", &json!({"name": "Chain"})).unwrap(); let art = chain["outer_corner"].clone(); let frame = json!(Rect::new(1.0, 2.0, 15.0, 18.0)); for (name, source, borrowed) in [("Borrow Outer", "inner", "outer"), ("Borrow Inner", "outer", "inner")] { let source_art = format!("{source}_corner"); let source_frame = format!("{source_art}_frame"); let borrowed_art = format!("{borrowed}_corner"); let borrowed_frame = format!("{borrowed_art}_frame"); let source_tile = format!("{source_art}_tile"); let borrowed_tile = format!("{borrowed_art}_tile"); s.execute("brush.new", &json!({"type":"pattern", "name":name, "params":{"side":chain["side"], source_art:art, source_frame:frame}})).unwrap(); s.execute("brush.options", &json!({"name":name, "params":{source_tile:"none", borrowed_tile:"original"}})).unwrap(); let saved = s.execute("brush.get", &json!({"name":name})).unwrap(); assert_eq!(saved[&borrowed_art], chain["outer_corner"], "{borrowed} art"); assert_eq!(saved[&borrowed_frame], frame, "{borrowed} frame"); assert_eq!(saved[format!("{source}_source")], "none"); } } #[test] fn pattern_tile_object_selects_reserved_swatch_names() { use std::sync::Arc; use vectorcraft_doc::PatternDef; let mut s = session(); let names = ["none", "original", "autoCentered", "autoBetween", "autoSliced", "autoOverlap"]; s.edit("Patterns", |doc, _| { for name in names { let art = Node::path( doc.alloc_id(), vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 30.0, 4.0)), vectorcraft_doc::Appearance::default_art(), ); doc.patterns.push(PatternDef { tile: Rect::new(0.0, 0.0, 10.0, 4.0), ..PatternDef::new(name, vec![Arc::new(art)]) }); } Ok(()) }) .unwrap(); let before = s.execute("brush.get", &json!({"name":"Diamonds"})).unwrap(); for invalid in [json!({}), json!({"pattern": null}), json!({"pattern": 1})] { let error = s.execute("brush.options", &json!({"name":"Diamonds", "params":{"side_tile":invalid}})).unwrap_err().to_string(); assert!(error.contains("invalid pattern tile choice"), "{error}"); assert_eq!(s.execute("brush.get", &json!({"name":"Diamonds"})).unwrap(), before); } let slots = ["side_tile", "outer_corner_tile", "inner_corner_tile", "start_tile", "end_tile"]; for (slot, name) in slots.into_iter().zip(names) { s.execute("brush.options", &json!({"name":"Diamonds", "params":{(slot):{"pattern":name}}})).unwrap(); } let saved = s.execute("brush.get", &json!({"name":"Diamonds"})).unwrap(); for (art, name) in ["side", "outer_corner", "inner_corner", "start", "end"].into_iter().zip(names) { assert_eq!(saved[format!("{art}_swatch")], name); assert_eq!(saved[format!("{art}_frame")], json!(Rect::new(0.0, 0.0, 10.0, 4.0))); } s.execute("brush.options", &json!({"name":"Diamonds", "params":{"outer_corner_tile":{"pattern":"autoOverlap"}}})).unwrap(); assert_eq!(s.execute("brush.get", &json!({"name":"Diamonds"})).unwrap()["outer_corner_swatch"], "autoOverlap"); } #[test] fn pattern_end_tile_none_choices_leave_side_art_at_both_path_ends() { use vectorcraft_doc::StrokeLayer; use vectorcraft_geom::BezPath; let mut s = session(); let side = s.execute("brush.get", &json!({"name":"Diamonds"})).unwrap()["side"].clone(); let cap = s.execute("brush.get", &json!({"name":"Chain"})).unwrap()["outer_corner"].clone(); s.execute("brush.new", &json!({"type":"pattern", "name":"Capped", "params":{"side":side, "start":cap, "end":cap}})).unwrap(); s.execute("brush.options", &json!({"name":"Capped", "params":{"start_tile":"none", "end_tile":"none"}})).unwrap(); let def = s.execute("brush.get", &json!({"name":"Capped"})).unwrap(); assert!(def.get("start").is_none() && def.get("end").is_none()); let brush: vectorcraft_brush::Brush = serde_json::from_value(def).unwrap(); let mut path = BezPath::new(); path.move_to((0.0, 0.0)); path.line_to((100.0, 0.0)); let pieces = vectorcraft_brush::stroke_pieces( &s.doc().unwrap().doc, &brush, &path, &StrokeLayer::new(vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK), 1.0), ); let bounds = pieces.iter().fold(None, |bounds, node| vectorcraft_geom::union_opt(bounds, node.geometric_bounds())).unwrap(); assert!((bounds.x0 - 0.0).abs() < 1e-6 && (bounds.x1 - 100.0).abs() < 1e-6, "{bounds:?}"); } // ---------- symbols ---------- fn make_symbol(s: &mut Session) -> (NodeId, String, Rect) { let a = rect(s, 100.0, 50.0, 80.0, 40.0); let b = bounds(s, a); let r = s.execute("symbol.new", &json!({"name": "Box"})).unwrap(); (NodeId(r["id"].as_u64().unwrap()), r["name"].as_str().unwrap().to_string(), b) } #[test] fn new_symbol_replaces_selection_with_instance_of_same_bounds() { let mut s = session(); let (inst, name, b) = make_symbol(&mut s); assert_eq!(name, "Box"); let n = node(&s, inst); assert!(matches!(&n.kind, NodeKind::SymbolInstance { symbol, .. } if symbol == "Box")); assert!(close(n.geometric_bounds().unwrap(), b, 1e-9), "{:?} vs {b:?}", n.geometric_bounds()); let d = &s.doc().unwrap().doc; assert_eq!(d.symbols.len(), 1); assert_eq!(d.layers[0].children().unwrap().len(), 1); let l = s.execute("symbol.list", &json!({})).unwrap(); assert_eq!(l["symbols"][0]["instances"], 1); assert_eq!(l["symbols"][0]["size"], json!([80.0, 40.0])); // A second symbol gets a unique name. rect(&mut s, 0.0, 0.0, 10.0, 10.0); assert_eq!(s.execute("symbol.new", &json!({"name": "Box"})).unwrap()["name"], "Box 2"); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(s.doc().unwrap().doc.symbols.len(), 1); } /// New Symbol given `ids` that list an object and then the group that holds it puts the instance /// where the group was and removes both, as removing one object at a time does. #[test] fn new_symbol_of_an_object_and_the_group_that_holds_it_keeps_order_and_ids() { let mut s = session(); let (a, b) = (rect(&mut s, 0.0, 0.0, 10.0, 10.0), rect(&mut s, 100.0, 0.0, 10.0, 10.0)); select(&mut s, &[a, b]); let group = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); let c = rect(&mut s, 200.0, 0.0, 10.0, 10.0); let before = s.doc().unwrap().doc.clone(); let r = s.execute("symbol.new", &json!({"name": "Pair", "ids": [a.0, group.0]})).unwrap(); let inst = NodeId(r["id"].as_u64().unwrap()); let ids = |n: &Node| n.children().unwrap().iter().map(|c| c.id).collect::>(); assert_eq!(ids(&s.doc().unwrap().doc.layers[0]), [inst, c]); assert_eq!(s.doc().unwrap().selection.objects, [inst]); s.execute("edit.undo", &json!({})).unwrap(); assert!(*s.doc().unwrap().doc == *before, "Undo gives back the document"); } /// New Symbol given `ids` that list an object and a group that holds it (directly, or inside /// another group) makes the symbol of the outer group, whose art holds the object once, as it does /// with those objects selected. #[test] fn new_symbol_of_an_object_and_a_group_that_holds_it_has_the_object_once() { let nested = || { let mut s = session(); let (a, b) = (rect(&mut s, 0.0, 0.0, 10.0, 10.0), rect(&mut s, 100.0, 0.0, 10.0, 10.0)); select(&mut s, &[a, b]); let inner = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); let c = rect(&mut s, 200.0, 0.0, 10.0, 10.0); select(&mut s, &[inner, c]); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.execute("select.none", &json!({})).unwrap(); (s, [a, inner, outer]) }; for (picks, paths) in [(&[0, 1][..], 2), (&[1, 0, 2], 3)] { let (mut given, ids) = nested(); let list: Vec = picks.iter().map(|i| ids[*i]).collect(); given.execute("symbol.new", &json!({"name": "Pair", "ids": list.iter().map(|i| i.0).collect::>()})).unwrap(); let d = &given.doc().unwrap().doc; let mut n = 0; d.symbols[0].art.walk(&mut |c| n += usize::from(matches!(c.kind, NodeKind::Path { .. }))); assert_eq!(n, paths, "ids {list:?}"); let (mut selected, _) = nested(); select(&mut selected, &list); selected.execute("symbol.new", &json!({"name": "Pair"})).unwrap(); let e = &selected.doc().unwrap().doc; assert!(d.symbols == e.symbols && d.layers == e.layers, "ids {list:?}"); } } #[test] fn place_and_break_link_round_trip_geometry() { let mut s = session(); let (_, name, _) = make_symbol(&mut s); let r = s.execute("symbol.place", &json!({"name": name, "x": 300, "y": 200})).unwrap(); let p = NodeId(r["id"].as_u64().unwrap()); assert!(close(bounds(&s, p), Rect::new(260.0, 180.0, 340.0, 220.0), 1e-9), "{:?}", bounds(&s, p)); let r = s.execute("symbol.breakLink", &json!({"ids": [p.0]})).unwrap(); let art = NodeId(r["ids"][0].as_u64().unwrap()); let n = node(&s, art); assert!(matches!(n.kind, NodeKind::Path { .. })); assert!(close(n.geometric_bounds().unwrap(), Rect::new(260.0, 180.0, 340.0, 220.0), 1e-6)); assert_eq!(n.appearance.stroke_width(), 1.0, "strokes are not scaled by normalisation"); } #[test] fn hidden_instance_fill_does_not_tint_expanded_symbol_art() { for command in ["symbol.breakLink", "object.expand"] { let mut s = session(); let source = rect(&mut s, 100.0, 50.0, 80.0, 40.0); s.execute("paint.setFill", &json!({"ids": [source.0], "color": "#0000ff"})).unwrap(); let inst = NodeId(s.execute("symbol.new", &json!({"name": "Blue Box"})).unwrap()["id"].as_u64().unwrap()); s.execute("appearance.addFill", &json!({"ids": [inst.0]})).unwrap(); let fill = node(&s, inst).appearance.items.len() - 1; s.execute("appearance.setItem", &json!({"ids": [inst.0], "index": fill, "color": "#ff0000"})).unwrap(); let center = |s: &Session| { let doc = s.doc().unwrap().doc.clone(); vectorcraft_render::Renderer::new().render_region(&doc, Rect::new(0.0, 0.0, 400.0, 300.0), 1.0, true).pixel(140, 70) }; let tinted = center(&s); assert!(tinted[0] > tinted[2], "the visible instance fill tints the symbol: {tinted:?}"); s.execute("appearance.setItem", &json!({"ids": [inst.0], "index": fill, "visible": false})).unwrap(); let original = center(&s); assert!(original[2] > original[0], "hiding the fill restores the blue symbol: {original:?}"); let params = if command == "symbol.breakLink" { json!({"ids": [inst.0]}) } else { json!({"object": true, "fill": false, "stroke": false}) }; s.execute(command, ¶ms).unwrap(); assert_eq!(center(&s), original, "{command} must keep the untinted colors"); } } #[test] fn break_link_then_update_redefines_all_instances() { let mut s = session(); let (inst, name, _) = make_symbol(&mut s); let other = NodeId(s.execute("symbol.place", &json!({"x": 300, "y": 200})).unwrap()["id"].as_u64().unwrap()); select(&mut s, &[inst]); let r = s.execute("symbol.breakLink", &json!({})).unwrap(); let art = NodeId(r["ids"][0].as_u64().unwrap()); // Make the art twice as wide, then redefine. s.execute("object.transform", &json!({"ids": [art.0], "matrix": [2, 0, 0, 1, -140, 0]})).ok(); let w = bounds(&s, art).width(); let r = s.execute("symbol.update", &json!({"name": name})).unwrap(); let new_inst = NodeId(r["id"].as_u64().unwrap()); assert!((bounds(&s, new_inst).width() - w).abs() < 1e-6); assert!((bounds(&s, other).width() - w).abs() < 1e-6, "other instances pick up the new size"); assert!((bounds(&s, other).center().x - 300.0).abs() < 1e-6); } /// A linked opacity mask of a 10 × 10 square at (100, 50). fn square_mask(s: &mut Session) -> Box { let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); let art = Node::path(doc.alloc_id(), vectorcraft_geom::shapes::rectangle(Rect::new(100.0, 50.0, 110.0, 60.0)), Default::default()); Box::new(vectorcraft_doc::OpacityMask::new(art, true)) } /// Break Link to Symbol keeps the instance's opacity mask on the art. #[test] fn break_link_keeps_the_instances_opacity_mask() { let mut s = session(); let (inst, _, _) = make_symbol(&mut s); let m = square_mask(&mut s); std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(inst).unwrap().extras_mut().mask = Some(m.clone()); let r = s.execute("symbol.breakLink", &json!({"ids": [inst.0]})).unwrap(); let art = node(&s, NodeId(r["ids"][0].as_u64().unwrap())); assert!(matches!(art.kind, NodeKind::Path { .. }), "{}", art.kind_label()); assert_eq!(art.extras().mask, Some(m), "the art keeps the mask"); } /// When the symbol's art has an opacity mask of its own, Break Link puts the instance's mask on a /// group around the art, and the art keeps its own. #[test] fn break_link_puts_the_instances_mask_on_a_group_around_masked_art() { let mut s = session(); let (inst, name, _) = make_symbol(&mut s); let (own, m) = (square_mask(&mut s), square_mask(&mut s)); let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); let sym = doc.symbols.iter_mut().find(|s| s.name == name).unwrap(); std::sync::Arc::make_mut(&mut sym.art).extras_mut().mask = Some(own); doc.node_mut(inst).unwrap().extras_mut().mask = Some(m.clone()); let r = s.execute("symbol.breakLink", &json!({"ids": [inst.0]})).unwrap(); let art = node(&s, NodeId(r["ids"][0].as_u64().unwrap())); let NodeKind::Group { children, .. } = &art.kind else { panic!("a group around the art: {}", art.kind_label()) }; assert_eq!(art.extras().mask, Some(m), "the group holds the instance's mask"); assert!(children.len() == 1 && children[0].extras().mask.is_some(), "the art keeps its own mask"); } /// The art of the instance's opacity mask counts against Break Link's object limit: an instance /// whose mask holds more than a million objects stays an instance. #[test] fn break_link_counts_the_instances_mask_art() { let mut s = session(); let (inst, _, _) = make_symbol(&mut s); let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); let square = std::sync::Arc::new(Node::path(doc.alloc_id(), vectorcraft_geom::shapes::rectangle(Rect::new(100.0, 50.0, 110.0, 60.0)), Default::default())); let art = Node::group(doc.alloc_id(), vec![square; 1_000_000]); doc.node_mut(inst).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(art, true))); assert!(s.execute("symbol.breakLink", &json!({"ids": [inst.0]})).is_err(), "too many objects"); assert!(matches!(node(&s, inst).kind, NodeKind::SymbolInstance { .. }), "it stays an instance"); } /// A hidden, a locked and a plain instance of one symbol in a group, the group selected. fn group_with_hidden_and_locked_instances(s: &mut Session) -> (NodeId, String) { let (shown, name, _) = make_symbol(s); let hidden = NodeId(s.execute("symbol.place", &json!({"name": name, "x": 300, "y": 200})).unwrap()["id"].as_u64().unwrap()); let locked = NodeId(s.execute("symbol.place", &json!({"name": name, "x": 300, "y": 100})).unwrap()["id"].as_u64().unwrap()); select(s, &[shown, hidden, locked]); let group = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); doc.node_mut(hidden).unwrap().visible = false; doc.node_mut(locked).unwrap().locked = true; select(s, &[group]); (group, name) } /// The visibility and lock state of the children of `group`, in paint order. fn shown_and_locked(s: &Session, group: NodeId) -> Vec<(bool, bool)> { let g = node(s, group); let NodeKind::Group { children, .. } = &g.kind else { panic!("a group: {}", g.kind_label()) }; assert!(children.iter().all(|c| !matches!(c.kind, NodeKind::SymbolInstance { .. })), "every instance is expanded"); children.iter().map(|c| (c.visible, c.locked)).collect() } /// Break Link to Symbol on a selected group keeps a hidden instance's art hidden and a locked /// instance's art locked, and selects only the art that can be selected. #[test] fn break_link_keeps_hidden_and_locked_instances_hidden_and_locked() { let mut s = session(); let (group, _) = group_with_hidden_and_locked_instances(&mut s); let r = s.execute("symbol.breakLink", &json!({})).unwrap(); assert_eq!(r["ids"].as_array().unwrap().len(), 3, "{r}"); assert_eq!(shown_and_locked(&s, group), vec![(true, false), (false, false), (true, true)]); let d = &s.doc().unwrap().doc; let selected: Vec = s.doc().unwrap().selection.objects.to_vec(); assert_eq!(selected.len(), 1, "{selected:?}"); assert!(selected.iter().all(|id| d.node(*id).is_some_and(|n| n.visible && !n.locked)), "{selected:?}"); } /// Break Link to Symbol on a selected group does not select the art of a shown, unlocked instance /// that sits in a hidden or a locked group inside it. #[test] fn break_link_leaves_art_in_hidden_or_locked_groups_unselected() { let mut s = session(); let (shown, name, _) = make_symbol(&mut s); let subgroup = |s: &mut Session, y: i64| { let id = NodeId(s.execute("symbol.place", &json!({"name": name, "x": 300, "y": y})).unwrap()["id"].as_u64().unwrap()); select(s, &[id]); NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()) }; let hidden = subgroup(&mut s, 200); let locked = subgroup(&mut s, 100); select(&mut s, &[shown, hidden, locked]); let group = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); doc.node_mut(hidden).unwrap().visible = false; doc.node_mut(locked).unwrap().locked = true; select(&mut s, &[group]); let r = s.execute("symbol.breakLink", &json!({})).unwrap(); assert_eq!(r["ids"].as_array().unwrap().len(), 3, "{r}"); let d = &s.doc().unwrap().doc; let selected: Vec = s.doc().unwrap().selection.objects.to_vec(); assert_eq!(selected.len(), 1, "{selected:?}"); let a = d.ancestry(selected[0]).unwrap(); assert!(!a.contains(&hidden) && !a.contains(&locked), "{a:?}"); } /// Break Link to Symbol on an instance just placed on a locked artboard selects the art that /// replaces it, which stays new art there. The art of an older instance there, named by id, is not /// selected. #[test] fn break_link_keeps_new_art_on_a_locked_artboard_selected() { let mut s = session(); let (old, name, _) = make_symbol(&mut s); s.execute("artboard.new", &json!({"x": 1000, "y": 0, "width": 400, "height": 400})).unwrap(); s.execute("artboard.setProps", &json!({"index": 1, "locked": true})).unwrap(); select(&mut s, &[old]); s.execute("object.move", &json!({"dx": 1000, "dy": 0})).unwrap(); assert!(s.doc().unwrap().selection.objects.is_empty(), "moved onto the locked artboard"); let inst = NodeId(s.execute("symbol.place", &json!({"name": name, "x": 1100, "y": 100})).unwrap()["id"].as_u64().unwrap()); assert_eq!(s.doc().unwrap().selection.objects.to_vec(), [inst], "placed art is selected"); let r = s.execute("symbol.breakLink", &json!({})).unwrap(); let art: Vec = r["ids"].as_array().unwrap().iter().map(|v| NodeId(v.as_u64().unwrap())).collect(); assert_eq!(art.len(), 1, "{r}"); assert_eq!(s.doc().unwrap().selection.objects.to_vec(), art, "the art that replaces it"); s.execute("object.move", &json!({"dx": 10, "dy": 0})).unwrap(); assert_eq!(s.doc().unwrap().selection.objects.to_vec(), art, "the art stays editable"); let r = s.execute("symbol.breakLink", &json!({"ids": [old.0]})).unwrap(); assert_eq!(r["ids"].as_array().unwrap().len(), 1, "{r}"); assert!(s.doc().unwrap().selection.objects.is_empty(), "the older instance's art"); } /// Delete Symbol, expanding the instances, keeps a hidden instance's art hidden and a locked /// instance's art locked. #[test] fn delete_symbol_keeps_hidden_and_locked_instances_hidden_and_locked() { let mut s = session(); let (group, name) = group_with_hidden_and_locked_instances(&mut s); s.execute("symbol.delete", &json!({"name": name})).unwrap(); assert_eq!(shown_and_locked(&s, group), vec![(true, false), (false, false), (true, true)]); } #[test] fn delete_duplicate_and_replace_symbols() { let mut s = session(); let (inst, name, _) = make_symbol(&mut s); let d = s.execute("symbol.duplicate", &json!({"name": name})).unwrap(); assert_eq!(d["name"], "Box copy"); rect(&mut s, 0.0, 0.0, 20.0, 20.0); s.execute("symbol.new", &json!({"name": "Small"})).unwrap(); select(&mut s, &[inst]); s.execute("symbol.replace", &json!({"name": "Small"})).unwrap(); assert!(matches!(&node(&s, inst).kind, NodeKind::SymbolInstance { symbol, .. } if symbol == "Small")); assert!(close(bounds(&s, inst), Rect::new(130.0, 60.0, 150.0, 80.0), 1e-6), "{:?}", bounds(&s, inst)); s.execute("symbol.delete", &json!({"name": "Small"})).unwrap(); let doc = &s.doc().unwrap().doc; assert!(doc.symbols.iter().all(|x| x.name != "Small")); assert!(doc.node(inst).is_none()); let mut plain = 0; doc.walk(|n| plain += matches!(n.kind, NodeKind::Path { .. }) as i32); assert_eq!(plain, 2, "both instances of Small were expanded"); assert!(s.execute("symbol.delete", &json!({"name": "Small"})).is_err()); } fn xf_of(s: &Session, id: NodeId) -> vectorcraft_geom::Affine { match node(s, id).kind { NodeKind::SymbolInstance { xf, .. } => xf, _ => panic!("{id:?} is not a symbol instance"), } } fn close_xf(a: vectorcraft_geom::Affine, b: vectorcraft_geom::Affine) -> bool { a.as_coeffs().iter().zip(b.as_coeffs()).all(|(x, y)| (x - y).abs() < 1e-9) } fn undo_len(s: &Session) -> usize { s.doc().unwrap().history.undo.len() } /// Reset Transformation gives rotated, scaled, sheared and reflected instances (in a group too) /// their symbol's own scale again, each keeping the center of its bounding box, as one undo step. #[test] fn reset_transformation_restores_the_symbols_scale_and_keeps_the_center() { let mut s = session(); let (a, name, placed) = make_symbol(&mut s); let b = NodeId(s.execute("symbol.place", &json!({"name": name, "x": 300, "y": 200})).unwrap()["id"].as_u64().unwrap()); let c = NodeId(s.execute("symbol.place", &json!({"name": name, "x": 100, "y": 220})).unwrap()["id"].as_u64().unwrap()); let master = xf_of(&s, b).with_translation(vectorcraft_geom::Vec2::ZERO); select(&mut s, &[a]); s.execute("object.rotate", &json!({"angle": 30})).unwrap(); s.execute("object.scale", &json!({"sx": 150, "sy": 50})).unwrap(); select(&mut s, &[b]); s.execute("object.shear", &json!({"angle": 20})).unwrap(); select(&mut s, &[c]); s.execute("object.reflect", &json!({"axis": "vertical"})).unwrap(); let turned: Vec<_> = [a, b, c].iter().map(|id| xf_of(&s, *id)).collect(); let centers: Vec<_> = [a, b, c].iter().map(|id| bounds(&s, *id).center()).collect(); let plain = rect(&mut s, 0.0, 0.0, 10.0, 10.0); select(&mut s, &[b, c]); let group = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); select(&mut s, &[a, group, plain]); let plain_before = node(&s, plain); let before = undo_len(&s); let r = s.execute("symbol.instance.resetTransform", &json!({})).unwrap(); assert_eq!(r["ids"], json!([a.0, b.0, c.0])); assert_eq!(undo_len(&s), before + 1, "one undo step"); for (id, center) in [a, b, c].into_iter().zip(¢ers) { let got = bounds(&s, id); assert!((got.width() - placed.width()).abs() < 1e-9 && (got.height() - placed.height()).abs() < 1e-9, "{id:?}: {got:?}"); assert!((got.center() - *center).hypot() < 1e-9, "{id:?} keeps its center: {got:?} vs {center:?}"); let xf = xf_of(&s, id); assert!(close_xf(xf.with_translation(vectorcraft_geom::Vec2::ZERO), master), "{id:?}: {xf:?}"); } assert!(node(&s, plain) == plain_before, "other objects are left as they are"); s.execute("edit.undo", &json!({})).unwrap(); for (id, xf) in [a, b, c].into_iter().zip(turned) { assert!(close_xf(xf_of(&s, id), xf), "Undo gives {id:?} its own transform back"); } } /// An instance of a symbol whose art lies away from the symbol's origin (as imported symbols can) /// keeps the center of the box its art fills when Reset Transformation turns it back. #[test] fn reset_transformation_keeps_the_center_of_art_away_from_the_origin() { let mut s = session(); let r = rect(&mut s, 300.0, 10.0, 20.0, 20.0); let art = node(&s, r); s.execute("edit.clear", &json!({"ids": [r.0]})).unwrap(); let st = s.doc_mut().unwrap(); let d = std::sync::Arc::make_mut(&mut st.doc); d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Off".into(), art: std::sync::Arc::new(art) }); let layer = d.layers[0].id; let id = d.alloc_id(); let xf = vectorcraft_geom::Affine::translate((100.0, 100.0)) * vectorcraft_geom::Affine::rotate(0.5); d.insert(Some(layer), usize::MAX, Node::new(id, NodeKind::SymbolInstance { symbol: "Off".into(), xf })).unwrap(); let measured = |s: &Session| s.doc().unwrap().doc.bounds_of(&[id], false).unwrap(); let before = measured(&s); assert_eq!(s.execute("symbol.instance.resetTransform", &json!({"ids": [id.0]})).unwrap()["ids"], json!([id.0])); let after = measured(&s); assert!((after.center() - before.center()).hypot() < 1e-9, "{after:?} vs {before:?}"); assert!((after.width() - 20.0).abs() < 1e-9 && (after.height() - 20.0).abs() < 1e-9, "{after:?}"); } /// A stored natural size that is not positive (from a damaged file: zero or negative on either /// side) does not flatten or mirror the instance: Reset Transformation gives it the art's own /// size, as drawing the symbol does. #[test] fn reset_transformation_ignores_a_stored_size_that_is_not_positive() { for size in [json!([0.0, 20.0]), json!([-20.0, 20.0]), json!([20.0, -5.0]), json!([-20.0, -20.0])] { let mut s = session(); let r = rect(&mut s, 0.0, 0.0, 20.0, 20.0); let art = node(&s, r); s.execute("edit.clear", &json!({"ids": [r.0]})).unwrap(); let st = s.doc_mut().unwrap(); let d = std::sync::Arc::make_mut(&mut st.doc); d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Flat".into(), art: std::sync::Arc::new(art) }); d.unknown.insert(vectorcraft_doc::SYMBOL_SIZES.to_string(), json!({ "Flat": size })); let layer = d.layers[0].id; let id = d.alloc_id(); let xf = vectorcraft_geom::Affine::translate((100.0, 100.0)) * vectorcraft_geom::Affine::rotate(0.5); d.insert(Some(layer), usize::MAX, Node::new(id, NodeKind::SymbolInstance { symbol: "Flat".into(), xf })).unwrap(); assert_eq!(s.execute("symbol.instance.resetTransform", &json!({"ids": [id.0]})).unwrap()["ids"], json!([id.0]), "{size}"); let after = s.doc().unwrap().doc.bounds_of(&[id], false).unwrap(); assert!((after.width() - 20.0).abs() < 1e-9 && (after.height() - 20.0).abs() < 1e-9, "{size}: {after:?}"); assert!(close_xf(xf_of(&s, id).with_translation(vectorcraft_geom::Vec2::ZERO), vectorcraft_geom::Affine::IDENTITY), "{size}: not mirrored"); } } /// Reset Transformation adds no undo step for an instance already at its symbol's scale or for /// one in perspective, and refuses an id naming no object and objects without an instance, /// changing nothing. #[test] fn reset_transformation_leaves_unchanged_instances_and_refuses_bad_ids() { let mut s = Session::new(); s.execute("file.new", &json!({"width": 1000, "height": 800})).unwrap(); let (inst, name, _) = make_symbol(&mut s); let before = undo_len(&s); assert_eq!(s.execute("symbol.instance.resetTransform", &json!({"ids": [inst.0]})).unwrap()["ids"], json!([])); assert_eq!(undo_len(&s), before, "nothing changed: no undo step"); let plain = rect(&mut s, 0.0, 0.0, 10.0, 10.0); select(&mut s, &[inst]); s.execute("object.rotate", &json!({"angle": 45})).unwrap(); let turned = s.doc().unwrap().doc.clone(); let before = undo_len(&s); for bad in [json!({"ids": [inst.0, 999_999]}), json!({"id": 999_999}), json!({"ids": [plain.0]}), json!({"ids": "x"})] { assert!(s.execute("symbol.instance.resetTransform", &bad).is_err(), "{bad}"); } assert!(*s.doc().unwrap().doc == *turned && undo_len(&s) == before, "the refusals change nothing"); // An instance in perspective is left as it is. s.execute("perspective.grid.preset", &json!({"kind": 2})).unwrap(); let projected = NodeId(s.execute("symbol.place", &json!({"name": name, "x": 500, "y": 350})).unwrap()["id"].as_u64().unwrap()); select(&mut s, &[projected]); s.execute("object.rotate", &json!({"angle": 30})).unwrap(); s.execute("perspective.attach", &json!({"ids": [projected.0], "plane": "right"})).unwrap(); let xf = xf_of(&s, projected); let before = undo_len(&s); assert_eq!(s.execute("symbol.instance.resetTransform", &json!({"ids": [projected.0]})).unwrap()["ids"], json!([])); assert!(undo_len(&s) == before && close_xf(xf_of(&s, projected), xf), "an instance in perspective stays as it was"); // Selected with a turned instance, the instance in perspective still stays as it was, and only // the other one is reset and returned, in one undo step. select(&mut s, &[inst, projected]); assert_eq!(s.execute("symbol.instance.resetTransform", &json!({})).unwrap()["ids"], json!([inst.0])); assert_eq!(undo_len(&s), before + 1, "one undo step"); assert!(close_xf(xf_of(&s, projected), xf), "the instance in perspective stays as it was"); let reset = xf_of(&s, inst).with_translation(vectorcraft_geom::Vec2::ZERO); assert!(close_xf(reset, s.doc().unwrap().doc.symbol_natural_xf(&name)), "the turned instance is reset: {reset:?}"); } /// New Symbol and Duplicate Symbol trim the name they are given, as Symbol Options trims /// `newName`; the trimmed name is the one compared with the symbols there are. #[test] fn new_and_duplicate_symbols_trim_their_names_as_a_rename_does() { let mut s = session(); rect(&mut s, 0.0, 0.0, 10.0, 10.0); assert_eq!(s.execute("symbol.new", &json!({"name": " Box \t"})).unwrap()["name"], "Box"); rect(&mut s, 20.0, 0.0, 10.0, 10.0); assert_eq!(s.execute("symbol.new", &json!({"name": " Box"})).unwrap()["name"], "Box 2"); rect(&mut s, 40.0, 0.0, 10.0, 10.0); assert_eq!(s.execute("symbol.new", &json!({"name": " "})).unwrap()["name"], "New Symbol"); assert_eq!(s.execute("symbol.duplicate", &json!({"name": "Box", "newName": " Logo\t"})).unwrap()["name"], "Logo"); assert_eq!(s.execute("symbol.duplicate", &json!({"name": "Box", "newName": " "})).unwrap()["name"], "Box copy"); let names: Vec = s.doc().unwrap().doc.symbols.iter().map(|x| x.name.clone()).collect(); assert_eq!(names, ["Box", "Box 2", "New Symbol", "Logo", "Box copy"]); } #[test] fn symbol_export_type_defaults_to_movie_clip_and_round_trips() { let mut s = session(); let (_, name, _) = make_symbol(&mut s); let list = s.execute("symbol.list", &json!({})).unwrap(); assert_eq!(list["symbols"][0]["exportType"], "movieClip"); assert!(s.doc().unwrap().doc.unknown.get(vectorcraft_doc::SYMBOL_EXPORT_TYPE).is_none()); s.execute("symbol.options", &json!({"name": name, "exportType": "graphic"})).unwrap(); assert_eq!(s.execute("symbol.list", &json!({})).unwrap()["symbols"][0]["exportType"], "graphic"); assert_eq!(s.doc().unwrap().doc.unknown[vectorcraft_doc::SYMBOL_EXPORT_TYPE][&name], json!("graphic")); let copy = s.execute("symbol.duplicate", &json!({"name": name})).unwrap(); assert_eq!(copy["exportType"], "graphic"); s.execute("symbol.options", &json!({"name": name, "newName": "Renamed", "exportType": "movieClip"})).unwrap(); let list = s.execute("symbol.list", &json!({})).unwrap(); let renamed = list["symbols"].as_array().unwrap().iter().find(|x| x["name"] == "Renamed").unwrap(); assert_eq!(renamed["exportType"], "movieClip"); assert!(s.doc().unwrap().doc.unknown.get(vectorcraft_doc::SYMBOL_EXPORT_TYPE).and_then(|m| m.get("Renamed")).is_none()); assert_eq!(s.doc().unwrap().doc.unknown[vectorcraft_doc::SYMBOL_EXPORT_TYPE]["Box copy"], json!("graphic")); s.execute("symbol.delete", &json!({"name": "Box copy"})).unwrap(); assert!(s.doc().unwrap().doc.unknown.get(vectorcraft_doc::SYMBOL_EXPORT_TYPE).is_none()); rect(&mut s, 0.0, 0.0, 10.0, 10.0); let r = s.execute("symbol.new", &json!({"name": "Icon", "exportType": "graphic"})).unwrap(); assert_eq!(r["exportType"], "graphic"); assert!(s.execute("symbol.new", &json!({"exportType": "bogus"})).is_err()); assert!(s.execute("symbol.options", &json!({"name": "Renamed"})).is_err()); } #[test] fn sprayer_command_creates_a_symbol_set_and_alt_removes() { let mut s = session(); make_symbol(&mut s); s.select(|_, sel| sel.clear()).unwrap(); let pts: Vec = (0..20).map(|i| json!([20.0 + i as f64 * 15.0, 150.0])).collect(); let r = s.execute("symbol.spray", &json!({"points": pts, "radius": 10, "density": 8})).unwrap(); let set = NodeId(r["id"].as_u64().unwrap()); let n = r["count"].as_u64().unwrap() as usize; assert!(n >= 3, "{n}"); let g = node(&s, set); assert_eq!(g.name.as_deref(), Some("Symbol Set")); assert_eq!(g.children().unwrap().len(), n); // Spraying again with the set selected adds to it. s.execute("symbol.spray", &json!({"points": [[200, 250]]})).unwrap(); assert_eq!(node(&s, set).children().unwrap().len(), n + 1); let r = s.execute("symbol.spray", &json!({"points": [[200, 250]], "radius": 60, "alt": true})).unwrap(); assert!(r["count"].as_u64().unwrap() >= 1); assert_eq!(node(&s, set).children().unwrap().len(), n + 1 - r["count"].as_u64().unwrap() as usize); } #[test] fn symbolism_adjustments() { let mut s = session(); let (inst, _, b) = make_symbol(&mut s); let c = b.center(); let at = json!([[c.x, c.y]]); s.execute("symbol.adjust", &json!({"tool": "size", "points": at, "radius": 50, "intensity": 10})).unwrap(); assert!(bounds(&s, inst).width() > b.width() + 1.0); s.execute("symbol.adjust", &json!({"tool": "screen", "points": at, "intensity": 10})).unwrap(); assert!(node(&s, inst).opacity < 1.0); s.execute("symbol.adjust", &json!({"tool": "stain", "points": at, "color": "#ff0000", "intensity": 10})).unwrap(); let f = node(&s, inst).appearance.fill().cloned().unwrap(); assert!(f.opacity > 0.0 && f.paint.color().unwrap().to_hex() == "#ff0000"); s.execute("symbol.adjust", &json!({"tool": "shift", "points": [[c.x, c.y], [c.x + 20.0, c.y]], "intensity": 10})).unwrap(); assert!(bounds(&s, inst).center().x > c.x + 5.0); let before = bounds(&s, inst); s.execute("symbol.adjust", &json!({"tool": "spin", "points": at, "intensity": 10})).unwrap(); assert!(!close(bounds(&s, inst), before, 1e-6), "spin rotates"); let far = s.execute("symbol.adjust", &json!({"tool": "size", "points": [[0, 290]], "radius": 5})).unwrap(); assert_eq!(far["count"], 0); assert!(s.execute("symbol.adjust", &json!({"tool": "nope", "points": at})).is_err()); } /// The Symbol Stainer's `color` with an alpha part (`#rrggbbaa` or `#rgba`) is refused and changes /// nothing, rather than staining with the current fill; `#rrggbb` still stains with it. #[test] fn a_stain_color_with_an_alpha_part_is_refused() { let mut s = session(); let (inst, _, b) = make_symbol(&mut s); let c = b.center(); let at = json!([[c.x, c.y]]); let before = s.doc().unwrap().doc.clone(); let undo = s.doc().unwrap().history.undo.len(); for bad in ["#ff000080", "ff000080", "#f008"] { assert!(s.execute("symbol.adjust", &json!({"tool": "stain", "points": at, "color": bad, "intensity": 10})).is_err(), "{bad}"); } assert_eq!(*s.doc().unwrap().doc, *before); assert_eq!(s.doc().unwrap().history.undo.len(), undo); s.execute("symbol.adjust", &json!({"tool": "stain", "points": at, "color": "#ff0000", "intensity": 10})).unwrap(); let f = node(&s, inst).appearance.fill().cloned().unwrap(); assert_eq!(f.paint.color().map(|c| c.to_hex()).as_deref(), Some("#ff0000")); } /// In a CMYK document the Symbol Stainer's RGB `color` separates through the color settings, as /// `paint.setFill` stores it. #[test] fn symbol_stainer_rgb_color_in_a_cmyk_document_separates_through_the_color_settings() { use vectorcraft_color::{Color, Paint, cms}; let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300, "colorMode": "cmyk"})).unwrap(); let (inst, _, b) = make_symbol(&mut s); let c = b.center(); s.execute("symbol.adjust", &json!({"tool": "stain", "points": [[c.x, c.y]], "color": "#ff0000", "intensity": 10})).unwrap(); let red = cms::active().convert(&Color::rgb(1.0, 0.0, 0.0), cms::Model::Cmyk, cms::active().settings().intent); assert_eq!(node(&s, inst).appearance.fill().map(|f| f.paint.clone()), Some(Paint::solid(red))); } /// In a CMYK document the Symbol Stainer refuses a `keepModel` given as text or as another value that /// is not true or false, and the instance and the undo history stay as they were. Null is read as /// `keepModel` left out. #[test] fn symbol_stainer_refuses_a_keep_model_given_as_text() { use vectorcraft_color::{Color, Paint, cms}; let mut s = Session::new(); s.execute("file.new", &json!({"width": 400, "height": 300, "colorMode": "cmyk"})).unwrap(); let (inst, _, b) = make_symbol(&mut s); let at = json!([[b.center().x, b.center().y]]); let state = |s: &Session| (node(s, inst), s.doc().unwrap().history.undo.len()); let before = state(&s); for keep in [json!("true"), json!(1), json!("false"), json!([true])] { let p = json!({"tool": "stain", "points": at, "color": "#ff0000", "intensity": 10, "keepModel": keep}); let r = s.execute("symbol.adjust", &p); assert!(matches!(&r, Err(EngineError::BadParams { msg, .. }) if msg == "`keepModel` must be true or false"), "{p}: {r:?}"); assert_eq!(state(&s), before, "{p}"); } s.execute("symbol.adjust", &json!({"tool": "stain", "points": at, "color": "#ff0000", "intensity": 10, "keepModel": null})).unwrap(); let red = cms::active().convert(&Color::rgb(1.0, 0.0, 0.0), cms::Model::Cmyk, cms::active().settings().intent); assert_eq!(node(&s, inst).appearance.fill().map(|f| f.paint.clone()), Some(Paint::solid(red))); } /// Place Symbol Instance, Delete Symbol, the Symbol Sprayer and the symbolism tools refuse a number /// or an on/off option given as text or as another value of the wrong kind, naming the key; the /// document and the undo history stay as they were. Numbers, true, false and null still work. #[test] fn symbol_commands_refuse_a_number_or_boolean_given_as_text() { use crate::tests::{not_a_number, not_a_switch, not_refused}; let mut s = session(); let (_, name, b) = make_symbol(&mut s); let at = json!([[b.center().x, b.center().y]]); let mut calls: Vec<_> = [ ("symbol.place", json!({"name": name, "x": "200"}), "x"), ("symbol.place", json!({"name": name, "x": 200, "y": [100]}), "y"), ("symbol.spray", json!({"name": name, "points": at, "radius": "40"}), "radius"), ("symbol.spray", json!({"name": name, "points": at, "density": "5"}), "density"), ("symbol.spray", json!({"name": name, "points": at, "scale": true}), "scale"), ("symbol.adjust", json!({"tool": "size", "points": at, "radius": "40"}), "radius"), ("symbol.adjust", json!({"tool": "size", "points": at, "intensity": "10"}), "intensity"), ] .into_iter() .map(|(cmd, p, key)| not_a_number(cmd, p, key)) .collect(); calls.extend( [ ("symbol.spray", json!({"name": name, "points": at, "alt": "true"}), "alt"), ("symbol.adjust", json!({"tool": "size", "points": at, "alt": 1}), "alt"), ("symbol.delete", json!({"name": name, "expandInstances": "false"}), "expandInstances"), ] .map(|(cmd, p, key)| not_a_switch(cmd, p, key)), ); let wrong = not_refused(&mut s, &calls); assert!(wrong.is_empty(), "not refused: {wrong:#?}"); s.execute("symbol.place", &json!({"name": name, "x": 200, "y": null})).unwrap(); s.execute("symbol.spray", &json!({"name": name, "points": at, "radius": 40, "alt": false})).unwrap(); s.execute("symbol.delete", &json!({"name": name, "expandInstances": true})).unwrap(); assert!(s.doc().unwrap().doc.symbols.is_empty()); } #[test] fn symbol_sprayer_tool_gesture_is_one_undo_step() { let mut s = session(); make_symbol(&mut s); s.select(|_, sel| sel.clear()).unwrap(); let n = s.doc().unwrap().history.undo.len(); let v = ViewInfo::default(); s.select_tool("symbolSprayer", v).unwrap(); let mut evs = vec![PointerEvent::new(PointerKind::Down, 50.0, 200.0)]; for i in 1..15 { evs.push(PointerEvent::new(PointerKind::Drag, 50.0 + i as f64 * 20.0, 200.0)); } evs.push(PointerEvent::new(PointerKind::Up, 350.0, 200.0).with_mods(Mods::default())); for e in evs { s.pointer(&e, v).unwrap(); } assert_eq!(s.doc().unwrap().history.undo.len(), n + 1); let set = s.doc().unwrap().selection.objects[0]; assert!(node(&s, set).children().unwrap().len() >= 3); // The sizer tool grows the sprayed instances. let first = node(&s, set).children().unwrap()[0].clone(); let c = first.geometric_bounds().unwrap().center(); s.select_tool("symbolSizer", v).unwrap(); s.pointer(&PointerEvent::new(PointerKind::Down, c.x, c.y), v).unwrap(); s.pointer(&PointerEvent::new(PointerKind::Up, c.x, c.y), v).unwrap(); let after = s.doc().unwrap().doc.node(first.id).unwrap().geometric_bounds().unwrap(); assert!(after.width() > first.geometric_bounds().unwrap().width()); } // ---------- rendering & persistence ---------- #[test] fn brushed_strokes_and_instances_render() { let mut s = session(); make_symbol(&mut s); let l = line(&mut s); s.execute("brush.apply", &json!({"name": "Chain", "ids": [l.0]})).unwrap(); let doc = s.doc().unwrap().doc.clone(); let img = vectorcraft_render::Renderer::new().render_region(&doc, Rect::new(0.0, 0.0, 400.0, 300.0), 1.0, true); let dark = |x: u32, y: u32| { let p = img.pixel(x, y); (p[0] as u32 + p[1] as u32 + p[2] as u32) < 300 }; // The instance's outline (the rectangle's black stroke) is drawn. assert!((45..=55).any(|y| dark(100, y)), "instance stroke rendered"); // Chain links are thicker than the 1 pt stroke, with gaps. let hits = (50..250).filter(|x| (95..=105).any(|y| dark(*x, y))).count(); assert!(hits > 100, "chain covers most of the line: {hits}"); } #[test] fn brushes_and_symbols_survive_vectorcraft_round_trip() { let mut s = session(); make_symbol(&mut s); s.execute("brush.new", &json!({"type": "calligraphic", "name": "Mine", "params": {"size": 7}})).unwrap(); let l = line(&mut s); s.execute("brush.apply", &json!({"name": "Mine", "ids": [l.0]})).unwrap(); let doc = s.doc().unwrap().doc.clone(); let bytes = vectorcraft_format::save(&doc, false); let back = vectorcraft_format::load(&bytes).unwrap(); assert_eq!(vectorcraft_brush::library(&back), vectorcraft_brush::library(&doc)); assert_eq!(vectorcraft_brush::find(&back, "Mine").map(|b| b.kind.type_id()), Some("calligraphic")); assert_eq!(back.symbols, doc.symbols); assert_eq!(back.unknown.get("symbolSizes"), doc.unknown.get("symbolSizes")); assert_eq!(back.unknown.get(vectorcraft_doc::SYMBOL_REGISTRATION), doc.unknown.get(vectorcraft_doc::SYMBOL_REGISTRATION)); let bl = back.node(l).unwrap(); assert_eq!(bl.appearance.stroke().unwrap().brush.as_deref(), Some("Mine")); // A reloaded document places instances at the natural size. let mut s2 = Session::new(); s2.add_document(back, None); let r = s2.execute("symbol.place", &json!({"name": "Box", "x": 100, "y": 100})).unwrap(); let b = s2.doc().unwrap().doc.node(NodeId(r["id"].as_u64().unwrap())).unwrap().geometric_bounds().unwrap(); assert!(close(b, Rect::new(60.0, 80.0, 140.0, 120.0), 1e-9)); } /// Symbol Options › Registration: place puts the chosen 9-point grid point on (x, y). #[test] fn symbol_registration_places_the_chosen_point_on_xy() { let mut s = session(); rect(&mut s, 0.0, 0.0, 80.0, 40.0); let r = s.execute("symbol.new", &json!({"name": "Corner", "registration": 0})).unwrap(); assert_eq!(r["registration"], 0); assert_eq!(s.execute("symbol.list", &json!({})).unwrap()["symbols"][0]["registration"], 0); // Creating kept the art where it was (top-left still at the origin of the selection). let inst = NodeId(r["id"].as_u64().unwrap()); assert!(close(bounds(&s, inst), Rect::new(0.0, 0.0, 80.0, 40.0), 1e-9), "{:?}", bounds(&s, inst)); let p = NodeId(s.execute("symbol.place", &json!({"name": "Corner", "x": 300, "y": 200})).unwrap()["id"].as_u64().unwrap()); assert!(close(bounds(&s, p), Rect::new(300.0, 200.0, 380.0, 240.0), 1e-9), "{:?}", bounds(&s, p)); assert!(s.execute("symbol.new", &json!({"registration": 9})).is_err()); } /// Symbol Options › 9-slice guides: off by default, stored per symbol, follow rename/duplicate, validated. #[test] fn symbol_nine_slice_guides_round_trip() { let mut s = session(); let (_, name, _) = make_symbol(&mut s); assert!(s.execute("symbol.list", &json!({})).unwrap()["symbols"][0]["nineSlice"].is_null()); assert!(s.doc().unwrap().doc.unknown.get(vectorcraft_doc::SYMBOL_NINE_SLICE).is_none()); assert!(s.execute("symbol.options", &json!({"name": name, "nineSliceGuides": [0.6, 0.2, 0.6, 0.2]})).is_err()); assert!(s.execute("symbol.options", &json!({"name": name, "nineSlice": "yes"})).is_err()); let r = s.execute("symbol.options", &json!({"name": name, "nineSlice": true})).unwrap(); assert_eq!(r["nineSlice"].as_array().unwrap().len(), 4); s.execute("symbol.options", &json!({"name": name, "nineSliceGuides": [0.25, 0.1, 0.25, 0.1]})).unwrap(); let copy = s.execute("symbol.duplicate", &json!({"name": name})).unwrap(); assert_eq!(copy["nineSlice"], json!([0.25, 0.1, 0.25, 0.1])); s.execute("symbol.options", &json!({"name": name, "newName": "Panel"})).unwrap(); let g = s.doc().unwrap().doc.symbol_nine_slice("Panel").unwrap(); assert_eq!((g.left, g.top), (0.25, 0.1)); assert!(s.doc().unwrap().doc.symbol_nine_slice(&name).is_none()); s.execute("symbol.options", &json!({"name": "Panel", "nineSlice": false})).unwrap(); s.execute("symbol.delete", &json!({"name": "Box copy"})).unwrap(); assert!(s.doc().unwrap().doc.unknown.get(vectorcraft_doc::SYMBOL_NINE_SLICE).is_none()); } /// Symbols panel › Select All Unused (#4918): a symbol counts as used when an instance in the art, /// a pattern tile, the art of a brush or a used symbol places it; one placed only inside an unused /// symbol is unused too. Delete Symbol with `names` deletes several in one undo step. #[test] fn symbol_unused_lists_the_symbols_nothing_places_and_delete_takes_several() { let mut s = session(); // A symbol of the selection (a new square at `x` when `x` is given), left placed. let symbol = |s: &mut Session, name: &str, x: Option| { if let Some(x) = x { rect(s, x, 0.0, 10.0, 10.0); } NodeId(s.execute("symbol.new", &json!({"name": name})).unwrap()["id"].as_u64().unwrap()) }; let unused = |s: &mut Session| -> Vec { serde_json::from_value(s.execute("symbol.unused", &json!({})).unwrap()["names"].clone()).unwrap() }; // Placed: its instance stays on the artboard. symbol(&mut s, "Placed", Some(0.0)); // Inner is placed only inside Outer, whose instance stays: both are used. let inner = symbol(&mut s, "Inner", Some(20.0)); s.execute("select.set", &json!({"ids": [inner.0]})).unwrap(); symbol(&mut s, "Outer", None); // Lonely's and Nested's instances go; Nested is placed only inside Lonely. let nested = symbol(&mut s, "Nested", Some(40.0)); s.execute("select.set", &json!({"ids": [nested.0]})).unwrap(); let lonely = symbol(&mut s, "Lonely", None); s.execute("edit.clear", &json!({"ids": [lonely.0]})).unwrap(); // In a brush's art only. let in_brush = symbol(&mut s, "Brushed", Some(60.0)); s.execute("brush.new", &json!({"type": "art", "name": "B", "ids": [in_brush.0]})).unwrap(); s.execute("edit.clear", &json!({"ids": [in_brush.0]})).unwrap(); assert_eq!(unused(&mut s), ["Nested", "Lonely"], "in panel order"); // A placed one becomes unused when its last instance goes. let placed = s.doc().unwrap().doc.layers[0] .children() .unwrap() .iter() .find(|n| matches!(&n.kind, NodeKind::SymbolInstance { symbol, .. } if symbol == "Placed")) .unwrap() .id; s.execute("edit.clear", &json!({"ids": [placed.0]})).unwrap(); assert_eq!(unused(&mut s), ["Placed", "Nested", "Lonely"]); // Delete Symbol takes them all in one undo step, and Undo brings them back. let before = s.doc().unwrap().history.undo.len(); s.execute("symbol.delete", &json!({"names": ["Placed", "Nested", "Lonely"]})).unwrap(); let names = |s: &Session| s.doc().unwrap().doc.symbols.iter().map(|x| x.name.clone()).collect::>(); assert_eq!(names(&s), ["Inner", "Outer", "Brushed"]); assert_eq!(s.doc().unwrap().history.undo.len(), before + 1); assert!(unused(&mut s).is_empty()); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(names(&s), ["Placed", "Inner", "Outer", "Nested", "Lonely", "Brushed"]); // Deleting a symbol and the one placed inside it, in either order, expands every instance // while both definitions are still there: no instance is left without its symbol. for order in [["Inner", "Outer"], ["Outer", "Inner"]] { s.execute("symbol.delete", &json!({"names": order})).unwrap(); let mut dangling = 0; s.doc() .unwrap() .doc .walk(|n| dangling += usize::from(matches!(&n.kind, NodeKind::SymbolInstance { symbol, .. } if symbol == "Inner" || symbol == "Outer"))); assert_eq!(dangling, 0, "{order:?}"); s.execute("edit.undo", &json!({})).unwrap(); } // Bad `names` are refused and change nothing. for bad in [json!({"names": []}), json!({"names": "Placed"}), json!({"names": [1]}), json!({"names": ["Placed", "Nope"]})] { assert!(s.execute("symbol.delete", &bad).is_err(), "{bad}"); } assert_eq!(names(&s).len(), 6); } /// Symbols panel › Sort by Name: the document's symbols in name order, ignoring case, with names /// that differ only in case kept in their order, as one undo step that keeps the current symbol /// and every instance; sorting symbols already in order adds no undo step. #[test] fn symbol_sort_by_name_orders_the_symbols_in_one_undo_step() { let mut s = session(); let names = |s: &mut Session| -> Vec { let list = s.execute("symbol.list", &json!({})).unwrap(); list["symbols"].as_array().unwrap().iter().map(|x| x["name"].as_str().unwrap_or_default().to_string()).collect() }; let current = |s: &mut Session| s.execute("symbol.list", &json!({})).unwrap()["current"].clone(); let made = ["leaf", "Badge", "apple", "Leaf", "Zed"]; for (i, name) in made.into_iter().enumerate() { rect(&mut s, 20.0 * i as f64, 0.0, 10.0, 10.0); s.execute("symbol.new", &json!({"name": name})).unwrap(); } // No current symbol stored: the first one is current. std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).unknown.remove("currentSymbol"); assert_eq!(current(&mut s), "leaf"); let instances = |s: &Session| { let mut v = vec![]; s.doc().unwrap().doc.walk(|n| { if let NodeKind::SymbolInstance { symbol, .. } = &n.kind { v.push((n.id, symbol.clone())); } }); v }; let placed = instances(&s); assert_eq!(placed.len(), 5); let before = s.doc().unwrap().history.undo.len(); s.execute("symbol.sortByName", &json!({})).unwrap(); assert_eq!(names(&mut s), ["apple", "Badge", "leaf", "Leaf", "Zed"]); assert_eq!(current(&mut s), "leaf", "the current symbol stays current"); assert_eq!(instances(&s), placed, "every instance keeps its symbol"); assert_eq!(s.doc().unwrap().history.undo.len(), before + 1, "one undo step"); // Already in order: nothing changes and no undo step is added. let revision = s.doc().unwrap().revision; s.execute("symbol.sortByName", &json!({})).unwrap(); assert_eq!((s.doc().unwrap().history.undo.len(), s.doc().unwrap().revision), (before + 1, revision)); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(names(&mut s), made); assert_eq!(current(&mut s), "leaf"); s.execute("edit.redo", &json!({})).unwrap(); assert_eq!(names(&mut s), ["apple", "Badge", "leaf", "Leaf", "Zed"]); // An explicitly current symbol stays current too. s.execute("edit.undo", &json!({})).unwrap(); s.execute("symbol.setCurrent", &json!({"name": "Zed"})).unwrap(); s.execute("symbol.sortByName", &json!({})).unwrap(); assert_eq!(current(&mut s), "Zed"); // Run as a batch step it sorts as well, and with no symbols it adds no undo step. s.execute("edit.undo", &json!({})).unwrap(); s.execute("command.batch", &json!({"commands": [{"command": "symbol.sortByName", "params": {}}]})).unwrap(); assert_eq!(names(&mut s), ["apple", "Badge", "leaf", "Leaf", "Zed"]); s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap(); let before = s.doc().unwrap().history.undo.len(); s.execute("symbol.sortByName", &json!({})).unwrap(); assert_eq!(s.doc().unwrap().history.undo.len(), before); } /// `cmd::current_symbol` answers what `symbol.list` answers as `current`, and what `rule` (the /// stored name while a symbol has it, else the first of all the names) answers: /// with no symbols, with no stored name, with a stored name that is not first, after the current /// symbol is deleted, with a stored name no symbol has and with a stored value that is not text. #[test] fn current_symbol_is_the_current_symbol_list_answers() { fn rule(d: &Document) -> Option { let names: Vec = d.symbols.iter().map(|s| s.name.clone()).collect(); let stored = d.unknown.get("currentSymbol").and_then(Value::as_str).filter(|n| d.symbols.iter().any(|s| s.name == *n)).map(str::to_string); stored.or_else(|| names.into_iter().next()) } fn check(s: &mut Session, want: Option<&str>, case: &str) { let listed = s.execute("symbol.list", &json!({})).unwrap()["current"].clone(); let d = &s.doc().unwrap().doc; let helper = crate::cmd::current_symbol(d); assert_eq!(helper.as_deref(), want, "{case}"); assert_eq!(json!(helper), listed, "{case}: symbol.list"); assert_eq!(helper, rule(d), "{case}: the rule"); } fn store(s: &mut Session, v: Value) { std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).unknown.insert("currentSymbol".into(), v); } let mut s = session(); check(&mut s, None, "no symbols"); store(&mut s, json!("Gone")); check(&mut s, None, "no symbols and a stored name"); for (i, name) in ["A", "B", "C"].into_iter().enumerate() { rect(&mut s, 20.0 * i as f64, 0.0, 10.0, 10.0); s.execute("symbol.new", &json!({"name": name})).unwrap(); } check(&mut s, Some("C"), "the newest symbol is current"); std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).unknown.remove("currentSymbol"); check(&mut s, Some("A"), "no stored name"); s.execute("symbol.setCurrent", &json!({"name": "B"})).unwrap(); check(&mut s, Some("B"), "a stored name that is not first"); s.execute("symbol.delete", &json!({"name": "B"})).unwrap(); check(&mut s, Some("A"), "the current symbol deleted"); store(&mut s, json!("Renamed")); check(&mut s, Some("A"), "a stored name no symbol has"); store(&mut s, json!(3)); check(&mut s, Some("A"), "a stored number"); store(&mut s, json!(["C"])); check(&mut s, Some("A"), "a stored list"); store(&mut s, json!("C")); check(&mut s, Some("C"), "a stored name that is last"); }