Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Integration: #836 a grey stop beside a CMYK stop exports as the K ink, keeping the PDF shading in DeviceCMYK

aiongg87682c367479
diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs
index 73a8cb269..24c2798a2 100644
--- a/crates/color/src/lib.rs
+++ b/crates/color/src/lib.rs
@@ -169,7 +169,6 @@ impl Color {
let [r, g, b] = self.to_rgb();
Color::rgb(1.0 - r, 1.0 - g, 1.0 - b)
}
- /// Linear interpolation in display RGB.
/// The colour `t` of the way to `other`. Process inks blend as inks (CMYK with CMYK or grey,
/// grey with grey), as InDesign blends a gradient's stops; anything else blends in RGB.
pub fn lerp(&self, other: &Color, t: f32) -> Color {
diff --git a/crates/pdf/src/export.rs b/crates/pdf/src/export.rs
index dcf8abb83..d66ad6438 100644
--- a/crates/pdf/src/export.rs
+++ b/crates/pdf/src/export.rs
@@ -981,7 +981,15 @@ impl Exporter<'_> {
let (swatch, tint, angle) = (fill.swatch.as_str(), fill.tint, fill.gradient_angle);
if let Some(g) = designcraft_color::swatch::resolve_gradient(&self.doc.swatches, swatch) {
let kind = g.kind;
- let expanded = g.expanded_stops();
+ let mut expanded = g.expanded_stops();
+ // Grey and CMYK stops blend as inks, so a grey stop beside a CMYK one is the K ink.
+ if !self.rgb_only && expanded.iter().any(|(_, c, _)| matches!(c, Color::Cmyk { .. })) {
+ for (_, c, _) in &mut expanded {
+ if let Color::Gray { k } = *c {
+ *c = Color::cmyk(0.0, 0.0, 0.0, k);
+ }
+ }
+ }
// A PDF shading needs one colour space. Midpoint expansion can introduce RGB even
// when every authored stop is CMYK/Gray; PDF/A can also change the output space.
let components = |c: &Color| match c {
diff --git a/crates/pdf/tests/gradient_color_spaces.rs b/crates/pdf/tests/gradient_color_spaces.rs
index f9892965b..05806a120 100644
--- a/crates/pdf/tests/gradient_color_spaces.rs
+++ b/crates/pdf/tests/gradient_color_spaces.rs
@@ -53,7 +53,7 @@ fn mixed_gradient_export_survives_in_subprocess() {
let rgb = Color::rgb(1.0, 0.0, 0.0);
let cmyk = Color::cmyk(1.0, 0.0, 0.0, 0.0);
let gray = Color::gray(0.4);
- for colors in [vec![cmyk, rgb], vec![rgb, cmyk], vec![rgb, gray], vec![gray, cmyk], vec![gray, rgb, cmyk]] {
+ for colors in [vec![cmyk, rgb], vec![rgb, cmyk], vec![rgb, gray], vec![gray, rgb, cmyk]] {
for kind in [GradientKind::Linear, GradientKind::Radial] {
for standard in [Standard::None, Standard::PdfX4, Standard::PdfA2b] {
let d = document(&colors, kind, 0.5, 0.7);
@@ -107,6 +107,8 @@ fn homogeneous_gradients_keep_device_spaces_and_components() {
([Color::rgb(1.0, 0.0, 0.0), Color::rgb(0.0, 0.0, 1.0)], "DeviceRGB", vec![1.0, 0.0, 0.0], vec![0.0, 0.0, 1.0]),
([Color::cmyk(1.0, 0.0, 0.0, 0.0), Color::cmyk(0.0, 1.0, 0.0, 0.0)], "DeviceCMYK", vec![1.0, 0.0, 0.0, 0.0], vec![0.0, 1.0, 0.0, 0.0]),
([Color::gray(0.2), Color::gray(0.8)], "DeviceGray", vec![0.8], vec![0.2]),
+ // Grey beside CMYK is the K ink, so the shading stays CMYK.
+ ([Color::gray(0.4), Color::cmyk(1.0, 0.0, 0.0, 0.0)], "DeviceCMYK", vec![0.0, 0.0, 0.0, 0.4], vec![1.0, 0.0, 0.0, 0.0]),
] {
for standard in [Standard::None, Standard::PdfX4] {
let report = export_pdf_with_report(