Images: Overprint Fill makes a colorized image's color overprint

A grayscale or 1-bit image colorized with a fill color had no way to
overprint: object.setOverprint {fill: true} changed nothing, since an
image has no fill row. The color now carries its own overprint flag
(colorize_overprint in native files).

- object.setOverprint (fill, without item) and attributes.info set and
  read it; Overprint Black and Print Ink Fixes' Remove White Overprint
  treat the color as the image's fill.
- Overprint Preview, Separations Preview and printed separations show
  the inks below the image where its color has none; on a plate the
  color puts no ink on, the image is left out.
- PDF export draws the image in an overprinting graphics state, a
  process color as an image of only the process inks it puts down.
- EPS export turns overprinting on for it, and now leaves white fills,
  strokes and colorized images knocking out with Discard White
  Overprint, as PDF export does.
- A legacy .ai file writes it as the image mask's fill overprint and
  reads it back. Warped pieces, geometry-effect copies and flattening
  keep or drop it as they do for fills.
- A white spot color is an ink: with Discard White Overprint it keeps
  overprinting in PDF and EPS export and in both previews.

Docs: docs/mcp.md (Overprint, Print Ink Fixes), docs/target-app-parity.md.
This commit is contained in:
dotCooCoo
2026-11-22 06:15:41 -08:00
parent db9acab4da
commit 4f73ef7fbb
29 changed files with 583 additions and 83 deletions
+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() {
+4 -2
View File
@@ -236,8 +236,9 @@ pub(crate) fn mesh_warp_image_with(
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 +272,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,
+189 -9
View File
@@ -65,7 +65,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 +91,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,36 +146,115 @@ 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())
}
}
/// Document Setup → Discard White Overprint as it applies to paints: off, or on with the names of
/// the document's spot swatches. Paper white knocks out where it overprints; the color of a spot
/// swatch is an ink of its own, so a white spot ink still overprints.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DiscardWhite(Option<Vec<String>>);
impl DiscardWhite {
/// The setting of document `doc`.
pub fn of(doc: &Document) -> Self {
Self(doc.setup.discard_white_overprint.then(|| doc.spot_names()))
}
/// Whether `p` knocks out instead of overprinting.
pub fn knocks_out(&self, p: &Paint) -> bool {
self.0.as_ref().is_some_and(|spots| is_paper_white(p, |name| spots.iter().any(|s| s == name)))
}
}
/// Whether `p` is paper white ([`is_white`]) and not linked to a swatch that `spot` says is a
/// spot ink.
fn is_paper_white(p: &Paint, spot: impl Fn(&str) -> bool) -> bool {
is_white(p) && !matches!(p, Paint::Solid { swatch: Some(name), .. } if spot(name))
}
impl Document {
/// Whether paint `p` is paper white: white ([`is_white`]) and not linked to a spot swatch,
/// whose white is an ink.
pub fn is_paper_white(&self, p: &Paint) -> bool {
is_paper_white(p, |name| self.swatch(name).is_some_and(|w| w.spot))
}
/// Whether paint `p` knocks out where it overprints under Document Setup → Discard White
/// Overprint ([`DiscardWhite`]).
pub fn discards_white(&self, p: &Paint) -> bool {
self.setup.discard_white_overprint && self.is_paper_white(p)
}
}
/// Whether fill or stroke row `i` of an image draws: it is visible, has a paint and a visible
/// effect, which gives a row on an image a shape of its own.
pub fn image_row_draws(i: &crate::AppearanceItem) -> bool {
i.visible() && !i.paint().is_none() && i.effects().iter().any(|e| e.visible)
}
/// Whether paint `paint` of blend mode `blend` darkens when `overprint` asks it to: with the
/// Normal blend, and not knocked out by `discard_white`.
fn darkens(overprint: bool, blend: BlendMode, paint: &Paint, discard_white: &DiscardWhite) -> bool {
overprint && blend == BlendMode::Normal && !discard_white.knocks_out(paint)
}
/// Whether colorized image `n` is drawn overprinting as a whole: its color overprints
/// ([`Node::colorize_overprints`]) with the Normal blend and is not knocked out by
/// `discard_white`, 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) -> 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),
};
n.colorize_overprints()
&& n.extras().colorize.as_deref().is_some_and(|p| darkens(true, n.blend, p, discard_white))
&& n.appearance.items.iter().filter(|i| image_row_draws(i)).all(row)
}
/// 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. Paints that `discard_white` knocks out
/// (Document Setup → Discard White Overprint) are left as they are. A colorized image darkens as
/// a whole ([`image_overprints_whole`]).
pub fn darken_overprints(a: &mut Arc<Node>, discard_white: &DiscardWhite) {
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);
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);
}
@@ -208,6 +297,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.colorize_overprints()
|| 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 +317,9 @@ pub fn clear_overprints(a: &mut Arc<Node>) {
(r.style.overprint_fill, r.style.overprint_stroke) = (false, false);
}
}
if n.colorize_overprints() {
n.extras_mut().colorize_overprint = false;
}
for c in n.children_mut().into_iter().flatten() {
clear_overprints(c);
}
@@ -348,6 +441,93 @@ 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_spot_ink_overprints_under_discard_white() {
let mut d = Document::new(100.0, 100.0);
let white = Color::cmyk(0.0, 0.0, 0.0, 0.0);
d.swatches.push(vectorcraft_color::Swatch { name: "Opaque White".into(), paint: Paint::solid(white), global: true, spot: true });
let spot = Paint::Solid { color: white, swatch: Some("Opaque White".into()), tint: 1.0 };
let paper = Paint::solid(white);
d.setup.discard_white_overprint = true;
let discard = DiscardWhite::of(&d);
assert!(discard.knocks_out(&paper) && d.discards_white(&paper), "paper white knocks out");
assert!(!discard.knocks_out(&spot) && !d.discards_white(&spot), "a white spot ink is an ink");
d.setup.discard_white_overprint = false;
assert!(!DiscardWhite::of(&d).knocks_out(&paper) && !d.discards_white(&paper), "off: white overprints too");
// The previews darken the spot ink and leave paper white knocking out.
let blend = |fill: Paint| {
let mut n = Node::path(NodeId(1), Default::default(), Appearance::basic(fill, Paint::None, 0.0));
if let Some(f) = n.appearance.fill_mut() {
f.overprint = true;
}
let mut a = Arc::new(n);
darken_overprints(&mut a, &discard);
a.appearance.fill().map(|f| f.blend)
};
assert_eq!(blend(spot.clone()), Some(BlendMode::Darken));
assert_eq!(blend(paper.clone()), Some(BlendMode::Normal));
let image = |color: Paint| {
let mut a = Arc::new(colorized(color, Paint::None));
darken_overprints(&mut a, &discard);
a.blend
};
assert_eq!((image(spot), image(paper)), (BlendMode::Darken, BlendMode::Normal), "a colorized image's color too");
}
#[test]
fn a_colorized_image_darkens_unless_its_rows_draw_an_effect() {
let blend = |effect: Option<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(visible) = effect {
f.effects.push(crate::Effect { id: "path.offsetPath".into(), params: serde_json::json!({}), visible });
}
}
let mut a = Arc::new(n);
darken_overprints(&mut a, &DiscardWhite::default());
a.blend
};
assert_eq!(blend(None, false), BlendMode::Darken, "a row with no effect paints nothing on an image");
assert_eq!(blend(Some(false), false), BlendMode::Darken, "nor one whose effect is hidden");
assert_eq!(blend(Some(true), false), BlendMode::Normal, "a knocking-out row drawn by its effect keeps knocking out");
assert_eq!(blend(Some(true), true), BlendMode::Darken, "with an overprinting row the whole image overprints");
}
#[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 legacy_overprint_black_list_migrates() {
let mut d = Document::new(100.0, 100.0);