diff --git a/Cargo.lock b/Cargo.lock index c4aee4617..3fbf55e99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2519,6 +2519,7 @@ dependencies = [ "craft-image-codecs", "craft-pdf", "craft-prepress", + "craft-xml", "designcraft-color", "designcraft-compose", "designcraft-doc", diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index e29e5340b..20beebe38 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -19,6 +19,7 @@ craft-pdf = { workspace = true, features = ["read", "write-pdf", "write-svg"] } craft-image-codecs = { workspace = true } craft-prepress = { workspace = true } craft-cms = { workspace = true } +craft-xml = { workspace = true } designcraft-images = { workspace = true } image = { workspace = true } thiserror = { workspace = true } diff --git a/crates/pdf/src/pdfx.rs b/crates/pdf/src/pdfx.rs index 0d2b2b28a..2e6a0a85c 100644 --- a/crates/pdf/src/pdfx.rs +++ b/crates/pdf/src/pdfx.rs @@ -158,13 +158,15 @@ pub fn check_pdfx4(pdf: &[u8]) -> Vec { if file.as_ref().and_then(|f| info_text(pdf, f, b"GTS_PDFXVersion")).as_deref() != Some("PDF/X-4") { issues.push("the document information doesn't name PDF/X-4".into()); } - if !text.contains("pdfxid:GTS_PDFXVersion>PDF/X-4<") { + let xmp = file.as_ref().and_then(xmp_metadata); + let property = |name: &str| xmp.as_ref().and_then(|x| xmp_property(x, name)); + if property("pdfxid:GTS_PDFXVersion").as_deref() != Some("PDF/X-4") { issues.push("the XMP metadata doesn't name PDF/X-4".into()); } - if !text.contains("") { + if property("xmp:MetadataDate").is_none() { issues.push("the XMP metadata has no xmp:MetadataDate".into()); } - if !text.contains("") { + if property("xmpMM:VersionID").is_none() { issues.push("the XMP metadata has no xmpMM:VersionID".into()); } if text.contains("/Interpolate true") { @@ -192,6 +194,28 @@ pub fn pdfx_version(pdf: &[u8]) -> Option { info_text(pdf, &craft_pdf::syntax::Pdf::new(pdf.to_vec()).ok()?, b"GTS_PDFXVersion") } +/// The XMP namespaces the PDF/X-4 check reads, by URI, with the prefixes it names them by. +const XMP_NAMESPACES: &[(&str, &str)] = + &[("pdfxid", "http://www.npes.org/pdfx/ns/id/"), ("xmp", "http://ns.adobe.com/xap/1.0/"), ("xmpMM", "http://ns.adobe.com/xap/1.0/mm/")]; + +/// The catalog's XMP metadata packet as XML, its names in [`XMP_NAMESPACES`] given those +/// prefixes whatever prefix the packet binds them to and wherever it declares them. +fn xmp_metadata(file: &craft_pdf::syntax::Pdf) -> Option { + let xref = file.xref(); + let catalog = xref.get::>(xref.root_id())?; + let data = catalog.get::>(b"Metadata")?.decoded().ok()?; + let names = craft_xml::Canonical::new(XMP_NAMESPACES); + craft_xml::Options::canonical(&names).parse(&data).ok() +} + +/// The value of the XMP property `name` (`prefix:local`, a prefix of [`XMP_NAMESPACES`]), +/// trimmed: written as an element, or as an attribute (the shorthand on `rdf:Description`). +fn xmp_property(xmp: &craft_xml::El, name: &str) -> Option { + std::iter::once(xmp) + .chain(xmp.descendants()) + .find_map(|el| if el.name == name { Some(el.text().trim().to_string()) } else { el.attr(name).map(|v| v.trim().to_string()) }) +} + /// The catalog's `GTS_PDFX` output intents, read as objects (whitespace and object streams /// don't matter). pub(crate) fn pdfx_intents<'a>(file: &'a craft_pdf::syntax::Pdf) -> Vec> { diff --git a/crates/pdf/src/pdfx/tests.rs b/crates/pdf/src/pdfx/tests.rs index 14246a479..5bea2ab06 100644 --- a/crates/pdf/src/pdfx/tests.rs +++ b/crates/pdf/src/pdfx/tests.rs @@ -666,6 +666,69 @@ fn x4_checks_ignore_whitespace_between_tokens() { assert!(issues.iter().any(|i| i == NO_INTENT) && issues.iter().any(|i| i == NO_INFO), "{issues:?}"); } +const NO_XMP_VERSION: &str = "the XMP metadata doesn't name PDF/X-4"; +const NO_XMP_DATE: &str = "the XMP metadata has no xmp:MetadataDate"; +const PDFXID: &str = "http://www.npes.org/pdfx/ns/id/"; +const XMP: &str = "http://ns.adobe.com/xap/1.0/"; + +/// The PDF/X-4 issues of a file whose catalog metadata is an XMP packet holding `description`. +fn xmp_issues(description: &str) -> Vec { + let packet = format!( + "\ + {description}" + ); + let metadata = format!("<>\nstream\n{packet}\nendstream", packet.len()); + check_pdfx4(&assemble_x4( + &["<>", "<>", &metadata], + "<>", + )) +} + +/// The XMP's PDF/X version and metadata date are found by namespace URI (#3144): declared on the +/// property itself, bound to another prefix, or written as attributes of `rdf:Description`. +#[test] +fn xmp_properties_are_found_by_namespace_whatever_the_prefix_or_form() { + let date = "2026-01-02T03:04:05Z"; + let packets = [ + format!( + "PDF/X-4\ + {date}" + ), + format!( + "\ + PDF/X-4{date}" + ), + format!( + "" + ), + ]; + for packet in packets { + let issues = xmp_issues(&packet); + assert!(!issues.iter().any(|i| i == NO_XMP_VERSION || i == NO_XMP_DATE), "{packet}: {issues:?}"); + } +} + +/// A packet that names another PDF/X version, lacks the properties, or uses their prefixes for +/// other namespaces still fails. +#[test] +fn xmp_without_a_pdfx4_version_or_metadata_date_fails() { + let packets = [ + format!( + "PDF/X-1a:2003" + ), + "application/pdf" + .to_string(), + "\ + PDF/X-42026-01-02T03:04:05Z" + .to_string(), + ]; + for packet in packets { + let issues = xmp_issues(&packet); + assert!(issues.iter().any(|i| i == NO_XMP_VERSION) && issues.iter().any(|i| i == NO_XMP_DATE), "{packet}: {issues:?}"); + } +} + /// The output intent and the document information are found inside a compressed object stream, /// through a cross-reference stream. #[test]