mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-21 20:38:55 +00:00
pdf,eps: enable EPS image rendering, scale step budget, and align CMYK with After Effects (#3677)
This commit is contained in:
+33
-4
@@ -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;
|
||||
|
||||
@@ -253,11 +253,11 @@ fn eps_fallback_font(name: &[u8]) -> Option<&'static [u8]> {
|
||||
}
|
||||
|
||||
fn parse_eps(bytes: &[u8]) -> Result<Doc, Error> {
|
||||
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),
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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,46 @@ 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user