diff --git a/crates/tok-pdf/assets/icc/b2a.bin b/crates/tok-pdf/assets/icc/b2a.bin
index 3f4e5aa..3dda067 100644
Binary files a/crates/tok-pdf/assets/icc/b2a.bin and b/crates/tok-pdf/assets/icc/b2a.bin differ
diff --git a/crates/tok-pdf/assets/icc/gamt.bin b/crates/tok-pdf/assets/icc/gamt.bin
new file mode 100644
index 0000000..c32e5c9
Binary files /dev/null and b/crates/tok-pdf/assets/icc/gamt.bin differ
diff --git a/crates/tok-pdf/assets/icc/gen_icc_luts.py b/crates/tok-pdf/assets/icc/gen_icc_luts.py
index e4edaf5..b481bc3 100644
--- a/crates/tok-pdf/assets/icc/gen_icc_luts.py
+++ b/crates/tok-pdf/assets/icc/gen_icc_luts.py
@@ -18,6 +18,7 @@
A2B_GRID = 9
B2A_GRID = 9
+GAMT_GRID = 17
# D50 reference white
XN, YN, ZN = 0.9642, 1.0, 0.8249
@@ -72,12 +73,20 @@ def cmyk_to_rgb(c, m, y, k):
def rgb_to_cmyk(r, g, b):
+ """Maximum-GCR separation: the black channel carries the whole grey component.
+
+ Neutrals therefore land on K only, and the three coloured inks together never
+ exceed 200%, so the total ink stays below 300% without any further limiting.
+ The model above is multiplicative, so this separation is colorimetrically exact.
+ """
r, g, b = (min(max(v, 0.0), 1.0) for v in (r, g, b))
- k = 1 - max(r, g, b)
- if k >= 1 - 1e-9:
+ top = max(r, g, b)
+ # Near-black (including values pushed slightly off-neutral by the 16-bit Lab encoding)
+ # would otherwise blow tiny colour differences up into full-strength inks.
+ if top <= 1e-3:
return 0.0, 0.0, 0.0, 1.0
- d = 1 - k
- return (1 - r - k) / d, (1 - g - k) / d, (1 - b - k) / d, k
+ k = 1 - top
+ return (top - r) / top, (top - g) / top, (top - b) / top, k
def u16(v):
@@ -133,20 +142,48 @@ def build_a2b():
def build_b2a():
clut = []
n = B2A_GRID - 1
+ # The input tables are the identity over 0..65535, so grid node i sits at the 16-bit
+ # input value i / n * 65535 on every channel (L, a and b alike).
for li in range(B2A_GRID):
for ai in range(B2A_GRID):
for bi in range(B2A_GRID):
- l = dec_l(li / n * 65280)
- a = dec_ab(ai / n * 65280)
- b = dec_ab(bi / n * 65280)
+ l = dec_l(li / n * 65535)
+ a = dec_ab(ai / n * 65535)
+ b = dec_ab(bi / n * 65535)
r, g, bl = xyz_d50_to_rgb(*lab_to_xyz(l, a, b))
c, m, y, k = rgb_to_cmyk(r, g, bl)
clut += [u16(c * 65535), u16(m * 65535), u16(y * 65535), u16(k * 65535)]
return lut16(3, 4, B2A_GRID, clut)
+def build_gamt():
+ """Gamut tag (Lab -> 1 channel): 0 inside the model's gamut, rising with the overshoot outside.
+
+ The model reaches every sRGB colour and nothing beyond it, so "outside" means a linear
+ sRGB component below 0 or above 1.
+ """
+ clut = []
+ n = GAMT_GRID - 1
+ for li in range(GAMT_GRID):
+ for ai in range(GAMT_GRID):
+ for bi in range(GAMT_GRID):
+ l = dec_l(li / n * 65535)
+ a = dec_ab(ai / n * 65535)
+ b = dec_ab(bi / n * 65535)
+ x, y, z = lab_to_xyz(l, a, b)
+ rl = 3.1338561 * x - 1.6168667 * y - 0.4906146 * z
+ gl = -0.9787684 * x + 1.9161415 * y + 0.0334540 * z
+ bl = 0.0719453 * x - 0.2289914 * y + 1.4052427 * z
+ over = max(0.0, max(rl, gl, bl) - 1.0, -min(rl, gl, bl))
+ if over < 1e-4:
+ over = 0.0
+ clut.append(u16(min(over * 4.0, 1.0) * 65535))
+ return lut16(3, 1, GAMT_GRID, clut)
+
+
if __name__ == "__main__":
out_dir = Path(sys.argv[1] if len(sys.argv) > 1 else ".")
out_dir.mkdir(parents=True, exist_ok=True)
(out_dir / "a2b.bin").write_bytes(build_a2b())
(out_dir / "b2a.bin").write_bytes(build_b2a())
+ (out_dir / "gamt.bin").write_bytes(build_gamt())
diff --git a/crates/tok-pdf/src/color.rs b/crates/tok-pdf/src/color.rs
index ecb8e0f..720f4d6 100644
--- a/crates/tok-pdf/src/color.rs
+++ b/crates/tok-pdf/src/color.rs
@@ -127,7 +127,10 @@ impl IccProfileId {
}
}
- /// Official PDF/X `OutputConditionIdentifier` registered at `http://www.color.org`.
+ /// Identifier of the registered press condition this profile approximates.
+ ///
+ /// Informational (CLI output, UI): the PDF itself declares a custom output condition,
+ /// because the embedded profile is a generic approximation and not that characterisation.
pub fn output_condition_identifier(&self) -> &'static str {
match self {
Self::Fogra39 => "FOGRA39",
@@ -149,7 +152,8 @@ impl IccProfileId {
}
}
- /// Registry URL for PDF/X OutputIntent.
+ /// Registry that lists the condition named by `output_condition_identifier`.
+ /// Not written to the PDF, which declares a custom (unregistered) output condition.
pub fn registry_name(&self) -> &'static str {
"http://www.color.org"
}
@@ -273,6 +277,8 @@ impl ColorManagementEngine {
// CMYK -> Lab and Lab -> CMYK `lut16Type` tag data.
const A2B_LUT: &[u8] = include_bytes!("../assets/icc/a2b.bin");
const B2A_LUT: &[u8] = include_bytes!("../assets/icc/b2a.bin");
+ // Lab -> gamut distance (0 inside the gamut); ICC v2 requires it for LUT-based output.
+ const GAMT: &[u8] = include_bytes!("../assets/icc/gamt.bin");
let desc_str = format!("TypesetOK approximation of {}", profile.info());
let cprt_str = "Copyright TypesetOK Pre-Press Color Engine (ISO 15076-1)";
@@ -305,10 +311,10 @@ impl ColorManagementEngine {
wtpt_tag.extend_from_slice(&0x0000_D32Du32.to_be_bytes()); // 0.8249 in s15Fixed16
// Distinct data blocks, stored in this order. Every block starts on a 4-byte boundary.
- let blocks: [&[u8]; 5] = [&desc_tag, &cprt_tag, &wtpt_tag, A2B_LUT, B2A_LUT];
+ let blocks: [&[u8]; 6] = [&desc_tag, &cprt_tag, &wtpt_tag, A2B_LUT, B2A_LUT, GAMT];
// Tag table: signature and the block it points at. The three rendering intents of each
// transform share one block.
- let tags: [(&[u8; 4], usize); 9] = [
+ let tags: [(&[u8; 4], usize); 10] = [
(b"desc", 0),
(b"cprt", 1),
(b"wtpt", 2),
@@ -318,12 +324,13 @@ impl ColorManagementEngine {
(b"B2A0", 4),
(b"B2A1", 4),
(b"B2A2", 4),
+ (b"gamt", 5),
];
let header_size = 128u32;
let tag_count = tags.len() as u32;
let tag_table_size = 4 + tag_count * 12;
- let mut offsets = [0u32; 5];
+ let mut offsets = [0u32; 6];
let mut cursor = header_size + tag_table_size;
for (offset, block) in offsets.iter_mut().zip(blocks.iter()) {
*offset = cursor;
@@ -421,6 +428,51 @@ mod tests {
);
}
+ /// Regression: the Lab -> CMYK table used to put 100% cyan and 64% magenta under pure black
+ /// (about 364% total ink) and tinted every dark neutral with coloured inks.
+ #[test]
+ fn icc_b2a_table_keeps_neutrals_on_black_and_within_ink_limit() {
+ let icc = ColorManagementEngine::generate_icc_profile_bytes(IccProfileId::Fogra39);
+ let be32 = |at: usize| u32::from_be_bytes(icc[at..at + 4].try_into().unwrap());
+ let tag_count = be32(128) as usize;
+ let b2a_offset = (0..tag_count)
+ .map(|i| 132 + i * 12)
+ .find(|&entry| icc[entry..entry + 4] == *b"B2A0")
+ .map(|entry| be32(entry + 4) as usize)
+ .expect("B2A0 tag");
+ assert_eq!(&icc[b2a_offset..b2a_offset + 4], b"mft2");
+ assert_eq!(icc[b2a_offset + 8], 3, "B2A0 has 3 input channels (Lab)");
+ assert_eq!(icc[b2a_offset + 9], 4, "B2A0 has 4 output channels (CMYK)");
+ let grid = icc[b2a_offset + 10] as usize;
+ // 52-byte header, then three input tables of two 16-bit entries, then the grid.
+ let clut = b2a_offset + 52 + 3 * 2 * 2;
+ let node = |l: usize, a: usize, b: usize| -> [f32; 4] {
+ let at = clut + ((l * grid + a) * grid + b) * 4 * 2;
+ let mut ink = [0.0f32; 4];
+ for (i, value) in ink.iter_mut().enumerate() {
+ let hi = icc[at + i * 2];
+ let lo = icc[at + i * 2 + 1];
+ *value = f32::from(u16::from_be_bytes([hi, lo])) / 65535.0;
+ }
+ ink
+ };
+ let neutral = grid / 2;
+ for l in 0..grid {
+ let [c, m, y, _] = node(l, neutral, neutral);
+ assert!(
+ c < 0.01 && m < 0.01 && y < 0.01,
+ "neutral node {l} must use the black channel only, got c={c} m={m} y={y}"
+ );
+ for a in 0..grid {
+ for b in 0..grid {
+ let total: f32 = node(l, a, b).iter().sum();
+ assert!(total <= 3.0 + 1e-3, "node ({l},{a},{b}) has {total} ink");
+ }
+ }
+ }
+ assert_eq!(node(0, neutral, neutral)[3], 1.0, "L=0 is pure 100% K");
+ }
+
#[test]
fn color_management_engine_enforces_tac_and_preserves_pure_black() {
// Pure RGB black -> pure 100% K (0, 0, 0, 1)
@@ -459,7 +511,7 @@ mod tests {
let be32 = |at: usize| u32::from_be_bytes(icc_bytes[at..at + 4].try_into().unwrap());
assert_eq!(be32(0) as usize, icc_bytes.len(), "profile size field");
let tag_count = be32(128) as usize;
- assert_eq!(tag_count, 9);
+ assert_eq!(tag_count, 10);
let mut signatures = Vec::new();
for i in 0..tag_count {
let entry = 132 + i * 12;
@@ -478,5 +530,6 @@ mod tests {
}
assert!(signatures.iter().any(|s| s == b"A2B0"));
assert!(signatures.iter().any(|s| s == b"B2A0"));
+ assert!(signatures.iter().any(|s| s == b"gamt"));
}
}
diff --git a/crates/tok-pdf/src/pdf_engine.rs b/crates/tok-pdf/src/pdf_engine.rs
index 8c26ae1..c7d8a44 100644
--- a/crates/tok-pdf/src/pdf_engine.rs
+++ b/crates/tok-pdf/src/pdf_engine.rs
@@ -236,6 +236,109 @@ impl LineTextWriter {
}
}
+/// Producing application, written to both the Info dictionary and the XMP packet.
+const CREATOR_TOOL: &str = "TypesetOK Native Pre-Press Engine 2026";
+
+/// Escapes text for use inside an XML element (characters XML 1.0 forbids are dropped).
+fn xml_escape(s: &str) -> String {
+ let mut out = String::with_capacity(s.len());
+ for ch in s.chars() {
+ match ch {
+ '&' => out.push_str("&"),
+ '<' => out.push_str("<"),
+ '>' => out.push_str(">"),
+ '"' => out.push_str("""),
+ '\'' => out.push_str("'"),
+ c if u32::from(c) < 0x20 && !matches!(c, '\t' | '\n' | '\r') => {}
+ c => out.push(c),
+ }
+ }
+ out
+}
+
+/// Date for the XMP packet: ISO 8601 with a time zone, matching what `parse_pdf_date` reads for
+/// the Info dictionary. Unrecognised input falls back to the deterministic default date.
+fn xmp_date(s: &str) -> String {
+ const FALLBACK: &str = "2026-01-01T00:00:00Z";
+ let s = s.trim();
+ let s = s.strip_prefix("D:").unwrap_or(s);
+ let bytes = s.as_bytes();
+ if bytes.len() >= 10 && bytes[4] == b'-' && bytes[7] == b'-' {
+ return match bytes.len() {
+ 10 => format!("{s}T00:00:00Z"),
+ 19 => format!("{}Z", s.replacen(' ', "T", 1)),
+ _ => s.replacen(' ', "T", 1),
+ };
+ }
+ if bytes.len() >= 14 && bytes[..14].iter().all(u8::is_ascii_digit) {
+ return format!(
+ "{}-{}-{}T{}:{}:{}Z",
+ &s[0..4],
+ &s[4..6],
+ &s[6..8],
+ &s[8..10],
+ &s[10..12],
+ &s[12..14]
+ );
+ }
+ FALLBACK.to_string()
+}
+
+/// `uuid:` URN built from the first 16 bytes of the file identifier.
+fn uuid_urn(id: &[u8]) -> String {
+ let hex: String = id.iter().take(16).map(|b| format!("{b:02x}")).collect();
+ format!(
+ "uuid:{}-{}-{}-{}-{}",
+ &hex[0..8],
+ &hex[8..12],
+ &hex[12..16],
+ &hex[16..20],
+ &hex[20..32]
+ )
+}
+
+/// XMP packet required by PDF/X-4 (ISO 15930-7): it repeats the Info dictionary values and adds
+/// the PDF/X version and the document identity.
+fn build_xmp(
+ title: &str,
+ author: &str,
+ created: &str,
+ modified: &str,
+ document_id: &str,
+) -> String {
+ format!(
+ r#"
+
+
+
+ application/pdf
+ {title}
+ {author}
+ {tool}
+ {created}
+ {modified}
+ {modified}
+ False
+ PDF/X-4
+ {document_id}
+ 1
+ default
+
+
+
+"#,
+ bom = '\u{feff}',
+ title = xml_escape(title),
+ author = xml_escape(author),
+ tool = xml_escape(CREATOR_TOOL),
+ )
+}
+
/// Parses an RFC 3339 / ISO 8601 or PDF formatted date string into a [`pdf_writer::Date`].
pub fn parse_pdf_date(s: &str) -> Option {
let s = s.trim();
@@ -470,7 +573,7 @@ impl PdfPrePressEngine {
let mut info = pdf.document_info(info_id);
info.title(TextStr(&options.title));
info.author(TextStr(&options.author));
- info.creator(TextStr("TypesetOK Native Pre-Press Engine 2026"));
+ info.creator(TextStr(CREATOR_TOOL));
info.trapped(TrappingStatus::NotTrapped);
if let Some(cdate) = parse_pdf_date(creation_str) {
@@ -570,6 +673,8 @@ impl PdfPrePressEngine {
// 7. Embedded ICC DestOutputProfile Stream (N = 4 CMYK components) & Catalog OutputIntent
let icc_profile_id = alloc();
+ let profile_info = options.color_profile.info();
+ let intent_info = format!("TypesetOK approximation of {profile_info}");
let icc_raw = ColorManagementEngine::generate_icc_profile_bytes(options.color_profile);
if options.compress_streams {
let compressed_icc = miniz_oxide::deflate::compress_to_vec_zlib(&icc_raw, 6);
@@ -583,26 +688,50 @@ impl PdfPrePressEngine {
icc_stream.finish();
}
+ // 8. Deterministic file identifier (required by PDF/X); also the XMP DocumentID.
+ id_hash = fnv1a(id_hash, options.title.as_bytes());
+ id_hash = fnv1a(id_hash, creation_str.as_bytes());
+ id_hash = fnv1a(id_hash, mod_str.as_bytes());
+ let file_id = [id_hash.to_be_bytes(), fnv1a(id_hash, b"tok").to_be_bytes()].concat();
+
+ // XMP metadata: required by PDF/X-4; PDF 1.3 (PDF/X-1a) has no /Metadata entry.
+ let xmp_id = if matches!(options.standard, PdfXStandard::PdfX4) {
+ let id = alloc();
+ let xmp = build_xmp(
+ &options.title,
+ &options.author,
+ &xmp_date(creation_str),
+ &xmp_date(mod_str),
+ &uuid_urn(&file_id),
+ );
+ let mut xmp_stream = pdf.stream(id, xmp.as_bytes());
+ xmp_stream.pair(Name(b"Type"), Name(b"Metadata"));
+ xmp_stream.pair(Name(b"Subtype"), Name(b"XML"));
+ xmp_stream.finish();
+ Some(id)
+ } else {
+ None
+ };
+
let mut catalog = pdf.catalog(catalog_id);
catalog.pages(pages_id);
+ if let Some(id) = xmp_id {
+ catalog.pair(Name(b"Metadata"), id);
+ }
let mut catalog_intents = catalog.output_intents();
let mut intent = catalog_intents.push();
intent.subtype(OutputIntentSubtype::PDFX);
- intent.output_condition_identifier(TextStr(
- options.color_profile.output_condition_identifier(),
- ));
- intent.info(TextStr(options.color_profile.info()));
- intent.registry_name(TextStr(options.color_profile.registry_name()));
+ // The embedded profile is a generic approximation, not the characterisation of the
+ // registered press condition it is modelled on. PDF/X allows a custom output condition
+ // when the profile is embedded, so declare one instead of a registered identifier.
+ intent.output_condition_identifier(TextStr("Custom"));
+ intent.info(TextStr(&intent_info));
intent.dest_output_profile(icc_profile_id);
intent.finish();
catalog_intents.finish();
catalog.finish();
- // 8. Deterministic file identifier (required by PDF/X).
- id_hash = fnv1a(id_hash, options.title.as_bytes());
- id_hash = fnv1a(id_hash, creation_str.as_bytes());
- id_hash = fnv1a(id_hash, mod_str.as_bytes());
- let file_id = [id_hash.to_be_bytes(), fnv1a(id_hash, b"tok").to_be_bytes()].concat();
+ // 9. File identifier (computed earlier, it is also the XMP DocumentID).
pdf.set_file_id((file_id.clone(), file_id));
Ok(pdf.finish())
@@ -1308,8 +1437,16 @@ mod tests {
"Must embed GRACoL 2013 OutputIntent info"
);
assert!(
- s.contains("CGATS TR 016"),
- "Must embed CGATS TR 016 OutputConditionIdentifier"
+ s.contains("(Custom)") && !s.contains("CGATS TR 016") && !s.contains("color.org"),
+ "The approximation profile must declare a custom, not a registered, condition"
+ );
+ assert!(
+ s.contains("/Metadata") && s.contains("PDF/X-4<"),
+ "PDF/X-4 must carry an XMP packet with the PDF/X version"
+ );
+ assert!(
+ s.contains("uuid:"),
+ "PDF/X-4 XMP must carry a DocumentID"
);
// Both separator line widths (0.5 w for primary apparatus, 0.35 w for secondary apparatus)
assert!(
@@ -1371,4 +1508,55 @@ mod tests {
"Must contain crop mark vector paths"
);
}
+
+ #[test]
+ fn xmp_helpers_escape_text_and_normalise_dates_and_ids() {
+ assert_eq!(
+ xml_escape("a & b \"d\" 'e'"),
+ "a & b <c> "d" 'e'"
+ );
+ assert_eq!(xml_escape("a\u{1}b"), "ab");
+ assert_eq!(xmp_date("2026-03-04"), "2026-03-04T00:00:00Z");
+ assert_eq!(xmp_date("2026-03-04T05:06:07Z"), "2026-03-04T05:06:07Z");
+ assert_eq!(xmp_date("2026-03-04 05:06:07"), "2026-03-04T05:06:07Z");
+ assert_eq!(
+ xmp_date("2026-03-04T05:06:07+02:00"),
+ "2026-03-04T05:06:07+02:00"
+ );
+ assert_eq!(xmp_date("D:20260304050607"), "2026-03-04T05:06:07Z");
+ assert_eq!(xmp_date("not a date"), "2026-01-01T00:00:00Z");
+ let id: Vec = (0u8..16).collect();
+ assert_eq!(uuid_urn(&id), "uuid:00010203-0405-0607-0809-0a0b0c0d0e0f");
+ }
+
+ #[test]
+ fn pdf_x4_xmp_matches_the_info_dictionary_and_escapes_the_title() {
+ let options = PdfExportOptions {
+ standard: PdfXStandard::PdfX4,
+ title: "A & B <ספר>".to_string(),
+ compress_streams: false,
+ creation_date: Some("2026-05-06T07:08:09Z".to_string()),
+ ..Default::default()
+ };
+ let bytes = PdfPrePressEngine::export_pdf(&[], &options).unwrap();
+ let s = String::from_utf8_lossy(&bytes);
+ let title_xml = "A & B <ספר>";
+ let created_xml = "2026-05-06T07:08:09Z";
+ assert!(s.contains(title_xml));
+ assert!(s.contains(created_xml));
+ assert!(s.contains("False"));
+ assert!(s.contains("/Subtype /XML"));
+ }
+
+ #[test]
+ fn pdf_x1a_has_no_xmp_metadata() {
+ let options = PdfExportOptions {
+ standard: PdfXStandard::PdfX1a2001,
+ compress_streams: false,
+ ..Default::default()
+ };
+ let bytes = PdfPrePressEngine::export_pdf(&[], &options).unwrap();
+ let s = String::from_utf8_lossy(&bytes);
+ assert!(!s.contains("xpacket") && !s.contains("/Metadata"));
+ }
}