Files
craft-libs/crates/pdf/tests/plates.rs
T
aiongg ac3dfd1f53 Merge storytold main into next (17 Nov sync)
storytold main 83cb1133dd (291 commits) into the integration head bab1a99b85; 47 conflicted files.

- Domain test files (metafile, prepress, pdf plates, settings store, heif, media-testkit, atomic): tests next cut stay cut; tests new on main are kept, as trials where the crate's test target runs trials.
- atomic: main's group-bits test and Faulty::set_group go to src/tests.rs. audio: main's GSM and SBC oracles join tests/main.rs. cms constructors, pdf knockout_backdrop and testing_pdf join their crates' one test binary; timeline's preflight tests become src/preflight_tests.rs trials.
- update: main's changes to detect, install (linux, macos) and the tests land in next's split modules. The app's own offer is inspected on a thread of its own, picked up by tick's polling; settle_for_tests settles offers too. Later on a ready update drops the close it asked for. updater_pin_tests.rs stays deleted; its new pin test is in tests/it/update.rs.
- craft-color: main added a different crate at crates/color (the colour tags of compositor and display frames); it is next's craft-color module `tags`, re-exported at the root.
- Old crates/ui (combo, docking, testing, README) and keymap's ui/view.rs stay deleted; main's new crates/ui/src/testing files are dropped with them.
- New crates in the craft facade: ai, control, dds, frame, ipc, media-source, palette, press-print, score, ssip. testkit and xtask are listed outside it.
- Cargo.lock: the union of both locks, resolved by cargo metadata. Workflow: main's nextest Windows leg, with next's --keep-going clippy.
2026-11-17 12:44:52 +00:00

