mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 00:54:44 +00:00
Merge Swatches relinked a gradient stop linked to a merged spot swatch to the kept swatch but kept the process plates it printed with the old ink, so a kept spot swatch printed them beside its own tint and a kept global process swatch held them for a later change back to spot. The merge now clears those plates in the art, gradient swatches, graphic styles, the art of brushes and the fill and stroke for new art, in the same undo step. Stops linked to other swatches keep their plates.
549 lines
31 KiB
Rust
549 lines
31 KiB
Rust
//! Gradient swatches and gradient stops linked to global and spot swatches: the recorded links,
|
|
//! stops following swatch edits, spot stops on their plate and as Separation and DeviceN shadings in PDF.
|
|
|
|
use serde_json::{Value, json};
|
|
use vectorcraft_color::{Color, GradientPaint, GradientStop, Paint};
|
|
use vectorcraft_render::proof;
|
|
|
|
use crate::*;
|
|
|
|
fn session() -> Session {
|
|
let mut s = Session::new();
|
|
s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap();
|
|
s
|
|
}
|
|
|
|
fn run(s: &mut Session, id: &str, p: Value) -> Value {
|
|
s.execute(id, &p).unwrap_or_else(|e| panic!("{id} {p}: {e}"))
|
|
}
|
|
|
|
fn doc(s: &Session) -> &vectorcraft_doc::Document {
|
|
&s.doc().unwrap().doc
|
|
}
|
|
|
|
const INK: Color = Color::Cmyk { c: 0.0, m: 1.0, y: 0.0, k: 0.0 };
|
|
|
|
/// A document with spot swatch "Ink" and a selected rectangle.
|
|
fn setup() -> (Session, NodeId) {
|
|
let mut s = session();
|
|
run(&mut s, "swatch.new", json!({"name": "Ink", "color": INK, "spot": true}));
|
|
let id = NodeId(run(&mut s, "shape.rectangle", json!({"x": 10, "y": 10, "width": 30, "height": 30}))["id"].as_u64().unwrap());
|
|
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
|
(s, id)
|
|
}
|
|
|
|
fn gradient(s: &Session, id: NodeId) -> GradientPaint {
|
|
match doc(s).node(id).unwrap().appearance.fill_paint() {
|
|
Paint::Gradient(g) => *g,
|
|
p => panic!("not a gradient: {p:?}"),
|
|
}
|
|
}
|
|
|
|
/// Ink at `tint` % → white.
|
|
fn ink_to_white(tint: f64) -> Value {
|
|
json!({"stops": [{"offset": 0, "swatch": "Ink", "tint": tint}, {"offset": 1, "color": "#ffffff"}]})
|
|
}
|
|
|
|
#[test]
|
|
fn applying_a_gradient_swatch_records_the_link_until_the_stops_change() {
|
|
let (mut s, id) = setup();
|
|
run(&mut s, "paint.setFill", json!({"swatch": "Sunset"}));
|
|
assert_eq!(gradient(&s, id).swatch.as_deref(), Some("Sunset"));
|
|
let used = run(&mut s, "swatch.unused", json!({}));
|
|
assert!(!used["names"].as_array().unwrap().contains(&json!("Sunset")));
|
|
run(&mut s, "paint.editGradient", json!({"angle": 45}));
|
|
assert_eq!(gradient(&s, id).swatch.as_deref(), Some("Sunset"), "moving the vector keeps it");
|
|
run(&mut s, "paint.editGradient", ink_to_white(100.0));
|
|
assert_eq!(gradient(&s, id).swatch, None, "new stops are another gradient");
|
|
}
|
|
|
|
#[test]
|
|
fn stops_link_to_swatches_and_follow_their_edits() {
|
|
let (mut s, id) = setup();
|
|
run(&mut s, "paint.editGradient", ink_to_white(50.0));
|
|
let st = gradient(&s, id).gradient.stops[0].clone();
|
|
assert_eq!((st.swatch.as_deref(), st.tint, st.color), (Some("Ink"), 0.5, INK.tinted(0.5)), "the colour comes from the swatch");
|
|
assert_eq!(gradient(&s, id).gradient.stops[1].swatch, None);
|
|
// A tint swatch links the stop to its base at its tint; a process swatch just gives its colour.
|
|
run(&mut s, "paint.setFill", json!({"swatch": "Ink", "tint": 40}));
|
|
run(&mut s, "swatch.new", json!({}));
|
|
run(&mut s, "paint.setFill", json!({"gradient": {"stops": [{"offset": 0, "swatch": "Ink 40%"}, {"offset": 1, "swatch": "Black"}]}}));
|
|
let stops = gradient(&s, id).gradient.stops;
|
|
assert_eq!((stops[0].swatch.as_deref(), stops[0].tint), (Some("Ink"), 0.4));
|
|
assert_eq!((stops[1].swatch.as_deref(), stops[1].tint), (None, 1.0));
|
|
assert!(s.execute("paint.editGradient", &json!({"stops": [{"offset": 0, "swatch": "Nope"}, {"offset": 1, "color": "#fff"}]})).is_err());
|
|
// Editing the swatch recolours the stops (and the tint swatch) as one undo step.
|
|
let undo = s.doc().unwrap().history.undo.len();
|
|
run(&mut s, "swatch.edit", json!({"name": "Ink", "color": {"c": 1, "m": 0, "y": 0, "k": 0}}));
|
|
let cyan = Color::cmyk(1.0, 0.0, 0.0, 0.0);
|
|
assert_eq!(gradient(&s, id).gradient.stops[0].color, cyan.tinted(0.4));
|
|
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);
|
|
// Global mode of Adjust Color Balance shifts stop tints too.
|
|
run(&mut s, "edit.colors.adjustBalance", json!({"tint": 60}));
|
|
assert_eq!(gradient(&s, id).gradient.stops[0].color, cyan);
|
|
// Recolouring a stop unlinks it; deleting the swatch unlinks the rest, which keep their colour.
|
|
run(&mut s, "paint.editGradient", ink_to_white(25.0));
|
|
run(&mut s, "swatch.delete", json!({"name": "Ink"}));
|
|
let st = gradient(&s, id).gradient.stops[0].clone();
|
|
assert_eq!((st.swatch, st.tint, st.color), (None, 1.0, cyan.tinted(0.25)));
|
|
}
|
|
|
|
#[test]
|
|
fn gradient_swatches_keep_their_stop_links_and_follow_edits() {
|
|
let (mut s, id) = setup();
|
|
run(&mut s, "swatch.new", json!({"name": "Glow", "gradient": ink_to_white(100.0)}));
|
|
run(&mut s, "paint.setFill", json!({"swatch": "Glow"}));
|
|
run(&mut s, "swatch.edit", json!({"name": "Ink", "color": {"c": 0, "m": 0, "y": 1, "k": 0}}));
|
|
let yellow = Color::cmyk(0.0, 0.0, 1.0, 0.0);
|
|
let Paint::Gradient(sw) = doc(&s).swatch("Glow").unwrap().paint.clone() else { panic!() };
|
|
assert_eq!(sw.gradient.stops[0].color, yellow, "the gradient swatch's stop follows");
|
|
let g = gradient(&s, id);
|
|
assert_eq!((g.gradient.stops[0].color, g.swatch.as_deref()), (yellow, Some("Glow")), "so does the art, still linked to its swatch");
|
|
assert_eq!(g.gradient, sw.gradient);
|
|
}
|
|
|
|
#[test]
|
|
fn spot_stops_separate_on_their_plate() {
|
|
let (mut s, id) = setup();
|
|
run(&mut s, "paint.editGradient", ink_to_white(60.0));
|
|
let mut node = doc(&s).node(id).unwrap().clone();
|
|
let mut links = vec![];
|
|
proof::map_node_colors(&mut node, &mut |c, link| {
|
|
links.push(link.map(|(n, t)| (n.to_string(), t)));
|
|
*c
|
|
});
|
|
assert!(links.contains(&Some(("Ink".to_string(), 0.6))), "{links:?}");
|
|
let c = vectorcraft_color::cms::active();
|
|
let st = gradient(&s, id).gradient.stops[0].clone();
|
|
let ink = proof::inks(doc(&s), &c, &st.color, Some(("Ink", st.tint)), proof::Intent::RelativeColorimetric);
|
|
assert_eq!((ink.cmyk, ink.spot), ([0.0; 4], Some(("Ink".to_string(), 0.6))));
|
|
}
|
|
|
|
fn pdf_of(s: &Session) -> (String, Vec<String>) {
|
|
let opts = vectorcraft_pdf::PdfOptions { created: Some(0), ..vectorcraft_pdf::PdfOptions::uncompressed() };
|
|
let r = vectorcraft_pdf::export_with_report(doc(s), &opts).unwrap();
|
|
(String::from_utf8_lossy(&r.bytes).into_owned(), r.warnings)
|
|
}
|
|
|
|
#[test]
|
|
fn spot_gradients_export_as_separation_shadings() {
|
|
let (mut s, _) = setup();
|
|
run(&mut s, "paint.editGradient", ink_to_white(80.0));
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(pdf.contains("/ShadingType 2") && pdf.contains("/Separation/Ink/DeviceCMYK"), "an Ink → paper gradient is a Separation shading");
|
|
// Its function runs over the tints: 80 % (204 / 255) to 0 %.
|
|
assert!(pdf.contains("/C0[0.8]/C1[0]"), "the shading's tints");
|
|
// Mixing the spot ink with an RGB color writes the gradient without the spot ink, with a warning.
|
|
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": "#00ff00"}]}));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(!pdf.contains("/Separation") && !pdf.contains("/DeviceN"), "no spot ink for a gradient with an RGB stop");
|
|
assert!(warnings.iter().any(|w| w.contains("spot")), "{warnings:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn white_stops_linked_to_process_swatches_are_paper_in_spot_gradients() {
|
|
let (mut s, _) = setup();
|
|
run(&mut s, "swatch.new", json!({"name": "Paper", "color": "#ffffff", "global": true}));
|
|
let ink_to_paper = json!({"stops": [{"offset": 0, "swatch": "Ink", "tint": 80}, {"offset": 1, "swatch": "Paper"}]});
|
|
// As Ink → unlinked white: a Separation shading from 80 % to 0 %.
|
|
run(&mut s, "paint.editGradient", ink_to_paper.clone());
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/ShadingType 2") && pdf.contains("/Separation/Ink/DeviceCMYK"), "a Separation shading");
|
|
assert!(!pdf.contains("/DeviceN") && pdf.contains("/C0[0.8]/C1[0]"), "the shading's tints");
|
|
assert!(!warnings.iter().any(|w| w.contains("spot")), "{warnings:?}");
|
|
// A Perceptual one too.
|
|
run(&mut s, "paint.editGradient", with(ink_to_paper, json!({"method": "perceptual"})));
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(!pdf.contains("/Subtype/Image") && !pdf.contains("/DeviceN") && pdf.contains("/Separation/Ink/DeviceCMYK"), "Perceptual");
|
|
// With process yellow, the linked white stop adds no ink: Yellow and Ink only.
|
|
let yellow = json!({"c": 0, "m": 0, "y": 0.6, "k": 0});
|
|
let three =
|
|
json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 0.5, "swatch": "Paper"}, {"offset": 1, "color": yellow}], "method": "classic"});
|
|
run(&mut s, "paint.editGradient", three);
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(pdf.contains("/DeviceN[/Yellow/Ink]/DeviceCMYK") && pdf.contains("/C0[0 1]/C1[0 0]"), "Yellow and Ink");
|
|
// A white spot ink is an ink, not paper: a DeviceN shading of both spot inks.
|
|
run(&mut s, "swatch.new", json!({"name": "Opaque", "color": "#ffffff", "spot": true}));
|
|
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "swatch": "Opaque"}]}));
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(pdf.contains("/DeviceN[/Ink/Opaque]/DeviceCMYK"), "a white spot ink keeps its plate");
|
|
}
|
|
|
|
#[test]
|
|
fn spot_gradients_mixed_with_other_inks_export_as_device_n_shadings() {
|
|
let (mut s, _) = setup();
|
|
// Ink → process yellow: a DeviceN shading of Yellow and Ink, with no warning.
|
|
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": {"c": 0, "m": 0, "y": 0.6, "k": 0}}]}));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/ShadingType 2") && pdf.contains("/DeviceN[/Yellow/Ink]/DeviceCMYK"), "a DeviceN shading of Yellow and Ink");
|
|
assert!(pdf.contains("/C0[0 1]/C1[0.6 0]"), "the shading's tints: Yellow and Ink");
|
|
assert!(!warnings.iter().any(|w| w.contains("spot")), "{warnings:?}");
|
|
// Two spot inks, with an off-center midpoint: a stop halfway between the two inks.
|
|
run(&mut s, "swatch.new", json!({"name": "Leaf", "color": {"c": 1, "m": 0, "y": 1, "k": 0}, "spot": true}));
|
|
run(
|
|
&mut s,
|
|
"paint.editGradient",
|
|
json!({"stops": [{"offset": 0, "swatch": "Ink", "midpoint": 30}, {"offset": 1, "swatch": "Leaf", "tint": 50}]}),
|
|
);
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/DeviceN[/Ink/Leaf]/DeviceCMYK"), "a DeviceN shading of both spot inks");
|
|
let functions: Vec<_> = pdf.match_indices("/C0[").filter_map(|(i, _)| pdf.get(i..i + 32)).collect();
|
|
assert!(pdf.contains("/C0[1 0]/C1[0.5 0.25]") && pdf.contains("/C0[0.5 0.25]/C1[0 0.5]"), "the tints through the midpoint: {functions:?}");
|
|
assert!(!warnings.iter().any(|w| w.contains("spot")), "{warnings:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn spot_stops_with_process_plates_export_both_in_device_n_shadings() {
|
|
// #3235: Ink 0 % to 100 % over a constant 30 % cyan plate, as a DeviceN gradient imported
|
|
// from a PDF has it.
|
|
let (mut s, id) = setup();
|
|
let cyan = json!({"c": 0.3, "m": 0, "y": 0, "k": 0});
|
|
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "color": cyan}, {"offset": 1, "swatch": "Ink", "plates": [0.3, 0, 0, 0]}]}));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/DeviceN[/Cyan/Ink]/DeviceCMYK"), "a DeviceN shading of Cyan and Ink");
|
|
assert!(pdf.contains("/C0[0.3 0]/C1[0.3 1]"), "the cyan plate runs under the whole gradient");
|
|
assert!(!warnings.iter().any(|w| w.contains("spot")), "{warnings:?}");
|
|
let stops = gradient(&s, id).gradient.stops;
|
|
assert_eq!((stops[1].swatch.as_deref(), stops[1].plates), (Some("Ink"), Some([0.3, 0.0, 0.0, 0.0])), "{stops:?}");
|
|
// The plates are part of the stop's params, so moving the vector keeps them.
|
|
run(&mut s, "paint.editGradient", json!({"angle": 45}));
|
|
assert_eq!(gradient(&s, id).gradient.stops[1].plates, Some([0.3, 0.0, 0.0, 0.0]));
|
|
// An unlinked stop has none.
|
|
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "color": cyan}, {"offset": 1, "color": "#ffffff", "plates": [0.3, 0, 0, 0]}]}));
|
|
assert_eq!(gradient(&s, id).gradient.stops[1].plates, None);
|
|
}
|
|
|
|
#[test]
|
|
fn deleting_a_spot_swatch_takes_the_process_plates_of_the_stops_it_unlinks() {
|
|
let (mut s, id) = setup();
|
|
let plated = json!({"stops": [{"offset": 0, "color": "#ffffff"}, {"offset": 1, "swatch": "Ink", "tint": 50, "plates": [0.3, 0, 0, 0]}]});
|
|
run(&mut s, "paint.editGradient", plated.clone());
|
|
run(&mut s, "swatch.new", json!({"name": "Glow", "gradient": plated}));
|
|
let linked = gradient(&s, id).gradient.stops[1].clone();
|
|
assert_eq!((linked.swatch.as_deref(), linked.plates), (Some("Ink"), Some([0.3, 0.0, 0.0, 0.0])));
|
|
run(&mut s, "swatch.delete", json!({"name": "Ink"}));
|
|
// The art's stop and the gradient swatch's stop keep the color they show, unlinked and without plates.
|
|
let Paint::Gradient(sw) = doc(&s).swatch("Glow").unwrap().paint.clone() else { panic!("a gradient swatch") };
|
|
for st in [&gradient(&s, id).gradient.stops[1], &sw.gradient.stops[1]] {
|
|
assert_eq!((st.color, st.swatch.as_deref(), st.tint, st.plates), (linked.color, None, 1.0, None), "{st:?}");
|
|
}
|
|
run(&mut s, "edit.undo", json!({}));
|
|
assert_eq!(gradient(&s, id).gradient.stops[1], linked, "undo brings back the link and the plates");
|
|
}
|
|
|
|
/// The stops of the gradients the merge test puts in place: the art's fill, gradient swatch "Glow",
|
|
/// graphic style "Style", the art of brush "Brush" and the fill new art takes.
|
|
fn merge_test_stops(s: &Session, id: NodeId) -> Vec<(&'static str, Vec<GradientStop>)> {
|
|
let d = doc(s);
|
|
let stops = |p: Paint| match p {
|
|
Paint::Gradient(g) => g.gradient.stops,
|
|
p => panic!("not a gradient: {p:?}"),
|
|
};
|
|
let style = d.graphic_style("Style").unwrap().appearance.items.iter().find_map(|it| match it {
|
|
vectorcraft_doc::AppearanceItem::Fill(f) => Some(f.paint.clone()),
|
|
vectorcraft_doc::AppearanceItem::Stroke(_) => None,
|
|
});
|
|
let mut brush = None;
|
|
for n in vectorcraft_brush::find(d, "Brush").unwrap().art() {
|
|
n.walk(&mut |m| {
|
|
if brush.is_none() && matches!(m.appearance.fill_paint(), Paint::Gradient(_)) {
|
|
brush = Some(m.appearance.fill_paint());
|
|
}
|
|
});
|
|
}
|
|
vec![
|
|
("art", stops(d.node(id).unwrap().appearance.fill_paint())),
|
|
("gradient swatch", stops(d.swatch("Glow").unwrap().paint.clone())),
|
|
("graphic style", stops(style.unwrap())),
|
|
("brush art", stops(brush.unwrap())),
|
|
("new art", stops(s.paint.fill.clone())),
|
|
]
|
|
}
|
|
|
|
#[test]
|
|
fn merging_swatches_takes_the_process_plates_of_the_stops_it_relinks() {
|
|
// Kept swatch "Keep": spot, global process, and process that isn't global.
|
|
for (spot, global) in [(true, true), (false, true), (false, false)] {
|
|
let (mut s, id) = setup();
|
|
run(&mut s, "swatch.new", json!({"name": "Keep", "color": "#00ff00", "spot": spot, "global": global}));
|
|
run(&mut s, "swatch.new", json!({"name": "Other", "color": {"c": 1, "m": 0, "y": 0, "k": 0}, "spot": true}));
|
|
// Ink at 50 % over a 30 % cyan plate, and Other over its own 20 % magenta plate, which the
|
|
// merge leaves alone.
|
|
let plated = json!({"stops": [
|
|
{"offset": 0, "swatch": "Ink", "tint": 50, "plates": [0.3, 0, 0, 0]},
|
|
{"offset": 1, "swatch": "Other", "plates": [0, 0.2, 0, 0]},
|
|
]});
|
|
run(&mut s, "paint.editGradient", plated.clone());
|
|
run(&mut s, "swatch.new", json!({"name": "Glow", "gradient": plated}));
|
|
run(&mut s, "graphicStyle.new", json!({"name": "Style"}));
|
|
run(&mut s, "brush.new", json!({"type": "art", "name": "Brush"}));
|
|
s.paint.fill = doc(&s).node(id).unwrap().appearance.fill_paint();
|
|
let before = merge_test_stops(&s, id);
|
|
let kept = doc(&s).linked_color(doc(&s).swatch("Keep").unwrap().paint.color().unwrap(), spot);
|
|
assert!(before.iter().all(|(_, st)| st[0].plates == Some([0.3, 0.0, 0.0, 0.0])), "{before:?}");
|
|
|
|
run(&mut s, "swatch.merge", json!({"names": ["Keep", "Ink"]}));
|
|
let after = merge_test_stops(&s, id);
|
|
// The stop shows its tint of the kept color, unlinked when the kept swatch isn't global.
|
|
let (link, tint, color) = if global { (Some("Keep"), 0.5, kept.tinted(0.5)) } else { (None, 1.0, kept.tinted(0.5)) };
|
|
for (place, st) in &after {
|
|
let what = format!("spot {spot}, global {global}, {place}: {st:?}");
|
|
assert_eq!((st[0].swatch.as_deref(), st[0].tint, st[0].plates), (link, tint, None), "{what}");
|
|
assert_eq!(st[0].color, color, "{what}");
|
|
assert_eq!((st[1].swatch.as_deref(), st[1].plates), (Some("Other"), Some([0.0, 0.2, 0.0, 0.0])), "{what}");
|
|
}
|
|
|
|
run(&mut s, "edit.undo", json!({}));
|
|
assert_eq!(merge_test_stops(&s, id), before, "undo brings back the links and the plates");
|
|
run(&mut s, "edit.redo", json!({}));
|
|
assert_eq!(merge_test_stops(&s, id), after, "redo merges again");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn radial_spot_gradients_mixed_with_other_inks_export_as_device_n_shadings() {
|
|
let (mut s, _) = setup();
|
|
run(
|
|
&mut s,
|
|
"paint.editGradient",
|
|
json!({"kind": "radial", "stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": {"c": 0, "m": 0, "y": 0.6, "k": 0}}]}),
|
|
);
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/ShadingType 3") && pdf.contains("/DeviceN[/Yellow/Ink]/DeviceCMYK"), "a radial DeviceN shading of Yellow and Ink");
|
|
assert!(pdf.contains("/C0[0 1]/C1[0.6 0]"), "the shading's tints: Yellow and Ink");
|
|
assert!(!warnings.iter().any(|w| w.contains("spot")), "{warnings:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn linked_stops_round_trip() {
|
|
let (mut s, id) = setup();
|
|
run(&mut s, "paint.editGradient", ink_to_white(30.0));
|
|
let d = doc(&s).clone();
|
|
let back = vectorcraft_format::load(&vectorcraft_format::save(&d, false)).unwrap();
|
|
assert_eq!(back.node(id).unwrap().appearance.fill_paint(), d.node(id).unwrap().appearance.fill_paint());
|
|
// Stops from before links read as unlinked.
|
|
let old: GradientStop = serde_json::from_value(json!({"offset": 0.5, "color": {"model": "rgb", "r": 1, "g": 0, "b": 0}})).unwrap();
|
|
assert_eq!(old, GradientStop::new(0.5, Color::rgb(1.0, 0.0, 0.0)));
|
|
assert!(!serde_json::to_string(&old).unwrap().contains("tint"));
|
|
}
|
|
|
|
/// `stops` with `extra` (method, dither…) as a gradient edit.
|
|
fn with(mut stops: Value, extra: Value) -> Value {
|
|
if let (Some(g), Some(e)) = (stops.as_object_mut(), extra.as_object()) {
|
|
g.extend(e.clone());
|
|
}
|
|
stops
|
|
}
|
|
|
|
#[test]
|
|
fn perceptual_spot_gradients_export_as_separation_shadings() {
|
|
let (mut s, _) = setup();
|
|
let ink_to_tint = json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "swatch": "Ink", "tint": 20}]});
|
|
for (what, stops) in [("Ink → paper", ink_to_white(80.0)), ("Ink → Ink 20 %", ink_to_tint)] {
|
|
run(&mut s, "paint.editGradient", with(stops, json!({"method": "perceptual"})));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(!pdf.contains("/Subtype/Image"), "{what}: no image");
|
|
assert!(pdf.contains("/ShadingType 2") && pdf.contains("/Separation/Ink/DeviceCMYK"), "{what}: a Separation shading");
|
|
// The curve's stops: a stitching function of more than the two ends.
|
|
assert!(pdf.contains("/FunctionType 3"), "{what}: tints on the curve");
|
|
assert!(!warnings.iter().any(|w| w.contains("spot")), "{what}: {warnings:?}");
|
|
}
|
|
// A radial one, and one on a stroke, too.
|
|
run(&mut s, "paint.editGradient", with(ink_to_white(100.0), json!({"method": "perceptual", "kind": "radial"})));
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(!pdf.contains("/Subtype/Image") && pdf.contains("/Separation/Ink/DeviceCMYK"), "radial");
|
|
run(&mut s, "paint.setFill", json!({"color": "#ffffff"}));
|
|
run(&mut s, "paint.setStroke", json!({"color": "#000000"}));
|
|
run(&mut s, "stroke.set", json!({"weight": 6}));
|
|
run(&mut s, "paint.editGradient", with(ink_to_white(100.0), json!({"method": "perceptual", "stroke": true})));
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(!pdf.contains("/Subtype/Image") && pdf.contains("/Separation/Ink/DeviceCMYK"), "stroke");
|
|
// A white spot ink has paper white's lightness at every tint: the stops keep their own tints.
|
|
run(&mut s, "swatch.new", json!({"name": "Opaque", "color": "#ffffff", "spot": true}));
|
|
run(&mut s, "paint.setStroke", json!({"none": true}));
|
|
let white =
|
|
json!({"stops": [{"offset": 0, "swatch": "Opaque"}, {"offset": 1, "swatch": "Opaque", "tint": 40}], "method": "perceptual", "stroke": false});
|
|
run(&mut s, "paint.editGradient", white);
|
|
let (pdf, _) = pdf_of(&s);
|
|
let at = pdf.find("/FunctionType").unwrap_or(0);
|
|
assert!(!pdf.contains("/Subtype/Image") && pdf.contains("/Separation/Opaque"), "white ink: {}", &pdf[at..(at + 200).min(pdf.len())]);
|
|
assert!(pdf.contains("/C0[1]") && pdf.contains("/C1[0.4"), "white ink at 100% to 40%: {}", &pdf[at..(at + 200).min(pdf.len())]);
|
|
}
|
|
|
|
#[test]
|
|
fn perceptual_spot_gradients_mixed_with_other_inks_export_as_device_n_shadings() {
|
|
let (mut s, _) = setup();
|
|
run(&mut s, "swatch.new", json!({"name": "Leaf", "color": {"c": 1, "m": 0, "y": 1, "k": 0}, "spot": true}));
|
|
let ink_to_cyan = json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": {"c": 1, "m": 0, "y": 0, "k": 0}}]});
|
|
let ink_to_leaf = json!({"stops": [{"offset": 0, "swatch": "Ink", "midpoint": 30}, {"offset": 1, "swatch": "Leaf", "tint": 50}]});
|
|
for (what, edit, space) in [
|
|
("Ink → cyan", with(ink_to_cyan.clone(), json!({"method": "perceptual"})), "/DeviceN[/Cyan/Ink]/DeviceCMYK"),
|
|
("Ink → Leaf 50 %", with(ink_to_leaf, json!({"method": "perceptual"})), "/DeviceN[/Ink/Leaf]/DeviceCMYK"),
|
|
] {
|
|
run(&mut s, "paint.editGradient", edit);
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(!pdf.contains("/Subtype/Image"), "{what}: no image");
|
|
assert!(pdf.contains("/ShadingType 2") && pdf.contains(space), "{what}: a DeviceN shading");
|
|
// The curve's stops: a stitching function of more than the two ends.
|
|
assert!(pdf.contains("/FunctionType 3"), "{what}: inks on the curve");
|
|
assert!(!warnings.iter().any(|w| w.contains("spot")), "{what}: {warnings:?}");
|
|
}
|
|
// The curve starts at the first stop's inks: no cyan, Ink at 100 %.
|
|
run(&mut s, "paint.editGradient", with(ink_to_cyan.clone(), json!({"method": "perceptual"})));
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(pdf.contains("/C0[0 1]"), "the first stop's inks");
|
|
// A radial one too.
|
|
run(&mut s, "paint.editGradient", with(ink_to_cyan, json!({"method": "perceptual", "kind": "radial"})));
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(!pdf.contains("/Subtype/Image") && pdf.contains("/ShadingType 3") && pdf.contains("/DeviceN[/Cyan/Ink]/DeviceCMYK"), "radial");
|
|
}
|
|
|
|
#[test]
|
|
fn perceptual_spot_gradients_with_rgb_stops_in_a_cmyk_document_export_as_device_n_shadings() {
|
|
let (mut s, id) = setup();
|
|
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": "#00ff00"}], "method": "perceptual"}));
|
|
assert!(gradient(&s, id).gradient.stops.iter().any(|st| matches!(st.color, Color::Rgb { .. })), "an RGB stop");
|
|
let (pdf, _) = pdf_of(&s);
|
|
assert!(pdf.contains("/Subtype/Image"), "an image in an RGB document");
|
|
// A CMYK document, or a conversion to a CMYK destination, writes the RGB stop in CMYK: a
|
|
// DeviceN shading, not an image.
|
|
let mut cmyk = doc(&s).clone();
|
|
cmyk.color_mode = vectorcraft_doc::ColorMode::Cmyk;
|
|
let plain = vectorcraft_pdf::PdfOptions { created: Some(0), ..vectorcraft_pdf::PdfOptions::uncompressed() };
|
|
let mut converted = plain.clone();
|
|
converted.settings.output = vectorcraft_pdf::OutputSettings {
|
|
conversion: vectorcraft_pdf::ColorConversion::Destination,
|
|
destination: vectorcraft_pdf::DOCUMENT_CMYK.into(),
|
|
..Default::default()
|
|
};
|
|
for (what, d, opts) in [("CMYK document", &cmyk, &plain), ("CMYK destination", doc(&s), &converted)] {
|
|
let r = vectorcraft_pdf::export_with_report(d, opts).unwrap();
|
|
let pdf = String::from_utf8_lossy(&r.bytes);
|
|
assert!(!pdf.contains("/Subtype/Image"), "{what}: no image");
|
|
assert!(pdf.contains("/ShadingType 2") && pdf.contains("/Ink]/DeviceCMYK"), "{what}: a DeviceN shading with Ink");
|
|
assert!(!r.warnings.iter().any(|w| w.contains("spot")), "{what}: {:?}", r.warnings);
|
|
}
|
|
// A conversion to an RGB destination keeps the RGB stop in RGB, in a CMYK document too: an
|
|
// image, with the spot warning.
|
|
let mut to_rgb = plain.clone();
|
|
to_rgb.settings.output = vectorcraft_pdf::OutputSettings {
|
|
conversion: vectorcraft_pdf::ColorConversion::Destination,
|
|
destination: vectorcraft_color::cms::SRGB.into(),
|
|
..Default::default()
|
|
};
|
|
let r = vectorcraft_pdf::export_with_report(&cmyk, &to_rgb).unwrap();
|
|
assert!(String::from_utf8_lossy(&r.bytes).contains("/Subtype/Image"), "RGB destination: an image");
|
|
assert!(r.warnings.iter().any(|w| w.contains("spot")), "RGB destination: {:?}", r.warnings);
|
|
}
|
|
|
|
#[test]
|
|
fn classic_spot_gradients_with_rgb_stops_in_a_cmyk_document_export_as_device_n_shadings() {
|
|
let (mut s, _) = setup();
|
|
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": "#00ff00"}]}));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(!pdf.contains("/DeviceN") && warnings.iter().any(|w| w.contains("spot")), "an RGB document drops the ink: {warnings:?}");
|
|
// A CMYK document, or a conversion to a CMYK destination, writes the RGB stop in CMYK: a
|
|
// DeviceN shading that keeps the ink.
|
|
let mut cmyk = doc(&s).clone();
|
|
cmyk.color_mode = vectorcraft_doc::ColorMode::Cmyk;
|
|
let plain = vectorcraft_pdf::PdfOptions { created: Some(0), ..vectorcraft_pdf::PdfOptions::uncompressed() };
|
|
let mut converted = plain.clone();
|
|
converted.settings.output = vectorcraft_pdf::OutputSettings {
|
|
conversion: vectorcraft_pdf::ColorConversion::Destination,
|
|
destination: vectorcraft_pdf::DOCUMENT_CMYK.into(),
|
|
..Default::default()
|
|
};
|
|
for (what, d, opts) in [("CMYK document", &cmyk, &plain), ("CMYK destination", doc(&s), &converted)] {
|
|
let r = vectorcraft_pdf::export_with_report(d, opts).unwrap();
|
|
let pdf = String::from_utf8_lossy(&r.bytes);
|
|
assert!(pdf.contains("/ShadingType 2") && pdf.contains("/Ink]/DeviceCMYK"), "{what}: a DeviceN shading with Ink");
|
|
assert!(!r.warnings.iter().any(|w| w.contains("spot")), "{what}: {:?}", r.warnings);
|
|
}
|
|
// A conversion to an RGB destination keeps the RGB stop in RGB, in a CMYK document too.
|
|
let mut to_rgb = plain.clone();
|
|
to_rgb.settings.output = vectorcraft_pdf::OutputSettings {
|
|
conversion: vectorcraft_pdf::ColorConversion::Destination,
|
|
destination: vectorcraft_color::cms::SRGB.into(),
|
|
..Default::default()
|
|
};
|
|
let r = vectorcraft_pdf::export_with_report(&cmyk, &to_rgb).unwrap();
|
|
assert!(!String::from_utf8_lossy(&r.bytes).contains("/DeviceN"), "RGB destination: no DeviceN shading");
|
|
assert!(r.warnings.iter().any(|w| w.contains("spot")), "RGB destination: {:?}", r.warnings);
|
|
}
|
|
|
|
#[test]
|
|
fn spot_gradients_drawn_as_images_warn() {
|
|
let (mut s, _) = setup();
|
|
let spot_warning = |w: &[String]| w.iter().any(|w| w.contains("spot colors in gradients exported as images"));
|
|
let ink_to_green = json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": "#00ff00"}]});
|
|
// More stops than the export finds tints for: an image, as before.
|
|
let n = 4097;
|
|
let stops: Vec<Value> = (0..n)
|
|
.map(|i| {
|
|
let offset = i as f64 / (n - 1) as f64;
|
|
if i % 2 == 0 { json!({"offset": offset, "swatch": "Ink"}) } else { json!({"offset": offset, "color": "#ffffff"}) }
|
|
})
|
|
.collect();
|
|
for (what, edit) in [
|
|
("Perceptual Ink → RGB green", with(ink_to_green, json!({"method": "perceptual"}))),
|
|
("dithered Classic Ink → paper", with(ink_to_white(100.0), json!({"dither": true, "method": "classic"}))),
|
|
("dithered Perceptual Ink → paper", with(ink_to_white(100.0), json!({"dither": true, "method": "perceptual"}))),
|
|
("Perceptual Ink and paper, 4,097 stops", json!({"stops": stops, "dither": false, "method": "perceptual"})),
|
|
] {
|
|
run(&mut s, "paint.editGradient", edit);
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/Subtype/Image"), "{what}: an image");
|
|
assert!(spot_warning(&warnings), "{what}: {warnings:?}");
|
|
}
|
|
// A Perceptual gradient without spot inks is an image, without the warning.
|
|
let blue_yellow = json!({"stops": [{"offset": 0, "color": "#0000ff"}, {"offset": 1, "color": "#ffff00"}]});
|
|
run(&mut s, "paint.editGradient", with(blue_yellow, json!({"method": "perceptual", "dither": false})));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/Subtype/Image") && !pdf.contains("/Separation"), "a process Perceptual gradient");
|
|
assert!(!spot_warning(&warnings), "{warnings:?}");
|
|
}
|
|
|
|
/// A spot gradient mixed around the hue wheel is an image in process colors with the warning,
|
|
/// where the same stops mixed by Perceptual are a Separation or DeviceN shading: the colors
|
|
/// between its stops are not tints or mixes of the stops' inks.
|
|
#[test]
|
|
fn hue_wheel_spot_gradients_export_as_images_and_warn() {
|
|
let (mut s, _) = setup();
|
|
run(&mut s, "swatch.new", json!({"name": "Leaf", "color": {"c": 1, "m": 0, "y": 1, "k": 0}, "spot": true}));
|
|
let spot_warning = |w: &[String]| w.iter().any(|w| w.contains("spot colors in gradients exported as images"));
|
|
let ink_to_leaf = json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "swatch": "Leaf"}]});
|
|
for method in ["hue-clockwise", "hue-counterclockwise"] {
|
|
for (what, stops) in [("Ink → paper", ink_to_white(80.0)), ("Ink → Leaf", ink_to_leaf.clone())] {
|
|
for kind in ["linear", "radial", "conic", "diamond"] {
|
|
run(&mut s, "paint.editGradient", with(stops.clone(), json!({"method": method, "kind": kind})));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/Subtype/Image"), "{method} {kind} {what}: an image");
|
|
assert!(!pdf.contains("/Separation") && !pdf.contains("/DeviceN"), "{method} {kind} {what}: no spot ink shading");
|
|
assert!(spot_warning(&warnings), "{method} {kind} {what}: {warnings:?}");
|
|
}
|
|
}
|
|
// Without spot inks: an image, without the warning.
|
|
let red_blue = json!({"stops": [{"offset": 0, "color": "#ff0000"}, {"offset": 1, "color": "#0000ff"}], "kind": "linear"});
|
|
run(&mut s, "paint.editGradient", with(red_blue, json!({"method": method})));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/Subtype/Image"), "{method}: a process gradient is an image");
|
|
assert!(!spot_warning(&warnings), "{method}: {warnings:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn freeform_gradients_without_points_warn_about_spot_stops() {
|
|
let (mut s, id) = setup();
|
|
run(&mut s, "paint.editGradient", with(ink_to_white(100.0), json!({"kind": "freeform"})));
|
|
// A freeform field with no points of its own: the colors come from the stops, the first
|
|
// linked to the spot swatch.
|
|
let mut g = gradient(&s, id);
|
|
g.freeform = Some(vectorcraft_color::Freeform::default());
|
|
g.gradient.stops[0].swatch = Some("Ink".into());
|
|
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
|
d.node_mut(id).unwrap().appearance.set_fill(Paint::Gradient(Box::new(g)));
|
|
let (pdf, warnings) = pdf_of(&s);
|
|
assert!(pdf.contains("/Subtype/Image"), "an image");
|
|
assert!(warnings.iter().any(|w| w.contains("spot colors in gradients exported as images")), "{warnings:?}");
|
|
}
|