Merge main into #8336

# Conflicts:
#	crates/ui-egui/src/i18n/bg.tsv
#	crates/ui-egui/src/i18n/bs.tsv
#	crates/ui-egui/src/i18n/ca.tsv
#	crates/ui-egui/src/i18n/cs.tsv
#	crates/ui-egui/src/i18n/da.tsv
#	crates/ui-egui/src/i18n/de.tsv
#	crates/ui-egui/src/i18n/el.tsv
#	crates/ui-egui/src/i18n/es.tsv
#	crates/ui-egui/src/i18n/fi.tsv
#	crates/ui-egui/src/i18n/fr.tsv
#	crates/ui-egui/src/i18n/gu.tsv
#	crates/ui-egui/src/i18n/hi.tsv
#	crates/ui-egui/src/i18n/hu.tsv
#	crates/ui-egui/src/i18n/id.tsv
#	crates/ui-egui/src/i18n/it.tsv
#	crates/ui-egui/src/i18n/ja.tsv
#	crates/ui-egui/src/i18n/ka.tsv
#	crates/ui-egui/src/i18n/ko.tsv
#	crates/ui-egui/src/i18n/nl.tsv
#	crates/ui-egui/src/i18n/pl.tsv
#	crates/ui-egui/src/i18n/pt-br.tsv
#	crates/ui-egui/src/i18n/ru.tsv
#	crates/ui-egui/src/i18n/sv.tsv
#	crates/ui-egui/src/i18n/tr.tsv
#	crates/ui-egui/src/i18n/uk.tsv
#	crates/ui-egui/src/i18n/vi.tsv
#	crates/ui-egui/src/i18n/zh-hans.tsv
#	crates/ui-egui/src/i18n/zh-hant.tsv
This commit is contained in:
bflatastic
2026-11-20 06:59:22 +07:00
161 changed files with 10373 additions and 772 deletions
+53
View File
@@ -115,6 +115,7 @@ fn the_builtin_libraries_are_original_brushes_generated_in_code() {
"paint",
"pattern-borders",
"decorative",
"ornamental",
"scatter-shapes",
"stipple",
"game-trails",
@@ -197,6 +198,7 @@ fn every_builtin_library_brush_has_its_kind_and_draws() {
("paint", "art"),
("pattern-borders", "pattern"),
("decorative", "pattern"),
("ornamental", "art"),
("scatter-shapes", "scatter"),
("stipple", "scatter"),
("game-trails", "art"),
@@ -661,6 +663,57 @@ fn decorative_brushes_repeat_distinct_art_and_survive_a_saved_library() {
assert_eq!(vectorcraft_brush::library(&reopened), lib.brushes);
}
#[test]
fn ornamental_brushes_draw_flourishes_and_survive_a_saved_library() {
let mut s = session();
let (_, lib) = cmd::brushlib::library(&s, "Ornamental").unwrap();
let names = ["Scrollwork", "Filigree Arrow", "Petal Sweep", "Twin Curl"];
assert_eq!(lib.brushes.iter().map(|b| b.name.as_str()).collect::<Vec<_>>(), names);
let path = run(&mut s, "shape.line", json!({"x1": 10, "y1": 40, "x2": 190, "y2": 40}))["id"].as_u64().unwrap();
let mut curve = line_to(20.0);
curve.curve_to((60.0, -25.0), (140.0, 25.0), (180.0, 0.0));
for b in &lib.brushes {
let BrushKind::Art(art) = &b.kind else { panic!("{} is not an art brush", b.name) };
assert_eq!(art.colorization, vectorcraft_brush::Colorization::Tints);
assert_eq!(art.width, 300.0);
for line in [line_to(2.0), line_to(180.0), curve.clone()] {
let pieces = vectorcraft_brush::stroke_pieces(&lib.doc, b, &line, &black_stroke());
let points = anchors(&pieces.iter().collect::<Vec<_>>());
assert!(!points.is_empty() && points.iter().all(|p| p.x.is_finite() && p.y.is_finite()), "{}", b.name);
}
let before = undo_len(&s);
let added = run(&mut s, "brush.addFromLibrary", json!({"library": "ornamental", "name": b.name, "apply": true, "ids": [path]}));
assert_eq!(strs(&added["added"]), [b.name.as_str()]);
assert_eq!(added["applied"], json!([path]));
assert_eq!(undo_len(&s), before + 1);
assert_eq!(doc(&s).node(NodeId(path)).unwrap().appearance.stroke().unwrap().brush.as_deref(), Some(b.name.as_str()));
}
let saved = run(&mut s, "brush.saveLibrary", json!({"names": names}));
let bytes = vectorcraft_format::base64_decode(saved["dataBase64"].as_str().unwrap()).unwrap();
let reopened = vectorcraft_format::load(&bytes).unwrap();
assert_eq!(vectorcraft_brush::library(&reopened), lib.brushes);
}
/// Filigree Arrow's shaft runs into its head with no gap, so a straight stroke draws one arrow.
#[test]
fn the_filigree_arrow_shaft_meets_its_head() {
let s = session();
let (_, lib) = cmd::brushlib::library(&s, "Ornamental").unwrap();
let BrushKind::Art(art) = &lib.brush("Filigree Arrow").unwrap().kind else { panic!("Filigree Arrow is not an art brush") };
let mut parts = vec![];
art.art.walk(&mut |c| {
if c.path_data().is_some() {
parts.push(c.contains_fn());
}
});
for y in [-0.6, 0.0, 0.6] {
for i in 0..=930 {
let p = vectorcraft_geom::Point::new(4.05 + 0.1 * f64::from(i), y);
assert!(parts.iter().any(|inside| inside(p)), "gap at {p:?}");
}
}
}
#[test]
fn pressure_sensitive_brushes_widen_with_pen_pressure_and_survive_a_saved_library() {
let mut s = session();
+454
View File
@@ -4283,6 +4283,353 @@ fn live_paint_group_stroke_panel_edits_change_its_edges() {
assert_eq!(node(&s, g).appearance.stroke().map(|st| st.width), Some(1.0));
}
/// A face or edge selected before its Live Paint group counts with the group: the Fill proxy shows
/// the faces' fill and the Stroke panel the edges' stroke, whatever the order of the selection.
#[test]
fn live_paint_group_proxies_do_not_depend_on_the_selection_order() {
let mut s = session();
let g = live(&mut s);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ff00ff"})).unwrap();
s.execute("stroke.set", &json!({"weight": 6})).unwrap();
let magenta = serde_json::to_value(Paint::solid(Color::from_hex("#ff00ff").unwrap())).unwrap();
let edge = vectorcraft_tools::builder::edges(&node(&s, g))[0].id;
s.execute("select.set", &json!({"ids": [edge.0, g.0]})).unwrap();
assert_eq!(s.execute("paint.proxies", &json!({})).unwrap()["fill"], magenta, "an edge first shows the faces' fill");
// A face with a stroke of its own (from a file, say), selected first, shows the edges' stroke.
let face = vectorcraft_tools::builder::faces(&node(&s, g))[0].id;
s.edit("t", |d, _| {
let n = d.node_mut(face).unwrap();
n.appearance.set_stroke(Paint::solid(Color::BLACK));
n.appearance.stroke_at_mut(None).unwrap().width = 2.0;
Ok(())
})
.unwrap();
s.execute("select.set", &json!({"ids": [face.0, g.0]})).unwrap();
assert_eq!(s.shown_stroke().map(|st| st.width), Some(6.0), "a face first shows the edges' stroke");
}
/// The Eyedropper onto a whole selected Live Paint group, or reversed onto a clicked one, gives the
/// picked fill attributes to its faces and the picked stroke attributes to its edges; its hidden
/// source paths keep their paint.
#[test]
fn live_paint_group_eyedropper_gives_the_fill_to_faces_and_the_stroke_to_edges() {
for reverse in [false, true] {
let mut s = session();
let g = live(&mut s);
let src = rect(&mut s, 400.0, 0.0, 50.0, 50.0);
s.execute("paint.setFill", &json!({"ids": [src.0], "color": "#ff00ff"})).unwrap();
s.execute("paint.setStroke", &json!({"ids": [src.0], "color": "#0000ff"})).unwrap();
s.execute("stroke.set", &json!({"ids": [src.0], "weight": 3})).unwrap();
let (faces0, edges0) = part_looks(&s, g);
let sources0 = sources_looks(&s, g);
if reverse {
s.execute("select.set", &json!({"ids": [src.0]})).unwrap();
s.execute("appearance.copyFrom", &json!({"source": g.0, "reverse": true})).unwrap();
} else {
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("appearance.copyFrom", &json!({"source": src.0})).unwrap();
}
let (faces1, edges1) = part_looks(&s, g);
let magenta = Paint::solid(Color::from_hex("#ff00ff").unwrap());
let blue = Paint::solid(Color::from_hex("#0000ff").unwrap());
for (f, f0) in faces1.iter().zip(&faces0) {
assert_eq!(f.fill_paint(), magenta, "reverse {reverse}: a face takes the fill");
assert_eq!((f.stroke_paint(), f.stroke_width()), (f0.stroke_paint(), f0.stroke_width()), "reverse {reverse}: a face takes no stroke");
}
for (e, e0) in edges1.iter().zip(&edges0) {
assert_eq!((e.stroke_paint(), e.stroke_width()), (blue.clone(), 3.0), "reverse {reverse}: an edge takes the stroke");
assert_eq!(e.fill_paint(), e0.fill_paint(), "reverse {reverse}: an edge takes no fill");
}
assert_eq!(sources_looks(&s, g), sources0, "reverse {reverse}: the hidden source paths keep their paint");
}
}
/// A whole-appearance Eyedropper pick of a source with several fills and strokes gives each face
/// of a selected Live Paint group every fill row and each edge every stroke row.
#[test]
fn live_paint_group_eyedropper_gives_every_fill_and_stroke_row() {
let mut s = session();
let g = live(&mut s);
let src = rect(&mut s, 400.0, 0.0, 50.0, 50.0);
s.execute("paint.setFill", &json!({"ids": [src.0], "color": "#ff00ff"})).unwrap();
s.execute("paint.setStroke", &json!({"ids": [src.0], "color": "#0000ff"})).unwrap();
s.execute("appearance.addFill", &json!({"ids": [src.0]})).unwrap();
s.execute("appearance.addStroke", &json!({"ids": [src.0]})).unwrap();
let kinds = |a: &vectorcraft_doc::Appearance, fill: bool| a.items.iter().filter(|it| it.is_fill() == fill).count();
let source = (*node(&s, src).appearance).clone();
assert_eq!((kinds(&source, true), kinds(&source, false)), (2, 2));
// A face with a stroke row below its fill keeps that row below the picked fills.
let face = vectorcraft_tools::builder::faces(&node(&s, g))[0].id;
s.edit("t", |d, _| {
let n = d.node_mut(face).unwrap();
n.appearance.items.insert(0, vectorcraft_doc::AppearanceItem::Stroke(vectorcraft_doc::StrokeLayer::new(Paint::solid(Color::BLACK), 9.0)));
Ok(())
})
.unwrap();
let (faces0, edges0) = part_looks(&s, g);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("appearance.copyFrom", &json!({"source": src.0})).unwrap();
let (faces1, edges1) = part_looks(&s, g);
let order: Vec<bool> = faces1[0].items.iter().map(|it| it.is_fill()).collect();
assert_eq!(order.first(), Some(&false), "the face's lower stroke row stays below its fills: {order:?}");
for (f, f0) in faces1.iter().zip(&faces0) {
assert_eq!(kinds(f, true), 2, "a face takes every fill row");
assert_eq!(kinds(f, false), kinds(f0, false), "a face keeps its strokes");
}
for (e, e0) in edges1.iter().zip(&edges0) {
assert_eq!(kinds(e, false), 2, "an edge takes every stroke row");
assert_eq!(kinds(e, true), kinds(e0, true), "an edge keeps its fills");
}
}
/// The Appearance panel's edits of a Live Paint group's contents (`target: "contents"`) follow the
/// group's rule: a new fill goes on its faces, a new stroke on its edges, and its hidden source paths
/// keep their appearance under every contents edit.
#[test]
fn live_paint_group_contents_edits_follow_the_group_rule() {
let mut s = session();
let g = live(&mut s);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
let (faces0, edges0) = part_looks(&s, g);
let sources0 = sources_looks(&s, g);
s.execute("appearance.addFill", &json!({"target": "contents"})).unwrap();
let (faces1, edges1) = part_looks(&s, g);
assert!(faces1.iter().zip(&faces0).all(|(f, f0)| f.items.len() == f0.items.len() + 1), "every face takes a new fill");
assert_eq!(edges1, edges0, "edges take no new fill");
s.execute("appearance.addStroke", &json!({"target": "contents"})).unwrap();
let (faces2, edges2) = part_looks(&s, g);
assert_eq!(faces2, faces1, "faces take no new stroke");
assert!(edges2.iter().zip(&edges1).all(|(e, e0)| e.items.len() == e0.items.len() + 1), "every edge takes a new stroke");
// A stroke row edit of the contents reaches the edges, not the faces.
s.execute("stroke.set", &json!({"target": "contents", "item": 0, "weight": 7})).unwrap();
let (faces3, edges3) = part_looks(&s, g);
assert_eq!(faces3, faces2, "faces keep their rows");
assert!(edges3.iter().all(|e| e.stroke_at(Some(0)).map(|st| st.width) == Some(7.0)), "every edge's stroke row takes the weight");
s.execute("appearance.clear", &json!({"target": "contents"})).unwrap();
assert_eq!(sources_looks(&s, g), sources0, "the hidden source paths keep their appearance");
}
/// Fill edits of a Live Paint group's contents with an `item` (a gradient vector, Invert, Overprint
/// Fill) change its faces and leave an edge's fill row as it is, and Overprint on the whole group goes
/// to the faces' fills and the edges' strokes, leaving the hidden source paths as they are.
#[test]
fn live_paint_group_item_edits_and_overprint_follow_the_group_rule() {
let mut s = session();
let g = live(&mut s);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("paint.setFill", &json!({"gradient": {}})).unwrap();
// Each edge gets a copy of a face's gradient fill row, below its stroke.
let n = node(&s, g);
let face_row = vectorcraft_tools::builder::faces(&n)[0].appearance.items[0].clone();
let edge_ids: Vec<NodeId> = vectorcraft_tools::builder::edges(&n).iter().map(|e| e.id).collect();
let mut face_ids: Vec<u64> = vectorcraft_tools::builder::faces(&n).iter().map(|f| f.id.0).collect();
face_ids.sort_unstable();
s.edit("t", |d, _| {
for e in &edge_ids {
d.node_mut(*e).unwrap().appearance.items.insert(0, face_row.clone());
}
Ok(())
})
.unwrap();
let (faces0, edges0) = part_looks(&s, g);
let sources0 = sources_looks(&s, g);
let r = s.execute("paint.setGradientGeom", &json!({"stroke": false, "item": 0, "target": "contents", "start": [0, 0], "end": [100, 0]})).unwrap();
let mut changed: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect();
changed.sort_unstable();
assert_eq!(changed, face_ids, "only the faces take the vector");
assert_eq!(part_looks(&s, g).1, edges0, "an edge's fill row keeps its vector");
let stops = |a: &vectorcraft_doc::Appearance| match a.fill_paint() {
Paint::Gradient(gr) => gr.gradient.stops.iter().map(|st| st.color).collect::<Vec<_>>(),
_ => vec![],
};
let before: Vec<_> = part_looks(&s, g).0.iter().map(stops).collect();
s.execute("paint.invert", &json!({"stroke": false, "item": 0, "target": "contents"})).unwrap();
for (f, b) in part_looks(&s, g).0.iter().zip(&before) {
assert_eq!(stops(f), b.iter().rev().copied().collect::<Vec<_>>(), "the black-to-white stops of a face's fill are inverted");
}
assert_ne!(part_looks(&s, g).0, faces0);
assert_eq!(part_looks(&s, g).1, edges0, "an edge's fill row is not inverted");
s.execute("object.setOverprint", &json!({"fill": true, "item": 0, "target": "contents"})).unwrap();
let (faces1, edges1) = part_looks(&s, g);
assert!(faces1.iter().all(|f| f.items[0].overprint()), "the faces' fill overprints");
assert_eq!(edges1, edges0, "an edge's fill row does not overprint");
s.execute("object.setOverprint", &json!({"fill": true, "stroke": true})).unwrap();
let (_, edges2) = part_looks(&s, g);
assert!(edges2.iter().all(|e| e.items.iter().all(|it| it.overprint() != it.is_fill())), "only the edges' strokes overprint");
assert_eq!(sources_looks(&s, g), sources0, "the hidden source paths keep their flags");
}
/// A face selected before an ordinary group that holds its Live Paint group counts with that group:
/// the Stroke panel shows the edges' stroke.
#[test]
fn live_paint_group_inside_a_selected_group_keeps_the_proxies_independent_of_order() {
let mut s = session();
let g = live(&mut s);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("stroke.set", &json!({"weight": 6})).unwrap();
let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
let face = vectorcraft_tools::builder::faces(&node(&s, g))[0].id;
s.edit("t", |d, _| {
let n = d.node_mut(face).unwrap();
n.appearance.set_stroke(Paint::solid(Color::BLACK));
n.appearance.stroke_at_mut(None).unwrap().width = 2.0;
Ok(())
})
.unwrap();
s.execute("select.set", &json!({"ids": [face.0, outer.0]})).unwrap();
assert_eq!(s.shown_stroke().map(|st| st.width), Some(6.0), "a face first shows the edges' stroke");
assert!(!s.active().unwrap().stroke_mixed().weight, "the face's own stroke does not make the weight mixed");
// An unfilled edge selected with the outer group does not make the faces' fill mixed.
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap();
let edge = vectorcraft_tools::builder::edges(&node(&s, g))[0].id;
s.execute("select.set", &json!({"ids": [edge.0, outer.0]})).unwrap();
let r = s.execute("paint.proxies", &json!({})).unwrap();
assert_eq!(r["fill"], serde_json::to_value(Paint::solid(Color::from_hex("#ff0000").unwrap())).unwrap());
assert_eq!(r["fillMixed"], false, "the edge counts with its group");
}
/// The Eyedropper picks up a whole Live Paint group as its faces' fill under its edges' stroke, and
/// `append` onto a whole group adds the picked fills and strokes without the picked effects.
#[test]
fn live_paint_group_eyedropper_picks_up_the_group_and_appends_without_effects() {
let mut s = session();
let g = live(&mut s);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ff00ff"})).unwrap();
s.execute("paint.setStroke", &json!({"color": "#0000ff"})).unwrap();
s.execute("stroke.set", &json!({"weight": 5})).unwrap();
s.edit("t", |d, _| {
d.node_mut(g).unwrap().opacity = 0.5;
Ok(())
})
.unwrap();
let target = rect(&mut s, 400.0, 0.0, 50.0, 50.0);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("appearance.copyFrom", &json!({"source": target.0, "reverse": true})).unwrap();
let a = (*node(&s, target).appearance).clone();
assert_eq!(a.fill_paint(), Paint::solid(Color::from_hex("#ff00ff").unwrap()), "the faces' fill");
assert_eq!((a.stroke_paint(), a.stroke_width()), (Paint::solid(Color::from_hex("#0000ff").unwrap()), 5.0), "the edges' stroke");
assert_eq!(node(&s, target).opacity, 0.5, "the group's own opacity");
// A Live Paint group with no face and no edge is not picked up: the next selected object is.
let empty = rect(&mut s, 600.0, 0.0, 50.0, 50.0);
s.execute("select.set", &json!({"ids": [empty.0]})).unwrap();
let e = NodeId(s.execute("livePaint.make", &json!({})).unwrap()["id"].as_u64().unwrap());
let en = node(&s, e);
let parts: Vec<NodeId> =
vectorcraft_tools::builder::faces(&en).iter().chain(vectorcraft_tools::builder::edges(&en).iter()).map(|p| p.id).collect();
assert!(!parts.is_empty());
s.edit("t", |d, _| {
d.node_mut(e).unwrap().children_mut().unwrap().retain(|c| !parts.contains(&c.id));
Ok(())
})
.unwrap();
assert!(vectorcraft_tools::builder::is_live_paint(&node(&s, e)));
let other = rect(&mut s, 700.0, 0.0, 50.0, 50.0);
s.execute("paint.setFill", &json!({"ids": [other.0], "color": "#00ff00"})).unwrap();
s.execute("select.set", &json!({"ids": [e.0, other.0]})).unwrap();
s.execute("appearance.copyFrom", &json!({"source": target.0, "reverse": true})).unwrap();
assert_eq!(node(&s, target).appearance.fill_paint(), Paint::solid(Color::from_hex("#00ff00").unwrap()), "the next selected object's fill");
// Append a source with a drop shadow onto the whole group.
let src = rect(&mut s, 500.0, 0.0, 50.0, 50.0);
s.execute("select.set", &json!({"ids": [src.0]})).unwrap();
s.execute("effect.apply", &json!({"effect": "stylize.dropShadow", "item": null})).unwrap();
assert!(!node(&s, src).appearance.effects.is_empty());
let (faces0, edges0) = part_looks(&s, g);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("appearance.copyFrom", &json!({"source": src.0, "append": true})).unwrap();
let (faces1, edges1) = part_looks(&s, g);
for (p, p0) in faces1.iter().chain(&edges1).zip(faces0.iter().chain(&edges0)) {
assert_eq!(p.effects, p0.effects, "a face or edge takes no effect");
}
assert!(faces1.iter().zip(&faces0).all(|(f, f0)| f.items.len() > f0.items.len()), "every face takes the fill");
}
/// Reversed onto a clicked object, an empty Live Paint group first in the selection passes to the next
/// selected Live Paint group, whose faces' fill and edges' stroke are both picked up.
#[test]
fn live_paint_group_eyedropper_reversed_skips_an_empty_group_for_the_next_whole_group() {
let mut s = session();
let g = live(&mut s);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ff00ff"})).unwrap();
s.execute("paint.setStroke", &json!({"color": "#0000ff"})).unwrap();
s.execute("stroke.set", &json!({"weight": 5})).unwrap();
let empty = rect(&mut s, 600.0, 0.0, 50.0, 50.0);
s.execute("select.set", &json!({"ids": [empty.0]})).unwrap();
let e = NodeId(s.execute("livePaint.make", &json!({})).unwrap()["id"].as_u64().unwrap());
let en = node(&s, e);
let parts: Vec<NodeId> =
vectorcraft_tools::builder::faces(&en).iter().chain(vectorcraft_tools::builder::edges(&en).iter()).map(|p| p.id).collect();
s.edit("t", |d, _| {
d.node_mut(e).unwrap().children_mut().unwrap().retain(|c| !parts.contains(&c.id));
Ok(())
})
.unwrap();
let target = rect(&mut s, 400.0, 0.0, 50.0, 50.0);
s.execute("appearance.copyFrom", &json!({"source": target.0, "reverse": true, "ids": [e.0, g.0]})).unwrap();
let a = (*node(&s, target).appearance).clone();
assert_eq!(a.fill_paint(), Paint::solid(Color::from_hex("#ff00ff").unwrap()), "the faces' fill");
assert_eq!((a.stroke_paint(), a.stroke_width()), (Paint::solid(Color::from_hex("#0000ff").unwrap()), 5.0), "the edges' stroke");
}
/// An Eyedropper pick whose source is the selected Live Paint group leaves the group's own faces and
/// edges as they are.
#[test]
fn live_paint_group_eyedropper_does_not_target_its_own_source_group() {
let mut s = session();
let g = live(&mut s);
s.execute("livePaint.fill", &json!({"group": g.0, "point": [25, 50], "color": "#ff0000"})).unwrap();
s.execute("livePaint.fill", &json!({"group": g.0, "point": [75, 50], "color": "#00ff00"})).unwrap();
let before = part_looks(&s, g);
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
s.execute("appearance.copyFrom", &json!({"source": g.0})).unwrap();
assert_eq!(part_looks(&s, g), before);
}
/// A Live Paint group left with only its hidden source paths is still a selection: a stroke edit of its
/// contents changes nothing and leaves the settings for new art as they are.
#[test]
fn live_paint_group_with_only_source_paths_leaves_new_art_alone_for_a_contents_edit() {
let mut s = session();
let g = live(&mut s);
let n = node(&s, g);
let parts: Vec<NodeId> = vectorcraft_tools::builder::faces(&n).iter().chain(vectorcraft_tools::builder::edges(&n).iter()).map(|p| p.id).collect();
s.edit("t", |d, _| {
d.children_mut(Some(g)).unwrap().retain(|c| !parts.contains(&c.id));
Ok(())
})
.unwrap();
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
let (paint0, stroke0) = (s.paint.clone(), s.new_art_stroke());
s.execute("stroke.setAdvanced", &json!({"target": "contents", "item": 0, "arrowScale": [200, 150]})).unwrap();
assert_eq!((s.paint.clone(), s.new_art_stroke()), (paint0, stroke0), "new art keeps its stroke");
}
/// Adding a fill or a stroke to the contents of a Live Paint group left with only its hidden source paths
/// succeeds and changes nothing, while an empty selection is still refused.
#[test]
fn live_paint_group_with_only_source_paths_ignores_add_fill_and_add_stroke_of_its_contents() {
let mut s = session();
let g = live(&mut s);
let n = node(&s, g);
let parts: Vec<NodeId> = vectorcraft_tools::builder::faces(&n).iter().chain(vectorcraft_tools::builder::edges(&n).iter()).map(|p| p.id).collect();
s.edit("t", |d, _| {
d.children_mut(Some(g)).unwrap().retain(|c| !parts.contains(&c.id));
Ok(())
})
.unwrap();
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
let before = node(&s, g);
for cmd in ["appearance.addFill", "appearance.addStroke"] {
s.execute(cmd, &json!({"target": "contents"})).unwrap();
assert_eq!(node(&s, g), before, "{cmd} changes nothing");
}
s.execute("select.none", &json!({})).unwrap();
assert!(s.execute("appearance.addFill", &json!({"target": "contents"})).is_err(), "no selection is still refused");
}
/// A selected Live Paint group with no part of the edited kind is still a selection: a stroke edit
/// of a group without edges, or a fill gradient edit of a group without faces, changes nothing and
/// leaves the settings for new art as they are.
@@ -4304,6 +4651,8 @@ fn live_paint_group_without_parts_of_the_edited_kind_leaves_new_art_alone() {
s.execute("stroke.set", &json!({"weight": 4})).unwrap();
s.execute("stroke.weightUp", &json!({})).unwrap();
s.execute("stroke.setAdvanced", &json!({"arrowScale": [200, 150]})).unwrap();
// A stroke row edit of the group's contents finds no edge either.
s.execute("stroke.set", &json!({"target": "contents", "item": 0, "weight": 9})).unwrap();
assert_eq!((s.paint.clone(), s.new_art_stroke()), (paint0, stroke0), "new art keeps its stroke");
assert_eq!(part_looks(&s, g), looks);
@@ -9280,6 +9629,111 @@ fn image_trace_built_in_presets_cannot_be_saved_over_or_deleted() {
assert_eq!(six["params"], serde_json::to_value(vectorcraft_trace::preset("6 Colors").unwrap()).unwrap());
}
/// Saved tracing presets go out to a .vctracepresets file and come back with their names and
/// settings; a hand-written file in the documented layout imports too.
#[test]
fn image_trace_presets_export_and_import_round_trip() {
let mut s = session();
assert!(s.execute("imageTrace.presets.export", &json!({})).unwrap_err().to_string().contains("no saved tracing presets"));
s.execute("imageTrace.presets.save", &json!({"name": "Poster", "preset": "6 Colors", "params": {"noise": 40, "ignoreWhite": true}})).unwrap();
s.execute("imageTrace.presets.save", &json!({"name": "Ink", "params": {"threshold": 90, "fills": false, "strokes": true}})).unwrap();
let e = s.execute("imageTrace.presets.export", &json!({"names": ["Default"]})).unwrap_err().to_string();
assert!(e.contains("only saved presets are exported"), "{e}");
assert!(s.execute("imageTrace.presets.export", &json!({"names": ["Nope"]})).is_err());
let all = s.execute("imageTrace.presets.export", &json!({})).unwrap();
assert_eq!(all["count"], 2);
let one = s.execute("imageTrace.presets.export", &json!({"names": ["poster", "Poster"]})).unwrap();
assert_eq!(one["count"], 1);
let file: Value = serde_json::from_str(all["data"].as_str().unwrap()).unwrap();
assert_eq!((file["format"].as_str(), file["version"].as_u64()), (Some("vctracepresets"), Some(1)));
let saved = s.prefs.trace_presets.clone();
// Into a session without them: the same names and settings, and a new revision the panel follows.
let mut t = session();
let before = t.trace_presets_revision();
let r = t.execute("imageTrace.presets.import", &json!({"data": all["data"]})).unwrap();
assert_eq!(r, json!({"imported": ["Poster", "Ink"], "replaced": [], "skipped": []}));
assert_eq!(t.prefs.trace_presets, saved);
assert_eq!(t.trace_presets_revision(), before + 2);
// A file written by hand in the documented layout: missing settings are Default's.
let hand = r#"{"format": "vctracepresets", "version": 1, "presets": [{"name": "Hand", "params": {"mode": "grayscale", "colors": 12}}]}"#;
t.execute("imageTrace.presets.import", &json!({ "dataBase64": vectorcraft_format::base64_encode(hand.as_bytes()) })).unwrap();
let hand = t.trace_preset("hand").unwrap().params.clone();
assert_eq!((hand.mode, hand.colors, hand.threshold), (vectorcraft_trace::Mode::Grayscale, 12, 128));
}
/// Export writes no file that import would refuse (more than 1,000 presets, or more than 4 MiB),
/// and no more than 1,000 tracing presets can be saved.
#[test]
fn image_trace_presets_export_keeps_to_what_import_reads() {
let mut s = session();
let params = vectorcraft_trace::preset("Default").unwrap();
let preset = |name: String| crate::cmd::TracePreset { name, params: params.clone() };
s.prefs.trace_presets = (1..=1001).map(|i| preset(format!("P{i}"))).collect();
let e = s.execute("imageTrace.presets.export", &json!({})).unwrap_err().to_string();
assert!(e.contains("more than one file holds (1000)"), "{e}");
s.prefs.trace_presets.pop();
let all = s.execute("imageTrace.presets.export", &json!({})).unwrap();
assert_eq!(all["count"], 1000);
let mut t = session();
assert_eq!(t.execute("imageTrace.presets.import", &json!({"data": all["data"]})).unwrap()["imported"].as_array().map(Vec::len), Some(1000));
// No more can be saved or imported then, so every saved preset still fits in one file; a
// saved preset can still change, and a file of saved names still replaces them.
let e = t.execute("imageTrace.presets.save", &json!({"name": "One More"})).unwrap_err().to_string();
assert!(e.contains("delete one first"), "{e}");
t.execute("imageTrace.presets.save", &json!({"name": "P1", "params": {"noise": 9}})).unwrap();
let one = json!({"format": "vctracepresets", "version": 1, "presets": [{"name": "Fresh", "params": {}}]}).to_string();
let e = t.execute("imageTrace.presets.import", &json!({ "data": one })).unwrap_err().to_string();
assert!(e.contains("more than 1000 saved"), "{e}");
let again = json!({"format": "vctracepresets", "version": 1, "presets": [{"name": "P2", "params": {}}]}).to_string();
assert_eq!(t.execute("imageTrace.presets.import", &json!({"data": again, "replace": true})).unwrap()["replaced"], json!(["P2"]));
assert_eq!(t.prefs.trace_presets.len(), 1000);
s.prefs.trace_presets = vec![preset("x".repeat(4 << 20))];
let e = s.execute("imageTrace.presets.export", &json!({})).unwrap_err().to_string();
assert!(e.contains("larger than 4 MiB"), "{e}");
}
/// A saved preset of the same name is skipped unless `replace` is on; a file with a fault, or one
/// too large, changes nothing.
#[test]
fn image_trace_presets_import_checks_the_whole_file_first() {
let mut s = session();
s.execute("imageTrace.presets.save", &json!({"name": "Poster", "params": {"noise": 40}})).unwrap();
let file = |rows: Value| json!({"format": "vctracepresets", "version": 1, "presets": rows}).to_string();
let data = file(json!([{"name": "poster", "params": {"noise": 7}}, {"name": "Fresh", "params": {}}]));
let r = s.execute("imageTrace.presets.import", &json!({ "data": data })).unwrap();
assert_eq!(r, json!({"imported": ["Fresh"], "replaced": [], "skipped": ["poster"]}));
assert_eq!(s.trace_preset("Poster").unwrap().params.noise, 40);
let r = s.execute("imageTrace.presets.import", &json!({"data": data, "replace": true})).unwrap();
assert_eq!(r, json!({"imported": [], "replaced": ["Poster", "Fresh"], "skipped": []}));
assert_eq!(s.trace_preset("Poster").unwrap().params.noise, 7);
assert!(s.execute("imageTrace.presets.import", &json!({"data": data, "replace": "yes"})).is_err());
let saved = s.prefs.trace_presets.clone();
let revision = s.trace_presets_revision();
let bad_files = [
(file(json!([{"name": "New One", "params": {}}, {"name": "[Default]", "params": {}}])), "built-in preset"),
(file(json!([{"name": "custom", "params": {}}])), "built-in preset"),
(file(json!([{"name": "Twice", "params": {}}, {"name": "twice", "params": {}}])), "two presets named"),
(file(json!([{"name": " ", "params": {}}])), "has no name"),
(file(json!([{"name": "No Params"}])), "has no `params`"),
(file(json!([{"name": "Neither", "params": {"fills": false, "strokes": false}}])), "Create needs fills"),
(file(json!([{"name": "Typo", "params": {"nosie": 3}}])), "nosie"),
(json!({"format": "vctracepresets", "version": 2, "presets": []}).to_string(), "newer"),
(json!({"format": "vcflattener", "version": 1, "presets": []}).to_string(), "not a tracing presets file"),
("{".to_string(), "not a tracing presets file"),
(file(Value::Array(vec![json!({"name": "x", "params": {}}); 1001])), "more than 1000"),
(" ".repeat((4 << 20) + 1), "larger than 4 MiB"),
];
for (data, why) in bad_files {
let e = s.execute("imageTrace.presets.import", &json!({ "data": data })).unwrap_err().to_string();
assert!(e.contains(why), "{why}: {e}");
}
let big = vectorcraft_format::base64_encode(&vec![b' '; (4 << 20) + 1]);
assert!(s.execute("imageTrace.presets.import", &json!({ "dataBase64": big })).unwrap_err().to_string().contains("larger than 4 MiB"));
assert!(s.execute("imageTrace.presets.import", &json!({})).is_err());
assert_eq!(s.prefs.trace_presets, saved, "nothing changed");
assert_eq!(s.trace_presets_revision(), revision);
}
/// Saved tracing presets are kept with the preferences, are not a preference key, and survive
/// resetting the preferences.
#[test]
+29
View File
@@ -204,6 +204,35 @@ fn the_circle_is_not_moved_onto_the_pixel_grid() {
assert!(!node(&s, id).extras().pixel_aligned);
}
/// `anchors: "points"` puts an anchor exactly at each point, with one more on each half of a
/// diameter's circle; the circle is a plain path, not pixel-aligned, and one undo step. Any other
/// `anchors` value is an error and draws nothing.
#[test]
fn anchors_at_the_points_puts_an_anchor_at_each_point() {
let mut s = session();
s.execute("document.setup", &json!({"alignNewObjectsToPixelGrid": true})).unwrap();
let undo = undo_len(&s);
let points = [Point::new(0.3, 0.2), Point::new(80.6, 0.1), Point::new(0.4, 60.7)];
let pairs: Vec<Value> = points.iter().map(|p| json!([p.x, p.y])).collect();
let id = id_of(&s.execute(C, &json!({ "points": pairs, "anchors": "points" })).unwrap());
let n = node(&s, id);
let anchors: Vec<Point> = n.path_data().unwrap().subpaths[0].anchors.iter().map(|a| a.p).collect();
assert!(points.iter().all(|p| anchors.contains(p)), "{anchors:?}");
assert!(matches!(n.kind, NodeKind::Path { live: None, .. }), "{:?}", n.kind);
assert!(!n.extras().pixel_aligned);
assert_eq!(undo_len(&s), undo + 1);
let id = id_of(&s.execute(C, &json!({ "points": [[0, 0], [100, 0]], "anchors": "points" })).unwrap());
let anchors: Vec<Point> = node(&s, id).path_data().unwrap().subpaths[0].anchors.iter().map(|a| a.p).collect();
assert_eq!(anchors.len(), 4, "{anchors:?}");
assert_eq!((anchors[0], anchors[2]), (Point::new(0.0, 0.0), Point::new(100.0, 0.0)));
let (count, undo) = (object_count(&s), undo_len(&s));
for bad in [json!("corners"), json!(true), json!(4), Value::Null] {
let e = s.execute(C, &json!({ "points": [[0, 0], [100, 0]], "anchors": bad })).unwrap_err().to_string();
assert!(e.contains("`anchors` must be one of standard, points"), "{e}");
}
assert_eq!((object_count(&s), undo_len(&s)), (count, undo));
}
fn view() -> ViewInfo {
ViewInfo { smart_guides: false, ..ViewInfo::default() }
}
+264
View File
@@ -127,6 +127,25 @@ fn c_flips_and_brackets_size_the_last_sharp_arc() {
assert!(!s.tool_claims_key(ToolKey::Letter('c'), view()));
}
/// The drawing end (where the Quick Pen toolbar sits beside) is the path's last point with the
/// direction it arrives in: none before the first click, no direction for one point, and none
/// once Escape ends the path.
#[test]
fn the_drawing_end_is_the_last_point_and_its_direction() {
let mut s = session();
quick_pen(&mut s);
assert_eq!(s.tool_drawing_end(view()), None);
click(&mut s, 100.0, 100.0);
assert_eq!(s.tool_drawing_end(view()), Some((pt(100.0, 100.0), None)));
click(&mut s, 100.0, 200.0);
let (p, dir) = s.tool_drawing_end(view()).unwrap();
assert_eq!(p, pt(100.0, 200.0));
let dir = dir.unwrap();
assert!(dir.x.abs() < 1e-9 && dir.y > 0.99, "arrives heading down: {dir:?}");
key(&mut s, ToolKey::Escape);
assert_eq!(s.tool_drawing_end(view()), None);
}
/// Alt tapped during a drag draws the piece with the next action: a continuous arc becomes a
/// sharp-corner arc with a corner where it starts, and the path goes on drawing after it.
#[test]
@@ -183,6 +202,251 @@ fn escape_ends_the_path_and_a_drag_places_the_piece() {
assert_eq!(paths(&s).len(), 2);
}
/// The piece a press draws ends where the press is let go, past the last drag too, and a press let
/// go a drag's distance away with no drag in between places it there as well.
#[test]
fn the_piece_ends_where_the_press_is_let_go() {
let mut s = session();
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 150.0, 100.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 180.0, 120.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Up, 250.0, 150.0), v).unwrap();
assert_eq!(only_path(&s).anchors.last().unwrap().p, pt(250.0, 150.0));
let undo = undo_steps(&s);
s.pointer(&PointerEvent::new(PointerKind::Down, 300.0, 100.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Up, 340.0, 100.0), v).unwrap();
assert_eq!(only_path(&s).anchors.last().unwrap().p, pt(340.0, 100.0));
assert_eq!(undo_steps(&s), undo + 1);
}
/// C and `[` `]` during a drag change the sharp-corner arc being dragged, within the press's one
/// undo step.
#[test]
fn c_and_brackets_during_a_drag_change_the_arc_being_dragged() {
let mut s = session();
quick_pen(&mut s);
s.set_tool_option_cmd(&json!({"tool": "quickPen", "key": "action", "value": "sharpArc"})).unwrap();
click(&mut s, 100.0, 300.0);
let undo = undo_steps(&s);
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 280.0, 300.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 300.0, 300.0), v).unwrap();
assert!(s.tool_claims_key(ToolKey::Letter('c'), view()));
key(&mut s, ToolKey::Letter('c'));
assert!(only_path(&s).anchors.get(1).unwrap().p.distance(pt(200.0, 400.0)) < 1e-9, "flipped while dragging");
key(&mut s, ToolKey::BracketLeft);
assert_eq!(s.tool_options()["turn"], 165.0);
// A 165° arc is two segments, and its middle point lies the arc's sagitta below the chord
// (a 180° arc's lies 100 below).
let half = 82.5f64.to_radians();
let mid_165 = pt(200.0, 300.0 + 100.0 / half.sin() * (1.0 - half.cos()));
assert_eq!(only_path(&s).anchors.len(), 3);
assert!(only_path(&s).anchors.get(1).unwrap().p.distance(mid_165) < 1e-6, "the dragged arc is 165°");
s.pointer(&PointerEvent::new(PointerKind::Up, 300.0, 300.0), v).unwrap();
let sp = only_path(&s);
assert_eq!(sp.anchors.len(), 3);
assert!(sp.anchors.get(1).unwrap().p.distance(mid_165) < 1e-6, "the flip and the size stay after the release");
assert_eq!(undo_steps(&s), undo + 1);
}
/// An edit made by another command during a drag ends the path: the next drag does not draw over
/// the edit, the piece stays as last shown, and the next click starts a new path.
#[test]
fn an_edit_during_a_drag_ends_the_path() {
let mut s = session();
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
click(&mut s, 200.0, 100.0);
let id = paths(&s)[0].0;
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 200.0), v).unwrap();
s.execute("path.moveAnchor", &json!({"id": id.0, "anchor": 0, "x": 90, "y": 90})).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 250.0, 250.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Up, 260.0, 260.0), v).unwrap();
let ends: Vec<Point> = only_path(&s).anchors.iter().map(|a| a.p).collect();
assert_eq!(ends, [pt(90.0, 90.0), pt(200.0, 100.0), pt(200.0, 200.0)], "the edit and the piece as last shown");
click(&mut s, 400.0, 400.0);
assert_eq!(paths(&s).len(), 2, "a new path");
}
/// With Real-time Drawing off, a sharp-corner arc let go past the last drag is placed where it was
/// let go, and C then flips that arc.
#[test]
fn a_deferred_arc_let_go_past_the_last_drag_flips() {
let mut s = session();
s.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": false})).unwrap();
quick_pen(&mut s);
s.set_tool_option_cmd(&json!({"tool": "quickPen", "key": "action", "value": "sharpArc"})).unwrap();
click(&mut s, 100.0, 300.0);
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 250.0, 300.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 270.0, 300.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Up, 300.0, 300.0), v).unwrap();
let sp = only_path(&s);
assert_eq!(sp.anchors.len(), 3);
assert_eq!(sp.anchors.last().unwrap().p, pt(300.0, 300.0));
let mid = sp.anchors.get(1).unwrap().p;
key(&mut s, ToolKey::Letter('c'));
let flipped = only_path(&s);
assert_eq!(flipped.anchors.len(), 3);
assert!(flipped.anchors.get(1).unwrap().p.distance(pt(200.0, 600.0 - mid.y)) < 1e-9, "flipped across the chord");
}
/// An edit made by another command to the path's look during a drag ends the path too.
#[test]
fn a_look_edit_during_a_drag_ends_the_path() {
let mut s = session();
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
click(&mut s, 200.0, 100.0);
let id = paths(&s)[0].0;
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 200.0), v).unwrap();
s.execute("paint.setStroke", &json!({"ids": [id.0], "color": "#00ff00"})).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Up, 260.0, 260.0), v).unwrap();
let ends: Vec<Point> = only_path(&s).anchors.iter().map(|a| a.p).collect();
assert_eq!(ends, [pt(100.0, 100.0), pt(200.0, 100.0), pt(200.0, 200.0)], "the piece as last shown");
click(&mut s, 400.0, 400.0);
assert_eq!(paths(&s).len(), 2, "a new path");
}
/// An edit made by another command to another object during a drag stays and ends the path: the
/// press previews no more, its piece stays as last shown, and the next click starts a new path.
#[test]
fn an_edit_to_another_object_during_a_drag_stays() {
let mut s = session();
s.execute("shape.rectangle", &json!({"x": 500, "y": 400, "width": 50, "height": 50})).unwrap();
let rect = s.doc().unwrap().selection.objects[0];
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
click(&mut s, 200.0, 100.0);
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 200.0), v).unwrap();
s.execute("paint.setStroke", &json!({"ids": [rect.0], "color": "#00ff00"})).unwrap();
let edited = s.doc().unwrap().doc.node(rect).unwrap().clone();
s.pointer(&PointerEvent::new(PointerKind::Up, 260.0, 260.0), v).unwrap();
assert_eq!(*s.doc().unwrap().doc.node(rect).unwrap(), edited, "the other object's edit stays");
let pen = paths(&s).into_iter().find(|(id, _)| *id != rect).unwrap().1;
let ends: Vec<Point> = pen.subpaths[0].anchors.iter().map(|a| a.p).collect();
assert_eq!(ends, [pt(100.0, 100.0), pt(200.0, 100.0), pt(200.0, 200.0)], "the piece as last shown");
click(&mut s, 400.0, 400.0);
assert_eq!(paths(&s).len(), 3, "a new path");
}
/// With Real-time Drawing off, a drag during which another command edits the path adds no piece,
/// and the edit stays.
#[test]
fn an_edit_during_a_deferred_drag_stays() {
let mut s = session();
s.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": false})).unwrap();
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
click(&mut s, 200.0, 100.0);
let id = paths(&s)[0].0;
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 200.0), v).unwrap();
s.execute("path.moveAnchor", &json!({"id": id.0, "anchor": 0, "x": 90, "y": 90})).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 250.0, 250.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Up, 260.0, 260.0), v).unwrap();
let ends: Vec<Point> = only_path(&s).anchors.iter().map(|a| a.p).collect();
assert_eq!(ends, [pt(90.0, 90.0), pt(200.0, 100.0)], "the edit, and no piece");
}
/// An edit made by another command to another object during a click stays, and the path goes on:
/// a click previews its piece only when the press begins.
#[test]
fn an_edit_to_another_object_during_a_click_stays() {
let mut s = session();
s.execute("shape.rectangle", &json!({"x": 500, "y": 400, "width": 50, "height": 50})).unwrap();
let rect = s.doc().unwrap().selection.objects[0];
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
click(&mut s, 200.0, 100.0);
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 200.0), v).unwrap();
s.execute("paint.setStroke", &json!({"ids": [rect.0], "color": "#00ff00"})).unwrap();
let edited = s.doc().unwrap().doc.node(rect).unwrap().clone();
s.pointer(&PointerEvent::new(PointerKind::Up, 200.0, 200.0), v).unwrap();
assert_eq!(*s.doc().unwrap().doc.node(rect).unwrap(), edited, "the other object's edit stays");
click(&mut s, 300.0, 200.0);
let all = paths(&s);
assert_eq!(all.len(), 2, "no new path");
let pen = all.into_iter().find(|(id, _)| *id != rect).unwrap().1;
let ends: Vec<Point> = pen.subpaths[0].anchors.iter().map(|a| a.p).collect();
assert_eq!(ends, [pt(100.0, 100.0), pt(200.0, 100.0), pt(200.0, 200.0), pt(300.0, 200.0)]);
}
/// An edit made by another command during a click (a press let go where it went down) ends the
/// path too: the next click starts a new path instead of drawing over the edit.
#[test]
fn an_edit_during_a_click_ends_the_path() {
let mut s = session();
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
click(&mut s, 200.0, 100.0);
let id = paths(&s)[0].0;
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap();
s.execute("path.moveAnchor", &json!({"id": id.0, "anchor": 0, "x": 90, "y": 90})).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Up, 200.0, 180.0), v).unwrap();
click(&mut s, 400.0, 400.0);
assert_eq!(paths(&s).len(), 2, "a new path");
let ends: Vec<Point> = paths(&s)[0].1.subpaths[0].anchors.iter().map(|a| a.p).collect();
assert_eq!(ends, [pt(90.0, 90.0), pt(200.0, 100.0), pt(200.0, 180.0)], "the edit stays");
}
/// With Align New Objects to Pixel Grid on, the path the tool draws is pixel-aligned: its points
/// move onto the grid, and the next click and drag still go on drawing the same path.
#[test]
fn a_pixel_aligned_path_goes_on_drawing() {
let mut s = session();
s.execute("document.setup", &json!({"alignNewObjectsToPixelGrid": true})).unwrap();
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
click(&mut s, 200.0, 100.0);
let ends = |s: &Session| only_path(s).anchors.iter().map(|a| a.p).collect::<Vec<Point>>();
let two = ends(&s);
assert_eq!(two.len(), 2);
assert_eq!(two[0].y, two[1].y, "a level line");
press(&mut s, &[(300.0, 150.0), (310.0, 170.0), (320.0, 200.0)]);
let three = ends(&s);
assert_eq!(three.len(), 3, "one path of three points");
assert_eq!(three[..2], two[..], "the points before the piece stay as they were");
assert!(three[2].distance(pt(320.0, 200.0)) <= 1.0, "the drag's piece ends near where it was let go: {:?}", three[2]);
click(&mut s, 400.0, 100.0);
let four = ends(&s);
assert_eq!(four.len(), 4);
assert_eq!(four[..3], three[..]);
}
/// A dropped press takes back only its own piece, and with Real-time Drawing off a drag places its
/// piece on the release: in both cases the next click goes on drawing the same path.
#[test]
fn a_dropped_press_and_a_deferred_drag_keep_the_path() {
let mut s = session();
quick_pen(&mut s);
click(&mut s, 100.0, 100.0);
click(&mut s, 200.0, 100.0);
let v = view();
s.pointer(&PointerEvent::new(PointerKind::Down, 300.0, 100.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 320.0, 150.0), v).unwrap();
s.drop_tool_press(v).unwrap();
assert_eq!(only_path(&s).anchors.len(), 2, "the dropped piece is gone");
click(&mut s, 300.0, 200.0);
assert_eq!(only_path(&s).anchors.len(), 3, "the path goes on");
s.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": false})).unwrap();
press(&mut s, &[(400.0, 200.0), (420.0, 250.0), (450.0, 300.0)]);
assert_eq!(only_path(&s).anchors.last().unwrap().p, pt(450.0, 300.0));
click(&mut s, 500.0, 100.0);
assert_eq!(only_path(&s).anchors.len(), 5, "the path goes on after the deferred piece");
}
/// An anchor moved by another command ends the path being drawn: C does not flip the old arc back
/// over the edit, and the next click starts a new path instead of rebuilding the old one.
#[test]
+168
View File
@@ -133,6 +133,174 @@ fn colors_with_nothing_selected_leave_out_what_select_all_leaves_out() {
assert_eq!(hexes(&run(&mut s, "recolor.colors", json!({"ids": [hidden.0]}))), ["#0000ff"]);
}
#[test]
fn used_colors_list_the_most_used_first_with_their_colors() {
let mut s = session();
let reds: Vec<NodeId> = (0..3).map(|_| rect(&mut s, json!("#ff0000"))).collect();
let blue = rect(&mut s, json!({"c": 100, "m": 100, "y": 0, "k": 0}));
run(&mut s, "select.set", json!({"ids": []}));
let r = run(&mut s, "color.used", json!({}));
assert_eq!(r["selection"], false, "{r}");
let colors = r["colors"].as_array().unwrap();
assert_eq!(colors.len(), 2, "{r}");
assert_eq!((colors[0]["key"].as_str(), colors[0]["hex"].as_str(), colors[0]["count"].as_u64()), (Some("rgb 255 0 0"), Some("#ff0000"), Some(3)));
assert_eq!(colors[1]["count"], 1);
let first = run(&mut s, "color.used", json!({"limit": 1}));
assert_eq!(first["colors"], json!([colors[0]]), "`limit` keeps the most used");
assert_eq!((r["uses"].clone(), first["uses"].clone()), (json!(4), json!(4)), "`uses` counts every color");
// `color` is the color in its own model, which the paint commands take back.
assert_eq!(colors[1]["color"]["model"], "cmyk", "{r}");
assert!(close(color_value(&colors[1]["color"]).unwrap(), fill(&s, blue)));
// The selection's colors while something is selected; `selection: false` reads all the art.
run(&mut s, "select.set", json!({"ids": [blue.0]}));
let r = run(&mut s, "color.used", json!({}));
assert_eq!((r["selection"].clone(), r["colors"].as_array().unwrap().len()), (json!(true), 1), "{r}");
let all = run(&mut s, "color.used", json!({"selection": false}));
assert_eq!((all["selection"].clone(), all["colors"][0]["count"].clone()), (json!(false), json!(3)), "{all}");
assert_eq!(s.doc().unwrap().selection.objects, [blue], "the selection is left as it was");
// A hidden object's color is left out with nothing selected, as Select All leaves it out.
run(&mut s, "object.hide", json!({"ids": [reds[0].0]}));
assert_eq!(run(&mut s, "color.used", json!({"selection": false}))["colors"][0]["count"], 2);
let e = s.execute("color.used", &json!({"selection": "no"})).unwrap_err().to_string();
assert!(e.contains("`selection` must be true or false"), "{e}");
assert!(s.execute("color.used", &json!({"ids": [blue.0]})).is_err(), "an unknown key is refused");
}
#[test]
fn used_colors_name_the_swatch_and_tint_a_color_is_linked_to() {
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "Ink", "color": {"c": 0, "m": 100, "y": 60, "k": 0}, "spot": true}));
let tinted = rect(&mut s, json!("#000000"));
run(&mut s, "paint.setFill", json!({"ids": [tinted.0], "swatch": "Ink", "tint": 40}));
run(&mut s, "select.set", json!({"ids": []}));
let r = run(&mut s, "color.used", json!({}));
let c = &r["colors"][0];
assert_eq!(c["swatch"].as_str(), Some("Ink"), "{r}");
assert!((c["tint"].as_f64().unwrap() - 40.0).abs() < 1e-4, "{r}");
// recolor.colors keeps its own fields.
assert_eq!(run(&mut s, "recolor.colors", json!({}))["colors"][0].get("tint"), None);
// A swatch of paper white looks the same at every tint; the tint comes from the paint.
run(&mut s, "swatch.new", json!({"name": "Paper", "color": {"c": 0, "m": 0, "y": 0, "k": 0}, "spot": true}));
let paper = rect(&mut s, json!("#000000"));
run(&mut s, "paint.setFill", json!({"ids": [paper.0], "swatch": "Paper", "tint": 33.333}));
run(&mut s, "select.set", json!({"ids": [paper.0]}));
let c = run(&mut s, "color.used", json!({}))["colors"][0].clone();
assert_eq!(c["swatch"].as_str(), Some("Paper"), "{c}");
// The tint is the paint's own, unrounded.
let vectorcraft_color::Paint::Solid { tint, .. } = s.doc().unwrap().doc.node(paper).unwrap().appearance.fill_paint() else {
panic!("a solid fill")
};
assert_eq!((c["tint"].as_f64().unwrap() / 100.0) as f32, tint, "{c}");
assert!((tint - 0.33333).abs() < 1e-5);
// A pattern's tile keeps its tint as well.
run(&mut s, "select.set", json!({"ids": [paper.0]}));
run(&mut s, "object.pattern.make", json!({"name": "Sheet", "width": 20, "height": 20}));
run(&mut s, "object.pattern.done", json!({}));
let filled = rect(&mut s, json!("#000000"));
run(&mut s, "paint.setFill", json!({"ids": [filled.0], "swatch": "Sheet"}));
run(&mut s, "select.set", json!({"ids": [filled.0]}));
let c = run(&mut s, "color.used", json!({}))["colors"][0].clone();
assert_eq!(c["swatch"].as_str(), Some("Paper"), "{c}");
assert_eq!((c["tint"].as_f64().unwrap() / 100.0) as f32, tint, "{c}");
}
#[test]
fn used_colors_read_the_tiles_of_patterns_used_inside_pattern_tiles() {
let mut s = session();
let red = rect(&mut s, json!("#ff0000"));
run(&mut s, "select.set", json!({"ids": [red.0]}));
run(&mut s, "object.pattern.make", json!({"name": "Inner", "width": 20, "height": 20}));
run(&mut s, "object.pattern.done", json!({}));
let middle = rect(&mut s, json!("#000000"));
run(&mut s, "paint.setFill", json!({"ids": [middle.0], "swatch": "Inner"}));
run(&mut s, "select.set", json!({"ids": [middle.0]}));
run(&mut s, "object.pattern.make", json!({"name": "Outer", "width": 20, "height": 20}));
run(&mut s, "object.pattern.done", json!({}));
let square = rect(&mut s, json!("#000000"));
run(&mut s, "paint.setFill", json!({"ids": [square.0], "swatch": "Outer"}));
// The inner tile's square is stroked with the outer pattern, so the two patterns use each other.
let outer = s.doc().unwrap().doc.node(square).unwrap().appearance.fill_paint().clone();
let st = s.doc_mut().unwrap();
let doc = std::sync::Arc::make_mut(&mut st.doc);
let tile = doc.pattern_mut("Inner").unwrap().art.first_mut().unwrap();
std::sync::Arc::make_mut(tile).appearance.set_stroke(outer);
run(&mut s, "select.set", json!({"ids": [square.0]}));
let r = run(&mut s, "color.used", json!({}));
let colors = r["colors"].as_array().unwrap();
assert_eq!(colors.iter().map(|c| (c["hex"].as_str(), c["count"].as_u64())).collect::<Vec<_>>(), [(Some("#ff0000"), Some(1))], "{r}");
}
#[test]
fn used_colors_give_no_swatch_for_a_stop_that_mixes_its_ink_with_process_inks() {
use vectorcraft_color::{Gradient, GradientKind, GradientPaint, GradientStop, Paint};
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "Ink", "color": {"c": 0, "m": 100, "y": 60, "k": 0}, "spot": true}));
let shape = rect(&mut s, json!("#000000"));
let mixed = Color::Cmyk { c: 0.3, m: 0.5, y: 0.3, k: 0.0 };
let stop = |color, plates| GradientStop { swatch: Some("Ink".into()), tint: 0.5, plates, ..GradientStop::new(0.0, color) };
let mut end = stop(Color::Cmyk { c: 0.0, m: 0.5, y: 0.3, k: 0.0 }, None);
end.offset = 1.0;
let g = Gradient::new(GradientKind::Linear, vec![stop(mixed, Some([0.3, 0.0, 0.0, 0.0])), end]);
let st = s.doc_mut().unwrap();
std::sync::Arc::make_mut(&mut st.doc).node_mut(shape).unwrap().appearance.set_fill(Paint::Gradient(Box::new(GradientPaint::new(g))));
let r = run(&mut s, "color.used", json!({}));
let entry = |key: &str| r["colors"].as_array().unwrap().iter().find(|c| c["key"] == key).cloned().unwrap();
let mix = entry("cmyk 30 50 30 0");
assert_eq!((mix.get("swatch"), mix.get("tint")), (None, None), "{r}");
assert_eq!(entry("cmyk 0 50 30 0")["swatch"], "Ink", "{r}");
// recolor.colors keeps the swatch it lists.
let listed = run(&mut s, "recolor.colors", json!({}));
assert!(listed["colors"].as_array().unwrap().iter().all(|c| c["swatch"] == "Ink"), "{listed}");
}
#[test]
fn used_colors_take_the_swatch_of_a_plain_use_painted_after_a_mixed_stop_of_the_same_color() {
use vectorcraft_color::{Gradient, GradientKind, GradientPaint, GradientStop, Paint};
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "Ink", "color": {"c": 0, "m": 100, "y": 60, "k": 0}, "spot": true}));
let shape = rect(&mut s, json!("#000000"));
let plain = rect(&mut s, json!("#000000"));
run(&mut s, "paint.setFill", json!({"ids": [plain.0], "swatch": "Ink", "tint": 50}));
let ink = fill(&s, plain);
// The stop below the plain fill has the same color, and process plates that add nothing.
let stop = GradientStop { swatch: Some("Ink".into()), tint: 0.5, plates: Some([0.0; 4]), ..GradientStop::new(0.0, ink) };
let mut end = stop.clone();
end.offset = 1.0;
let g = Gradient::new(GradientKind::Linear, vec![stop, end]);
let st = s.doc_mut().unwrap();
std::sync::Arc::make_mut(&mut st.doc).node_mut(shape).unwrap().appearance.set_fill(Paint::Gradient(Box::new(GradientPaint::new(g))));
run(&mut s, "select.set", json!({"ids": []}));
let r = run(&mut s, "color.used", json!({}));
let c = r["colors"].as_array().unwrap().iter().find(|c| c["count"] == 3).cloned().unwrap();
assert_eq!((c["swatch"].as_str(), c["tint"].as_f64()), (Some("Ink"), Some(50.0)), "{r}");
}
#[test]
fn recolor_colors_keeps_the_swatch_of_the_first_linked_use_when_a_mixed_stop_comes_first() {
use vectorcraft_color::{Gradient, GradientKind, GradientPaint, GradientStop, Paint};
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "Ink", "color": {"c": 0, "m": 100, "y": 60, "k": 0}, "spot": true}));
run(&mut s, "swatch.new", json!({"name": "Other", "color": {"c": 0, "m": 100, "y": 60, "k": 0}, "spot": true}));
let shape = rect(&mut s, json!("#000000"));
let plain = rect(&mut s, json!("#000000"));
run(&mut s, "paint.setFill", json!({"ids": [plain.0], "swatch": "Other", "tint": 50}));
let ink = fill(&s, plain);
// The stop below the plain fill has the same color, linked to another swatch, with process
// plates that add nothing.
let stop = GradientStop { swatch: Some("Ink".into()), tint: 0.5, plates: Some([0.0; 4]), ..GradientStop::new(0.0, ink) };
let mut end = stop.clone();
end.offset = 1.0;
let g = Gradient::new(GradientKind::Linear, vec![stop, end]);
let st = s.doc_mut().unwrap();
std::sync::Arc::make_mut(&mut st.doc).node_mut(shape).unwrap().appearance.set_fill(Paint::Gradient(Box::new(GradientPaint::new(g))));
run(&mut s, "select.set", json!({"ids": []}));
let three = |r: &Value| r["colors"].as_array().unwrap().iter().find(|c| c["count"] == 3).cloned().unwrap();
let used = run(&mut s, "color.used", json!({}));
assert_eq!(three(&used)["swatch"], "Other", "{used}");
let listed = run(&mut s, "recolor.colors", json!({}));
assert_eq!(three(&listed)["swatch"], "Ink", "recolor.colors names the first linked use: {listed}");
}
#[test]
fn colors_with_nothing_selected_leave_out_hidden_and_locked_objects_in_groups() {
let mut s = session();