From 5a82ba3742d4057998a1c57955ed5dd9973346b2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 23:07:34 -0800 Subject: [PATCH] Overprint Preview: a colorized image showing a full-color preview keeps knocking out A linked image whose file is missing shows its full-color preview as it is, without its colorize paint, and separations and PDF export already print it that way. Overprint Preview and simulated overprints in composite output still darkened the whole image. darken_overprints and image_overprints_whole now take the document's images and leave such an image at its own blend mode, and the helpers that find the pixels a colorized image shows move from the separations code to vectorcraft_doc::overprint so both use them. The preview's cached art is made again when the document's images change. --- crates/doc/src/overprint.rs | 70 +++++++++++++++++++++++++++------- crates/pdf/src/print.rs | 2 +- crates/pdf/src/print/plates.rs | 38 ++++-------------- crates/render/src/proof.rs | 20 +++++++++- 4 files changed, 84 insertions(+), 46 deletions(-) diff --git a/crates/doc/src/overprint.rs b/crates/doc/src/overprint.rs index 1d70879e95..8cc4565ff1 100644 --- a/crates/doc/src/overprint.rs +++ b/crates/doc/src/overprint.rs @@ -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; 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`]). @@ -209,12 +210,38 @@ 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) -> Option { + 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) -> 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 white with `discard_white`, -/// 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: bool) -> bool { +/// 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: bool, images: &BTreeMap) -> 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), @@ -224,6 +251,7 @@ pub fn image_overprints_whole(n: &Node, discard_white: bool) -> bool { && 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 @@ -232,13 +260,13 @@ pub fn image_overprints_whole(n: &Node, discard_white: bool) -> bool { /// 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. A -/// colorized image darkens as a whole ([`image_overprints_whole`]). -pub fn darken_overprints(a: &mut Arc, discard_white: bool) { +/// colorized image darkens as a whole ([`image_overprints_whole`], with the document's `images`). +pub fn darken_overprints(a: &mut Arc, discard_white: bool, images: &BTreeMap) { if !a.has_overprint() { return; } let n = Arc::make_mut(a); - let whole = image_overprints_whole(n, discard_white); + 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, discard_white) => l.blend = BlendMode::Darken, @@ -250,7 +278,7 @@ pub fn darken_overprints(a: &mut Arc, discard_white: bool) { n.blend = BlendMode::Darken; } for c in n.children_mut().into_iter().flatten() { - darken_overprints(c, discard_white); + darken_overprints(c, discard_white, images); } } @@ -458,7 +486,7 @@ mod tests { 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, discard_white); + darken_overprints(&mut a, discard_white, &BTreeMap::new()); a.blend }; let white = Paint::solid(Color::cmyk(0.0, 0.0, 0.0, 0.0)); @@ -467,6 +495,22 @@ mod tests { assert_eq!(image(cmyk(0.0, 0.0, 0.0, 1.0), true), BlendMode::Darken); } + #[test] + fn a_colorized_image_showing_pixels_that_refuse_its_color_keeps_knocking_out() { + let blend = |images: &BTreeMap| { + let mut a = Arc::new(colorized(cmyk(0.0, 0.0, 0.0, 1.0), Paint::None)); + darken_overprints(&mut a, 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| { @@ -478,7 +522,7 @@ mod tests { } } let mut a = Arc::new(n); - darken_overprints(&mut a, false); + darken_overprints(&mut a, false, &BTreeMap::new()); a.blend }; let shape = "convertToShape.rectangle"; @@ -508,7 +552,7 @@ mod tests { f.effects.push(effect); } let mut a = Arc::new(n); - darken_overprints(&mut a, false); + darken_overprints(&mut a, false, &BTreeMap::new()); a.blend }; assert_eq!(blend(false, 0.0), BlendMode::Darken, "a fill with no opacity paints nothing"); @@ -523,7 +567,7 @@ mod tests { 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, false); + darken_overprints(&mut a, false, &BTreeMap::new()); a.blend }; assert_eq!(blend("stylize.dropShadow", true), BlendMode::Normal, "a drop shadow is not darkened with the image"); diff --git a/crates/pdf/src/print.rs b/crates/pdf/src/print.rs index 6c619b3165..0672bf97af 100644 --- a/crates/pdf/src/print.rs +++ b/crates/pdf/src/print.rs @@ -416,7 +416,7 @@ fn composite_overprints(doc: &mut Document, set: &PrintSettings) { let nodes = doc.layers.iter_mut().chain(symbols).chain(doc.patterns.iter_mut().flat_map(|p| p.art.iter_mut())); for n in nodes { if how == PrintOverprints::Simulate { - darken_overprints(n, discard_white); + darken_overprints(n, discard_white, &doc.images); } clear_overprints(n); } diff --git a/crates/pdf/src/print/plates.rs b/crates/pdf/src/print/plates.rs index 1d44778e6f..478b38b329 100644 --- a/crates/pdf/src/print/plates.rs +++ b/crates/pdf/src/print/plates.rs @@ -9,7 +9,9 @@ use vectorcraft_color::cms::{self, Cms, Model, ProfileKind}; use vectorcraft_color::swatch::REGISTRATION; use vectorcraft_color::{BlendMode, Color, GradientKind, GradientStop, Paint}; use vectorcraft_doc::inks::{Link, StopPlates, inks, plates, spot_color, stop_inks, visit_node_colors}; -use vectorcraft_doc::overprint::{darken_overprints, image_overprints_whole, image_row_draws, is_black_ink, is_white}; +use vectorcraft_doc::overprint::{ + colorized_shown_key, darken_overprints, image_overprints_whole, image_row_draws, is_black_ink, is_colorized_image, is_white, refuses_colorize, +}; use vectorcraft_doc::{AppearanceItem, ColorMode, Document, ImageBlob, Node, NodeKind}; use super::{DEFAULT_FREQUENCY, InkSettings, PrintInk, PrintSettings}; @@ -251,7 +253,7 @@ impl<'a> Separator<'a> { fn clear_inkless_overprints(&self, plate: &str, a: &mut Arc, discard_white: bool) { let inkless = |overprint: bool, p: &Paint| overprint && !p.is_none() && !(discard_white && is_white(p)) && self.no_ink(plate, p); let n = Arc::make_mut(a); - let whole = image_overprints_whole(n, discard_white) && !refuses_colorize(n, &self.doc.images); + let whole = image_overprints_whole(n, discard_white, &self.doc.images); if n.colorize_overprints() && !whole { n.extras_mut().colorize_overprint = false; } @@ -299,7 +301,7 @@ impl<'a> Separator<'a> { let nodes = d.layers.iter_mut().chain(d.symbols.iter_mut().map(|s| &mut s.art)).chain(d.patterns.iter_mut().flat_map(|p| p.art.iter_mut())); for n in nodes { self.clear_inkless_overprints(plate, n, discard_white); - darken_overprints(n, discard_white); + darken_overprints(n, discard_white, &d.images); self.node(plate, Arc::make_mut(n)); keep_colorized_pixels(n, &d.images, &mut colorized); } @@ -352,45 +354,21 @@ fn colorized_key(key: &str) -> String { format!("{key}\u{0}colorized") } -/// Whether `n` is an image with no pixels of its own and a colorize paint. -fn colorized(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. -fn shown_key(n: &Node, images: &BTreeMap) -> Option { - match &n.kind { - NodeKind::Image(im) if colorized(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 separated as those pixels and not as its color. -fn refuses_colorize(n: &Node, images: &BTreeMap) -> bool { - shown_key(n, images).and_then(|key| images.get(&key)).is_some_and(|b| !b.takes_colorize()) -} - /// Point the colorized images in `n`'s subtree at [`colorized_key`], collecting their keys in /// `keys`. A colorized image with a color mask is separated as it shows: its masked pixels /// ([`Document::shown_image`]). An image whose pixels it shows in `images` don't take its colorize /// paint ([`ImageBlob::takes_colorize`]) loses the paint instead, and is separated as the image it /// shows. fn keep_colorized_pixels(n: &mut Arc, images: &BTreeMap, keys: &mut BTreeSet) { - if !colorized(n) && n.children().is_none_or(|ch| ch.is_empty()) { + if !is_colorized_image(n) && n.children().is_none_or(|ch| ch.is_empty()) { return; } let node = Arc::make_mut(n); - let shown = shown_key(node, images); + let shown = colorized_shown_key(node, images); if refuses_colorize(node, images) { node.extras_mut().colorize = None; } - if let Some(key) = shown.filter(|_| colorized(node)) { + if let Some(key) = shown.filter(|_| is_colorized_image(node)) { if let Some(e) = node.extras_if_any() { e.color_mask = None; } diff --git a/crates/render/src/proof.rs b/crates/render/src/proof.rs index 13dbbce989..03bb42fcfe 100644 --- a/crates/render/src/proof.rs +++ b/crates/render/src/proof.rs @@ -25,7 +25,8 @@ //! [`crate::RenderOptions`] with [`active_proof`] and [`overprint_preview_on`]. use std::borrow::Cow; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; +use std::hash::{DefaultHasher, Hash, Hasher}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex, OnceLock, RwLock, Weak}; @@ -399,9 +400,23 @@ pub(crate) struct Prepared { #[derive(PartialEq)] struct Inputs { discard_white: bool, + /// [`images_state`]: which pixels a colorized image shows, and whether they take its color + /// ([`ImageBlob::takes_colorize`]), and so whether it darkens, depend on the document's images. + images: u64, plates: Option, } +/// A hash of `images`' keys and the address of each one's bytes and whether they are a preview +/// ([`ImageBlob::is_proxy`]): it changes when an image is added, removed or given other bytes, +/// without reading any pixels. +fn images_state(images: &BTreeMap) -> u64 { + let mut h = DefaultHasher::new(); + for (key, b) in images { + (key, Arc::as_ptr(&b.bytes), b.is_proxy()).hash(&mut h); + } + h.finish() +} + /// What the colors of Separations Preview's plates are made from ([`plate_color`]). struct Plates { /// The proof setup, with the visible plates. @@ -443,7 +458,7 @@ impl How<'_> { /// Change the fills, strokes and other colors of node `a`, which holds no members. fn apply(&self, a: &mut Arc) { - darken_overprints(a, self.discard_white); + darken_overprints(a, self.discard_white, &self.doc.images); if let Some((c, proof, visible)) = self.plates { map_node_inks(Arc::make_mut(a), &mut |col, sw, plates| plate_color(self.doc, c, proof, visible, col, sw, plates)); } @@ -463,6 +478,7 @@ pub(crate) fn prepare<'a>(doc: &'a Document, opts: &RenderOptions, kept: &mut Op let c = cms::active(); let inputs = Inputs { discard_white: doc.setup.discard_white_overprint, + images: images_state(&doc.images), plates: seps.map(|(proof, _)| Plates { proof: proof.clone(), swatches: doc.swatches.clone(),