From 6fb2028149a74250e4cd2c28ce6a35740a68719e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 23:55:44 -0800 Subject: [PATCH] Print: classify a freeform gradient's plate cells after their opacity A translucent cell whose samples round to ink before its opacity is applied, such as 0.4% cyan at 25% opacity, is written with no ink once the opacity is applied, yet it was kept and painted paper over the ink below. The cells are now classified from the samples the image writes after opacity, and RGB cells in CMYK output from their converted inks. --- crates/eps/src/scene.rs | 33 ++++++++++++++++++++++++++++++--- crates/eps/src/tests.rs | 24 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/crates/eps/src/scene.rs b/crates/eps/src/scene.rs index 5643bb4127..35a68371b1 100644 --- a/crates/eps/src/scene.rs +++ b/crates/eps/src/scene.rs @@ -1597,9 +1597,36 @@ impl<'a> Scene<'a> { (field.display_grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect(), 255) }; let n = if in_inks { 5 } else { 4 }; - // Overprinting on a plate: the cells whose samples put none of the ink down are left out, - // as clear pixels at Level 3 and outside a clip of the other cells at Level 2. - let blank: Option> = self.keep_below.then(|| samples.chunks(n).map(|s| s.iter().take(n - 1).all(|&v| v == paper)).collect()); + // Overprinting on a plate: the cells whose samples put none of the ink down once their + // opacity is applied, as `encode_inks` and `encode_image` write them (RGB converted to + // CMYK in CMYK output), are left out, as clear pixels at Level 3 and outside a clip of the + // other cells at Level 2. + let written = |v: u8, a: u8| { + let (v, a) = (u32::from(v), u32::from(a)); + ((v * a + u32::from(paper) * (255 - a) + 127) / 255) as u8 + }; + let (cms, to_cmyk) = (self.cms.clone(), !in_inks && self.cmyk); + let intent = cms.settings().intent; + let mut converted: HashMap<[u8; 3], bool> = HashMap::new(); + let blank: Option> = self.keep_below.then(|| { + samples + .chunks(n) + .map(|s| { + let a = s.last().copied().unwrap_or(0); + if a == 0 || in_inks { + return s.iter().take(n - 1).all(|&v| written(v, a) == paper); + } + let rgb = [0, 1, 2].map(|i| s.get(i).map_or(paper, |&v| written(v, a))); + if !to_cmyk { + return rgb == [paper; 3]; + } + *converted.entry(rgb).or_insert_with(|| { + let inks = cms.srgb_to_cmyk(rgb.map(|x| f32::from(x) / 255.0), intent); + inks.iter().all(|&x| (unit(x) * 255.0).round() as u8 == 0) + }) + }) + .collect() + }); let blank = blank.filter(|b| b.contains(&true)); if blank.as_ref().is_some_and(|b| !b.contains(&false)) { return; diff --git a/crates/eps/src/tests.rs b/crates/eps/src/tests.rs index 317aefe355..e6161da23d 100644 --- a/crates/eps/src/tests.rs +++ b/crates/eps/src/tests.rs @@ -1637,6 +1637,30 @@ fn a_repeated_field_clipped_on_a_plate_is_charged_its_reference_at_the_page_limi assert_eq!((&page.setup, &page.body), (&full.setup, &full.body)); } +/// On a plate, an overprinting freeform gradient whose cells are translucent enough that their +/// opacity takes every sample to no ink (or to paper white in RGB) draws no image at either level. +#[test] +fn a_translucent_field_whose_samples_write_no_ink_on_a_plate_draws_no_image() { + let faint = [Color::cmyk(0.004, 0.0, 0.0, 0.0), Color::rgb(254.0 / 255.0, 254.0 / 255.0, 254.0 / 255.0)]; + for color in faint { + let d = doc_with(|d| { + let mut g = GradientPaint::new(Gradient { kind: GradientKind::Freeform, ..Default::default() }); + let point = |x: f64| FreeformPoint { opacity: 0.25, ..FreeformPoint::new(Point::new(x, 32.0), color) }; + g.freeform = Some(Freeform { points: vec![point(12.0), point(52.0)], ..Default::default() }); + let mut n = path(d, shapes::rectangle(Rect::new(0.0, 0.0, 64.0, 64.0)), Paint::Gradient(Box::new(g)), Paint::None, 0.0); + for it in &mut n.appearance.items { + *it.overprint_mut() = true; + } + vec![n] + }); + for level in [Level::Three, Level::Two] { + let o = EpsOptions { level, ..opts(&d) }; + let out = crate::scene::Scene::new(&d, &o).on_plate(true).run(); + assert!(!out.body.contains("image"), "{color:?} at {level:?}: {}", out.body); + } + } +} + /// A 200 × 200 pt document holding 8 × 8 pixels of noise as image `pic` and a three-pixel gray ramp /// as image `gray`, its first layer holding what `nodes` makes. fn image_doc_with(nodes: impl FnOnce(&mut Document) -> Vec) -> Document {