mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 04:38:29 +00:00
Merge main into fix/live-shape-replay-negative-radius
This commit is contained in:
@@ -468,6 +468,72 @@ fn styles_symbols_and_images_of_the_same_name_are_renamed() {
|
||||
assert_eq!(key(&s, img2), src_key);
|
||||
}
|
||||
|
||||
/// A graph whose series fill, value labels and legend rows paint with global swatch "Brand" and
|
||||
/// whose value labels are set in character style "Emphasis" and paragraph style "Body", as a
|
||||
/// later redraw would give them to its marks and labels.
|
||||
fn styled_graph(s: &mut Session) -> NodeId {
|
||||
s.execute("swatch.new", &json!({"name": "Brand", "color": "#ff0000", "global": true})).unwrap();
|
||||
s.execute("charStyle.new", &json!({"name": "Emphasis", "attrs": {"size": 24.0}})).unwrap();
|
||||
s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"space_after": 8.0}})).unwrap();
|
||||
let v = s.execute("graph.create", &json!({"type": "column", "x": 200, "y": 200, "width": 60, "height": 40, "csv": ",2024\nQ1,3\nQ2,5"}));
|
||||
let id = id_of(&v.unwrap());
|
||||
let brand = linked(hex("#ff0000"), "Brand", 1.0);
|
||||
let mut extras = doc_mut(s).node_mut(id).unwrap().extras_mut();
|
||||
let g = extras.graph.as_mut().unwrap();
|
||||
g.series_paints = vec![vectorcraft_doc::SeriesPaint { fill: Some(brand.clone()), ..Default::default() }];
|
||||
let l = &mut g.label_styles;
|
||||
l.values = Some(vectorcraft_doc::CharStyle { fill: brand.clone(), style_name: Some("Emphasis".into()), ..Default::default() });
|
||||
l.values_para = Some(vectorcraft_doc::ParaStyle { style_name: Some("Body".into()), ..Default::default() });
|
||||
let rows = vectorcraft_doc::SeriesPaint { stroke: Some(brand), ..Default::default() };
|
||||
l.legend_rows = Some(vectorcraft_doc::graph::LabelRows { paint: rows, ..Default::default() });
|
||||
id
|
||||
}
|
||||
|
||||
/// The swatch names a graph's stored paints link to, and its value labels' character and
|
||||
/// paragraph style names.
|
||||
fn graph_links(s: &Session, id: NodeId) -> (Vec<String>, Option<String>, Option<String>) {
|
||||
let n = node(s, id);
|
||||
let swatches = vectorcraft_doc::swatches::graph_paints(&n).filter_map(|p| match p {
|
||||
Paint::Solid { swatch: Some(w), .. } => Some(w.clone()),
|
||||
_ => None,
|
||||
});
|
||||
let l = &n.extras().graph.as_deref().unwrap().label_styles;
|
||||
let values = l.values.as_ref().and_then(|st| st.style_name.clone());
|
||||
(swatches.collect(), values, l.values_para.as_ref().and_then(|pa| pa.style_name.clone()))
|
||||
}
|
||||
|
||||
/// A pasted graph brings the swatches and text styles its series and labels keep, and follows
|
||||
/// them when they come in under new names.
|
||||
#[test]
|
||||
fn a_pasted_graph_brings_and_follows_its_label_styles_and_paints() {
|
||||
let mut s = session();
|
||||
let g = styled_graph(&mut s);
|
||||
copy(&mut s, &[g]);
|
||||
|
||||
new_doc(&mut s);
|
||||
let v = s.execute("edit.pasteInPlace", &json!({})).unwrap();
|
||||
let d = doc(&s);
|
||||
assert!(d.swatch("Brand").is_some(), "{v}");
|
||||
assert!(d.char_styles.iter().any(|x| x.name == "Emphasis") && d.para_styles.iter().any(|x| x.name == "Body"), "{v}");
|
||||
let brand = vec!["Brand".to_string(); 3];
|
||||
assert_eq!(graph_links(&s, ids_of(&v)[0]), (brand.clone(), Some("Emphasis".into()), Some("Body".into())));
|
||||
|
||||
// Into a document that gives each name something else: they come in renamed.
|
||||
new_doc(&mut s);
|
||||
s.execute("swatch.new", &json!({"name": "Brand", "color": "#0000ff", "global": true})).unwrap();
|
||||
s.execute("charStyle.new", &json!({"name": "Emphasis", "attrs": {"size": 8.0}})).unwrap();
|
||||
s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"space_after": 2.0}})).unwrap();
|
||||
let v = s.execute("edit.pasteInPlace", &json!({"swatchConflict": "add"})).unwrap();
|
||||
let brand2 = vec!["Brand 2".to_string(); 3];
|
||||
assert_eq!(graph_links(&s, ids_of(&v)[0]), (brand2, Some("Emphasis 2".into()), Some("Body 2".into())), "{v}");
|
||||
|
||||
// Without formatting: the labels' text styles stay behind.
|
||||
new_doc(&mut s);
|
||||
let v = s.execute("edit.pasteWithoutFormatting", &json!({})).unwrap();
|
||||
assert!(doc(&s).char_styles.is_empty() && doc(&s).para_styles.is_empty(), "{v}");
|
||||
assert_eq!(graph_links(&s, ids_of(&v)[0]), (brand, None, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pasting_again_reuses_what_an_earlier_paste_added() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -422,6 +422,122 @@ fn a_compound_clipping_path_in_the_art_keeps_its_shape() {
|
||||
assert_eq!(seen, (true, true));
|
||||
}
|
||||
|
||||
/// A Drop Shadow 20 pt to the right on row `row` of instance `id`, solid black with no blur.
|
||||
fn shadow_row(s: &mut Session, id: NodeId, row: usize) {
|
||||
let params = json!({"x": 20, "y": 0, "blur": 0, "opacity": 100, "mode": "normal"});
|
||||
run(s, "effect.apply", json!({"ids": [id.0], "item": row, "effect": "stylize.dropShadow", "params": params}));
|
||||
}
|
||||
|
||||
/// Is `p` dark: a shadow, not the white page or the red fill?
|
||||
fn dark(p: [u8; 4]) -> bool {
|
||||
p[..3].iter().all(|c| *c < 100)
|
||||
}
|
||||
|
||||
/// A raster effect on a symbol instance's fill draws on the filled shapes of its art: the shadow
|
||||
/// falls right of the square, the bounds take it in, a hidden fill draws none, and Expand
|
||||
/// Appearance keeps the look in one undo step.
|
||||
#[test]
|
||||
fn a_symbol_instance_fill_shadow_shadows_its_filled_art() {
|
||||
let mut s = session();
|
||||
let (id, row) = instance_with_row(&mut s, "Square", true);
|
||||
assert!(near(render(&s).pixel(160, 100), WHITE));
|
||||
shadow_row(&mut s, id, row);
|
||||
let before = render(&s);
|
||||
assert!(dark(before.pixel(165, 100)), "the fill's shadow falls right of the square: {:?}", before.pixel(165, 100));
|
||||
assert!(near(before.pixel(100, 100), RED), "the art draws over its shadow: {:?}", before.pixel(100, 100));
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let b = vectorcraft_render::painted_bounds(&doc, doc.node(id).unwrap()).expect("the instance has bounds");
|
||||
assert!(b.x1 >= 169.0, "the bounds take in the shadow: {b:?}");
|
||||
// A view that shows only the shadow, right of the square: the instance is still drawn.
|
||||
let opts = vectorcraft_render::RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
let right = vectorcraft_render::Renderer::new().render(&doc, 200, 200, vectorcraft_geom::Affine::translate((-155.0, 0.0)), &opts);
|
||||
assert!(dark(right.pixel(10, 100)), "the shadow past the square's box is drawn: {:?}", right.pixel(10, 100));
|
||||
|
||||
assert!(expandable(&s), "the fill's shadow can be expanded");
|
||||
run(&mut s, "effect.expandAppearance", json!({}));
|
||||
let ch = node(&s, id).children().cloned().expect("the instance becomes a group");
|
||||
assert!(matches!(ch[0].kind, NodeKind::Image(_)), "the shadow is an image under the art: {}", ch[0].kind_label());
|
||||
assert!(matches!(ch[1].kind, NodeKind::SymbolInstance { .. }), "the art stays an instance: {}", ch[1].kind_label());
|
||||
no_effects_left(&ch[1]);
|
||||
let after = render(&s);
|
||||
assert!(differing(&before, &after, 24) < 400, "expanding changed {} pixels", differing(&before, &after, 24));
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
let n = node(&s, id);
|
||||
assert!(matches!(n.kind, NodeKind::SymbolInstance { .. }), "one undo gives the instance back");
|
||||
assert!(n.appearance.items[row].effects().iter().any(|e| e.id == "stylize.dropShadow"));
|
||||
|
||||
run(&mut s, "appearance.setItem", json!({"ids": [id.0], "index": row, "visible": false}));
|
||||
assert!(near(render(&s).pixel(165, 100), WHITE), "a hidden fill draws no shadow");
|
||||
}
|
||||
|
||||
/// A raster effect on a symbol instance's stroke draws on the stroked shapes of its art only: the
|
||||
/// stroke's shadow falls right of the stroke, and none falls where only the fill would cast one.
|
||||
#[test]
|
||||
fn a_symbol_instance_stroke_shadow_shadows_only_its_stroked_art() {
|
||||
let mut s = session();
|
||||
let (id, row) = instance_with_row(&mut s, "Square", false);
|
||||
shadow_row(&mut s, id, row);
|
||||
let r = render(&s);
|
||||
assert!(dark(r.pixel(170, 100)), "the stroke's shadow falls right of the stroke: {:?}", r.pixel(170, 100));
|
||||
assert!(near(r.pixel(160, 100), WHITE), "the fill casts no shadow: {:?}", r.pixel(160, 100));
|
||||
}
|
||||
|
||||
/// Geometry and raster effects on one fill of an instance: the shadow falls from the grown fill.
|
||||
#[test]
|
||||
fn a_symbol_instance_fill_shadow_follows_the_fill_offset_before_it() {
|
||||
let mut s = session();
|
||||
let (id, row) = instance_with_row(&mut s, "Square", true);
|
||||
offset_row(&mut s, id, row, 10.0);
|
||||
shadow_row(&mut s, id, row);
|
||||
let r = render(&s);
|
||||
assert!(near(r.pixel(44, 100), RED), "the fill grows by the offset: {:?}", r.pixel(44, 100));
|
||||
assert!(dark(r.pixel(175, 100)), "the shadow falls from the grown fill: {:?}", r.pixel(175, 100));
|
||||
}
|
||||
|
||||
/// An instance inside a symbol's art passes the outer instance's fill shadow on to its own art.
|
||||
#[test]
|
||||
fn a_nested_symbol_instance_takes_the_outer_fill_shadow() {
|
||||
let mut s = session();
|
||||
square(&mut s);
|
||||
run(&mut s, "symbol.new", json!({"name": "Inner"}));
|
||||
let id = NodeId(run(&mut s, "symbol.new", json!({"name": "Outer"}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "appearance.addFill", json!({"ids": [id.0]}));
|
||||
let row = node(&s, id).appearance.items.len() - 1;
|
||||
run(&mut s, "appearance.setItem", json!({"ids": [id.0], "index": row, "none": true}));
|
||||
shadow_row(&mut s, id, row);
|
||||
assert!(dark(render(&s).pixel(165, 100)), "the inner art casts the shadow: {:?}", render(&s).pixel(165, 100));
|
||||
}
|
||||
|
||||
/// Export draws a symbol instance's fill shadow on the filled shapes of its art: baking leaves the
|
||||
/// shadow on the art's fill, not its stroke, and PDF export makes an image of it.
|
||||
#[test]
|
||||
fn export_draws_a_symbol_instance_fill_shadow() {
|
||||
let mut s = session();
|
||||
let (id, row) = instance_with_row(&mut s, "Square", true);
|
||||
shadow_row(&mut s, id, row);
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let baked = vectorcraft_render::effects::bake_document(&doc).expect("the instance is baked");
|
||||
let n = baked.node(id).expect("the baked art keeps the instance's id").clone();
|
||||
assert!(!matches!(n.kind, NodeKind::SymbolInstance { .. }));
|
||||
let shadowed = |i: &vectorcraft_doc::AppearanceItem| i.effects().iter().any(|e| e.id == "stylize.dropShadow");
|
||||
let (mut fills, mut strokes) = (0, 0);
|
||||
n.walk(&mut |part| {
|
||||
fills += part.appearance.items.iter().filter(|i| i.is_fill() && shadowed(i)).count();
|
||||
strokes += part.appearance.items.iter().filter(|i| !i.is_fill() && shadowed(i)).count();
|
||||
});
|
||||
assert_eq!((fills, strokes), (1, 0), "the art's fill carries the shadow");
|
||||
|
||||
let flat = crate::flatten_raster_effects(&doc).expect("the shadow becomes an image");
|
||||
let mut image = None;
|
||||
flat.node(id).expect("the flattened art keeps the instance's id").walk(&mut |part| {
|
||||
if matches!(part.kind, NodeKind::Image(_)) {
|
||||
image = part.geometric_bounds();
|
||||
}
|
||||
});
|
||||
let image = image.expect("the shadow is an image");
|
||||
assert!(image.x1 >= 165.0, "the image holds the shadow right of the square: {image:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_fills_and_an_offset_stroke_become_three_paths_that_look_the_same() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -138,7 +138,7 @@ fn knockout_settings_round_trip_and_old_bools_load() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdf_and_svg_write_knockout_groups_as_masks() {
|
||||
fn pdf_writes_knockout_groups_and_svg_writes_them_as_masks() {
|
||||
let mut s = session();
|
||||
let g = knockout_group(&mut s);
|
||||
let plain = s.doc().unwrap().doc.clone();
|
||||
@@ -149,10 +149,11 @@ fn pdf_and_svg_write_knockout_groups_as_masks() {
|
||||
let r = pdf(&plain);
|
||||
assert!(!String::from_utf8_lossy(&r.bytes).contains("/SMask") && r.warnings.is_empty());
|
||||
let r = pdf(&ko);
|
||||
let text = String::from_utf8_lossy(&r.bytes);
|
||||
// The lower rectangle is drawn through a luminosity mask made of the upper one's alpha.
|
||||
assert!(text.contains("/Luminosity") && text.contains("/Alpha"), "soft masks");
|
||||
assert!(r.warnings.iter().any(|w| w.contains("knockout")), "{:?}", r.warnings);
|
||||
let text = String::from_utf8_lossy(&r.bytes).replace(' ', "");
|
||||
// The rectangles knock out the area they paint: a PDF knockout group, without soft masks.
|
||||
assert_eq!(text.matches("/Ktrue").count(), 1, "a knockout group");
|
||||
assert!(!text.contains("/Luminosity"), "no soft masks");
|
||||
assert!(r.warnings.is_empty(), "{:?}", r.warnings);
|
||||
|
||||
let svg = |d: &Document| vectorcraft_svg::export(d, &Default::default());
|
||||
assert!(!svg(&plain).contains("knockout"));
|
||||
@@ -173,5 +174,5 @@ fn pdf_and_svg_write_knockout_groups_as_masks() {
|
||||
assert!(svg(&page).contains("isolation"));
|
||||
page.page_knockout = true;
|
||||
assert_eq!(svg(&page).matches("mask=\"url(#knockout-").count(), 1, "the layer is neutral: its objects knock each other out");
|
||||
assert!(String::from_utf8_lossy(&pdf(&page).bytes).contains("/Luminosity"));
|
||||
assert!(String::from_utf8_lossy(&pdf(&page).bytes).replace(' ', "").contains("/Ktrue"));
|
||||
}
|
||||
|
||||
@@ -849,3 +849,75 @@ fn journal_entries_pass_the_check() {
|
||||
assert!(check_params(id, params).is_ok(), "{id} {params}");
|
||||
}
|
||||
}
|
||||
|
||||
/// An index of a path, type, panel, library, style or ruler command given as something other than a
|
||||
/// whole number of 0 or more (`"3"`, `2.5`, `-1`, `true`, `[]`, `{}`) is refused as the wrong type,
|
||||
/// naming its key. A required one is not reported as missing, and an optional one (`subpath` of
|
||||
/// `path.appendAnchor`, `path.close` and `path.reverse`, `artboard` of the ruler origin commands) is
|
||||
/// not read as its default. A required one left out or null is still missing, and an optional one left
|
||||
/// out or null keeps its default. Neither changes the document or records an undo step.
|
||||
#[test]
|
||||
fn a_malformed_index_is_not_missing_or_its_default() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = s.execute("path.create", &json!({"d": "M0 0 L10 0 M20 0 L30 0"})).unwrap()["id"].clone();
|
||||
s.execute("library.create", &json!({"name": "Brand"})).unwrap();
|
||||
s.execute("library.createGroup", &json!({"name": "Warm"})).unwrap();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let with = |base: &serde_json::Value, key: &str, v: Option<serde_json::Value>| {
|
||||
let mut p = base.clone();
|
||||
let o = p.as_object_mut().unwrap();
|
||||
match v {
|
||||
Some(v) => o.insert(key.into(), v),
|
||||
None => o.remove(key),
|
||||
};
|
||||
p
|
||||
};
|
||||
let refused = |s: &mut Session, cmd: &str, p: &serde_json::Value, want: &str| match s.execute(cmd, p) {
|
||||
Err(EngineError::BadParams { cmd: c, msg }) => assert_eq!((c.as_str(), msg.as_str()), (cmd, want), "{p}"),
|
||||
other => panic!("{cmd} {p}: {other:?}"),
|
||||
};
|
||||
let malformed = [json!("3"), json!(2.5), json!(-1), json!(true), json!([]), json!({})];
|
||||
let handle = json!({"id": id, "x": 5, "y": 5});
|
||||
let required = [
|
||||
("path.setHandle", handle.clone(), "anchor", "missing anchor"),
|
||||
("path.moveAnchor", handle.clone(), "anchor", "missing anchor"),
|
||||
("path.insertAnchor", json!({"id": id}), "segment", "missing segment"),
|
||||
("text.setPathHandle", handle.clone(), "anchor", "missing anchor"),
|
||||
("artboard.reorder", json!({"index": 0}), "to", "missing to"),
|
||||
("library.moveGroup", json!({"group": "Warm"}), "to", "missing `to` (an index)"),
|
||||
("graphicStyle.move", json!({"name": "Default"}), "to", "missing `to` (an index)"),
|
||||
];
|
||||
for (cmd, base, key, missing) in &required {
|
||||
for v in &malformed {
|
||||
refused(&mut s, cmd, &with(base, key, Some(v.clone())), &format!("`{key}` must be a whole number"));
|
||||
}
|
||||
for p in [with(base, key, None), with(base, key, Some(json!(null)))] {
|
||||
refused(&mut s, cmd, &p, missing);
|
||||
}
|
||||
}
|
||||
let optional = [
|
||||
("path.appendAnchor", handle.clone(), "subpath"),
|
||||
("path.close", json!({"id": id}), "subpath"),
|
||||
("path.reverse", json!({"ids": [id]}), "subpath"),
|
||||
("document.rulerOrigin", json!({}), "artboard"),
|
||||
("document.setRulerOrigin", json!({"x": 5, "y": 5}), "artboard"),
|
||||
("document.resetRulerOrigin", json!({}), "artboard"),
|
||||
];
|
||||
for (cmd, base, key) in &optional {
|
||||
for v in &malformed {
|
||||
refused(&mut s, cmd, &with(base, key, Some(v.clone())), &format!("`{key}` must be a whole number"));
|
||||
}
|
||||
}
|
||||
// A whole number that names no artboard is still out of range.
|
||||
refused(&mut s, "document.resetRulerOrigin", &json!({"artboard": 1}), "index out of range");
|
||||
assert_eq!(s.doc().unwrap().doc, doc);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
// Left out or null, an optional one keeps its default.
|
||||
for (cmd, base, key) in &optional {
|
||||
for p in [with(base, key, None), with(base, key, Some(json!(null)))] {
|
||||
s.execute(cmd, &p).unwrap_or_else(|e| panic!("{cmd} {p}: {e}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user