From 61dfd6c4dc2b67ac72a2b2c7946a2685099f477a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 21 Nov 2026 14:34:09 -0500 Subject: [PATCH] EPS: enable sampled images, scale step budget, and align CMYK with After Effects (#4057) * pdf,eps: enable EPS image rendering, scale step budget, and align CMYK with After Effects (#3677) * chore: repin craft-ps to 068e079c for save/restore dictionary journaling (#790) * chore: align craft-pdf/hayro revs and xtask pdf-deps pin to 068e079c --- Cargo.lock | 10 +++--- Cargo.toml | 8 ++--- crates/pdf/src/color.rs | 37 +++++++++++++++++++--- crates/pdf/src/lib.rs | 6 ++-- crates/pdf/src/tests.rs | 2 +- crates/pdf/src/tests/import.rs | 58 ++++++++++++++++++++++++++++++++-- xtask/src/pdf_deps.rs | 2 +- 7 files changed, 102 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81cc29132..cab80bb90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1319,7 +1319,7 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=808730f49b9637b7909fe2ae56f9d63fdb100cae#808730f49b9637b7909fe2ae56f9d63fdb100cae" +source = "git+https://github.com/storytold/craft-libs?rev=068e079c7be4b100a8a6b305b31ac881e069727e#068e079c7be4b100a8a6b305b31ac881e069727e" [[package]] name = "craft-exr" @@ -1485,7 +1485,7 @@ dependencies = [ [[package]] name = "craft-pdf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=808730f49b9637b7909fe2ae56f9d63fdb100cae#808730f49b9637b7909fe2ae56f9d63fdb100cae" +source = "git+https://github.com/storytold/craft-libs?rev=068e079c7be4b100a8a6b305b31ac881e069727e#068e079c7be4b100a8a6b305b31ac881e069727e" dependencies = [ "hayro-syntax", "thiserror 2.0.21", @@ -1494,7 +1494,7 @@ dependencies = [ [[package]] name = "craft-ps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=808730f49b9637b7909fe2ae56f9d63fdb100cae#808730f49b9637b7909fe2ae56f9d63fdb100cae" +source = "git+https://github.com/storytold/craft-libs?rev=068e079c7be4b100a8a6b305b31ac881e069727e#068e079c7be4b100a8a6b305b31ac881e069727e" dependencies = [ "craft-eps", "kurbo", @@ -3761,7 +3761,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a" [[package]] name = "hayro-jbig2" version = "0.3.0" -source = "git+https://github.com/storytold/craft-libs?rev=808730f49b9637b7909fe2ae56f9d63fdb100cae#808730f49b9637b7909fe2ae56f9d63fdb100cae" +source = "git+https://github.com/storytold/craft-libs?rev=068e079c7be4b100a8a6b305b31ac881e069727e#068e079c7be4b100a8a6b305b31ac881e069727e" dependencies = [ "hayro-ccitt", ] @@ -3775,7 +3775,7 @@ checksum = "c75ab947623ef4ccaa7acf0579edf7cbb5a73838e3839a7be73335e522f433a1" [[package]] name = "hayro-syntax" version = "0.7.2" -source = "git+https://github.com/storytold/craft-libs?rev=808730f49b9637b7909fe2ae56f9d63fdb100cae#808730f49b9637b7909fe2ae56f9d63fdb100cae" +source = "git+https://github.com/storytold/craft-libs?rev=068e079c7be4b100a8a6b305b31ac881e069727e#068e079c7be4b100a8a6b305b31ac881e069727e" dependencies = [ "hayro-jbig2", "hayro-jpeg2000", diff --git a/Cargo.toml b/Cargo.toml index c1c46eb1d..b993313dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,13 +13,13 @@ repository = "https://github.com/storytold/effectcraft" [workspace.dependencies] craft-timeline = { git = "https://github.com/storytold/craft-libs", rev = "0d6dd8bed815b8366869a800d2bf22588ca62dcb", default-features = false } craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } -craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", version = "=0.1.0", default-features = false, features = ["read"] } +craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "068e079c7be4b100a8a6b305b31ac881e069727e", version = "=0.1.0", default-features = false, features = ["read"] } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-jsonrpc = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "bc186ca719958fcce3b6ee24794277eabed14609" } craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } -craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] } +craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "068e079c7be4b100a8a6b305b31ac881e069727e", default-features = false, features = ["read"] } craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "3b8e426a260695af840a2cd79a064d40778f939e" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ba25ccd233934d1b2f74e3c0f235168bf5f13ef3" } craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c" } @@ -231,8 +231,8 @@ lto = "thin" codegen-units = 1 [patch.crates-io] -hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", version = "=0.7.2" } -hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", version = "=0.3.0" } +hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "068e079c7be4b100a8a6b305b31ac881e069727e", version = "=0.7.2" } +hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "068e079c7be4b100a8a6b305b31ac881e069727e", version = "=0.3.0" } ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } diff --git a/crates/pdf/src/color.rs b/crates/pdf/src/color.rs index 8b150f907..ff141990e 100644 --- a/crates/pdf/src/color.rs +++ b/crates/pdf/src/color.rs @@ -53,10 +53,7 @@ impl Cs { match self { Cs::Gray => [g(0); 3], Cs::Rgb | Cs::Pattern => [g(0), g(1), g(2)], - Cs::Cmyk => { - let k = g(3); - [(1.0 - g(0)) * (1.0 - k), (1.0 - g(1)) * (1.0 - k), (1.0 - g(2)) * (1.0 - k)] - } + Cs::Cmyk => cmyk_to_rgb(g(0), g(1), g(2), g(3)), Cs::Lab => lab_to_rgb(c.first().copied().unwrap_or(0.0), c.get(1).copied().unwrap_or(0.0), c.get(2).copied().unwrap_or(0.0)), Cs::Indexed { base, hival, lookup } => { let i = (c.first().copied().unwrap_or(0.0).round().max(0.0) as usize).min(*hival); @@ -69,6 +66,38 @@ impl Cs { } } +/// The screen colour of each solid overprint of cyan, magenta and yellow, indexed by +/// `c * 4 + m * 2 + y`. Paper is white. +const INK_SOLIDS: [[f64; 3]; 8] = [ + [1.0, 1.0, 1.0], + [0.9422, 0.9861, 0.1296], + [0.9734, 0.1382, 0.5601], + [1.0, 0.1424, 0.0305], + [0.0754, 0.6765, 0.8047], + [0.0, 0.5965, 0.2797], + [0.2292, 0.0166, 0.5538], + [0.0251, 0.002, 0.0], +]; +/// How each ink's value (C, M, Y, K) becomes the area it covers. +const INK_COVERAGE: [f64; 4] = [1.0774, 1.0434, 1.0474, 1.1823]; + +/// DeviceCMYK (each 0 to 1) to RGB, as the Neugebauer equations model halftone printing: the +/// solid colour of every combination of the inks, weighted by the area that combination +/// covers. The solids and the coverage curves are fitted to what After Effects shows for a +/// chart of 1,296 CMYK patches. `(1 - c)(1 - k)` makes every pale pink a bright magenta. +pub(crate) fn cmyk_to_rgb(c: f64, m: f64, y: f64, k: f64) -> [f64; 3] { + let area = |i: usize, v: f64| v.clamp(0.0, 1.0).powf(INK_COVERAGE[i]); + let (ac, am, ay, ak) = (area(0, c), area(1, m), area(2, y), area(3, k)); + let mut out = [0.0; 3]; + for (i, solid) in INK_SOLIDS.iter().enumerate() { + let w = (if i & 4 != 0 { ac } else { 1.0 - ac }) * (if i & 2 != 0 { am } else { 1.0 - am }) * (if i & 1 != 0 { ay } else { 1.0 - ay }); + for (o, s) in out.iter_mut().zip(solid) { + *o += w * s; + } + } + out.map(|v| v * (1.0 - ak)) +} + fn lab_to_rgb(l: f64, a: f64, b: f64) -> [f64; 3] { let fy = (l + 16.0) / 116.0; let fx = fy + a / 500.0; diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index 4fd5d701c..884ffc1f8 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -253,11 +253,11 @@ fn eps_fallback_font(name: &[u8]) -> Option<&'static [u8]> { } fn parse_eps(bytes: &[u8]) -> Result { + let steps = (bytes.len() as u64).saturating_mul(4).clamp(8_000_000, 256_000_000); let limits = craft_ps::read::Limits { - // Preserve EffectCraft's established 8,000,000-unit PostScript execution budget. - steps: 8_000_000, + steps, stack_overflow_noop: true, - skip_images: true, + skip_images: false, keep_unpainted: true, zero_division_zero: true, fallback_font: Some(eps_fallback_font), diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index c1f519595..9ac6bb6b1 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -129,7 +129,7 @@ fn pdf_forms_rotation_cmyk_and_alpha() { // The form (CMYK red, clipped to its 10×10 box, scaled ×2) covers PDF (0..20, 0..20); the // half-transparent black square covers PDF (0..10, 0..10). /Rotate 90: PDF (x, y) → (y, x). let red = img.get(15, 15); - assert!(red[0] > 0.99 && red[1] < 0.01 && red[3] > 0.99, "{red:?}"); + assert!(red[0] > 0.99 && (red[1] - 0.1424).abs() < 0.01 && (red[2] - 0.0305).abs() < 0.01, "CMYK 0 1 1 0, as After Effects shows it: {red:?}"); let dark = img.get(5, 5); assert!((dark[0] - 0.5).abs() < 0.02 && dark[3] > 0.99, "{dark:?}"); assert_eq!(img.get(30, 30)[3], 0.0, "form clipped to its bounding box"); diff --git a/crates/pdf/src/tests/import.rs b/crates/pdf/src/tests/import.rs index 80be350f6..68d6487c0 100644 --- a/crates/pdf/src/tests/import.rs +++ b/crates/pdf/src/tests/import.rs @@ -49,9 +49,7 @@ fn eps_with_aliased_operators_device_queries_and_embedded_data_draws() { fn eps_filter_on_a_string_source_does_not_skip_the_program() { let square = "1 0 0 setrgbcolor 50 25 moveto 150 25 lineto 150 75 lineto 50 75 lineto closepath fill\n"; let img = "gsave 1 1 8 [1 0 0 1 0 0] (FF) /ASCIIHexDecode filter image grestore\n"; - // Images aren't drawn yet, so this parses without `eps`'s nothing-skipped check. - let doc = parse(format!("%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 200 100\n{img}{square}").as_bytes()).unwrap(); - assert_eq!(doc.skipped, ["image"]); + let doc = eps(format!("{img}{square}").as_bytes()); assert!(coverage(&render(&doc), 50, 25, 150, 75) > 4900.0, "drawing after an image read from a string"); } @@ -643,3 +641,57 @@ fn eps_unembedded_reencoded_accent_uses_bundled_glyph() { assert!(ia > 50.0 && (ia - ib).abs() < 1.0, "EPS accent {ia} vs PDF accent {ib}"); assert_eq!(ink_x(&a), ink_x(&b)); } + +#[test] +fn eps_images_are_drawn() { + // Hexadecimal data read by a procedure; the program goes on after it. + let doc = eps(b"gsave 100 50 scale 2 1 8 [2 0 0 -1 0 1] { currentfile 6 string readhexstring pop } false 3 colorimage\nFF0000 00FF00\ngrestore 0 0 1 setrgbcolor 150 0 10 10 rectfill\n"); + let img = render(&doc); + let (red, green) = (img.get(25, 75), img.get(75, 75)); + assert!(red[0] > 0.9 && red[1] < 0.1 && red[3] > 0.99, "{red:?}"); + assert!(green[1] > 0.9 && green[0] < 0.1 && green[3] > 0.99, "{green:?}"); + assert!(img.get(155, 95)[2] > 0.99, "drawing continues after the image data"); + + // A stencil paints the current colour where its sample is 0. + let doc = eps(b"1 0 0 setrgbcolor 100 50 scale 2 1 false [2 0 0 -1 0 1] {<40>} imagemask\n"); + let img = render(&doc); + assert!(img.get(25, 75)[0] > 0.9 && img.get(25, 75)[3] > 0.9, "{:?}", img.get(25, 75)); + assert_eq!(img.get(75, 75)[3], 0.0); +} + +#[test] +fn cmyk_converts_as_the_measured_chart_does() { + // Measured from After Effects' rendering of a CMYK chart, within 3 of 255. + for (cmyk, want) in [ + ([0.0, 0.0, 0.0, 0.0], [255.0, 255.0, 255.0]), + ([0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0]), + ([0.0, 0.25, 0.0, 0.0], [255.0, 205.0, 229.0]), + ([0.0, 0.1, 0.0, 0.0], [255.0, 235.0, 244.0]), + ] { + let got = crate::color::cmyk_to_rgb(cmyk[0], cmyk[1], cmyk[2], cmyk[3]).map(|v| v * 255.0); + for (g, w) in got.iter().zip(want) { + assert!((g - w).abs() < 3.0, "{cmyk:?}: {got:?} vs {want:?}"); + } + } +} + +#[test] +fn a_prolog_keeps_the_built_in_cmyk_conversion() { + // The idiom of Illustrator's EPS prolog: define the operator only when it does not exist. + let doc = eps(b"/setcmykcolor where { pop } { /setcmykcolor { pop pop pop pop 0 1 0 setrgbcolor } def } ifelse 0 1 1 0 setcmykcolor 0 0 10 10 rectfill\n"); + let c = render(&doc).get(5, 95); + assert!(c[0] > 0.9 && c[1] < 0.3, "{c:?}"); + let doc = eps(b"0.1 0.2 0.3 0.4 setcmykcolor currentcmykcolor 0.4 eq exch 0.3 eq and exch 0.2 eq and exch 0.1 eq and { 0 0 10 10 rectfill } if\n"); + assert_eq!(shape_names(&doc).len(), 1); +} + +#[test] +fn eps_restore_undoes_dictionary_changes() { + // Illustrator's prolog counts graphics states in a dictionary and relies on `restore` to + // take the count back down. + let doc = eps(b"/w 10 def /d 1 dict def d /v 1 put save /w 40 def d /v 2 put restore 0 0 w 10 rectfill d /v get 1 eq { 100 0 10 10 rectfill } if\n"); + let img = render(&doc); + assert_eq!(img.get(20, 95)[3], 0.0, "w is 10 again"); + assert!(img.get(5, 95)[3] > 0.99); + assert!(img.get(105, 95)[3] > 0.99, "d's entry is 1 again"); +} diff --git a/xtask/src/pdf_deps.rs b/xtask/src/pdf_deps.rs index e3aab07a7..15da0dac7 100644 --- a/xtask/src/pdf_deps.rs +++ b/xtask/src/pdf_deps.rs @@ -5,7 +5,7 @@ use std::process::Command; use serde_json::Value; -const REV: &str = "808730f49b9637b7909fe2ae56f9d63fdb100cae"; +const REV: &str = "068e079c7be4b100a8a6b305b31ac881e069727e"; const PACKAGES: &[(&str, &str)] = &[("craft-pdf", "0.1.0"), ("craft-ps", "0.1.0"), ("hayro-syntax", "0.7.2"), ("hayro-jbig2", "0.3.0")]; pub fn run(root: &Path) -> Result<(), String> {