hayro: DeviceCMYK luminosity masks take their values from the inks

A luminosity soft mask whose group is DeviceCMYK draws the group's four process plates over the backdrop and applies 1 - min(1, 0.3c + 0.59m + 0.11y + k) (ISO 32000-2 §10.4.2.2), so K 100% hides the masked object. Spot colorants on those plates paint through their alternate space (Plate::spots_as_process).
This commit is contained in:
aiongg committed 2026-10-23 07:26:21 +00:00
1 parent af39757fea
commit bd5c21682c
12 files changed
+282 -62

No files matched your search

+30 -2
View File
@@ -80,7 +80,10 @@ impl Plates {
fn plates(pdf: &[u8], inks: &[Ink], overprint: bool, simulation: Option<&CmykSimulation>) -> Plates {
let settings: Vec<InterpreterSettings> = 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() });
}
}
+1 -1
View File
@@ -190,7 +190,7 @@ pub fn plates(pdf: &[u8], page: usize, dpi: f32, opts: &Options) -> Result<Plate
let mut size = None;
let mut planes = Vec::with_capacity(separated.len());
for ink in separated {
let plate = Plate { ink, overprint: opts.overprint, simulation: simulation.clone() };
let plate = Plate { ink, overprint: opts.overprint, simulation: simulation.clone(), spots_as_process: false };
let settings = InterpreterSettings { plate: Some(plate), ..Default::default() };
let film = render(p, &cache, &settings, scale)?;
size = Some((film.width, film.height));
+10
View File
@@ -48,6 +48,16 @@ test in `crates/pdf/tests/plates.rs` checks that a knockout group separates with
object where two objects overlap. The erase pass resolves the group's resources with
`Resources::from_scope`, as the group's own draw does in this tree.
A craft-libs patch, labeled `Craft patch (CMYK masks)` in code comments,
takes a luminosity soft mask whose group is DeviceCMYK from the group's inks: ISO 32000-2
§11.6.5.2 and §10.4.2.2 give `1 - min(1, 0.3c + 0.59m + 0.11y + k)`, where the colors as
drawn through the CMYK profile leave K 100% at about a tenth. hayro's `renderer.rs` draws
the group's four process plates over the backdrop's film (`SoftMask::plate`,
`SoftMask::interpret_plate`, `Plate::tint`) and applies the formula. On those plates spot
colorants paint through their alternate space (`Plate::spots_as_process`) and nothing
overprints. Masks in other color spaces are unchanged. A test in
`crates/pdf/tests/plates.rs` checks process, spot, RGB and backdrop colors.
The output-intent conversion of patch 36 goes through a `color::CmykConversion` trait, so
that callers can convert with their own color management. `CmykSimulation::new` keeps
#2770's moxcms conversion as the default, and `CmykSimulation::with_conversion` takes a
+6 -6
View File
@@ -182,7 +182,7 @@
"path": "src/color.rs",
"source_sha256": "799ce29bb5e59c8fc94db202873e48c2047dd35962e6f26efd215156c47158c1",
"source_git_blob": "56bb04f24c04ba33ab6bf99c02a24735670959ad",
"sha256": "50a039fbf0d8122b31fe03c0a06e2d4bb2994f50fd04072f6637233bc2ff9eb6"
"sha256": "ceea74f5c29b533969fa5e724dd77bae007036f5a668b2423332ee6ce7e323a0"
},
{
"path": "src/context.rs",
@@ -362,13 +362,13 @@
"path": "src/interpret/mod.rs",
"source_sha256": "2b002323055bc19aa497e92c81ad8e06dc60ee03686cad89366df4ac7c7a06b2",
"source_git_blob": "fe08fc284ba7c914d1414b68a5a346674a6f910f",
"sha256": "6b0c0080f1301bcdc24c03777b056d0501815c82cf958923595b29266349f50c"
"sha256": "0fa907a9124535f1f1466c887a773e68eba54d78076ee65b9d9c41ba5a3b0ad8"
},
{
"path": "src/interpret/path.rs",
"source_sha256": "a5defbc4b883b262662f7e7474a00ec1f97dbef8c03b8669859f624f5d02344b",
"source_git_blob": "838ea099f11d918d6e333e32503368be864689f8",
"sha256": "eb42777dc3063794c21aa914b61e40a8547820de3e4d7b47924d169890f4fbb4"
"sha256": "3104a301f331bf1902318ad35a1ea2f2d8e2a1c84ec352ea66658732afc9ef06"
},
{
"path": "src/interpret/state.rs",
@@ -410,13 +410,13 @@
"path": "src/shading.rs",
"source_sha256": "5d541a2d52f69c88e87a40580bf28afa4b8a6a2d62a3385009087264384ead32",
"source_git_blob": "2e619e47f2800d7fc7cbdae77c66c58f012c84c2",
"sha256": "6e80c039ab7ca38ede2b1778709f1b4258d8cf219d044d3298f4a5f6dd2c8f9a"
"sha256": "c6008d05330877ebc2b3b5eccc269aedc0242c1f81b1e522c42ecd4ba897349d"
},
{
"path": "src/soft_mask.rs",
"source_sha256": "6cd9c59c0cff580879c1f06ff40f3537c04b719bdb3ea59008fa1ab7559db5dd",
"source_git_blob": "4d97d565b53c820fa927fbba8c3dda6acead7f34",
"sha256": "0e3871896575006f194fb46242fcaa6e1026f459c6144c38da438f82ace95e50"
"sha256": "fe3da157602bf5b16a452ecbe1d8d554af992a5b9dfaa771dd23c2e2e66d730b"
},
{
"path": "src/types.rs",
@@ -434,7 +434,7 @@
"path": "src/x_object.rs",
"source_sha256": "e1c51e96a335c32c122011b2d05c5020c36a33c2c73342ba173c28a3e2fe0219",
"source_git_blob": "21b134ce8cd9604399cd0776febf923d69c02673",
"sha256": "a1828456a715f0d2a6a76ab10656a018d4b94c4012050b739bce09728ac27225"
"sha256": "8ddb1ceb10fe80f62c4a01af23842a92947f5fac8d9b0fe54b3761af3e290d74"
}
],
"compatibility_reference": {
+90 -14
View File
@@ -1271,7 +1271,7 @@ impl Color {
pub(crate) fn for_plate(&self, plate: &Plate, overprint: bool, overprint_mode: u8) -> 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<CmykSimulation>,
/// 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<Hub> = 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<CmykSimulation>) -> 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<CmykSimulation>,
/// 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)
}
+3 -2
View File
@@ -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<crate::color::Plate>,
/// PdfCraft patch (35): told about each spot ink a paint uses (colours, images, shadings
/// and uncoloured patterns), whether or not `plate` is set.
+5 -1
View File
@@ -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 => {}
+7 -4
View File
@@ -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<Self> {
+55 -2
View File
@@ -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<CmykSimulation>,
}
impl Hash for Repr<'_> {
@@ -128,6 +132,7 @@ impl<'a> SoftMask<'a> {
.and_then(|g| g.get::<Object<'_>>(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::<Object<'_>>(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);
+2 -2
View File
@@ -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,
};
+1 -1
View File
@@ -68,7 +68,7 @@
"path": "src/renderer.rs",
"source_sha256": "f7ff7bd088ceb43823ede33151138384af331cf71403f7d23f7b2937212efc3b",
"source_git_blob": "6aa53716ad2d0203a8058b2f7271515b4fc1db22",
"sha256": "81ac25ccc066b4e84e7dedf3abee813948e443c87a32f9f28cb067c3a383904d"
"sha256": "d762789ffe04fae9dc37fb5cf5b81ef59a61742286f4a6b5eb64e52e0c76c39c"
}
]
}
+72 -27
View File
@@ -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::<Srgb>::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::<Srgb>::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::<Srgb>::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();