Merge main into feat/plugin-api-document-info

This commit is contained in:
dotCooCoo
2026-11-24 22:49:15 -08:00
465 changed files with 27095 additions and 2709 deletions
+6 -1
View File
@@ -93,7 +93,7 @@ fn every_command_without_a_document() {
}
/// Regression: huge column counts used to panic with `capacity overflow`; in every layout and
/// order (#681), with huge and negative spacing too.
/// order (#681), with huge and negative spacing too, and with `indices` and `at`.
#[test]
fn artboard_rearrange_huge_columns() {
for fx in Fixture::ALL {
@@ -106,6 +106,11 @@ fn artboard_rearrange_huge_columns() {
}
}
}
// Listed artboards, out of range, and the grid's top-left at the far ends of the plane.
for (indices, at) in [(json!([0]), json!([1e308, -1e308])), (json!([0, u64::MAX]), json!([0, 0])), (json!([]), json!([-1e308, 1e308]))] {
let p = json!({ "indices": indices, "at": at, "columns": u64::MAX, "spacing": 1e308 });
assert_eq!(probe(fx, "artboard.rearrange", &p), None);
}
}
}
+196
View File
@@ -466,6 +466,160 @@ fn turned_horizontal_dimensions_stay_horizontal() {
assert_eq!(text(&s, id), "100.00 pt");
}
/// A dimension of a whole path measures the path's overall width, or its height when vertical,
/// between its outermost points, and follows the path when it turns. Turned 30°, a 100 × 60
/// rectangle is 100 cos 30° + 60 sin 30° wide, more than either side; a turned ellipse is measured
/// at the outermost points of its curves, which are not anchors.
#[test]
fn a_dimension_of_a_whole_path_measures_its_overall_width_and_height() {
let value = |s: &Session, id: u64| text(s, id).trim_end_matches(" pt").parse::<f64>().unwrap();
let mut s = session();
let rect = run(&mut s, "shape.rectangle", json!({"x":100,"y":100,"width":100,"height":60}))["id"].as_u64().unwrap();
let source = json!({"kind":"extent","node":rect});
let id = run(&mut s, "dimension.create", json!({"source":source,"orientation":"horizontal","offset":-30}))["id"].as_u64().unwrap();
assert_eq!(text(&s, id), "100.00 pt");
run(&mut s, "select.set", json!({"ids":[rect]}));
run(&mut s, "object.rotate", json!({"angle":30}));
let wide = 100.0 * 30f64.to_radians().cos() + 60.0 * 30f64.to_radians().sin();
assert!((value(&s, id) - wide).abs() < 0.01, "{} against {wide}", text(&s, id));
// The rectangle's box: left, top, right, bottom.
let bounds = |s: &Session| {
let anchors = s.doc().unwrap().doc.node(NodeId(rect)).unwrap().path_data().unwrap().subpaths[0].anchors.clone();
let (xs, ys): (Vec<f64>, Vec<f64>) = anchors.iter().map(|a| (a.p.x, a.p.y)).unzip();
let low = |v: &[f64]| v.iter().copied().fold(f64::INFINITY, f64::min);
let high = |v: &[f64]| v.iter().copied().fold(f64::NEG_INFINITY, f64::max);
(low(&xs), low(&ys), high(&xs), high(&ys))
};
// The line stays 30 pt above the turned rectangle's topmost corner, clear of the art.
let (left, top, right, _) = bounds(&s);
let line = recipe(&s, id).line_ends().0.y;
assert!((line - (top - 30.0)).abs() < 0.01, "line at y = {line}, top at {top}");
run(&mut s, "dimension.update", json!({"ids":[id],"orientation":"vertical"}));
let tall = 100.0 * 30f64.to_radians().sin() + 60.0 * 30f64.to_radians().cos();
assert!((value(&s, id) - tall).abs() < 0.01, "{} against {tall}", text(&s, id));
let d = recipe(&s, id);
let (top, bottom) = (d.a.y.min(d.b.y), d.a.y.max(d.b.y));
assert!(((bottom - top) - tall).abs() < 0.01, "the points are the topmost and bottommost: {:?} {:?}", d.a, d.b);
// The line was 30 pt above the path; turned, it is 30 pt beside it.
let line = d.line_ends().0.x;
assert!((line - (left - 30.0)).abs() < 0.01 || (line - (right + 30.0)).abs() < 0.01, "line at x = {line}, path from {left} to {right}");
run(&mut s, "edit.undo", json!({}));
assert!((value(&s, id) - wide).abs() < 0.01, "{}", text(&s, id));
// A linear dimension only.
assert!(s.execute("dimension.create", &json!({"mode":"angular","source":source})).is_err());
let ellipse = run(&mut s, "shape.ellipse", json!({"x":300,"y":100,"width":100,"height":50}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":ellipse},"orientation":"horizontal"}))["id"].as_u64().unwrap();
run(&mut s, "select.set", json!({"ids":[ellipse]}));
run(&mut s, "object.rotate", json!({"angle":45}));
let anchors = s.doc().unwrap().doc.node(NodeId(ellipse)).unwrap().path_data().unwrap().subpaths[0].anchors.clone();
let between_anchors = anchors.iter().map(|a| a.p.x).fold(f64::MIN, f64::max) - anchors.iter().map(|a| a.p.x).fold(f64::MAX, f64::min);
// 2 √(50² cos² 45° + 25² sin² 45°)
let across = 2.0 * (2500.0f64 * 0.5 + 625.0 * 0.5).sqrt();
assert!(value(&s, id) > between_anchors + 1.0, "{} against the anchors' {between_anchors}", text(&s, id));
assert!((value(&s, id) - across).abs() < 0.1, "{} against {across}", text(&s, id));
}
/// A dimension of a whole path measures the segments the path draws: an anchor that starts no
/// segment does not widen it. Its line stays clear of the whole path when only parts other than
/// its measured points move, also when it is aligned or is scaled together with the path, when the
/// path's direction is reversed or the path is reflected onto itself, and when it becomes aligned.
/// A reflection that moves the path puts the line on the reflected side.
#[test]
fn a_dimension_of_a_whole_path_stays_clear_of_the_path_it_draws() {
let mut s = session();
let lone = run(&mut s, "path.create", json!({"d":"M100 300 L110 300 M200 400","closed":false}))["id"].as_u64().unwrap();
assert_eq!(s.doc().unwrap().doc.node(NodeId(lone)).unwrap().path_data().unwrap().subpaths.len(), 2, "the lone anchor is kept");
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":lone},"orientation":"horizontal"}))["id"].as_u64().unwrap();
assert_eq!(text(&s, id), "10.00 pt");
// A triangle whose top rises while its leftmost and rightmost points stay: the line stays
// 30 pt above the top.
let tri = run(&mut s, "path.create", json!({"d":"M100 200 L150 120 L200 200 Z","closed":true}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":tri},"orientation":"horizontal","offset":-110}))["id"]
.as_u64()
.unwrap();
assert!((recipe(&s, id).line_ends().0.y - 90.0).abs() < 1e-6);
run(&mut s, "select.set", json!({"ids":[tri]}));
run(&mut s, "object.scale", json!({"sx":100,"sy":150,"origin":[150,200]}));
let line = recipe(&s, id).line_ends().0.y;
assert!((line - 50.0).abs() < 1e-6, "line at y = {line}");
// The same for an aligned dimension, and for a dimension scaled together with its path.
for (x, orientation, with_dimension) in [(300.0, "aligned", false), (500.0, "horizontal", true)] {
let d = format!("M{x} 200 L{} 120 L{} 200 Z", x + 50.0, x + 100.0);
let tri = run(&mut s, "path.create", json!({"d":d,"closed":true}))["id"].as_u64().unwrap();
let source = json!({"kind":"extent","node":tri});
let id = run(&mut s, "dimension.create", json!({"source":source,"orientation":orientation,"offset":-110}))["id"].as_u64().unwrap();
let ids = if with_dimension { vec![tri, id] } else { vec![tri] };
run(&mut s, "select.set", json!({"ids":ids}));
run(&mut s, "object.scale", json!({"sx":100,"sy":150,"origin":[x + 50.0, 200]}));
let line = recipe(&s, id).line_ends().0.y;
assert!((line - 50.0).abs() < 1e-6, "{orientation}: line at y = {line}");
}
// Reversing the path's direction draws the same outline: the line stays above it.
let rect = run(&mut s, "shape.rectangle", json!({"x":100,"y":400,"width":100,"height":60}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":rect},"orientation":"horizontal","offset":-30}))["id"]
.as_u64()
.unwrap();
run(&mut s, "path.reverse", json!({"ids":[rect]}));
let line = recipe(&s, id).line_ends().0.y;
assert!((line - 370.0).abs() < 1e-6, "line at y = {line}");
// So does a reflection onto itself whose coordinates do not come out exactly, while a
// reflection that moves the art puts the line on the reflected side.
let rect = run(&mut s, "shape.rectangle", json!({"x":100,"y":500.2,"width":100,"height":60}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":rect},"orientation":"horizontal","offset":-30}))["id"]
.as_u64()
.unwrap();
run(&mut s, "select.set", json!({"ids":[rect]}));
run(&mut s, "object.reflect", json!({"axis":"horizontal","origin":[0,530.2]}));
let line = recipe(&s, id).line_ends().0.y;
assert!((line - 470.2).abs() < 1e-6, "line at y = {line}");
let tri = run(&mut s, "path.create", json!({"d":"M100 700 L150 620 L200 700 Z","closed":true}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":tri},"orientation":"horizontal","offset":-110}))["id"]
.as_u64()
.unwrap();
run(&mut s, "select.set", json!({"ids":[tri]}));
run(&mut s, "object.reflect", json!({"axis":"horizontal","origin":[0,700]}));
let line = recipe(&s, id).line_ends().0.y;
assert!((line - 810.0).abs() < 1e-6, "line at y = {line}");
// Made aligned, the line runs from the leftmost to the rightmost point, with every corner of
// the rectangle on one side of it and 40 pt from the nearest, as it was 40 pt above the top.
let rect = run(&mut s, "shape.rectangle", json!({"x":300,"y":100,"width":100,"height":60}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":rect},"orientation":"horizontal","offset":-40}))["id"]
.as_u64()
.unwrap();
run(&mut s, "dimension.update", json!({"ids":[id],"orientation":"aligned"}));
let (a, b) = recipe(&s, id).line_ends();
let side = |x: f64, y: f64| (b - a).cross(vectorcraft_geom::Point::new(x, y) - a) / (b - a).hypot();
let sides = [side(300.0, 100.0), side(400.0, 100.0), side(400.0, 160.0), side(300.0, 160.0)];
assert!(sides.iter().all(|v| *v > 1e-6) || sides.iter().all(|v| *v < -1e-6), "{a:?} to {b:?}: {sides:?}");
let nearest = sides.iter().map(|v| v.abs()).fold(f64::INFINITY, f64::min);
assert!((nearest - 40.0).abs() < 1e-6, "nearest corner {nearest} pt from the line");
// A line across the path, 30 pt below the top, made vertical: it goes 30 pt past the path's
// side rather than onto it.
let rect = run(&mut s, "shape.rectangle", json!({"x":600,"y":100,"width":100,"height":60}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":rect},"orientation":"horizontal","offset":30}))["id"]
.as_u64()
.unwrap();
run(&mut s, "dimension.update", json!({"ids":[id],"orientation":"vertical"}));
let line = recipe(&s, id).line_ends().0.x;
assert!((line - 570.0).abs() < 1e-6 || (line - 730.0).abs() < 1e-6, "line at x = {line}, path from 600 to 700");
// When that would put the line farther from its first point than an offset may be, the
// change is refused rather than leaving the line across the path.
let far = run(&mut s, "path.create", json!({"d":"M0 0 L950000 -100 L950000 200000 Z","closed":true}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":far},"orientation":"horizontal","offset":100000}))["id"]
.as_u64()
.unwrap();
assert!(s.execute("dimension.update", &json!({"ids":[id],"orientation":"vertical"})).is_err());
assert_eq!(recipe(&s, id).orientation, vectorcraft_doc::dimension::Orientation::Horizontal);
// An offset given with the orientation places the line instead.
run(&mut s, "dimension.update", json!({"ids":[id],"orientation":"vertical","offset":-30}));
let line = recipe(&s, id).line_ends().0.x;
assert!((line - 950030.0).abs() < 1e-6, "line at x = {line}");
}
/// The line of a linked horizontal dimension keeps its gap from the outermost point on its side
/// when the art turns, so it stays clear of the art: after a half turn the line is still 50 pt
/// above the upper point, which is now the other anchor.
@@ -1091,6 +1245,48 @@ fn free_points_survive_a_native_file() {
assert!(near(d.a, before.a + v) && near(d.b, before.b + v), "{before:?} → {d:?}");
}
/// Saved and opened again, a dimension of a whole path still measures the path: turned, the path
/// is wider than before, and so is the dimension.
#[test]
fn a_dimension_of_a_whole_path_survives_a_native_file() {
let value = |s: &Session, id: u64| text(s, id).trim_end_matches(" pt").parse::<f64>().unwrap();
let mut s = session();
let rect = run(&mut s, "shape.rectangle", json!({"x":100,"y":100,"width":100,"height":60}))["id"].as_u64().unwrap();
let id = run(&mut s, "dimension.create", json!({"source":{"kind":"extent","node":rect},"orientation":"horizontal","offset":-30}))["id"]
.as_u64()
.unwrap();
let before = recipe(&s, id);
let doc = vectorcraft_format::load(&vectorcraft_format::save(&s.doc().unwrap().doc, false)).unwrap();
let mut reopened = Session::new();
reopened.add_document(doc, None);
assert_eq!(recipe(&reopened, id).source, before.source);
run(&mut reopened, "select.set", json!({"ids":[rect]}));
run(&mut reopened, "object.rotate", json!({"angle":30}));
let wide = 100.0 * 30f64.to_radians().cos() + 60.0 * 30f64.to_radians().sin();
assert!((value(&reopened, id) - wide).abs() < 0.01, "{} against {wide}", text(&reopened, id));
// A file whose dimension names a node that is not there: the next edit unlinks it.
let mut v: Value = serde_json::from_slice(&vectorcraft_format::save(&s.doc().unwrap().doc, false)).unwrap();
fn dangle(v: &mut Value) {
match v {
Value::Object(m) => {
if m.get("kind") == Some(&json!("extent")) {
m.insert("node".into(), json!(99999));
}
m.values_mut().for_each(dangle);
}
Value::Array(a) => a.iter_mut().for_each(dangle),
_ => {}
}
}
dangle(&mut v);
let mut dangling = Session::new();
dangling.add_document(vectorcraft_format::load(&serde_json::to_vec(&v).unwrap()).unwrap(), None);
assert!(recipe(&dangling, id).source.is_some());
run(&mut dangling, "shape.rectangle", json!({"x":300,"y":300,"width":10,"height":10}));
assert!(recipe(&dangling, id).source.is_none(), "no longer linked");
}
/// A linked dimension opened from a file with options it cannot be drawn with keeps its art, as a
/// plain group, when the art it measures is deleted.
#[test]
@@ -43,8 +43,10 @@ const OUTSIDE: &[(&str, &str, &str)] = &[
/// Reading a file or a folder, a file dialog and a message to the person do not change anything
/// outside it.
const INSIDE: &[(&str, &str)] = &[
("", "$ File Folder alert confirm localize prompt toSource"),
("", "$ File Folder UnitValue alert confirm localize prompt toSource"),
("Object.prototype.", "toSource"),
("UnitValue.", "baseUnit baseUnit= length prototype"),
("UnitValue.prototype.", "as baseUnit baseUnit= constructor convert toString type type= value value="),
("localize.", "length"),
("toSource.", "length"),
(
@@ -57,7 +59,7 @@ const INSIDE: &[(&str, &str)] = &[
(
"File.prototype.",
"absoluteURI alias changePath close constructor created creator displayName encoding encoding= eof error error= \
exists fsName fullName getRelativeURI hidden length lineFeed lineFeed= modified name openDlg parent path read \
exists fsName fullName getRelativeURI hidden length lineFeed lineFeed= localizedName modified name openDlg parent path read \
readch readln readonly relativeURI resolve saveDlg seek tell toString type",
),
(
@@ -68,7 +70,7 @@ const INSIDE: &[(&str, &str)] = &[
(
"Folder.prototype.",
"absoluteURI alias changePath constructor created displayName error error= exists fsName fullName getFiles \
getRelativeURI modified name parent path relativeURI resolve selectDlg toString",
getRelativeURI localizedName modified name parent path relativeURI resolve selectDlg toString",
),
];
+34 -10
View File
@@ -1,6 +1,6 @@
//! Untrusted plug-ins never crash the app: mutated and garbage modules handed to `plugin.install`,
//! and plug-ins returning hostile output (random JSON in the object model's shape: huge or
//! malformed coordinates, unknown ids, broken paints and gradients, junk of every type) as object
//! Untrusted plug-ins never crash the app: mutated and garbage modules, and modules whose
//! `vc_manifest` custom section holds garbage, handed to `plugin.install`, and plug-ins returning
//! hostile output (random JSON in the object model's shape: huge or malformed coordinates, unknown ids, broken paints and gradients, junk of every type) as object
//! filters (`plugin.run`) or live effects (`effect.apply`), and plug-ins running bulk memory
//! instructions (`memory.fill`, `memory.copy`, `memory.init`, `data.drop`) on ranges in, at and past
//! the ends of memory and the data segment, under small and default budgets. Each run must end as
@@ -620,7 +620,11 @@ proptest! {
}
let mut s = rich_session();
s.execute("select.all", &json!({})).unwrap();
for module in [bytes, [b"\0asm\x01\0\0\0".as_slice(), &garbage].concat(), garbage.clone()] {
// A module whose `vc_manifest` custom section holds the garbage.
let section = tpl::custom_section(vectorcraft_plugins::manifest::MANIFEST_SECTION, &garbage);
let sectioned = tpl::with_field(&tpl::module(&tpl::manifest("fuzz.section", "filter", "{}"), tpl::ECHO, "", 1), &section).unwrap();
let sectioned = wat::parse_str(sectioned).unwrap();
for module in [bytes, [b"\0asm\x01\0\0\0".as_slice(), &garbage].concat(), garbage.clone(), sectioned] {
let r = catch_quiet(|| {
if let Ok(info) = install(&mut s, &module) {
let _ = s.execute("plugin.run", &json!({"id": info["id"]}));
@@ -707,9 +711,10 @@ proptest! {
}
/// Manifests with hostile `commands` (junk of every type, bad and repeated ids, labels with
/// control and bidirectional override characters, a `menu` naming another menu, junk
/// parameters) parse to an error or to commands whose menu labels show no hidden character and
/// that sit under Plug-ins alone; installing one and running each of its commands ends as an
/// control and bidirectional override characters, a `menu` of any shape naming any menu, junk
/// parameters) at API level 1 or 2 parse to an error or to commands whose menu labels show no
/// hidden character and that sit under Plug-ins, or at level 2 in one of the menus a manifest
/// may name, with the plug-in's name; installing one and running each of its commands ends as an
/// error or an edit, never a panic, and the document then renders and exports.
#[test]
fn hostile_manifest_commands_never_crash(
@@ -723,7 +728,17 @@ proptest! {
1 => "[a-z\u{0007}\n]{1,8}".prop_map(Value::from),
1 => arb_junk(),
],
prop_oneof![Just(Value::Null), Just(json!("File")), arb_junk()],
prop_oneof![
Just(Value::Null),
Just(json!("File")),
Just(json!({"path": ["Select"], "after": "Inverse"})),
Just(json!({"path": ["Object", "Path"]})),
Just(json!({"path": ["File"]})),
Just(json!({"path": ["Select"], "after": "\u{202E}Inverse"})),
arb_junk().prop_map(|j| json!({"path": j})),
arb_junk().prop_map(|j| json!({"path": ["Select"], "after": j})),
arb_junk(),
],
prop_oneof![Just(Value::Null), Just(json!({"by": {"type": "number", "min": 0, "max": 10, "default": 2}})), arb_junk()],
)
.prop_map(|(id, label, menu, params)| {
@@ -742,8 +757,9 @@ proptest! {
),
junk in prop::option::weighted(0.1, arb_junk()),
given in prop_oneof![Just(json!({})), Just(json!({"by": 3})), arb_junk()],
api in 1u32..=2,
) {
let mut m = json!({"id": "fuzz.commands", "name": "Fuzz \u{202E}Commands", "kind": kind});
let mut m = json!({"id": "fuzz.commands", "name": "Fuzz \u{202E}Commands", "kind": kind, "api": api});
if matches!(kind, "exporter" | "importer") {
m["extensions"] = json!(["fz"]);
}
@@ -756,6 +772,12 @@ proptest! {
let shown = c.shown_label();
prop_assert!(!shown.chars().any(|ch| ch.is_control() || matches!(ch, '\u{202E}' | '\u{2066}' | '\u{200F}')), "{}", shown);
prop_assert!(shown.chars().count() <= vectorcraft_plugins::permissions::MAX_SHOWN_NAME);
let placed = c.menu_label(&man.name);
prop_assert!(!placed.chars().any(|ch| ch.is_control() || matches!(ch, '\u{202E}' | '\u{2066}' | '\u{200F}')), "{}", placed);
if let Some(place) = &c.place {
prop_assert!(api >= 2 && vectorcraft_plugins::manifest::MENU_PLACES.contains(&place.path), "{:?}", place);
prop_assert!(placed.ends_with(')') && placed.len() > shown.len(), "a placed item names its plug-in: {}", placed);
}
}
}
let mut s = rich_session();
@@ -774,7 +796,9 @@ proptest! {
let ran = r.map_err(|msg| TestCaseError::fail(format!("panicked running {text}: {msg}")))?;
prop_assert!(ran.is_empty() || parsed.is_ok(), "a manifest that doesn't parse installed: {}", text);
for (id, menu) in &ran {
prop_assert!(menu.len() == 3 && menu[1] == "Plug-ins" && matches!(menu[0], "Object" | "Effect"), "{id}: {menu:?}");
let submenu = menu.len() == 3 && menu[1] == "Plug-ins" && matches!(menu[0], "Object" | "Effect");
let place = api >= 2 && vectorcraft_plugins::manifest::MENU_PLACES.contains(&menu.as_slice());
prop_assert!(submenu || place, "{id}: {menu:?}");
}
renders(&s, "after hostile commands")?;
vectorcraft_plugins::registry::remove("fuzz.commands");