Plug-ins: painted live effects refuse gradient stop plates without a spot link

A gradient stop's process plates print beside the tint of the spot swatch
that the stop itself links to. A painted live effect could return a stop
with `plates` and no link, or a link to a process swatch or Registration,
and the result was drawn with plates that no plate ever prints.

- The painted-output decoder refuses `plates` on a stop with no `swatch`
  (absent or null). The gradient's own `swatch` does not link its stops.
- The reference check refuses `plates` on a stop linked to a swatch that
  is not a spot swatch. It runs again on a cached result, so a result is
  refused once its swatch stops being a spot swatch. A missing swatch
  keeps the error that names the swatch.
- Either fault refuses the whole result, and `lastError` names the
  stop's `plates`. Plates on a stop linked to a spot swatch, all-zero
  ones included, are kept. Filter and importer output keep clamping and
  unlinking plates as before.

Docs: docs/plugins.md, docs/target-app-parity.md. The new message has a
row in every complete message catalog.
This commit is contained in:
dotCooCoo
2026-11-14 16:42:12 -08:00
parent 895e9493d2
commit d078e13ad8
29 changed files with 182 additions and 7 deletions
+38
View File
@@ -620,6 +620,44 @@ mod tests {
registry::remove(ID);
}
/// A cached result whose gradient stop prints `plates` beside a spot swatch is refused once that
/// swatch is no longer a spot swatch, and used again when it is one again.
#[test]
fn cached_painted_plates_refuse_after_spot_becomes_process() {
let _lock = crate::safe_mode::TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
crate::safe_mode::set_active(None);
const ID: &str = "test.effect.spotplates";
let manifest = format!(r#"{{"id":"{ID}","name":"Test","version":"1","kind":"effect","api":2,"paintOutput":true,"params":{{}}}}"#);
let gray = json!({"model": "gray", "k": 0});
let stops = json!([{"offset": 0, "color": gray}, {"offset": 1, "color": gray, "swatch": "Ink", "plates": [0.1, 0.2, 0.3, 0.4]}]);
let fill = json!({"type": "gradient", "gradient": {"kind": "Linear", "stops": stops}});
let square = PathData::single(SubPath::polyline(&[(0.0, 0.0), (9.0, 0.0), (9.0, 9.0)].map(|(x, y)| Point::new(x, y)), true));
let output = json!({"objects": [{"path": square, "fills": [fill]}]}).to_string();
let src = crate::wat::with_imports(&manifest, "", "", &output);
registry::install(crate::Plugin::load(&wat::parse_str(&src).unwrap(), Limits::default()).unwrap()).unwrap();
let with_ink = |spot: bool| {
let mut doc = Document::new(100.0, 100.0);
doc.swatches.push(vectorcraft_color::Swatch {
name: "Ink".into(),
paint: Paint::solid(Color::cmyk(0.0, 0.5, 1.0, 0.0)),
global: true,
spot,
});
doc
};
let (spot, process) = (with_ink(true), with_ink(false));
let path = line(2);
let draw =
|doc: &Document| apply_painted(ID, &json!({}), &path, Rect::new(0.0, 0.0, 1.0, 1.0), FillRule::NonZero, Paints::default(), Some(doc));
assert!(draw(&spot).is_some());
assert_eq!(last_error(ID), None);
assert!(draw(&process).is_none(), "from the cache, refused");
let e = last_error(ID).unwrap();
assert!(e.contains("objects[0].fills[0].gradient.stops[1].plates: `plates` requires a stop linked to a spot swatch"), "{e}");
assert!(draw(&spot).is_some(), "from the cache, used again");
registry::remove(ID);
}
/// A clock that reads `step` milliseconds later each time it is read.
fn stepping(step: u64) -> crate::deferred::Clock {
let t = Arc::new(std::sync::atomic::AtomicU64::new(0));
+23 -6
View File
@@ -7,8 +7,9 @@
//! An effect on a fill may return only `fills`, one on a stroke only `strokes`.
//!
//! The result is checked whole before any of it is used ([`decode`]): every number finite and in
//! range (nothing is clamped), every key known, and every swatch and pattern a paint names in the
//! document ([`check_references`]). One fault refuses the whole result, naming the field.
//! range (nothing is clamped), every key known, a gradient stop's `plates` only on a stop linked to
//! a spot swatch, and every swatch and pattern a paint names in the document ([`check_references`]).
//! One fault refuses the whole result, naming the field.
use serde_json::Value;
use vectorcraft_color::{Color, Paint};
@@ -236,7 +237,8 @@ fn paint_keys(v: &Value, at: &str) -> Result<()> {
}
/// A paint as the filter form has it (`null`: none), checked rather than clamped: colors, tints,
/// stops, opacities, midpoints and placements in range.
/// stops, opacities, midpoints and placements in range, and a gradient stop's `plates` only on a
/// stop linked to a swatch ([`check_references`] checks that the swatch is a spot swatch).
fn paint(v: &Value, at: &str) -> Result<Paint> {
if v.is_null() {
return Ok(Paint::None);
@@ -265,6 +267,11 @@ fn paint(v: &Value, at: &str) -> Result<Paint> {
for x in s.plates.iter().flatten() {
unit(*x, &format!("{at}.plates"), "a plate")?;
}
// Plates print beside the tint of the spot swatch the stop itself links to; a
// gradient's own `swatch` does not link its stops.
if s.plates.is_some() && s.swatch.is_none() {
return Err(out_err(&format!("{at}.plates"), "`plates` requires a stop linked to a spot swatch"));
}
}
if stops.windows(2).any(|w| w[1].offset < w[0].offset) {
return Err(out_err(&format!("{at}.gradient.stops"), "stops must be in order of `offset`"));
@@ -308,8 +315,9 @@ fn paint(v: &Value, at: &str) -> Result<Paint> {
}
/// Checks that every swatch a solid paint, gradient or gradient stop of `pieces` links to is a
/// swatch of `doc`, and every pattern a pattern paint names is one of its patterns. Without a
/// document (`None`), any swatch or pattern a piece names is refused.
/// swatch of `doc`, that a gradient stop with `plates` links to a spot swatch, and every pattern a
/// pattern paint names is one of its patterns. Without a document (`None`), any swatch or pattern a
/// piece names is refused.
pub fn check_references(pieces: &[Piece], doc: Option<&Document>) -> Result<()> {
let swatch = |name: &Option<String>, at: &str| match name {
Some(n) if doc.and_then(|d| d.swatch(n)).is_none() => Err(out_err(at, format!("the document has no swatch {n:?}"))),
@@ -321,7 +329,16 @@ pub fn check_references(pieces: &[Piece], doc: Option<&Document>) -> Result<()>
Paint::Solid { swatch: s, .. } => swatch(s, &format!("{at}.swatch")),
Paint::Gradient(g) => {
swatch(&g.swatch, &format!("{at}.swatch"))?;
g.gradient.stops.iter().enumerate().try_for_each(|(i, s)| swatch(&s.swatch, &format!("{at}.gradient.stops[{i}].swatch")))
g.gradient.stops.iter().enumerate().try_for_each(|(i, s)| {
let at = format!("{at}.gradient.stops[{i}]");
swatch(&s.swatch, &format!("{at}.swatch"))?;
match s.swatch.as_deref().and_then(|n| doc.and_then(|d| d.swatch(n))) {
Some(w) if s.plates.is_some() && !w.spot => {
Err(out_err(&format!("{at}.plates"), "`plates` requires a stop linked to a spot swatch"))
}
_ => Ok(()),
}
})
}
Paint::Pattern { pattern, .. } if doc.and_then(|d| d.pattern(pattern)).is_none() => {
Err(out_err(&format!("{at}.pattern"), format!("the document has no pattern {pattern:?}")))
+94
View File
@@ -239,3 +239,97 @@ fn named_swatches_and_patterns_must_exist() {
let e = check_references(&pieces(json!([{"path": square(), "fills": [{"type": "pattern", "pattern": "Dots"}]}])), None).unwrap_err().to_string();
assert!(e.contains("objects[0].fills[0].pattern"), "{e}");
}
/// A gradient of two stops: a plain first one, then `stop`, with `extra` keys on the paint.
fn ramp(stop: Value, extra: Value) -> Value {
let mut g = json!({"type": "gradient", "gradient": {"kind": "Linear", "stops": [{"offset": 0, "color": {"model": "gray", "k": 0}}, stop]}});
g.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
g
}
/// A second stop with process `plates`, and `extra` keys.
fn plated(extra: Value) -> Value {
let mut s = json!({"offset": 1, "color": {"model": "gray", "k": 1}, "plates": [0.1, 0.2, 0.3, 0.4]});
s.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
s
}
/// A gradient stop's process `plates` need the stop's own link to a swatch: a stop without one
/// (no `swatch` or a `null` one) refuses the whole result, naming the stop's `plates`, in a fill or
/// a stroke at every scope.
#[test]
fn unlinked_gradient_stop_plates_refuse_the_whole_painted_result() {
const WHY: &str = "`plates` requires a stop linked to a spot swatch";
for orphan in [ramp(plated(json!({})), json!({})), ramp(plated(json!({"swatch": null})), json!({}))] {
let fills = out(json!([{"path": square(), "fills": [red()]}, {"path": square(), "fills": [red(), orphan]}]));
for scope in [Scope::Object, Scope::Fill] {
let e = decode(&fills, scope, FillRule::NonZero).unwrap_err();
assert_eq!(e.to_string(), format!("plug-in output: objects[1].fills[1].gradient.stops[1].plates: {WHY}"), "{scope:?}");
}
let strokes = out(json!([{"path": square(), "strokes": [{"paint": red()}]}, {"path": square(), "strokes": [{"paint": orphan, "width": 2}]}]));
for scope in [Scope::Object, Scope::Stroke] {
let e = decode(&strokes, scope, FillRule::NonZero).unwrap_err();
assert_eq!(e.to_string(), format!("plug-in output: objects[1].strokes[0].paint.gradient.stops[1].plates: {WHY}"), "{scope:?}");
}
}
// `plugin.info`'s `lastError` names the plug-in before it.
let e = decode(&out(json!([{"path": square(), "fills": [ramp(plated(json!({})), json!({}))]}])), Scope::Object, FillRule::NonZero).unwrap_err();
assert_eq!(named("Test Tone", &e), format!("Test Tone: plug-in output: objects[0].fills[0].gradient.stops[1].plates: {WHY}"));
// A plate out of range keeps its own error.
let wide = ramp(json!({"offset": 1, "color": {"model": "gray", "k": 1}, "plates": [2, 0, 0, 0]}), json!({}));
let e = decode(&out(json!([{"path": square(), "fills": [wide]}])), Scope::Object, FillRule::NonZero).unwrap_err().to_string();
assert!(e.contains("objects[0].fills[0].gradient.stops[1].plates: a plate must be from 0 to 1"), "{e}");
}
/// The gradient's own `swatch` does not link its stops, so it does not admit a stop's `plates`.
#[test]
fn gradient_swatch_does_not_link_its_stops_plates() {
let fill = ramp(plated(json!({})), json!({"swatch": "Ink"}));
let e = decode(&out(json!([{"path": square(), "fills": [fill]}])), Scope::Object, FillRule::NonZero).unwrap_err().to_string();
assert!(e.contains("objects[0].fills[0].gradient.stops[1].plates: `plates` requires a stop linked to a spot swatch"), "{e}");
}
/// A stop's `plates` beside a link to a swatch that is not a spot swatch (a process swatch or
/// Registration) refuse the result at the reference check; a link to a missing swatch keeps the
/// error that names the swatch.
#[test]
fn process_swatch_cannot_back_stop_plates() {
let mut doc = Document::new(100.0, 100.0);
doc.swatches.push(Swatch { name: "Ink".into(), paint: Paint::solid(Color::cmyk(0.0, 0.5, 1.0, 0.0)), global: true, spot: true });
doc.swatches.push(Swatch { name: "Process".into(), paint: Paint::solid(Color::cmyk(0.0, 0.5, 1.0, 0.0)), global: true, spot: false });
let check = |pieces: Value| check_references(&decode(&out(pieces), Scope::Object, FillRule::NonZero).unwrap(), Some(&doc));
for name in ["Process", vectorcraft_color::swatch::REGISTRATION] {
let fill = ramp(plated(json!({"swatch": name})), json!({}));
let e = check(json!([{"path": square(), "fills": [fill.clone()]}])).unwrap_err().to_string();
assert!(e.contains("objects[0].fills[0].gradient.stops[1].plates: `plates` requires a stop linked to a spot swatch"), "{name}: {e}");
let e = check(json!([{"path": square()}, {"path": square(), "strokes": [{"paint": fill}]}])).unwrap_err().to_string();
assert!(e.contains("objects[1].strokes[0].paint.gradient.stops[1].plates:"), "{name}: {e}");
// Without `plates`, a stop may link to any swatch of the document.
let plain = ramp(json!({"offset": 1, "color": {"model": "gray", "k": 1}, "swatch": name}), json!({}));
assert!(check(json!([{"path": square(), "fills": [plain]}])).is_ok(), "{name}");
}
let gone = ramp(plated(json!({"swatch": "Gone"})), json!({}));
let e = check(json!([{"path": square(), "fills": [gone]}])).unwrap_err().to_string();
assert!(e.contains("objects[0].fills[0].gradient.stops[1].swatch:"), "{e}");
}
/// A stop linked to a spot swatch keeps its `plates` as given, all-zero ones included, through the
/// reference check and the swatch colors.
#[test]
fn linked_spot_stop_plates_are_kept() {
let mut doc = Document::new(100.0, 100.0);
doc.swatches.push(Swatch { name: "Ink".into(), paint: Paint::solid(Color::cmyk(0.0, 0.5, 1.0, 0.0)), global: true, spot: true });
let zero = json!({"offset": 0, "color": {"model": "gray", "k": 0}, "swatch": "Ink", "plates": [0, 0, 0, 0]});
let fill = json!({"type": "gradient", "gradient": {"kind": "Linear", "stops": [zero, plated(json!({"swatch": "Ink"}))]}});
let stroke = ramp(plated(json!({"swatch": "Ink"})), json!({}));
let pieces =
decode(&out(json!([{"path": square(), "fills": [fill], "strokes": [{"paint": stroke}]}])), Scope::Object, FillRule::NonZero).unwrap();
assert!(check_references(&pieces, Some(&doc)).is_ok());
let pieces = with_swatch_colors(std::sync::Arc::new(pieces), &doc);
let plates = |p: &Paint| match p {
Paint::Gradient(g) => g.gradient.stops.iter().map(|s| s.plates).collect::<Vec<_>>(),
_ => panic!("{p:?}"),
};
assert_eq!(plates(&pieces[0].fills.as_ref().unwrap()[0]), [Some([0.0; 4]), Some([0.1, 0.2, 0.3, 0.4])]);
assert_eq!(plates(&pieces[0].strokes.as_ref().unwrap()[0].paint), [None, Some([0.1, 0.2, 0.3, 0.4])]);
}