183 lines
8.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Plate mode through the raster path: each ink's separation drawn as a gray film.
//! Adapted from storytold/pdfcraft#2770 by Guillem Hermida (`separations::tests` in pdfcraft-render at
//! 6fcf2a4aca29e943e28cb69017ff8f87d00656c6, MIT OR Apache-2.0).
#![cfg(feature = "raster")]
use craft_pdf::interpret::InterpreterSettings;
use craft_pdf::interpret::color::{CmykSimulation, Ink, Plate};
use craft_pdf::{
open_slice,
raster::{self, Options, RenderCache},
};
/// A 40 × 40 pt page with `content` and `resources`, and `objects` numbered from 5 (each a
/// whole object body: a dictionary, or a dictionary and its stream).
fn page_pdf(content: &str, resources: &str, objects: &[Vec<u8>]) -> Vec<u8> {
let mut out = b"%PDF-1.7\n".to_vec();
out.extend_from_slice(b"1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n");
out.extend_from_slice(b"2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n");
out.extend_from_slice(
format!("3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 40 40] /Contents 4 0 R /Resources << {resources} >> >> endobj\n").as_bytes(),
);
out.extend_from_slice(format!("4 0 obj << /Length {} >> stream\n{content}\nendstream endobj\n", content.len()).as_bytes());
for (i, body) in objects.iter().enumerate() {
out.extend_from_slice(format!("{} 0 obj ", i + 5).as_bytes());
out.extend_from_slice(body);
out.extend_from_slice(b" endobj\n");
}
out.extend_from_slice(b"trailer << /Root 1 0 R >>\n%%EOF");
out
}
/// A stream object body with `dict` entries and `data`.
fn stream(dict: &str, data: &[u8]) -> Vec<u8> {
let mut out = format!("<< {dict} /Length {} >> stream\n", data.len()).into_bytes();
out.extend_from_slice(data);
out.extend_from_slice(b"\nendstream");
out
}
/// The page rendered once per settings, sharing one render cache as a separating caller does.
fn render_each(pdf: &[u8], settings: &[InterpreterSettings]) -> Vec<Vec<u8>> {
let doc = open_slice(pdf, None).unwrap();
let cache = RenderCache::new();
let options = Options { background: [255; 4], ..Default::default() };
settings
.iter()
.map(|settings| {
let raster = raster::render(&doc.pages()[0], &cache, settings, &options).unwrap();
assert_eq!((raster.width, raster.height), (40, 40));
raster.bytes().to_vec()
})
.collect()
}
/// One film per ink, as RGBA8 over white.
struct Plates(Vec<Vec<u8>>);
impl Plates {
/// Each ink's tint at device pixel (x, y), in whole percent.
fn at(&self, x: usize, y: usize) -> Vec<u32> {
self.0
.iter()
.map(|film| {
let px = &film[(y * 40 + x) * 4..][..4];
assert_eq!((px[0], px[3]), (px[2], 255), "a film is opaque gray");
(f32::from(255 - px[0]) * 100.0 / 255.0).round() as u32
})
.collect()
}
}
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(), spots_as_process: false }),
..Default::default()
})
.collect();
Plates(render_each(pdf, &settings))
}
fn spot(name: &str) -> Ink {
Ink::Spot(name.into())
}
/// The four process plates, then `spots`.
fn inks(spots: &[&str]) -> Vec<Ink> {
Ink::PROCESS.into_iter().chain(spots.iter().map(|s| spot(s))).collect()
}
/// The process plates and Gold, overprint honored.
fn separate(pdf: &[u8]) -> Plates {
plates(pdf, &inks(&["Gold"]), true, None)
}
const GOLD: &str = "[/Separation /Gold /DeviceCMYK << /FunctionType 2 /Domain [0 1] /C0 [0 0 0 0] /C1 [0 0.3 1 0] /N 1 >>]";
fn gold_resource() -> String {
format!("/ColorSpace << /CS0 {GOLD} >>")
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn a_spot_color_paints_its_own_plate_and_knocks_out_the_process_plates() {
let s = separate(&page_pdf("0 0 0 1 k 0 0 40 40 re f /CS0 cs 0.5 scn 10 10 20 20 re f", &gold_resource(), &[]));
assert_eq!(s.at(20, 20), [0, 0, 0, 0, 50], "knocked out under the spot color");
assert_eq!(s.at(2, 2), [0, 0, 0, 100, 0]);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn overprinting_keeps_the_plates_a_color_does_not_specify() {
let resources = format!("{} /ExtGState << /GS1 << /op true >> >>", gold_resource());
let pdf = page_pdf("0 0 0 1 k 0 0 40 40 re f /GS1 gs /CS0 cs 0.5 scn 10 10 20 20 re f", &resources, &[]);
assert_eq!(separate(&pdf).at(20, 20), [0, 0, 0, 100, 50], "overprinted");
assert_eq!(plates(&pdf, &inks(&["Gold"]), false, None).at(20, 20), [0, 0, 0, 0, 50], "overprint simulation off: knocked out");
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn nonzero_overprint_mode_leaves_zero_device_cmyk_components() {
let resources = "/ExtGState << /M1 << /OP true /OPM 1 >> /M0 << /OP true /OPM 0 >> >>";
let pdf = |gs: &str| page_pdf(&format!("1 0 0 0 k 0 0 40 40 re f /{gs} gs 0 1 0 0 k 10 10 20 20 re f"), resources, &[]);
let process = inks(&[]);
assert_eq!(plates(&pdf("M1"), &process, true, None).at(20, 20), [100, 100, 0, 0], "OPM 1: the zero cyan leaves the plate");
assert_eq!(plates(&pdf("M0"), &process, true, None).at(20, 20), [0, 100, 0, 0], "OPM 0: zero components paint");
}
/// A knockout group (`/K true`, pdfcraft #2974) of two process squares at 50 % opacity separates
/// with only the top square where they overlap: its plates replace the bottom square's there.
/// Without `/K` the two blend on every plate.
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn a_knockout_group_separates_with_only_the_top_object_in_the_overlap() {
let group = |entries: &str| {
let form = "/A gs 1 0 0 0 k 0 0 25 25 re f 0 1 0 0 k 15 15 25 25 re f";
page_pdf(
"/Fm Do",
"/XObject << /Fm 5 0 R >>",
&[stream(
&format!(
"/Type /XObject /Subtype /Form /BBox [0 0 40 40] /Group << /S /Transparency {entries} >> /Resources << /ExtGState << /A << /ca 0.5 >> >> >>"
),
form.as_bytes(),
)],
)
};
// Device rows run top down: the overlap is at (20, 20), cyan alone at (5, 35), magenta alone
// at (35, 5).
let knockout = plates(&group("/K true"), &inks(&[]), true, None);
assert_eq!(knockout.at(20, 20), [0, 50, 0, 0], "only the magenta square in the overlap");
assert_eq!(knockout.at(5, 35), [50, 0, 0, 0]);
assert_eq!(knockout.at(35, 5), [0, 50, 0, 0]);
let blended = plates(&group(""), &inks(&[]), true, None);
assert_eq!(blended.at(20, 20), [25, 50, 0, 0], "both squares without /K");
}
/// #870: in a non-isolated knockout group each element composites with the group's backdrop in
/// its own blend mode (ISO 32000-2 §11.4.6, §11.4.8). Magenta, then cyan over it, both Multiply,
/// over a yellow page keep the yellow under them, and the cyan replaces the magenta where they
/// overlap. Drawn into a layer starting transparent, the yellow was knocked out.
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn a_non_isolated_knockout_group_multiplies_each_element_with_its_backdrop() {
let form = "/M gs 0 1 0 0 k 5 5 20 20 re f 1 0 0 0 k 15 15 20 20 re f";
let pdf = page_pdf(
"0 0 1 0 k 0 0 40 40 re f /Fm Do",
"/XObject << /Fm 5 0 R >>",
&[stream(
"/Type /XObject /Subtype /Form /BBox [0 0 40 40] /Group << /S /Transparency /CS /DeviceCMYK /K true >> /Resources << /ExtGState << /M << /BM /Multiply >> >> >>",
form.as_bytes(),
)],
);
// Device rows run top down: magenta alone at (8, 32), the overlap at (20, 20), cyan alone at
// (32, 8), the page alone at (37, 37).
let s = plates(&pdf, &inks(&[]), true, None);
assert_eq!(s.at(8, 32), [0, 100, 100, 0], "magenta multiplied with the yellow");
assert_eq!(s.at(20, 20), [100, 0, 100, 0], "cyan replaces the magenta, multiplied with the yellow");
assert_eq!(s.at(32, 8), [100, 0, 100, 0], "cyan multiplied with the yellow");
assert_eq!(s.at(37, 37), [0, 0, 100, 0], "the page");
}