diff --git a/Cargo.lock b/Cargo.lock index f69d62d492..5db9d55ce0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1320,7 +1320,7 @@ dependencies = [ [[package]] name = "craft-affinity" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "crc32fast", "flate2", @@ -1331,7 +1331,7 @@ dependencies = [ [[package]] name = "craft-atomic" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "rustix 1.1.5", ] @@ -1339,7 +1339,7 @@ dependencies = [ [[package]] name = "craft-crw" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "thiserror 2.0.21", ] @@ -1347,17 +1347,17 @@ dependencies = [ [[package]] name = "craft-docpath" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" [[package]] name = "craft-file-formats" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "craft-eps", "craft-idml", @@ -1375,12 +1375,12 @@ dependencies = [ [[package]] name = "craft-fonts-build" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" [[package]] name = "craft-heif" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "heic-rs", "rayon", @@ -1389,7 +1389,7 @@ dependencies = [ [[package]] name = "craft-i18n" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "fixed_decimal", "icu_decimal", @@ -1412,7 +1412,7 @@ dependencies = [ [[package]] name = "craft-idml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "craft-xml", "craft-zip", @@ -1422,7 +1422,7 @@ dependencies = [ [[package]] name = "craft-image-codecs" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "craft-heif", "craft-jpeg", @@ -1445,7 +1445,7 @@ dependencies = [ [[package]] name = "craft-jpeg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "thiserror 2.0.21", ] @@ -1453,7 +1453,7 @@ dependencies = [ [[package]] name = "craft-kra" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "craft-xml", "craft-zip", @@ -1463,7 +1463,7 @@ dependencies = [ [[package]] name = "craft-metadata" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "brotli-decompressor 6.3.1", "chrono", @@ -1475,7 +1475,7 @@ dependencies = [ [[package]] name = "craft-ora" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "craft-xml", "craft-zip", @@ -1485,7 +1485,7 @@ dependencies = [ [[package]] name = "craft-pcraft-bundle" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "blake3", "craft-atomic", @@ -1498,7 +1498,7 @@ dependencies = [ [[package]] name = "craft-pcraft-format" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "serde", "serde_json", @@ -1508,9 +1508,10 @@ dependencies = [ [[package]] name = "craft-pdf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "bytemuck", + "flate2", "hayro", "hayro-interpret", "hayro-syntax", @@ -1520,7 +1521,7 @@ dependencies = [ [[package]] name = "craft-pdn" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "flate2", "thiserror 2.0.21", @@ -1529,7 +1530,7 @@ dependencies = [ [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "craft-winprint-gdi", "log", @@ -1543,7 +1544,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "encoding_rs", "flate2", @@ -1554,7 +1555,7 @@ dependencies = [ [[package]] name = "craft-raw" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "chrono", "craft-crw", @@ -1572,7 +1573,7 @@ dependencies = [ [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "craft-image-codecs", "craft-xml", @@ -1587,7 +1588,7 @@ dependencies = [ [[package]] name = "craft-tiff" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "serde", "thiserror 2.0.21", @@ -1596,7 +1597,7 @@ dependencies = [ [[package]] name = "craft-ui" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "craft-fonts-build", "craft-i18n", @@ -1617,7 +1618,7 @@ dependencies = [ [[package]] name = "craft-update" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "chrono", "dunce", @@ -1652,7 +1653,7 @@ dependencies = [ [[package]] name = "craft-xcf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "flate2", "thiserror 2.0.21", @@ -1661,7 +1662,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1670,7 +1671,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "flate2", "thiserror 2.0.21", @@ -3076,7 +3077,7 @@ dependencies = [ [[package]] name = "hayro" version = "0.7.1" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "bytemuck", "hayro-interpret", @@ -3106,7 +3107,7 @@ dependencies = [ [[package]] name = "hayro-interpret" version = "0.7.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "bitflags 2.13.2", "hayro-cmap", @@ -3124,7 +3125,7 @@ dependencies = [ [[package]] name = "hayro-jbig2" version = "0.3.0" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "fearless_simd", "hayro-ccitt", @@ -3148,7 +3149,7 @@ checksum = "bc2d80da85f31c3fdc0366431f44cfd645a9da1e242ed1f4a61f213d9765660f" [[package]] name = "hayro-syntax" version = "0.7.2" -source = "git+https://github.com/storytold/craft-libs?rev=365bf47db#365bf47db64dc2ab88e7649548e9ab64b6644eda" +source = "git+https://github.com/storytold/craft-libs?rev=c0622649f#c0622649ff239b618b999162456fd8b37aa1918c" dependencies = [ "flate2", "hayro-ccitt", diff --git a/Cargo.toml b/Cargo.toml index 12886078d5..1e9ec1a9e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0" rust-version = "1.95" [workspace.dependencies] -craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db", features = ["serde"] } +craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f", features = ["serde"] } # internal photocraft-geom = { path = "crates/geom" } photocraft-color = { path = "crates/color" } @@ -18,11 +18,11 @@ photocraft-cms = { path = "crates/cms" } photocraft-raster = { path = "crates/raster" } photocraft-doc = { path = "crates/doc" } photocraft-ops = { path = "crates/ops" } -photocraft-psd = { package = "craft-psd", git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -photocraft-codecs = { package = "craft-image-codecs", git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -photocraft-formats = { package = "craft-file-formats", git = "https://github.com/storytold/craft-libs", rev = "365bf47db", features = ["raster-codecs", "pdf", "svg", "ora", "kra", "xcf", "pdn", "raw", "psd", "affinity", "pcraft"] } +photocraft-psd = { package = "craft-psd", git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +photocraft-codecs = { package = "craft-image-codecs", git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +photocraft-formats = { package = "craft-file-formats", git = "https://github.com/storytold/craft-libs", rev = "c0622649f", features = ["raster-codecs", "pdf", "svg", "ora", "kra", "xcf", "pdn", "raw", "psd", "affinity", "pcraft"] } photocraft-raw = { path = "crates/raw" } -photocraft-affinity = { package = "craft-affinity", git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } +photocraft-affinity = { package = "craft-affinity", git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } photocraft-compose = { path = "crates/compose" } photocraft-gpu = { path = "crates/gpu" } photocraft-paint = { path = "crates/paint" } @@ -40,27 +40,27 @@ photocraft-tablet = { path = "crates/tablet" } photocraft-ipp = { path = "crates/ipp" } # external -craft-print = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } +craft-print = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } # HEIC/HEIF: the shared decoder (replaces crates/heif; craft-libs#784). -craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db", features = ["raster"] } -craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-pcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-pcraft-bundle = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-ora = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-kra = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-xcf = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-pdn = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db", features = ["tree-render", "tree-render-system-fonts"] } -craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } +craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f", features = ["raster", "write-raster"] } +craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-pcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-pcraft-bundle = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-ora = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-kra = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-xcf = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-pdn = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f", features = ["tree-render", "tree-render-system-fonts"] } +craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } # Interface translations: lookup, fallback, CLDR plurals and language names over the craft-i18n # catalogs that `crates/ui-egui/build.rs` embeds from `CRAFT_I18N_DIR` (storytold/craft-i18n, at # the commit in `craft-i18n.rev`). Already in the tree through craft-ui. -craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } +craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } # craft-update: app-specific pretend versions apply to startup and manual checks. -craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "365bf47db", features = ["egui"] } +craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "c0622649f", features = ["egui"] } thiserror = "2" serde = { version = "1", features = ["derive", "rc"] } serde_json = "1" @@ -220,7 +220,7 @@ egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6d518fc7 egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } # craft-pdf's vendored Hayro trees: consumers patch the same craft-libs revision (crates/pdf README). -hayro = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } -hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "365bf47db" } +hayro = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } +hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "c0622649f" } diff --git a/crates/io/Cargo.toml b/crates/io/Cargo.toml index 3f1c9948df..d24f3b5b5e 100644 --- a/crates/io/Cargo.toml +++ b/crates/io/Cargo.toml @@ -37,6 +37,7 @@ photocraft-text = { workspace = true } photocraft-paint = { workspace = true } photocraft-vector = { workspace = true } photocraft-format = { path = "../format" } +craft-pdf = { workspace = true } craft-docpath = { workspace = true } craft-ora = { workspace = true } craft-kra = { workspace = true } diff --git a/crates/io/src/pdf.rs b/crates/io/src/pdf.rs index 79631ab9c6..f2ce34edb9 100644 --- a/crates/io/src/pdf.rs +++ b/crates/io/src/pdf.rs @@ -1,387 +1,34 @@ -//! The PDF writer (ISO 32000-1 basics): one raster page per file. +//! PDF export model mapping for PhotoCraft. //! -//! A small, self-contained writer used by two paths: File › Save a Copy / Save As › PDF (#3401) -//! through [`export`], and File › Print through [`print_pdf`] (called by the engine, which adds -//! paper geometry, printer marks and labels around the image). One page holds a -//! Flate-compressed image XObject, optional printer marks as vector operators and optional -//! Helvetica text; an embedded ICC profile rides in an ICCBased colour space. Multi-page PDF, -//! layer-preserving PDF and PDF import stay open (FILE-215-10). -//! -//! Spot channels leave as plates (FILE-215-17): one 8-bit image XObject per channel in a -//! `/Separation` colour space named after the channel, painted over the composite with overprint -//! on, so a RIP or a separating rasteriser (Ghostscript `tiffsep`, `psdcmyk`) keeps each one as -//! its own named plate instead of mixing it into CMYK. The plates carry no mask: a viewer that -//! ignores overprint shows the last plate opaque, Acrobat's Overprint Preview and any separating -//! RIP show the real result, and an explicit image mask makes Illustrator report "An unknown -//! imaging construct was encountered". An artboard page carries its own region of every plate. - -use std::io::Write as _; +//! This module projects document rasters, vectors, spot channels and print policy into the +//! neutral page records in craft-pdf. PDF object layout and serialization live in craft-pdf. use photocraft_color::{ColorMode, PixelFormat}; use photocraft_doc::Document; use crate::{ExportResult, IoError}; -mod selection; mod shapes; mod text; -/// The image placed on a page: 8-bit interleaved samples, 1 (gray), 3 (RGB) or 4 (CMYK, -/// 1 = full ink), with the profile it was authored in if any. -pub struct PrintImage { - pub width: u32, - pub height: u32, - pub channels: usize, - pub data: Vec, - pub icc: Option>, -} - -/// One spot plate: a named `/Separation` colorant with 8-bit ink density over the whole image -/// box (`width × height` samples, 255 = solid ink), and the CMYK a viewer mixes to show it. -pub struct SpotPlate { - /// The colorant name, as the channel is named. A RIP names the plate after it. - pub name: String, - /// The alternate colour (`/DeviceCMYK`, 1 = full ink) of the tint transform: how a viewer - /// previews the ink. A RIP prints the plate, not this. - pub cmyk: [f32; 4], - /// The ink colour as CIE L*a*b* (D50) and as encoded sRGB, and the solidity (0..1), for the - /// XMP description a converter reads to rebuild the spot channel (see [`plates_xmp`]). - pub lab: [f32; 3], - pub srgb: [f32; 3], - pub solidity: f32, - pub width: u32, - pub height: u32, - pub data: Vec, -} - -/// Printer marks around the image. -#[derive(Clone, Copy, Debug, Default, PartialEq)] -pub struct Marks { - pub corner_crop: bool, - pub center_crop: bool, - pub registration: bool, -} - -/// One page: paper (pt), image box (pt, origin bottom-left), marks, texts and spot plates. -/// -/// The image is optional: a Multichannel document is nothing but its plates. The plates are -/// painted over the image box in order, each with overprint on, so painting one never knocks -/// the others (or the image) out. -pub struct PrintPage { - pub paper: (f64, f64), - pub image: Option, - pub rect: (f64, f64, f64, f64), - pub marks: Marks, - pub description: Option, - pub label: Option, - /// The page's name in the PDF `/PageLabels` tree (Unicode-capable; not drawn on the page). - pub page_label: Option, - pub plates: Vec, -} - -pub(super) fn pdf_string(s: &str) -> String { - let mut o = String::from("("); - for c in s.chars() { - match c { - '(' | ')' | '\\' => { - o.push('\\'); - o.push(c); - } - c if c.is_ascii() && !c.is_ascii_control() => o.push(c), - _ => o.push('?'), - } - } - o.push(')'); - o -} - -/// A PDF text string with UTF-16BE hex (the Unicode-capable form, byte-order mark included). -fn pdf_unicode_string(s: &str) -> String { - let mut o = String::from("'); - o -} - -/// `s` as a PDF name object (ISO 32000-1 §7.3.5): `/` then the UTF-8 bytes, every byte outside -/// the regular printable ASCII range, `#` and the delimiters as `#xx`. An empty name would be -/// the bare `/`, which some readers reject, so it becomes `/Spot`. -fn pdf_name(s: &str) -> String { - let mut o = String::from("/"); - for b in s.bytes() { - let regular = (0x21..=0x7e).contains(&b) && !matches!(b, b'#' | b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%'); - if regular { - o.push(char::from(b)); - } else { - o.push_str(&format!("#{b:02X}")); - } - } - if o.len() == 1 { - o.push_str("Spot"); - } - o -} - -fn deflate(data: &[u8]) -> Vec { - // Fast: print PDFs are transient and large. Level 3, not 1: zlib-rs level 1 writes static - // Huffman blocks only (13-30% bigger on photos); level 3 is the lowest that is never bigger - // than miniz's level 1 was, at about twice its time. - let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::new(3)); - let _ = e.write_all(data); - e.finish().unwrap_or_default() -} - -/// Vector marks around `(x, y, w, h)`. -fn marks_ops(m: Marks, (x, y, w, h): (f64, f64, f64, f64)) -> String { - let mut s = String::from("q 0 G 0.3 w\n"); - let (gap, len) = (6.0, 18.0); - let line = |s: &mut String, x0: f64, y0: f64, x1: f64, y1: f64| s.push_str(&format!("{x0:.2} {y0:.2} m {x1:.2} {y1:.2} l S\n")); - if m.corner_crop { - for (cx, cy, dx, dy) in [(x, y, -1.0, -1.0), (x + w, y, 1.0, -1.0), (x, y + h, -1.0, 1.0), (x + w, y + h, 1.0, 1.0)] { - line(&mut s, cx + dx * gap, cy, cx + dx * (gap + len), cy); - line(&mut s, cx, cy + dy * gap, cx, cy + dy * (gap + len)); - } - } - if m.center_crop { - let (mx, my) = (x + w / 2.0, y + h / 2.0); - line(&mut s, mx, y - gap, mx, y - gap - len); - line(&mut s, mx, y + h + gap, mx, y + h + gap + len); - line(&mut s, x - gap, my, x - gap - len, my); - line(&mut s, x + w + gap, my, x + w + gap + len, my); - } - if m.registration { - let r = 5.0; - let off = gap + len / 2.0; - for (cx, cy) in [(x + w / 2.0, y - off), (x + w / 2.0, y + h + off), (x - off, y + h / 2.0), (x + w + off, y + h / 2.0)] { - // Circle (four Béziers) plus crosshair. - let k = 0.5523 * r; - s.push_str(&format!( - "{:.2} {cy:.2} m {:.2} {:.2} {:.2} {:.2} {cx:.2} {:.2} c {:.2} {:.2} {:.2} {:.2} {:.2} {cy:.2} c {:.2} {:.2} {:.2} {:.2} {cx:.2} {:.2} c {:.2} {:.2} {:.2} {:.2} {:.2} {cy:.2} c S\n", - cx + r, cx + r, cy + k, cx + k, cy + r, cy + r, cx - k, cy + r, cx - r, cy + k, cx - r, cx - r, cy - k, cx - k, cy - r, cy - r, cx + k, cy - r, cx + r, cy - k, cx + r - )); - line(&mut s, cx - r * 1.6, cy, cx + r * 1.6, cy); - line(&mut s, cx, cy - r * 1.6, cx, cy + r * 1.6); - } - } - s.push_str("Q\n"); - s -} - -/// An image XObject dictionary and its Flate-compressed samples, as one object body. -fn image_object(width: u32, height: u32, colour_space: &str, data: &[u8]) -> Vec { - let z = deflate(data); - let mut body = format!( - "<< /Type /XObject /Subtype /Image /Width {width} /Height {height} /ColorSpace {colour_space} /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>\nstream\n", - z.len() - ) - .into_bytes(); - body.extend_from_slice(&z); - body.extend_from_slice(b"\nendstream"); - body -} +/// Neutral page records and PDF syntax are owned by craft-pdf; this module only maps the +/// PhotoCraft document to those records and supplies its product-level cancellation policy. +pub use craft_pdf::write_raster::{Image as PrintImage, Marks, Page as PrintPage, SelectionFont, SpotPlate}; /// A one-page PDF. pub fn print_pdf(page: &PrintPage) -> Vec { - print_pdf_pages(std::slice::from_ref(page), &[String::new()], &selection::SelectionFont::default(), None, &photocraft_raster::Interrupt::NONE) - .unwrap_or_default() + craft_pdf::write_raster::write_pages(std::slice::from_ref(page), &[String::new()], &SelectionFont::default(), None) } -/// A PDF with one page per entry; `vectors[i]` (when present) is drawn over page `i`, after its -/// image and before its plates and marks (the top layers kept as paths and selectable text), and -/// the selection font their text refers to is `/FSelect`. `xmp` is the packet of the catalog's -/// `/Metadata` stream, if any. -/// -/// Objects: 1 catalog, 2 pages, then per page its page, contents, image and one XObject per -/// plate; then the shared font, ICC profile, overprint ExtGState and XMP metadata stream, each -/// only when present, the selection font's three objects, and last the page-label tree. fn print_pdf_pages( pages: &[PrintPage], vectors: &[String], - selection_font: &selection::SelectionFont, + selection_font: &SelectionFont, xmp: Option<&str>, ctl: &photocraft_raster::Interrupt, ) -> std::result::Result, IoError> { - if ctl.check().is_err() { - return Err(IoError::Cancelled); - } - if pages.is_empty() { - return Ok(Vec::new()); - } - let mut out: Vec = b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n".to_vec(); - let mut offsets: Vec = Vec::new(); - let mut obj = |out: &mut Vec, body: &[u8]| { - offsets.push(out.len()); - out.extend_from_slice(format!("{} 0 obj\n", offsets.len()).as_bytes()); - out.extend_from_slice(body); - out.extend_from_slice(b"\nendobj\n"); - }; - let n = pages.len(); - // A label tree is written only for a non-empty name: an all-empty tree would be `/Nums []`, - // and filtering the first empty name away would start the tree at page index 1, not 0. - let labels_on = pages.iter().any(|p| p.page_label.as_deref().is_some_and(|label| !label.is_empty())); - // Object numbers, handed out in the order the objects are written. - let mut next = 3usize; - let mut take = |present: bool| { - let o = present.then_some(next); - next += usize::from(present); - o - }; - struct PageObjects { - page: usize, - contents: usize, - image: Option, - plates: Vec, - } - // Field initialisers run in this order. - let page_objects: Vec = pages - .iter() - .map(|p| PageObjects { - page: take(true).unwrap_or_default(), - contents: take(true).unwrap_or_default(), - image: take(p.image.is_some()), - plates: p.plates.iter().filter_map(|_| take(true)).collect(), - }) - .collect(); - let font_obj = take(true).unwrap_or_default(); - // The profile is the document's, shared by every page's image. - let icc = pages.iter().find_map(|p| p.image.as_ref().and_then(|i| i.icc.as_ref().map(|icc| (icc, i.channels)))); - let icc_obj = take(icc.is_some()); - let gs_obj = take(pages.iter().any(|p| !p.plates.is_empty())); - let xmp_obj = take(xmp.is_some()); - let selection_base = next; - let selection_objects = selection_font.objects(selection_base); - let labels_obj = labels_on.then_some(selection_base + selection_objects.len()); - - let metadata = xmp_obj.map(|o| format!(" /Metadata {o} 0 R")).unwrap_or_default(); - let page_labels = labels_obj.map(|o| format!(" /PageLabels {o} 0 R")).unwrap_or_default(); - obj(&mut out, format!("<< /Type /Catalog /Pages 2 0 R{metadata}{page_labels} >>").as_bytes()); - let kids = page_objects.iter().map(|p| format!("{} 0 R", p.page)).collect::>().join(" "); - obj(&mut out, format!("<< /Type /Pages /Kids [{kids}] /Count {n} >>").as_bytes()); - for (i, (page, objects)) in pages.iter().zip(&page_objects).enumerate() { - let (pw, ph) = page.paper; - let (x, y, w, h) = page.rect; - let mut xobjects = String::new(); - if let Some(o) = objects.image { - xobjects.push_str(&format!("/Im0 {o} 0 R ")); - } - for (k, o) in objects.plates.iter().enumerate() { - xobjects.push_str(&format!("/Sp{k} {o} 0 R ")); - } - let ext_gstate = match gs_obj { - Some(o) if !page.plates.is_empty() => format!("/ExtGState << /GS0 {o} 0 R >> "), - _ => String::new(), - }; - obj( - &mut out, - format!( - "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {pw:.2} {ph:.2}] /Resources << /XObject << {xobjects}>> {ext_gstate}/Font << /F1 {font_obj} 0 R /FSelect {selection_base} 0 R >> >> /Contents {} 0 R >>", - objects.contents - ) - .as_bytes(), - ); - // An explicit white page, so every viewer (and rasteriser) shows paper, not transparency. - let mut content = format!("q 1 g 0 0 {pw:.2} {ph:.2} re f Q\n"); - if objects.image.is_some() { - content.push_str(&format!("q {w:.3} 0 0 {h:.3} {x:.3} {y:.3} cm /Im0 Do Q\n")); - } - if let Some(v) = vectors.get(i) { - content.push_str(v); - } - // Each plate overprints what is already on the page: with `/OP true` a Separation paints - // its own colorant only, leaving the others (and the composite) as they are. - for k in 0..page.plates.len() { - content.push_str(&format!("q /GS0 gs {w:.3} 0 0 {h:.3} {x:.3} {y:.3} cm /Sp{k} Do Q\n")); - } - content.push_str(&marks_ops(page.marks, page.rect)); - let any_marks = page.marks.corner_crop || page.marks.center_crop || page.marks.registration; - let pad = if any_marks { 30.0 } else { 6.0 }; - if let Some(l) = page.label.as_deref().filter(|l| !l.is_empty()) { - content.push_str(&format!("BT /F1 8 Tf 0 g {:.2} {:.2} Td {} Tj ET\n", x, y + h + pad, pdf_string(l))); - } - if let Some(d) = page.description.as_deref().filter(|d| !d.is_empty()) { - content.push_str(&format!("BT /F1 8 Tf 0 g {:.2} {:.2} Td {} Tj ET\n", x, y - pad - 8.0, pdf_string(d))); - } - let c = deflate(content.as_bytes()); - let mut body = format!("<< /Length {} /Filter /FlateDecode >>\nstream\n", c.len()).into_bytes(); - body.extend_from_slice(&c); - body.extend_from_slice(b"\nendstream"); - obj(&mut out, &body); - if let Some(img) = &page.image { - let device = match img.channels { - 1 => "/DeviceGray", - 4 => "/DeviceCMYK", - _ => "/DeviceRGB", - }; - let cs = match icc_obj { - Some(o) if img.icc.is_some() => format!("[/ICCBased {o} 0 R]"), - _ => device.to_string(), - }; - obj(&mut out, &image_object(img.width, img.height, &cs, &img.data)); - } - for p in &page.plates { - // A tint transform from the colorant's density to its alternate: 0 → paper, 1 → `cmyk` - // (an exponential interpolation function with N = 1 is linear). - let cmyk = p.cmyk.map(|v| format!("{:.4}", v.clamp(0.0, 1.0))).join(" "); - let cs = format!("[/Separation {} /DeviceCMYK << /FunctionType 2 /Domain [0 1] /C0 [0 0 0 0] /C1 [{cmyk}] /N 1 >>]", pdf_name(&p.name)); - // No mask on the plate: where it has no ink it paints "0" of its own colorant, which a - // separating RIP reads as no ink and overprint keeps the other plates under. (A stencil - // or colour-key mask would let plain viewers show every plate, but Illustrator reports - // an explicit image mask as an unknown imaging construct, and Quartz draws nothing for a - // colour-key-masked Separation image.) - obj(&mut out, &image_object(p.width, p.height, &cs, &p.data)); - } - } - obj(&mut out, b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>"); - if let Some((icc, channels)) = icc { - let device = match channels { - 1 => "/DeviceGray", - 4 => "/DeviceCMYK", - _ => "/DeviceRGB", - }; - let z = deflate(icc); - let mut body = format!("<< /N {channels} /Alternate {device} /Filter /FlateDecode /Length {} >>\nstream\n", z.len()).into_bytes(); - body.extend_from_slice(&z); - body.extend_from_slice(b"\nendstream"); - obj(&mut out, &body); - } - if gs_obj.is_some() { - obj(&mut out, b"<< /Type /ExtGState /OP true /op true /OPM 1 >>"); - } - if let Some(xmp) = xmp { - // Unfiltered, as the XMP specification asks of a packet in a PDF, so a reader can find it - // by scanning for the packet header. - let mut body = format!("<< /Type /Metadata /Subtype /XML /Length {} >>\nstream\n", xmp.len()).into_bytes(); - body.extend_from_slice(xmp.as_bytes()); - body.extend_from_slice(b"\nendstream"); - obj(&mut out, &body); - } - for body in &selection_objects { - obj(&mut out, body); - } - if labels_obj.is_some() { - // Every page gets an entry: a number-tree range runs until the next key, so omitting an - // empty name would make that page inherit the previous page's label. An empty - // dictionary gives the page the default (decimal) label instead. - let nums = pages - .iter() - .enumerate() - .map(|(i, p)| match p.page_label.as_deref().filter(|label| !label.is_empty()) { - Some(label) => format!("{i} << /P {} >>", pdf_unicode_string(label)), - None => format!("{i} << >>"), - }) - .collect::>() - .join(" "); - obj(&mut out, format!("<< /Nums [{nums}] >>").as_bytes()); - } - let xref = out.len(); - out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", offsets.len() + 1).as_bytes()); - for o in &offsets { - out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes()); - } - out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", offsets.len() + 1).as_bytes()); - Ok(out) + ctl.check().map_err(|_| IoError::Cancelled)?; + Ok(craft_pdf::write_raster::write_pages(pages, vectors, selection_font, xmp)) } /// `s` as XML character data. @@ -554,7 +201,7 @@ fn plates_of(doc: &Document, rect: photocraft_geom::Rect) -> Vec { /// indices of the layers they replace, and the selection font covering their text (plus, when a /// lower candidate is rejected during emission, that layer's unused characters). /// Hidden and blocked layers contribute no characters, so they cannot exhaust the font's codes. -fn collect_vectors(doc: &Document) -> (Vec<(usize, String)>, selection::SelectionFont) { +fn collect_vectors(doc: &Document) -> (Vec<(usize, String)>, SelectionFont) { enum Candidate<'a> { Ops(String), Text(text::PreparedText<'a>), @@ -603,7 +250,7 @@ fn collect_vectors(doc: &Document) -> (Vec<(usize, String)>, selection::Selectio candidates.push((index, candidate)); } } - let selection_font = selection::SelectionFont::new(candidates.iter().filter_map(|(_, candidate)| match candidate { + let selection_font = SelectionFont::new(candidates.iter().filter_map(|(_, candidate)| match candidate { Candidate::Text(prepared) => Some(prepared.text()), Candidate::Ops(_) => None, })); @@ -748,14 +395,14 @@ pub fn export_with(doc: &Document, spot_plates: bool, doc_xmp: Option<&str>, ctl } warnings.push(format!("PDF exported {} artboards as {} pages; vector/text layers on artboards are rasterised", artboards.len(), pages.len())); let vectors = vec![String::new(); pages.len()]; - (pages, vectors, selection::SelectionFont::default()) + (pages, vectors, SelectionFont::default()) } else { let canvas = photocraft_geom::Rect::new(0, 0, w as i32, h as i32); // Simple topmost shapes and text layers can be drawn over the flattened backdrop without // changing compositing. Stop at the first unsupported visible layer, since it may depend // on the backdrop (adjustments, blend modes, masks, effects, or clipping). A Multichannel // document has no composite for them to sit on: it is its plates alone. - let (vector_layers, selection_font) = if device.is_some() { collect_vectors(doc) } else { (Vec::new(), selection::SelectionFont::default()) }; + let (vector_layers, selection_font) = if device.is_some() { collect_vectors(doc) } else { (Vec::new(), SelectionFont::default()) }; let image = device.map(|device| { let mut backdrop = doc.clone(); for (index, _) in &vector_layers { diff --git a/crates/io/src/pdf/tests.rs b/crates/io/src/pdf/tests.rs index af857f22f5..86d1472005 100644 --- a/crates/io/src/pdf/tests.rs +++ b/crates/io/src/pdf/tests.rs @@ -613,7 +613,7 @@ fn a_text_layer_refused_at_emission_keeps_the_upper_vectors_and_stays_raster() { // Pin the fixture to the intended path: accepted by `prepare`, refused by `ops`. let LayerContent::Text(lower_text) = &lower.content else { panic!("the fixture is a text layer") }; let prepared = super::text::prepare(lower_text, ColorMode::Rgb, d.resolution_dpi).expect("prepare accepts the truncated box"); - let lower_font = super::selection::SelectionFont::new(std::iter::once(lower_text.text.as_str())); + let lower_font = super::SelectionFont::new(std::iter::once(lower_text.text.as_str())); assert!(super::text::ops(&prepared, ColorMode::Rgb, &lower_font).is_none(), "emission must refuse the unrepresented source text"); d.layers.push(lower); d.layers.push(Layer::new( diff --git a/crates/io/src/pdf/text.rs b/crates/io/src/pdf/text.rs index cb2e9ac7dc..dfd5862128 100644 --- a/crates/io/src/pdf/text.rs +++ b/crates/io/src/pdf/text.rs @@ -59,7 +59,7 @@ fn straight_horizontal(layer: &TextLayer, dpi: f32) -> bool { #[cfg(test)] fn text_ops(layer: &TextLayer, mode: ColorMode, dpi: f32) -> Option { let prepared = prepare(layer, mode, dpi)?; - let font = super::selection::SelectionFont::new(std::iter::once(layer.text.as_str())); + let font = super::SelectionFont::new(std::iter::once(layer.text.as_str())); ops(&prepared, mode, &font) } @@ -146,7 +146,7 @@ fn layout_checks(layer: &TextLayer, mode: ColorMode, layout: &photocraft_text::T true } -pub(super) fn ops(prepared: &PreparedText<'_>, mode: ColorMode, selection_font: &super::selection::SelectionFont) -> Option { +pub(super) fn ops(prepared: &PreparedText<'_>, mode: ColorMode, selection_font: &super::SelectionFont) -> Option { let channels = channels(mode)?; layout_ops(prepared.layer, mode, channels, &prepared.layout, selection_font) } @@ -206,7 +206,7 @@ fn layout_ops( mode: ColorMode, channels: usize, layout: &photocraft_text::TextLayout, - selection_font: &super::selection::SelectionFont, + selection_font: &super::SelectionFont, ) -> Option { if !layout_checks(layer, mode, layout) { return None; @@ -267,7 +267,7 @@ fn layout_ops( /// cluster, so paragraph breaks and other whitespace omitted by shaping survive /// in the selection; the first span also takes leading characters. Without this a multi-line /// layer extracts as one run of text. -fn selection_ops(out: &mut String, layer: &TextLayer, layout: &photocraft_text::TextLayout, selection_font: &super::selection::SelectionFont) -> Option<()> { +fn selection_ops(out: &mut String, layer: &TextLayer, layout: &photocraft_text::TextLayout, selection_font: &super::SelectionFont) -> Option<()> { let [a, b, c, d, _, _] = layer.transform.m; let mut current_line = None; let mut clusters: Vec<_> = layout.clusters.iter().collect(); @@ -347,7 +347,7 @@ mod tests { face.font.data = restricted.clone().into(); face.font.index = 0; } - let font = super::super::selection::SelectionFont::new(std::iter::once(layer.text.as_str())); + let font = super::super::SelectionFont::new(std::iter::once(layer.text.as_str())); assert!(layout_ops(&layer, ColorMode::Rgb, 3, &layout, &font).is_none(), "restricted text must remain raster"); } assert!(!outlines_permitted(&[], 0)); @@ -463,7 +463,7 @@ mod tests { let line = layout.lines.get(cluster.line).expect("a line"); let origin = layer.transform.apply(photocraft_geom::Point::new(f64::from(cluster.x), f64::from(line.baseline))); assert!(origin.x.is_finite() && origin.y.is_finite(), "the fixture's origin stays finite"); - let font = super::super::selection::SelectionFont::new(std::iter::once(layer.text.as_str())); + let font = super::super::SelectionFont::new(std::iter::once(layer.text.as_str())); assert!(layout_ops(&layer, ColorMode::Rgb, 3, &layout, &font).is_none(), "overflowing Tm coefficients must not be emitted"); assert!(text_ops(&layer, ColorMode::Rgb, 72.0).is_none()); }