diff --git a/crates/pdf/tests/plates.rs b/crates/pdf/tests/plates.rs index dfce021b8..d7f0da0db 100644 --- a/crates/pdf/tests/plates.rs +++ b/crates/pdf/tests/plates.rs @@ -80,7 +80,10 @@ impl Plates { fn plates(pdf: &[u8], inks: &[Ink], overprint: bool, simulation: Option<&CmykSimulation>) -> Plates { let settings: Vec = inks .iter() - .map(|ink| InterpreterSettings { plate: Some(Plate { ink: ink.clone(), overprint, simulation: simulation.cloned() }), ..Default::default() }) + .map(|ink| InterpreterSettings { + plate: Some(Plate { ink: ink.clone(), overprint, simulation: simulation.cloned(), spots_as_process: false }), + ..Default::default() + }) .collect(); Plates(render_each(pdf, &settings)) } @@ -243,6 +246,31 @@ fn soft_masks_keep_the_luminosity_of_their_own_colors() { assert!((1..30).contains(&black), "black through a blue luminosity mask: {black}%"); } +#[cfg_attr(not(target_arch = "wasm32"), test)] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +fn a_device_cmyk_luminosity_mask_reads_its_inks_by_the_device_formula() { + // Black ink through a mask whose DeviceCMYK group paints the left half, at (10, 20), and + // leaves the backdrop on the right, at (30, 20). Luminosity is 1 - min(1, 0.3c + 0.59m + + // 0.11y + k) (ISO 32000-2 §10.4.2.2); Gold's alternate is C0 M30 Y100 K0. + let masked = |paint: &str, backdrop: &str| { + let group = stream( + &format!( + "/Type /XObject /Subtype /Form /BBox [0 0 40 40] /Group << /S /Transparency /CS /DeviceCMYK >> /Resources << {} >>", + gold_resource() + ), + format!("{paint} 0 0 20 40 re f").as_bytes(), + ); + let resources = format!("/ExtGState << /SM << /SMask << /S /Luminosity /G 5 0 R {backdrop} >> >> >>"); + let s = plates(&page_pdf("/SM gs 0 0 0 1 k 0 0 40 40 re f", &resources, &[group]), &[Ink::Black], true, None); + [s.at(10, 20)[0], s.at(30, 20)[0]] + }; + assert_eq!(masked("0 0 0 1 k", ""), [0, 0], "K 100% and the default black backdrop hide all of it"); + assert_eq!(masked("0 0 0 0 k", "/BC [0 0 0 0]"), [100, 100], "paper shows all of it"); + assert_eq!(masked("0.5 0 0 0 k", "/BC [0 0 0 0.25]"), [85, 75]); + assert_eq!(masked("/CS0 cs 1 scn", ""), [71, 0], "a spot ink reads as its alternate CMYK"); + assert_eq!(masked("1 0 0 rg", ""), [30, 0], "RGB red is C0 M100 Y100 K0 by the device formulas"); +} + #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] fn the_ink_sink_reports_spot_inks_of_patterns_and_images_but_not_of_soft_masks() { @@ -410,7 +438,7 @@ fn mutated_profiles_simulate_or_are_refused_without_panicking() { let mut bent = whole.clone(); bent[at_byte] = [0x00, 0x7F, 0xFF][n % 3]; if let Ok(simulation) = CmykSimulation::new(&bent) { - let plate = Plate { ink: Ink::Cyan, overprint: true, simulation: Some(simulation) }; + let plate = Plate { ink: Ink::Cyan, overprint: true, simulation: Some(simulation), spots_as_process: false }; render(&pdf, &InterpreterSettings { plate: Some(plate), ..Default::default() }); } } diff --git a/crates/prepress/src/plates.rs b/crates/prepress/src/plates.rs index ea7ebd25f..739b28292 100644 --- a/crates/prepress/src/plates.rs +++ b/crates/prepress/src/plates.rs @@ -190,7 +190,7 @@ pub fn plates(pdf: &[u8], page: usize, dpi: f32, opts: &Options) -> Result Self { let overprint = plate.overprint && overprint; let untouched = Self::film(0.0, 0.0); - match self.color_space.ink_use(&plate.ink) { + match self.color_space.ink_use(&plate.ink, plate.spots_as_process) { InkUse::Nothing => untouched, InkUse::Unspecified if overprint => untouched, InkUse::Unspecified => Self::film(0.0, self.opacity), @@ -1281,6 +1281,7 @@ impl Color { &plate.ink, plate.simulation.as_ref(), false, + plate.spots_as_process, ); // Nonzero overprint mode: a DeviceCMYK component of 0 leaves its plate as it // is. It applies to colours set in DeviceCMYK itself, not to images, shadings @@ -1384,6 +1385,28 @@ pub struct Plate { /// PdfCraft patch (36): the printing condition process colour converts to, from the /// document's output intent. `None` converts by the ISO 32000-2 §10.4.2 device formulas. pub simulation: Option, + /// Craft patch (CMYK masks): spot colorants paint the process plates through their + /// alternate space and tint transform, as a composite CMYK device converts them, so no spot + /// ink has a plate of its own. A DeviceCMYK soft mask's luminosity is separated this way; + /// separations leave it `false`. + pub spots_as_process: bool, +} + +impl Plate { + /// Craft patch (CMYK masks): the tint, `0.0` (no ink) to `1.0` (solid), that `color` puts + /// on this plate, ignoring overprint and opacity. + pub fn tint(&self, color: &Color) -> f32 { + match color.color_space.ink_use(&self.ink, self.spots_as_process) { + InkUse::Paints => color.color_space.ink_tint( + &color.components, + &self.ink, + self.simulation.as_ref(), + false, + self.spots_as_process, + ), + InkUse::Unspecified | InkUse::Nothing => 0.0, + } + } } /// PdfCraft patch (35): told about each spot ink a paint uses, with the ink's appearance: the @@ -1765,14 +1788,15 @@ impl Hub { static HUB: LazyLock = LazyLock::new(Hub::new); impl ColorSpace { - /// PdfCraft patch (35): this space drawn as `ink`'s plate (for images and shadings, whose - /// colours are converted sample by sample), converting process colour through + /// PdfCraft patch (35): this space drawn as `plate` (for images and shadings, whose + /// colours are converted sample by sample), converting process colour through its /// `simulation` (patch 36). - pub(crate) fn plate(&self, ink: Ink, simulation: Option) -> Self { + pub(crate) fn plate(&self, plate: &Plate) -> Self { Self(Arc::new(ColorSpaceType::Plate(PlateSpace { inner: self.clone(), - ink, - simulation, + ink: plate.ink.clone(), + simulation: plate.simulation.clone(), + spots_as_process: plate.spots_as_process, }))) } @@ -1865,9 +1889,42 @@ impl ColorSpace { } } + /// Craft patch (CMYK masks): the alternate space of a `Separation` or `DeviceN` space that + /// names a spot ink; `None` for other spaces, and for colorants that are all process inks, + /// `All` or `None`. + fn spot_alternate(&self) -> Option<&ColorSpace> { + let is_spot = |c: &Colorant| matches!(c, Colorant::Ink(Ink::Spot(_))); + match self.0.as_ref() { + ColorSpaceType::Separation(s) if is_spot(&s.colorant) => Some(&s.alternate_space), + ColorSpaceType::DeviceN(d) if !d.is_none && d.colorants.iter().any(is_spot) => { + Some(&d.alternate_space) + } + _ => None, + } + } + + /// Craft patch (CMYK masks): a `Separation` or `DeviceN` color in its alternate space, + /// through the tint transform; the components unchanged for other spaces. + fn tint_transform(&self, components: &[f32]) -> ColorComponents { + let (function, alternate) = match self.0.as_ref() { + ColorSpaceType::Separation(s) => (&s.tint_transform, &s.alternate_space), + ColorSpaceType::DeviceN(d) => (&d.tint_transform, &d.alternate_space), + _ => return components.iter().copied().collect(), + }; + count_tint_transform_runs(1); + function + .eval(components.iter().copied().collect()) + .unwrap_or(alternate.initial_color()) + } + /// PdfCraft patch (35): how the space paints `ink`'s plate. Gray, RGB, CIE-based and ICC - /// spaces are process colour: they specify the four process inks and no spot ink. - pub(crate) fn ink_use(&self, ink: &Ink) -> InkUse { + /// spaces are process colour: they specify the four process inks and no spot ink. With + /// `spots_as_process` (Craft patch, CMYK masks), a space naming a spot ink paints as its + /// alternate space. + pub(crate) fn ink_use(&self, ink: &Ink, spots_as_process: bool) -> InkUse { + if spots_as_process && let Some(alternate) = self.spot_alternate() { + return alternate.ink_use(ink, false); + } match self.0.as_ref() { ColorSpaceType::DeviceCmyk | ColorSpaceType::DeviceGray @@ -1882,7 +1939,7 @@ impl ColorSpace { InkUse::Unspecified } } - ColorSpaceType::Indexed(i) => i.base.ink_use(ink), + ColorSpaceType::Indexed(i) => i.base.ink_use(ink, spots_as_process), ColorSpaceType::Separation(s) => match &s.colorant { Colorant::None => InkUse::Nothing, Colorant::All => InkUse::Paints, @@ -1904,7 +1961,7 @@ impl ColorSpace { } // A pattern is separated through what it paints. ColorSpaceType::Pattern(_) => InkUse::Unspecified, - ColorSpaceType::Plate(p) => p.inner.ink_use(ink), + ColorSpaceType::Plate(p) => p.inner.ink_use(ink, spots_as_process), } } @@ -1912,14 +1969,19 @@ impl ColorSpace { /// `components` puts on `ink`'s plate; `0.0` when the space doesn't specify the ink. /// Process colour converts through `simulation` when there is one (patch 36). /// `manual_scale` as for [`ToRgb::convert_f32`] (components normalised to `0.0..=1.0`, as - /// an Indexed palette gives them). + /// an Indexed palette gives them). `spots_as_process` as for [`Self::ink_use`]. pub(crate) fn ink_tint( &self, components: &[f32], ink: &Ink, simulation: Option<&CmykSimulation>, manual_scale: bool, + spots_as_process: bool, ) -> f32 { + if spots_as_process && let Some(alternate) = self.spot_alternate() { + let alternate_components = self.tint_transform(components); + return alternate.ink_tint(&alternate_components, ink, simulation, false, false); + } let tint = match self.0.as_ref() { ColorSpaceType::Separation(s) => match &s.colorant { Colorant::All => components.first().copied().unwrap_or(0.0), @@ -1937,12 +1999,18 @@ impl ColorSpace { let index = components.first().copied().unwrap_or(0.0); let index = (clamp_component(index, 0.0, f32::from(i.hival)) + 0.5) as usize; match i.values.get(index) { - Some(values) => i.base.ink_tint(values, ink, simulation, true), + Some(values) => { + i.base + .ink_tint(values, ink, simulation, true, spots_as_process) + } None => 0.0, } } ColorSpaceType::Pattern(_) => 0.0, - ColorSpaceType::Plate(p) => p.inner.ink_tint(components, ink, simulation, manual_scale), + ColorSpaceType::Plate(p) => { + p.inner + .ink_tint(components, ink, simulation, manual_scale, spots_as_process) + } _ => match ( self.process_cmyk(components, simulation, manual_scale), ink.process_index(), @@ -2043,6 +2111,8 @@ pub(crate) struct PlateSpace { ink: Ink, /// Patch 36: the printing condition process colour converts to, if any. simulation: Option, + /// Craft patch (CMYK masks): see [`Plate::spots_as_process`]. + spots_as_process: bool, } impl PlateSpace { @@ -2054,7 +2124,13 @@ impl PlateSpace { fn film(&self, sample: &[f32], manual_scale: bool) -> u8 { let tint = self .inner - .ink_tint(sample, &self.ink, self.simulation.as_ref(), manual_scale); + .ink_tint( + sample, + &self.ink, + self.simulation.as_ref(), + manual_scale, + self.spots_as_process, + ); f32_to_u8(1.0 - tint) } diff --git a/vendor/hayro-interpret/src/interpret/mod.rs b/vendor/hayro-interpret/src/interpret/mod.rs index 70bb05c7b..4d065c1fb 100644 --- a/vendor/hayro-interpret/src/interpret/mod.rs +++ b/vendor/hayro-interpret/src/interpret/mod.rs @@ -122,8 +122,9 @@ pub struct InterpreterSettings { pub preview: bool, /// PdfCraft patch (35): draw one ink's separation instead of the composite page (see /// [`Plate`](crate::color::Plate)). Soft masks still draw in colour: a mask's luminosity - /// comes from its group's colours, not from any plate. Patch 36: the plate's `simulation` - /// converts process colour to an output intent's CMYK. + /// comes from its group's colours, not from the page's plate (a DeviceCMYK luminosity + /// mask's from its own process plates, see `SoftMask::plate`). Patch 36: the plate's + /// `simulation` converts process colour to an output intent's CMYK. pub plate: Option, /// PdfCraft patch (35): told about each spot ink a paint uses (colours, images, shadings /// and uncoloured patterns), whether or not `plate` is set. diff --git a/vendor/hayro-interpret/src/interpret/path.rs b/vendor/hayro-interpret/src/interpret/path.rs index 6c75c4648..2850ddab1 100644 --- a/vendor/hayro-interpret/src/interpret/path.rs +++ b/vendor/hayro-interpret/src/interpret/path.rs @@ -219,7 +219,11 @@ fn plate_paint<'a>( }; match &mut pattern { Pattern::Shading(s) => { - match s.shading.color_space.ink_use(&plate.ink) { + match s + .shading + .color_space + .ink_use(&plate.ink, plate.spots_as_process) + { InkUse::Nothing => return untouched(), InkUse::Unspecified if plate.overprint && overprint => return untouched(), InkUse::Paints | InkUse::Unspecified => {} diff --git a/vendor/hayro-interpret/src/shading.rs b/vendor/hayro-interpret/src/shading.rs index 0c4ad1e78..22f5cf77e 100644 --- a/vendor/hayro-interpret/src/shading.rs +++ b/vendor/hayro-interpret/src/shading.rs @@ -142,11 +142,14 @@ impl Shading { /// PdfCraft patch (35): the shading drawn as `plate`'s plate, through its simulation /// (patch 36). pub(crate) fn for_plate(&mut self, plate: &crate::color::Plate) { - self.color_space = self - .color_space - .plate(plate.ink.clone(), plate.simulation.clone()); + self.color_space = self.color_space.plate(plate); let simulation = plate.simulation.as_ref().map(|s| s.digest()); - self.cache_key = crate::util::hash128(&(self.cache_key, &plate.ink, simulation)); + self.cache_key = crate::util::hash128(&( + self.cache_key, + &plate.ink, + simulation, + plate.spots_as_process, + )); } pub(crate) fn new(dict: &Dict<'_>, stream: Option<&Stream<'_>>, cache: &Cache) -> Option { diff --git a/vendor/hayro-interpret/src/soft_mask.rs b/vendor/hayro-interpret/src/soft_mask.rs index 657d6bbff..4fdeaac81 100644 --- a/vendor/hayro-interpret/src/soft_mask.rs +++ b/vendor/hayro-interpret/src/soft_mask.rs @@ -1,4 +1,4 @@ -use crate::color::{Color, ColorComponents, ColorSpace}; +use crate::color::{CmykSimulation, Color, ColorComponents, ColorSpace, Ink, Plate}; use crate::context::{Context, InterpreterCache}; use crate::device::Device; use crate::function::Function; @@ -76,6 +76,10 @@ struct Repr<'a> { background: Color, xref: &'a XRef, nesting_depth: u32, + /// Craft patch (CMYK masks): the group's `/CS` is DeviceCMYK. + device_cmyk: bool, + /// Craft patch (CMYK masks): the page plate's printing condition, for [`SoftMask::plate`]. + simulation: Option, } impl Hash for Repr<'_> { @@ -128,6 +132,7 @@ impl<'a> SoftMask<'a> { .and_then(|g| g.get::>(CS)) .and_then(|o| ColorSpace::new(o, &context.interpreter_cache.object_cache)) .unwrap_or_else(ColorSpace::device_rgb); + let device_cmyk = cs.is_device_cmyk(); let transfer_function = dict .get::>(TR) .and_then(|o| Function::new(&o)) @@ -152,7 +157,13 @@ impl<'a> SoftMask<'a> { let root_transform = context.get().ctm; let bbox = context.bbox(); let resources_key = resources_cache_key(&parent_resources); + let simulation = context + .settings + .plate + .as_ref() + .and_then(|p| p.simulation.clone()); let cache_key = hash128(&( + simulation.as_ref().map(|s| s.digest()), dict.cache_key(), root_transform.cache_key(), [bbox.x0.to_bits(), bbox.y0.to_bits(), bbox.x1.to_bits(), bbox.y1.to_bits()], @@ -170,7 +181,7 @@ impl<'a> SoftMask<'a> { bbox: context.bbox(), interpreter_cache: context.interpreter_cache.clone(), // PdfCraft patch (35): a mask's luminosity comes from its group's colours, never - // from a plate, and the inks it uses are never printed. + // from the page's plate, and the inks it uses are never printed. settings: InterpreterSettings { plate: None, ink_sink: None, @@ -180,9 +191,51 @@ impl<'a> SoftMask<'a> { background, parent_resources, nesting_depth, + device_cmyk, + simulation, }))) } + /// Craft patch (CMYK masks): whether the mask group's color space is DeviceCMYK. A + /// luminosity mask then takes its values from the group's CMYK by the ISO 32000-2 §10.4.2.2 + /// formula, `1 - min(1, 0.3c + 0.59m + 0.11y + k)`, rather than from its colors as drawn: + /// drawn through a CMYK profile, K 100% is not black. + pub fn is_device_cmyk(&self) -> bool { + self.0.device_cmyk + } + + /// Craft patch (CMYK masks): the plate of process `ink` that the mask group composites on a + /// CMYK device, for [`Self::interpret_plate`] and [`Plate::tint`] of the backdrop. Spot + /// colorants paint through their alternate space, and nothing overprints. + pub fn plate(&self, ink: Ink) -> Plate { + Plate { + ink, + overprint: false, + simulation: self.0.simulation.clone(), + spots_as_process: true, + } + } + + /// Craft patch (CMYK masks): interpret the mask's contents as the film of `plate`'s ink + /// (see [`Self::plate`]). + pub fn interpret_plate(&self, device: &mut impl Device<'a>, plate: Plate) { + let state = State::new(self.0.root_transform); + let settings = InterpreterSettings { + plate: Some(plate), + ..self.0.settings.clone() + }; + let mut ctx = Context::new_with( + self.0.root_transform, + self.0.bbox, + &self.0.interpreter_cache, + self.0.xref, + settings, + state, + self.0.nesting_depth, + ); + draw_form_xobject(&self.0.parent_resources, &self.0.group, &mut ctx, device); + } + /// Interpret the contents of the mask into the given device. pub fn interpret(&self, device: &mut impl Device<'a>) { let state = State::new(self.0.root_transform); diff --git a/vendor/hayro-interpret/src/x_object.rs b/vendor/hayro-interpret/src/x_object.rs index 83a0aaddb..a7bc2320a 100644 --- a/vendor/hayro-interpret/src/x_object.rs +++ b/vendor/hayro-interpret/src/x_object.rs @@ -278,7 +278,7 @@ pub(crate) fn draw_image_xobject<'a, 'b>( && !x_object.is_mask { let ink_use = match raster_cs { - Some(cs) => cs.ink_use(&plate.ink), + Some(cs) => cs.ink_use(&plate.ink, plate.spots_as_process), None if plate.ink.process_index().is_some() => InkUse::Paints, None => InkUse::Unspecified, }; @@ -687,7 +687,7 @@ fn decode_context<'a>( .unwrap_or(ColorSpace::device_gray()); // PdfCraft patch (35): on a plate, every sample converts to the ink's film. let color_space = match &obj.plate { - Some(plate) => color_space.plate(plate.ink.clone(), plate.simulation.clone()), + Some(plate) => color_space.plate(plate), None => color_space, }; diff --git a/vendor/hayro/SOURCE.json b/vendor/hayro/SOURCE.json index 737e78b53..d7d97f1e1 100644 --- a/vendor/hayro/SOURCE.json +++ b/vendor/hayro/SOURCE.json @@ -68,7 +68,7 @@ "path": "src/renderer.rs", "source_sha256": "f7ff7bd088ceb43823ede33151138384af331cf71403f7d23f7b2937212efc3b", "source_git_blob": "6aa53716ad2d0203a8058b2f7271515b4fc1db22", - "sha256": "81ac25ccc066b4e84e7dedf3abee813948e443c87a32f9f28cb067c3a383904d" + "sha256": "d762789ffe04fae9dc37fb5cf5b81ef59a61742286f4a6b5eb64e52e0c76c39c" } ] } diff --git a/vendor/hayro/src/renderer.rs b/vendor/hayro/src/renderer.rs index fafe4d79d..d8e8010c8 100644 --- a/vendor/hayro/src/renderer.rs +++ b/vendor/hayro/src/renderer.rs @@ -1967,9 +1967,10 @@ fn render_shading_texture( ) } -fn draw_soft_mask(mask: &SoftMask<'_>, settings: RenderSettings, width: u16, height: u16, preview: bool) -> Mask { - let mut renderer = Renderer { - ctx: RenderContext::new_with(width, height, derive_settings(&settings)), +/// A renderer for a soft mask's contents, `width` × `height` device pixels. +fn soft_mask_renderer(settings: &RenderSettings, width: u16, height: u16, preview: bool) -> Renderer { + Renderer { + ctx: RenderContext::new_with(width, height, derive_settings(settings)), // PdfCraft patch: a soft mask must keep its original luminance values, so its // content is never remapped by the reading-colours setting. color_replacement: None, @@ -1985,35 +1986,42 @@ fn draw_soft_mask(mask: &SoftMask<'_>, settings: RenderSettings, width: u16, hei scaler: Scaler::new(ResamplingFunction::CatmullRom), preview, image_transparency_stack: Vec::new(), - }; - - let bg_color = mask.background_color().to_rgba(); - let apply_bg = bg_color.to_rgba8() != BLACK.to_rgba8().to_u8_array(); - - if apply_bg { - renderer - .ctx - .set_paint(AlphaColor::::new(bg_color.components())); - renderer - .ctx - .fill_rect(&Rect::new(0.0, 0.0, width as f64, height as f64)); - renderer.ctx.push_layer(None, None, None, None, None); } +} - mask.interpret(&mut renderer); +fn draw_soft_mask(mask: &SoftMask<'_>, settings: RenderSettings, width: u16, height: u16, preview: bool) -> Mask { + let mut rendered_mask = if mask.mask_type() == MaskType::Luminosity && mask.is_device_cmyk() { + cmyk_luminosity(mask, &settings, width, height, preview) + } else { + let mut renderer = soft_mask_renderer(&settings, width, height, preview); + let bg_color = mask.background_color().to_rgba(); + let apply_bg = bg_color.to_rgba8() != BLACK.to_rgba8().to_u8_array(); - if apply_bg { - renderer.ctx.pop_layer(); - } + if apply_bg { + renderer + .ctx + .set_paint(AlphaColor::::new(bg_color.components())); + renderer + .ctx + .fill_rect(&Rect::new(0.0, 0.0, width as f64, height as f64)); + renderer.ctx.push_layer(None, None, None, None, None); + } - let mut pix = Pixmap::new(width, height); - renderer.ctx.flush(); - let mut resources = vello_cpu::Resources::default(); - renderer.ctx.render_to_pixmap(&mut resources, &mut pix); + mask.interpret(&mut renderer); - let mut rendered_mask = match mask.mask_type() { - MaskType::Luminosity => Mask::new_luminance(&pix), - MaskType::Alpha => Mask::new_alpha(&pix), + if apply_bg { + renderer.ctx.pop_layer(); + } + + let mut pix = Pixmap::new(width, height); + renderer.ctx.flush(); + let mut resources = vello_cpu::Resources::default(); + renderer.ctx.render_to_pixmap(&mut resources, &mut pix); + + match mask.mask_type() { + MaskType::Luminosity => Mask::new_luminance(&pix), + MaskType::Alpha => Mask::new_alpha(&pix), + } }; if let Some(transfer_function) = mask.transfer_function() { @@ -2034,6 +2042,43 @@ fn draw_soft_mask(mask: &SoftMask<'_>, settings: RenderSettings, width: u16, hei rendered_mask } +/// Craft patch (CMYK masks): a luminosity mask whose group is DeviceCMYK. ISO 32000-2 §11.6.5.2 +/// takes the luminosity of the group's CMYK result, which §10.4.2.2 defines as `1 - min(1, 0.3c +/// + 0.59m + 0.11y + k)`. Taken from the colors as drawn, through the CMYK profile, K 100% +/// would leave about a tenth of the masked object visible. So each process ink's plate of the +/// group is drawn over the backdrop's film, and the formula is applied to the four tints. +fn cmyk_luminosity(mask: &SoftMask<'_>, settings: &RenderSettings, width: u16, height: u16, preview: bool) -> Mask { + let backdrop = mask.background_color(); + let pixels = usize::from(width) * usize::from(height); + let mut ink = vec![0.0_f32; pixels]; + for (plate_ink, weight) in hayro_interpret::color::Ink::PROCESS.into_iter().zip([0.3, 0.59, 0.11, 1.0]) { + let plate = mask.plate(plate_ink); + let film = 1.0 - plate.tint(&backdrop); + let mut renderer = soft_mask_renderer(settings, width, height, preview); + renderer.ctx.set_paint(AlphaColor::::new([film, film, film, 1.0])); + renderer + .ctx + .fill_rect(&Rect::new(0.0, 0.0, width as f64, height as f64)); + renderer.ctx.push_layer(None, None, None, None, None); + mask.interpret_plate(&mut renderer, plate); + renderer.ctx.pop_layer(); + + let mut pix = Pixmap::new(width, height); + renderer.ctx.flush(); + let mut resources = vello_cpu::Resources::default(); + renderer.ctx.render_to_pixmap(&mut resources, &mut pix); + // The backdrop is opaque, so every pixel is too: red is the film. + for (sum, px) in ink.iter_mut().zip(pix.data()) { + *sum += weight * (1.0 - f32::from(px.r) / 255.0); + } + } + let values = ink + .into_iter() + .map(|sum| ((1.0 - sum.min(1.0)) * 255.0 + 0.5) as u8) + .collect(); + Mask::from_parts(values, width, height) +} + pub(crate) fn max_factor(transform: &Affine) -> f32 { let scale_skew_transform = { let c = transform.as_coeffs();