Merge pull request #9102 from dotCooCoo/feat/colorized-image-overprint

Images: Overprint Fill makes a colorized image's color overprint
This commit is contained in:
BFlat
2026-11-24 16:39:55 +07:00
committed by GitHub
30 changed files with 1176 additions and 112 deletions
+12
View File
@@ -13,6 +13,18 @@ pub struct Effect {
pub visible: bool,
}
impl Effect {
/// Whether this effect gives an image's fill or stroke a shape to paint: it is a visible
/// Convert to Shape or Outline Object.
pub fn gives_image_shape(&self) -> bool {
self.visible
&& matches!(
self.id.as_str(),
"convertToShape.rectangle" | "convertToShape.roundedRectangle" | "convertToShape.ellipse" | "path.outlineObject"
)
}
}
fn yes() -> bool {
true
}
+27
View File
@@ -1340,6 +1340,33 @@ mod tests {
assert!(piece.knockout_outline().is_none());
}
/// Each piece of a mesh-warped colorized image keeps its color and whether it overprints.
#[test]
fn mesh_warp_image_pieces_keep_an_overprinting_color() {
let im = crate::ImageObject {
key: "pic".into(),
width: 4,
height: 4,
xf: Affine::scale(10.0),
link: None,
placement: Default::default(),
pixels: None,
};
let mut n = Node::new(NodeId(3), NodeKind::Image(Box::new(im.clone())));
n.extras_mut().colorize = Some(Box::new(Paint::solid(Color::BLACK)));
n.extras_mut().colorize_overprint = true;
for masked in [false, true] {
let g = mesh_warp_image(&n, &im, &|p| p, 2, false, masked);
let mut pieces = vec![];
g.walk(&mut |c| {
if matches!(c.kind, NodeKind::Image(_)) {
pieces.push(c.colorize_overprints());
}
});
assert_eq!(pieces, [true; 8], "masked {masked}");
}
}
/// More cells than the objects live art may make cut no pieces (the count overflowed).
#[test]
fn mesh_warp_image_past_the_budget_makes_an_empty_group() {
+5 -2
View File
@@ -231,13 +231,15 @@ pub(crate) fn mesh_warp_image_with(
g.knockout = crate::Knockout::On;
if let Some(e) = g.extras_if_any() {
e.colorize = None;
e.colorize_overprint = false;
e.image_quad = None;
}
if mesh_warp_cost(k) > MAX_GENERATED {
return g;
}
// A grayscale image's fill color colorizes each piece; the group of pieces takes none.
let colorize = n.extras().colorize.clone();
// A grayscale image's fill color colorizes each piece, overprinting as the image's does; the
// group of pieces takes none.
let (colorize, colorize_overprint) = (n.extras().colorize.clone(), n.extras().colorize_overprint);
// Each piece shows what the image shows: its color mask's masked pixels.
let color_mask = n.extras().color_mask.clone();
let (pw, ph) = (im.width as f64, im.height as f64);
@@ -271,6 +273,7 @@ pub(crate) fn mesh_warp_image_with(
let mut image = Node::new(n.id, NodeKind::Image(Box::new(ImageObject { xf, ..im.clone() })));
image.extras_mut().pixelated = pixelated;
image.extras_mut().colorize = colorize.clone();
image.extras_mut().colorize_overprint = colorize_overprint;
image.extras_mut().color_mask = color_mask.clone();
if masked {
// Preserve Shape Using Transparency: the triangle is the piece's alpha (a
+5
View File
@@ -63,6 +63,10 @@ pub struct NodeExtras {
/// colorizes it. Each pixel is drawn in this color, as opaque as the pixel is dark, so white
/// pixels are transparent. `None`: the image shows its own pixels.
pub colorize: Option<Box<vectorcraft_color::Paint>>,
/// Attributes panel › Overprint Fill on a colorized image: its color prints over the inks
/// below instead of knocking them out. It counts only while [`Self::colorize`] is set
/// ([`crate::Node::colorize_overprints`]).
pub colorize_overprint: bool,
/// Pathfinder panel › Make Compound Shape: this group is a compound shape
/// ([`Node::is_compound_shape`], [`crate::compound_shape`]).
pub compound_shape: bool,
@@ -115,6 +119,7 @@ pub(super) static NO_EXTRAS: NodeExtras = NodeExtras {
live_paint_gaps: None,
objects_on_path: None,
colorize: None,
colorize_overprint: false,
compound_shape: false,
shape_mode: crate::ShapeMode::Add,
symbol_overrides: Vec::new(),
+5
View File
@@ -51,6 +51,7 @@ impl Serialize for Node {
e.live_paint_gaps.is_none(),
e.objects_on_path.is_none(),
e.colorize.is_none(),
is_default(&e.colorize_overprint),
is_default(&e.compound_shape),
is_default(&e.shape_mode),
e.symbol_overrides.is_empty(),
@@ -99,6 +100,7 @@ impl Serialize for Node {
field!("live_paint_gaps", &e.live_paint_gaps);
field!("objects_on_path", &e.objects_on_path);
field!("colorize", &e.colorize);
field!("colorize_overprint", &e.colorize_overprint);
field!("compound_shape", &e.compound_shape);
field!("shape_mode", &e.shape_mode);
field!("symbol_overrides", &e.symbol_overrides);
@@ -244,6 +246,8 @@ pub(super) struct NodeWire {
#[serde(default)]
colorize: Option<Box<vectorcraft_color::Paint>>,
#[serde(default)]
colorize_overprint: bool,
#[serde(default)]
compound_shape: bool,
#[serde(default)]
shape_mode: crate::ShapeMode,
@@ -477,6 +481,7 @@ impl From<NodeWire> for Node {
live_paint_gaps: w.live_paint_gaps,
objects_on_path: w.objects_on_path,
colorize: w.colorize,
colorize_overprint: w.colorize_overprint,
compound_shape: w.compound_shape,
shape_mode: w.shape_mode,
symbol_overrides: w.symbol_overrides,
+340 -11
View File
@@ -2,11 +2,12 @@
//! over the inks below instead of knocking them out ([`crate::FillLayer::overprint`]).
//! Edit → Edit Colors → Overprint Black sets it on black fills and strokes ([`OverprintBlack`]).
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
use vectorcraft_color::{BlendMode, Color, GradientKind, Paint};
use crate::{Document, Node, NodeId, NodeKind};
use crate::{Document, ImageBlob, Node, NodeId, NodeKind};
/// `Document.unknown` key of files from before per-fill/stroke overprint: the ids of objects
/// marked Overprint Black ([`Document::migrate_overprint_black`]).
@@ -65,7 +66,8 @@ impl OverprintBlack {
}
}
/// Set (or clear) the overprint of `n`'s own black fills and strokes and its characters'.
/// Set (or clear) the overprint of `n`'s own black fills and strokes, its characters' and,
/// for a colorized image, its color's.
/// `spots`: the document's spot swatch names ([`Document::spot_names`]). Returns whether
/// anything changed.
pub fn apply(&self, n: &mut Node, spots: &[String]) -> bool {
@@ -90,9 +92,18 @@ impl OverprintBlack {
}
}
}
if self.colorize_black(n, spots) {
set(&mut n.extras_mut().colorize_overprint);
}
changed
}
/// Whether `n` is an image colorized with a black color that [`Self::fill`] covers: the color
/// of a gray or 1-bit image is its fill ([`crate::NodeExtras::colorize`]).
fn colorize_black(&self, n: &Node, spots: &[String]) -> bool {
self.fill && matches!(n.kind, NodeKind::Image(_)) && n.extras().colorize.as_deref().is_some_and(|p| self.black(p, spots))
}
/// [`Self::apply`] to every node of `a`'s subtree, copying only the nodes on the way to those
/// that change. Returns whether anything changed.
pub fn apply_deep(&self, a: &mut Arc<Node>, spots: &[String]) -> bool {
@@ -136,38 +147,138 @@ impl OverprintBlack {
(self.fill && r.style.overprint_fill != on && self.black(&r.style.fill, spots))
|| (self.stroke && r.style.overprint_stroke != on && self.black(&r.style.stroke, spots))
};
n.appearance.items.iter().any(item) || matches!(&n.kind, NodeKind::Text(t) if t.runs.iter().any(run))
n.appearance.items.iter().any(item)
|| matches!(&n.kind, NodeKind::Text(t) if t.runs.iter().any(run))
|| (n.extras().colorize_overprint != on && self.colorize_black(n, spots))
}
}
impl Node {
/// Whether any fill or stroke of this subtree overprints (characters' aside).
pub fn has_overprint(&self) -> bool {
self.appearance.items.iter().any(|i| i.overprint()) || self.children().is_some_and(|c| c.iter().any(|c| c.has_overprint()))
self.appearance.items.iter().any(|i| i.overprint())
|| self.colorize_overprints()
|| self.children().is_some_and(|c| c.iter().any(|c| c.has_overprint()))
}
/// Whether this is an image colorized with a color ([`crate::NodeExtras::colorize`]) that
/// overprints ([`crate::NodeExtras::colorize_overprint`]).
pub fn colorize_overprints(&self) -> bool {
let e = self.extras();
matches!(self.kind, NodeKind::Image(_)) && e.colorize_overprint && e.colorize.as_deref().is_some_and(|p| !p.is_none())
}
}
/// Whether fill or stroke row `i` of an image draws: it is visible, has a paint and an opacity
/// above 0 (a stroke a width too) and a visible Convert to Shape or Outline Object, which gives a
/// row on an image a shape of its own ([`crate::Effect::gives_image_shape`]).
pub fn image_row_draws(i: &crate::AppearanceItem) -> bool {
let width = match i {
crate::AppearanceItem::Stroke(s) => s.width > 0.0,
crate::AppearanceItem::Fill(_) => true,
};
i.visible() && i.opacity() > 0.0 && !i.paint().is_none() && width && i.effects().iter().any(crate::Effect::gives_image_shape)
}
/// Whether paint `paint` of blend mode `blend` darkens when `overprint` asks it to: with the
/// Normal blend, and not a white that `discard_white` knocks out ([`DiscardWhite::discards`]).
fn darkens(overprint: bool, blend: BlendMode, paint: &Paint, discard_white: &DiscardWhite) -> bool {
overprint && blend == BlendMode::Normal && !discard_white.discards(paint)
}
/// The effects that only move or reshape what an object draws, keeping its own colors, besides
/// those whose ids start with `warp.`, `convertToShape.` or `addon.path.` ([`reshapes_only`]).
const RESHAPING_EFFECTS: [&str; 12] = [
"distort.freeDistort",
"distort.puckerBloat",
"distort.roughen",
"distort.transform",
"distort.tweak",
"distort.twist",
"distort.zigZag",
"path.offsetPath",
"path.outlineObject",
"path.outlineStroke",
"stylize.roundCorners",
"stylize.scribble",
];
/// Whether effect `id` only moves or reshapes what an object draws ([`RESHAPING_EFFECTS`]).
/// Any other effect (a shadow, a glow, a raster effect, a color adjustment, a plug-in effect)
/// paints colors of its own.
pub fn reshapes_only(id: &str) -> bool {
["warp.", "convertToShape.", "addon.path."].iter().any(|p| id.starts_with(p)) || RESHAPING_EFFECTS.contains(&id)
}
/// Whether `n` is an image with no pixels of its own and a colorize paint.
pub fn is_colorized_image(n: &Node) -> bool {
matches!(n.kind, NodeKind::Image(ref im) if im.pixels.is_none()) && n.extras().colorize.is_some()
}
/// The key of the pixels colorized image `n` shows in `images`: its color mask's when the mask
/// was made for it and is there ([`Document::shown_image`]), else its own. `None` for any other
/// node.
pub fn colorized_shown_key(n: &Node, images: &BTreeMap<String, ImageBlob>) -> Option<String> {
match &n.kind {
NodeKind::Image(im) if is_colorized_image(n) => {
let mask = Document::color_mask_of(n).filter(|m| m.made_for(im) && images.contains_key(&m.key));
Some(mask.map_or(&im.key, |m| &m.key).clone())
}
_ => None,
}
}
/// Whether colorized image `n` shows pixels in `images` that don't take its colorize paint
/// ([`ImageBlob::takes_colorize`]), so it is drawn and separated as those pixels and not as its
/// color.
pub fn refuses_colorize(n: &Node, images: &BTreeMap<String, ImageBlob>) -> bool {
colorized_shown_key(n, images).and_then(|key| images.get(&key)).is_some_and(|b| !b.takes_colorize())
}
/// Whether colorized image `n` is drawn overprinting as a whole: its color overprints
/// ([`Node::colorize_overprints`]) with the Normal blend and is not a white `discard_white` knocks out,
/// the pixels it shows in `images` take its color ([`refuses_colorize`]), each of its own visible
/// effects only reshapes it ([`reshapes_only`]), and each row that draws ([`image_row_draws`])
/// overprints that way too. A colorized image's color is not a fill row, so the whole node
/// overprints or none of it does.
pub fn image_overprints_whole(n: &Node, discard_white: &DiscardWhite, images: &BTreeMap<String, ImageBlob>) -> bool {
let row = |i: &crate::AppearanceItem| match i {
crate::AppearanceItem::Fill(l) => darkens(l.overprint, l.blend, &l.paint, discard_white),
crate::AppearanceItem::Stroke(l) => darkens(l.overprint, l.blend, &l.paint, discard_white),
};
let paints_past = n.appearance.effects.iter().any(|e| e.visible && !reshapes_only(&e.id));
n.colorize_overprints()
&& n.extras().colorize.as_deref().is_some_and(|p| darkens(true, n.blend, p, discard_white))
&& !paints_past
&& n.appearance.items.iter().filter(|i| image_row_draws(i)).all(row)
&& !refuses_colorize(n, images)
}
/// Draw the overprinting fills and strokes of `a`'s subtree with Darken (those with a blend
/// mode of their own keep it), copying only the nodes on the way to them: on screen and on a
/// printed plate this approximates their inks printing over the inks below. Darken keeps the
/// stronger ink of each plate (and each additive channel), so a colour overprinting itself stays
/// that colour, where Multiply would darken it. With `discard_white`
/// (Document Setup → Discard White Overprint) white fills and strokes knock out instead.
pub fn darken_overprints(a: &mut Arc<Node>, discard_white: bool) {
/// that colour, where Multiply would darken it. White fills and strokes that `discard_white`
/// (Document Setup → Discard White Overprint) knocks out do so instead. A colorized image
/// darkens as a whole ([`image_overprints_whole`], with the document's `images`).
pub fn darken_overprints(a: &mut Arc<Node>, discard_white: &DiscardWhite, images: &BTreeMap<String, ImageBlob>) {
if !a.has_overprint() {
return;
}
let n = Arc::make_mut(a);
let darkens = |overprint: bool, blend: BlendMode, paint: &Paint| overprint && blend == BlendMode::Normal && !(discard_white && is_white(paint));
let whole = image_overprints_whole(n, discard_white, images);
for it in &mut n.appearance.items {
match it {
crate::AppearanceItem::Fill(l) if darkens(l.overprint, l.blend, &l.paint) => l.blend = BlendMode::Darken,
crate::AppearanceItem::Stroke(l) if darkens(l.overprint, l.blend, &l.paint) => l.blend = BlendMode::Darken,
crate::AppearanceItem::Fill(l) if darkens(l.overprint, l.blend, &l.paint, discard_white) => l.blend = BlendMode::Darken,
crate::AppearanceItem::Stroke(l) if darkens(l.overprint, l.blend, &l.paint, discard_white) => l.blend = BlendMode::Darken,
_ => {}
}
}
if whole {
n.blend = BlendMode::Darken;
}
for c in n.children_mut().into_iter().flatten() {
darken_overprints(c, discard_white);
darken_overprints(c, discard_white, images);
}
}
@@ -182,12 +293,53 @@ pub fn is_white(p: &Paint) -> bool {
}
}
/// Discard White Overprint (Document Setup) as output applies it: whether it is on, and the spot
/// inks a white tint of which still overprints ([`Document::spot_ink`]).
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DiscardWhite {
on: bool,
spot_inks: BTreeSet<String>,
}
impl DiscardWhite {
/// The document's setting and spot inks.
pub fn of(doc: &Document) -> Self {
let spot_inks = doc.swatches_iter().map(|s| &s.name).filter(|n| doc.swatch(n).is_some_and(|s| s.spot && s.paint.color().is_some()));
Self { on: doc.setup.discard_white_overprint, spot_inks: spot_inks.cloned().collect() }
}
/// The setting `on`, with no spot inks.
pub fn new(on: bool) -> Self {
Self { on, spot_inks: BTreeSet::new() }
}
/// Whether `p` loses its overprint: the setting is on and `p` is paper white
/// ([`is_white`]), not a tint above 0% of a spot ink, which prints as that ink.
pub fn discards(&self, p: &Paint) -> bool {
let spot = matches!(p, Paint::Solid { swatch: Some(name), tint, .. } if *tint > 0.0 && self.spot_inks.contains(name));
self.on && is_white(p) && !spot
}
}
impl Document {
/// The names of the spot swatches.
pub fn spot_names(&self) -> Vec<String> {
self.swatches_iter().filter(|s| s.spot).map(|s| s.name.clone()).collect()
}
/// Whether `p` is a tint above 0% of a spot swatch with a solid color, which prints as an ink
/// of its own even when its color is white ([`is_white`]); a link is judged by the first
/// swatch of its name.
pub fn spot_ink(&self, p: &Paint) -> bool {
matches!(p, Paint::Solid { swatch: Some(name), tint, .. } if *tint > 0.0 && self.swatch(name).is_some_and(|s| s.spot && s.paint.color().is_some()))
}
/// Whether Discard White Overprint (Document Setup) takes the overprint off `p`
/// ([`DiscardWhite::discards`]).
pub fn discards_white_overprint(&self, p: &Paint) -> bool {
self.setup.discard_white_overprint && is_white(p) && !self.spot_ink(p)
}
/// Files from before per-fill/stroke overprint listed the objects marked Overprint Black
/// under [`LEGACY_OVERPRINT_KEY`]: their 100% black fills and strokes overprint now.
pub fn migrate_overprint_black(&mut self) {
@@ -208,6 +360,7 @@ impl Document {
/// Whether any fill or stroke of `n`'s subtree overprints, characters' too.
fn overprints_anywhere(n: &Node) -> bool {
n.appearance.items.iter().any(|i| i.overprint())
|| n.extras().colorize_overprint
|| matches!(&n.kind, NodeKind::Text(t) if t.runs.iter().any(|r| r.style.overprint_fill || r.style.overprint_stroke))
|| n.children().is_some_and(|c| c.iter().any(|c| overprints_anywhere(c)))
}
@@ -227,6 +380,10 @@ pub fn clear_overprints(a: &mut Arc<Node>) {
(r.style.overprint_fill, r.style.overprint_stroke) = (false, false);
}
}
// Also the flag an image keeps after its color is removed.
if n.extras().colorize_overprint {
n.extras_mut().colorize_overprint = false;
}
for c in n.children_mut().into_iter().flatten() {
clear_overprints(c);
}
@@ -348,6 +505,178 @@ mod tests {
assert!(Arc::ptr_eq(&group, &marked), "and nothing is copied");
}
/// A gray image colorized with `color`, its color overprinting, with the fill row `fill`.
fn colorized(color: Paint, fill: Paint) -> Node {
let im = crate::ImageObject {
key: "pic".into(),
width: 1,
height: 1,
xf: vectorcraft_geom::Affine::IDENTITY,
link: None,
placement: Default::default(),
pixels: None,
};
let mut n = Node::new(NodeId(1), NodeKind::Image(Box::new(im)));
n.appearance = Appearance::basic(fill, Paint::None, 0.0).into();
n.extras_mut().colorize = Some(Box::new(color));
n.extras_mut().colorize_overprint = true;
n
}
#[test]
fn a_white_colorized_image_knocks_out_under_discard_white() {
let image = |color: Paint, discard_white: bool| {
let mut a = Arc::new(colorized(color, Paint::None));
darken_overprints(&mut a, &DiscardWhite::new(discard_white), &BTreeMap::new());
a.blend
};
let white = Paint::solid(Color::cmyk(0.0, 0.0, 0.0, 0.0));
assert_eq!(image(white.clone(), true), BlendMode::Normal, "white knocks out, as a white fill does");
assert_eq!(image(white, false), BlendMode::Darken);
assert_eq!(image(cmyk(0.0, 0.0, 0.0, 1.0), true), BlendMode::Darken);
}
/// A tint above 0% of a spot ink whose color is white keeps overprinting with Discard White
/// Overprint, as an image's color and as a fill; a 0% tint, a process white and a swatch that
/// is not spot knock out.
#[test]
fn a_white_spot_ink_keeps_overprinting_under_discard_white() {
let mut doc = Document::new(100.0, 100.0);
let white = Color::cmyk(0.0, 0.0, 0.0, 0.0);
doc.swatches.push(crate::Swatch { name: "White Ink".into(), paint: Paint::solid(white), global: true, spot: true });
doc.swatches.push(crate::Swatch { name: "Paper".into(), paint: Paint::solid(white), global: true, spot: false });
doc.setup.discard_white_overprint = true;
let discard = DiscardWhite::of(&doc);
let linked = |name: &str, tint: f32| Paint::Solid { color: white, swatch: Some(name.into()), tint };
let blend = |color: Paint, fill: Paint| {
let mut n = colorized(color, Paint::None);
if let Some(f) = n.appearance.fill_mut() {
f.paint = fill;
f.overprint = true;
}
let mut a = Arc::new(n);
darken_overprints(&mut a, &discard, &BTreeMap::new());
(a.blend, a.appearance.fill().map(|f| f.blend))
};
let spot = linked("White Ink", 0.5);
assert_eq!(blend(spot.clone(), spot.clone()), (BlendMode::Darken, Some(BlendMode::Darken)));
assert!(doc.spot_ink(&spot) && !discard.discards(&spot));
for knocks_out in [linked("White Ink", 0.0), linked("Paper", 1.0), Paint::solid(white)] {
assert!(discard.discards(&knocks_out), "{knocks_out:?}");
assert_eq!(blend(knocks_out.clone(), knocks_out.clone()).0, BlendMode::Normal, "{knocks_out:?}");
}
assert!(!DiscardWhite::new(false).discards(&Paint::solid(white)));
}
#[test]
fn a_colorized_image_showing_pixels_that_refuse_its_color_keeps_knocking_out() {
let blend = |images: &BTreeMap<String, ImageBlob>| {
let mut a = Arc::new(colorized(cmyk(0.0, 0.0, 0.0, 1.0), Paint::None));
darken_overprints(&mut a, &DiscardWhite::new(false), images);
a.blend
};
let bytes = crate::links::missing_placeholder().unwrap();
let preview = Arc::new(bytes.clone());
let shown = ImageBlob { mime: "image/png".into(), bytes: preview.clone(), proxy: Some(preview), file_font: Default::default() };
assert!(!shown.takes_colorize());
assert_eq!(blend(&BTreeMap::from([("pic".to_string(), shown)])), BlendMode::Normal, "a full-color preview is drawn as it is");
let own = ImageBlob::new("image/png", bytes);
assert_eq!(blend(&BTreeMap::from([("pic".to_string(), own)])), BlendMode::Darken, "the image's own pixels take the color");
}
#[test]
fn a_colorized_image_darkens_unless_its_rows_draw_an_effect() {
let blend = |effect: Option<(&str, bool)>, overprint: bool| {
let mut n = colorized(cmyk(0.0, 0.0, 0.0, 1.0), cmyk(1.0, 0.0, 0.0, 0.0));
if let Some(f) = n.appearance.fill_mut() {
f.overprint = overprint;
if let Some((id, visible)) = effect {
f.effects.push(crate::Effect { id: id.into(), params: serde_json::json!({}), visible });
}
}
let mut a = Arc::new(n);
darken_overprints(&mut a, &DiscardWhite::new(false), &BTreeMap::new());
a.blend
};
let shape = "convertToShape.rectangle";
assert_eq!(blend(None, false), BlendMode::Darken, "a row with no effect paints nothing on an image");
assert_eq!(blend(Some((shape, false)), false), BlendMode::Darken, "nor one whose effect is hidden");
assert_eq!(blend(Some(("path.offsetPath", true)), false), BlendMode::Darken, "nor one whose effect gives it no shape");
assert_eq!(blend(Some((shape, true)), false), BlendMode::Normal, "a knocking-out row drawn by its effect keeps knocking out");
assert_eq!(blend(Some(("path.outlineObject", true)), false), BlendMode::Normal, "Outline Object gives a row a shape too");
assert_eq!(blend(Some((shape, true)), true), BlendMode::Darken, "with an overprinting row the whole image overprints");
}
#[test]
fn a_colorized_image_darkens_past_a_row_with_no_opacity() {
let blend = |stroke: bool, opacity: f32| {
let mut n = colorized(cmyk(0.0, 0.0, 0.0, 1.0), Paint::None);
let effect = crate::Effect { id: "convertToShape.rectangle".into(), params: serde_json::json!({}), visible: true };
if stroke {
if let Some(s) = n.appearance.stroke_mut() {
s.paint = cmyk(1.0, 0.0, 0.0, 0.0);
s.width = 1.0;
s.opacity = opacity;
s.effects.push(effect);
}
} else if let Some(f) = n.appearance.fill_mut() {
f.paint = cmyk(1.0, 0.0, 0.0, 0.0);
f.opacity = opacity;
f.effects.push(effect);
}
let mut a = Arc::new(n);
darken_overprints(&mut a, &DiscardWhite::new(false), &BTreeMap::new());
a.blend
};
assert_eq!(blend(false, 0.0), BlendMode::Darken, "a fill with no opacity paints nothing");
assert_eq!(blend(true, 0.0), BlendMode::Darken, "nor a stroke with no opacity");
assert_eq!(blend(false, 0.5), BlendMode::Normal, "a fill with some opacity still knocks out");
assert_eq!(blend(true, 0.5), BlendMode::Normal, "and so does a stroke");
}
#[test]
fn a_colorized_image_with_a_node_effect_keeps_knocking_out() {
let blend = |id: &str, visible: bool| {
let mut n = colorized(cmyk(0.0, 0.0, 0.0, 1.0), Paint::None);
n.appearance.effects.push(crate::Effect { id: id.into(), params: serde_json::json!({}), visible });
let mut a = Arc::new(n);
darken_overprints(&mut a, &DiscardWhite::new(false), &BTreeMap::new());
a.blend
};
assert_eq!(blend("stylize.dropShadow", true), BlendMode::Normal, "a drop shadow is not darkened with the image");
assert_eq!(blend("stylize.outerGlow", true), BlendMode::Normal, "nor an outer glow");
assert_eq!(blend("stylize.innerGlow", true), BlendMode::Normal, "nor an inner glow");
assert_eq!(blend("artistic.neonGlow", true), BlendMode::Normal, "nor a raster effect");
assert_eq!(blend("stylize.dropShadow", false), BlendMode::Darken, "a hidden effect draws nothing");
assert_eq!(blend("distort.roughen", true), BlendMode::Darken, "a geometry effect reshapes the image itself");
assert_eq!(blend("warp.arc", true), BlendMode::Darken, "so does a warp");
}
#[test]
fn overprint_black_takes_a_black_colorized_image() {
let off = |mut n: Node| {
n.extras_mut().colorize_overprint = false;
Arc::new(n)
};
let mut a = off(colorized(cmyk(0.0, 0.0, 0.0, 1.0), Paint::None));
assert!(!OverprintBlack { fill: false, ..Default::default() }.apply_deep(&mut a, &[]), "the color is the image's fill");
assert!(OverprintBlack::default().apply_deep(&mut a, &[]) && a.colorize_overprints());
assert!(!OverprintBlack::default().apply_deep(&mut a, &[]), "nothing left to change");
assert!(OverprintBlack { remove: true, ..Default::default() }.apply_deep(&mut a, &[]) && !a.colorize_overprints());
let mut cyan = off(colorized(cmyk(1.0, 0.0, 0.0, 0.0), Paint::None));
assert!(!OverprintBlack::default().apply_deep(&mut cyan, &[]), "a cyan image is not black");
}
#[test]
fn clearing_overprints_clears_the_flag_of_an_image_without_a_color() {
let mut n = colorized(cmyk(0.0, 0.0, 0.0, 1.0), Paint::None);
n.extras_mut().colorize = None;
let mut a = Arc::new(n);
clear_overprints(&mut a);
Arc::make_mut(&mut a).extras_mut().colorize = Some(Box::new(cmyk(0.0, 0.0, 0.0, 1.0)));
assert!(!a.colorize_overprints(), "a color given later knocks out");
}
#[test]
fn legacy_overprint_black_list_migrates() {
let mut d = Document::new(100.0, 100.0);