diff --git a/crates/eps/src/import/native/compare.rs b/crates/eps/src/import/native/compare.rs index 424ac4079a..24a7fbb762 100644 --- a/crates/eps/src/import/native/compare.rs +++ b/crates/eps/src/import/native/compare.rs @@ -1,9 +1,11 @@ use super::build::Frame; use super::slots::OUTLINE_REACH; use super::{Arc, Document, Node, NodeKind, Rect}; +use vectorcraft_doc::ColorMode; -/// `doc` with its layers that don't print hidden, when it has any. -fn as_printed(doc: &Document) -> Option { +/// `doc` as the page prints it, when that isn't `doc` as it is: its layers that don't print hidden, +/// and, with `cmyk`, in CMYK when it is an RGB document (see [`blends_in_cmyk`]). +fn as_printed(doc: &Document, cmyk: bool) -> Option { fn hide(nodes: &mut [Arc]) -> bool { let mut any = false; for n in nodes { @@ -19,7 +21,24 @@ fn as_printed(doc: &Document) -> Option { any } let mut d = doc.clone(); - hide(&mut d.layers).then_some(d) + let mut changed = hide(&mut d.layers); + if cmyk && d.color_mode == ColorMode::Rgb { + d.color_mode = ColorMode::Cmyk; + changed = true; + } + changed.then_some(d) +} + +/// Whether the layers of `doc` may print differently from the CMYK page `page` (`page_cmyk`) by +/// where their transparency was blended. An RGB document written as CMYK PostScript (the EPS option +/// that converts its colours for output, +/// https://helpx.adobe.com/illustrator/using/saving-artwork.html) has its page in the colours' CMYK +/// equivalents, its transparency flattened in the document's blending space (Edit › Transparency +/// Blend Space: Document RGB or Document CMYK), which the editing data doesn't name: blend modes +/// such as Color or Screen mix the inks in CMYK, not the screen colours. Art without transparency +/// prints alike either way. +fn blends_in_cmyk(page_cmyk: bool, doc: &Document) -> bool { + page_cmyk && doc.color_mode == ColorMode::Rgb && doc.layers.iter().any(|l| l.shows_transparency()) } /// A premultiplied pixel seen on white. @@ -335,6 +354,12 @@ pub(super) struct PageView { boards: Vec, sheets: Vec, samples: u32, + /// Whether the page is printed in CMYK ([`blends_in_cmyk`]). + cmyk: bool, + /// Whether the layers are drawn in CMYK to weigh them against the page: decided by the first + /// RGB document with transparency weighed, as the blending space that draws it closer to the + /// page. + in_cmyk: Option, layers: vectorcraft_render::Renderer, /// The drawings of the last [`KEPT_DOCUMENTS`] documents weighed, the last one weighed last (the /// import weighs two readings of a file back and forth). @@ -382,7 +407,19 @@ impl PageView { Page::on(frame.page, comparison_render(&mut renderer, frame.page, ra, scale, samples), ra, region, scale, moved) }) .collect(); - Self { pages, boards, sheets, samples, layers: vectorcraft_render::Renderer::new(), drawn: vec![], without: vec![], draws: 0 } + let cmyk = frame.page.color_mode == ColorMode::Cmyk; + Self { + pages, + boards, + sheets, + samples, + cmyk, + in_cmyk: None, + layers: vectorcraft_render::Renderer::new(), + drawn: vec![], + without: vec![], + draws: 0, + } } /// The areas in the layers' document that the pages are compared on: `rb` for the first page, @@ -462,8 +499,25 @@ impl PageView { return; } // The page doesn't print what isn't printed. - let printed = as_printed(b); - for (i, pixels) in self.draw(printed.as_ref().unwrap_or(b), &missing) { + let drawn = if self.in_cmyk.is_none() && blends_in_cmyk(self.cmyk, b) { + // Drawn both ways on every page (not only those wanted, which may not show the + // difference), the closer to the pages decides; the drawings of all of them are kept. + let every: Vec = (0..n).collect(); + let both = [false, true].map(|cmyk| { + let printed = as_printed(b, cmyk); + let drawn = self.draw(printed.as_ref().unwrap_or(b), &every); + let off: f64 = drawn.iter().filter_map(|(i, d)| Some(compared(&self.pages.get(*i)?.drawn, d, &[], &[]).any)).sum(); + (off, cmyk, drawn) + }); + let [rgb, cmyk] = both; + let (_, chosen, drawn) = if cmyk.0 < rgb.0 { cmyk } else { rgb }; + self.in_cmyk = Some(chosen); + drawn + } else { + let printed = as_printed(b, self.in_cmyk == Some(true)); + self.draw(printed.as_ref().unwrap_or(b), &missing) + }; + for (i, pixels) in drawn { if let Some(slot) = self.drawn.last_mut().and_then(|d| d.pages.get_mut(i)) { *slot = Some(Layered { pixels, laid: None, reflowed: None, colour: None }); } @@ -475,7 +529,7 @@ impl PageView { pub(super) fn differences(&mut self, b: &Document, rb: Rect, reflow: bool) -> Vec { let all: Vec = (0..self.pages.len()).collect(); self.layered(b, &all); - let printed = as_printed(b); + let printed = as_printed(b, self.in_cmyk == Some(true)); let printed = printed.as_ref().unwrap_or(b); let mut out = vec![]; for i in all { @@ -515,7 +569,7 @@ impl PageView { pub(super) fn solid_regions_on_every_page(&mut self, b: &Document, reflow: bool) -> Option<(Difference, Vec)> { let all: Vec = (0..self.pages.len()).collect(); self.layered(b, &all); - let printed = as_printed(b); + let printed = as_printed(b, self.in_cmyk == Some(true)); let b = printed.as_ref().unwrap_or(b); let (mut diff, mut regions) = (Difference { any: 0.0, art: 0.0, solid: 0.0, typed: 0.0 }, vec![]); for (page, layered) in self.pages.iter().zip(&self.drawn.last()?.pages) { @@ -540,7 +594,7 @@ impl PageView { /// for [`Self::adopt`]. pub(super) fn weigh_without(&mut self, doc: &Document, without: &Document, on: &[usize]) -> Vec<(usize, f64, f64)> { self.layered(doc, on); - let printed = as_printed(without); + let printed = as_printed(without, self.in_cmyk == Some(true)); let drawn = self.draw(printed.as_ref().unwrap_or(without), on); let mut shares = vec![]; self.without.clear(); @@ -684,19 +738,27 @@ fn colour_share(x: &vectorcraft_render::Rendered, y: &vectorcraft_render::Render } /// How `a` on `ra` and `b` on `rb` (rectangles of the same size) draw differently ([`compared`]), -/// for a region other than the page's ([`PageView`] draws that one once). +/// for a region other than the page's ([`PageView`] draws that one once): `b` blended the way that +/// draws it closer to `a` ([`blends_in_cmyk`]). pub(super) fn difference(a: &Document, ra: Rect, b: &Document, rb: Rect) -> Difference { - let printed = as_printed(b); - let b = printed.as_ref().unwrap_or(b); let scale = compare_scale(ra); // A renderer each: the two documents' images can share their keys and not their pixels. let samples = comparison_samples(a); let x = comparison_render(&mut vectorcraft_render::Renderer::new(), a, ra, scale, samples); - let y = comparison_render(&mut vectorcraft_render::Renderer::new(), b, rb, scale, samples); - let mut areas = vec![]; - type_areas(&a.layers, ra, scale, &mut areas); - type_areas(&b.layers, rb, scale, &mut areas); - compared(&x, &y, &areas, &[]) + let ways: &[bool] = if blends_in_cmyk(a.color_mode == ColorMode::Cmyk, b) { &[false, true] } else { &[false] }; + // The way of blending that draws `b` closer to the page. + ways.iter() + .map(|cmyk| { + let printed = as_printed(b, *cmyk); + let b = printed.as_ref().unwrap_or(b); + let y = comparison_render(&mut vectorcraft_render::Renderer::new(), b, rb, scale, samples); + let mut areas = vec![]; + type_areas(&a.layers, ra, scale, &mut areas); + type_areas(&b.layers, rb, scale, &mut areas); + compared(&x, &y, &areas, &[]) + }) + .min_by(|p, q| p.any.total_cmp(&q.any)) + .unwrap_or(Difference::ALL) } /// How two drawings of the same size differ, each drawn on nothing and seen on white, away from @@ -933,7 +995,7 @@ pub(super) const PAST_THE_PAGE: f64 = 1.0; pub(super) fn compare(frame: &Frame<'_>, view: &mut PageView, layered: &Document, type_reflow: bool) -> Result, String> { let mut diff = view.difference_on_every_page(layered, frame.rect, type_reflow); if frame.whole - && let Some(art) = vectorcraft_render::encode::art_bounds(as_printed(layered).as_ref().unwrap_or(layered)) + && let Some(art) = vectorcraft_render::encode::art_bounds(as_printed(layered, false).as_ref().unwrap_or(layered)) .filter(|a| !frame.rect.inflate(PAST_THE_PAGE, PAST_THE_PAGE).contains_rect(*a)) { // The page and the art the layers print beyond it, each in its own document's place. @@ -1138,4 +1200,130 @@ mod tests { } assert_eq!(view.draws, 2, "each document is drawn once"); } + + /// The page of an RGB document printed in CMYK has its transparency blended in either space: a + /// green screened over a grey of black ink alone leaves no ink when blended in CMYK, and shows a + /// light green when blended in screen colours. The layers are weighed blended the way that + /// draws them closer to the page, so they match either page. + #[test] + fn an_rgb_documents_blending_matches_its_page_printed_in_cmyk() { + use vectorcraft_doc::ColorMode; + let area = Rect::new(0.0, 0.0, 100.0, 100.0); + let page_with = |centre: vectorcraft_color::Color, blend: vectorcraft_color::BlendMode| { + let mut page = Document::new_with_mode(100.0, 100.0, ColorMode::Cmyk); + let layer = page.layers[0].id; + filled(&mut page, layer, area, 0.6); + let paint = vectorcraft_color::Paint::solid(centre); + let mut n = Node::path( + page.alloc_id(), + vectorcraft_geom::shapes::rectangle(Rect::new(20.0, 20.0, 80.0, 80.0)), + vectorcraft_doc::Appearance::basic(paint, vectorcraft_color::Paint::None, 0.0), + ); + n.blend = blend; + page.insert(Some(layer), usize::MAX, n).unwrap(); + page + }; + let screened = page_with(vectorcraft_color::Color::cmyk(1.0, 0.0, 1.0, 0.0), vectorcraft_color::BlendMode::Screen); + let mut layers = screened.clone(); + layers.color_mode = ColorMode::Rgb; + // Blended in screen colours, the centre is a light green. + let shown = |d: &Document| comparison_render(&mut vectorcraft_render::Renderer::new(), d, area, compare_scale(area), 1); + let rgb = shown(&layers); + let at = (rgb.height as usize / 2 * rgb.width as usize + rgb.width as usize / 2) * 4; + let [r, g, b] = [0, 1, 2].map(|k| f32::from(rgb.pixels[at + k]) / 255.0); + assert!(compared(&shown(&screened), &rgb, &[], &[]).any > 0.3); + // A page blended in CMYK, and one blended in screen colours (its centre painted that green). + let in_rgb = page_with(vectorcraft_color::Color::rgb(r, g, b), vectorcraft_color::BlendMode::Normal); + for (page, cmyk) in [(&screened, true), (&in_rgb, false)] { + assert!(difference(page, area, &layers, area).any < 0.001, "cmyk: {cmyk}"); + let frame = frame_of(page, &[area]); + let mut view = PageView::new(&frame); + assert_eq!(compare(&frame, &mut view, &layers, false), Ok(vec![]), "cmyk: {cmyk}"); + assert_eq!(view.in_cmyk, Some(cmyk)); + } + } + + /// Two pages in a document of `mode`, a grey on each; the second has a green over its grey, + /// blended Screen when `screened`, else Normal. Their areas are returned too. + fn two_pages(mode: vectorcraft_doc::ColorMode, screened: bool) -> (Document, [Rect; 2]) { + let (a, b) = (Rect::new(0.0, 0.0, 100.0, 100.0), Rect::new(200.0, 0.0, 300.0, 100.0)); + let mut page = Document::new_with_mode(300.0, 100.0, mode); + let layer = page.layers[0].id; + filled(&mut page, layer, a, 0.6); + filled(&mut page, layer, b, 0.6); + let paint = vectorcraft_color::Paint::solid(vectorcraft_color::Color::cmyk(1.0, 0.0, 1.0, 0.0)); + let mut n = Node::path( + page.alloc_id(), + vectorcraft_geom::shapes::rectangle(Rect::new(220.0, 20.0, 280.0, 80.0)), + vectorcraft_doc::Appearance::basic(paint, vectorcraft_color::Paint::None, 0.0), + ); + if screened { + n.blend = vectorcraft_color::BlendMode::Screen; + } + page.insert(Some(layer), usize::MAX, n).unwrap(); + (page, [a, b]) + } + + /// The blending space is chosen from every page, also when the first pages drawn are only + /// some of them: here the first page has no transparency, and the second is blended in CMYK. + /// The layers then match the pages. + #[test] + fn the_blending_space_is_chosen_from_every_page() { + use vectorcraft_doc::ColorMode; + let (page, [a, b]) = two_pages(ColorMode::Cmyk, true); + let mut layers = page.clone(); + layers.color_mode = ColorMode::Rgb; + let frame = frame_of(&page, &[a, b]); + let mut view = PageView::new(&frame); + view.layered(&layers, &[0]); + assert_eq!(view.in_cmyk, Some(true)); + // Both pages were drawn, and kept. + assert!(view.drawn.last().is_some_and(|d| d.pages.iter().all(Option::is_some))); + assert!(view.differences(&layers, a, false).iter().all(|d| d.any < 0.001)); + assert_eq!(compare(&frame, &mut view, &layers, false), Ok(vec![])); + } + + /// Only an RGB document with transparency, weighed against a CMYK page, is drawn both ways, and + /// only until the way is chosen: later documents are drawn once, in that way. An RGB document + /// without transparency, a CMYK document, and an RGB page are drawn once, as they are. + #[test] + fn only_an_rgb_document_with_transparency_on_a_cmyk_page_is_drawn_both_ways() { + use vectorcraft_doc::ColorMode; + // The draws of weighing `layers` (as RGB or not) on `page`, then of a second document, and + // the way chosen. + let weigh = |page: &Document, areas: [Rect; 2], layers: &Document, rgb: bool| { + let mut layers = layers.clone(); + if rgb { + layers.color_mode = ColorMode::Rgb; + } + let frame = frame_of(page, &areas); + let mut view = PageView::new(&frame); + view.layered(&layers, &[0, 1]); + let first = view.draws; + // The same document again draws nothing; another one is drawn once. + view.layered(&layers, &[0, 1]); + assert_eq!(view.draws, first); + let mut other = layers.clone(); + let layer = other.layers[0].id; + filled(&mut other, layer, Rect::new(10.0, 10.0, 20.0, 20.0), 1.0); + view.layered(&other, &[0, 1]); + (first, view.draws - first, view.in_cmyk) + }; + let (cmyk_page, areas) = two_pages(ColorMode::Cmyk, true); + let (both, then, chosen) = weigh(&cmyk_page, areas, &cmyk_page, true); + assert_eq!(chosen, Some(true)); + assert!(then > 0); + assert_eq!(both, 2 * then, "drawn both ways once, then one way"); + let (plain_page, _) = two_pages(ColorMode::Cmyk, false); + let (rgb_page, _) = two_pages(ColorMode::Rgb, true); + for (what, page, layers, rgb) in [ + ("an RGB document without transparency", &plain_page, &plain_page, true), + ("a CMYK document", &cmyk_page, &cmyk_page, false), + ("an RGB page", &rgb_page, &rgb_page, false), + ] { + let (first, second, chosen) = weigh(page, areas, layers, rgb); + assert_eq!(chosen, None, "{what}"); + assert_eq!((first, second), (then, then), "{what} is drawn once"); + } + } }