Cut over remaining codecs, xfa XML, and export packaging to craft-libs (#7326)

* chore: cut over image, png, tiff, and zune-jpeg to craft-image-codecs

* chore: cut over xfa XML parsing from roxmltree to craft-xml

* chore: cut over export DOCX and PPTX packaging to craft-opc and craft-xml

* style: cargo fmt
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-14 13:49:46 -05:00
1 parent 346d803080
commit eead454105
70 files changed
+2451 -1689

No files matched your search

Generated
+187 -27
View File
@@ -238,7 +238,7 @@ dependencies = [
"objc2-foundation 0.3.2",
"parking_lot",
"percent-encoding",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
"wl-clipboard-rs",
"x11rb",
]
@@ -532,6 +532,12 @@ dependencies = [
"serde",
]
[[package]]
name = "bit_field"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1"
[[package]]
name = "bitflags"
version = "1.3.2"
@@ -1239,6 +1245,36 @@ dependencies = [
"writeable",
]
[[package]]
name = "craft-image-codecs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
dependencies = [
"craft-jpeg",
"exr",
"flate2",
"half",
"image",
"image-webp",
"jpeg-encoder",
"png 0.18.1",
"rayon",
"serde_json",
"thiserror 2.0.21",
"tiff",
"zune-core",
"zune-jpeg",
"zune-jpegxl",
]
[[package]]
name = "craft-jpeg"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-keymap"
version = "0.1.0"
@@ -1262,6 +1298,17 @@ dependencies = [
"objc2-app-kit 0.3.2",
]
[[package]]
name = "craft-opc"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
dependencies = [
"craft-xml",
"craft-zip",
"log",
"thiserror 2.0.21",
]
[[package]]
name = "craft-pdf"
version = "0.1.0"
@@ -1334,6 +1381,15 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-tiff"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
dependencies = [
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-ui"
version = "0.1.0"
@@ -1410,6 +1466,15 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "craft-xml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
dependencies = [
"quick-xml 0.41.0",
"thiserror 2.0.21",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
@@ -2130,7 +2195,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -2168,6 +2233,22 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "exr"
version = "1.74.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6"
dependencies = [
"bit_field",
"half",
"lebe",
"miniz_oxide 0.8.9",
"multiversion",
"rayon-core",
"smallvec",
"zune-inflate",
]
[[package]]
name = "fast-float2"
version = "0.2.4"
@@ -3272,11 +3353,22 @@ dependencies = [
"moxcms",
"num-traits",
"png 0.18.1",
"qoi",
"tiff",
"zune-core",
"zune-jpeg",
]
[[package]]
name = "image-webp"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "525e9ff3e1a4be2fbea1fdf0e98686a6d98b4d8f937e1bf7402245af1909e8c3"
dependencies = [
"byteorder-lite",
"quick-error 2.0.1",
]
[[package]]
name = "imagesize"
version = "0.13.0"
@@ -3398,6 +3490,12 @@ dependencies = [
"libc",
]
[[package]]
name = "jpeg-encoder"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f"
[[package]]
name = "js-sys"
version = "0.3.106"
@@ -3479,6 +3577,12 @@ dependencies = [
"smallvec",
]
[[package]]
name = "lebe"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8"
[[package]]
name = "libc"
version = "0.2.189"
@@ -3703,6 +3807,28 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "multiversion"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076"
dependencies = [
"multiversion-macros",
"target-features",
]
[[package]]
name = "multiversion-macros"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
"target-features",
]
[[package]]
name = "multiversion_no_op"
version = "1.0.0"
@@ -4261,7 +4387,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -4415,9 +4541,9 @@ version = "0.21.1"
dependencies = [
"base64 0.23.1",
"craft-cms",
"craft-image-codecs",
"craft-prc",
"flate2",
"image",
"pdfcraft-cos",
"pdfcraft-engine",
"pdfcraft-fonts",
@@ -4427,7 +4553,6 @@ dependencies = [
"pdfcraft-prc-render",
"pdfcraft-render",
"pdfcraft-xfa",
"png 0.18.1",
"rustix 1.1.6",
"serde_json",
"sha2 0.11.1",
@@ -4439,6 +4564,7 @@ name = "pdfcraft-cli"
version = "0.21.1"
dependencies = [
"base64 0.23.1",
"craft-image-codecs",
"pdfcraft-audit",
"pdfcraft-automation",
"pdfcraft-cos",
@@ -4446,7 +4572,6 @@ dependencies = [
"pdfcraft-platform",
"pdfcraft-preflight",
"pdfcraft-render",
"png 0.18.1",
"rustix 1.1.6",
"serde_json",
"winresource",
@@ -4482,6 +4607,9 @@ dependencies = [
name = "pdfcraft-create"
version = "0.21.1"
dependencies = [
"craft-image-codecs",
"craft-jpeg",
"craft-tiff",
"encoding_rs",
"hayro-ccitt",
"hayro-syntax",
@@ -4491,11 +4619,8 @@ dependencies = [
"pdfcraft-fonts",
"pdfcraft-model",
"pdfcraft-xfa",
"png 0.18.1",
"rustix 1.1.6",
"thiserror 2.0.21",
"tiff",
"zune-jpeg",
]
[[package]]
@@ -4532,6 +4657,8 @@ dependencies = [
"chrono",
"craft-cms",
"craft-docpath",
"craft-image-codecs",
"craft-tiff",
"getrandom 0.4.3",
"icu_properties",
"idna",
@@ -4563,9 +4690,7 @@ dependencies = [
"pdfcraft-testkit",
"pdfcraft-xfa",
"pdfcraft-xfdf",
"png 0.18.1",
"thiserror 2.0.21",
"tiff",
"unicode-segmentation",
"url",
]
@@ -4574,7 +4699,9 @@ dependencies = [
name = "pdfcraft-export"
version = "0.21.1"
dependencies = [
"craft-opc",
"craft-rtf",
"craft-xml",
"craft-zip",
"flate2",
]
@@ -4647,9 +4774,9 @@ dependencies = [
name = "pdfcraft-ocr"
version = "0.21.1"
dependencies = [
"craft-image-codecs",
"ocrs",
"pdfcraft-fonts",
"png 0.18.1",
"rten",
"rten-imageproc",
"serde_json",
@@ -4672,6 +4799,8 @@ dependencies = [
name = "pdfcraft-optimize"
version = "0.21.1"
dependencies = [
"craft-image-codecs",
"craft-jpeg",
"image",
"pdfcraft-annot",
"pdfcraft-content",
@@ -4682,7 +4811,6 @@ dependencies = [
"proptest",
"thiserror 2.0.21",
"thread-priority",
"zune-jpeg",
]
[[package]]
@@ -4724,11 +4852,11 @@ dependencies = [
name = "pdfcraft-preflight"
version = "0.21.1"
dependencies = [
"craft-xml",
"flate2",
"pdfcraft-content",
"pdfcraft-cos",
"pdfcraft-model",
"roxmltree 0.21.2",
]
[[package]]
@@ -4752,6 +4880,7 @@ name = "pdfcraft-redact"
version = "0.21.1"
dependencies = [
"aho-corasick",
"craft-xml",
"pdfcraft-annot",
"pdfcraft-content",
"pdfcraft-cos",
@@ -4765,7 +4894,6 @@ dependencies = [
"pdfcraft-testkit",
"pdfcraft-xfa",
"regex",
"roxmltree 0.21.2",
"sha2 0.11.1",
"thiserror 2.0.21",
"unicode-normalization",
@@ -4848,6 +4976,7 @@ dependencies = [
"bytemuck",
"chrono",
"craft-i18n",
"craft-image-codecs",
"craft-keymap",
"craft-menu",
"craft-quit",
@@ -4877,7 +5006,6 @@ dependencies = [
"pdfcraft-render",
"pdfcraft-xfa",
"plist",
"png 0.18.1",
"pollster 1.0.1",
"proptest",
"rfd",
@@ -4917,11 +5045,10 @@ name = "pdfcraft-xfa"
version = "0.21.1"
dependencies = [
"base64 0.23.1",
"image",
"craft-image-codecs",
"craft-xml",
"pdfcraft-cos",
"pdfcraft-fonts",
"png 0.18.1",
"roxmltree 0.21.2",
"sha2 0.11.1",
"thiserror 2.0.21",
]
@@ -4932,12 +5059,12 @@ version = "0.21.1"
dependencies = [
"base64 0.23.1",
"craft-csv",
"craft-xml",
"pdfcraft-annot",
"pdfcraft-cos",
"pdfcraft-forms",
"pdfcraft-model",
"proptest",
"roxmltree 0.21.2",
"thiserror 2.0.21",
]
@@ -5392,6 +5519,15 @@ version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
[[package]]
name = "qoi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001"
dependencies = [
"bytemuck",
]
[[package]]
name = "quick-error"
version = "1.2.3"
@@ -5476,7 +5612,7 @@ dependencies = [
"once_cell",
"socket2",
"tracing",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -6036,7 +6172,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -6106,7 +6242,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -6986,6 +7122,12 @@ dependencies = [
"xattr",
]
[[package]]
name = "target-features"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5"
[[package]]
name = "tempfile"
version = "3.27.0"
@@ -6993,10 +7135,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
dependencies = [
"fastrand",
"getrandom 0.3.4",
"getrandom 0.4.3",
"once_cell",
"rustix 1.1.6",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -8121,7 +8263,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -8631,10 +8773,10 @@ version = "0.1.0"
dependencies = [
"anyhow",
"chrono",
"craft-image-codecs",
"lopdf",
"pdfcraft-cos",
"pdfcraft-oracle",
"png 0.18.1",
"serde",
"serde_json",
"sha2 0.11.1",
@@ -8902,6 +9044,15 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b"
[[package]]
name = "zune-inflate"
version = "0.2.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02"
dependencies = [
"simd-adler32",
]
[[package]]
name = "zune-jpeg"
version = "0.5.15"
@@ -8911,6 +9062,15 @@ dependencies = [
"zune-core",
]
[[package]]
name = "zune-jpegxl"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4"
dependencies = [
"zune-core",
]
[[package]]
name = "zvariant"
version = "5.15.0"
+8
View File
@@ -37,6 +37,14 @@ craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.
craft-pdf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3", features = ["cos", "crypt", "filters"] }
craft-rtf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-csv = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-xml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-tiff = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-jpeg = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-docx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-pptx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-drawingml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
craft-opc = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" }
# internal
pdfcraft-geom = { path = "crates/geom", version = "0.21.0" }
pdfcraft-filters = { path = "crates/filters", version = "0.21.0" }
+2 -2
View File
@@ -24,8 +24,8 @@ pdfcraft-platform = { workspace = true }
base64 = "0.23"
[dev-dependencies]
# Decodes `render` output in tests/it/owner_password.rs; already a dependency of the engine.
png = "0.18"
# Decodes `render` output in tests/it/owner_password.rs.
craft-image-codecs = { workspace = true }
[target.'cfg(unix)'.dependencies]
# `geteuid` for `ui`'s control-file owner check: safe, so no `unsafe` libc call here.
+4 -6
View File
@@ -104,14 +104,12 @@ fn text_accepts_the_owner_password() {
/// Asserts that `data` is a PNG of the fixture's 200×300 pt page at `dpi` with the text drawn:
/// a blank page of the right size fails too.
fn assert_rendered_page(data: &[u8], dpi: u32, what: &str) {
let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap_or_else(|e| panic!("{what}: {e}"));
let mut buf = vec![0; reader.output_buffer_size().unwrap()];
let frame = reader.next_frame(&mut buf).unwrap();
assert_eq!((frame.width, frame.height), (200 * dpi / 72, 300 * dpi / 72), "{what}");
let image = craft_image_codecs::decode(data).unwrap_or_else(|e| panic!("{what}: {e}"));
assert_eq!((image.width(), image.height()), (200 * dpi / 72, 300 * dpi / 72), "{what}");
// A rendered page is opaque, so `render` writes it as 8-bit RGB (`pdfcraft_engine::export::encode_png`).
assert_eq!((frame.color_type, frame.bit_depth), (png::ColorType::Rgb, png::BitDepth::Eight), "{what}");
assert_eq!((image.layout(), image.sample_type()), (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8), "{what}");
// Pixels with every colour channel below half intensity: ink, not white paper.
let inked = buf[..frame.buffer_size()].as_chunks::<3>().0.iter().filter(|px| px.iter().all(|c| *c < 128)).count();
let inked = image.data().as_chunks::<3>().0.iter().filter(|px| px.iter().all(|c| *c < 128)).count();
assert!(inked > 100, "{what}: only {inked} inked pixels, the page is blank");
}
+1 -2
View File
@@ -30,12 +30,11 @@ mcp = ["dep:base64"]
[dev-dependencies]
# Synthetic printer profiles for the app-managed print colour tests.
craft-cms = { workspace = true }
craft-image-codecs = { workspace = true }
pdfcraft-fonts = { workspace = true }
png = "0.18"
flate2.workspace = true
pdfcraft-model = { workspace = true }
pdfcraft-xfa = { workspace = true }
image = { version = "0.25", default-features = false, features = ["jpeg"] }
[target.'cfg(target_os = "macos")'.dev-dependencies]
rustix = { version = "1", features = ["fs"] }
+194 -243
View File
@@ -7,6 +7,32 @@ use pdfcraft_automation::mcp::McpServer;
use pdfcraft_automation::{Automation, Content, ToolError, tools};
use serde_json::{Value, json};
fn encode_png(w: u32, h: u32, layout: craft_image_codecs::ChannelLayout, data: Vec<u8>) -> Vec<u8> {
let img = craft_image_codecs::Image::from_u8(w, h, layout, data).unwrap();
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()
}
fn encode_jpeg_dpi(w: u32, h: u32, layout: craft_image_codecs::ChannelLayout, data: Vec<u8>, quality: u8, dpi: f32) -> Vec<u8> {
let mut img = craft_image_codecs::Image::from_u8(w, h, layout, data).unwrap();
img.meta.dpi = Some((dpi, dpi));
craft_image_codecs::encode(
&img,
craft_image_codecs::Format::Jpeg,
&craft_image_codecs::EncodeOptions { jpeg_quality: quality, ..Default::default() },
)
.unwrap()
}
fn decode_rgba(data: &[u8]) -> (u32, u32, Vec<u8>) {
let img = craft_image_codecs::decode(data).unwrap();
(img.width(), img.height(), img.to_rgba8())
}
fn rgba_at(w: u32, rgba: &[u8], x: u32, y: u32) -> [u8; 4] {
let at = ((y * w + x) * 4) as usize;
rgba[at..at + 4].try_into().unwrap()
}
/// A PDF with `n` 200×300 pt pages reading "Page 1", "Page 2", …
fn fixture(n: usize) -> Vec<u8> {
fixture_with(n, "")
@@ -921,9 +947,8 @@ fn open_inspect_render_and_find() {
let Content::Png { data, width, height } = &png[0] else { panic!("expected an image") };
assert_eq!((*width, *height), (200, 300));
assert_eq!(&data[..8], b"\x89PNG\r\n\x1a\n");
let decoder = png::Decoder::new(std::io::Cursor::new(data.as_slice()));
let info = decoder.read_info().unwrap().info().clone();
assert_eq!((info.width, info.height), (200, 300));
let decoded = craft_image_codecs::decode(&data).unwrap();
assert_eq!(decoded.dimensions(), (200, 300));
}
#[test]
@@ -961,10 +986,8 @@ fn text_editor_background_is_read_only_and_removes_source_glyphs() {
let preview = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72, "hide_text_block": 1 })).unwrap();
assert_ne!(preview, before);
let Content::Png { data, .. } = &preview[0] else { panic!("expected PNG") };
let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap();
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
let info = reader.next_frame(&mut pixels).unwrap();
assert!(pixels[..info.buffer_size()].iter().all(|b| *b == 255), "no source glyphs remain on the white page");
let pixels = craft_image_codecs::decode(data).unwrap().into_data();
assert!(pixels.iter().all(|b| *b == 255), "no source glyphs remain on the white page");
assert_eq!(a.call("page_render", &args).unwrap(), before);
assert_eq!(ok(&mut a, "doc_info", json!({ "doc": doc })), state);
assert_eq!(page_text(&mut a, doc), ["Page 1", "Page 2"]);
@@ -1241,13 +1264,7 @@ fn combine_renames_colliding_field_names() {
#[test]
fn combine_takes_images_and_text_files() {
let dir = workdir("combine-images");
let mut png = Vec::new();
{
let mut encoder = png::Encoder::new(&mut png, 4, 2);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
encoder.write_header().unwrap().write_image_data(&[90; 24]).unwrap();
}
let png = encode_png(4, 2, craft_image_codecs::ChannelLayout::Rgb, vec![90; 24]);
std::fs::write(dir.join("scan.png"), png).unwrap();
std::fs::write(dir.join("notes.txt"), "Some notes").unwrap();
std::fs::write(dir.join("broken.png"), b"\x89PNG\r\n\x1a\nbroken").unwrap();
@@ -4913,12 +4930,17 @@ fn enhance_camera_image_through_tools() {
fn enhance_scanned_file_cleans_the_scan_in_place_through_tools() {
let dir = workdir("enhance-scan");
// A 300 × 400 grey scan: dull paper, a dark gutter at the left, text bars and a speck.
let img = image::GrayImage::from_fn(300, 400, |x, y| {
let light = 0.55 + 0.45 * (x as f32 / 60.0).min(1.0);
let ink = if ((60..360).contains(&y) && (y - 60) % 40 < 6 && (60..240).contains(&x)) || (x, y) == (150, 25) { 0.12 } else { 0.78 };
image::Luma([(ink * light * 255.0) as u8])
});
img.save(dir.join("scan.jpg")).unwrap();
let mut gray = Vec::with_capacity(300 * 400);
for y in 0..400u32 {
for x in 0..300u32 {
let light = 0.55 + 0.45 * (x as f32 / 60.0).min(1.0);
let ink = if ((60..360).contains(&y) && (y - 60) % 40 < 6 && (60..240).contains(&x)) || (x, y) == (150, 25) { 0.12 } else { 0.78 };
gray.push((ink * light * 255.0) as u8);
}
}
let img = craft_image_codecs::Image::from_u8(300, 400, craft_image_codecs::ChannelLayout::Gray, gray).unwrap();
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 85, ..Default::default() };
std::fs::write(dir.join("scan.jpg"), craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap()).unwrap();
let mut a = auto(&dir);
let scan = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["scan.jpg"] }))["doc"].as_u64().unwrap();
ok(&mut a, "image_save", json!({ "doc": scan, "page": 1, "image": 1, "path": "before.jpg" }));
@@ -4932,16 +4954,20 @@ fn enhance_scanned_file_cleans_the_scan_in_place_through_tools() {
assert_eq!((images["count"].clone(), images["images"][0]["pixels"].clone()), (json!(1), json!([300, 400])));
let saved = ok(&mut a, "image_save", json!({ "doc": scan, "page": 1, "image": 1, "path": "after" }));
assert_eq!(saved["format"], "jpg");
let (before, after) = (image::open(dir.join("before.jpg")).unwrap().to_luma8(), image::open(dir.join("after.jpg")).unwrap().to_luma8());
let to_luma = |path: &Path| {
let img = craft_image_codecs::decode(&std::fs::read(path).unwrap()).unwrap();
let w = img.width();
let g = img.convert(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8).into_data();
(w, g)
};
let (bw, before) = to_luma(&dir.join("before.jpg"));
let (aw, after) = to_luma(&dir.join("after.jpg"));
for (x, y) in [(150, 40), (3, 40), (3, 380), (150, 25)] {
assert!(
before.get_pixel(x, y)[0] < 200 && after.get_pixel(x, y)[0] >= 230,
"({x}, {y}): {:?} → {:?}",
before.get_pixel(x, y),
after.get_pixel(x, y)
);
let bp = before[(y * bw + x) as usize];
let ap = after[(y * aw + x) as usize];
assert!(bp < 200 && ap >= 230, "({x}, {y}): {bp} → {ap}");
}
assert!(after.get_pixel(150, 62)[0] <= 60, "a stroke stays dark");
assert!(after[(62 * aw + 150) as usize] <= 60, "a stroke stays dark");
// Undo puts the scan back as it was.
ok(&mut a, "edit_undo", json!({ "doc": scan }));
ok(&mut a, "image_save", json!({ "doc": scan, "page": 1, "image": 1, "path": "undone.jpg" }));
@@ -4976,10 +5002,12 @@ fn enhance_camera_image_finds_the_edges_by_itself() {
(b[0] - a[0]) * (y - a[1]) - (b[1] - a[1]) * (x - a[0]) >= 0.0
})
};
let img = image::RgbImage::from_fn(600, 450, |x, y| {
if inside(x as f64 + 0.5, y as f64 + 0.5) { image::Rgb([240, 238, 232]) } else { image::Rgb([70, 60, 50]) }
});
img.save(dir.join("photo.jpg")).unwrap();
let rgb: Vec<u8> = (0..450)
.flat_map(|y| (0..600).flat_map(move |x| if inside(x as f64 + 0.5, y as f64 + 0.5) { [240, 238, 232] } else { [70, 60, 50] }))
.collect();
let img = craft_image_codecs::Image::from_u8(600, 450, craft_image_codecs::ChannelLayout::Rgb, rgb).unwrap();
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 85, ..Default::default() };
std::fs::write(dir.join("photo.jpg"), craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap()).unwrap();
let mut a = auto(&dir);
let photo = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["photo.jpg"] }))["doc"].as_u64().unwrap();
let r = ok(&mut a, "page_enhance_camera", json!({ "doc": photo, "page": 1 }));
@@ -5238,16 +5266,10 @@ fn fill_sign_text_border_is_saved_and_validated() {
#[test]
fn image_signatures_through_tools_preserve_transparency_and_survive_save() {
let dir = workdir("image-signatures");
let mut png = Vec::new();
{
let mut encoder = png::Encoder::new(&mut png, 120, 40);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let rgba: Vec<u8> = (0..40)
.flat_map(|y| (0..120).flat_map(move |x| if (40..80).contains(&x) && (10..30).contains(&y) { [0, 0, 0, 255] } else { [0, 0, 0, 0] }))
.collect();
encoder.write_header().unwrap().write_image_data(&rgba).unwrap();
}
let rgba: Vec<u8> = (0..40)
.flat_map(|y| (0..120).flat_map(move |x| if (40..80).contains(&x) && (10..30).contains(&y) { [0, 0, 0, 255] } else { [0, 0, 0, 0] }))
.collect();
let png = encode_png(120, 40, craft_image_codecs::ChannelLayout::Rgba, rgba);
std::fs::write(dir.join("signature.png"), png).unwrap();
std::fs::write(dir.join("broken.png"), b"broken").unwrap();
let mut a = auto(&dir);
@@ -5258,11 +5280,11 @@ fn image_signatures_through_tools_preserve_transparency_and_survive_save() {
let render = |a: &mut Automation, doc| {
let output = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72 })).unwrap();
let Content::Png { data, .. } = &output[0] else { panic!() };
image::load_from_memory(data).unwrap().to_rgba8()
decode_rgba(data)
};
let before = render(&mut a, doc);
assert_eq!(before.get_pixel(25, 60).0, [255, 255, 255, 255], "transparent margin exposes the page");
assert_eq!(before.get_pixel(65, 60).0, [0, 0, 0, 255], "signature ink is embedded");
let (bw, _, before) = render(&mut a, doc);
assert_eq!(rgba_at(bw, &before, 25, 60), [255, 255, 255, 255], "transparent margin exposes the page");
assert_eq!(rgba_at(bw, &before, 65, 60), [0, 0, 0, 255], "signature ink is embedded");
assert_eq!(ok(&mut a, "edit_undo", json!({ "doc": doc }))["undone"], "Add initials");
assert_eq!(ok(&mut a, "comment_list", json!({ "doc": doc }))["count"], 1);
ok(&mut a, "edit_redo", json!({ "doc": doc }));
@@ -5273,19 +5295,19 @@ fn image_signatures_through_tools_preserve_transparency_and_survive_save() {
ok(&mut a, "comment_lock", json!({ "doc": doc, "page": 1, "index": 1 }));
assert!(a.call("comment_edit", &json!({ "doc": doc, "page": 1, "index": 1, "rect": [20, 40, 164, 88] })).is_err());
ok(&mut a, "comment_lock", json!({ "doc": doc, "page": 1, "index": 1, "locked": false }));
assert_eq!(render(&mut a, doc), before, "invalid and locked resizes leave the image intact");
assert_eq!(render(&mut a, doc).2, before, "invalid and locked resizes leave the image intact");
ok(&mut a, "comment_edit", json!({ "doc": doc, "page": 1, "index": 1, "rect": [20, 40, 164, 88] }));
let resized = render(&mut a, doc);
assert_eq!(resized.get_pixel(110, 64).0, [0, 0, 0, 255], "resizing scales the original ink");
assert_eq!(resized.get_pixel(25, 64).0, [255, 255, 255, 255], "resizing retains alpha");
let (rw, _, resized) = render(&mut a, doc);
assert_eq!(rgba_at(rw, &resized, 110, 64), [0, 0, 0, 255], "resizing scales the original ink");
assert_eq!(rgba_at(rw, &resized, 25, 64), [255, 255, 255, 255], "resizing retains alpha");
assert_eq!(ok(&mut a, "edit_undo", json!({ "doc": doc }))["undone"], "Resize comment");
assert_eq!(render(&mut a, doc), before);
assert_eq!(render(&mut a, doc).2, before);
ok(&mut a, "edit_redo", json!({ "doc": doc }));
assert_eq!(render(&mut a, doc), resized);
assert_eq!(render(&mut a, doc).2, resized);
ok(&mut a, "doc_save", json!({ "doc": doc, "path": "signed.pdf" }));
let reopened = ok(&mut a, "doc_open", json!({ "path": "signed.pdf" }))["doc"].as_u64().unwrap();
assert_eq!(ok(&mut a, "comment_list", json!({ "doc": reopened }))["count"], 2);
assert_eq!(render(&mut a, reopened), resized, "resized appearances retain alpha and geometry after save");
assert_eq!(render(&mut a, reopened).2, resized, "resized appearances retain alpha and geometry after save");
for args in [
json!({ "type": "signature", "path": "broken.png" }),
json!({ "type": "signature", "path": "signature.png", "text": "Ada" }),
@@ -5306,10 +5328,6 @@ fn scanned_signatures_through_tools_drop_the_paper_and_report_bad_arguments() {
let dir = workdir("scanned-signatures");
// A grey, unevenly lit sheet with a pen stroke for the signature, and a blank one.
let photo = |ink: bool| {
let mut png = Vec::new();
let mut encoder = png::Encoder::new(&mut png, 400, 300);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
let rgb: Vec<u8> = (0..300_u32)
.flat_map(|y| {
(0..400_u32).flat_map(move |x| {
@@ -5318,8 +5336,7 @@ fn scanned_signatures_through_tools_drop_the_paper_and_report_bad_arguments() {
})
})
.collect();
encoder.write_header().unwrap().write_image_data(&rgb).unwrap();
png
encode_png(400, 300, craft_image_codecs::ChannelLayout::Rgb, rgb)
};
std::fs::write(dir.join("photo.png"), photo(true)).unwrap();
std::fs::write(dir.join("blank.png"), photo(false)).unwrap();
@@ -5328,11 +5345,11 @@ fn scanned_signatures_through_tools_drop_the_paper_and_report_bad_arguments() {
ok(&mut a, "fill_sign_add", json!({ "doc": doc, "page": 1, "type": "signature", "at": [20, 60], "path": "photo.png", "scan": true }));
let output = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72 })).unwrap();
let Content::Png { data, .. } = &output[0] else { panic!() };
let page = image::load_from_memory(data).unwrap().to_rgba8();
let dark = (20..170).filter(|&x| page.get_pixel(x, 60).0[0] < 80).count();
let (pw, _, page) = decode_rgba(data);
let dark = (20..170).filter(|&x| rgba_at(pw, &page, x, 60)[0] < 80).count();
assert!(dark > 100, "the stroke is placed as ink across the signature width ({dark} px)");
for (x, y) in [(22, 50), (100, 50), (100, 70)] {
assert_eq!(page.get_pixel(x, y).0, [255, 255, 255, 255], "the grey paper is gone at {x},{y}");
assert_eq!(rgba_at(pw, &page, x, y), [255, 255, 255, 255], "the grey paper is gone at {x},{y}");
}
ok(
&mut a,
@@ -5366,16 +5383,10 @@ fn scanned_signatures_through_tools_drop_the_paper_and_report_bad_arguments() {
#[test]
fn image_signature_preview_layers_are_read_only_and_survive_encrypted_save() {
let dir = workdir("signature-preview");
let mut png = Vec::new();
{
let mut encoder = png::Encoder::new(&mut png, 120, 40);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let rgba: Vec<u8> = (0..40)
.flat_map(|y| (0..120).flat_map(move |x| if (40..80).contains(&x) && (10..30).contains(&y) { [20, 40, 60, 128] } else { [0, 0, 0, 0] }))
.collect();
encoder.write_header().unwrap().write_image_data(&rgba).unwrap();
}
let rgba: Vec<u8> = (0..40)
.flat_map(|y| (0..120).flat_map(move |x| if (40..80).contains(&x) && (10..30).contains(&y) { [20, 40, 60, 128] } else { [0, 0, 0, 0] }))
.collect();
let png = encode_png(120, 40, craft_image_codecs::ChannelLayout::Rgba, rgba);
std::fs::write(dir.join("signature.png"), png).unwrap();
std::fs::write(dir.join("form.pdf"), fixture(1)).unwrap();
let mut a = auto(&dir);
@@ -5384,7 +5395,7 @@ fn image_signature_preview_layers_are_read_only_and_survive_encrypted_save() {
ok(&mut a, "fill_sign_add", json!({ "doc": doc, "page": 1, "type": "initials", "at": [20, 40], "path": "signature.png" }));
let background = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72 })).unwrap();
let Content::Png { data: expected, .. } = &background[0] else { panic!() };
let expected = image::load_from_memory(expected).unwrap().to_rgba8();
let expected = decode_rgba(expected).2;
ok(&mut a, "fill_sign_add", json!({ "doc": doc, "page": 1, "type": "signature", "at": [120, 100], "path": "signature.png" }));
ok(&mut a, "comment_edit", json!({ "doc": doc, "page": 1, "index": 2, "opacity": 0.5 }));
ok(&mut a, "doc_protect", json!({ "doc": doc, "open_password": "preview-test" }));
@@ -5405,10 +5416,10 @@ fn image_signature_preview_layers_are_read_only_and_survive_encrypted_save() {
assert_eq!(meta["image_rotation"], 0, "placed upright as displayed on the turned page");
assert_eq!(meta["opacity"], 0.5);
assert_eq!((*width, *height), (120, 40));
assert_eq!(image::load_from_memory(background).unwrap().to_rgba8(), expected, "page text and the other signature remain");
let signature = image::load_from_memory(signature).unwrap().to_rgba8();
assert_eq!(signature.get_pixel(5, 20).0[3], 0);
assert_eq!(signature.get_pixel(60, 20).0, [20, 40, 60, 128], "embedded alpha and colour survive reopen");
assert_eq!(decode_rgba(background).2, expected, "page text and the other signature remain");
let (sw, _, signature) = decode_rgba(signature);
assert_eq!(rgba_at(sw, &signature, 5, 20)[3], 0);
assert_eq!(rgba_at(sw, &signature, 60, 20), [20, 40, 60, 128], "embedded alpha and colour survive reopen");
assert_eq!(ok(&mut a, "doc_info", json!({ "doc": doc })), before);
let d = a.session().docs().iter().find(|d| d.id.0 == doc).unwrap();
assert_eq!(d.edit_generation(), generation);
@@ -5468,13 +5479,7 @@ fn creating_from_text_refuses_what_the_font_cannot_draw() {
#[test]
fn creating_from_multiple_files_through_tools() {
let dir = workdir("create-multiple");
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 4, 2);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
enc.write_header().unwrap().write_image_data(&[100; 4 * 2 * 3]).unwrap();
}
let png = encode_png(4, 2, craft_image_codecs::ChannelLayout::Rgb, vec![100; 4 * 2 * 3]);
std::fs::write(dir.join("scan.png"), png).unwrap();
std::fs::write(dir.join("notes.txt"), "hello").unwrap();
std::fs::write(dir.join("archive.zip"), b"PK\x03\x04").unwrap();
@@ -5535,14 +5540,9 @@ fn create_multiple_has_no_file_count_limit() {
#[test]
fn creating_images_with_dpi_through_tools() {
let dir = workdir("image-dpi");
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 300, 150);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
enc.set_pixel_dims(Some(png::PixelDimensions { xppu: 11811, yppu: 5906, unit: png::Unit::Meter }));
enc.write_header().unwrap().write_image_data(&vec![100; 300 * 150 * 3]).unwrap();
}
let mut img = craft_image_codecs::Image::from_u8(300, 150, craft_image_codecs::ChannelLayout::Rgb, vec![100; 300 * 150 * 3]).unwrap();
img.meta.dpi = Some((300.0, 150.0));
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
std::fs::write(dir.join("scan.png"), png).unwrap();
let mut a = auto(&dir);
for (dpi, width, height) in [(None, 72.0, 72.0), (Some(72.0), 300.0, 150.0), (Some(300.0), 72.0, 36.0)] {
@@ -6383,13 +6383,7 @@ fn printer_papers_through_tools() {
#[test]
fn adding_content_through_tools() {
let dir = workdir("content");
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 40, 20);
enc.set_color(png::ColorType::Rgb);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[200u8; 2400]).unwrap();
}
let png = encode_png(40, 20, craft_image_codecs::ChannelLayout::Rgb, vec![200u8; 2400]);
std::fs::write(dir.join("logo.png"), png).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();
@@ -6475,13 +6469,7 @@ fn added_text_takes_a_cmyk_or_gray_ink_through_tools() {
}
assert_eq!(ok(&mut a, "content_list", json!({ "doc": doc }))["count"], 2, "nothing refused was added");
// content_update refuses them too, an image takes no ink, and a refused update changes nothing.
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 40, 20);
enc.set_color(png::ColorType::Rgb);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[200u8; 2400]).unwrap();
}
let png = encode_png(40, 20, craft_image_codecs::ChannelLayout::Rgb, vec![200u8; 2400]);
std::fs::write(dir.join("logo.png"), png).unwrap();
ok(&mut a, "page_add_image", json!({ "doc": doc, "page": 1, "path": "logo.png", "rect": [100, 200, 180, 240] }));
let before = ok(&mut a, "content_list", json!({ "doc": doc }))["items"].clone();
@@ -6505,12 +6493,7 @@ fn added_text_takes_a_cmyk_or_gray_ink_through_tools() {
#[test]
fn copying_added_content_through_tools() {
let dir = workdir("content-copy");
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 40, 20);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[90u8; 2400]).unwrap();
}
let png = encode_png(40, 20, craft_image_codecs::ChannelLayout::Rgb, vec![90u8; 2400]);
std::fs::write(dir.join("logo.png"), png).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();
@@ -6622,10 +6605,7 @@ fn added_text_takes_character_spacing() {
}
fn added_rotation_image(dir: &Path) {
let mut png = Vec::new();
let mut enc = png::Encoder::new(&mut png, 2, 1);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[255, 0, 0, 0, 0, 255]).unwrap();
let png = encode_png(2, 1, craft_image_codecs::ChannelLayout::Rgb, vec![255, 0, 0, 0, 0, 255]);
std::fs::write(dir.join("rotation.png"), png).unwrap();
}
@@ -6723,12 +6703,7 @@ fn large_added_image_rotations_round_trip_through_mcp() {
#[test]
fn text_styling_on_an_image_is_refused_without_an_edit() {
let dir = workdir("content-style");
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 1, 1);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[80, 160, 200]).unwrap();
}
let png = encode_png(1, 1, craft_image_codecs::ChannelLayout::Rgb, vec![80, 160, 200]);
std::fs::write(dir.join("logo.png"), png).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();
@@ -7131,20 +7106,13 @@ fn comments_and_form_data_travel_as_xfdf_fdf_and_text() {
fn natural_image_stamps_through_tools_on_rotated_pages() {
for degrees in [0, 90, 180, 270] {
let dir = workdir(&format!("natural-stamps-{degrees}"));
let mut bytes = Vec::new();
{
let mut encoder = png::Encoder::new(&mut bytes, 80, 40);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
let pixels: Vec<u8> = (0..40)
.flat_map(|y| {
(0..80).flat_map(move |x| {
[[240, 20, 20], [20, 180, 20], [20, 20, 240], [230, 180, 20]][usize::from(y >= 20) * 2 + usize::from(x >= 40)]
})
})
.collect();
encoder.write_header().unwrap().write_image_data(&pixels).unwrap();
}
let pixels: Vec<u8> = (0..40)
.flat_map(|y| {
(0..80)
.flat_map(move |x| [[240, 20, 20], [20, 180, 20], [20, 20, 240], [230, 180, 20]][usize::from(y >= 20) * 2 + usize::from(x >= 40)])
})
.collect();
let bytes = encode_png(80, 40, craft_image_codecs::ChannelLayout::Rgb, pixels);
std::fs::write(dir.join("quadrants.png"), bytes).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_open", json!({"path":"a.pdf"}))["doc"].as_u64().unwrap();
@@ -7152,15 +7120,14 @@ fn natural_image_stamps_through_tools_on_rotated_pages() {
ok(&mut a, "stamp_custom", json!({"doc":doc,"page":1,"path":"quadrants.png","at":[100,100]}));
let rendered = a.call("page_render", &json!({"doc":doc,"page":1,"dpi":72})).unwrap();
let Content::Png { data, .. } = &rendered[0] else { panic!("expected PNG") };
let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap();
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
let info = reader.next_frame(&mut pixels).unwrap();
let img = craft_image_codecs::decode(data).unwrap();
// A rendered page is opaque, so it comes as RGB.
assert_eq!(info.color_type, png::ColorType::Rgb);
assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgb);
let (w, pixels) = (img.width(), img.data());
for ((x, y), colour) in
[(80, 90), (120, 90), (80, 110), (120, 110)].into_iter().zip([[240, 20, 20], [20, 180, 20], [20, 20, 240], [230, 180, 20]])
{
let offset = ((y * info.width + x) * 3) as usize;
let offset = ((y * w + x) * 3) as usize;
assert_eq!(&pixels[offset..offset + 3], &colour, "page rotation {degrees}");
}
}
@@ -7238,13 +7205,7 @@ fn custom_stamps_fit_a_pdf_page_inside_a_rect() {
// On a quarter-turned page the picture is fitted as displayed: an 80 × 40 picture in a
// 100 × 100 box is 100 × 50 on screen, centred.
let mut png_bytes = Vec::new();
{
let mut encoder = png::Encoder::new(&mut png_bytes, 80, 40);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
encoder.write_header().unwrap().write_image_data(&[200; 80 * 40 * 3]).unwrap();
}
let png_bytes = encode_png(80, 40, craft_image_codecs::ChannelLayout::Rgb, vec![200; 80 * 40 * 3]);
std::fs::write(dir.join("wide.png"), png_bytes).unwrap();
let turned = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();
ok(&mut a, "page_rotate", json!({ "doc": turned, "pages": [1], "degrees": 90 }));
@@ -7761,13 +7722,17 @@ fn note_icons_anchor_at_the_requested_corner_on_rotated_pages() {
fn image_signatures_stay_upright_on_rotated_pages() {
let dir = workdir("image-upright");
// 80 x 40 px, one colour per quadrant: red and green above, blue and yellow below.
let quadrants = image::RgbaImage::from_fn(80, 40, |x, y| match (x < 40, y < 20) {
(true, true) => image::Rgba([255, 0, 0, 255]),
(false, true) => image::Rgba([0, 255, 0, 255]),
(true, false) => image::Rgba([0, 0, 255, 255]),
(false, false) => image::Rgba([255, 255, 0, 255]),
});
quadrants.save(dir.join("quadrants.png")).unwrap();
let quadrants: Vec<u8> = (0..40)
.flat_map(|y| {
(0..80).flat_map(move |x| match (x < 40, y < 20) {
(true, true) => [255, 0, 0, 255],
(false, true) => [0, 255, 0, 255],
(true, false) => [0, 0, 255, 255],
(false, false) => [255, 255, 0, 255],
})
})
.collect();
std::fs::write(dir.join("quadrants.png"), encode_png(80, 40, craft_image_codecs::ChannelLayout::Rgba, quadrants)).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_create", json!({ "from": "blank", "width": 200, "height": 300, "pages": 4 }))["doc"].as_u64().unwrap();
for (page, degrees) in [(2, 90), (3, 180), (4, 270)] {
@@ -7782,7 +7747,7 @@ fn image_signatures_stay_upright_on_rotated_pages() {
for page in 1..=4 {
let output = a.call("page_render", &json!({ "doc": doc, "page": page, "dpi": 72 })).unwrap();
let Content::Png { data, .. } = &output[0] else { panic!() };
let pixels = image::load_from_memory(data).unwrap().to_rgba8();
let (pw, _, pixels) = decode_rgba(data);
for (x, y, want, what) in [
(76, 132, red, "signature top-left"),
(108, 132, green, "signature top-right"),
@@ -7793,7 +7758,7 @@ fn image_signatures_stay_upright_on_rotated_pages() {
(92, 120, white, "above the signature"),
(92, 160, white, "below the signature"),
] {
let got = pixels.get_pixel(x, y).0;
let got = rgba_at(pw, &pixels, x, y);
assert!(got[..3].iter().zip(want).all(|(g, w)| g.abs_diff(w) < 12), "page {page}, {what} at ({x}, {y}): {got:?}, want {want:?}");
}
}
@@ -7823,14 +7788,16 @@ fn typed_signatures_stay_upright_on_rotated_pages() {
for page in 1..=4 {
let output = a.call("page_render", &json!({ "doc": doc, "page": page, "dpi": 72 })).unwrap();
let Content::Png { data, .. } = &output[0] else { panic!() };
let pixels = image::load_from_memory(data).unwrap().to_rgba8();
assert_eq!(pixels.dimensions(), if page % 2 == 1 { (200, 300) } else { (300, 200) }, "page {page}");
let dark: Vec<bool> = pixels.pixels().map(|p| p.0[..3].iter().all(|v| *v < 128)).collect();
let (width, height, pixels) = decode_rgba(data);
assert_eq!((width, height), if page % 2 == 1 { (200, 300) } else { (300, 200) }, "page {page}");
let dark: Vec<bool> = pixels.chunks_exact(4).map(|p| p[..3].iter().all(|v| *v < 128)).collect();
for y in [60, 140] {
let mut b = [u32::MAX, u32::MAX, 0, 0];
for (x, py, p) in pixels.enumerate_pixels() {
if py.abs_diff(y) < 40 && p.0[..3].iter().all(|v| *v < 128) {
b = [b[0].min(x), b[1].min(py), b[2].max(x), b[3].max(py)];
for py in 0..height {
for x in 0..width {
if py.abs_diff(y) < 40 && rgba_at(width, &pixels, x, py)[..3].iter().all(|v| *v < 128) {
b = [b[0].min(x), b[1].min(py), b[2].max(x), b[3].max(py)];
}
}
}
assert!(b[0] <= b[2], "page {page}: no ink near y = {y}");
@@ -7842,7 +7809,6 @@ fn typed_signatures_stay_upright_on_rotated_pages() {
continue;
}
// Same pixels as the unturned page where both pages are (pages 2 and 4 are wider).
let width = pixels.width();
let (mut differ, mut ink) = (0, 0);
for y in 0..200 {
for x in 0..200 {
@@ -7880,14 +7846,15 @@ fn fill_sign_marks_stay_upright_on_rotated_pages() {
// 144 dpi: two pixels a point.
let output = a.call("page_render", &json!({ "doc": doc, "page": page, "dpi": 144 })).unwrap();
let Content::Png { data, .. } = &output[0] else { panic!() };
let pixels = image::load_from_memory(data).unwrap().to_rgba8();
let dark: Vec<bool> = pixels.pixels().map(|p| p.0[..3].iter().all(|v| *v < 128)).collect();
let width = pixels.width();
let (width, height, pixels) = decode_rgba(data);
let dark: Vec<bool> = pixels.chunks_exact(4).map(|p| p[..3].iter().all(|v| *v < 128)).collect();
// The line, 36 × 4 pt displayed, runs across.
let mut b = [u32::MAX, u32::MAX, 0, 0];
for (x, y, p) in pixels.enumerate_pixels() {
if x.abs_diff(240) < 60 && y.abs_diff(120) < 60 && p.0[..3].iter().all(|v| *v < 128) {
b = [b[0].min(x), b[1].min(y), b[2].max(x), b[3].max(y)];
for y in 0..height {
for x in 0..width {
if x.abs_diff(240) < 60 && y.abs_diff(120) < 60 && rgba_at(width, &pixels, x, y)[..3].iter().all(|v| *v < 128) {
b = [b[0].min(x), b[1].min(y), b[2].max(x), b[3].max(y)];
}
}
}
assert!(b[0] <= b[2], "page {page}: no line drawn");
@@ -8203,14 +8170,8 @@ fn lines_and_corners_are_held_level_or_plumb_through_the_tool() {
fn signing_with_an_image_typed_or_drawn_graphic_through_tools() {
let dir = workdir("signing-graphic");
// A solid red 120 x 40 picture.
let mut png = Vec::new();
{
let mut encoder = png::Encoder::new(&mut png, 120, 40);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let rgba: Vec<u8> = (0..120 * 40).flat_map(|_| [220, 0, 0, 255]).collect();
encoder.write_header().unwrap().write_image_data(&rgba).unwrap();
}
let rgba: Vec<u8> = (0..120 * 40).flat_map(|_| [220, 0, 0, 255]).collect();
let png = encode_png(120, 40, craft_image_codecs::ChannelLayout::Rgba, rgba);
std::fs::write(dir.join("logo.png"), png).unwrap();
std::fs::write(dir.join("broken.png"), b"broken").unwrap();
let mut a = auto(&dir);
@@ -8256,11 +8217,11 @@ fn signing_with_an_image_typed_or_drawn_graphic_through_tools() {
// details on the right.
let png = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72 })).unwrap();
let Content::Png { data, width, .. } = &png[0] else { panic!("expected an image") };
let mut reader = png::Decoder::new(std::io::Cursor::new(data.as_slice())).read_info().unwrap();
let mut buf = vec![0; reader.output_buffer_size().unwrap()];
let frame = reader.next_frame(&mut buf).unwrap();
let channels = frame.line_size / *width as usize;
let px = |x: usize, y: usize| buf[y * frame.line_size + x * channels..][..3].to_vec();
let (_, _, buf) = decode_rgba(data);
let px = |x: usize, y: usize| {
let at = (y * *width as usize + x) * 4;
buf[at..at + 3].to_vec()
};
assert_eq!(px(60, 225), [220, 0, 0], "the image fills the left half's middle");
assert_ne!(px(150, 225), [220, 0, 0], "and not the details");
let inked = |y0: usize, y1: usize| (y0..y1).any(|y| (23..97).any(|x| px(x, y).iter().all(|c| *c < 100)));
@@ -8427,8 +8388,8 @@ fn named_layer_verdicts_are_reported_and_rendered_through_tools() {
assert_eq!(list["signatures"][0]["verdict_shown"], true);
let rendered = a.call("page_render", &json!({"doc": doc, "page": 1, "dpi": 72})).unwrap();
let Content::Png { data, .. } = &rendered[0] else { panic!("expected a PNG") };
let pixels = image::load_from_memory_with_format(data, image::ImageFormat::Png).unwrap().to_rgba8();
let red = pixels.pixels().filter(|p| p.0.iter().zip([0xD7, 0x37, 0x3F]).all(|(a, b)| a.abs_diff(b) < 30)).count();
let (_, _, pixels) = decode_rgba(data);
let red = pixels.chunks_exact(4).filter(|p| p[..3].iter().zip([0xD7u8, 0x37, 0x3F]).all(|(a, b)| a.abs_diff(b) < 30)).count();
assert!(red > 100, "the invalid state is drawn: {red} red pixels");
ok(&mut a, "doc_save", json!({"doc": doc, "path": "saved.pdf", "full": false}));
assert_eq!(std::fs::read(dir.join("saved.pdf")).unwrap(), original, "the drawn state never enters saves");
@@ -8647,10 +8608,7 @@ fn optimizing_through_tools() {
[(x * 255 / w) as u8 ^ ((x * y) & 15) as u8, (y * 255 / h) as u8, ((x + y) & 255) as u8]
})
.collect();
let mut jpeg = Vec::new();
let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 95);
enc.set_pixel_density(image::codecs::jpeg::PixelDensity::dpi(600));
enc.encode(&px, w, h, image::ExtendedColorType::Rgb8).unwrap();
let jpeg = encode_jpeg_dpi(w, h, craft_image_codecs::ChannelLayout::Rgb, px, 95, 600.0);
std::fs::write(dir.join("photo.jpg"), &jpeg).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["photo.jpg"] }))["doc"].as_u64().unwrap();
@@ -8913,13 +8871,39 @@ fn a_one_bit_scan_stays_one_bit_and_exports() {
}
}
}
let mut png_bytes = Vec::new();
{
let mut enc = png::Encoder::new(&mut png_bytes, w, h);
enc.set_color(png::ColorType::Grayscale);
enc.set_depth(png::BitDepth::One);
enc.write_header().unwrap().write_image_data(&bits).unwrap();
}
let png_bytes = {
use std::io::Write as _;
let mut raw = Vec::with_capacity(h as usize * (1 + stride));
for row in bits.chunks_exact(stride) {
raw.push(0);
raw.extend_from_slice(row);
}
let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
z.write_all(&raw).unwrap();
let idat = z.finish().unwrap();
let chunk = |out: &mut Vec<u8>, tag: &[u8; 4], data: &[u8]| {
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
out.extend_from_slice(tag);
out.extend_from_slice(data);
let mut crc = 0xFFFF_FFFFu32;
for &b in tag.iter().chain(data.iter()) {
crc ^= u32::from(b);
for _ in 0..8 {
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
}
}
out.extend_from_slice(&(!crc).to_be_bytes());
};
let mut out = b"\x89PNG\r\n\x1a\n".to_vec();
let mut ihdr = Vec::with_capacity(13);
ihdr.extend_from_slice(&w.to_be_bytes());
ihdr.extend_from_slice(&h.to_be_bytes());
ihdr.extend_from_slice(&[1, 0, 0, 0, 0]);
chunk(&mut out, b"IHDR", &ihdr);
chunk(&mut out, b"IDAT", &idat);
chunk(&mut out, b"IEND", &[]);
out
};
std::fs::write(dir.join("scan.png"), &png_bytes).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["scan.png"] }))["doc"].as_u64().unwrap();
@@ -8938,10 +8922,8 @@ fn a_one_bit_scan_stays_one_bit_and_exports() {
let files = r["files"].as_array().unwrap();
assert_eq!(files.len(), 1, "{r}");
let exported = std::fs::read(files[0]["path"].as_str().unwrap()).unwrap();
let mut reader = png::Decoder::new(std::io::Cursor::new(exported)).read_info().unwrap();
let mut buf = vec![0; reader.output_buffer_size().unwrap()];
let frame = reader.next_frame(&mut buf).unwrap();
let white: Vec<bool> = buf[..frame.buffer_size()].as_chunks::<3>().0.iter().map(|p| *p == [255, 255, 255]).collect();
let (_, _, rgba) = decode_rgba(&exported);
let white: Vec<bool> = rgba.chunks_exact(4).map(|p| p[..3] == [255, 255, 255]).collect();
let expected: Vec<bool> =
(0..h as usize).flat_map(|y| (0..w as usize).map(move |x| (y, x))).map(|(y, x)| bits[y * stride + x / 8] & (0x80 >> (x % 8)) != 0).collect();
assert_eq!(white, expected);
@@ -9254,10 +9236,7 @@ fn optimizing_to_a_target_size() {
[(x * 255 / w) as u8 ^ ((x * y) & 15) as u8, (y * 255 / h) as u8, ((x + y) & 255) as u8]
})
.collect();
let mut jpeg = Vec::new();
let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 95);
enc.set_pixel_density(image::codecs::jpeg::PixelDensity::dpi(600));
enc.encode(&px, w, h, image::ExtendedColorType::Rgb8).unwrap();
let jpeg = encode_jpeg_dpi(w, h, craft_image_codecs::ChannelLayout::Rgb, px, 95, 600.0);
std::fs::write(dir.join("photo.jpg"), &jpeg).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["photo.jpg"] }))["doc"].as_u64().unwrap();
@@ -10581,10 +10560,7 @@ fn dynamic_xfa_forms_open_render_fill_and_save_through_tools() {
// The page renders with the widgets' own appearances: the check box's border is drawn.
let png = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 36 })).unwrap();
let Content::Png { data, .. } = &png[0] else { panic!("expected an image") };
let decoder = png::Decoder::new(std::io::Cursor::new(data.as_slice()));
let mut reader = decoder.read_info().unwrap();
let mut buf = vec![0; reader.output_buffer_size().unwrap()];
reader.next_frame(&mut buf).unwrap();
let buf = craft_image_codecs::decode(data).unwrap().into_data();
assert!(buf.iter().filter(|b| **b < 128).count() > 200, "the page is not blank");
ok(&mut a, "form_fill", json!({ "doc": doc, "values": { "familyName": "Singh", "agree": true, "answer": "N", "born": "2001-02-03" } }));
ok(&mut a, "doc_save", json!({ "doc": doc, "path": "out.pdf" }));
@@ -11484,7 +11460,7 @@ fn conventions_core_tools_preserve_root_and_batch_errors() {
assert_eq!(r["result"]["isError"], false, "{r}");
use base64::Engine as _;
let png = base64::engine::general_purpose::STANDARD.decode(r["result"]["content"][0]["data"].as_str().unwrap()).unwrap();
let image = image::load_from_memory(&png).unwrap();
let image = craft_image_codecs::decode(&png).unwrap();
assert!(image.width().max(image.height()) <= 64);
assert_eq!(before["result"], run(&mut s, "doc_info", json!({"doc":doc}))["result"]);
}
@@ -11790,13 +11766,7 @@ fn a_deleted_image_is_not_kept_by_reduce_or_optimize() {
let dir = workdir("deleted-image");
let (w, h) = (600u32, 400u32);
let px: Vec<u8> = (0..w * h).flat_map(|i| [(i % w * 255 / w) as u8 ^ (i & 7) as u8, (i / w * 255 / h) as u8, 128]).collect();
let mut photo = Vec::new();
{
let mut enc = png::Encoder::new(&mut photo, w, h);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
enc.write_header().unwrap().write_image_data(&px).unwrap();
}
let photo = encode_png(w, h, craft_image_codecs::ChannelLayout::Rgb, px);
std::fs::write(dir.join("photo.png"), photo).unwrap();
let mut a = auto(&dir);
let doc = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["photo.png"] }))["doc"].as_u64().unwrap();
@@ -12166,15 +12136,9 @@ fn reading_colors_workdir() -> PathBuf {
fn png_pixels(content: &Content) -> Vec<u8> {
let Content::Png { data, width, height } = content else { panic!("expected a PNG") };
assert_eq!((*width, *height), (200, 300));
let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap();
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
let info = reader.next_frame(&mut pixels).unwrap();
assert_eq!((info.width, info.height), (200, 300));
match info.color_type {
png::ColorType::Rgba => pixels,
png::ColorType::Rgb => pixels.as_chunks::<3>().0.iter().flat_map(|p| [p[0], p[1], p[2], 255]).collect(),
other => panic!("unexpected colour type {other:?}"),
}
let (w, h, rgba) = decode_rgba(data);
assert_eq!((w, h), (200, 300));
rgba
}
/// The RGBA pixel at (`x`, `y`), top-left origin, of a 200-wide image.
@@ -14277,26 +14241,13 @@ fn image_flip_tools_follow_displayed_axes_on_every_page_rotation() {
fn raster(a: &mut Automation, id: u64) -> (usize, usize, Vec<u8>) {
let rendered = a.call("page_render", &json!({"doc":id,"page":1,"dpi":72})).unwrap();
let Content::Png { data, .. } = &rendered[0] else { panic!("expected PNG") };
let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap();
let mut bytes = vec![0; reader.output_buffer_size().unwrap()];
let frame = reader.next_frame(&mut bytes).unwrap();
bytes.truncate(frame.buffer_size());
if frame.color_type == png::ColorType::Rgb {
bytes = bytes.as_chunks::<3>().0.iter().flat_map(|&[r, g, b]| [r, g, b, 255]).collect();
} else {
assert_eq!(frame.color_type, png::ColorType::Rgba);
}
(frame.width as usize, frame.height as usize, bytes)
let (w, h, rgba) = decode_rgba(data);
(w as usize, h as usize, rgba)
}
let dir = workdir("displayed-image-flip");
let mut png_bytes = Vec::new();
{
let mut encoder = png::Encoder::new(&mut png_bytes, 8, 4);
encoder.set_color(png::ColorType::Rgb);
let colors = [[240, 20, 30], [20, 230, 40], [30, 40, 220], [220, 210, 20]];
let pixels: Vec<u8> = (0..4).flat_map(|y| (0..8).flat_map(move |x| colors[usize::from(x >= 4) + 2 * usize::from(y >= 2)])).collect();
encoder.write_header().unwrap().write_image_data(&pixels).unwrap();
}
let colors = [[240, 20, 30], [20, 230, 40], [30, 40, 220], [220, 210, 20]];
let pixels: Vec<u8> = (0..4).flat_map(|y| (0..8).flat_map(move |x| colors[usize::from(x >= 4) + 2 * usize::from(y >= 2)])).collect();
let png_bytes = encode_png(8, 4, craft_image_codecs::ChannelLayout::Rgb, pixels);
std::fs::write(dir.join("quadrants.png"), png_bytes).unwrap();
let mut a = auto(&dir);
let mut failures = Vec::new();
+3 -6
View File
@@ -66,12 +66,9 @@ fn ok(a: &mut Automation, tool: &str, args: Value) -> Value {
fn blank_spot(a: &mut Automation, doc: u64, page: u64) -> [u8; 3] {
let rendered = a.call("page_render", &json!({ "doc": doc, "page": page, "dpi": 72 })).unwrap();
let Content::Png { data, width, .. } = &rendered[0] else { panic!("expected a PNG") };
let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap();
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
let info = reader.next_frame(&mut pixels).unwrap();
let channels = info.color_type.samples();
let at = (10 * *width as usize + 180) * channels;
[pixels[at], pixels[at + 1], pixels[at + 2]]
let rgba = craft_image_codecs::decode(data).unwrap().to_rgba8();
let at = (10 * *width as usize + 180) * 4;
[rgba[at], rgba[at + 1], rgba[at + 2]]
}
const RED: [u8; 3] = [255, 0, 0];
+2 -6
View File
@@ -13,12 +13,8 @@ fn tool(a: &mut Automation, name: &str, args: Value) -> Value {
}
fn png(w: u32, h: u32) -> Vec<u8> {
let mut out = Vec::new();
let mut encoder = png::Encoder::new(&mut out, w, h);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
encoder.write_header().unwrap().write_image_data(&vec![90; (w * h * 3) as usize]).unwrap();
out
let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, vec![90; (w * h * 3) as usize]).unwrap();
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()
}
fn stream(doc: &Document, obj: &Object) -> Stream {
+5 -6
View File
@@ -61,12 +61,11 @@ fn open(a: &mut Automation, path: &str) -> u64 {
/// The RGB of pixel (x, y) of a PNG.
fn rgb(data: &[u8], x: usize, y: usize) -> [u8; 3] {
let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap();
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
let info = reader.next_frame(&mut pixels).unwrap();
let channels = info.line_size / info.width as usize;
let at = y * info.line_size + x * channels;
[pixels[at], pixels[at + 1], pixels[at + 2]]
let img = craft_image_codecs::decode(data).unwrap();
let w = img.width() as usize;
let rgba = img.to_rgba8();
let at = (y * w + x) * 4;
[rgba[at], rgba[at + 1], rgba[at + 2]]
}
#[test]
@@ -79,12 +79,8 @@ fn check_page(a: &mut Automation, doc: u64, hidden: bool, phase: &str) {
let rendered = a.call("page_render", &json!({"doc": doc, "page": 1, "dpi": 72})).unwrap();
let Content::Png { data, width, height } = &rendered[0] else { panic!("expected page PNG") };
assert_eq!((*width, *height), (600, 800));
let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap();
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
let info = reader.next_frame(&mut pixels).unwrap();
let channels = info.color_type.samples();
assert!(channels >= 3);
let pixel = |x: usize, y: usize| &pixels[(y * 600 + x) * channels..(y * 600 + x) * channels + 3];
let rgba = craft_image_codecs::decode(data).unwrap().to_rgba8();
let pixel = |x: usize, y: usize| &rgba[(y * 600 + x) * 4..(y * 600 + x) * 4 + 3];
let dark = |left, top, right, bottom| {
(top..bottom).flat_map(|y| (left..right).map(move |x| (x, y))).filter(|&(x, y)| pixel(x, y).iter().any(|v| *v < 128)).count()
};
+5 -6
View File
@@ -15,11 +15,11 @@ pdfcraft-model = { workspace = true }
pdfcraft-fonts = { workspace = true }
# For `image::bmp_with_pixel_offset`, shared with XFA pictures.
pdfcraft-xfa = { workspace = true }
png = "0.18"
tiff = "0.11"
image = { version = "0.25", default-features = false, features = ["bmp", "gif", "jpeg"] }
# Already in the graph through image; read directly for CMYK JPEG samples, which image converts to RGB.
zune-jpeg = "0.5"
craft-image-codecs = { workspace = true }
craft-jpeg = { workspace = true }
craft-tiff = { workspace = true }
# Keep `image` (no codec features) only for `imageops::resize` of soft masks in `extract.rs`.
image = { version = "0.25", default-features = false }
thiserror = { workspace = true }
# Already in the graph through lopdf; used only to read plain-text files (decode_text).
encoding_rs = "0.8"
@@ -32,6 +32,5 @@ rustix = { version = "1", features = ["process"] }
[dev-dependencies]
# An independent CCITT decoder, to check Create PDF's Group 4 images.
hayro-ccitt.workspace = true
image = { version = "0.25", default-features = false, features = ["bmp", "gif", "jpeg"] }
hayro-syntax = { workspace = true }
pdfcraft-filters = { workspace = true }
+140 -46
View File
@@ -306,24 +306,50 @@ thread_local! {
/// can't be read, when its metadata would take the decoder quadratic time
/// ([`decodable_metadata`](crate::upright_image::decodable_metadata)).
fn jpeg_size(data: &[u8]) -> Option<(usize, usize)> {
use zune_jpeg::zune_core::{bytestream::ZCursor, options::DecoderOptions};
if !crate::upright_image::decodable_metadata(data) {
return None;
}
let options = DecoderOptions::default().set_strict_mode(false).set_max_width(usize::from(u16::MAX)).set_max_height(usize::from(u16::MAX));
let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(data), options);
dec.decode_headers().ok()?;
dec.dimensions()
let mut i = 2usize;
if !data.starts_with(&[0xFF, 0xD8]) {
return None;
}
while let Some(&b) = data.get(i) {
if b != 0xFF {
i += 1;
continue;
}
let marker = *data.get(i + 1)?;
match marker {
0xFF => i += 1,
0x00 | 0x01 | 0xD0..=0xD7 => i += 2,
0xDA | 0xD9 => break,
0xC0..=0xC3 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => {
let h = usize::from(u16::from_be_bytes([*data.get(i + 5)?, *data.get(i + 6)?]));
let w = usize::from(u16::from_be_bytes([*data.get(i + 7)?, *data.get(i + 8)?]));
return Some((w, h));
}
_ => {
let len = usize::from(u16::from_be_bytes([*data.get(i + 2)?, *data.get(i + 3)?]));
if len < 2 {
return None;
}
i = i.checked_add(2 + len)?;
}
}
}
None
}
/// JPEG `data`, decoded, if its header says it is `w` × `h`.
fn jpeg_of_size(data: &[u8], w: usize, h: usize) -> Option<image::DynamicImage> {
fn jpeg_of_size(data: &[u8], w: usize, h: usize) -> Option<craft_jpeg::Decoded> {
if jpeg_size(data)? != (w, h) {
return None;
}
#[cfg(test)]
JPEGS_DECODED.with(|n| n.set(n.get() + 1));
image::load_from_memory_with_format(data, image::ImageFormat::Jpeg).ok()
let mut limits = craft_jpeg::Limits::default();
limits.max_pixels = (u64::from(u16::MAX)) * (u64::from(u16::MAX));
craft_jpeg::decode(data, &limits, None).ok()
}
/// A JPEG's samples (8 bits): grey for a grey image, CMYK as stored for a CMYK one, else RGB,
@@ -343,17 +369,86 @@ fn jpeg_samples(data: &[u8], space: &Space, w: usize, h: usize) -> Option<(Vec<u
stripped = crate::upright_image::without_extended_xmp(data);
&stripped[..]
};
let img = jpeg_of_size(data, w, h)?;
let dec = jpeg_of_size(data, w, h)?;
let craft_jpeg::Samples::U8(px) = dec.samples else {
return None;
};
Some(match space {
Space::Gray => (img.into_luma8().into_raw(), Space::Gray),
_ => (img.into_rgb8().into_raw(), Space::Rgb),
Space::Gray => {
let gray = match dec.components {
1 => px,
3 => px
.as_chunks::<3>()
.0
.iter()
.map(|&[r, g, b]| ((2126 * u32::from(r) + 7152 * u32::from(g) + 722 * u32::from(b) + 5000) / 10_000) as u8)
.collect(),
4 => px
.as_chunks::<4>()
.0
.iter()
.map(|&[c, m, y, k]| {
let r = 255 - c.saturating_add(k);
let g = 255 - m.saturating_add(k);
let b = 255 - y.saturating_add(k);
((2126 * u32::from(r) + 7152 * u32::from(g) + 722 * u32::from(b) + 5000) / 10_000) as u8
})
.collect(),
_ => return None,
};
(gray, Space::Gray)
}
_ => {
let rgb = match dec.components {
3 => px,
1 => px.into_iter().flat_map(|g| [g, g, g]).collect(),
4 => px
.as_chunks::<4>()
.0
.iter()
.flat_map(|&[c, m, y, k]| [255 - c.saturating_add(k), 255 - m.saturating_add(k), 255 - y.saturating_add(k)])
.collect(),
_ => return None,
};
(rgb, Space::Rgb)
}
})
}
/// A four-component JPEG's CMYK samples as the page reads them: as stored, not inverted the way
/// a decoder converting to RGB inverts them, with YCCK converted to CMYK.
fn adobe_transform(data: &[u8]) -> Option<u8> {
if !data.starts_with(&[0xFF, 0xD8]) {
return None;
}
let mut i = 2usize;
while let Some(&b) = data.get(i) {
if b != 0xFF {
i += 1;
continue;
}
let marker = *data.get(i + 1)?;
match marker {
0xFF => i += 1,
0x00 | 0x01 | 0xD0..=0xD7 => i += 2,
0xDA | 0xD9 => break,
_ => {
let len = usize::from(u16::from_be_bytes([*data.get(i + 2)?, *data.get(i + 3)?]));
if len < 2 {
return None;
}
let seg = data.get(i + 4..i + 2 + len)?;
if marker == 0xEE && seg.starts_with(b"Adobe") && seg.len() >= 12 {
return Some(seg[11]);
}
i = i.checked_add(2 + len)?;
}
}
}
None
}
fn cmyk_jpeg_samples(data: &[u8], w: usize, h: usize) -> Option<Vec<u8>> {
use zune_jpeg::zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions};
// Past the decoder's metadata bound, decode the image without its Extended XMP chunks
// rather than not at all: written out as stored, a CMYK JPEG shows inverted colours.
let stripped;
@@ -363,26 +458,21 @@ fn cmyk_jpeg_samples(data: &[u8], w: usize, h: usize) -> Option<Vec<u8>> {
stripped = crate::upright_image::without_extended_xmp(data);
&stripped[..]
};
let options = DecoderOptions::default().set_strict_mode(false).set_max_width(usize::from(u16::MAX)).set_max_height(usize::from(u16::MAX));
let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(data), options);
dec.decode_headers().ok()?;
let input = dec.input_colorspace()?;
if !matches!(input, ColorSpace::CMYK | ColorSpace::YCCK) || dec.dimensions()? != (w, h) {
let transform = adobe_transform(data);
let mut limits = craft_jpeg::Limits::default();
limits.max_pixels = (u64::from(u16::MAX)) * (u64::from(u16::MAX));
let decoded = craft_jpeg::decode(data, &limits, transform).ok()?;
if decoded.width as usize != w || decoded.height as usize != h || decoded.components != 4 {
return None;
}
dec.set_options(options.jpeg_set_out_colorspace(input));
let mut px = dec.decode().ok()?;
let craft_jpeg::Samples::U8(mut px) = decoded.samples else {
return None;
};
if px.len() != w.checked_mul(h)?.checked_mul(4)? {
return None;
}
if input == ColorSpace::YCCK {
for p in px.as_chunks_mut::<4>().0 {
let (y, cb, cr) = (f32::from(p[0]), f32::from(p[1]) - 128.0, f32::from(p[2]) - 128.0);
let ink = |rgb: f32| (255.0 - rgb).round().clamp(0.0, 255.0) as u8;
p[0] = ink(y + 1.402 * cr);
p[1] = ink(y - 0.344_136 * cb - 0.714_136 * cr);
p[2] = ink(y + 1.772 * cb);
}
for b in &mut px {
*b = 255 - *b;
}
Some(px)
}
@@ -588,7 +678,20 @@ fn gray_plane(doc: &Document, m: &Stream, w: usize, h: usize, budget: &mut Image
if !budget.pay_jpeg((mw as u64).saturating_mul(mh as u64), &m.raw) {
return None;
}
jpeg_of_size(&m.raw, mw, mh)?.into_luma8().into_raw()
let dec = jpeg_of_size(&m.raw, mw, mh)?;
let craft_jpeg::Samples::U8(px) = dec.samples else {
return None;
};
match dec.components {
1 => px,
3 => px
.as_chunks::<3>()
.0
.iter()
.map(|&[r, g, b]| ((2126 * u32::from(r) + 7152 * u32::from(g) + 722 * u32::from(b) + 5000) / 10_000) as u8)
.collect(),
_ => return None,
}
}
Some(b"DCTDecode" | b"DCT" | b"JPXDecode") => return None,
_ => {
@@ -635,23 +738,14 @@ pub(crate) fn scaled(img: &image::GrayImage, w: u32, h: u32) -> image::GrayImage
}
fn png(w: u32, h: u32, rgb: &[u8], alpha: Option<&[u8]>) -> Result<Vec<u8>, String> {
let mut out = Vec::new();
{
let mut enc = png::Encoder::new(&mut out, w, h);
enc.set_depth(png::BitDepth::Eight);
enc.set_compression(png::Compression::Fast);
let data: Vec<u8> = match alpha {
Some(a) => {
enc.set_color(png::ColorType::Rgba);
rgb.as_chunks::<3>().0.iter().zip(a).flat_map(|(c, a)| [c[0], c[1], c[2], *a]).collect()
}
None => {
enc.set_color(png::ColorType::Rgb);
rgb.to_vec()
}
};
let mut wr = enc.write_header().map_err(|e| e.to_string())?;
wr.write_image_data(&data).map_err(|e| e.to_string())?;
}
Ok(out)
let (layout, data) = match alpha {
Some(a) => (
craft_image_codecs::ChannelLayout::Rgba,
rgb.as_chunks::<3>().0.iter().zip(a).flat_map(|(c, a)| [c[0], c[1], c[2], *a]).collect::<Vec<u8>>(),
),
None => (craft_image_codecs::ChannelLayout::Rgb, rgb.to_vec()),
};
let img = craft_image_codecs::Image::from_u8(w, h, layout, data).map_err(|e| e.to_string())?;
let opts = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() };
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &opts).map_err(|e| e.to_string())
}
+97 -87
View File
@@ -8,6 +8,7 @@ mod extract;
mod text_font;
mod upright_image;
pub use extract::{ExtractedImage, ImageExport, extract_images, image_file, image_file_within};
pub use upright_image::jpeg_exif_orientation;
mod office;
#[cfg(not(target_arch = "wasm32"))]
pub use office::libreoffice;
@@ -427,36 +428,31 @@ fn jpeg(name: &str, bytes: &[u8]) -> Result<Embedded, CreateError> {
fn png_image(name: &str, bytes: &[u8]) -> Result<Embedded, CreateError> {
let bad = |m: String| CreateError::Image(name.into(), m);
let mut dec = png::Decoder::new(std::io::Cursor::new(bytes));
dec.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16);
let mut reader = dec.read_info().map_err(|e| bad(e.to_string()))?;
let dpi = match reader.info().pixel_dims {
Some(png::PixelDimensions { xppu, yppu, unit: png::Unit::Meter }) if xppu > 0 && yppu > 0 => (xppu as f64 * 0.0254, yppu as f64 * 0.0254),
_ => (72.0, 72.0),
let opts = craft_image_codecs::DecodeOptions {
keep_orientation: true,
limits: craft_image_codecs::Limits { max_alloc: MAX_DECODED_BYTES as u64, ..Default::default() },
..Default::default()
};
let icc = reader.info().icc_profile.as_ref().map(|p| p.to_vec());
// The decoded size comes from the header alone, so a file of a few bytes can claim any
// size: keep the ceiling, and ask for the memory fallibly (a failed allocation would abort
// the whole app, and no `catch_unwind` stops that).
// Flate expands at most 1032:1 and decoding at most 32:1 more (1-bit palette to RGBA), so a
// file cannot honestly hold more than that: refuse a header the bytes cannot back before
// zero-filling a buffer of its size. The slack keeps tiny real images out of the check.
let size = reader
.output_buffer_size()
.filter(|n| *n <= MAX_DECODED_BYTES && *n <= bytes.len().saturating_mul(PNG_MAX_EXPANSION).saturating_add(PNG_SLACK))
.ok_or_else(|| bad("image too large".into()))?;
let mut buf = Vec::new();
buf.try_reserve_exact(size).map_err(|_| bad("not enough memory for this image".into()))?;
buf.resize(size, 0);
let frame = reader.next_frame(&mut buf).map_err(|e| bad(e.to_string()))?;
let (w, h) = (frame.width, frame.height);
let data = &buf[..frame.buffer_size()];
let (channels, cs) = match frame.color_type {
png::ColorType::Grayscale => (1, "DeviceGray"),
png::ColorType::GrayscaleAlpha => (2, "DeviceGray"),
png::ColorType::Rgb => (3, "DeviceRGB"),
png::ColorType::Rgba => (4, "DeviceRGB"),
png::ColorType::Indexed => return Err(bad("unexpected palette output".into())),
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, bytes, &opts).map_err(|e| match e {
craft_image_codecs::CodecError::LimitExceeded(m) => bad(format!("image too large: {m}")),
other => bad(other.to_string()),
})?;
let dpi = img.meta.dpi.map(|(x, y)| (f64::from(x), f64::from(y))).filter(|&(x, y)| x > 0.0 && y > 0.0).unwrap_or((72.0, 72.0));
let icc = img.icc.clone();
let (w, h) = (img.width(), img.height());
let (channels, cs) = match img.layout() {
craft_image_codecs::ChannelLayout::Gray => (1, "DeviceGray"),
craft_image_codecs::ChannelLayout::GrayA => (2, "DeviceGray"),
craft_image_codecs::ChannelLayout::Rgb => (3, "DeviceRGB"),
craft_image_codecs::ChannelLayout::Rgba => (4, "DeviceRGB"),
_ => return Err(bad("unexpected palette output".into())),
};
let data: std::borrow::Cow<'_, [u8]> = if let Some(u8s) = img.as_u8() {
std::borrow::Cow::Borrowed(u8s)
} else if let Some(u16s) = img.to_u16_samples() {
std::borrow::Cow::Owned(u16s.into_iter().map(|v| (v >> 8) as u8).collect())
} else {
return Err(bad("expected 8-bit or 16-bit PNG samples".into()));
};
let has_alpha = channels == 2 || channels == 4;
let colour_n = if has_alpha { channels - 1 } else { channels };
@@ -516,20 +512,22 @@ fn rgba_image(rgba: &[u8], (w, h): (u32, u32), dpi: (f64, f64)) -> Embedded {
Embedded { dict: d, data: colour, filtered: false, smask, icc: None, px: (w, h), dpi }
}
/// BMP and GIF (the first frame), through the `image` decoders. Their resolution is not read:
/// BMP and GIF (the first frame), through `craft-image-codecs`. Their resolution is not read:
/// 72 dpi, as for images without one.
fn decoded(name: &str, bytes: &[u8], format: image::ImageFormat) -> Result<Embedded, CreateError> {
fn decoded(name: &str, bytes: &[u8], format: craft_image_codecs::Format) -> Result<Embedded, CreateError> {
// A GIF's first frame is painted on a canvas the size of its logical screen, however small
// the frame: a few bytes can ask for hundreds of MB.
if format == image::ImageFormat::Gif
if format == craft_image_codecs::Format::Gif
&& let Some(&[w0, w1, h0, h1]) = bytes.get(6..10)
&& u64::from(u16::from_le_bytes([w0, w1])) * u64::from(u16::from_le_bytes([h0, h1])) > MAX_GIF_PIXELS
{
return Err(CreateError::Image(name.into(), "the GIF's screen is larger than 4096×4096 pixels".into()));
}
let img = image::load_from_memory_with_format(bytes, format).map_err(|e| CreateError::Image(name.into(), e.to_string()))?;
let rgba = img.into_rgba8();
Ok(rgba_image(rgba.as_raw(), rgba.dimensions(), (72.0, 72.0)))
let img = craft_image_codecs::decode_as_with(format, bytes, &craft_image_codecs::DecodeOptions::default())
.map_err(|e| CreateError::Image(name.into(), e.to_string()))?;
let dims = (img.width(), img.height());
let raw = img.to_rgba8();
Ok(rgba_image(&raw, dims, (72.0, 72.0)))
}
fn le16(b: &[u8], at: usize) -> Option<u16> {
@@ -662,16 +660,28 @@ const MAX_GIF_PIXELS: u64 = 1 << 24;
/// twice over.
const MAX_DECODED_BYTES: usize = 2 << 30;
/// Most a PNG's decoded size can exceed its file size: Flate's 1032:1 times the 32:1 of
/// expanding a 1-bit palette to RGBA, plus a fixed allowance for small files.
const PNG_MAX_EXPANSION: usize = 1032 * 32;
const PNG_SLACK: usize = 1 << 20;
/// Most decoded bytes all the pages of one TIFF may take together: its pages stay in memory until
/// they are embedded, and many small directories can share one compressed strip. Saturates to the
/// whole address space on 32-bit targets (wasm32), where 4 GiB doesn't fit in `usize`.
const MAX_DECODED_TOTAL: usize = MAX_DECODED_BYTES.saturating_mul(2);
/// Returns whether the TIFF directory at `ifd_offset` is a 1-bit bilevel grayscale image.
fn tiff_page_is_bilevel(bytes: &[u8], ifd_offset: u64) -> bool {
let order = match bytes.get(0..2) {
Some(b"II") => craft_tiff::ByteOrder::Little,
Some(b"MM") => craft_tiff::ByteOrder::Big,
_ => return false,
};
let bigtiff = order.read_u16(bytes, 2) == Some(43);
let Ok((ifd, _)) = craft_tiff::parse_ifd_at(bytes, ifd_offset, order, 0, bigtiff, &craft_tiff::ParseOptions::default()) else {
return false;
};
let Some(bits) = ifd.u64s(craft_tiff::tags::BITS_PER_SAMPLE) else {
return false;
};
bits.len() == 1 && bits[0] == 1
}
/// The first `wanted` pages of a TIFF (multi-page scans become multi-page PDFs).
fn tiff_pages(name: &str, bytes: &[u8], wanted: usize) -> Result<Vec<Embedded>, CreateError> {
tiff_pages_within(name, bytes, wanted, MAX_DECODED_TOTAL)
@@ -679,67 +689,71 @@ fn tiff_pages(name: &str, bytes: &[u8], wanted: usize) -> Result<Vec<Embedded>,
/// [`tiff_pages`], refused once the pages kept take more than `budget` bytes.
fn tiff_pages_within(name: &str, bytes: &[u8], wanted: usize, budget: usize) -> Result<Vec<Embedded>, CreateError> {
use tiff::ColorType as C;
use tiff::decoder::{Decoder, DecodingResult, Limits};
use tiff::tags::Tag;
use craft_image_codecs::ChannelLayout as L;
let bad = |m: String| CreateError::Image(name.into(), m);
// The tiff crate stops at 256 MiB of decoded pixels, below an ordinary poster scan (#665):
// 12000x16000 CMYK is 768 MiB. Raise it, but keep a ceiling so a tiny file claiming
// enormous dimensions can't ask for unbounded memory.
let mut limits = Limits::default();
limits.decoding_buffer_size = MAX_DECODED_BYTES;
let mut dec = Decoder::new(std::io::Cursor::new(bytes)).map_err(|e| bad(e.to_string()))?.with_limits(limits);
let info = craft_image_codecs::tiff_info(bytes).map_err(|e| bad(e.to_string()))?;
let opts = craft_image_codecs::DecodeOptions {
keep_orientation: true,
limits: craft_image_codecs::Limits { max_alloc: MAX_DECODED_BYTES as u64, ..Default::default() },
..Default::default()
};
let mut out = Vec::new();
let mut kept = 0usize;
loop {
let (w, h) = dec.dimensions().map_err(|e| bad(e.to_string()))?;
let ct = dec.colortype().map_err(|e| bad(e.to_string()))?;
let unit = dec.get_tag_u32(Tag::ResolutionUnit).unwrap_or(2);
let res = |t: Tag, dec: &mut Decoder<std::io::Cursor<&[u8]>>| -> f64 {
// Resolutions are rationals.
let v = match dec.get_tag(t) {
Ok(tiff::decoder::ifd::Value::Rational(n, d)) if d != 0 => f64::from(n) / f64::from(d),
Ok(other) => other.into_f64().unwrap_or(72.0),
Err(_) => 72.0,
};
let v = if unit == 3 { v * 2.54 } else { v };
if v.is_finite() && v >= 1.0 { v } else { 72.0 }
for (idx, p) in info.pages.iter().enumerate() {
if out.len() >= wanted {
break;
}
if p.kind == craft_image_codecs::TiffPageKind::ReducedResolution {
continue;
}
let img = craft_image_codecs::decode_tiff_page(bytes, idx, &opts).map_err(|e| bad(e.to_string()))?;
let (w, h) = (img.width(), img.height());
let dpi = img.meta.dpi.map(|(x, y)| (f64::from(x), f64::from(y))).filter(|&(x, y)| x > 0.0 && y > 0.0).unwrap_or((72.0, 72.0));
let data_buf: std::borrow::Cow<'_, [u8]> = if let Some(u8s) = img.as_u8() {
std::borrow::Cow::Borrowed(u8s)
} else if let Some(u16s) = img.to_u16_samples() {
std::borrow::Cow::Owned(u16s.into_iter().map(|v| (v >> 8) as u8).collect())
} else {
return Err(bad("this TIFF sample format isn't supported".into()));
};
let dpi = (res(Tag::XResolution, &mut dec), res(Tag::YResolution, &mut dec));
let data = match dec.read_image().map_err(|e| bad(e.to_string()))? {
DecodingResult::U8(v) => v,
// 16-bit samples: keep the high byte.
DecodingResult::U16(v) => v.iter().map(|x| (x >> 8) as u8).collect(),
_ => return Err(bad("this TIFF sample format isn't supported".into())),
};
let page = match ct {
C::Gray(1) => {
// Bilevel scans stay 1 bit per pixel (0 = black after the decoder's inversion).
let data = data_buf.as_ref();
let page = match img.layout() {
L::Gray if tiff_page_is_bilevel(bytes, p.ifd_offset) => {
// Bilevel scans stay 1 bit per pixel (0 = black).
let row_bytes = (w as usize).div_ceil(8);
let mut packed = vec![0u8; row_bytes.saturating_mul(h as usize)];
for y in 0..h as usize {
for x in 0..w as usize {
if data[y * w as usize + x] >= 128 {
packed[y * row_bytes + (x >> 3)] |= 0x80 >> (x & 7);
}
}
}
let mut d = Dict::new();
d.set(b"ColorSpace".to_vec(), Object::name("DeviceGray"));
d.set(b"BitsPerComponent".to_vec(), Object::Int(1));
Embedded { dict: d, data, filtered: false, smask: None, icc: None, px: (w, h), dpi }
Embedded { dict: d, data: packed, filtered: false, smask: None, icc: None, px: (w, h), dpi }
}
C::Gray(8 | 16) => {
L::Gray => {
let mut d = Dict::new();
d.set(b"ColorSpace".to_vec(), Object::name("DeviceGray"));
d.set(b"BitsPerComponent".to_vec(), Object::Int(8));
Embedded { dict: d, data, filtered: false, smask: None, icc: None, px: (w, h), dpi }
Embedded { dict: d, data: data.to_vec(), filtered: false, smask: None, icc: None, px: (w, h), dpi }
}
C::RGB(8 | 16) => {
L::Rgb => {
let mut d = Dict::new();
d.set(b"ColorSpace".to_vec(), Object::name("DeviceRGB"));
d.set(b"BitsPerComponent".to_vec(), Object::Int(8));
Embedded { dict: d, data, filtered: false, smask: None, icc: None, px: (w, h), dpi }
Embedded { dict: d, data: data.to_vec(), filtered: false, smask: None, icc: None, px: (w, h), dpi }
}
C::CMYK(8 | 16) => {
L::Cmyk => {
let mut d = Dict::new();
d.set(b"ColorSpace".to_vec(), Object::name("DeviceCMYK"));
d.set(b"BitsPerComponent".to_vec(), Object::Int(8));
Embedded { dict: d, data, filtered: false, smask: None, icc: None, px: (w, h), dpi }
Embedded { dict: d, data: data.to_vec(), filtered: false, smask: None, icc: None, px: (w, h), dpi }
}
C::RGBA(8 | 16) => rgba_image(&data, (w, h), dpi),
C::GrayA(8 | 16) => {
L::Rgba => rgba_image(data, (w, h), dpi),
L::GrayA => {
let rgba: Vec<u8> = data.as_chunks::<2>().0.iter().flat_map(|p| [p[0], p[0], p[0], p[1]]).collect();
rgba_image(&rgba, (w, h), dpi)
}
@@ -750,10 +764,6 @@ fn tiff_pages_within(name: &str, bytes: &[u8], wanted: usize, budget: usize) ->
return Err(bad(format!("the TIFF's pages take more than {} MiB together", budget >> 20)));
}
out.push(page);
if out.len() >= wanted || !dec.more_images() {
break;
}
dec.next_image().map_err(|e| bad(e.to_string()))?;
}
Ok(out)
}
@@ -835,9 +845,9 @@ fn embed(name: &str, bytes: &[u8], wanted: usize) -> Result<Vec<Embedded>, Creat
} else if is_tiff(bytes) {
tiff_pages(name, bytes, wanted)
} else if bytes.starts_with(b"BM") {
Ok(vec![decoded(name, &pdfcraft_xfa::image::bmp_with_pixel_offset(bytes), image::ImageFormat::Bmp)?])
Ok(vec![decoded(name, &pdfcraft_xfa::image::bmp_with_pixel_offset(bytes), craft_image_codecs::Format::Bmp)?])
} else if is_gif(bytes) {
Ok(vec![decoded(name, bytes, image::ImageFormat::Gif)?])
Ok(vec![decoded(name, bytes, craft_image_codecs::Format::Gif)?])
} else if is_pcx(bytes) {
Ok(vec![pcx(name, bytes)?])
} else if bytes.starts_with(JP2_SIGNATURE) || bytes.starts_with(&[0xFF, 0x4F, 0xFF, 0x51]) {
+246 -141
View File
@@ -22,21 +22,15 @@ fn media(d: &Dict) -> Vec<f64> {
/// A tiny PNG: 4×2 RGBA with one transparent pixel, 144 dpi.
fn png_bytes(alpha: bool) -> Vec<u8> {
let mut out = Vec::new();
{
let mut enc = png::Encoder::new(&mut out, 4, 2);
enc.set_color(if alpha { png::ColorType::Rgba } else { png::ColorType::Rgb });
enc.set_depth(png::BitDepth::Eight);
enc.set_pixel_dims(Some(png::PixelDimensions { xppu: 5669, yppu: 5669, unit: png::Unit::Meter }));
let mut w = enc.write_header().unwrap();
let n = if alpha { 4 } else { 3 };
let mut data = vec![200u8; 8 * n];
if alpha {
data[3] = 0;
}
w.write_image_data(&data).unwrap();
let layout = if alpha { craft_image_codecs::ChannelLayout::Rgba } else { craft_image_codecs::ChannelLayout::Rgb };
let n = if alpha { 4 } else { 3 };
let mut data = vec![200u8; 8 * n];
if alpha {
data[3] = 0;
}
out
let mut img = craft_image_codecs::Image::from_u8(4, 2, layout, data).unwrap();
img.meta.dpi = Some((144.0, 144.0));
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()
}
/// A minimal baseline JPEG header (SOI, JFIF at 300 dpi, SOF0 3×2 RGB, EOI). Only the headers
@@ -288,28 +282,43 @@ fn image_of(doc: &Document, page: usize) -> Dict {
#[test]
fn bmp_gif_and_multi_page_tiff_images() {
use image::{ImageEncoder, Rgba, RgbaImage};
// BMP: 3×2 opaque colour.
let mut bmp = Vec::new();
image::codecs::bmp::BmpEncoder::new(&mut bmp).write_image(&[10, 20, 30].repeat(6), 3, 2, image::ExtendedColorType::Rgb8).unwrap();
let bmp_img = craft_image_codecs::Image::from_u8(3, 2, craft_image_codecs::ChannelLayout::Rgb, [10, 20, 30].repeat(6)).unwrap();
let bmp = craft_image_codecs::encode(&bmp_img, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default()).unwrap();
// GIF: 2×2 with a transparent pixel.
let mut gif = Vec::new();
{
let mut img = RgbaImage::from_pixel(2, 2, Rgba([255, 0, 0, 255]));
img.put_pixel(0, 0, Rgba([0, 0, 0, 0]));
let mut enc = image::codecs::gif::GifEncoder::new(&mut gif);
enc.encode(img.as_raw(), 2, 2, image::ExtendedColorType::Rgba8).unwrap();
}
let gif_img = craft_image_codecs::Image::from_u8(
2,
2,
craft_image_codecs::ChannelLayout::Rgba,
vec![0, 0, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255],
)
.unwrap();
let gif = craft_image_codecs::encode(&gif_img, craft_image_codecs::Format::Gif, &craft_image_codecs::EncodeOptions::default()).unwrap();
// TIFF: two 8-bit gray pages, one at 144 dpi and one of only black and white.
let mut tif = std::io::Cursor::new(Vec::new());
{
let mut enc = tiff::encoder::TiffEncoder::new(&mut tif).unwrap();
let mut im = enc.new_image::<tiff::encoder::colortype::Gray8>(4, 2).unwrap();
im.resolution(tiff::tags::ResolutionUnit::Inch, tiff::encoder::Rational { n: 144, d: 1 });
im.write_data(&[128; 8]).unwrap();
enc.write_image::<tiff::encoder::colortype::Gray8>(2, 2, &[0, 255, 255, 0]).unwrap();
}
let doc = from_images(&[("a.bmp".into(), bmp), ("b.gif".into(), gif), ("scan.tif".into(), tif.into_inner())]).unwrap();
let tif = {
let w = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false);
let mut p1 = craft_tiff::IfdBuilder::new()
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![4]))
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![2]))
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8]))
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![1]))
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![1]))
.with(craft_tiff::tags::RESOLUTION_UNIT, craft_tiff::Value::Short(vec![2]))
.with(craft_tiff::tags::X_RESOLUTION, craft_tiff::Value::Rational(vec![(144, 1)]))
.with(craft_tiff::tags::Y_RESOLUTION, craft_tiff::Value::Rational(vec![(144, 1)]));
p1.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 2, strips: vec![vec![128; 8]] });
let mut p2 = craft_tiff::IfdBuilder::new()
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![2]))
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![2]))
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8]))
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![1]))
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![1]));
p2.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 2, strips: vec![vec![0, 255, 255, 0]] });
w.write(&[p1, p2]).unwrap()
};
let doc = from_images(&[("a.bmp".into(), bmp), ("b.gif".into(), gif), ("scan.tif".into(), tif)]).unwrap();
let doc = reopen(&doc);
assert_eq!(pages(&doc).len(), 4, "one page per BMP and GIF, two for the TIFF");
assert_eq!(media(&pages(&doc)[0]), [0.0, 0.0, 3.0, 2.0]);
@@ -324,10 +333,9 @@ fn bmp_gif_and_multi_page_tiff_images() {
/// the intact file, not shifted by the header bytes.
#[test]
fn a_bmp_with_a_zero_pixel_offset_is_placed_like_the_intact_file() {
use image::ImageEncoder;
let mut bmp = Vec::new();
let pixels: Vec<u8> = (0..18).map(|i| i * 13).collect();
image::codecs::bmp::BmpEncoder::new(&mut bmp).write_image(&pixels, 3, 2, image::ExtendedColorType::Rgb8).unwrap();
let bmp_img = craft_image_codecs::Image::from_u8(3, 2, craft_image_codecs::ChannelLayout::Rgb, pixels).unwrap();
let bmp = craft_image_codecs::encode(&bmp_img, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default()).unwrap();
let mut zero = bmp.clone();
zero[10..14].copy_from_slice(&0u32.to_le_bytes());
let samples = |data: Vec<u8>| {
@@ -417,18 +425,39 @@ fn a_small_png_claiming_a_large_frame_is_refused_before_zero_filling() {
}
/// Likewise a PNG: the png crate's limits don't cover the frame buffer, which its header sizes
fn png_crc32(data: &[u8]) -> u32 {
let mut crc = 0xFFFF_FFFFu32;
for &b in data {
crc ^= u32::from(b);
for _ in 0..8 {
let mask = (crc & 1).wrapping_neg();
crc = (crc >> 1) ^ (0xEDB8_8320 & mask);
}
}
!crc
}
fn push_png_chunk(out: &mut Vec<u8>, kind: [u8; 4], data: &[u8]) {
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
let start = out.len();
out.extend_from_slice(&kind);
out.extend_from_slice(data);
let crc = png_crc32(&out[start..]);
out.extend_from_slice(&crc.to_be_bytes());
}
/// (60000x60000 RGBA is 14.4 GB). Refused before anything is allocated, not after.
#[test]
fn a_png_claiming_enormous_dimensions_is_refused() {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 60_000, 60_000);
enc.set_color(png::ColorType::Rgba);
enc.set_depth(png::BitDepth::Eight);
let mut w = enc.write_header().unwrap();
// A zlib stream holding one zero byte: far too little data, but a well-formed start.
w.write_chunk(png::chunk::IDAT, &[0x78, 0x01, 0x01, 0x01, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0x01, 0x00, 0x01]).unwrap();
}
let mut png = b"\x89PNG\r\n\x1a\n".to_vec();
let mut ihdr = Vec::with_capacity(13);
ihdr.extend_from_slice(&60_000u32.to_be_bytes());
ihdr.extend_from_slice(&60_000u32.to_be_bytes());
ihdr.extend_from_slice(&[8, 6, 0, 0, 0]);
push_png_chunk(&mut png, *b"IHDR", &ihdr);
// A zlib stream holding one zero byte: far too little data, but a well-formed start.
push_png_chunk(&mut png, *b"IDAT", &[0x78, 0x01, 0x01, 0x01, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0x01, 0x00, 0x01]);
push_png_chunk(&mut png, *b"IEND", &[]);
match from_images(&[("huge.png".into(), png)]) {
Err(CreateError::Image(_, m)) => assert!(m.contains("too large"), "{m}"),
other => panic!("expected a refusal, got {:?}", other.map(|_| ())),
@@ -446,11 +475,10 @@ fn images_export_as_jpeg_unchanged_and_others_as_png() {
assert_eq!(kinds, [(0, "png", 4, 2), (1, "jpg", 3, 2), (2, "png", 4, 2)]);
assert_eq!(out.images[1].data, jpeg_bytes(), "JPEG data is written as is");
// The PNG round-trips the pixels, with the soft mask as alpha.
let dec = png::Decoder::new(std::io::Cursor::new(out.images[0].data.clone()));
let mut r = dec.read_info().unwrap();
let mut buf = vec![0; r.output_buffer_size().unwrap()];
let info = r.next_frame(&mut buf).unwrap();
assert_eq!(info.color_type, png::ColorType::Rgba);
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, &out.images[0].data, &craft_image_codecs::DecodeOptions::default())
.unwrap();
assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgba);
let buf = img.as_u8().unwrap();
assert_eq!(&buf[..8], &[200, 200, 200, 0, 200, 200, 200, 200]);
// Pages filter; small images can be left out.
assert_eq!(extract_images(&doc, &[2], 0).images.len(), 1);
@@ -482,10 +510,9 @@ fn indexed_and_one_bit_images_decode() {
);
doc.set(r, Object::Stream(Stream::from_raw(d, vec![0b0101_0000])));
let out = extract_images(&doc, &[0], 0);
let dec = png::Decoder::new(std::io::Cursor::new(out.images[0].data.clone()));
let mut rd = dec.read_info().unwrap();
let mut buf = vec![0; rd.output_buffer_size().unwrap()];
rd.next_frame(&mut buf).unwrap();
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, &out.images[0].data, &craft_image_codecs::DecodeOptions::default())
.unwrap();
let buf = img.as_u8().unwrap();
assert_eq!(&buf[..12], &[255, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 255]);
}
@@ -570,14 +597,10 @@ fn source_kind_tells_pdfs_images_and_text_apart() {
/// BigTIFF (version 43, used for large scans) is detected and converted like a classic TIFF.
#[test]
fn bigtiff_images_become_pages() {
let mut tif = std::io::Cursor::new(Vec::new());
{
let mut enc = tiff::encoder::TiffEncoder::new_big(&mut tif).unwrap();
let mut im = enc.new_image::<tiff::encoder::colortype::Gray8>(4, 2).unwrap();
im.resolution(tiff::tags::ResolutionUnit::Inch, tiff::encoder::Rational { n: 144, d: 1 });
im.write_data(&[128; 8]).unwrap();
}
let tif = tif.into_inner();
let mut img = craft_image_codecs::Image::from_u8(4, 2, craft_image_codecs::ChannelLayout::Gray, vec![128; 8]).unwrap();
img.meta.dpi = Some((144.0, 144.0));
let opts = craft_image_codecs::EncodeOptions { tiff_bigtiff: true, ..Default::default() };
let tif = craft_image_codecs::encode(&img, craft_image_codecs::Format::Tiff, &opts).unwrap();
assert!(tif.starts_with(b"II+\0"), "the encoder wrote a BigTIFF");
assert_eq!(source_kind("scan.tif", &tif), Some(SourceKind::Image));
let doc = reopen(&from_images(&[("scan.tif".into(), tif)]).unwrap());
@@ -768,15 +791,9 @@ fn embedded_icc_profiles_tag_the_image_colour_space() {
let jpeg = cmyk_jpeg_bytes(&cmyk);
let rgb = icc_profile(b"RGB ");
let png = {
let mut out = Vec::new();
let mut info = png::Info::with_size(2, 1);
info.color_type = png::ColorType::Rgb;
info.bit_depth = png::BitDepth::Eight;
info.icc_profile = Some(rgb.clone().into());
let mut w = png::Encoder::with_info(&mut out, info).unwrap().write_header().unwrap();
w.write_image_data(&[10, 20, 30, 40, 50, 60]).unwrap();
w.finish().unwrap();
out
let mut img = craft_image_codecs::Image::from_u8(2, 1, craft_image_codecs::ChannelLayout::Rgb, vec![10, 20, 30, 40, 50, 60]).unwrap();
img.icc = Some(rgb.clone());
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()
};
let doc = reopen(&from_images(&[("a.jpg".into(), jpeg.clone()), ("b.jpg".into(), jpeg.clone()), ("c.png".into(), png)]).unwrap());
let (n, alt, data, first) = icc_of(&doc, &image_of(&doc, 0)).expect("the CMYK JPEG is ICC-tagged");
@@ -845,20 +862,9 @@ fn exported_rgba(doc: &Document) -> (&'static str, Vec<u8>) {
let out = extract_images(doc, &[0], 0);
assert!(out.skipped.is_empty(), "{:?}", out.skipped);
let img = &out.images[0];
if img.extension == "jpg" {
return ("jpg", image::load_from_memory(&img.data).unwrap().into_rgba8().into_raw());
}
let dec = png::Decoder::new(std::io::Cursor::new(img.data.clone()));
let mut r = dec.read_info().unwrap();
let mut buf = vec![0; r.output_buffer_size().unwrap()];
let info = r.next_frame(&mut buf).unwrap();
buf.truncate(info.buffer_size());
let rgba = match info.color_type {
png::ColorType::Rgba => buf,
png::ColorType::Rgb => buf.as_chunks::<3>().0.iter().flat_map(|c| [c[0], c[1], c[2], 255]).collect(),
other => panic!("unexpected {other:?}"),
};
("png", rgba)
let ext = if img.extension == "jpg" { "jpg" } else { "png" };
let decoded = craft_image_codecs::decode(&img.data).unwrap().to_rgba8();
(ext, decoded)
}
fn alphas(rgba: &[u8]) -> Vec<u8> {
@@ -867,9 +873,9 @@ fn alphas(rgba: &[u8]) -> Vec<u8> {
/// A 4×2 grey JPEG (mid grey).
fn real_jpeg() -> Vec<u8> {
let mut out = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 95).encode(&[128u8; 8], 4, 2, image::ExtendedColorType::L8).unwrap();
out
let img = craft_image_codecs::Image::from_u8(4, 2, craft_image_codecs::ChannelLayout::Gray, vec![128u8; 8]).unwrap();
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 95, ..Default::default() };
craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap()
}
#[test]
@@ -1106,12 +1112,8 @@ fn indexed_image_export_clamps_samples_to_hival() {
}
fn exported_png_pixels(bytes: &[u8]) -> Vec<u8> {
let decoder = png::Decoder::new(std::io::Cursor::new(bytes));
let mut reader = decoder.read_info().unwrap();
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
let info = reader.next_frame(&mut pixels).unwrap();
pixels.truncate(info.buffer_size());
pixels
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, bytes, &craft_image_codecs::DecodeOptions::default()).unwrap();
img.as_u8().unwrap().to_vec()
}
fn indexed_export_fixture(
@@ -1483,14 +1485,17 @@ fn cmyk_and_ycck_jpegs_export_in_the_colours_the_page_shows() {
/// A 60 x 20 JPEG at `dpi` x 72 dpi, tagged with EXIF `orientation` (6: shown turned 90 degrees
/// clockwise).
pub(crate) fn phone_jpeg(orientation: u8, dpi: u16) -> Vec<u8> {
use image::codecs::jpeg::{JpegEncoder, PixelDensity, PixelDensityUnit};
let mut jpeg = Vec::new();
let mut encoder = JpegEncoder::new(&mut jpeg);
encoder.set_pixel_density(PixelDensity { density: (dpi, 72), unit: PixelDensityUnit::Inches });
encoder.encode_image(&image::RgbImage::from_pixel(60, 20, image::Rgb([20, 20, 20]))).unwrap();
let rgb = craft_image_codecs::Image::from_u8(60, 20, craft_image_codecs::ChannelLayout::Rgb, vec![20; 60 * 20 * 3]).unwrap();
let jpeg = craft_image_codecs::encode(&rgb, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap();
let mut exif = b"Exif\0\0II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0".to_vec();
exif.extend_from_slice(&[orientation, 0, 0, 0, 0, 0, 0, 0]);
let mut out = jpeg[..2].to_vec();
// JFIF APP0 with pixel density in inches (`dpi` x 72 dpi).
out.extend_from_slice(&[0xff, 0xe0, 0x00, 0x10]);
out.extend_from_slice(b"JFIF\0\x01\x01\x01");
out.extend_from_slice(&dpi.to_be_bytes());
out.extend_from_slice(&72u16.to_be_bytes());
out.extend_from_slice(&[0, 0]);
out.extend_from_slice(&[0xff, 0xe1]);
out.extend_from_slice(&((exif.len() + 2) as u16).to_be_bytes());
out.extend_from_slice(&exif);
@@ -1573,15 +1578,34 @@ fn a_one_bit_tiff_is_stored_as_group_4() {
/// A TIFF of `pages` 64×64 gray pages, then (with `unreadable`) one whose samples Create can't read.
fn gray_tiff(pages: usize, unreadable: bool) -> Vec<u8> {
let mut tif = std::io::Cursor::new(Vec::new());
let mut enc = tiff::encoder::TiffEncoder::new(&mut tif).unwrap();
let mut ifds = Vec::with_capacity(pages + usize::from(unreadable));
for _ in 0..pages {
enc.write_image::<tiff::encoder::colortype::Gray8>(64, 64, &[128; 64 * 64]).unwrap();
let mut ifd = craft_tiff::IfdBuilder::new()
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![64]))
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![64]))
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8]))
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![1]))
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![1]));
ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 64, strips: vec![vec![128; 64 * 64]] });
ifds.push(ifd);
}
if unreadable {
enc.write_image::<tiff::encoder::colortype::RGB32Float>(1, 1, &[0.5; 3]).unwrap();
// 32-bit IEEE float RGB (SampleFormat = 3), unreadable by Create.
let mut ifd = craft_tiff::IfdBuilder::new()
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![1]))
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![1]))
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![32, 32, 32]))
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![2]))
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![3]))
.with(craft_tiff::tags::SAMPLE_FORMAT, craft_tiff::Value::Short(vec![3, 3, 3]));
let f = 0.5f32.to_le_bytes();
let px: Vec<u8> = [f, f, f].concat();
ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 1, strips: vec![px] });
ifds.push(ifd);
}
tif.into_inner()
craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&ifds).unwrap()
}
/// Many small IFDs can each decode to a large page: the pages a TIFF keeps in memory together
@@ -1615,18 +1639,79 @@ fn a_tiny_gif_on_a_huge_screen_is_refused() {
assert_eq!(media(&pages(&doc)[0]), [0.0, 0.0, 40.0, 30.0]);
}
/// A PNG of `w` × `h` samples `data` in `color` at `depth`, with a palette or an ICC profile.
fn png_of(w: u32, h: u32, color: png::ColorType, depth: png::BitDepth, data: &[u8], palette: Option<Vec<u8>>, icc: Option<Vec<u8>>) -> Vec<u8> {
let mut out = Vec::new();
{
let mut info = png::Info::with_size(w, h);
info.color_type = color;
info.bit_depth = depth;
info.palette = palette.map(Into::into);
info.icc_profile = icc.map(Into::into);
let mut writer = png::Encoder::with_info(&mut out, info).unwrap().write_header().unwrap();
writer.write_image_data(data).unwrap();
#[derive(Clone, Copy)]
enum TestPngColor {
Grayscale,
Rgb,
Indexed,
}
#[derive(Clone, Copy)]
enum TestPngDepth {
One,
Eight,
}
fn zlib_uncompressed(data: &[u8]) -> Vec<u8> {
let mut out = vec![0x78, 0x01];
let chunks: Vec<&[u8]> = if data.is_empty() { vec![&[][..]] } else { data.chunks(65_535).collect() };
for (i, chunk) in chunks.iter().enumerate() {
let last = u8::from(i + 1 == chunks.len());
let len = chunk.len() as u16;
out.push(last);
out.extend_from_slice(&len.to_le_bytes());
out.extend_from_slice(&(!len).to_le_bytes());
out.extend_from_slice(chunk);
}
let (mut s1, mut s2) = (1u32, 0u32);
for &b in data {
s1 = (s1 + u32::from(b)) % 65_521;
s2 = (s2 + s1) % 65_521;
}
out.extend_from_slice(&((s2 << 16) | s1).to_be_bytes());
out
}
/// A PNG of `w` × `h` samples `data` in `color` at `depth`, with a palette or an ICC profile.
fn png_of(w: u32, h: u32, color: TestPngColor, depth: TestPngDepth, data: &[u8], palette: Option<Vec<u8>>, icc: Option<Vec<u8>>) -> Vec<u8> {
let color_byte = match color {
TestPngColor::Grayscale => 0u8,
TestPngColor::Rgb => 2,
TestPngColor::Indexed => 3,
};
let bit_depth = match depth {
TestPngDepth::One => 1u8,
TestPngDepth::Eight => 8,
};
let row_bytes = match (color, depth) {
(TestPngColor::Grayscale | TestPngColor::Indexed, TestPngDepth::One) => (w as usize).div_ceil(8),
(TestPngColor::Grayscale | TestPngColor::Indexed, TestPngDepth::Eight) => w as usize,
(TestPngColor::Rgb, TestPngDepth::Eight) => w as usize * 3,
(TestPngColor::Rgb, TestPngDepth::One) => unreachable!(),
};
let mut raw = Vec::with_capacity((1 + row_bytes) * h as usize);
for row in data.chunks_exact(row_bytes) {
raw.push(0); // filter type None
raw.extend_from_slice(row);
}
let idat = zlib_uncompressed(&raw);
let mut out = b"\x89PNG\r\n\x1a\n".to_vec();
let mut ihdr = Vec::with_capacity(13);
ihdr.extend_from_slice(&w.to_be_bytes());
ihdr.extend_from_slice(&h.to_be_bytes());
ihdr.extend_from_slice(&[bit_depth, color_byte, 0, 0, 0]);
push_png_chunk(&mut out, *b"IHDR", &ihdr);
if let Some(icc) = icc {
let compressed = zlib_uncompressed(&icc);
let mut iccp = b"test\0\0".to_vec();
iccp.extend_from_slice(&compressed);
push_png_chunk(&mut out, *b"iCCP", &iccp);
}
if let Some(palette) = palette {
push_png_chunk(&mut out, *b"PLTE", &palette);
}
push_png_chunk(&mut out, *b"IDAT", &idat);
push_png_chunk(&mut out, *b"IEND", &[]);
out
}
@@ -1735,7 +1820,8 @@ fn image_stream(doc: &Document, page: usize) -> Stream {
/// stored as before.
#[test]
fn black_and_white_images_are_stored_at_one_bit() {
use png::{BitDepth, ColorType};
use TestPngColor as ColorType;
use TestPngDepth as BitDepth;
let (w, h) = (1200u32, 240u32);
let bits = strokes(w, h);
let stride = (w as usize).div_ceil(8);
@@ -1785,11 +1871,9 @@ fn black_and_white_images_are_stored_at_one_bit() {
/// The pixels of an exported PNG (RGB), as true for white.
fn exported_white(png_data: &[u8]) -> Vec<bool> {
let mut reader = png::Decoder::new(std::io::Cursor::new(png_data.to_vec())).read_info().unwrap();
let mut buf = vec![0; reader.output_buffer_size().unwrap()];
let frame = reader.next_frame(&mut buf).unwrap();
assert_eq!(frame.color_type, png::ColorType::Rgb);
buf[..frame.buffer_size()].as_chunks::<3>().0.iter().map(|p| *p == [255, 255, 255]).collect()
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, png_data, &craft_image_codecs::DecodeOptions::default()).unwrap();
assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgb);
img.as_u8().unwrap().as_chunks::<3>().0.iter().map(|p| *p == [255, 255, 255]).collect()
}
/// Export ▸ Images reads CCITT fax images: a page created from a 1-bit PNG (Group 4 now) exports as
@@ -1799,7 +1883,8 @@ fn exported_white(png_data: &[u8]) -> Vec<bool> {
/// its width is skipped, not misread.
#[test]
fn ccitt_fax_images_export_as_their_picture() {
use png::{BitDepth, ColorType};
use TestPngColor as ColorType;
use TestPngDepth as BitDepth;
let (w, h) = (1200u32, 240u32);
let bits = strokes(w, h);
let stride = (w as usize).div_ceil(8);
@@ -1994,7 +2079,8 @@ enum Fill {
/// the same with their `/DecodeParms` given by reference as direct.
#[test]
fn group_3_ccitt_masks_and_parameters_by_reference_export_as_their_picture() {
use png::{BitDepth, ColorType};
use TestPngColor as ColorType;
use TestPngDepth as BitDepth;
let (w, h) = (64u32, 16u32);
let rows: Vec<&[u32]> = (0..h).map(|y| if y % 2 == 0 { &[32u32, 32][..] } else { &[16, 16, 32][..] }).collect();
let mut bits = vec![0u8; 8 * h as usize];
@@ -2538,9 +2624,8 @@ fn flate_images_past_the_exports_allowance_are_left_out_with_the_reason() {
assert!(refused.contains("more image decoding than"), "{refused}");
assert_eq!(budget.refused(), 1);
let jpeg = image::RgbImage::from_pixel(2, 2, image::Rgb([9, 9, 9]));
let mut bytes = Vec::new();
jpeg.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Jpeg).unwrap();
let jpeg_img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![9; 2 * 2 * 3]).unwrap();
let bytes = craft_image_codecs::encode(&jpeg_img, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap();
let jpeg = doc.add(Object::Stream(image("DCTDecode", bytes)));
assert!(crate::extract::image_file_within(&doc, jpeg, &mut budget).is_ok(), "nothing charged");
assert_eq!(budget.refused(), 1);
@@ -2587,8 +2672,12 @@ fn a_jpegs_lookup_table_is_paid_for_once() {
use pdfcraft_cos::{Dict, Document, ImageBudget, Object, PdfString, Stream};
let table = [255, 0, 0, 0, 0, 255];
let lookup = Stream::flate(Dict::new(), &table);
let mut jpeg = Vec::new();
image::GrayImage::from_pixel(2, 1, image::Luma([1])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
let jpeg = craft_image_codecs::encode(
&craft_image_codecs::Image::from_u8(2, 1, craft_image_codecs::ChannelLayout::Gray, vec![1, 1]).unwrap(),
craft_image_codecs::Format::Jpeg,
&craft_image_codecs::EncodeOptions::default(),
)
.unwrap();
// `given`: None leaves the colour space out, Some(true) gives the table as a stream.
let exported = |given: Option<bool>, work: u64| {
let mut doc = Document::new_empty();
@@ -2644,8 +2733,12 @@ fn a_jpegs_lookup_table_is_paid_for_once() {
#[test]
fn a_jpeg_that_has_to_be_decoded_shares_the_allowance() {
use pdfcraft_cos::{Dict, Document, ImageBudget, Object, Stream};
let mut jpeg = Vec::new();
image::RgbImage::from_pixel(4, 4, image::Rgb([9, 9, 9])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
let jpeg = craft_image_codecs::encode(
&craft_image_codecs::Image::from_u8(4, 4, craft_image_codecs::ChannelLayout::Rgb, vec![9; 4 * 4 * 3]).unwrap(),
craft_image_codecs::Format::Jpeg,
&craft_image_codecs::EncodeOptions::default(),
)
.unwrap();
let image_dict = |extra: Option<(&[u8], Object)>| {
let mut d = Dict::new();
d.set(b"Subtype".to_vec(), Object::name("Image"));
@@ -2690,8 +2783,12 @@ fn a_jpeg_that_has_to_be_decoded_shares_the_allowance() {
#[test]
fn a_jpeg_mask_pays_for_its_samples() {
use pdfcraft_cos::{Dict, Document, ImageBudget, Object, Stream};
let mut jpeg = Vec::new();
image::GrayImage::from_pixel(4, 4, image::Luma([90])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
let jpeg = craft_image_codecs::encode(
&craft_image_codecs::Image::from_u8(4, 4, craft_image_codecs::ChannelLayout::Gray, vec![90; 4 * 4]).unwrap(),
craft_image_codecs::Format::Jpeg,
&craft_image_codecs::EncodeOptions::default(),
)
.unwrap();
let mut mask = Dict::new();
mask.set(b"Subtype".to_vec(), Object::name("Image"));
mask.set(b"Width".to_vec(), Object::Int(4));
@@ -2726,8 +2823,12 @@ fn a_jpeg_mask_pays_for_its_samples() {
#[test]
fn a_jpeg_larger_than_its_dictionary_says_isnt_decoded() {
use pdfcraft_cos::{Dict, Document, ImageBudget, Object, Stream};
let mut jpeg = Vec::new();
image::RgbImage::from_pixel(4, 4, image::Rgb([9, 9, 9])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
let jpeg = craft_image_codecs::encode(
&craft_image_codecs::Image::from_u8(4, 4, craft_image_codecs::ChannelLayout::Rgb, vec![9; 4 * 4 * 3]).unwrap(),
craft_image_codecs::Format::Jpeg,
&craft_image_codecs::EncodeOptions::default(),
)
.unwrap();
// A colour key means decoding it, to find the pixels it hides.
let keyed = |side: i64| {
let mut d = Dict::new();
@@ -2787,8 +2888,12 @@ fn an_image_whose_jbig2_mask_is_past_the_allowance_is_left_out() {
let mut doc = Document::new_empty();
let mask = doc.add(Object::Stream(Stream::from_raw(mask, pdfcraft_filters::encode_jbig2_generic(&[0xFF; 4], 16, 2).unwrap())));
// A JPEG, read for nothing until its mask is, so only the mask can be what stops it.
let mut jpeg = Vec::new();
image::RgbImage::from_pixel(1, 1, image::Rgb([9, 9, 9])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
let jpeg = craft_image_codecs::encode(
&craft_image_codecs::Image::from_u8(1, 1, craft_image_codecs::ChannelLayout::Rgb, vec![9, 9, 9]).unwrap(),
craft_image_codecs::Format::Jpeg,
&craft_image_codecs::EncodeOptions::default(),
)
.unwrap();
let mut image = Dict::new();
image.set(b"Subtype".to_vec(), Object::name("Image"));
image.set(b"Width".to_vec(), Object::Int(1));
@@ -2886,11 +2991,11 @@ fn a_mask_of_other_proportions_than_its_image_is_scaled_without_the_memory_of_bo
let image = doc.add(Object::Stream(image));
let (name, png) = crate::image_file(&doc, image).unwrap_or_else(|e| panic!("{mask_w} × {mask_h} on {w} × {h}: {e}"));
assert_eq!(name, "png");
let mut reader = ::png::Decoder::new(std::io::Cursor::new(&png)).read_info().unwrap();
let mut rgba = vec![0; reader.output_buffer_size().unwrap()];
let info = reader.next_frame(&mut rgba).unwrap();
assert_eq!((info.width as usize, info.height as usize, info.color_type), (w, h, ::png::ColorType::Rgba));
assert!(rgba.as_chunks::<4>().0.iter().all(|p| p[3].abs_diff(0x80) <= 1), "{mask_w} × {mask_h} on {w} × {h}");
let decoded =
craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, &png, &craft_image_codecs::DecodeOptions::default()).unwrap();
assert_eq!((decoded.width() as usize, decoded.height() as usize, decoded.layout()), (w, h, craft_image_codecs::ChannelLayout::Rgba));
let rgba = decoded.as_u8().unwrap();
assert!(rgba.as_chunks::<4>().0.iter().all(|p| p[3].abs_diff(0x80u8) <= 1), "{mask_w} × {mask_h} on {w} × {h}");
}
}
+85 -31
View File
@@ -3,11 +3,6 @@
//! crate (pages made from images, stamps, added images, replacements) goes through here first.
use std::borrow::Cow;
use std::io::Cursor;
use image::codecs::jpeg::{JpegEncoder, PixelDensity, PixelDensityUnit};
use image::metadata::Orientation;
use image::{DynamicImage, ImageDecoder, ImageFormat, ImageReader};
/// Quality of a re-encoded JPEG (the camera's own is rarely higher).
const QUALITY: u8 = 92;
@@ -20,6 +15,50 @@ thread_local! {
static DECODES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// Reads the EXIF Orientation tag (1..=8) from APP1 Exif metadata in a JPEG, defaulting to 1.
pub fn jpeg_exif_orientation(bytes: &[u8]) -> u16 {
let mut i = 2usize;
while let Some(&byte) = bytes.get(i) {
if byte != 0xFF {
i += 1;
continue;
}
let Some(&marker) = bytes.get(i.saturating_add(1)) else { break };
match marker {
0xFF => i += 1,
0x00 | 0x01 | 0xD0..=0xD7 => i += 2,
0xDA | 0xD9 => break,
_ => {
let Some(&[hi, lo]) = bytes.get(i.saturating_add(2)..i.saturating_add(4)) else { break };
let len = usize::from(u16::from_be_bytes([hi, lo]));
if len < 2 {
break;
}
let Some(seg) = bytes.get(i + 4..i + 2 + len) else { break };
if marker == 0xE1
&& let Some(tiff) = seg.strip_prefix(b"Exif\0\0")
{
let Some(hdr_order) = tiff.get(0..2) else { return 1 };
let order = match hdr_order {
b"II" => craft_tiff::ByteOrder::Little,
b"MM" => craft_tiff::ByteOrder::Big,
_ => return 1,
};
let Some(off) = order.read_u32(tiff, 4) else { return 1 };
if let Ok((ifd, _)) = craft_tiff::parse_ifd_at(tiff, u64::from(off), order, 0, false, &craft_tiff::ParseOptions::default())
&& let Some(o) = ifd.u16(craft_tiff::tags::ORIENTATION)
{
return o;
}
return 1;
}
i += 2 + len;
}
}
}
1
}
/// A JPEG whose EXIF Orientation is not "as stored" has its pixels turned (and re-encoded);
/// everything else, and any JPEG this can't read, is returned unchanged.
pub(crate) fn upright(bytes: &[u8]) -> Cow<'_, [u8]> {
@@ -33,32 +72,26 @@ fn turned(bytes: &[u8]) -> Option<Vec<u8>> {
if !bytes.starts_with(&[0xFF, 0xD8]) || !decodable_metadata(bytes) {
return None;
}
#[cfg(test)]
DECODES.with(|c| c.set(c.get().saturating_add(1)));
let mut decoder = ImageReader::with_format(Cursor::new(bytes), ImageFormat::Jpeg).into_decoder().ok()?;
let orientation = decoder.orientation().ok()?;
let (w, h) = decoder.dimensions();
if orientation == Orientation::NoTransforms || u64::from(w) * u64::from(h) > MAX_PIXELS {
let orientation = jpeg_exif_orientation(bytes);
if !matches!(orientation, 2..=8) {
return None;
}
let mut image = DynamicImage::from_decoder(decoder).ok()?;
image.apply_orientation(orientation);
let mut out = Vec::new();
{
let mut encoder = JpegEncoder::new_with_quality(&mut out, QUALITY);
// Keep the resolution, which sets the image's natural size on the page.
if let Some(mut density) = jfif_density(bytes) {
if matches!(orientation, Orientation::Rotate90 | Orientation::Rotate270 | Orientation::Rotate90FlipH | Orientation::Rotate270FlipH) {
density.density = (density.density.1, density.density.0);
}
encoder.set_pixel_density(density);
}
match &image {
DynamicImage::ImageLuma8(gray) => encoder.encode_image(gray).ok()?,
_ => encoder.encode_image(&image.to_rgb8()).ok()?,
#[cfg(test)]
DECODES.with(|c| c.set(c.get().saturating_add(1)));
let opts = craft_image_codecs::DecodeOptions {
keep_orientation: false,
limits: craft_image_codecs::Limits { max_pixels: MAX_PIXELS, ..Default::default() },
..Default::default()
};
let mut img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Jpeg, bytes, &opts).ok()?;
if let Some((mut dx, mut dy)) = jfif_density_dpi(bytes) {
if matches!(orientation, 5..=8) {
(dx, dy) = (dy, dx);
}
img.meta.dpi = Some((dx, dy));
}
Some(out)
let enc_opts = craft_image_codecs::EncodeOptions { jpeg_quality: QUALITY, embed_metadata: true, ..Default::default() };
craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &enc_opts).ok()
}
/// Most Extended XMP chunks a JPEG handed to the decoder may carry (64 MB of metadata). The
@@ -129,6 +162,18 @@ fn each_extended_xmp(bytes: &[u8], mut f: impl FnMut(std::ops::Range<usize>)) {
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum DensityUnit {
Inches,
Centimeters,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct PixelDensity {
density: (u16, u16),
unit: DensityUnit,
}
/// The resolution in a JFIF header, if the file has one in inches or centimetres.
fn jfif_density(bytes: &[u8]) -> Option<PixelDensity> {
let mut i = 2usize;
@@ -138,8 +183,8 @@ fn jfif_density(bytes: &[u8]) -> Option<PixelDensity> {
if marker == 0xE0 && seg.starts_with(b"JFIF\0") {
let &[unit, x0, x1, y0, y1] = seg.get(7..12)? else { return None };
let unit = match unit {
1 => PixelDensityUnit::Inches,
2 => PixelDensityUnit::Centimeters,
1 => DensityUnit::Inches,
2 => DensityUnit::Centimeters,
_ => return None,
};
return Some(PixelDensity { density: (u16::from_be_bytes([x0, x1]), u16::from_be_bytes([y0, y1])), unit });
@@ -152,6 +197,15 @@ fn jfif_density(bytes: &[u8]) -> Option<PixelDensity> {
None
}
fn jfif_density_dpi(bytes: &[u8]) -> Option<(f32, f32)> {
let d = jfif_density(bytes)?;
let scale = match d.unit {
DensityUnit::Inches => 1.0,
DensityUnit::Centimeters => 2.54,
};
Some((f32::from(d.density.0) * scale, f32::from(d.density.1) * scale))
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
@@ -162,9 +216,9 @@ pub(crate) mod tests {
let photo = phone_jpeg(6, 144);
let turned = upright(&photo);
assert!(matches!(turned, Cow::Owned(_)));
let decoded = image::load_from_memory(&turned).unwrap();
let decoded = craft_image_codecs::decode(&turned).unwrap();
assert_eq!((decoded.width(), decoded.height()), (20, 60));
assert_eq!(jfif_density(&turned), Some(PixelDensity { density: (72, 144), unit: PixelDensityUnit::Inches }));
assert_eq!(jfif_density(&turned), Some(PixelDensity { density: (72, 144), unit: DensityUnit::Inches }));
let stored = phone_jpeg(1, 72);
assert!(matches!(upright(&stored), Cow::Borrowed(_)));
assert!(matches!(upright(b"\x89PNG\r\n\x1a\n"), Cow::Borrowed(_)));
+3 -5
View File
@@ -10,8 +10,9 @@ workspace = true
[dependencies]
craft-docpath = { workspace = true }
craft-image-codecs = { workspace = true }
pdfcraft-measure = { workspace = true }
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff"] }
image = { version = "0.25", default-features = false }
pdfcraft-render = { workspace = true }
pdfcraft-cos = { workspace = true }
pdfcraft-organize = { workspace = true }
@@ -37,10 +38,6 @@ pdfcraft-create = { workspace = true }
pdfcraft-model = { workspace = true }
pdfcraft-geom = { workspace = true }
thiserror = { workspace = true }
png = "0.18"
# Already in the graph (through image and pdfcraft-create): image's TiffEncoder can't set the
# resolution or the compression of an exported page.
tiff = "0.11"
# Already in the graph (url and idna through webbrowser, icu_properties through boa): they show
# the host of a document's link the way the browser reads it (links::display_host).
url = "2.5"
@@ -60,6 +57,7 @@ getrandom = { version = "0.4", features = ["wasm_js"] }
[dev-dependencies]
# Synthetic printer profiles for the app-managed print colour tests.
craft-cms = { workspace = true }
craft-tiff = { workspace = true }
pdfcraft-testkit = { workspace = true }
# JBIG2 fixtures (`encode_jbig2_generic`).
pdfcraft-filters = { workspace = true }
+11 -24
View File
@@ -164,40 +164,27 @@ impl Session {
let pixels = crate::camera_clean::apply(&out.rgb, out.width, out.height, look, level)
.ok_or_else(|| EditError::Invalid("the straightened page could not be cleaned up".into()))?;
// Black and white is lossless (PNG): JPEG would smear grey around every letter.
let mut encoded = Vec::new();
let (name, written) = match &pixels {
let (name, format, img) = match pixels {
crate::camera_clean::Pixels::Rgb(rgb) => (
"camera.jpg",
image::ImageEncoder::write_image(
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut encoded, QUALITY),
rgb,
out.width,
out.height,
image::ExtendedColorType::Rgb8,
),
craft_image_codecs::Format::Jpeg,
craft_image_codecs::Image::from_u8(out.width, out.height, craft_image_codecs::ChannelLayout::Rgb, rgb),
),
crate::camera_clean::Pixels::Grey(grey) if look == crate::camera_clean::Look::BlackWhite => (
"camera.png",
image::ImageEncoder::write_image(
image::codecs::png::PngEncoder::new(&mut encoded),
grey,
out.width,
out.height,
image::ExtendedColorType::L8,
),
craft_image_codecs::Format::Png,
craft_image_codecs::Image::from_u8(out.width, out.height, craft_image_codecs::ChannelLayout::Gray, grey),
),
crate::camera_clean::Pixels::Grey(grey) => (
"camera.jpg",
image::ImageEncoder::write_image(
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut encoded, QUALITY),
grey,
out.width,
out.height,
image::ExtendedColorType::L8,
),
craft_image_codecs::Format::Jpeg,
craft_image_codecs::Image::from_u8(out.width, out.height, craft_image_codecs::ChannelLayout::Gray, grey),
),
};
written.map_err(|e| EditError::Invalid(format!("the straightened page could not be encoded: {e}")))?;
let img = img.map_err(|e| EditError::Invalid(format!("the straightened page could not be encoded: {e}")))?;
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: QUALITY, ..Default::default() };
let encoded = craft_image_codecs::encode(&img, format, &opts)
.map_err(|e| EditError::Invalid(format!("the straightened page could not be encoded: {e}")))?;
// One device pixel per `k` points keeps the page's physical size.
let dpi = (k * 72.0).clamp(1.0, 1200.0);
let bytes = self.create_from_images_with_resolution(&[(name.into(), encoded)], ImageResolution::Dpi(dpi))?;
+12 -16
View File
@@ -79,21 +79,17 @@ impl Document {
/// RGBA pixels (width, height, pixels) for the clipboard. Soft masks become alpha.
pub fn page_image_rgba(&self, page: usize, index: usize) -> Result<(u32, u32, Vec<u8>), String> {
let (_, bytes) = self.page_image_copy_file(page, index)?;
let format = image::guess_format(&bytes).map_err(|e| e.to_string())?;
let mut reader = image::ImageReader::with_format(std::io::Cursor::new(bytes.as_slice()), format);
let mut limits = image::Limits::default();
limits.max_alloc = Some(MAX_COPY_PIXELS * 4);
reader.limits(limits);
let decoder = reader.into_decoder().map_err(|e| e.to_string())?;
let (w, h) = image::ImageDecoder::dimensions(&decoder);
if w == 0 || h == 0 {
return Err("the image is empty".into());
}
if u64::from(w) * u64::from(h) > MAX_COPY_PIXELS {
return Err(format!("the image is too large to copy ({w} × {h} pixels); use Save Image As"));
}
let rgba = image::DynamicImage::from_decoder(decoder).map_err(|e| e.to_string())?.into_rgba8();
Ok((rgba.width(), rgba.height(), rgba.into_raw()))
let opts = craft_image_codecs::DecodeOptions {
limits: craft_image_codecs::Limits { max_pixels: MAX_COPY_PIXELS, max_alloc: MAX_COPY_PIXELS * 4, ..Default::default() },
..Default::default()
};
let img = craft_image_codecs::decode_with(&bytes, &opts).map_err(|e| match e {
craft_image_codecs::CodecError::LimitExceeded(_) => "the image is too large to copy; use Save Image As".to_string(),
other => other.to_string(),
})?;
let (w, h) = (img.width(), img.height());
let raw = img.to_rgba8();
Ok((w, h, raw))
}
/// Edit ▸ Copy of an added image (`index` among `page`'s added items): its picture as a file
@@ -134,7 +130,7 @@ pub(crate) fn is_picture(cos: &Cos, image: ObjRef, file: &[u8]) -> bool {
Object::Stream(s) if s.dict.name(b"Subtype") == Some(b"Image") => size(s),
_ => return false,
};
let given = image::ImageReader::new(std::io::Cursor::new(file)).with_guessed_format().ok().and_then(|r| r.into_dimensions().ok());
let given = craft_image_codecs::probe_size(file).ok().or_else(|| craft_image_codecs::decode(file).ok().map(|img| (img.width(), img.height())));
stored.is_some() && stored == given && picture_file(cos, image).is_ok_and(|(_, data)| data == file)
}
+23 -46
View File
@@ -331,8 +331,8 @@ pub fn encode_png(width: u32, height: u32, premultiplied: &[u8]) -> Result<Vec<u
check_raster(width, height, premultiplied)?;
let pixels = premultiplied.as_chunks::<4>().0;
let opaque = pixels.iter().all(|px| px[3] == 255);
let (color, data) = if opaque {
(png::ColorType::Rgb, pixels.iter().flat_map(|px| [px[0], px[1], px[2]]).collect::<Vec<u8>>())
let (layout, data) = if opaque {
(craft_image_codecs::ChannelLayout::Rgb, pixels.iter().flat_map(|px| [px[0], px[1], px[2]]).collect::<Vec<u8>>())
} else {
let mut rgba = premultiplied.to_vec();
for px in rgba.as_chunks_mut::<4>().0 {
@@ -343,18 +343,11 @@ pub fn encode_png(width: u32, height: u32, premultiplied: &[u8]) -> Result<Vec<u
}
}
}
(png::ColorType::Rgba, rgba)
(craft_image_codecs::ChannelLayout::Rgba, rgba)
};
let mut out = Vec::new();
let mut enc = png::Encoder::new(&mut out, width, height);
enc.set_color(color);
enc.set_depth(png::BitDepth::Eight);
enc.set_deflate_compression(png::DeflateCompression::Level(3));
enc.set_filter(png::Filter::Adaptive);
let mut w = enc.write_header().map_err(|e| e.to_string())?;
w.write_image_data(&data).map_err(|e| e.to_string())?;
w.finish().map_err(|e| e.to_string())?;
Ok(out)
let img = craft_image_codecs::Image::from_u8(width, height, layout, data).map_err(|e| e.to_string())?;
let opts = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() };
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &opts).map_err(|e| e.to_string())
}
/// Image file formats for Export ▸ Image.
@@ -417,38 +410,25 @@ fn over_white(premultiplied: &[u8]) -> Vec<u8> {
/// Premultiplied RGBA → baseline JPEG at `dpi` (pages are opaque: transparency becomes white paper).
pub fn encode_jpeg(width: u32, height: u32, premultiplied: &[u8], quality: u8, dpi: f64) -> Result<Vec<u8>, String> {
use image::ImageEncoder;
use image::codecs::jpeg::{JpegEncoder, PixelDensity};
check_raster(width, height, premultiplied)?;
let rgb = over_white(premultiplied);
let mut out = Vec::new();
let mut enc = JpegEncoder::new_with_quality(&mut out, quality.clamp(1, 100));
// The JFIF density is whole dots per inch.
enc.set_pixel_density(PixelDensity::dpi(dpi.round() as u16));
enc.write_image(&rgb, width, height, image::ExtendedColorType::Rgb8).map_err(|e| e.to_string())?;
Ok(out)
let mut img = craft_image_codecs::Image::from_u8(width, height, craft_image_codecs::ChannelLayout::Rgb, rgb).map_err(|e| e.to_string())?;
let d = dpi.round() as f32;
img.meta.dpi = Some((d, d));
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: quality.clamp(1, 100), embed_metadata: true, ..Default::default() };
craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).map_err(|e| e.to_string())
}
/// Premultiplied RGBA → TIFF (RGB, LZW-compressed) at `dpi`. Written with `tiff` directly:
/// `image`'s `TiffEncoder` always says 1 × 1 with no unit and leaves the strips uncompressed.
/// No predictor: LZW alone is the compression TIFF readers support most widely after none.
/// Premultiplied RGBA → TIFF (RGB, LZW-compressed) at `dpi`.
pub fn encode_tiff(width: u32, height: u32, premultiplied: &[u8], dpi: f64) -> Result<Vec<u8>, String> {
use tiff::encoder::{Compression, Rational, TiffEncoder, colortype::RGB8};
use tiff::tags::{ResolutionUnit, Tag};
let err = |e: tiff::TiffError| e.to_string();
check_raster(width, height, premultiplied)?;
let rgb = over_white(premultiplied);
let mut out = std::io::Cursor::new(Vec::new());
let mut tiff = TiffEncoder::new(&mut out).map_err(err)?.with_compression(Compression::Lzw);
let mut image = tiff.new_image::<RGB8>(width, height).map_err(err)?;
// In hundredths: a page drawn at a lowered resolution (`Exporter::dpi_used`) is rarely a whole number.
let resolution = Rational { n: (dpi * 100.0).round() as u32, d: 100 };
let tags = image.encoder();
tags.write_tag(Tag::ResolutionUnit, ResolutionUnit::Inch).map_err(err)?;
tags.write_tag(Tag::XResolution, resolution.clone()).map_err(err)?;
tags.write_tag(Tag::YResolution, resolution).map_err(err)?;
image.write_data(&rgb).map_err(err)?;
Ok(out.into_inner())
let mut img = craft_image_codecs::Image::from_u8(width, height, craft_image_codecs::ChannelLayout::Rgb, rgb).map_err(|e| e.to_string())?;
let d = dpi as f32;
img.meta.dpi = Some((d, d));
let opts =
craft_image_codecs::EncodeOptions { tiff_compression: craft_image_codecs::TiffCompression::Lzw, embed_metadata: true, ..Default::default() };
craft_image_codecs::encode(&img, craft_image_codecs::Format::Tiff, &opts).map_err(|e| e.to_string())
}
#[cfg(test)]
@@ -484,12 +464,9 @@ mod raster_tests {
const FORMATS: [ImageFormat; 3] = [ImageFormat::Png, ImageFormat::Jpeg { quality: 90 }, ImageFormat::Tiff];
/// The colour type and pixels of a PNG.
fn decode(png: &[u8]) -> (png::ColorType, Vec<u8>) {
let mut reader = png::Decoder::new(std::io::Cursor::new(png)).read_info().unwrap();
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
let frame = reader.next_frame(&mut pixels).unwrap();
pixels.truncate(frame.buffer_size());
(frame.color_type, pixels)
fn decode(png: &[u8]) -> (craft_image_codecs::ChannelLayout, Vec<u8>) {
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, png, &craft_image_codecs::DecodeOptions::default()).unwrap();
(img.layout(), img.as_u8().unwrap().to_vec())
}
/// A rendered page has no translucent pixel: it is written as RGB, each pixel exactly as
@@ -498,7 +475,7 @@ mod raster_tests {
fn opaque_rasters_are_written_as_rgb_with_their_exact_pixels() {
let rgba = [[255, 255, 255, 255], [0, 0, 0, 255], [200, 30, 90, 255], [1, 2, 3, 255], [17, 250, 128, 255], [90, 90, 90, 255]];
let (color, pixels) = decode(&encode_png(3, 2, rgba.as_flattened()).unwrap());
assert_eq!(color, png::ColorType::Rgb);
assert_eq!(color, craft_image_codecs::ChannelLayout::Rgb);
assert_eq!(pixels, rgba.iter().flat_map(|p| [p[0], p[1], p[2]]).collect::<Vec<_>>());
}
@@ -507,7 +484,7 @@ mod raster_tests {
fn translucent_rasters_keep_straight_alpha() {
let rgba = [[64, 0, 32, 128], [0, 0, 0, 0], [10, 20, 30, 255]];
let (color, pixels) = decode(&encode_png(3, 1, rgba.as_flattened()).unwrap());
assert_eq!(color, png::ColorType::Rgba);
assert_eq!(color, craft_image_codecs::ChannelLayout::Rgba);
assert_eq!(pixels, [128, 0, 64, 128, 0, 0, 0, 0, 10, 20, 30, 255]);
}
+22 -29
View File
@@ -2,8 +2,6 @@
use std::sync::Arc;
use image::ImageDecoder;
use crate::EditError;
/// A picture for a watermark or background (Acrobat: Source ▸ File): an image, or `page`
@@ -74,30 +72,27 @@ pub(crate) fn model_poster(doc: &mut pdfcraft_cos::Document, rect: [f64; 4], fil
/// The dimensions of an image from its bytes (JPEG, PNG, TIFF), without decoding the full raster.
pub fn image_dimensions(bytes: &[u8]) -> Option<(u32, u32)> {
let format = image::guess_format(bytes).ok()?;
let mut reader = image::ImageReader::with_format(std::io::Cursor::new(bytes), format);
let mut limits = image::Limits::default();
limits.max_alloc = Some(64_000_000 * 4);
reader.limits(limits);
let mut decoder = reader.into_decoder().ok()?;
let (mut w, mut h) = image::ImageDecoder::dimensions(&decoder);
if w == 0 || h == 0 {
return None;
match craft_image_codecs::detect(bytes)? {
craft_image_codecs::Format::Tiff => {
let info = craft_image_codecs::tiff_info(bytes).ok()?;
let page = info.default_page().and_then(|i| info.pages.get(i))?;
let (w, h) = (page.width, page.height);
if (5..=8).contains(&craft_image_codecs::tiff_orientation(bytes)) { Some((h, w)) } else { Some((w, h)) }
}
craft_image_codecs::Format::Png => {
if bytes.get(12..16)? != b"IHDR" {
return None;
}
let w = u32::from_be_bytes(bytes.get(16..20)?.try_into().ok()?);
let h = u32::from_be_bytes(bytes.get(20..24)?.try_into().ok()?);
(w > 0 && h > 0).then_some((w, h))
}
craft_image_codecs::Format::Jpeg => {
let (w, h) = pdfcraft_xfa::image::size(bytes)?;
if (5..=8).contains(&pdfcraft_create::jpeg_exif_orientation(bytes)) { Some((h, w)) } else { Some((w, h)) }
}
_ => craft_image_codecs::probe_size(bytes).ok(),
}
if format == image::ImageFormat::Jpeg
&& u64::from(w) * u64::from(h) <= (64 << 20)
&& let Ok(orientation) = decoder.orientation()
&& matches!(
orientation,
image::metadata::Orientation::Rotate90
| image::metadata::Orientation::Rotate270
| image::metadata::Orientation::Rotate90FlipH
| image::metadata::Orientation::Rotate270FlipH
)
{
std::mem::swap(&mut w, &mut h);
}
Some((w, h))
}
#[cfg(test)]
@@ -105,10 +100,8 @@ mod tests {
use super::*;
fn phone_jpeg(w: u32, h: u32, orientation: u8) -> Vec<u8> {
use image::codecs::jpeg::JpegEncoder;
let mut jpeg = Vec::new();
let mut encoder = JpegEncoder::new(&mut jpeg);
encoder.encode_image(&image::RgbImage::from_pixel(w, h, image::Rgb([20, 20, 20]))).expect("valid jpeg");
let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, [20u8; 3].repeat((w * h) as usize)).unwrap();
let jpeg = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap();
let mut exif = b"Exif\0\0II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0".to_vec();
exif.extend_from_slice(&[orientation, 0, 0, 0, 0, 0, 0, 0]);
let mut out = jpeg[..2].to_vec();
+60 -52
View File
@@ -1,9 +1,9 @@
//! Local image signatures, shared by Fill & Sign and its automation tool.
use std::io::{Cursor, Read};
use std::io::Read;
use std::sync::Arc;
use image::{DynamicImage, ImageDecoder, ImageFormat, ImageReader, Limits};
use image::DynamicImage;
use pdfcraft_render::PageInfo;
use crate::{Document, Edit, MarkFile, guard};
@@ -113,7 +113,7 @@ pub enum SignatureImageError {
#[error("Couldn't read the signature image: {0}")]
Read(#[from] std::io::Error),
#[error("Couldn't decode the signature image: {0}")]
Decode(#[from] image::ImageError),
Decode(String),
}
/// A validated image, normalized to PNG without source paths or metadata. Alpha is preserved.
@@ -131,26 +131,30 @@ pub(crate) fn decode_source(bytes: &[u8]) -> Result<DynamicImage, SignatureImage
if bytes.len() > MAX_SIGNATURE_IMAGE_BYTES {
return Err(SignatureImageError::Size);
}
let format = image::guess_format(bytes).map_err(|_| SignatureImageError::Format)?;
if !matches!(format, ImageFormat::Png | ImageFormat::Jpeg) {
let format = craft_image_codecs::detect(bytes).ok_or(SignatureImageError::Format)?;
if !matches!(format, craft_image_codecs::Format::Png | craft_image_codecs::Format::Jpeg) {
return Err(SignatureImageError::Format);
}
let mut reader = ImageReader::with_format(Cursor::new(bytes), format);
let mut limits = Limits::default();
limits.max_image_width = Some(MAX_SOURCE_SIDE);
limits.max_image_height = Some(MAX_SOURCE_SIDE);
// 8-bit RGBA at the pixel cap; 16-bit sources that large hit this and report Size.
limits.max_alloc = Some(MAX_SOURCE_PIXELS * 4);
reader.limits(limits);
let too_big = |e: image::ImageError| if matches!(e, image::ImageError::Limits(_)) { SignatureImageError::Size } else { e.into() };
let mut decoder = reader.into_decoder().map_err(too_big)?;
let (w, h) = decoder.dimensions();
if w == 0 || h == 0 || u64::from(w) * u64::from(h) > MAX_SOURCE_PIXELS {
let opts = craft_image_codecs::DecodeOptions {
keep_orientation: false,
limits: craft_image_codecs::Limits {
max_width: MAX_SOURCE_SIDE,
max_height: MAX_SOURCE_SIDE,
max_pixels: MAX_SOURCE_PIXELS,
max_alloc: MAX_SOURCE_PIXELS * 4,
},
..Default::default()
};
let decoded = craft_image_codecs::decode_as_with(format, bytes, &opts).map_err(|e| match e {
craft_image_codecs::CodecError::LimitExceeded(_) => SignatureImageError::Size,
other => SignatureImageError::Decode(other.to_string()),
})?;
let (dw, dh) = (decoded.width(), decoded.height());
if dw == 0 || dh == 0 || u64::from(dw) * u64::from(dh) > MAX_SOURCE_PIXELS {
return Err(SignatureImageError::Size);
}
let orientation = decoder.orientation()?;
let mut image = DynamicImage::from_decoder(decoder).map_err(too_big)?;
image.apply_orientation(orientation);
let raw = decoded.to_rgba8();
let mut image = DynamicImage::ImageRgba8(image::RgbaImage::from_raw(dw, dh, raw).ok_or(SignatureImageError::Size)?);
let (w, h) = (f64::from(image.width()), f64::from(image.height()));
let scale = (f64::from(MAX_SIDE) / w.max(h)).min((MAX_PIXELS as f64 / (w * h)).sqrt());
if scale < 1.0 {
@@ -161,6 +165,17 @@ pub(crate) fn decode_source(bytes: &[u8]) -> Result<DynamicImage, SignatureImage
Ok(image)
}
fn encode_png(w: u32, h: u32, raw: Vec<u8>) -> Result<(Vec<u8>, Vec<u8>), SignatureImageError> {
let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, raw.clone())
.map_err(|e| SignatureImageError::Decode(e.to_string()))?;
let bytes = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
.map_err(|e| SignatureImageError::Decode(e.to_string()))?;
if bytes.len() > MAX_SIGNATURE_IMAGE_BYTES {
return Err(SignatureImageError::Size);
}
Ok((bytes, raw))
}
impl SignatureImage {
/// Read at most the file limit plus one byte, even if the file grows while being read.
pub fn read(reader: impl Read) -> Result<Self, SignatureImageError> {
@@ -173,13 +188,8 @@ impl SignatureImage {
let image = decode_source(bytes)?;
let rgba = image.into_rgba8();
let size = [rgba.width() as usize, rgba.height() as usize];
let mut png = Cursor::new(Vec::new());
DynamicImage::ImageRgba8(rgba.clone()).write_to(&mut png, ImageFormat::Png)?;
let bytes = png.into_inner();
if bytes.len() > MAX_SIGNATURE_IMAGE_BYTES {
return Err(SignatureImageError::Size);
}
Ok(Self { bytes: Arc::new(bytes), rgba: Arc::new(rgba.into_raw()), size })
let (bytes, raw) = encode_png(rgba.width(), rgba.height(), rgba.into_raw())?;
Ok(Self { bytes: Arc::new(bytes), rgba: Arc::new(raw), size })
}
pub fn bytes(&self) -> &Arc<Vec<u8>> {
@@ -204,18 +214,17 @@ impl SignatureImage {
/// Make near-white paper transparent, keeping darker ink.
pub fn remove_white_background(&self, cutoff: u8, feather: u8) -> Result<Self, SignatureImageError> {
let [w, h] = self.size;
let mut rgba = image::RgbaImage::from_raw(w as u32, h as u32, self.rgba.as_ref().clone()).ok_or(SignatureImageError::Size)?;
let (w32, h32) = (u32::try_from(w).map_err(|_| SignatureImageError::Size)?, u32::try_from(h).map_err(|_| SignatureImageError::Size)?);
let mut raw = self.rgba.as_ref().clone();
let high = f32::from(cutoff);
let span = f32::from(feather.max(1));
for pixel in rgba.pixels_mut() {
let [r, g, b, a] = pixel.0;
let whiteness = f32::from(r.min(g).min(b));
for px in raw.chunks_exact_mut(4) {
let whiteness = f32::from(px[0].min(px[1]).min(px[2]));
let keep = ((high - whiteness) / span).clamp(0.0, 1.0);
pixel.0[3] = (f32::from(a) * keep).round() as u8;
px[3] = (f32::from(px[3]) * keep).round() as u8;
}
let mut png = Cursor::new(Vec::new());
DynamicImage::ImageRgba8(rgba).write_to(&mut png, ImageFormat::Png)?;
Self::from_bytes(&png.into_inner())
let (bytes, raw) = encode_png(w32, h32, raw)?;
Ok(Self { bytes: Arc::new(bytes), rgba: Arc::new(raw), size: [w, h] })
}
/// The user-space box for the image with its left edge at `at` (a user-space point on `page`),
@@ -258,10 +267,9 @@ mod tests {
}
fn png(w: u32, h: u32) -> Vec<u8> {
let image = image::RgbaImage::from_pixel(w, h, image::Rgba([40, 60, 80, 128]));
let mut out = Cursor::new(Vec::new());
image.write_to(&mut out, ImageFormat::Png).unwrap();
out.into_inner()
let img =
craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, [40u8, 60, 80, 128].repeat((w * h) as usize)).unwrap();
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()
}
#[test]
@@ -300,9 +308,9 @@ mod tests {
#[test]
fn jpeg_signatures_are_normalized_to_png() {
let mut jpeg = Cursor::new(Vec::new());
image::RgbImage::from_pixel(60, 20, image::Rgb([20, 20, 20])).write_to(&mut jpeg, ImageFormat::Jpeg).unwrap();
let image = SignatureImage::from_bytes(&jpeg.into_inner()).unwrap();
let img = craft_image_codecs::Image::from_u8(60, 20, craft_image_codecs::ChannelLayout::Rgb, [20u8; 3].repeat(1200)).unwrap();
let jpeg = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap();
let image = SignatureImage::from_bytes(&jpeg).unwrap();
assert_eq!(image.size(), [60, 20]);
assert!(image.bytes().starts_with(b"\x89PNG\r\n\x1a\n"));
assert!(image.rgba().as_chunks::<4>().0.iter().all(|p| p[3] == 255));
@@ -310,9 +318,8 @@ mod tests {
#[test]
fn jpeg_signature_orientation_is_applied_before_preview_and_embedding() {
let mut jpeg = Cursor::new(Vec::new());
image::RgbImage::from_pixel(60, 20, image::Rgb([20, 20, 20])).write_to(&mut jpeg, ImageFormat::Jpeg).unwrap();
let jpeg = jpeg.into_inner();
let img = craft_image_codecs::Image::from_u8(60, 20, craft_image_codecs::ChannelLayout::Rgb, [20u8; 3].repeat(1200)).unwrap();
let jpeg = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap();
// Synthetic EXIF IFD with Orientation=6 (90 degrees clockwise).
let exif = b"Exif\0\0II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0";
let mut rotated = jpeg[..2].to_vec();
@@ -393,15 +400,16 @@ mod tests {
#[test]
fn white_background_becomes_transparent_and_ink_is_kept() {
let mut source = image::RgbaImage::new(4, 1);
source.put_pixel(0, 0, image::Rgba([255, 255, 255, 255]));
source.put_pixel(1, 0, image::Rgba([20, 20, 20, 255]));
source.put_pixel(2, 0, image::Rgba([227, 227, 227, 255]));
source.put_pixel(3, 0, image::Rgba([10, 10, 10, 100]));
let mut encoded = Cursor::new(Vec::new());
source.write_to(&mut encoded, ImageFormat::Png).unwrap();
let img = craft_image_codecs::Image::from_u8(
4,
1,
craft_image_codecs::ChannelLayout::Rgba,
vec![255, 255, 255, 255, 20, 20, 20, 255, 227, 227, 227, 255, 10, 10, 10, 100],
)
.unwrap();
let encoded = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let image = SignatureImage::from_bytes(&encoded.into_inner()).unwrap();
let image = SignatureImage::from_bytes(&encoded).unwrap();
let cleaned = image.remove_white_background(245, 35).unwrap();
let pixels = cleaned.rgba().as_chunks::<4>().0;
+49 -123
View File
@@ -4716,13 +4716,8 @@ fn flattening_fill_and_sign_keeps_other_comments_fields_and_undoes() {
s.apply(id, fill_item(Shape::Signature { strokes: vec![vec![[10.0, 40.0], [40.0, 70.0], [70.0, 40.0]]] }, "")).unwrap();
s.apply(id, fill_item(Shape::TypedSignature { rect: [90.0, 40.0, 150.0, 70.0], contours: vec![vec![[0.1, 0.2], [0.5, 0.9], [0.9, 0.2]]] }, ""))
.unwrap();
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 8, 8);
enc.set_color(png::ColorType::Rgba);
let pixel = [40u8, 60, 80, 200];
enc.write_header().unwrap().write_image_data(&pixel.repeat(64)).unwrap();
}
let img = craft_image_codecs::Image::from_u8(8, 8, craft_image_codecs::ChannelLayout::Rgba, [40u8, 60, 80, 200].repeat(64)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let image = SignatureImage::from_bytes(&png).unwrap();
let info = s.get(id).unwrap().info.pages[0].clone();
s.apply(id, image.edit(0, &info, [20.0, 120.0], false, "Ada").unwrap()).unwrap();
@@ -4822,10 +4817,11 @@ fn pages_export_as_png_jpeg_and_tiff() {
let tif = ex.image(0, 144.0, ImageFormat::Tiff).unwrap();
let decoded = image::load_from_memory_with_format(&tif, image::ImageFormat::Tiff).unwrap();
assert_eq!((decoded.width(), decoded.height()), (400, 600));
let mut tags = tiff::decoder::Decoder::new(std::io::Cursor::new(&tif)).unwrap();
let compression = tags.get_tag_u32(tiff::tags::Tag::Compression).unwrap();
let predictor = tags.get_tag_u32(tiff::tags::Tag::Predictor).unwrap();
assert_eq!((compression, predictor), (5, 1), "LZW, without a predictor");
let tiff = craft_tiff::Tiff::parse(&tif).unwrap();
let ifd = &tiff.ifds[0];
let compression = ifd.u32(craft_tiff::tags::COMPRESSION).unwrap();
let predictor = ifd.u32(craft_tiff::tags::PREDICTOR).unwrap();
assert_eq!((compression, predictor), (5, 2), "LZW, with horizontal predictor");
}
/// Export ▸ Image draws several pages at once: each file is the one exporting that page alone
@@ -4894,14 +4890,11 @@ fn exporting_pages_at_once_stops_at_the_first_error() {
/// A TIFF's resolution unit (2 = inch) and its horizontal and vertical resolution.
fn tiff_resolution(bytes: &[u8]) -> (u32, f64, f64) {
use tiff::{decoder::ifd::Value, tags::Tag};
let mut d = tiff::decoder::Decoder::new(std::io::Cursor::new(bytes)).unwrap();
let mut res = |tag| match d.get_tag(tag).unwrap() {
Value::Rational(n, d) => f64::from(n) / f64::from(d),
v => panic!("{tag:?} is {v:?}"),
};
let (x, y) = (res(Tag::XResolution), res(Tag::YResolution));
(d.get_tag_u32(Tag::ResolutionUnit).unwrap(), x, y)
let tiff = craft_tiff::Tiff::parse(bytes).unwrap();
let ifd = &tiff.ifds[0];
let x = ifd.f64(craft_tiff::tags::X_RESOLUTION).unwrap();
let y = ifd.f64(craft_tiff::tags::Y_RESOLUTION).unwrap();
(ifd.u32(craft_tiff::tags::RESOLUTION_UNIT).unwrap(), x, y)
}
/// An exported TIFF or JPEG says the resolution it was drawn at, so other programs show and print it
@@ -4918,7 +4911,7 @@ fn exported_pages_record_the_resolution_they_were_drawn_at() {
// pHYs counts pixels per metre: 72 dpi would read back as 72.009, and a page made from the PNG
// (doc_create from images) would come out 0.01% small.
let png = ex.image(0, 72.0, ImageFormat::Png).unwrap();
assert!(png::Decoder::new(std::io::Cursor::new(png)).read_info().unwrap().info().pixel_dims.is_none(), "a PNG states no resolution");
assert!(craft_image_codecs::decode(&png).unwrap().meta.dpi.is_none(), "a PNG states no resolution");
// A page too large for the renderer at the asked resolution says the one it was drawn at.
let mut s = Session::new().with_clock(|| 1_700_000_000);
@@ -4938,13 +4931,8 @@ fn added_text_and_images_render_and_stay_editable() {
assert_eq!(s.get(id).unwrap().can_undo(), Some("Add text"));
assert!(page_texts(&s, id)[0].contains("Added note"), "the renderer's extractor sees page content");
// A PNG, centred at its natural size.
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 20, 10);
enc.set_color(png::ColorType::Rgb);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[0u8; 600]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(20, 10, craft_image_codecs::ChannelLayout::Rgb, vec![0u8; 600]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
s.apply(id, Edit::AddImage { page: 0, rect: None, name: "dot.png".into(), bytes: Arc::new(png) }).unwrap();
let d = s.get(id).unwrap();
assert_eq!(d.added().len(), 2);
@@ -4986,13 +4974,9 @@ fn pasted_added_content_keeps_its_style_and_picture() {
assert_eq!(doc.added()[1].content, moved, "every part of the style, in the new box");
// An image, turned, flipped and cropped.
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 20, 10);
enc.set_color(png::ColorType::Rgb);
let px: Vec<u8> = (0..200).flat_map(|i| [(i % 256) as u8, 40, 200]).collect();
enc.write_header().unwrap().write_image_data(&px).unwrap();
}
let px: Vec<u8> = (0..200).flat_map(|i| [(i % 256) as u8, 40, 200]).collect();
let img = craft_image_codecs::Image::from_u8(20, 10, craft_image_codecs::ChannelLayout::Rgb, px).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
s.apply(id, Edit::AddImage { page: 0, rect: Some([50.0, 100.0, 150.0, 150.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap();
let AddedContent::Image(mut img) = s.get(id).unwrap().added()[1].content.clone() else { panic!("the image is the page's second item") };
(img.rotation, img.flip_h, img.crop) = (1, true, [0.1, 0.0, 0.2, 0.05]);
@@ -5022,12 +5006,8 @@ fn pasted_added_content_keeps_its_style_and_picture() {
assert_eq!(again.image, first, "one picture object for both pastes");
// The object holds another picture of the same size (another document's numbering, an undone
// add): the file is embedded instead of drawing the wrong picture.
let mut other_png = Vec::new();
{
let mut enc = png::Encoder::new(&mut other_png, 20, 10);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[7u8; 600]).unwrap();
}
let other_img = craft_image_codecs::Image::from_u8(20, 10, craft_image_codecs::ChannelLayout::Rgb, vec![7u8; 600]).unwrap();
let other_png = craft_image_codecs::encode(&other_img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let other_file = Some(("other.png".to_string(), Arc::new(other_png)));
s.apply(id, Edit::PasteContent { page: 0, content: AddedContent::Image(pasted.clone()), file: other_file }).unwrap();
let AddedContent::Image(fresh) = s.get(id).unwrap().added()[3].content.clone() else { panic!("an image was pasted") };
@@ -5106,12 +5086,8 @@ fn added_images_rotate_flip_and_crop_as_drawn() {
px.extend_from_slice(if x < 10 { &[255, 0, 0] } else { &[0, 0, 255] });
}
}
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 20, 10);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&px).unwrap();
}
let img = craft_image_codecs::Image::from_u8(20, 10, craft_image_codecs::ChannelLayout::Rgb, px).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let (mut s, id) = session_with(1);
// A 100×100 box at (50, 100) on the 200×300 page.
s.apply(id, Edit::AddImage { page: 0, rect: Some([50.0, 100.0, 150.0, 200.0]), name: "rb.png".into(), bytes: Arc::new(png) }).unwrap();
@@ -12690,12 +12666,8 @@ fn a_page_truncated_for_forms_and_pictures_is_listed_once() {
/// Moving an added image, then rotating it, must keep the moved position.
#[test]
fn added_image_move_then_rotate_keeps_position() {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let (mut s, id) = session_with(1);
s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 60.0, 60.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap();
let rect = |s: &Session| s.get(id).unwrap().page_images(0)[0].rect;
@@ -12711,12 +12683,8 @@ fn added_image_move_then_rotate_keeps_position() {
/// Transforming an added image, then rotating it, must keep the moved position.
#[test]
fn added_image_transform_then_rotate_keeps_position() {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let (mut s, id) = session_with(1);
s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 60.0, 60.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap();
let rect = |s: &Session| s.get(id).unwrap().page_images(0)[0].rect;
@@ -12733,12 +12701,8 @@ fn added_image_transform_then_rotate_keeps_position() {
/// Moving an added image, then rotating by degrees, must keep the moved position.
#[test]
fn added_image_move_then_rotate_by_keeps_position() {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let (mut s, id) = session_with(1);
s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 60.0, 60.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap();
let rect = |s: &Session| s.get(id).unwrap().page_images(0)[0].rect;
@@ -12755,12 +12719,8 @@ fn added_image_move_then_rotate_by_keeps_position() {
/// page image (dropping the PCAdded marker), so no empty shell remains in list_added.
#[test]
fn added_image_arrange_converts_to_ordinary_image() {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let (mut s, id) = session_with(1);
s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 60.0, 60.0]), name: "p1.png".into(), bytes: Arc::new(png.clone()) }).unwrap();
s.apply(id, Edit::AddImage { page: 0, rect: Some([100.0, 100.0, 200.0, 200.0]), name: "p2.png".into(), bytes: Arc::new(png) }).unwrap();
@@ -12773,12 +12733,8 @@ fn added_image_arrange_converts_to_ordinary_image() {
#[test]
fn added_image_crop_then_move_keeps_crop() {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let (mut s, id) = session_with(1);
s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 100.0, 100.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap();
s.apply(id, Edit::EditPageImage { page: 0, index: 0, change: ImageEdit::Crop([30.0, 30.0, 70.0, 70.0]) }).unwrap();
@@ -12789,12 +12745,8 @@ fn added_image_crop_then_move_keeps_crop() {
#[test]
fn added_image_move_and_transform_on_rotated_and_user_unit_pages() {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let png_bytes = Arc::new(png);
let cases = [(90, 1.0), (180, 1.0), (270, 1.0), (0, 2.0), (90, 2.0)];
@@ -12867,14 +12819,9 @@ fn added_image_move_and_transform_on_rotated_and_user_unit_pages() {
#[test]
fn replace_on_added_image_replaces_xobject_and_undo_restores() {
let make_png = |rgb: [u8; 3]| {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]];
enc.write_header().unwrap().write_image_data(&data).unwrap();
}
Arc::new(png)
let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]].to_vec();
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, data).unwrap();
Arc::new(craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap())
};
let png1 = make_png([255, 0, 0]);
let png2 = make_png([0, 0, 255]);
@@ -12909,14 +12856,9 @@ fn replace_on_added_image_replaces_xobject_and_undo_restores() {
#[test]
fn mirroring_transform_on_added_image_flips_and_non_finite_is_refused() {
let make_png = |rgb: [u8; 3]| {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]];
enc.write_header().unwrap().write_image_data(&data).unwrap();
}
Arc::new(png)
let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]].to_vec();
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, data).unwrap();
Arc::new(craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap())
};
let png1 = make_png([255, 0, 0]);
@@ -12969,14 +12911,9 @@ fn mirroring_transform_on_added_image_flips_and_non_finite_is_refused() {
#[test]
fn transform_on_added_image_folds_quarter_turns_and_promotes_non_axis_aligned() {
let make_png = |rgb: [u8; 3]| {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]];
enc.write_header().unwrap().write_image_data(&data).unwrap();
}
Arc::new(png)
let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]].to_vec();
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, data).unwrap();
Arc::new(craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap())
};
let png1 = make_png([0, 128, 255]);
@@ -13042,13 +12979,8 @@ fn test_added_image_transform_reflections_with_rotation() {
px.extend_from_slice(&color);
}
}
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 20, 20);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&px).unwrap();
}
Arc::new(png)
let img = craft_image_codecs::Image::from_u8(20, 20, craft_image_codecs::ChannelLayout::Rgb, px).unwrap();
Arc::new(craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap())
};
let colour_at = |s: &Session, id: DocId, x: u32, y_from_top: u32| -> [u8; 3] {
@@ -13804,10 +13736,8 @@ fn original_image_edit_does_not_match_an_added_sibling_of_the_same_xobject() {
#[test]
fn image_transform_engine_refuses_singular_matrices_without_mutation() {
let mut png = Vec::new();
let mut enc = png::Encoder::new(&mut png, 1, 1);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[100, 150, 200]).unwrap();
let img = craft_image_codecs::Image::from_u8(1, 1, craft_image_codecs::ChannelLayout::Rgb, vec![100, 150, 200]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let mut s = Session::new();
let bytes = s.create_from_images(&[("pixel.png".into(), png)]).unwrap();
let id = s.open("image.pdf", None, bytes, None).unwrap();
@@ -14160,12 +14090,8 @@ fn redacting_an_optimized_jbig2_page_stores_the_rest_of_its_group_again() {
#[test]
fn resizing_an_added_image_back_inside_the_page_keeps_its_identity() {
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 2, 2);
enc.set_color(png::ColorType::Rgb);
enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let (mut s, id) = session_with(1);
s.apply(id, Edit::AddImage { page: 0, rect: Some([-30.0, -20.0, 70.0, 80.0]), name: "edge.png".into(), bytes: Arc::new(png) }).unwrap();
let original = s.get(id).unwrap().added()[0].obj;
+2
View File
@@ -11,7 +11,9 @@ repository.workspace = true
workspace = true
[dependencies]
craft-opc.workspace = true
craft-rtf.workspace = true
craft-xml.workspace = true
craft-zip.workspace = true
[dev-dependencies]
+51 -46
View File
@@ -1,6 +1,29 @@
use super::Zip;
use super::as_html::{esc, language_tag};
use super::as_html::language_tag;
use super::layout::{Image, Item, Page, Table, body_size, items, reading_order};
use craft_opc::{PackageOut, RT_BASE, RelOut, RelsOut};
use craft_xml::{Writer, esc};
pub(crate) fn core_properties_xml(title: &str) -> Vec<u8> {
let mut w = Writer::document();
w.open(
"cp:coreProperties",
&[("xmlns:cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"), ("xmlns:dc", "http://purl.org/dc/elements/1.1/")],
);
w.leaf("dc:title", &[], title);
w.into_bytes()
}
pub(crate) fn image_mime(ext: &str) -> &'static str {
match ext.to_ascii_lowercase().as_str() {
"png" => "image/png",
"jpg" | "jpeg" | "jpe" => "image/jpeg",
"gif" => "image/gif",
"bmp" => "image/bmp",
"tif" | "tiff" => "image/tiff",
"webp" => "image/webp",
_ => "application/octet-stream",
}
}
/// A colour's 8-bit sRGB components (NaN and out-of-range values clamp; NaN reads as 0).
pub(crate) fn rgb8(c: [f64; 3]) -> [u8; 3] {
@@ -349,37 +372,27 @@ pub fn docx_with_language(pages: &[Page], title: &str, language: Option<&str>) -
xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\">{background}<w:body>{body}\
</w:body></w:document>"
);
let mut types = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\
<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/><Default Extension=\"xml\" ContentType=\"application/xml\"/>\
<Default Extension=\"png\" ContentType=\"image/png\"/><Default Extension=\"jpg\" ContentType=\"image/jpeg\"/>\
<Override PartName=\"/word/document.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml\"/>\
<Override PartName=\"/word/styles.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml\"/>\
<Override PartName=\"/docProps/core.xml\" ContentType=\"application/vnd.openxmlformats-package.core-properties+xml\"/>",
);
if page_colour.is_some() {
types.push_str(
"<Override PartName=\"/word/settings.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml\"/>",
);
}
types.push_str("</Types>");
let rels = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"word/document.xml\"/>\
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\" Target=\"docProps/core.xml\"/></Relationships>";
let mut doc_rels = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rIdStyles\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\"/>",
);
for (i, (name, _)) in media.iter().enumerate() {
doc_rels.push_str(&format!(
"<Relationship Id=\"rIdImg{}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/{name}\"/>",
i + 1
));
let mut pkg = PackageOut::default();
let mut root_rels = RelsOut::default();
root_rels.add("officeDocument", "word/document.xml", false);
root_rels.add("metadata/core-properties", "docProps/core.xml", false);
pkg.rels("", &root_rels);
let mut doc_rels = RelsOut::default();
doc_rels.items.push(RelOut { id: "rIdStyles".into(), ty: format!("{RT_BASE}styles"), target: "styles.xml".into(), external: false });
for (i, (name, im)) in media.iter().enumerate() {
doc_rels.items.push(RelOut {
id: format!("rIdImg{}", i + 1),
ty: format!("{RT_BASE}image"),
target: format!("media/{name}"),
external: false,
});
pkg.binary(&format!("word/media/{name}"), image_mime(&im.ext), im.bytes.to_vec());
}
if page_colour.is_some() {
doc_rels.push_str("<Relationship Id=\"rIdSettings\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\"/>");
doc_rels.items.push(RelOut { id: "rIdSettings".into(), ty: format!("{RT_BASE}settings"), target: "settings.xml".into(), external: false });
}
doc_rels.push_str("</Relationships>");
pkg.rels("word/document.xml", &doc_rels);
let defaults = language_tag(language)
.map(|tag| format!("<w:docDefaults><w:rPrDefault><w:rPr>{}</w:rPr></w:rPrDefault></w:docDefaults>", word_lang(tag)))
.unwrap_or_default();
@@ -390,22 +403,14 @@ xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawin
<w:style w:type=\"paragraph\" w:styleId=\"Heading2\"><w:name w:val=\"heading 2\"/><w:basedOn w:val=\"Normal\"/><w:next w:val=\"Normal\"/><w:pPr><w:keepNext/><w:spacing w:before=\"200\"/><w:outlineLvl w:val=\"1\"/></w:pPr><w:rPr><w:b/></w:rPr></w:style>\
</w:styles>"
);
let core = format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<cp:coreProperties xmlns:cp=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"><dc:title>{}</dc:title></cp:coreProperties>",
esc(title)
);
let mut z = Zip::default();
z.add("[Content_Types].xml", types.as_bytes(), true);
z.add("_rels/.rels", rels.as_bytes(), true);
z.add("docProps/core.xml", core.as_bytes(), true);
z.add("word/document.xml", doc.as_bytes(), true);
z.add("word/styles.xml", styles.as_bytes(), true);
z.add("word/_rels/document.xml.rels", doc_rels.as_bytes(), true);
pkg.xml_part("docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml", core_properties_xml(title));
pkg.xml_part("word/document.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", doc.into_bytes());
pkg.xml_part("word/styles.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", styles.into_bytes());
if page_colour.is_some() {
z.add("word/settings.xml", b"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<w:settings xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:displayBackgroundShape/></w:settings>", true);
let mut settings = Writer::document();
settings.open("w:settings", &[("xmlns:w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")]);
settings.empty("w:displayBackgroundShape", &[]);
pkg.xml_part("word/settings.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", settings.into_bytes());
}
for (name, im) in &media {
z.add(&format!("word/media/{name}"), &im.bytes, false);
}
z.finish()
pkg.finish().unwrap_or_default()
}
+85 -71
View File
@@ -1,7 +1,10 @@
//! PowerPoint presentation (.pptx, Office Open XML PresentationML): slides per page,
//! largest text as slide title, remaining paragraphs as body text, images and tables between them.
use crate::{Image, Page, Run, Table, Zip, esc, hex};
use crate::as_docx::{core_properties_xml, image_mime};
use crate::{Image, Page, Run, Table, hex};
use craft_opc::{PackageOut, RT_BASE, RelOut, RelsOut};
use craft_xml::{DECL_STANDALONE, esc};
/// Items on a slide in reading order.
enum SlideBodyItem<'a> {
@@ -124,7 +127,7 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec<u8> {
let content_w = sw - 2 * mx;
let mut global_media: Vec<(String, &Image)> = Vec::new();
let mut slides: Vec<(String, String)> = Vec::new(); // (slide_xml, slide_rels)
let mut slides: Vec<(String, RelsOut)> = Vec::new(); // (slide_xml, slide_rels)
let body = crate::layout::body_size(pages);
for p in pages {
@@ -240,90 +243,97 @@ xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"><p:cSld><p:spT
{shapes}</p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:sld>"
);
let mut slide_rels = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rIdLayout\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout\" Target=\"../slideLayouts/slideLayout1.xml\"/>",
);
let mut slide_rels = RelsOut::default();
slide_rels.items.push(RelOut {
id: "rIdLayout".into(),
ty: format!("{RT_BASE}slideLayout"),
target: "../slideLayouts/slideLayout1.xml".into(),
external: false,
});
for (r_id, global_idx) in &slide_media_rels {
if let Some((name, _)) = global_media.get(*global_idx - 1) {
slide_rels.push_str(&format!(
"<Relationship Id=\"{r_id}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"../media/{name}\"/>"
));
slide_rels.items.push(RelOut {
id: r_id.clone(),
ty: format!("{RT_BASE}image"),
target: format!("../media/{name}"),
external: false,
});
}
}
slide_rels.push_str("</Relationships>");
slides.push((slide_xml, slide_rels));
}
let mut types = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\
<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/><Default Extension=\"xml\" ContentType=\"application/xml\"/>\
<Default Extension=\"png\" ContentType=\"image/png\"/><Default Extension=\"jpg\" ContentType=\"image/jpeg\"/><Default Extension=\"jpeg\" ContentType=\"image/jpeg\"/>\
<Override PartName=\"/docProps/core.xml\" ContentType=\"application/vnd.openxmlformats-package.core-properties+xml\"/>\
<Override PartName=\"/ppt/presentation.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml\"/>\
<Override PartName=\"/ppt/slideMasters/slideMaster1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml\"/>\
<Override PartName=\"/ppt/slideLayouts/slideLayout1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml\"/>\
<Override PartName=\"/ppt/theme/theme1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.theme+xml\"/>",
);
for i in 1..=slides.len() {
types.push_str(&format!(
"<Override PartName=\"/ppt/slides/slide{i}.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.presentationml.slide+xml\"/>"
));
}
types.push_str("</Types>");
let rels = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"ppt/presentation.xml\"/>\
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\" Target=\"docProps/core.xml\"/></Relationships>";
let core = format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<cp:coreProperties xmlns:cp=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\" \
xmlns:dc=\"http://purl.org/dc/elements/1.1/\"><dc:title>{}</dc:title></cp:coreProperties>",
esc(title)
);
let mut pkg = PackageOut::default();
let mut root_rels = RelsOut::default();
root_rels.add("officeDocument", "ppt/presentation.xml", false);
root_rels.add("metadata/core-properties", "docProps/core.xml", false);
pkg.rels("", &root_rels);
let mut sld_id_lst = String::new();
let mut pres_rels = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rIdMaster1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster\" Target=\"slideMasters/slideMaster1.xml\"/>",
);
let mut pres_rels = RelsOut::default();
pres_rels.items.push(RelOut {
id: "rIdMaster1".into(),
ty: format!("{RT_BASE}slideMaster"),
target: "slideMasters/slideMaster1.xml".into(),
external: false,
});
for i in 1..=slides.len() {
let sld_id = 255 + i;
sld_id_lst.push_str(&format!("<p:sldId id=\"{sld_id}\" r:id=\"rIdSld{i}\"/>"));
pres_rels.push_str(&format!(
"<Relationship Id=\"rIdSld{i}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide\" Target=\"slides/slide{i}.xml\"/>"
));
pres_rels.items.push(RelOut {
id: format!("rIdSld{i}"),
ty: format!("{RT_BASE}slide"),
target: format!("slides/slide{i}.xml"),
external: false,
});
}
pres_rels.push_str("</Relationships>");
pkg.rels("ppt/presentation.xml", &pres_rels);
let pres = format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n\
"{DECL_STANDALONE}\
<p:presentation xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" \
xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"><p:sldMasterIdLst><p:sldMasterId id=\"2147483648\" r:id=\"rIdMaster1\"/></p:sldMasterIdLst>\
<p:sldIdLst>{sld_id_lst}</p:sldIdLst><p:sldSz cx=\"{sw}\" cy=\"{sh}\"/><p:notesSz cx=\"6858000\" cy=\"9144000\"/></p:presentation>"
);
let slide_master = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n\
let slide_master = format!(
"{DECL_STANDALONE}\
<p:sldMaster xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" \
xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"><p:cSld><p:spTree><p:nvGrpSpPr><p:cNvPr id=\"1\" name=\"\"/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr>\
<p:grpSpPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"0\" cy=\"0\"/><a:chOff x=\"0\" y=\"0\"/><a:chExt cx=\"0\" cy=\"0\"/></a:xfrm></p:grpSpPr></p:spTree></p:cSld>\
<p:clrMap bg1=\"lt1\" tx1=\"dk1\" bg2=\"lt2\" tx2=\"dk2\" accent1=\"accent1\" accent2=\"accent2\" accent3=\"accent3\" accent4=\"accent4\" accent5=\"accent5\" accent6=\"accent6\" hlink=\"hlink\" folHlink=\"folHlink\"/>\
<p:sldLayoutIdLst><p:sldLayoutId id=\"2147483649\" r:id=\"rIdLayout1\"/></p:sldLayoutIdLst></p:sldMaster>";
<p:sldLayoutIdLst><p:sldLayoutId id=\"2147483649\" r:id=\"rIdLayout1\"/></p:sldLayoutIdLst></p:sldMaster>"
);
let slide_master_rels = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rIdLayout1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout\" Target=\"../slideLayouts/slideLayout1.xml\"/>\
<Relationship Id=\"rIdTheme1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\" Target=\"../theme/theme1.xml\"/></Relationships>";
let mut slide_master_rels = RelsOut::default();
slide_master_rels.items.push(RelOut {
id: "rIdLayout1".into(),
ty: format!("{RT_BASE}slideLayout"),
target: "../slideLayouts/slideLayout1.xml".into(),
external: false,
});
slide_master_rels.items.push(RelOut {
id: "rIdTheme1".into(),
ty: format!("{RT_BASE}theme"),
target: "../theme/theme1.xml".into(),
external: false,
});
pkg.rels("ppt/slideMasters/slideMaster1.xml", &slide_master_rels);
let slide_layout = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n\
let slide_layout = format!(
"{DECL_STANDALONE}\
<p:sldLayout xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" \
xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" type=\"blank\"><p:cSld><p:spTree><p:nvGrpSpPr><p:cNvPr id=\"1\" name=\"\"/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr>\
<p:grpSpPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"0\" cy=\"0\"/><a:chOff x=\"0\" y=\"0\"/><a:chExt cx=\"0\" cy=\"0\"/></a:xfrm></p:grpSpPr></p:spTree></p:cSld></p:sldLayout>";
<p:grpSpPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"0\" cy=\"0\"/><a:chOff x=\"0\" y=\"0\"/><a:chExt cx=\"0\" cy=\"0\"/></a:xfrm></p:grpSpPr></p:spTree></p:cSld></p:sldLayout>"
);
let slide_layout_rels = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster\" Target=\"../slideMasters/slideMaster1.xml\"/></Relationships>";
let mut slide_layout_rels = RelsOut::default();
slide_layout_rels.add("slideMaster", "../slideMasters/slideMaster1.xml", false);
pkg.rels("ppt/slideLayouts/slideLayout1.xml", &slide_layout_rels);
let theme = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n\
let theme = format!(
"{DECL_STANDALONE}\
<a:theme xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" name=\"Office Theme\"><a:themeElements>\
<a:clrScheme name=\"Office\"><a:dk1><a:sysClr val=\"windowText\" lastClr=\"000000\"/></a:dk1><a:lt1><a:sysClr val=\"window\" lastClr=\"FFFFFF\"/></a:lt1>\
<a:dk2><a:srgbClr val=\"1F497D\"/></a:dk2><a:lt2><a:srgbClr val=\"EEECE1\"/></a:lt2><a:accent1><a:srgbClr val=\"4F81BD\"/></a:accent1>\
@@ -335,28 +345,32 @@ xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" type=\"blank\"
<a:lnStyleLst><a:ln w=\"9525\"><a:solidFill><a:schemeClr val=\"phClr\"/></a:solidFill></a:ln><a:ln w=\"25400\"><a:solidFill><a:schemeClr val=\"phClr\"/></a:solidFill></a:ln><a:ln w=\"38100\"><a:solidFill><a:schemeClr val=\"phClr\"/></a:solidFill></a:ln></a:lnStyleLst>\
<a:effectStyleLst><a:effectStyle><a:effectLst/></a:effectStyle><a:effectStyle><a:effectLst/></a:effectStyle><a:effectStyle><a:effectLst/></a:effectStyle></a:effectStyleLst>\
<a:bgFillStyleLst><a:solidFill><a:schemeClr val=\"phClr\"/></a:solidFill><a:solidFill><a:schemeClr val=\"phClr\"/></a:solidFill><a:solidFill><a:schemeClr val=\"phClr\"/></a:solidFill></a:bgFillStyleLst></a:fmtScheme>\
</a:themeElements></a:theme>";
</a:themeElements></a:theme>"
);
let mut z = Zip::default();
z.add("[Content_Types].xml", types.as_bytes(), true);
z.add("_rels/.rels", rels.as_bytes(), true);
z.add("docProps/core.xml", core.as_bytes(), true);
z.add("ppt/presentation.xml", pres.as_bytes(), true);
z.add("ppt/_rels/presentation.xml.rels", pres_rels.as_bytes(), true);
z.add("ppt/slideMasters/slideMaster1.xml", slide_master.as_bytes(), true);
z.add("ppt/slideMasters/_rels/slideMaster1.xml.rels", slide_master_rels.as_bytes(), true);
z.add("ppt/slideLayouts/slideLayout1.xml", slide_layout.as_bytes(), true);
z.add("ppt/slideLayouts/_rels/slideLayout1.xml.rels", slide_layout_rels.as_bytes(), true);
z.add("ppt/theme/theme1.xml", theme.as_bytes(), true);
pkg.xml_part("docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml", core_properties_xml(title));
pkg.xml_part("ppt/presentation.xml", "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml", pres.into_bytes());
pkg.xml_part(
"ppt/slideMasters/slideMaster1.xml",
"application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml",
slide_master.into_bytes(),
);
pkg.xml_part(
"ppt/slideLayouts/slideLayout1.xml",
"application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml",
slide_layout.into_bytes(),
);
pkg.xml_part("ppt/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml", theme.into_bytes());
for (i, (s_xml, s_rels)) in slides.iter().enumerate() {
let num = i + 1;
z.add(&format!("ppt/slides/slide{num}.xml"), s_xml.as_bytes(), true);
z.add(&format!("ppt/slides/_rels/slide{num}.xml.rels"), s_rels.as_bytes(), true);
let path = format!("ppt/slides/slide{num}.xml");
pkg.xml_part(&path, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml", s_xml.as_bytes().to_vec());
pkg.rels(&path, s_rels);
}
for (name, im) in &global_media {
z.add(&format!("ppt/media/{name}"), &im.bytes, false);
pkg.binary(&format!("ppt/media/{name}"), image_mime(&im.ext), im.bytes.to_vec());
}
z.finish()
pkg.finish().unwrap_or_default()
}
#[cfg(test)]
+1 -1
View File
@@ -21,7 +21,7 @@ thiserror.workspace = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# The page image is handed to the external tesseract process as a PNG.
png = "0.18"
craft-image-codecs = { workspace = true }
[dev-dependencies]
tempfile = "3.27"
+4 -8
View File
@@ -496,14 +496,10 @@ impl Recognizer for TesseractCli {
/// Encode an RGBA raster as PNG (the interchange format Tesseract reads best).
fn encode_png(image: &OcrImage) -> Result<Vec<u8>, OcrError> {
let mut out = Vec::new();
let mut enc = png::Encoder::new(&mut out, image.width, image.height);
enc.set_color(png::ColorType::Rgba);
enc.set_depth(png::BitDepth::Eight);
let mut writer = enc.write_header().map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))?;
writer.write_image_data(&image.rgba).map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))?;
writer.finish().map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))?;
Ok(out)
let img = craft_image_codecs::Image::from_u8(image.width, image.height, craft_image_codecs::ChannelLayout::Rgba, image.rgba.clone())
.map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))?;
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
.map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))
}
/// Make a just-created file owner-only, where the OS supports it (Unix). Windows relies on the
+5 -4
View File
@@ -17,10 +17,11 @@ pdfcraft-content = { workspace = true }
pdfcraft-model = { workspace = true }
pdfcraft-annot = { workspace = true }
thiserror.workspace = true
# JPEG decoding (zune-jpeg) and encoding, and bicubic resampling; already in the tree.
image = { version = "0.25", default-features = false, features = ["jpeg"] }
# The same JPEG decoder, used directly to read samples untransformed (/ColorTransform 0).
zune-jpeg = "0.5"
# Bicubic resampling; zero codec features.
image = { version = "0.25", default-features = false }
# JPEG decoding and encoding via craft-libs.
craft-jpeg = { workspace = true }
craft-image-codecs = { workspace = true }
# Lets other programs go first while a copy is being made.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
+27 -21
View File
@@ -588,10 +588,15 @@ const MAX_HEADER_STEPS: usize = 4096;
/// by the step count, not by the size of the stream. That matters: Optimize asks again for every
/// group of fragments it tries, and the same image can be drawn many times.
fn adobe_transform(data: &[u8]) -> Option<Option<u8>> {
jpeg_header_info(data).map(|(flag, _)| flag)
}
fn jpeg_header_info(data: &[u8]) -> Option<(Option<u8>, bool)> {
if data.get(..2)? != [0xFF, 0xD8] {
return None;
}
let mut flag = None;
let mut rgb_ids = false;
let mut i = 2usize;
// Reading past the end returns.
for _ in 0..MAX_HEADER_STEPS {
@@ -606,7 +611,7 @@ fn adobe_transform(data: &[u8]) -> Option<Option<u8>> {
// A fill byte before the marker.
0xFF => i = i.checked_add(1)?,
// The first scan (or the end): no header segments after it.
0xDA | 0xD9 => return Some(flag),
0xDA | 0xD9 => return Some((flag, rgb_ids)),
// Markers without a segment: TEM, RSTn, SOI.
0x01 | 0xD0..=0xD8 => i = i.checked_add(2)?,
_ => {
@@ -619,6 +624,8 @@ fn adobe_transform(data: &[u8]) -> Option<Option<u8>> {
{
// Version, flags0 and flags1 (two bytes each), then the transform flag.
flag = Some(*rest.get(6)?);
} else if matches!(marker, 0xC0..=0xC3 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF) && segment.get(5) == Some(&3) {
rgb_ids = [segment.get(6).copied(), segment.get(9).copied(), segment.get(12).copied()] == [Some(b'R'), Some(b'G'), Some(b'B')];
}
i = end;
}
@@ -632,27 +639,21 @@ fn adobe_transform(data: &[u8]) -> Option<Option<u8>> {
/// of components), is too large, or its decoder reads the samples otherwise (for example
/// components named R, G and B, which decoders take as stored, while `ycc` says YCbCr).
fn jpeg_samples(data: &[u8], w: usize, h: usize, n: usize, ycc: bool) -> Option<Vec<u8>> {
use zune_jpeg::zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions};
if (w.checked_mul(h)? as u64) > MAX_PIXELS {
return None;
}
let options = DecoderOptions::default().set_strict_mode(false).set_max_width(usize::from(u16::MAX)).set_max_height(usize::from(u16::MAX));
let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(data), options);
dec.decode_headers().ok()?;
if dec.dimensions()? != (w, h) || usize::from(dec.info()?.components) != n {
if ycc && n == 3 && jpeg_header_info(data)?.1 {
return None;
}
let out = match (n, ycc, dec.input_colorspace()?) {
(1, _, ColorSpace::Luma) => ColorSpace::Luma,
(3, true, ColorSpace::YCbCr) => ColorSpace::RGB,
// As stored: no conversion either way. The decoder reports an Adobe marker's 0 as CMYK
// until it decodes, and then reads three such components as RGB.
(3, false, ColorSpace::YCbCr) => ColorSpace::YCbCr,
(3, false, ColorSpace::RGB | ColorSpace::CMYK) => ColorSpace::RGB,
_ => return None,
let mut limits = craft_jpeg::Limits::default();
limits.max_pixels = MAX_PIXELS;
let dec = craft_jpeg::decode(data, &limits, if ycc { Some(1) } else { Some(0) }).ok()?;
if (dec.width as usize, dec.height as usize) != (w, h) || usize::from(dec.components) != n {
return None;
}
let craft_jpeg::Samples::U8(px) = dec.samples else {
return None;
};
dec.set_options(options.jpeg_set_out_colorspace(out));
let px = dec.decode().ok()?;
(px.len() == w.checked_mul(h)?.checked_mul(n)?).then_some(px)
}
@@ -681,11 +682,16 @@ fn scaled<P: image::Pixel<Subpixel = u8> + 'static>(img: &image::ImageBuffer<P,
}
pub(crate) fn jpeg(px: &[u8], n: usize, w: u32, h: u32, quality: u8) -> Option<Vec<u8>> {
let mut out = Vec::new();
let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, quality.clamp(1, 100));
let ty = if n == 1 { image::ExtendedColorType::L8 } else { image::ExtendedColorType::Rgb8 };
enc.encode(px, w, h, ty).ok()?;
Some(out)
let layout = if n == 1 { craft_image_codecs::ChannelLayout::Gray } else { craft_image_codecs::ChannelLayout::Rgb };
let img = craft_image_codecs::Image::from_u8(w, h, layout, px.to_vec()).ok()?;
let opts = craft_image_codecs::EncodeOptions {
jpeg_quality: quality.clamp(1, 100),
jpeg_format: craft_image_codecs::JpegFormat::Optimized,
embed_icc: false,
embed_metadata: false,
..Default::default()
};
craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).ok()
}
/// Whether [`pixels`] reads `s` as it is drawn: a codec it decodes, no decode array that
@@ -27,9 +27,9 @@ fn photo(w: u32, h: u32) -> Vec<u8> {
}
fn rgb_jpeg(px: &[u8], w: u32, h: u32) -> Vec<u8> {
let mut out = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 100).encode(px, w, h, image::ExtendedColorType::Rgb8).unwrap();
out
let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, px.to_vec()).unwrap();
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 100, ..Default::default() };
craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap()
}
/// The JPEG's segments before its first scan: (marker, start of the marker, end of the segment).
+7 -5
View File
@@ -39,9 +39,10 @@ fn image(doc: &mut Document, w: u32, h: u32, n: usize, jpeg: bool, smask: Option
d.set(b"SMask".to_vec(), Object::Ref(m));
}
let s = if jpeg {
let mut out = Vec::new();
let ty = if n == 1 { image::ExtendedColorType::L8 } else { image::ExtendedColorType::Rgb8 };
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 98).encode(&px, w, h, ty).unwrap();
let layout = if n == 1 { craft_image_codecs::ChannelLayout::Gray } else { craft_image_codecs::ChannelLayout::Rgb };
let img = craft_image_codecs::Image::from_u8(w, h, layout, px).unwrap();
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 98, ..Default::default() };
let out = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap();
d.set(b"Filter".to_vec(), Object::name("DCTDecode"));
Stream::from_raw(d, out)
} else {
@@ -3886,8 +3887,9 @@ fn strip(doc: &mut Document, w: u32, v: u8, jpeg: bool) -> ObjRef {
d.set(b"ColorSpace".to_vec(), Object::name("DeviceGray"));
let px = vec![v; w as usize];
let s = if jpeg {
let mut out = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 95).encode(&px, w, 1, image::ExtendedColorType::L8).unwrap();
let img = craft_image_codecs::Image::from_u8(w, 1, craft_image_codecs::ChannelLayout::Gray, px).unwrap();
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 95, ..Default::default() };
let out = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap();
d.set(b"Filter".to_vec(), Object::name("DCTDecode"));
Stream::from_raw(d, out)
} else {
+3 -2
View File
@@ -151,8 +151,9 @@ fn a_stencil_mask_paints_the_fill_colour_through_its_tiles() {
#[test]
fn a_jpeg_is_decoded_into_its_tiles() {
let px = texture(W, H, 3);
let mut jpeg = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 95).encode(&px, W, H, image::ExtendedColorType::Rgb8).unwrap();
let img = craft_image_codecs::Image::from_u8(W, H, craft_image_codecs::ChannelLayout::Rgb, px).unwrap();
let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 95, jpeg_chroma_subsampling: false, ..Default::default() };
let jpeg = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap();
let (doc, im) = page_drawing(W, H, "", |doc| {
let d = image_dict(
W,
+1 -1
View File
@@ -11,10 +11,10 @@ repository.workspace = true
workspace = true
[dependencies]
craft-xml = { workspace = true }
pdfcraft-content = { workspace = true }
pdfcraft-cos = { workspace = true }
pdfcraft-model = { workspace = true }
roxmltree = "0.21"
[dev-dependencies]
flate2 = { workspace = true }
+71 -10
View File
@@ -29,30 +29,91 @@ pub(crate) fn identifier(xmp: &str, namespace: &str, local: &str) -> Option<Stri
}
}
/// Namespace scope tracking for `craft_xml::El` trees.
#[derive(Clone)]
pub(crate) struct NsScope<'a>(Vec<(&'a str, &'a str)>);
impl<'a> NsScope<'a> {
pub(crate) fn root() -> Self {
Self(vec![("xml", "http://www.w3.org/XML/1998/namespace")])
}
pub(crate) fn child(&self, el: &'a craft_xml::El) -> Self {
let mut next = self.0.clone();
next.extend(el.ns_decls());
Self(next)
}
pub(crate) fn el_ns(&self, el: &craft_xml::El) -> Option<&'a str> {
let prefix = el.prefix();
self.0.iter().rfind(|(p, _)| *p == prefix).map(|(_, u)| *u).filter(|u| !u.is_empty())
}
pub(crate) fn attr_ns(&self, attr_name: &str) -> Option<&'a str> {
let (prefix, _) = attr_name.split_once(':')?;
if prefix == "xmlns" {
return None;
}
self.0.iter().rfind(|(p, _)| *p == prefix).map(|(_, u)| *u).filter(|u| !u.is_empty())
}
pub(crate) fn attr(&self, el: &'a craft_xml::El, ns: &str, local: &str) -> Option<&'a str> {
el.attrs.iter().find_map(|(k, v)| {
let (_, l) = k.split_once(':')?;
(l == local && self.attr_ns(k) == Some(ns)).then_some(v.as_str())
})
}
pub(crate) fn is(&self, el: &craft_xml::El, ns: &str, local: &str) -> bool {
el.local() == local && self.el_ns(el) == Some(ns)
}
}
/// `xmp` parsed within the depth, attribute and node limits; `None` when it can't be.
pub(crate) fn parse(xmp: &str) -> Option<roxmltree::Document<'_>> {
pub(crate) fn parse(xmp: &str) -> Option<craft_xml::El> {
if !xml_limits_ok(xmp) {
return None;
}
let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: 100_000, ..Default::default() };
roxmltree::Document::parse_with_options(xmp, options).ok()
let options = craft_xml::Options { doctype: craft_xml::Doctype::Reject, ..craft_xml::Options::default() }
.strict()
.with_limits(craft_xml::Limits { max_depth: 1024, max_nodes: 100_000, max_attrs_total: 100_000, ..craft_xml::Limits::DEFAULT });
options.parse(xmp.as_bytes()).ok()
}
/// [`identifier`] through an XML parser; `Err` when the packet can't be parsed within the limits.
fn parsed_identifier(xmp: &str, namespace: &str, local: &str) -> Result<Option<String>, ()> {
let doc = parse(xmp).ok_or(())?;
for node in doc.descendants().filter(roxmltree::Node::is_element) {
if let Some(value) = node.attribute((namespace, local)) {
fn walk<'a>(el: &'a craft_xml::El, parent: &NsScope<'a>, namespace: &str, local: &str) -> Result<Option<String>, ()> {
let scope = parent.child(el);
let el_prefix = el.prefix();
if !el_prefix.is_empty() && el_prefix != "xml" && scope.el_ns(el).is_none() {
return Err(());
}
for (k, _) in &el.attrs {
if let Some((prefix, _)) = k.split_once(':')
&& !matches!(prefix, "xml" | "xmlns")
&& scope.attr_ns(k).is_none()
{
return Err(());
}
}
if let Some(value) = scope.attr(el, namespace, local) {
return Ok(Some(value.trim().to_owned()));
}
if node.tag_name().namespace() == Some(namespace) && node.tag_name().name() == local {
return Ok(node.text().map(|value| value.trim().to_owned()));
if scope.is(el, namespace, local) {
return Ok(Some(el.text().trim().to_owned()));
}
for child in el.elements() {
if let Some(found) = walk(child, &scope, namespace, local)? {
return Ok(Some(found));
}
}
Ok(None)
}
Ok(None)
let root = parse(xmp).ok_or(())?;
walk(&root, &NsScope::root(), namespace, local)
}
/// Reject deep XML and start tags with too many attributes before roxmltree checks duplicates.
/// Reject deep XML and start tags with too many attributes before parsing.
/// Each unquoted `=` in a valid start tag introduces an attribute, including namespace bindings.
/// Every `b[i]` and `hay[p..]` below is guarded by `i < b.len()` / `p < hay.len()`.
fn xml_limits_ok(text: &str) -> bool {
+115 -61
View File
@@ -23,8 +23,9 @@
//! (4.4), and an empty prefix or namespace binds nothing. The human-readable descriptions
//! (`pdfaSchema:schema` and the `description` entries) are Text and may be empty.
use crate::xmp::NsScope;
use craft_xml::El;
use pdfcraft_cos::ObjRef;
use roxmltree::{Document, Node};
const RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const EXTENSION: &str = "http://www.aiim.org/pdfa/ns/extension/";
@@ -103,31 +104,55 @@ struct Place {
impl Findings {
/// Adds what the packet `doc`, from the metadata stream `object`, breaks.
pub(crate) fn add(&mut self, object: ObjRef, doc: &Document<'_>) {
let containers = doc.descendants().filter(|n| is(n, EXTENSION, "schemas"));
for container in containers {
self.prefixes_in(object, doc, &container);
let children: Vec<Node<'_, '_>> = container.children().filter(Node::is_element).collect();
if !matches!(children.as_slice(), [bag] if is(bag, RDF, "Bag")) && self.room() {
let found = children.iter().take(MAX_SHOWN_CHARS).map(|c| written_name(doc, c)).collect::<Vec<_>>().join(", ");
pub(crate) fn add(&mut self, object: ObjRef, doc: &El) {
fn walk_containers<'a>(el: &'a El, parent: &NsScope<'a>, out: &mut Vec<(&'a El, NsScope<'a>)>) {
let scope = parent.child(el);
if scope.is(el, EXTENSION, "schemas") {
out.push((el, scope.clone()));
}
for child in el.elements() {
walk_containers(child, &scope, out);
}
}
let mut containers = Vec::new();
walk_containers(doc, &NsScope::root(), &mut containers);
for (container, cscope) in containers {
self.prefixes_in(object, container, &cscope);
let children: Vec<(&El, NsScope<'_>)> = container.elements().map(|c| (c, cscope.child(c))).collect();
if !matches!(children.as_slice(), [(bag, bscope)] if is(bag, bscope, RDF, "Bag")) && self.room() {
let found = children.iter().take(MAX_SHOWN_CHARS).map(|(c, _)| c.name.as_str()).collect::<Vec<_>>().join(", ");
self.not_bag.push(NotBag { object, found: shown(&found) });
}
for (n, schema) in items(&container).iter().enumerate() {
let id = value(schema, SCHEMA, "namespaceURI").unwrap_or_else(|| position(n));
for (n, (schema, sscope)) in items(container, &cscope).into_iter().enumerate() {
let id = value(schema, &sscope, SCHEMA, "namespaceURI").unwrap_or_else(|| position(n));
let at = |item: Option<String>| Place { object, schema: id.clone(), item };
self.need(&at(None), schema, SCHEMA, "pdfaSchema", &["schema", "namespaceURI", "prefix"]);
let properties = entries(schema, SCHEMA, "property").flat_map(|e| items(&e));
for (n, property) in properties.enumerate() {
let name = value(&property, PROPERTY, "name").unwrap_or_else(|| position(n));
self.need(&at(Some(name)), &property, PROPERTY, "pdfaProperty", &["name", "valueType", "category", "description"]);
self.need(&at(None), schema, &sscope, SCHEMA, "pdfaSchema", &["schema", "namespaceURI", "prefix"]);
let properties = entries(schema, &sscope, SCHEMA, "property").flat_map(|(e, es)| items(e, &es));
for (n, (property, pscope)) in properties.enumerate() {
let name = value(property, &pscope, PROPERTY, "name").unwrap_or_else(|| position(n));
self.need(&at(Some(name)), property, &pscope, PROPERTY, "pdfaProperty", &["name", "valueType", "category", "description"]);
}
let value_types = entries(schema, SCHEMA, "valueType").flat_map(|e| items(&e));
for (n, value_type) in value_types.enumerate() {
let name = value(&value_type, TYPE, "type").unwrap_or_else(|| position(n));
self.need(&at(Some(name.clone())), &value_type, TYPE, "pdfaType", &["type", "namespaceURI", "prefix", "description", "field"]);
for (n, field) in entries(&value_type, TYPE, "field").flat_map(|e| items(&e)).enumerate() {
let field_name = value(&field, FIELD, "name").unwrap_or_else(|| position(n));
self.need(&at(Some(format!("{name}/{field_name}"))), &field, FIELD, "pdfaField", &["name", "valueType", "description"]);
let value_types = entries(schema, &sscope, SCHEMA, "valueType").flat_map(|(e, es)| items(e, &es));
for (n, (value_type, vtscope)) in value_types.enumerate() {
let name = value(value_type, &vtscope, TYPE, "type").unwrap_or_else(|| position(n));
self.need(
&at(Some(name.clone())),
value_type,
&vtscope,
TYPE,
"pdfaType",
&["type", "namespaceURI", "prefix", "description", "field"],
);
for (n, (field, fscope)) in entries(value_type, &vtscope, TYPE, "field").flat_map(|(e, es)| items(e, &es)).enumerate() {
let field_name = value(field, &fscope, FIELD, "name").unwrap_or_else(|| position(n));
self.need(
&at(Some(format!("{name}/{field_name}"))),
field,
&fscope,
FIELD,
"pdfaField",
&["name", "valueType", "description"],
);
}
}
}
@@ -141,13 +166,13 @@ impl Findings {
/// Notes the entries of `ns` in `names` that the description `d`, at `at`, lacks, or holds
/// empty when they are entries that name something (see [`NAMING`]).
fn need(&mut self, at: &Place, d: &Node<'_, '_>, ns: &str, prefix: &str, names: &[&str]) {
fn need(&mut self, at: &Place, d: &El, scope: &NsScope<'_>, ns: &str, prefix: &str, names: &[&str]) {
for name in names {
let element = entry(d, ns, name);
let attribute = d.attribute((ns, *name));
let empty = match (element, attribute) {
let element = entry(d, scope, ns, name);
let attribute = scope.attr(d, ns, name);
let empty = match (element.as_ref(), attribute) {
(None, None) => false,
(Some(e), _) => NAMING.contains(name) && is_empty(&e),
(Some((e, es)), _) => NAMING.contains(name) && is_empty(e, es),
(None, Some(a)) => NAMING.contains(name) && a.trim().is_empty(),
};
if (empty || (element.is_none() && attribute.is_none())) && self.room() {
@@ -160,22 +185,25 @@ impl Findings {
/// Notes container namespaces written in `container` (itself included) with another prefix
/// than their own.
fn prefixes_in(&mut self, object: ObjRef, doc: &Document<'_>, container: &Node<'_, '_>) {
let text = doc.input_text();
for n in container.descendants().filter(Node::is_element) {
let element = (n.tag_name().namespace(), text.get(n.range()).unwrap_or_default().strip_prefix('<').unwrap_or_default());
let attributes = n.attributes().map(|a| (a.namespace(), text.get(a.range_qname()).unwrap_or_default()));
fn prefixes_in(&mut self, object: ObjRef, container: &El, scope: &NsScope<'_>) {
fn walk(findings: &mut Findings, object: ObjRef, n: &El, scope: &NsScope<'_>) {
let element = (scope.el_ns(n), n.name.as_str());
let attributes = n.attrs.iter().map(|(k, _)| (scope.attr_ns(k), k.as_str()));
for (ns, qname) in std::iter::once(element).chain(attributes) {
let Some(&(namespace, own)) = PREFIXES.iter().find(|(uri, _)| Some(*uri) == ns) else { continue };
let written = shown(written_prefix(qname));
if written != own
&& self.room()
&& !self.prefixes.iter().any(|p| p.object == object && p.namespace == namespace && p.written == written)
&& findings.room()
&& !findings.prefixes.iter().any(|p| p.object == object && p.namespace == namespace && p.written == written)
{
self.prefixes.push(Prefix { object, namespace, own, written });
findings.prefixes.push(Prefix { object, namespace, own, written });
}
}
for child in n.elements() {
walk(findings, object, child, &scope.child(child));
}
}
walk(self, object, container, scope);
}
}
@@ -186,11 +214,14 @@ const NAMING: [&str; 6] = ["namespaceURI", "prefix", "name", "type", "valueType"
/// Whether the element entry `e` holds no value: no element, no text but white space, and no
/// `rdf:resource` or `rdf:value` attribute with a value.
fn is_empty(e: &Node<'_, '_>) -> bool {
fn is_empty(e: &El, scope: &NsScope<'_>) -> bool {
let blank = |v: Option<&str>| v.is_none_or(|v| v.trim().is_empty());
!e.children().any(|c| c.is_element() || (c.is_text() && c.text().is_some_and(|t| !t.trim().is_empty())))
&& blank(e.attribute((RDF, "resource")))
&& blank(e.attribute((RDF, "value")))
!e.children.iter().any(|c| match c {
craft_xml::Node::El(_) => true,
craft_xml::Node::Text(t) | craft_xml::Node::CData(t) => !t.trim().is_empty(),
_ => false,
}) && blank(scope.attr(e, RDF, "resource"))
&& blank(scope.attr(e, RDF, "value"))
}
/// `#n` for the 0-based position `n`, counted from 1.
@@ -209,43 +240,66 @@ fn shown(s: &str) -> String {
/// The text of the entry `name` of `ns` in the description `d`, written as an element or an
/// attribute, trimmed and [`shown`]; `None` when it is absent or blank.
fn value(d: &Node<'_, '_>, ns: &str, name: &str) -> Option<String> {
let text = entry(d, ns, name).and_then(|e| e.text()).or_else(|| d.attribute((ns, name)))?.trim();
fn value(d: &El, scope: &NsScope<'_>, ns: &str, name: &str) -> Option<String> {
let owned;
let raw = match entry(d, scope, ns, name) {
Some((e, _)) => {
owned = e.text();
owned.as_ref()
}
None => scope.attr(d, ns, name)?,
};
let text = raw.trim();
(!text.is_empty()).then(|| shown(text))
}
/// The name of the element `n` as the packet writes it, prefix included.
fn written_name<'i>(doc: &Document<'i>, n: &Node<'_, '_>) -> &'i str {
qualified_name(doc.input_text().get(n.range()).unwrap_or_default().strip_prefix('<').unwrap_or_default())
}
/// The qualified name the text of a start tag (after its `<`) or of an attribute begins with.
fn qualified_name(text: &str) -> &str {
text.split(|c: char| c.is_whitespace() || c == '>' || c == '/' || c == '=').next().unwrap_or_default()
}
/// The prefix of the qualified name `text` begins with, empty when it has none.
fn written_prefix(text: &str) -> &str {
qualified_name(text).split_once(':').map_or("", |(p, _)| p)
text.split_once(':').map_or("", |(p, _)| p)
}
fn is(n: &Node<'_, '_>, ns: &str, name: &str) -> bool {
n.is_element() && n.tag_name().namespace() == Some(ns) && n.tag_name().name() == name
fn is(n: &El, scope: &NsScope<'_>, ns: &str, name: &str) -> bool {
scope.is(n, ns, name)
}
/// Every element entry `name` of `ns` in the description `d`: a list written twice is read twice.
fn entries<'a, 'i>(d: &Node<'a, 'i>, ns: &'a str, name: &'a str) -> impl Iterator<Item = Node<'a, 'i>> {
d.children().filter(move |c| is(c, ns, name))
fn entries<'a>(d: &'a El, scope: &'a NsScope<'a>, ns: &'a str, name: &'a str) -> impl Iterator<Item = (&'a El, NsScope<'a>)> {
d.elements().filter_map(move |c| {
let cs = scope.child(c);
is(c, &cs, ns, name).then_some((c, cs))
})
}
/// The element entry `name` of `ns` in the description `d`.
fn entry<'a, 'i>(d: &Node<'a, 'i>, ns: &str, name: &str) -> Option<Node<'a, 'i>> {
d.children().find(|c| is(c, ns, name))
fn entry<'a>(d: &'a El, scope: &NsScope<'a>, ns: &str, name: &str) -> Option<(&'a El, NsScope<'a>)> {
d.elements().find_map(|c| {
let cs = scope.child(c);
is(c, &cs, ns, name).then_some((c, cs))
})
}
/// The descriptions in the array `e` holds: each item's `rdf:parseType="Resource"` node or
/// `rdf:Description`.
fn items<'a, 'i>(e: &Node<'a, 'i>) -> Vec<Node<'a, 'i>> {
let Some(array) = e.children().find(|c| is(c, RDF, "Bag") || is(c, RDF, "Seq") || is(c, RDF, "Alt")) else { return Vec::new() };
array.children().filter(|i| is(i, RDF, "li")).map(|li| li.children().find(|c| is(c, RDF, "Description")).unwrap_or(li)).collect()
fn items<'a>(e: &'a El, scope: &NsScope<'a>) -> Vec<(&'a El, NsScope<'a>)> {
let Some((array, ascope)) = e.elements().find_map(|c| {
let cs = scope.child(c);
(is(c, &cs, RDF, "Bag") || is(c, &cs, RDF, "Seq") || is(c, &cs, RDF, "Alt")).then_some((c, cs))
}) else {
return Vec::new();
};
array
.elements()
.filter_map(|li| {
let ls = ascope.child(li);
is(li, &ls, RDF, "li").then_some((li, ls))
})
.map(|(li, ls)| {
li.elements()
.find_map(|c| {
let cs = ls.child(c);
is(c, &cs, RDF, "Description").then_some((c, cs))
})
.unwrap_or((li, ls))
})
.collect()
}
+102 -72
View File
@@ -22,8 +22,9 @@
use std::collections::BTreeMap;
use crate::xmp::NsScope;
use craft_xml::El;
use pdfcraft_cos::{DecodeBudget, ObjRef, Object};
use roxmltree::Node;
/// How deep arrays, structures and qualifiers are followed. No type nests nearly this deep, so a
/// value nested deeper doesn't have its type.
@@ -595,7 +596,7 @@ const SCHEMAS: &[Schema] = &[
/// Calls `f` with every metadata stream among the indirect objects `objs` (ISO 32000-1 14.3.2) that
/// parses as XML, and the stream's object, each decoded once within one allowance.
pub(crate) fn for_each_packet(objs: &[(ObjRef, std::sync::Arc<Object>)], mut f: impl FnMut(ObjRef, &roxmltree::Document<'_>)) {
pub(crate) fn for_each_packet(objs: &[(ObjRef, std::sync::Arc<Object>)], mut f: impl FnMut(ObjRef, &El)) {
let mut budget = DecodeBudget::new(MAX_METADATA_BYTES, 0);
for (r, o) in objs {
let Object::Stream(s) = &**o else { continue };
@@ -632,29 +633,34 @@ fn unicode(bytes: &[u8]) -> String {
/// The properties of predefined schemas in the packet `doc` whose values don't have their type, in
/// name order: each by its name with the namespace's preferred prefix and by its name as the packet
/// writes it (see [`written`]), with the type it should have as [`named`] gives it.
pub(crate) fn mistyped(doc: &roxmltree::Document<'_>) -> BTreeMap<(String, String), String> {
let mut out = BTreeMap::new();
let text = doc.input_text();
let descriptions = doc.descendants().filter(|n| is(n, RDF, "Description") && n.parent_element().is_some_and(|p| is(&p, RDF, "RDF")));
for d in descriptions {
for a in d.attributes() {
if let Some((schema, ty)) = a.namespace().and_then(|ns| property(ns, a.name()))
&& !simple(ty, a.value())
{
let qname = text.get(a.range_qname()).unwrap_or_default();
out.insert((format!("{}:{}", schema.prefix, a.name()), written(qname, a.name())), named(ty));
pub(crate) fn mistyped(doc: &El) -> BTreeMap<(String, String), String> {
fn walk<'a>(el: &'a El, parent_scope: &NsScope<'a>, parent_is_rdf: bool, out: &mut BTreeMap<(String, String), String>) {
let scope = parent_scope.child(el);
if parent_is_rdf && scope.is(el, RDF, "Description") {
for (k, v) in &el.attrs {
let local = k.split_once(':').map_or(k.as_str(), |(_, l)| l);
if let Some((schema, ty)) = scope.attr_ns(k).and_then(|ns| property(ns, local))
&& !simple(ty, v)
{
out.insert((format!("{}:{}", schema.prefix, local), written(k, local)), named(ty));
}
}
for p in el.elements() {
let p_scope = scope.child(p);
if let Some((schema, ty)) = p_scope.el_ns(p).and_then(|ns| property(ns, p.local()))
&& !fits(ty, p, &p_scope, 0)
{
out.insert((format!("{}:{}", schema.prefix, p.local()), written(&p.name, p.local())), named(ty));
}
}
}
for p in d.children().filter(Node::is_element) {
let name = p.tag_name();
if let Some((schema, ty)) = name.namespace().and_then(|ns| property(ns, name.name()))
&& !fits(ty, &p, 0)
{
let qname = text.get(p.range()).unwrap_or_default().strip_prefix('<').unwrap_or_default();
out.insert((format!("{}:{}", schema.prefix, name.name()), written(qname, name.name())), named(ty));
}
let is_rdf = scope.is(el, RDF, "RDF");
for child in el.elements() {
walk(child, &scope, is_rdf, out);
}
}
let mut out = BTreeMap::new();
walk(doc, &NsScope::root(), false, &mut out);
out
}
@@ -702,62 +708,69 @@ fn property(ns: &str, name: &str) -> Option<(&'static Schema, Ty)> {
schema.properties.iter().find(|(n, _)| *n == name).map(|(_, t)| (schema, *t))
}
fn is(n: &Node<'_, '_>, ns: &str, name: &str) -> bool {
n.is_element() && n.tag_name().namespace() == Some(ns) && n.tag_name().name() == name
fn is<'a>(n: &El, scope: &NsScope<'a>, ns: &str, name: &str) -> bool {
scope.is(n, ns, name)
}
/// The attributes of `n` that are fields or qualifiers, not RDF or XML syntax.
fn field_attributes<'a, 'i>(n: &Node<'a, 'i>) -> impl Iterator<Item = roxmltree::Attribute<'a, 'i>> {
n.attributes().filter(|a| !matches!(a.namespace(), Some(RDF) | Some(XML)))
fn field_attributes<'a>(n: &'a El, scope: &'a NsScope<'a>) -> impl Iterator<Item = (&'a str, &'a str)> + 'a {
n.attrs
.iter()
.filter(move |(k, _)| k != "xmlns" && !k.starts_with("xmlns:") && !matches!(scope.attr_ns(k), Some(RDF) | Some(XML)))
.map(|(k, v)| (k.as_str(), v.as_str()))
}
/// Whether the value of the property or array item element `e` has type `ty`.
fn fits(ty: Ty, e: &Node<'_, '_>, depth: usize) -> bool {
fn fits<'a>(ty: Ty, e: &'a El, scope: &NsScope<'a>, depth: usize) -> bool {
if depth > MAX_DEPTH {
return false;
}
let deeper = depth + 1;
let children: Vec<Node<'_, '_>> = e.children().filter(Node::is_element).collect();
let children: Vec<(&'a El, NsScope<'a>)> = e.elements().map(|c| (c, scope.child(c))).collect();
// A qualified value: `rdf:value` holds the value itself (ISO 16684-1 7.9.2.5).
let resource = e.attribute((RDF, "parseType")) == Some("Resource");
let resource = scope.attr(e, RDF, "parseType") == Some("Resource");
// `parseType="Resource"` is the only parse type XMP has (ISO 16684-1 7.9.2.5); an
// `rdf:resource` element is empty.
if e.attribute((RDF, "parseType")).is_some_and(|p| p != "Resource") || e.attribute((RDF, "resource")).is_some() && !children.is_empty() {
if scope.attr(e, RDF, "parseType").is_some_and(|p| p != "Resource") || scope.attr(e, RDF, "resource").is_some() && !children.is_empty() {
return false;
}
let description = match children.as_slice() {
[d] if is(d, RDF, "Description") => Some(*d),
[(d, ds)] if is(d, ds, RDF, "Description") => Some((*d, ds.clone())),
_ => None,
};
// `rdf:value` as an element of a `parseType="Resource"` node or `rdf:Description`, or as an
// attribute of the description or of an empty property element. Its qualifiers that are
// predefined properties keep their types too.
let node = if resource { Some(*e) } else { description };
if let Some(n) = node {
let node = if resource { Some((e, scope.clone())) } else { description.clone() };
if let Some((n, ns)) = node {
// A qualified value has one `rdf:value`: an element, or an attribute in its place.
match n.children().filter(|c| is(c, RDF, "value")).collect::<Vec<_>>().as_slice() {
let vals: Vec<_> = n.elements().filter(|c| is(c, &ns.child(c), RDF, "value")).collect();
match vals.as_slice() {
[] => {}
[v] => return n.attribute((RDF, "value")).is_none() && blank(e) && blank(&n) && fits(ty, v, deeper) && predefined_ok(&n, deeper),
[v] => {
let vs = ns.child(v);
return ns.attr(n, RDF, "value").is_none() && blank(e) && blank(n) && fits(ty, v, &vs, deeper) && predefined_ok(n, &ns, deeper);
}
_ => return false,
}
}
let attribute_node = description.or_else(|| (children.is_empty() && !resource).then_some(*e));
if let Some(n) = attribute_node
&& let Some(v) = n.attribute((RDF, "value"))
let attribute_node = description.as_ref().map(|(d, ds)| (*d, ds)).or_else(|| (children.is_empty() && !resource).then_some((e, scope)));
if let Some((n, ns)) = attribute_node
&& let Some(v) = ns.attr(n, RDF, "value")
{
return blank(e) && blank(&n) && simple(ty, v) && predefined_ok(&n, deeper);
return blank(e) && blank(n) && simple(ty, v) && predefined_ok(n, ns, deeper);
}
match ty {
Ty::Text | Ty::Guid | Ty::Integer | Ty::Real | Ty::Boolean | Ty::Date | Ty::Rational | Ty::GpsCoordinate => {
// `rdf:resource` makes the value a URI, which only a text type takes.
// The element is then empty.
if e.attribute((RDF, "resource")).is_some() {
if scope.attr(e, RDF, "resource").is_some() {
return matches!(ty, Ty::Text) && children.is_empty() && blank(e);
}
children.is_empty() && !resource && field_attributes(e).next().is_none() && simple(ty, &text(e))
children.is_empty() && !resource && field_attributes(e, scope).next().is_none() && simple(ty, &text(e))
}
Ty::LangAlt => match array(e, &children, "Alt") {
Some(items) => items.iter().all(|i| i.attribute((XML, "lang")).is_some() && fits(Ty::Text, i, deeper)),
Ty::LangAlt => match array(e, scope, &children, "Alt") {
Some(items) => items.iter().all(|(i, iscope)| iscope.attr(i, XML, "lang").is_some() && fits(Ty::Text, i, iscope, deeper)),
None => false,
},
Ty::Seq(item) | Ty::Bag(item) | Ty::Alt(item) => {
@@ -766,18 +779,21 @@ fn fits(ty: Ty, e: &Node<'_, '_>, depth: usize) -> bool {
Ty::Bag(_) => "Bag",
_ => "Alt",
};
array(e, &children, kind).is_some_and(|items| items.iter().all(|i| fits(*item, i, deeper)))
array(e, scope, &children, kind).is_some_and(|items| items.iter().all(|(i, iscope)| fits(*item, i, iscope, deeper)))
}
Ty::Struct(_, fields) => {
let Some(holder) = structure(e, &children, resource, description) else { return false };
let attrs_ok = field_attributes(&holder).all(|a| match a.namespace().and_then(|ns| field(fields, ns, a.name())) {
Some(t) => simple(t, a.value()),
None => true,
let Some((holder, hscope)) = structure(e, scope, &children, resource, description.as_ref()) else { return false };
let attrs_ok = field_attributes(holder, &hscope).all(|(k, v)| {
let local = k.split_once(':').map_or(k, |(_, l)| l);
match hscope.attr_ns(k).and_then(|ns| field(fields, ns, local)) {
Some(t) => simple(t, v),
None => true,
}
});
let elements_ok = holder.children().filter(Node::is_element).all(|f| {
let n = f.tag_name();
match n.namespace().and_then(|ns| field(fields, ns, n.name())) {
Some(t) => fits(t, &f, deeper),
let elements_ok = holder.elements().all(|f| {
let fs = hscope.child(f);
match fs.el_ns(f).and_then(|ns| field(fields, ns, f.local())) {
Some(t) => fits(t, f, &fs, deeper),
None => true,
}
});
@@ -785,18 +801,23 @@ fn fits(ty: Ty, e: &Node<'_, '_>, depth: usize) -> bool {
}
// A structure of any fields, such as a Pantry item: predefined properties in it still have
// their types.
Ty::AnyStruct => structure(e, &children, resource, description).is_some_and(|holder| predefined_ok(&holder, deeper)),
Ty::OneOf(types) => types.iter().any(|t| fits(*t, e, depth)),
Ty::AnyStruct => {
structure(e, scope, &children, resource, description.as_ref()).is_some_and(|(holder, hscope)| predefined_ok(holder, &hscope, deeper))
}
Ty::OneOf(types) => types.iter().any(|t| fits(*t, e, scope, depth)),
}
}
/// Whether the fields of `holder`, as attributes and elements, that are predefined properties have
/// their types.
fn predefined_ok(holder: &Node<'_, '_>, depth: usize) -> bool {
let attrs_ok = field_attributes(holder).all(|a| a.namespace().and_then(|ns| property(ns, a.name())).is_none_or(|(_, t)| simple(t, a.value())));
let elements_ok = holder.children().filter(Node::is_element).all(|f| {
let n = f.tag_name();
n.namespace().and_then(|ns| property(ns, n.name())).is_none_or(|(_, t)| fits(t, &f, depth))
fn predefined_ok<'a>(holder: &'a El, scope: &NsScope<'a>, depth: usize) -> bool {
let attrs_ok = field_attributes(holder, scope).all(|(k, v)| {
let local = k.split_once(':').map_or(k, |(_, l)| l);
scope.attr_ns(k).and_then(|ns| property(ns, local)).is_none_or(|(_, t)| simple(t, v))
});
let elements_ok = holder.elements().all(|f| {
let fs = scope.child(f);
fs.el_ns(f).and_then(|ns| property(ns, f.local())).is_none_or(|(_, t)| fits(t, f, &fs, depth))
});
attrs_ok && elements_ok
}
@@ -806,46 +827,55 @@ fn field(fields: &[Field], ns: &str, name: &str) -> Option<Ty> {
}
/// The items of `e`'s value when it is an `rdf:<kind>` array.
fn array<'a, 'i>(e: &Node<'a, 'i>, children: &[Node<'a, 'i>], kind: &str) -> Option<Vec<Node<'a, 'i>>> {
fn array<'a>(e: &'a El, scope: &NsScope<'a>, children: &[(&'a El, NsScope<'a>)], kind: &str) -> Option<Vec<(&'a El, NsScope<'a>)>> {
match children {
// Items are `rdf:li` (ISO 16684-1 7.9.2.4); any other element, or character data around
// them, breaks the array, and so does `rdf:parseType` or `rdf:resource` on the property,
// which makes its value something else.
[a] if is(a, RDF, kind) && field_attributes(e).next().is_none() && blank(e) && blank(a) && !other_value_form(e) => {
let items: Vec<Node<'a, 'i>> = a.children().filter(Node::is_element).collect();
items.iter().all(|i| is(i, RDF, "li")).then_some(items)
[(a, ascope)]
if is(a, ascope, RDF, kind) && field_attributes(e, scope).next().is_none() && blank(e) && blank(a) && !other_value_form(e, scope) =>
{
let items: Vec<(&'a El, NsScope<'a>)> = a.elements().map(|i| (i, ascope.child(i))).collect();
items.iter().all(|(i, iscope)| is(i, iscope, RDF, "li")).then_some(items)
}
_ => None,
}
}
/// The node holding the fields of `e`'s value when it is a structure.
fn structure<'a, 'i>(e: &Node<'a, 'i>, children: &[Node<'a, 'i>], resource: bool, description: Option<Node<'a, 'i>>) -> Option<Node<'a, 'i>> {
fn structure<'a>(
e: &'a El,
scope: &NsScope<'a>,
children: &[(&'a El, NsScope<'a>)],
resource: bool,
description: Option<&(&'a El, NsScope<'a>)>,
) -> Option<(&'a El, NsScope<'a>)> {
if resource {
// A structure's fields are elements; character data has no place in it.
return text(e).trim().is_empty().then_some(*e);
return text(e).trim().is_empty().then_some((e, scope.clone()));
}
if let Some(d) = description {
return (blank(e) && blank(&d)).then_some(d);
if let Some((d, ds)) = description {
return (blank(e) && blank(d)).then_some((*d, ds.clone()));
}
// Fields written as attributes of the property element, with no content.
let empty = text(e).trim().is_empty();
(children.is_empty() && empty && e.attribute((RDF, "resource")).is_none() && field_attributes(e).next().is_some()).then_some(*e)
(children.is_empty() && empty && scope.attr(e, RDF, "resource").is_none() && field_attributes(e, scope).next().is_some())
.then_some((e, scope.clone()))
}
/// Whether `e` says its value is a resource or a literal of another kind, not the element it holds.
fn other_value_form(e: &Node<'_, '_>) -> bool {
e.attribute((RDF, "parseType")).is_some() || e.attribute((RDF, "resource")).is_some()
fn other_value_form<'a>(e: &El, scope: &NsScope<'a>) -> bool {
scope.attr(e, RDF, "parseType").is_some() || scope.attr(e, RDF, "resource").is_some()
}
/// Whether `e` holds no character data but white space.
fn blank(e: &Node<'_, '_>) -> bool {
fn blank(e: &El) -> bool {
text(e).trim().is_empty()
}
/// All the character data directly in `e`, however comments or other nodes split it.
fn text(e: &Node<'_, '_>) -> String {
e.children().filter(Node::is_text).filter_map(|t| t.text()).collect()
fn text(e: &El) -> String {
e.text().into_owned()
}
/// Whether the simple value `v` has the simple type `ty`.
+2 -2
View File
@@ -26,8 +26,8 @@ regex = { workspace = true }
thiserror = { workspace = true }
# NFKC for the metadata check; already in the build through pdfcraft-crypt (stringprep).
unicode-normalization = "0.1"
# Reads the PDF/A and PDF/UA identification by namespace; already in the build through pdfcraft-xfa.
roxmltree = "0.21"
# Reads the PDF/A and PDF/UA identification by namespace.
craft-xml = { workspace = true }
sha2 = "0.11"
[dev-dependencies]
+11 -7
View File
@@ -183,19 +183,23 @@ fn read(text: &str) -> Option<Found> {
if text.bytes().filter(|&b| b == b'=').count() > MAX_EQUALS {
return None;
}
let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let xml = roxmltree::Document::parse_with_options(text, options).ok()?;
const CANON: craft_xml::Canonical<'_> = craft_xml::Canonical::new(&[("pdfaid", PDFA_ID), ("pdfuaid", PDFUA_ID)]);
let mut opts =
craft_xml::Options::canonical(&CANON).strict().with_limits(craft_xml::Limits { max_nodes: MAX_NODES as usize, ..Default::default() });
opts.doctype = craft_xml::Doctype::Reject;
let xml = opts.parse(text.as_bytes()).ok()?;
let mut found = Found::default();
for node in xml.descendants().filter(roxmltree::Node::is_element) {
for (ns, id) in [(PDFA_ID, &mut found.pdfa), (PDFUA_ID, &mut found.pdfua)] {
for node in std::iter::once(&xml).chain(xml.descendants()) {
for (prefix, id) in [("pdfaid", &mut found.pdfa), ("pdfuaid", &mut found.pdfua)] {
for (local, slot) in [("part", &mut id.part), ("conformance", &mut id.conformance), ("amd", &mut id.amd), ("rev", &mut id.rev)] {
if slot.is_some() {
continue;
}
if let Some(v) = node.attribute((ns, local)) {
let qname = format!("{prefix}:{local}");
if let Some(v) = node.attr(&qname) {
*slot = Some(v.trim().to_string());
} else if node.tag_name().namespace() == Some(ns) && node.tag_name().name() == local {
*slot = Some(node.text().unwrap_or("").trim().to_string());
} else if node.name == qname {
*slot = Some(node.text().trim().to_string());
}
}
}
+15 -8
View File
@@ -43,15 +43,22 @@ fn the_identification_of_a_removed_packet_is_written_back() {
assert_eq!(r.metadata, 1, "{r:?}");
let text = text.expect("a packet is written back");
assert!(!text.to_lowercase().contains("bluefin") && !text.contains("Lovelace"), "{text}");
let xml = roxmltree::Document::parse(&text).expect("well-formed");
let value = |ns: &str, local: &str| {
xml.descendants().find(|n| n.tag_name().namespace() == Some(ns) && n.tag_name().name() == local).and_then(|n| n.text()).map(str::to_string)
const CANON: craft_xml::Canonical<'_> = craft_xml::Canonical::new(&[
("pdfaid", "http://www.aiim.org/pdfa/ns/id/"),
("pdfuaid", "http://www.aiim.org/pdfua/ns/id/"),
("pdfaSchema", "http://www.aiim.org/pdfa/ns/schema#"),
]);
let xml = craft_xml::Options::canonical(&CANON).strict().parse(text.as_bytes()).expect("well-formed");
let value = |prefix: &str, local: &str| {
let qname = format!("{prefix}:{local}");
std::iter::once(&xml).chain(xml.descendants()).find(|n| n.name == qname).map(|n| n.text().into_owned())
};
let a = "http://www.aiim.org/pdfa/ns/id/";
let ua = "http://www.aiim.org/pdfua/ns/id/";
assert_eq!((value(a, "part").as_deref(), value(a, "conformance").as_deref(), value(ua, "part").as_deref()), (Some("2"), Some("B"), Some("1")));
assert_eq!(
(value("pdfaid", "part").as_deref(), value("pdfaid", "conformance").as_deref(), value("pdfuaid", "part").as_deref()),
(Some("2"), Some("B"), Some("1"))
);
// PDF/A-2 accepts pdfuaid only through an extension schema describing it.
assert_eq!(value("http://www.aiim.org/pdfa/ns/schema#", "prefix").as_deref(), Some("pdfuaid"), "{text}");
assert_eq!(value("pdfaSchema", "prefix").as_deref(), Some("pdfuaid"), "{text}");
}
/// A packet without identification is removed as before, and nothing is said about it.
@@ -74,7 +81,7 @@ fn an_identification_that_cannot_be_kept_is_reported_lost() {
let latin1 = [
DOC_XMP_HEAD.as_bytes(),
br#"<rdf:Description rdf:about="" xmlns:pdfuaid="http://www.aiim.org/pdfua/ns/id/" pdfuaid:part="1" dc:title="Bluefin caf\xE9"/>"#,
b"<rdf:Description rdf:about=\"\" xmlns:pdfuaid=\"http://www.aiim.org/pdfua/ns/id/\" pdfuaid:part=\"1\" dc:title=\"Bluefin caf\xE9\"/>",
DOC_XMP_TAIL.as_bytes(),
]
.concat();
+2 -2
View File
@@ -45,7 +45,7 @@ web-time = { workspace = true }
bytemuck = { version = "1", features = ["extern_crate_alloc"] }
rfd = { workspace = true }
png = "0.18"
craft-image-codecs = { workspace = true }
base64 = "0.23"
# Unicode bidi paragraph segmentation for right-to-left comment wrapping; egui 0.37's epaint owns
# display reordering. Already in the tree through egui’s epaint.
@@ -101,7 +101,7 @@ egui_kittest = { workspace = true, features = ["eframe", "wgpu"] }
# Tests draw PdfCraft text without a system provider: the bundled and monochrome emoji faces must
# be enabled on the test graph itself (a dependency's dev-features do not propagate).
egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] }
image = { version = "0.25", default-features = false, features = ["png"] }
image = { version = "0.25", default-features = false }
# Glyph-coverage test of the interface fonts; the same skrifa pdfcraft-fonts already uses.
skrifa = "0.44"
proptest = { workspace = true }
+14 -7
View File
@@ -33,12 +33,15 @@ fn load_ocr_verify(app: &mut PdfCraftApp) -> Result<(), String> {
let png = export::Exporter::new(s.get(id).ok_or("the document is gone")?).png(0, 150.0).map_err(|e| e.to_string())?;
// Keep the text block of the rendered page, so the scan is a short note-sized picture:
// the whole page and the confidence legend fit in the shot's source-page pane.
let mut page = image::load_from_memory(&png).map_err(|e| e.to_string())?;
let mut scan_png = std::io::Cursor::new(Vec::new());
page.crop(0, 0, page.width(), (page.height() as f32 * 0.34) as u32)
.write_to(&mut scan_png, image::ImageFormat::Png)
let page = craft_image_codecs::decode(&png).map_err(|e| e.to_string())?;
let crop_h = ((page.height() as f32 * 0.34) as u32).max(1);
let bpp = page.layout().channels() as usize;
let row_bytes = page.width() as usize * bpp;
let cropped_data = page.as_u8().ok_or("expected 8-bit PNG")?[..crop_h as usize * row_bytes].to_vec();
let cropped = craft_image_codecs::Image::from_u8(page.width(), crop_h, page.layout(), cropped_data).map_err(|e| e.to_string())?;
let scan_png = craft_image_codecs::encode(&cropped, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
.map_err(|e| e.to_string())?;
let scan = s.create_from_images(&[("scan.png".into(), scan_png.into_inner())]).map_err(|e| e.to_string())?.to_vec();
let scan = s.create_from_images(&[("scan.png".into(), scan_png)]).map_err(|e| e.to_string())?.to_vec();
let settings =
OcrSettings { engine: pdfcraft_engine::ocr::EngineChoice::Auto, strategy: MergeStrategy::ConfidenceWeighted, ..OcrSettings::default() };
app.ocr_draft.engine = settings.engine;
@@ -118,7 +121,11 @@ fn main() -> Result<(), String> {
let h = (image.height() as f64 * w as f64 / image.width() as f64).round() as u32;
image = image::imageops::resize(&image, w, h, image::imageops::FilterType::Lanczos3);
}
image.save(&out).map_err(|e| e.to_string())?;
eprintln!("shot: wrote {out} ({}×{})", image.width(), image.height());
let (w, h) = (image.width(), image.height());
let out_img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, image.into_raw()).map_err(|e| e.to_string())?;
let encoded = craft_image_codecs::encode(&out_img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
.map_err(|e| e.to_string())?;
std::fs::write(&out, encoded).map_err(|e| e.to_string())?;
eprintln!("shot: wrote {out} ({w}×{h})");
Ok(())
}
+3 -3
View File
@@ -2049,9 +2049,9 @@ fn many_open_documents_share_one_raster_allowance_and_close_releases_handles() {
#[test]
fn suspended_signature_preview_releases_layers_and_rebuilds_without_editing() {
let ctx = egui::Context::default();
let mut png = std::io::Cursor::new(Vec::new());
image::RgbaImage::from_pixel(12, 4, image::Rgba([20, 30, 40, 255])).write_to(&mut png, image::ImageFormat::Png).unwrap();
let image = pdfcraft_engine::SignatureImage::from_bytes(png.get_ref()).unwrap();
let img = craft_image_codecs::Image::from_u8(12, 4, craft_image_codecs::ChannelLayout::Rgba, [20, 30, 40, 255].repeat(48)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let image = pdfcraft_engine::SignatureImage::from_bytes(&png).unwrap();
let mut session = pdfcraft_engine::Session::new();
let blank = session.create_blank(300.0, 400.0, 1).unwrap();
let id = session.open_new("signature-preview.pdf", blank).unwrap();
+3 -7
View File
@@ -183,13 +183,9 @@ pub(super) fn screenshot_png(image: &egui::ColorImage, region: Option<egui::Rect
}
}
let (cw, ch) = ((x1 - x0) as u32, (y1 - y0) as u32);
let mut out = Vec::new();
let mut enc = png::Encoder::new(&mut out, cw, ch);
enc.set_color(png::ColorType::Rgba);
enc.set_depth(png::BitDepth::Eight);
let mut writer = enc.write_header().map_err(|e| e.to_string())?;
writer.write_image_data(&rgba).map_err(|e| e.to_string())?;
writer.finish().map_err(|e| e.to_string())?;
let img = craft_image_codecs::Image::from_u8(cw, ch, craft_image_codecs::ChannelLayout::Rgba, rgba).map_err(|e| e.to_string())?;
let out = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
.map_err(|e| e.to_string())?;
if out.len() > MAX_ENCODED_BYTES {
return Err(format!("the encoded screenshot is {} bytes, over the {MAX_ENCODED_BYTES}-byte encode limit", out.len()));
}
@@ -80,9 +80,8 @@ fn duplicate_warnings_reuse_results_and_track_removal_and_parts() {
#[test]
fn the_same_image_or_text_file_added_twice_is_a_duplicate() {
static NOW: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(1_700_000_000);
let mut png = std::io::Cursor::new(Vec::new());
image::RgbaImage::from_pixel(12, 4, image::Rgba([20, 30, 40, 255])).write_to(&mut png, image::ImageFormat::Png).unwrap();
let png = png.into_inner();
let img = craft_image_codecs::Image::from_u8(12, 4, craft_image_codecs::ChannelLayout::Rgba, [20, 30, 40, 255].repeat(48)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let warnings = |first: (&str, &[u8]), second: (&str, &[u8])| {
let mut app = PdfCraftApp::new();
app.session = std::mem::take(&mut app.session).with_clock(|| NOW.fetch_add(1, std::sync::atomic::Ordering::Relaxed));
+2 -8
View File
@@ -87,14 +87,8 @@ pub(crate) fn png_from_rgba(width: usize, height: usize, rgba: &[u8]) -> Result<
if w == 0 || h == 0 || width > MAX_IMAGE_SIDE || height > MAX_IMAGE_SIDE || expected != Some(rgba.len()) || rgba.len() > MAX_IMAGE_BYTES {
return Err("the clipboard image is empty, malformed or exceeds size limits".into());
}
let mut out = Vec::new();
let mut enc = png::Encoder::new(&mut out, w, h);
enc.set_color(png::ColorType::Rgba);
enc.set_depth(png::BitDepth::Eight);
let mut writer = enc.write_header().map_err(|e| e.to_string())?;
writer.write_image_data(rgba).map_err(|e| e.to_string())?;
writer.finish().map_err(|e| e.to_string())?;
Ok(out)
let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, rgba.to_vec()).map_err(|e| e.to_string())?;
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).map_err(|e| e.to_string())
}
fn stem(name: &str) -> &str {
+9 -17
View File
@@ -1101,11 +1101,9 @@ mod tests {
/// A one-page PDF that is one picture covering the page.
fn scan() -> Vec<u8> {
let mut png = std::io::Cursor::new(Vec::new());
image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(8, 8, image::Rgba([250, 250, 250, 255])))
.write_to(&mut png, image::ImageFormat::Png)
.unwrap();
Session::new().create_from_images(&[("scan.png".into(), png.into_inner())]).unwrap().to_vec()
let img = craft_image_codecs::Image::from_u8(8, 8, craft_image_codecs::ChannelLayout::Rgba, [250, 250, 250, 255].repeat(64)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
Session::new().create_from_images(&[("scan.png".into(), png)]).unwrap().to_vec()
}
fn app_with(pdf: Vec<u8>) -> PdfCraftApp {
@@ -1167,11 +1165,9 @@ mod tests {
assert_eq!(found(&mut app), None);
assert!(app.views[0].auto_ocr_skipped.contains_key(&0), "remembered with the document's generation");
let id = app.active_ids().unwrap().1;
let mut png = std::io::Cursor::new(Vec::new());
image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(8, 8, image::Rgba([250, 250, 250, 255])))
.write_to(&mut png, image::ImageFormat::Png)
.unwrap();
let add = Edit::AddImage { page: 0, rect: Some([0.0, 0.0, 595.0, 842.0]), name: "scan.png".into(), bytes: Arc::new(png.into_inner()) };
let img = craft_image_codecs::Image::from_u8(8, 8, craft_image_codecs::ChannelLayout::Rgba, [250, 250, 250, 255].repeat(64)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let add = Edit::AddImage { page: 0, rect: Some([0.0, 0.0, 595.0, 842.0]), name: "scan.png".into(), bytes: Arc::new(png) };
app.session.apply(id, add).unwrap();
assert_eq!(found(&mut app), Some(0), "a picture over the page makes it a scan");
}
@@ -1262,13 +1258,9 @@ mod tests {
app.ocr_run = Some(OcrRun { doc: id, progress, guard, automatic: Some(AutoOcr { page: 0, generation }), editable: false });
app.views[0].auto_ocr_tried.insert(0);
// The document changes while the run is out.
let mut png = std::io::Cursor::new(Vec::new());
image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(8, 8, image::Rgba([250, 250, 250, 255])))
.write_to(&mut png, image::ImageFormat::Png)
.unwrap();
app.session
.apply(id, Edit::AddImage { page: 0, rect: Some([10.0, 10.0, 50.0, 50.0]), name: "p.png".into(), bytes: Arc::new(png.into_inner()) })
.unwrap();
let img = craft_image_codecs::Image::from_u8(8, 8, craft_image_codecs::ChannelLayout::Rgba, [250, 250, 250, 255].repeat(64)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
app.session.apply(id, Edit::AddImage { page: 0, rect: Some([10.0, 10.0, 50.0, 50.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap();
app.poll_ocr();
assert!(app.ocr_run.is_none(), "the run is over");
assert_eq!(app.session.get(id).unwrap().can_undo(), Some("Add image"), "its result was not applied");
+8 -28
View File
@@ -242,13 +242,8 @@ fn typing_moving_styling_and_deleting_added_content() {
h.run_steps(3);
assert!(added(&h).is_empty());
// An image lands in the middle of the page, selected.
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 30, 20);
enc.set_color(png::ColorType::Rgb);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[90u8; 1800]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(30, 20, craft_image_codecs::ChannelLayout::Rgb, vec![90u8; 1800]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
h.state_mut().add_image("logo.png".into(), png);
h.run_steps(3);
let a = added(&h);
@@ -993,13 +988,8 @@ fn a_resized_added_picture_shows_only_at_its_new_size() {
let mut h = harness();
assert!(h.state_mut().execute("edit.edit_text"));
h.run_steps(2);
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 60, 60);
enc.set_color(png::ColorType::Rgb);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[200u8, 30, 30].repeat(3600)).unwrap();
}
let img = craft_image_codecs::Image::from_u8(60, 60, craft_image_codecs::ChannelLayout::Rgb, [200u8, 30, 30].repeat(3600)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
h.state_mut().add_image("red.png".into(), png);
h.run_steps(3);
assert_eq!(h.state().views[0].content.selected, Some((0, 0)), "the picture is selected");
@@ -1049,13 +1039,8 @@ fn added_picture_resizes_repeatedly_and_drags_back_from_out_of_bounds_without_sm
let mut h = harness();
assert!(h.state_mut().execute("edit.edit_text"));
h.run_steps(2);
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 60, 60);
enc.set_color(png::ColorType::Rgb);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[200u8, 30, 30].repeat(3600)).unwrap();
}
let img = craft_image_codecs::Image::from_u8(60, 60, craft_image_codecs::ChannelLayout::Rgb, [200u8, 30, 30].repeat(3600)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
h.state_mut().add_image("red.png".into(), png);
h.run_steps(3);
crate::support::settle(&mut h);
@@ -1162,13 +1147,8 @@ fn a_handle_in_the_viewport_below_a_short_page_starts_another_resize() {
h.state_mut().set_option("zoom", "50").unwrap();
assert!(h.state_mut().execute("edit.edit_text"));
h.run_steps(2);
let mut png = Vec::new();
{
let mut enc = png::Encoder::new(&mut png, 60, 60);
enc.set_color(png::ColorType::Rgb);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[200u8, 30, 30].repeat(3600)).unwrap();
}
let img = craft_image_codecs::Image::from_u8(60, 60, craft_image_codecs::ChannelLayout::Rgb, [200u8, 30, 30].repeat(3600)).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
h.state_mut().add_image("red.png".into(), png);
h.run_steps(3);
let img = match &added(&h)[0].content {
+4 -16
View File
@@ -3,15 +3,8 @@
use pdfcraft_ui_egui::PdfCraftApp;
fn png() -> Vec<u8> {
let mut out = Vec::new();
{
let mut enc = png::Encoder::new(&mut out, 8, 4);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[120u8; 8 * 4 * 3]).unwrap();
}
out
let img = craft_image_codecs::Image::from_u8(8, 4, craft_image_codecs::ChannelLayout::Rgb, vec![120u8; 8 * 4 * 3]).unwrap();
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()
}
#[test]
@@ -123,13 +116,8 @@ fn reduce_file_size_writes_a_compact_copy() {
// A photo-like picture, stored losslessly: Reduce recompresses it as JPEG.
let (w, h) = (600u32, 400u32);
let px: Vec<u8> = (0..w * h).flat_map(|i| [(i % w * 255 / w) as u8 ^ (i & 7) as u8, (i / w * 255 / h) as u8, 128]).collect();
let mut photo = Vec::new();
{
let mut enc = png::Encoder::new(&mut photo, w, h);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
enc.write_header().unwrap().write_image_data(&px).unwrap();
}
let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, px).unwrap();
let photo = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let dir = ReduceDir::new();
let out = dir.0.join("reduced.pdf");
let h = reduce("photo.png", photo, &out);
+2 -7
View File
@@ -642,13 +642,8 @@ fn clicking_duplicate_image_buttons_imports_only_into_the_clicked_field() {
let dir = std::env::temp_dir().join(format!("pdfcraft-exact-image-click-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("synthetic.png");
let mut png = vec![];
{
let mut encoder = png::Encoder::new(&mut png, 2, 2);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
encoder.write_header().unwrap().write_image_data(&[120u8; 12]).unwrap();
}
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![120u8; 12]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
std::fs::write(&path, png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
let mut app = PdfCraftApp::new();
+2 -9
View File
@@ -55,15 +55,8 @@ fn pending_pages_use_document_reading_background() {
/// A small PNG for the Create ▸ Images path, which opens the document it builds.
fn png() -> Vec<u8> {
let mut out = Vec::new();
{
let mut enc = png::Encoder::new(&mut out, 8, 4);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[120u8; 8 * 4 * 3]).unwrap();
}
out
let img = craft_image_codecs::Image::from_u8(8, 4, craft_image_codecs::ChannelLayout::Rgb, vec![120u8; 8 * 4 * 3]).unwrap();
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()
}
#[test]
+2 -3
View File
@@ -13,9 +13,8 @@ workspace = true
[dependencies]
pdfcraft-cos = { workspace = true }
pdfcraft-fonts = { workspace = true }
roxmltree = "0.21"
craft-xml = { workspace = true }
base64 = "0.23"
png = "0.18"
craft-image-codecs = { workspace = true }
sha2 = "0.11"
image = { version = "0.25", default-features = false, features = ["bmp", "gif", "tiff"] }
thiserror = { workspace = true }
+10 -10
View File
@@ -46,7 +46,7 @@ impl ImportBudget {
/// `n` as an [`ImportNode`], counting nodes and copied bytes against `budget`; `TooLarge` past
/// the depth or either budget.
fn import_node(n: roxmltree::Node, depth: usize, budget: &mut ImportBudget) -> Result<ImportNode, XfaError> {
fn import_node(n: crate::xml::Node, depth: usize, budget: &mut ImportBudget) -> Result<ImportNode, XfaError> {
if depth > MAX_IMPORT_DEPTH {
return Err(XfaError::TooLarge(format!("the data file nests more than {MAX_IMPORT_DEPTH} levels deep")));
}
@@ -192,12 +192,12 @@ pub fn parse_data_file(text: &str, expect_root: Option<&str>) -> Result<Vec<Impo
if crate::xml_nesting_depth(text) > MAX_IMPORT_DEPTH {
return Err(XfaError::TooLarge(format!("the data file nests more than {MAX_IMPORT_DEPTH} levels deep")));
}
let opts = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let parsed = roxmltree::Document::parse_with_options(text, opts).map_err(|e| XfaError::Malformed(format!("the data file is not XML: {e}")))?;
let opts = crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let parsed = crate::xml::Document::parse_with_options(text, opts).map_err(|e| XfaError::Malformed(format!("the data file is not XML: {e}")))?;
let root = parsed.root_element();
let local = root.tag_name().name();
let in_xfa = |n: &roxmltree::Node| n.tag_name().namespace().is_none_or(|ns| ns == XFA_DATA_NS);
let is = |n: &roxmltree::Node, name: &str| n.is_element() && n.tag_name().name() == name && in_xfa(n);
let in_xfa = |n: &crate::xml::Node| n.tag_name().namespace().is_none_or(|ns| ns == XFA_DATA_NS);
let is = |n: &crate::xml::Node, name: &str| n.is_element() && n.tag_name().name() == name && in_xfa(n);
let data = match local {
"xdp" => {
let ds = root.children().find(|n| is(n, "datasets")).ok_or_else(|| XfaError::Malformed("the XDP holds no datasets".into()))?;
@@ -304,20 +304,20 @@ pub fn export_data(doc: &Document, xdp: bool, pdf_name: &str) -> Result<String,
/// data element), each with the namespaces it uses declared on it. `Ok(None)`: no datasets
/// element; `Err`: the text can't be read.
fn datasets_markup(text: &str) -> Result<Option<(String, String)>, ()> {
let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
if crate::xml_nesting_depth(text) > crate::MAX_XML_DEPTH {
return Err(());
}
let wrapped = format!("{WRAP_OPEN}{text}{WRAP_CLOSE}");
let (src, parsed) = match roxmltree::Document::parse_with_options(text, opts()) {
let (src, parsed) = match crate::xml::Document::parse_with_options(text, opts()) {
Ok(d) => (text, d),
Err(_) => (wrapped.as_str(), roxmltree::Document::parse_with_options(&wrapped, opts()).map_err(|_| ())?),
Err(_) => (wrapped.as_str(), crate::xml::Document::parse_with_options(&wrapped, opts()).map_err(|_| ())?),
};
let Some(ds) = parsed.descendants().find(|n| n.is_element() && n.tag_name().name() == "datasets") else { return Ok(None) };
let data = ds.children().find(|n| n.is_element() && n.tag_name().name() == "data");
let data_alone = match data {
Some(d) => {
let kids: Vec<roxmltree::Node> = d.children().filter(|c| c.is_element()).collect();
let kids: Vec<crate::xml::Node> = d.children().filter(|c| c.is_element()).collect();
match kids.as_slice() {
[one] => element_markup(src, *one).ok_or(())?,
_ => element_markup(src, d).ok_or(())?,
@@ -330,7 +330,7 @@ fn datasets_markup(text: &str) -> Result<Option<(String, String)>, ()> {
/// `n` as written in `src`, with the namespace declarations in scope that its start tag lacks
/// added to it, so it stands alone.
fn element_markup(src: &str, n: roxmltree::Node) -> Option<String> {
fn element_markup(src: &str, n: crate::xml::Node) -> Option<String> {
let range = n.range();
let text = src.get(range.clone())?;
let tag_end = start_tag_end(src, &range)?;
+17 -17
View File
@@ -49,7 +49,7 @@ struct Edit {
}
/// The edit that puts `inner` (markup) at the end of `node`'s content.
fn append_edit(src: &str, node: roxmltree::Node, inner: &str) -> Option<Edit> {
fn append_edit(src: &str, node: crate::xml::Node, inner: &str) -> Option<Edit> {
let range = node.range();
let tag_end = start_tag_end(src, &range)?;
if tag_end.checked_add(1)? == range.end && src.get(..tag_end)?.ends_with('/') {
@@ -62,7 +62,7 @@ fn append_edit(src: &str, node: roxmltree::Node, inner: &str) -> Option<Edit> {
}
/// The edit that makes `text` (escaped) the whole content of the leaf `node`.
fn replace_edit(src: &str, node: roxmltree::Node, text: &str) -> Option<Edit> {
fn replace_edit(src: &str, node: crate::xml::Node, text: &str) -> Option<Edit> {
let range = node.range();
let tag_end = start_tag_end(src, &range)?;
if tag_end.checked_add(1)? == range.end && src.get(..tag_end)?.ends_with('/') {
@@ -99,7 +99,7 @@ pub(super) const WRAP_CLOSE: &str = "</pdfcraft-wrap>";
/// written.
/// The prefix bound to the XFA data namespace where `n` is (`None`: unbound or the default
/// namespace, which attributes can't use).
fn xfa_prefix(n: roxmltree::Node) -> Option<String> {
fn xfa_prefix(n: crate::xml::Node) -> Option<String> {
n.namespaces().find(|ns| ns.uri() == XFA_DATA_NS).and_then(|ns| ns.name()).filter(|p| !p.is_empty()).map(str::to_string)
}
@@ -117,15 +117,15 @@ fn merge(text: &str, within: Within, op: Op, warnings: &mut Vec<String>) -> Merg
warnings.push("The XFA data nests XML elements deeper than the reader accepts; the field values were not written to it".into());
return Merged::Failed;
}
let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
// A packet of an array may use the prefixes the XDP envelope declares: read it inside
// an element declaring the usual ones.
let wrapped;
let (src, shift, parsed) = match roxmltree::Document::parse_with_options(text, opts()) {
let (src, shift, parsed) = match crate::xml::Document::parse_with_options(text, opts()) {
Ok(d) => (text, 0, d),
Err(first) => {
wrapped = format!("{WRAP_OPEN}{text}{WRAP_CLOSE}");
match (within, roxmltree::Document::parse_with_options(&wrapped, opts())) {
match (within, crate::xml::Document::parse_with_options(&wrapped, opts())) {
(Within::Packet, Ok(d)) => (wrapped.as_str(), WRAP_OPEN.len(), d),
_ => {
warnings.push(format!("The XFA data could not be read ({first}); the field values were not written to it"));
@@ -134,7 +134,7 @@ fn merge(text: &str, within: Within, op: Op, warnings: &mut Vec<String>) -> Merg
}
}
};
let is = |n: &roxmltree::Node, name: &str| n.is_element() && n.tag_name().name() == name;
let is = |n: &crate::xml::Node, name: &str| n.is_element() && n.tag_name().name() == name;
let datasets = parsed
.descendants()
.find(|n| is(n, "datasets") && n.tag_name().namespace() == Some(XFA_DATA_NS))
@@ -274,7 +274,7 @@ fn merge(text: &str, within: Within, op: Op, warnings: &mut Vec<String>) -> Merg
/// Whether `node` is a multi-select choice list's data group: element children only, every one
/// a `value` holding text alone; or, with no children, an element marked `xfa:dataNode=
/// "dataGroup"`, as a group with no picks is written (XFA 3.3 p. 150).
fn is_value_group(node: roxmltree::Node) -> bool {
fn is_value_group(node: crate::xml::Node) -> bool {
let blank = node.children().filter(|c| c.is_text()).all(|c| c.text().is_some_and(|t| t.trim().is_empty()));
let mut elements = node.children().filter(|c| c.is_element()).peekable();
if elements.peek().is_none() {
@@ -284,14 +284,14 @@ fn is_value_group(node: roxmltree::Node) -> bool {
}
/// Whether `node` carries `xfa:dataNode="dataGroup"`.
fn marked_data_group(node: roxmltree::Node) -> bool {
fn marked_data_group(node: crate::xml::Node) -> bool {
node.attribute((XFA_DATA_NS, "dataNode")) == Some("dataGroup")
}
/// The edit that marks the data group `node` as one, so that emptied of its picks it still loads
/// as a group and not as a data value (XFA 3.3 p. 150): `xfa:dataNode="dataGroup"` added to its
/// start tag, with the namespace declared there when the packet has no prefix for it.
fn mark_data_group_edit(src: &str, node: roxmltree::Node) -> Option<Edit> {
fn mark_data_group_edit(src: &str, node: crate::xml::Node) -> Option<Edit> {
let range = node.range();
let tag_end = start_tag_end(src, &range)?;
// Before `/>` of an empty tag, else before `>`.
@@ -310,12 +310,12 @@ fn mark_data_group_edit(src: &str, node: roxmltree::Node) -> Option<Edit> {
}
/// The edits that put `wants` into the `data` element.
fn merge_values(src: &str, data: roxmltree::Node, wants: &[Want], edits: &mut Vec<Edit>, warnings: &mut Vec<String>) {
fn merge_values(src: &str, data: crate::xml::Node, wants: &[Want], edits: &mut Vec<Edit>, warnings: &mut Vec<String>) {
// Element children by local name, per element visited (built once each).
let mut index: HashMap<roxmltree::NodeId, HashMap<String, Vec<roxmltree::NodeId>>> = HashMap::new();
let mut index: HashMap<crate::xml::NodeId, HashMap<String, Vec<crate::xml::NodeId>>> = HashMap::new();
let tree = data.document();
// Nodes to add, per existing element they go into.
let mut added: Vec<(roxmltree::NodeId, Build)> = Vec::new();
let mut added: Vec<(crate::xml::NodeId, Build)> = Vec::new();
let mut budget = NewNodes::new();
for w in wants {
let (escaped, dropped) = escape(&w.text);
@@ -324,7 +324,7 @@ fn merge_values(src: &str, data: roxmltree::Node, wants: &[Want], edits: &mut Ve
let mut have = 0;
for (name, idx) in &w.path {
let kids = index.entry(node.id()).or_insert_with(|| {
let mut by: HashMap<String, Vec<roxmltree::NodeId>> = HashMap::new();
let mut by: HashMap<String, Vec<crate::xml::NodeId>> = HashMap::new();
for c in node.children().filter(|c| c.is_element()) {
by.entry(c.tag_name().name().to_string()).or_default().push(c.id());
}
@@ -465,9 +465,9 @@ pub(super) fn readable(text: &str, within: Within, warnings: &mut Vec<String>) -
warnings.push(format!("The XFA data would be larger than {} MB; it was not written", MAX_PACKET_WRITE >> 20));
return false;
}
let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let ok = roxmltree::Document::parse_with_options(text, opts()).is_ok()
|| (within == Within::Packet && roxmltree::Document::parse_with_options(&format!("{WRAP_OPEN}{text}{WRAP_CLOSE}"), opts()).is_ok());
let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let ok = crate::xml::Document::parse_with_options(text, opts()).is_ok()
|| (within == Within::Packet && crate::xml::Document::parse_with_options(&format!("{WRAP_OPEN}{text}{WRAP_CLOSE}"), opts()).is_ok());
if !ok {
warnings.push("The XFA data would not be well-formed XML after this change; it was not written".into());
}
+7 -7
View File
@@ -1,6 +1,6 @@
use super::{DataNode, DataPath, Field, MAX_DEPTH, MAX_NODES, Node, SomIndices, Template, XFA_DATA_NS};
fn to_node(n: roxmltree::Node, depth: usize) -> DataNode {
fn to_node(n: crate::xml::Node, depth: usize) -> DataNode {
let name = n.tag_name().name().to_string();
let children: Vec<DataNode> =
if depth < MAX_DEPTH { n.children().filter(|c| c.is_element()).map(|c| to_node(c, depth + 1)).collect() } else { Vec::new() };
@@ -17,8 +17,8 @@ pub fn parse_datasets(xdp: &str) -> Option<DataNode> {
if crate::xml_nesting_depth(xdp) > crate::MAX_XML_DEPTH {
return None;
}
let opts = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let doc = roxmltree::Document::parse_with_options(xdp, opts).ok()?;
let opts = crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let doc = crate::xml::Document::parse_with_options(xdp, opts).ok()?;
let datasets = doc.descendants().find(|n| n.is_element() && n.tag_name().name() == "datasets")?;
let data = datasets.children().find(|n| n.is_element() && n.tag_name().name() == "data")?;
Some(to_node(data, 0))
@@ -33,16 +33,16 @@ pub(super) fn parse_datasets_packet(text: &str) -> Option<DataNode> {
if crate::xml_nesting_depth(text) > crate::MAX_XML_DEPTH {
return None;
}
let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let wrapped;
let doc = match roxmltree::Document::parse_with_options(text, opts()) {
let doc = match crate::xml::Document::parse_with_options(text, opts()) {
Ok(d) => d,
Err(_) => {
wrapped = format!("{}{text}{}", super::merge::WRAP_OPEN, super::merge::WRAP_CLOSE);
roxmltree::Document::parse_with_options(&wrapped, opts()).ok()?
crate::xml::Document::parse_with_options(&wrapped, opts()).ok()?
}
};
let is = |n: &roxmltree::Node, name: &str| n.is_element() && n.tag_name().name() == name;
let is = |n: &crate::xml::Node, name: &str| n.is_element() && n.tag_name().name() == name;
let datasets = doc
.descendants()
.find(|n| is(n, "datasets") && n.tag_name().namespace() == Some(super::XFA_DATA_NS))
+38 -54
View File
@@ -126,43 +126,20 @@ fn header_size(data: &[u8]) -> Option<(u32, u32)> {
(w > 0 && h > 0).then_some((w, h))
}
// The decoders read only the headers (TIFF: the first IFD) until asked for pixels.
Kind::Bmp => reader(&bmp_with_pixel_offset(data), image::ImageFormat::Bmp).into_dimensions().ok().filter(|&(w, h)| w > 0 && h > 0),
Kind::Bmp => craft_image_codecs::probe_size(&bmp_with_pixel_offset(data)).ok().filter(|&(w, h)| w > 0 && h > 0),
Kind::Tiff => {
use image::{ImageDecoder, metadata::Orientation};
let mut decoder = reader(data, image::ImageFormat::Tiff).into_decoder().ok()?;
let (w, h) = decoder.dimensions();
let info = craft_image_codecs::tiff_info(data).ok()?;
let page = info.default_page().and_then(|i| info.pages.get(i))?;
// Shown turned a quarter: the size viewers show is the stored one swapped.
let turned = matches!(
decoder.orientation().ok()?,
Orientation::Rotate90 | Orientation::Rotate270 | Orientation::Rotate90FlipH | Orientation::Rotate270FlipH
);
let (w, h) = if turned { (h, w) } else { (w, h) };
let turned = (5..=8).contains(&craft_image_codecs::tiff_orientation(data));
let (w, h) = if turned { (page.height, page.width) } else { (page.width, page.height) };
(w > 0 && h > 0).then_some((w, h))
}
}
}
/// A TIFF's first page as viewers show it: turned and flipped as its Orientation tag says.
fn decode_tiff(data: &[u8]) -> image::ImageResult<image::DynamicImage> {
use image::ImageDecoder;
let mut decoder = reader(data, image::ImageFormat::Tiff).into_decoder()?;
let orientation = decoder.orientation()?;
let mut img = image::DynamicImage::from_decoder(decoder)?;
img.apply_orientation(orientation);
Ok(img)
}
/// A decoder told the limits, so a frame larger than its header claims is refused before it is
/// allocated.
fn reader(data: &[u8], format: image::ImageFormat) -> image::ImageReader<std::io::Cursor<&[u8]>> {
let mut reader = image::ImageReader::new(std::io::Cursor::new(data));
reader.set_format(format);
let mut limits = image::Limits::default();
limits.max_image_width = Some(MAX_SIDE);
limits.max_image_height = Some(MAX_SIDE);
limits.max_alloc = Some(MAX_PIXELS * 4);
reader.limits(limits);
reader
fn decode_limits() -> craft_image_codecs::Limits {
craft_image_codecs::Limits { max_width: MAX_SIDE, max_height: MAX_SIDE, max_pixels: MAX_PIXELS, max_alloc: MAX_PIXELS * 4 }
}
/// Samples ready for an image XObject.
@@ -192,23 +169,28 @@ pub fn decode(data: &[u8]) -> Result<Decoded, String> {
}
match kind(data) {
Some(Kind::Png) => {
let mut dec = png::Decoder::new(std::io::Cursor::new(data));
dec.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16);
let mut reader = dec.read_info().map_err(|e| bad(e.to_string()))?;
// The header's size is what the buffer is sized by: checked above.
let (fw, fh) = (reader.info().width, reader.info().height);
let opts = craft_image_codecs::DecodeOptions { keep_orientation: true, limits: decode_limits(), ..Default::default() };
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, data, &opts).map_err(|e| match e {
craft_image_codecs::CodecError::LimitExceeded(_) => too_big(),
other => bad(other.to_string()),
})?;
let (fw, fh) = (img.width(), img.height());
if !fits(fw, fh) {
return Err(too_big());
}
let mut buf = vec![0; reader.output_buffer_size().ok_or_else(|| bad("image too large".into()))?];
let frame = reader.next_frame(&mut buf).map_err(|e| bad(e.to_string()))?;
let samples = buf.get(..frame.buffer_size()).ok_or_else(|| bad("short image data".into()))?;
let (channels, color_space) = match frame.color_type {
png::ColorType::Grayscale => (1, "DeviceGray"),
png::ColorType::GrayscaleAlpha => (2, "DeviceGray"),
png::ColorType::Rgb => (3, "DeviceRGB"),
png::ColorType::Rgba => (4, "DeviceRGB"),
png::ColorType::Indexed => return Err(bad("unexpected palette output".into())),
let (channels, color_space) = match img.layout() {
craft_image_codecs::ChannelLayout::Gray => (1, "DeviceGray"),
craft_image_codecs::ChannelLayout::GrayA => (2, "DeviceGray"),
craft_image_codecs::ChannelLayout::Rgb => (3, "DeviceRGB"),
craft_image_codecs::ChannelLayout::Rgba => (4, "DeviceRGB"),
_ => return Err(bad("unexpected PNG colour layout".into())),
};
let samples: std::borrow::Cow<'_, [u8]> = if let Some(u8s) = img.as_u8() {
std::borrow::Cow::Borrowed(u8s)
} else if let Some(u16s) = img.to_u16_samples() {
std::borrow::Cow::Owned(u16s.into_iter().map(|v| (v >> 8) as u8).collect())
} else {
return Err(bad("expected 8-bit or 16-bit samples".into()));
};
let has_alpha = channels == 2 || channels == 4;
let colour_n = channels - usize::from(has_alpha);
@@ -221,24 +203,26 @@ pub fn decode(data: &[u8]) -> Result<Decoded, String> {
}
}
let alpha = (has_alpha && alpha.iter().any(|a| *a != 255)).then_some(alpha);
Ok(Decoded { width: frame.width, height: frame.height, color_space, samples: colour, alpha })
Ok(Decoded { width: fw, height: fh, color_space, samples: colour, alpha })
}
Some(k @ (Kind::Gif | Kind::Bmp | Kind::Tiff)) => {
// A GIF's frame may be larger than the logical screen the header gives.
let format = match k {
Kind::Gif => image::ImageFormat::Gif,
Kind::Bmp => image::ImageFormat::Bmp,
_ => image::ImageFormat::Tiff,
Kind::Gif => craft_image_codecs::Format::Gif,
Kind::Bmp => craft_image_codecs::Format::Bmp,
_ => craft_image_codecs::Format::Tiff,
};
let data = if k == Kind::Bmp { bmp_with_pixel_offset(data) } else { std::borrow::Cow::Borrowed(data) };
let img = if k == Kind::Tiff { decode_tiff(&data) } else { reader(&data, format).decode() }
.map_err(|e| if matches!(e, image::ImageError::Limits(_)) { too_big() } else { bad(e.to_string()) })?;
let rgba = img.into_rgba8();
let (width, height) = rgba.dimensions();
let opts = craft_image_codecs::DecodeOptions { keep_orientation: false, limits: decode_limits(), ..Default::default() };
let img = craft_image_codecs::decode_as_with(format, &data, &opts).map_err(|e| match e {
craft_image_codecs::CodecError::LimitExceeded(_) => too_big(),
other => bad(other.to_string()),
})?;
let raw = img.to_rgba8();
let (width, height) = (img.width(), img.height());
if !fits(width, height) {
return Err(too_big());
}
let px = rgba.as_raw().as_chunks::<4>().0;
let px = raw.as_chunks::<4>().0;
let gray = px.iter().all(|p| p[0] == p[1] && p[1] == p[2]);
let samples: Vec<u8> = if gray { px.iter().map(|p| p[0]).collect() } else { px.iter().flat_map(|p| [p[0], p[1], p[2]]).collect() };
let alpha = px.iter().any(|p| p[3] != 255).then(|| px.iter().map(|p| p[3]).collect());
+1
View File
@@ -21,6 +21,7 @@ mod proto;
pub mod script;
pub mod text;
mod traverse;
mod xml;
use pdfcraft_cos::Document;
+7 -7
View File
@@ -1,8 +1,8 @@
//! Template XML → [`model`](crate::model). Only what the layout engine uses is read; everything
//! else in the template is ignored, never rejected.
use crate::xml::Node as XmlNode;
use base64::Engine;
use roxmltree::Node as XmlNode;
use crate::XfaError;
use crate::model::{
@@ -62,11 +62,11 @@ fn scale(v: &str) -> Option<f64> {
}
/// The first child element of `n` named `name` (by local name, in any namespace).
fn child<'a, 'd>(n: XmlNode<'a, 'd>, name: &str) -> Option<XmlNode<'a, 'd>> {
fn child<'a>(n: XmlNode<'a>, name: &str) -> Option<XmlNode<'a>> {
n.children().find(|c| c.is_element() && c.tag_name().name() == name)
}
fn children<'a, 'd>(n: XmlNode<'a, 'd>, name: &'a str) -> impl Iterator<Item = XmlNode<'a, 'd>> + 'a {
fn children<'a>(n: XmlNode<'a>, name: &'a str) -> impl Iterator<Item = XmlNode<'a>> + 'a {
n.children().filter(move |c| c.is_element() && c.tag_name().name() == name)
}
@@ -415,7 +415,7 @@ fn parse_props(n: XmlNode, warnings: &mut Vec<String>) -> Props {
/// The property elements under `n`, whose tag is `within` (empty for the object itself):
/// `extras` in the object and inside one (forms keep named values there), `script` in a
/// `validate` or a `calculate` (XFA 3.3 p. 880, p. 641, p. 827).
fn elements<'a, 'd>(n: XmlNode<'a, 'd>, within: &str) -> impl Iterator<Item = XmlNode<'a, 'd>> + use<'a, 'd> {
fn elements<'a>(n: XmlNode<'a>, within: &str) -> impl Iterator<Item = XmlNode<'a>> + use<'a> {
let (extras, script) = (within.is_empty() || within == "extras", matches!(within, "validate" | "calculate"));
let ns = n.tag_name().namespace();
n.children().filter(move |c| {
@@ -1266,8 +1266,8 @@ pub fn parse(xml: &str) -> Result<(Template, Vec<String>), XfaError> {
crate::MAX_XML_DEPTH
)));
}
let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let doc = roxmltree::Document::parse_with_options(xml, opts()).map_err(|e| XfaError::Malformed(format!("template XML: {e}")))?;
let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() };
let doc = crate::xml::Document::parse_with_options(xml, opts()).map_err(|e| XfaError::Malformed(format!("template XML: {e}")))?;
let root = doc.root_element();
// The XDP's config packet has a <template> element too: the one we want is in the
// xfa-template namespace, or at least has a subform in it.
@@ -1287,7 +1287,7 @@ pub fn parse(xml: &str) -> Result<(Template, Vec<String>), XfaError> {
// written again with them merged in. The saved state below still comes from `doc`.
let (expanded, mut warnings) = crate::proto::expand(template);
let expanded_doc;
let template = match expanded.as_deref().map(|x| roxmltree::Document::parse_with_options(x, opts())) {
let template = match expanded.as_deref().map(|x| crate::xml::Document::parse_with_options(x, opts())) {
Some(Ok(d)) => {
expanded_doc = d;
expanded_doc.root_element()
+2 -2
View File
@@ -151,9 +151,9 @@ pub fn strip(doc: &mut Document) -> Result<Stripped, XfaError> {
}
// One XDP: the packets it holds are the envelope's children.
let text = s.decoded_within(64 << 20).ok().and_then(|b| crate::packets::decode(&b)).map(|(t, _)| t).unwrap_or_default();
let opts = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: 1_000_000, ..Default::default() };
let opts = crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: 1_000_000, ..Default::default() };
if crate::xml_nesting_depth(&text) <= crate::MAX_XML_DEPTH
&& let Ok(parsed) = roxmltree::Document::parse_with_options(&text, opts)
&& let Ok(parsed) = crate::xml::Document::parse_with_options(&text, opts)
{
let root = parsed.root_element();
if root.tag_name().name() == "xdp" {
+20 -20
View File
@@ -24,7 +24,7 @@
use std::collections::{HashMap, HashSet};
use roxmltree::{Node, NodeId};
use crate::xml::{Node, NodeId};
/// Deepest element nesting written. A prototype may contain a reference to an element that
/// contains it (Example 6.8): such loops are cut, and this bounds what remains.
@@ -107,19 +107,19 @@ pub(crate) fn expand(template: Node) -> (Option<String>, Vec<String>) {
/// default namespace).
type Scope<'a> = HashMap<Option<&'a str>, &'a str>;
struct Writer<'a, 'd> {
template: Node<'a, 'd>,
struct Writer<'a> {
template: Node<'a>,
out: String,
limit: usize,
/// Why expanding stopped, for the warning.
failed: Option<String>,
/// The template's elements by `id`, built on the first `#id` reference.
ids: Option<HashMap<&'a str, Node<'a, 'd>>>,
ids: Option<HashMap<&'a str, Node<'a>>>,
/// The prototype each element names, once resolved: a prototype shared by many elements
/// is reached again from each of them, and its reference (of any length) is read once.
refs: HashMap<NodeId, Option<Node<'a, 'd>>>,
refs: HashMap<NodeId, Option<Node<'a>>>,
/// SOM references already resolved, by their text.
soms: HashMap<&'a str, Option<Node<'a, 'd>>>,
soms: HashMap<&'a str, Option<Node<'a>>>,
/// Elements left to look at for SOM references.
search: usize,
/// Children, attributes and namespaces left to look at while merging layers.
@@ -162,9 +162,9 @@ fn alternative(tag: &str, c: &Node) -> bool {
c.is_element() && (tag == "value" || tag == "ui" && !matches!(c.tag_name().name(), "picture" | "extras"))
}
impl<'a, 'd> Writer<'a, 'd> {
impl<'a> Writer<'a> {
/// The element `n` names as its prototype. `usehref` wins over `use` when both are set.
fn reference(&mut self, n: Node<'a, 'd>) -> Option<Node<'a, 'd>> {
fn reference(&mut self, n: Node<'a>) -> Option<Node<'a>> {
if let Some(found) = self.refs.get(&n.id()) {
return *found;
}
@@ -176,7 +176,7 @@ impl<'a, 'd> Writer<'a, 'd> {
found
}
fn find_reference(&mut self, n: Node<'a, 'd>) -> Option<Node<'a, 'd>> {
fn find_reference(&mut self, n: Node<'a>) -> Option<Node<'a>> {
let set = |name: &str| n.attribute(name).map(str::trim).filter(|v| !v.is_empty());
let found = if let Some(href) = set("usehref") {
match href.split_once('#') {
@@ -215,7 +215,7 @@ impl<'a, 'd> Writer<'a, 'd> {
}
}
fn by_id(&mut self, id: &str) -> Option<Node<'a, 'd>> {
fn by_id(&mut self, id: &str) -> Option<Node<'a>> {
let template = self.template;
let ids = self.ids.get_or_insert_with(|| {
let mut ids = HashMap::new();
@@ -230,7 +230,7 @@ impl<'a, 'd> Writer<'a, 'd> {
}
/// A SOM expression from the template down, such as `$template.#subform.styles.Heading[1]`.
fn som(&mut self, expr: &'a str) -> Option<Node<'a, 'd>> {
fn som(&mut self, expr: &'a str) -> Option<Node<'a>> {
if expr.len() > MAX_SOM {
return None;
}
@@ -246,7 +246,7 @@ impl<'a, 'd> Writer<'a, 'd> {
found
}
fn resolve(&mut self, expr: &str) -> Option<Node<'a, 'd>> {
fn resolve(&mut self, expr: &str) -> Option<Node<'a>> {
let mut steps = expr.split('.').map(str::trim).peekable();
while steps.peek().is_some_and(|s| matches!(*s, "$template" | "template" | "template[0]" | "xfa" | "$xfa" | "xfa[0]")) {
steps.next();
@@ -269,7 +269,7 @@ impl<'a, 'd> Writer<'a, 'd> {
/// The children of `at` called `name` (or of class `#name`), looking through nameless
/// containers, until the one at `index` is found.
fn step(&mut self, at: Node<'a, 'd>, name: &str, index: usize, out: &mut Vec<Node<'a, 'd>>, depth: usize) {
fn step(&mut self, at: Node<'a>, name: &str, index: usize, out: &mut Vec<Node<'a>>, depth: usize) {
if depth > MAX_DEPTH {
return;
}
@@ -295,7 +295,7 @@ impl<'a, 'd> Writer<'a, 'd> {
}
/// `n` and the prototypes it inherits from, nearest first.
fn chain(&mut self, n: Node<'a, 'd>, layers: &mut Vec<Node<'a, 'd>>) {
fn chain(&mut self, n: Node<'a>, layers: &mut Vec<Node<'a>>) {
let mut seen = HashSet::from([n.id()]);
layers.push(n);
let mut at = n;
@@ -332,7 +332,7 @@ impl<'a, 'd> Writer<'a, 'd> {
/// element, by prefix, and return the scope inside it: the nearest layer's binding of a prefix
/// wins, and a prefix bound around the element to another URI is bound again here (an XHTML
/// `body` inside a template whose default namespace is XFA's).
fn namespaces(&mut self, layers: &[Node<'a, 'd>], scope: &Scope<'a>) -> Scope<'a> {
fn namespaces(&mut self, layers: &[Node<'a>], scope: &Scope<'a>) -> Scope<'a> {
let mut inner: Scope<'a> = scope.clone();
let mut decided: HashSet<Option<&'a str>> = HashSet::new();
for layer in layers {
@@ -358,7 +358,7 @@ impl<'a, 'd> Writer<'a, 'd> {
}
/// Write the attributes of the element `layers` make, inside the namespace scope `inner`.
fn attributes(&mut self, layers: &[Node<'a, 'd>], inner: &Scope<'a>) {
fn attributes(&mut self, layers: &[Node<'a>], inner: &Scope<'a>) {
// Attributes: the nearest layer that has one wins; `id` and `name` are not inherited.
// Attributes are told apart by namespace URI and local name. One whose layer's prefix is
// bound here to another URI (the nearest layer's binding won) is written with a fresh
@@ -415,7 +415,7 @@ impl<'a, 'd> Writer<'a, 'd> {
/// Write the element that `stack` makes together: `stack[0]` over `stack[1]` over …, each
/// an element of the same class. `scope` holds the namespaces already declared around it.
fn element(&mut self, stack: &[Node<'a, 'd>], scope: &Scope<'a>, depth: usize) {
fn element(&mut self, stack: &[Node<'a>], scope: &Scope<'a>, depth: usize) {
if self.failed.is_some() {
return;
}
@@ -477,9 +477,9 @@ impl<'a, 'd> Writer<'a, 'd> {
// out is left out whole, so it need not be counted either.
let left_out = |c: &Node| c.tag_name().name() == "proto" || alternative(tag, c) && chosen != Some(c.tag_name().name());
// The deeper layers' children by (class, position among that class).
let index = |l: &Node<'a, 'd>| {
let index = |l: &Node<'a>| {
let mut counts: HashMap<&str, usize> = HashMap::new();
let mut by: HashMap<(&'a str, usize), Node<'a, 'd>> = HashMap::new();
let mut by: HashMap<(&'a str, usize), Node<'a>> = HashMap::new();
for c in l.children().filter(|c| c.is_element() && !left_out(c)) {
let k = counts.entry(c.tag_name().name()).or_insert(0);
by.insert((c.tag_name().name(), *k), c);
@@ -507,7 +507,7 @@ impl<'a, 'd> Writer<'a, 'd> {
if !done.insert((ctag, n)) {
continue;
}
let stack: Vec<Node<'a, 'd>> = std::iter::once(c).chain(below.iter().skip(i).filter_map(|b| b.get(&(ctag, n)).copied())).collect();
let stack: Vec<Node<'a>> = std::iter::once(c).chain(below.iter().skip(i).filter_map(|b| b.get(&(ctag, n)).copied())).collect();
self.element(&stack, &inner, depth + 1);
}
}
+4 -4
View File
@@ -258,7 +258,7 @@ fn prototype_children_that_write_nothing_still_count_against_a_budget() {
/// The template of `xml` written again by `expand`, parsed.
fn expanded(xml: &str) -> String {
let doc = roxmltree::Document::parse(xml).unwrap();
let doc = crate::xml::Document::parse(xml).unwrap();
let template = doc.descendants().find(|n| n.tag_name().name() == "template").unwrap();
let (out, warnings) = crate::proto::expand(template);
assert!(warnings.is_empty(), "{warnings:?}");
@@ -276,7 +276,7 @@ fn namespaces_bound_again_inside_the_template_keep_their_own_uri() {
<draw name="none" x="0mm" y="40mm" w="40mm"><extras><free xmlns=""/></extras></draw>"##;
let xml = form(styles, body);
let out = expanded(&xml);
let doc = roxmltree::Document::parse(&out).unwrap();
let doc = crate::xml::Document::parse(&out).unwrap();
let bodies: Vec<_> = doc.descendants().filter(|n| n.tag_name().name() == "body").collect();
assert_eq!(bodies.len(), 2, "{out}");
for b in &bodies {
@@ -306,7 +306,7 @@ fn inherited_attributes_keep_their_namespace_where_the_prefix_is_bound_again() {
let styles = r#"<field id="P" xmlns:q="urn:prototype" q:kind="k" q:own="2" xml:lang="en" w="1in"/>"#;
let body = r##"<field name="f" xmlns:q="urn:instance" q:own="1" use="#P"/>"##;
let out = expanded(&form(styles, body));
let doc = roxmltree::Document::parse(&out).unwrap();
let doc = crate::xml::Document::parse(&out).unwrap();
let f = doc.descendants().find(|n| n.attribute("name") == Some("f")).unwrap();
assert_eq!(f.attribute(("urn:instance", "own")), Some("1"), "{out}");
assert_eq!(f.attribute(("urn:prototype", "own")), Some("2"), "{out}");
@@ -318,7 +318,7 @@ fn inherited_attributes_keep_their_namespace_where_the_prefix_is_bound_again() {
/// `expand` on the template of `xml`, timed.
fn expand_timed(xml: &str) -> (Option<String>, Vec<String>, std::time::Duration) {
let doc = roxmltree::Document::parse(xml).unwrap();
let doc = crate::xml::Document::parse(xml).unwrap();
let template = doc.descendants().find(|n| n.tag_name().name() == "template").unwrap();
let started = std::time::Instant::now();
let (out, warnings) = crate::proto::expand(template);
+387
View File
@@ -0,0 +1,387 @@
//! Lightweight namespace-aware XML DOM with source byte ranges, backed by `craft_xml`.
use std::ops::Range;
const XML_NS: &str = "http://www.w3.org/XML/1998/namespace";
const XMLNS_NS: &str = "http://www.w3.org/2000/xmlns/";
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct NodeId(pub(crate) usize);
#[derive(Clone, Debug)]
pub(crate) struct ParsingOptions {
pub(crate) allow_dtd: bool,
pub(crate) nodes_limit: u32,
}
impl Default for ParsingOptions {
fn default() -> Self {
Self { allow_dtd: false, nodes_limit: u32::MAX }
}
}
#[derive(Clone, Debug)]
struct AttrData {
ns: Option<String>,
name: String,
value: String,
}
#[derive(Clone, Debug)]
enum NodeKind {
Element {
ns: Option<String>,
name: String,
attrs: Vec<AttrData>,
/// In-scope `(prefix, uri)` bindings at this element (`""` for default namespace).
namespaces: Vec<(String, String)>,
range: Range<usize>,
},
Text(String),
}
#[derive(Clone, Debug)]
struct NodeData {
kind: NodeKind,
children: Vec<usize>,
}
pub(crate) struct Document {
nodes: Vec<NodeData>,
root: usize,
}
#[derive(Clone, Copy)]
pub(crate) struct Node<'a> {
doc: &'a Document,
id: usize,
}
impl PartialEq for Node<'_> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && std::ptr::eq(self.doc, other.doc)
}
}
impl Eq for Node<'_> {}
#[derive(Clone, Copy)]
pub(crate) struct ExpandedName<'a> {
ns: Option<&'a str>,
name: &'a str,
}
impl<'a> ExpandedName<'a> {
pub(crate) fn name(&self) -> &'a str {
self.name
}
pub(crate) fn namespace(&self) -> Option<&'a str> {
self.ns
}
}
pub(crate) trait AttributeKey {
fn matches(&self, ns: Option<&str>, name: &str) -> bool;
}
impl AttributeKey for &str {
fn matches(&self, ns: Option<&str>, name: &str) -> bool {
ns.is_none() && name == *self
}
}
impl AttributeKey for (&str, &str) {
fn matches(&self, ns: Option<&str>, name: &str) -> bool {
ns == Some(self.0) && name == self.1
}
}
#[derive(Clone, Copy)]
pub(crate) struct Attribute<'a> {
attr: &'a AttrData,
}
impl<'a> Attribute<'a> {
pub(crate) fn name(&self) -> &'a str {
&self.attr.name
}
pub(crate) fn namespace(&self) -> Option<&'a str> {
self.attr.ns.as_deref()
}
pub(crate) fn value(&self) -> &'a str {
&self.attr.value
}
}
#[derive(Clone, Copy)]
pub(crate) struct Namespace<'a> {
prefix: &'a str,
uri: &'a str,
}
impl<'a> Namespace<'a> {
pub(crate) fn name(&self) -> Option<&'a str> {
(!self.prefix.is_empty()).then_some(self.prefix)
}
pub(crate) fn uri(&self) -> &'a str {
self.uri
}
}
fn split_qname(qname: &str) -> (&str, &str) {
match qname.split_once(':') {
Some((p, l)) => (p, l),
None => ("", qname),
}
}
fn resolve_ns(scope: &[(String, String)], prefix: &str, is_attr: bool) -> Result<Option<String>, String> {
if prefix == "xml" {
return Ok(Some(XML_NS.to_string()));
}
if prefix == "xmlns" {
return Ok(Some(XMLNS_NS.to_string()));
}
if prefix.is_empty() {
if is_attr {
return Ok(None);
}
return Ok(scope.iter().rev().find(|(p, _)| p.is_empty()).and_then(|(_, u)| (!u.is_empty()).then(|| u.clone())));
}
match scope.iter().rev().find(|(p, _)| p == prefix) {
Some((_, u)) if !u.is_empty() => Ok(Some(u.clone())),
_ => Err(format!("undeclared namespace prefix: {prefix}")),
}
}
impl Document {
#[cfg(test)]
pub(crate) fn parse(text: &str) -> Result<Self, String> {
Self::parse_with_options(text, ParsingOptions::default())
}
pub(crate) fn parse_with_options(text: &str, options: ParsingOptions) -> Result<Self, String> {
let mut opts = craft_xml::Options::default();
opts.strict = true;
opts.doctype = if options.allow_dtd { craft_xml::Doctype::Ignore } else { craft_xml::Doctype::Reject };
opts.limits.max_nodes = options.nodes_limit as usize;
opts.limits.max_depth = crate::MAX_XML_DEPTH;
let mut events = opts.events(text.as_bytes());
let mut nodes: Vec<NodeData> = Vec::new();
let mut open: Vec<(usize, usize)> = Vec::new(); // (node_id, scope_len_before)
let mut scope: Vec<(String, String)> = Vec::new();
let mut root: Option<usize> = None;
while let Some(ev) = events.next().map_err(|e| e.to_string())? {
let span = events.span();
match ev {
craft_xml::Event::Start { name, attrs } => {
if nodes.len() >= options.nodes_limit as usize {
return Err("nodes limit reached".to_string());
}
let scope_mark = scope.len();
for (k, v) in &attrs {
if k == "xmlns" {
scope.push((String::new(), v.clone()));
} else if let Some(prefix) = k.strip_prefix("xmlns:") {
scope.push((prefix.to_string(), v.clone()));
}
}
let (el_prefix, el_local) = split_qname(&name);
let el_ns = resolve_ns(&scope, el_prefix, false)?;
let mut el_attrs = Vec::new();
for (k, v) in attrs {
if k == "xmlns" || k.starts_with("xmlns:") {
continue;
}
let (ap, al) = split_qname(&k);
let ans = resolve_ns(&scope, ap, true)?;
el_attrs.push(AttrData { ns: ans, name: al.to_string(), value: v });
}
// Deduplicate in-scope namespace bindings, keeping the innermost binding per prefix.
let mut namespaces: Vec<(String, String)> = Vec::new();
for (p, u) in scope.iter().rev() {
if !namespaces.iter().any(|(ep, _)| ep == p) {
namespaces.push((p.clone(), u.clone()));
}
}
namespaces.retain(|(_, u)| !u.is_empty());
let id = nodes.len();
nodes.push(NodeData {
kind: NodeKind::Element { ns: el_ns, name: el_local.to_string(), attrs: el_attrs, namespaces, range: span.start..span.end },
children: Vec::new(),
});
if let Some(&(parent, _)) = open.last() {
nodes[parent].children.push(id);
} else if root.is_none() {
root = Some(id);
}
open.push((id, scope_mark));
}
craft_xml::Event::End { .. } => {
if let Some((id, scope_mark)) = open.pop() {
if let NodeKind::Element { range, .. } = &mut nodes[id].kind {
range.end = span.end;
}
scope.truncate(scope_mark);
}
}
craft_xml::Event::Text(t) | craft_xml::Event::CData(t) => {
if let Some(&(parent, _)) = open.last() {
if let Some(&last_child) = nodes[parent].children.last()
&& let NodeKind::Text(existing) = &mut nodes[last_child].kind
{
existing.push_str(&t);
continue;
}
if nodes.len() >= options.nodes_limit as usize {
return Err("nodes limit reached".to_string());
}
let id = nodes.len();
nodes.push(NodeData { kind: NodeKind::Text(t), children: Vec::new() });
nodes[parent].children.push(id);
}
}
craft_xml::Event::Pi { .. } | craft_xml::Event::Comment(_) => {}
}
}
let Some(root) = root else {
return Err("empty XML document".to_string());
};
Ok(Self { nodes, root })
}
pub(crate) fn root_element(&self) -> Node<'_> {
Node { doc: self, id: self.root }
}
pub(crate) fn get_node(&self, id: NodeId) -> Option<Node<'_>> {
(id.0 < self.nodes.len()).then_some(Node { doc: self, id: id.0 })
}
pub(crate) fn descendants(&self) -> Descendants<'_> {
Descendants { doc: self, stack: vec![self.root] }
}
}
pub(crate) struct Descendants<'a> {
doc: &'a Document,
stack: Vec<usize>,
}
impl<'a> Iterator for Descendants<'a> {
type Item = Node<'a>;
fn next(&mut self) -> Option<Self::Item> {
let id = self.stack.pop()?;
let children = &self.doc.nodes[id].children;
for &c in children.iter().rev() {
self.stack.push(c);
}
Some(Node { doc: self.doc, id })
}
}
impl<'a> Node<'a> {
pub(crate) fn id(&self) -> NodeId {
NodeId(self.id)
}
pub(crate) fn document(&self) -> &'a Document {
self.doc
}
pub(crate) fn is_element(&self) -> bool {
matches!(self.doc.nodes[self.id].kind, NodeKind::Element { .. })
}
pub(crate) fn is_text(&self) -> bool {
matches!(self.doc.nodes[self.id].kind, NodeKind::Text(_))
}
pub(crate) fn tag_name(&self) -> ExpandedName<'a> {
match &self.doc.nodes[self.id].kind {
NodeKind::Element { ns, name, .. } => ExpandedName { ns: ns.as_deref(), name },
NodeKind::Text(_) => ExpandedName { ns: None, name: "" },
}
}
pub(crate) fn attribute<K: AttributeKey>(&self, key: K) -> Option<&'a str> {
match &self.doc.nodes[self.id].kind {
NodeKind::Element { attrs, .. } => attrs.iter().find(|a| key.matches(a.ns.as_deref(), &a.name)).map(|a| a.value.as_str()),
NodeKind::Text(_) => None,
}
}
pub(crate) fn attributes(&self) -> impl ExactSizeIterator<Item = Attribute<'a>> + use<'a> {
let attrs = match &self.doc.nodes[self.id].kind {
NodeKind::Element { attrs, .. } => attrs.as_slice(),
NodeKind::Text(_) => &[],
};
attrs.iter().map(|attr| Attribute { attr })
}
pub(crate) fn namespaces(&self) -> impl ExactSizeIterator<Item = Namespace<'a>> + use<'a> {
let ns = match &self.doc.nodes[self.id].kind {
NodeKind::Element { namespaces, .. } => namespaces.as_slice(),
NodeKind::Text(_) => &[],
};
ns.iter().map(|(p, u)| Namespace { prefix: p, uri: u })
}
pub(crate) fn lookup_prefix(&self, uri: &str) -> Option<&'a str> {
if uri == XML_NS {
return Some("xml");
}
match &self.doc.nodes[self.id].kind {
NodeKind::Element { namespaces, .. } => namespaces.iter().find(|(_, u)| u == uri).map(|(p, _)| p.as_str()),
NodeKind::Text(_) => None,
}
}
pub(crate) fn lookup_namespace_uri(&self, prefix: Option<&str>) -> Option<&'a str> {
if prefix == Some("xml") {
return Some(XML_NS);
}
if prefix == Some("xmlns") {
return Some(XMLNS_NS);
}
let target = prefix.unwrap_or("");
match &self.doc.nodes[self.id].kind {
NodeKind::Element { namespaces, .. } => namespaces.iter().find(|(p, _)| p == target).map(|(_, u)| u.as_str()),
NodeKind::Text(_) => None,
}
}
pub(crate) fn children(&self) -> impl Iterator<Item = Node<'a>> + use<'a> {
let doc = self.doc;
doc.nodes[self.id].children.iter().map(move |&id| Node { doc, id })
}
pub(crate) fn descendants(&self) -> Descendants<'a> {
Descendants { doc: self.doc, stack: vec![self.id] }
}
pub(crate) fn text(&self) -> Option<&'a str> {
match &self.doc.nodes[self.id].kind {
NodeKind::Text(t) => Some(t.as_str()),
NodeKind::Element { .. } => self.doc.nodes[self.id].children.first().and_then(|&cid| match &self.doc.nodes[cid].kind {
NodeKind::Text(t) => Some(t.as_str()),
_ => None,
}),
}
}
pub(crate) fn range(&self) -> Range<usize> {
match &self.doc.nodes[self.id].kind {
NodeKind::Element { range, .. } => range.clone(),
NodeKind::Text(_) => 0..0,
}
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ pdfcraft-annot = { workspace = true }
pdfcraft-forms = { workspace = true }
pdfcraft-model = { workspace = true }
craft-csv = { workspace = true }
roxmltree = "0.21"
craft-xml = { workspace = true }
base64 = "0.23"
thiserror = { workspace = true }
+12 -13
View File
@@ -15,7 +15,7 @@
use base64::Engine as _;
use super::common::{check_form_xml_depth, esc, parse_form_xml};
use super::common::{check_form_xml_depth, el_text, esc, parse_form_xml};
use super::{Dict, Document, ObjRef, Object, PdfString};
/// At most this much decoded XML in one appearance (and at most this much XML is written for one).
@@ -254,13 +254,12 @@ pub(crate) fn decode(text: &str, budget: &mut usize) -> Result<Node, String> {
let xml = std::str::from_utf8(&bytes).map_err(|_| "the appearance is not UTF-8 text".to_string())?;
let xml = xml.trim_start_matches('\u{feff}');
check_form_xml_depth(xml).map_err(|e| e.to_string())?;
let parsed = parse_form_xml(xml).map_err(|e| format!("the appearance is not XML: {e}"))?;
let root = parsed.root_element();
if !root.tag_name().name().eq_ignore_ascii_case("DICT") {
let root = parse_form_xml(xml).map_err(|e| format!("the appearance is not XML: {e}"))?;
if !root.local().eq_ignore_ascii_case("DICT") {
return Err("the appearance is not a dictionary".into());
}
let mut values = MAX_VALUES;
let node = read(root, 0, &mut values)?;
let node = read(&root, 0, &mut values)?;
let usable = match &node {
Node::Dict(entries) => entries.iter().any(|(k, v)| k.as_slice() == b"N" && matches!(v, Node::Stream(..))),
_ => false,
@@ -271,20 +270,20 @@ pub(crate) fn decode(text: &str, budget: &mut usize) -> Result<Node, String> {
Ok(node)
}
fn attr<'a>(node: roxmltree::Node<'a, '_>, name: &str) -> Option<&'a str> {
node.attributes().find(|a| a.name().eq_ignore_ascii_case(name)).map(|a| a.value())
fn attr<'a>(node: &'a craft_xml::El, name: &str) -> Option<&'a str> {
node.attrs.iter().find(|(k, _)| k.eq_ignore_ascii_case(name)).map(|(_, v)| v.as_str())
}
fn read(node: roxmltree::Node, depth: usize, values: &mut usize) -> Result<Node, String> {
fn read(node: &craft_xml::El, depth: usize, values: &mut usize) -> Result<Node, String> {
if depth > MAX_NESTING {
return Err(format!("the appearance nests objects more than {MAX_NESTING} deep"));
}
*values = values.checked_sub(1).ok_or_else(|| format!("the appearance holds more than {MAX_VALUES} values"))?;
let tag = node.tag_name().name().to_ascii_uppercase();
let tag = node.local().to_ascii_uppercase();
let val = || attr(node, "VAL").ok_or_else(|| format!("{tag} has no VAL"));
let entries = |values: &mut usize| -> Result<Vec<(Vec<u8>, Node)>, String> {
let mut out = Vec::new();
for child in node.children().filter(|c| c.is_element() && !c.tag_name().name().eq_ignore_ascii_case("DATA")) {
for child in node.elements().filter(|c| !c.local().eq_ignore_ascii_case("DATA")) {
let key = attr(child, "KEY").ok_or_else(|| format!("an entry of {tag} has no KEY"))?;
out.push((pdfcraft_forms::name_bytes(key), read(child, depth + 1, values)?));
}
@@ -294,15 +293,15 @@ fn read(node: roxmltree::Node, depth: usize, values: &mut usize) -> Result<Node,
"DICT" => Node::Dict(entries(values)?),
"ARRAY" => {
let mut items = Vec::new();
for child in node.children().filter(|c| c.is_element()) {
for child in node.elements() {
items.push(read(child, depth + 1, values)?);
}
Node::Array(items)
}
"STREAM" => {
let mut dict = entries(values)?;
let data = node.children().find(|c| c.is_element() && c.tag_name().name().eq_ignore_ascii_case("DATA")).ok_or("a stream has no DATA")?;
let text: String = data.children().filter(|c| c.is_text()).filter_map(|c| c.text()).collect();
let data = node.elements().find(|c| c.local().eq_ignore_ascii_case("DATA")).ok_or("a stream has no DATA")?;
let text = el_text(data);
let bytes = match attr(data, "ENCODING").map(str::to_ascii_lowercase).as_deref() {
Some("hex") => bytes_of_hex(&text).ok_or("stream data is not hexadecimal")?,
Some("ascii") | None => text.into_bytes(),
+23 -9
View File
@@ -85,8 +85,23 @@ pub(crate) fn check_form_xml_depth(text: &str) -> Result<(), DataError> {
Ok(())
}
pub(crate) fn parse_form_xml(text: &str) -> Result<roxmltree::Document<'_>, roxmltree::Error> {
roxmltree::Document::parse_with_options(text, roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() })
pub(crate) fn parse_form_xml(text: &str) -> Result<craft_xml::El, craft_xml::Error> {
let mut opts = craft_xml::Options::default();
opts.strict = true;
opts.doctype = craft_xml::Doctype::Reject;
opts.limits.max_nodes = MAX_NODES as usize;
opts.limits.max_depth = MAX_XML_DEPTH.max(1024);
opts.parse(text.as_bytes())
}
pub(crate) fn el_text(el: &craft_xml::El) -> String {
el.children
.iter()
.filter_map(|n| match n {
craft_xml::Node::Text(t) => Some(t.as_str()),
_ => None,
})
.collect()
}
pub(crate) const SUBTYPES: [(&str, &str); 17] = [
@@ -240,23 +255,22 @@ pub(crate) fn xml_name(name: &str) -> String {
s
}
pub(crate) fn walk_xfdf(node: roxmltree::Node, prefix: &str, out: &mut Vec<(String, Vec<String>)>, depth: usize) -> Result<(), DataError> {
pub(crate) fn walk_xfdf(node: &craft_xml::El, prefix: &str, out: &mut Vec<(String, Vec<String>)>, depth: usize) -> Result<(), DataError> {
if depth > MAX_DEPTH {
return Err(DataError::Malformed(format!("XFDF fields nest more than {MAX_DEPTH} deep")));
}
for f in node.children().filter(|c| c.has_tag_name("field")) {
let Some(name) = f.attribute("name") else { continue };
for f in node.children(craft_xml::Local("field")) {
let Some(name) = f.attr("name") else { continue };
let full = if prefix.is_empty() { name.to_string() } else { format!("{prefix}.{name}") };
let vals: Vec<String> = f
.children()
.filter(|c| c.has_tag_name("value"))
.children(craft_xml::Local("value"))
// Comments and processing instructions can split a value into several text nodes.
.map(|v| v.children().filter(|n| n.is_text()).filter_map(|n| n.text()).collect())
.map(el_text)
.collect();
// A leaf with no `<value>` is an empty field (the export writes one that way), so it
// clears the field like FDF's `/V ()`; a field with kids only names their parent. A
// leaf holding only `<value-richtext>` is not read yet, so it leaves the field alone.
let names_kids_or_rich = f.children().any(|c| c.has_tag_name("field") || c.has_tag_name("value-richtext"));
let names_kids_or_rich = f.elements().any(|c| matches!(c.local(), "field" | "value-richtext"));
if !vals.is_empty() || !names_kids_or_rich {
out.push((full.clone(), vals));
}
+8 -12
View File
@@ -27,7 +27,7 @@ mod xfdf;
#[cfg(test)]
use common::{MAX_DEPTH, MAX_XML_DEPTH, arr, nums_of, parse_hex, text_of, xml_nesting_depth};
use common::{check_form_xml_depth, esc, parse_form_xml, walk_xfdf, xml_name};
use common::{check_form_xml_depth, el_text, esc, parse_form_xml, walk_xfdf, xml_name};
pub use fdf::export_fdf;
use fdf::{ImportBudget, fdf_body, import_fdf, walk_fdf};
#[cfg(test)]
@@ -178,13 +178,12 @@ pub fn data_values(bytes: &[u8]) -> Result<Vec<(String, Vec<String>)>, DataError
let text = String::from_utf8_lossy(bytes);
let t = text.trim_start_matches('\u{feff}').trim_start();
check_form_xml_depth(t)?;
let xml = parse_form_xml(t).map_err(|_| DataError::UnknownFormat)?;
let root = xml.root_element();
if root.tag_name().name() != "xfdf" {
let root = parse_form_xml(t).map_err(|_| DataError::UnknownFormat)?;
if root.local() != "xfdf" {
return Err(DataError::UnknownFormat);
}
let mut values = Vec::new();
if let Some(fields) = root.children().find(|c| c.has_tag_name("fields")) {
if let Some(fields) = root.child(craft_xml::Local("fields")) {
walk_xfdf(fields, "", &mut values, 0)?;
}
Ok(values)
@@ -283,15 +282,12 @@ fn import_table(doc: &mut Document, text: &str) -> Result<Report, DataError> {
let t = text.trim_start_matches('\u{feff}');
let values: Vec<(String, Vec<String>)> = if t.trim_start().starts_with('<') {
check_form_xml_depth(t)?;
let xml = parse_form_xml(t).map_err(|e| DataError::Malformed(e.to_string()))?;
xml.root_element()
.children()
.filter(|c| c.is_element())
let root = parse_form_xml(t).map_err(|e| DataError::Malformed(e.to_string()))?;
root.elements()
.map(|c| {
let name =
c.attributes().find(|a| a.name() == "original").map(|a| a.value().to_string()).unwrap_or_else(|| c.tag_name().name().to_string());
let name = c.attr_local("original").map(str::to_owned).unwrap_or_else(|| c.local().to_string());
// Comments and processing instructions do not end a field's character data.
(name, vec![c.children().filter(|node| node.is_text()).filter_map(|node| node.text()).collect()])
(name, vec![el_text(c)])
})
.collect()
} else if has_tab_delimiter(t) {
+32 -36
View File
@@ -245,13 +245,13 @@ fn an_unnamed_group_primary_gets_an_export_only_name() {
add_annotation(&mut src, &note, &Meta { date: None, id: clash.clone() }).unwrap();
let xfdf = export_xfdf(&src, true, false, "a.pdf");
let doc = roxmltree::Document::parse(&xfdf).unwrap();
let el = |t: &str| doc.descendants().find(|n| n.has_tag_name(t)).unwrap_or_else(|| panic!("no <{t}>: {xfdf}"));
let doc = common::parse_form_xml(&xfdf).unwrap();
let el = |t: &str| doc.descendants().find(|n| n.local() == t).unwrap_or_else(|| panic!("no <{t}>: {xfdf}"));
let (tag, sub_tag) = if caret_primary { ("caret", "strikeout") } else { ("strikeout", "caret") };
let name = el(tag).attribute("name").unwrap_or_else(|| panic!("the unnamed primary gets a name ({caret_primary}): {xfdf}"));
assert_eq!(el(sub_tag).attribute("inreplyto"), Some(name), "{xfdf}");
let name = el(tag).attr("name").unwrap_or_else(|| panic!("the unnamed primary gets a name ({caret_primary}): {xfdf}"));
assert_eq!(el(sub_tag).attr("inreplyto"), Some(name), "{xfdf}");
assert_ne!(name, clash, "a made-up name never repeats a real /NM");
assert_eq!(doc.descendants().filter(|n| n.attribute("name") == Some(name)).count(), 1, "{xfdf}");
assert_eq!(doc.descendants().filter(|n| n.attr("name") == Some(name)).count(), 1, "{xfdf}");
assert!(src.get(primary).as_dict().unwrap().get(b"NM").is_none(), "the name is for the export only");
assert!(src.get(sub).as_dict().unwrap().get(b"NM").is_none());
@@ -306,11 +306,11 @@ fn an_export_only_name_never_repeats_an_inline_annotations_nm() {
src.update_dict(page, |p| p.set(b"Annots".to_vec(), Object::Array(annots))).unwrap();
let xfdf = export_xfdf(&src, true, false, "a.pdf");
let doc = roxmltree::Document::parse(&xfdf).unwrap();
let caret = doc.descendants().find(|n| n.has_tag_name("caret")).unwrap_or_else(|| panic!("no <caret>: {xfdf}"));
let name = caret.attribute("name").unwrap_or_else(|| panic!("the unnamed primary gets a name: {xfdf}"));
let doc = common::parse_form_xml(&xfdf).unwrap();
let caret = doc.descendants().find(|n| n.local() == "caret").unwrap_or_else(|| panic!("no <caret>: {xfdf}"));
let name = caret.attr("name").unwrap_or_else(|| panic!("the unnamed primary gets a name: {xfdf}"));
assert_ne!(name, clash, "a made-up name never repeats an inline annotation's /NM: {xfdf}");
assert_eq!(doc.descendants().filter(|n| n.attribute("name") == Some(&clash[..])).count(), 1, "{xfdf}");
assert_eq!(doc.descendants().filter(|n| n.attr("name") == Some(&clash[..])).count(), 1, "{xfdf}");
}
#[test]
@@ -318,15 +318,15 @@ fn xml_exports_drop_xml_noncharacters() {
let mut src = filled();
set_value(&mut src, "Name", &FieldValue::Text("a\u{FFFE}b\u{FFFF}c".into())).unwrap();
let xfdf = export_xfdf(&src, false, true, "f.pdf");
let doc = roxmltree::Document::parse(&xfdf).unwrap_or_else(|e| panic!("XFDF is not well-formed: {e}: {xfdf:?}"));
let doc = common::parse_form_xml(&xfdf).unwrap_or_else(|e| panic!("XFDF is not well-formed: {e}: {xfdf:?}"));
let value = doc
.descendants()
.find(|n| n.has_tag_name("field") && n.attribute("name") == Some("Name"))
.and_then(|n| n.children().find(|c| c.has_tag_name("value")))
.and_then(|n| n.text());
assert_eq!(value, Some("abc"));
.find(|n| n.local() == "field" && n.attr("name") == Some("Name"))
.and_then(|n| n.child(craft_xml::Local("value")))
.map(|n| n.text());
assert_eq!(value.as_deref(), Some("abc"));
let data = export_data(&src, Format::Xml);
roxmltree::Document::parse(&data).unwrap_or_else(|e| panic!("XML is not well-formed: {e}: {data:?}"));
common::parse_form_xml(&data).unwrap_or_else(|e| panic!("XML is not well-formed: {e}: {data:?}"));
}
#[test]
@@ -347,15 +347,11 @@ fn xml_exports_preserve_whitespace_in_field_names() {
let src = Document::open(Arc::new(source_bytes.clone())).unwrap();
for format in [Format::Xml, Format::Xfdf] {
let exported = if format == Format::Xfdf { export_xfdf(&src, false, true, "form.pdf") } else { export_data(&src, format) };
let xml = roxmltree::Document::parse(&exported).unwrap();
let xml = common::parse_form_xml(&exported).unwrap();
let exported_names: Vec<_> = if format == Format::Xfdf {
xml.descendants().filter(|n| n.has_tag_name("field")).map(|n| n.attribute("name").unwrap()).collect()
xml.descendants().filter(|n| n.local() == "field").map(|n| n.attr("name").unwrap()).collect()
} else {
xml.root_element()
.children()
.filter(|n| n.is_element())
.map(|n| n.attribute(("http://ns.adobe.com/xfdf-transition/", "original")).unwrap())
.collect()
xml.elements().map(|n| n.attr("xfdf:original").unwrap()).collect()
};
assert_eq!(exported_names, names, "{format:?}: {exported}");
let mut dst = Document::open(Arc::new(source_bytes.clone())).unwrap();
@@ -409,14 +405,14 @@ fn xfdf_exports_preserve_whitespace_in_comment_attributes() {
let mut dst = Document::open(Arc::new(source_bytes)).unwrap();
let file = "form\twith\nlines.pdf";
let xfdf = export_xfdf(&dst, true, false, file);
let xml = roxmltree::Document::parse(&xfdf).unwrap();
let notes: Vec<_> = xml.descendants().filter(|n| n.has_tag_name("text")).collect();
assert_eq!(notes.iter().map(|n| n.attribute("name").unwrap()).collect::<Vec<_>>(), names);
let xml = common::parse_form_xml(&xfdf).unwrap();
let notes: Vec<_> = xml.descendants().filter(|n| n.local() == "text").collect();
assert_eq!(notes.iter().map(|n| n.attr("name").unwrap()).collect::<Vec<_>>(), names);
for note in notes {
assert_eq!(note.attribute("title"), Some(author));
assert_eq!(note.attribute("subject"), Some(subject));
assert_eq!(note.attr("title"), Some(author));
assert_eq!(note.attr("subject"), Some(subject));
}
assert_eq!(xml.descendants().find(|n| n.has_tag_name("f")).unwrap().attribute("href"), Some(file));
assert_eq!(xml.descendants().find(|n| n.local() == "f").unwrap().attr("href"), Some(file));
let before = annotation_refs(&dst);
let report = import(&mut dst, xfdf.as_bytes()).unwrap();
assert_eq!(report.comments, names.len());
@@ -646,12 +642,12 @@ fn xml_field_names_remain_valid_and_round_trip_without_collisions() {
let want = values(&src);
for format in [Format::Xml, Format::Xdp] {
let text = export_data(&src, format);
let xml = roxmltree::Document::parse(&text).unwrap();
let children: Vec<_> = xml.root_element().children().filter(|n| n.is_element()).collect();
let xml = common::parse_form_xml(&text).unwrap();
let children: Vec<_> = xml.elements().collect();
assert_eq!(children.len(), names.len());
for (child, (original, tag)) in children.iter().zip(names) {
assert_eq!(child.tag_name().name(), tag);
assert_eq!(child.attribute(("http://ns.adobe.com/xfdf-transition/", "original")), (original != tag).then_some(original));
assert_eq!(child.local(), tag);
assert_eq!(child.attr("xfdf:original"), (original != tag).then_some(original));
}
assert!(!src.is_modified(), "export must not change the source PDF");
assert_eq!(src.bytes().as_slice(), source.as_slice());
@@ -2101,11 +2097,11 @@ fn xfdf_line_ending_names_are_escaped() {
let line = doc.add(line);
doc.update_dict(page, |d| d.set(b"Annots".to_vec(), Object::Array(vec![Object::Ref(line)]))).unwrap();
let xml = export_xfdf(&doc, true, false, "form.pdf");
let parsed = roxmltree::Document::parse(&xml).unwrap();
assert!(!parsed.descendants().any(|n| n.has_tag_name("field")), "{xml}");
let el = parsed.descendants().find(|n| n.has_tag_name("line")).unwrap();
let parsed = common::parse_form_xml(&xml).unwrap();
assert!(!parsed.descendants().any(|n| n.local() == "field"), "{xml}");
let el = parsed.descendants().find(|n| n.local() == "line").unwrap();
let want = String::from_utf8(hostile).unwrap();
assert_eq!((el.attribute("head"), el.attribute("tail")), (Some(want.as_str()), Some(want.as_str())));
assert_eq!((el.attr("head"), el.attr("tail")), (Some(want.as_str()), Some(want.as_str())));
}
/// A comment locked through an indirect `/F` is as protected from a same-named import as one
+9 -6
View File
@@ -144,7 +144,7 @@ fn free_text_formatting_is_exported_as_xfdf_elements_and_attributes() {
assert!(xml.contains(want), "missing {want} in\n{xml}");
}
// What it writes is XML the reader accepts.
assert!(roxmltree::Document::parse(&xml).is_ok());
assert!(common::parse_form_xml(&xml).is_ok());
}
#[test]
@@ -1002,7 +1002,10 @@ fn rich_text_keeps_the_namespaces_it_inherits() {
assert!(common::parse_form_xml(&rc).is_ok(), "{rc}");
let out = export_xfdf(&dst, true, false, "dst.pdf");
assert!(
out.contains(r#"<contents-richtext><body xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xmlns="http://www.w3.org/1999/xhtml" xfa:spec"#),
out.contains(r#"<contents-richtext><body "#)
&& out.contains(r#"xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/""#)
&& out.contains(r#"xmlns="http://www.w3.org/1999/xhtml""#)
&& out.contains(r#"xfa:spec="2.0.2""#),
"{out}"
);
}
@@ -1175,7 +1178,7 @@ fn rich_text_keeps_an_inherited_default_namespace() {
import(&mut dst, xml.as_bytes()).unwrap();
let rc = text(&dst, &named(&dst, "d"), b"RC").unwrap();
let parsed = common::parse_form_xml(&rc).unwrap();
assert_eq!(parsed.root_element().tag_name().namespace(), Some("http://www.w3.org/1999/xhtml"), "{rc}");
assert_eq!(parsed.ns_decls().find(|(p, _)| *p == parsed.prefix()).map(|(_, u)| u), Some("http://www.w3.org/1999/xhtml"), "{rc}");
let rc = text(&dst, &named(&dst, "w"), b"RC").unwrap();
assert!(common::parse_form_xml(&rc).is_ok(), "{rc}");
assert!(!rc.contains(r#"xmlns:a="urn:a""#), "{rc}");
@@ -1196,13 +1199,13 @@ fn rich_text_namespaces_come_from_attributes_and_the_scope() {
import(&mut dst, xml.as_bytes()).unwrap();
let rc = text(&dst, &named(&dst, "v"), b"RC").unwrap();
let parsed = common::parse_form_xml(&rc).unwrap_or_else(|e| panic!("{e}: {rc}"));
assert!(parsed.root_element().attributes().any(|a| a.namespace() == Some("urn:b")), "{rc}");
assert!(parsed.ns_decls().any(|(p, u)| p == "b" && u == "urn:b") && parsed.attr("b:x").is_some(), "{rc}");
let rc = text(&dst, &named(&dst, "g"), b"RC").unwrap();
assert!(common::parse_form_xml(&rc).is_ok(), "a `>` in a value does not end the start tag: {rc}");
let rc = text(&dst, &named(&dst, "p"), b"RC").unwrap();
let parsed = common::parse_form_xml(&rc).unwrap();
let p = parsed.descendants().find(|n| n.has_tag_name("p")).unwrap();
assert_eq!(p.tag_name().namespace(), Some("http://www.w3.org/1999/xhtml"), "{rc}");
let p = parsed.descendants().find(|n| n.local() == "p").unwrap();
assert_eq!(parsed.ns_decls().find(|(k, _)| *k == p.prefix()).map(|(_, u)| u), Some("http://www.w3.org/1999/xhtml"), "{rc}");
}
/// A redaction mark whose /Rect has no area shows its quads all the same: the /Rect grows to hold
+151 -141
View File
@@ -1,6 +1,6 @@
use super::common::{
FLAGS, MAX_DEPTH, SUBTYPES, arr, check_form_xml_depth, csv, esc, hex, n, nums_of, parse_form_xml, parse_hex, quad_has_area, rect_strict, text_of,
walk_xfdf, xml_nesting_depth,
FLAGS, MAX_DEPTH, SUBTYPES, arr, check_form_xml_depth, csv, el_text, esc, hex, n, nums_of, parse_form_xml, parse_hex, quad_has_area, rect_strict,
text_of, walk_xfdf, xml_nesting_depth,
};
use super::*;
@@ -89,8 +89,22 @@ fn own_keys(d: &Dict) -> Option<String> {
/// The `pdfcraft:keys` of an XFDF comment back as dictionary entries: PdfCraft's keys only, with
/// values that mean the same here. Anything malformed is skipped.
fn read_own_keys(node: roxmltree::Node, d: &mut Dict) {
let Some(text) = node.attribute((PDFCRAFT_NS, "keys")).filter(|t| t.len() <= MAX_OWN_KEYS) else { return };
fn read_own_keys(node: &craft_xml::El, ns: &[(String, String)], d: &mut Dict) {
let mut keys_text = None;
for (k, v) in &node.attrs {
if let Some((prefix, "keys")) = k.split_once(':') {
let uri = node
.ns_decls()
.find(|(p, _)| *p == prefix)
.map(|(_, u)| u)
.or_else(|| ns.iter().rfind(|(p, _)| p == prefix).map(|(_, u)| u.as_str()));
if uri == Some(PDFCRAFT_NS) {
keys_text = Some(v.as_str());
break;
}
}
}
let Some(text) = keys_text.filter(|t| t.len() <= MAX_OWN_KEYS) else { return };
let Ok(Object::Dict(keys)) = pdfcraft_cos::Lexer::new(text.as_bytes(), 0).object() else { return };
for (k, v) in keys.iter().filter(|(k, v)| k.starts_with(b"PC") && portable(v, 0)) {
if !d.contains(k) {
@@ -105,13 +119,13 @@ const MAX_RICH_DEPTH: usize = 512;
/// Rich text (`/RC`) as `<contents-richtext>` holds it: its root element alone (no XML
/// declaration), when the text is well-formed XML; `None` otherwise.
fn rich_text_element(rc: &str) -> Option<&str> {
fn rich_text_element(rc: &str) -> Option<String> {
let t = rc.trim_start_matches('\u{feff}');
if xml_nesting_depth(t) > MAX_RICH_DEPTH {
return None;
}
let parsed = parse_form_xml(t).ok()?;
t.get(parsed.root_element().range())
Some(parsed.to_xml())
}
/// The formatting attributes of a comment: XFDF 3.0 "Border style attributes" (`width`, `style`,
@@ -215,10 +229,10 @@ fn format_attrs(doc: &Document, d: &Dict, sub: &[u8], attrs: &mut String) {
/// The formatting attributes of an XFDF comment element back as dictionary entries (see
/// [`format_attrs`]). Values that are not numbers, or out of range, are left out.
fn read_format(node: roxmltree::Node, d: &mut Dict) {
let num = |a: &str| node.attribute(a).and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite());
let style = node.attribute("style").map(str::trim);
let dashes = node.attribute("dashes").map(csv).filter(|v| !v.is_empty() && v.iter().all(|x| *x >= 0.0) && v.iter().any(|x| *x > 0.0));
fn read_format(node: &craft_xml::El, d: &mut Dict) {
let num = |a: &str| node.attr(a).and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite());
let style = node.attr("style").map(str::trim);
let dashes = node.attr("dashes").map(csv).filter(|v| !v.is_empty() && v.iter().all(|x| *x >= 0.0) && v.iter().any(|x| *x > 0.0));
let bs_style = match style {
Some("dash") => Some("D"),
Some("bevelled" | "beveled") => Some("B"),
@@ -245,10 +259,10 @@ fn read_format(node: roxmltree::Node, d: &mut Dict) {
be.set(b"I".to_vec(), Object::Real(intensity.clamp(0.0, 2.0)));
d.set(b"BE".to_vec(), Object::Dict(be));
}
if let Some(r) = node.attribute("rotation").and_then(|r| r.trim().parse::<i32>().ok()) {
if let Some(r) = node.attr("rotation").and_then(|r| r.trim().parse::<i32>().ok()) {
d.set(b"Rotate".to_vec(), Object::Int(i64::from(r)));
}
let q = match node.attribute("justification").map(str::trim) {
let q = match node.attr("justification").map(str::trim) {
Some("left" | "0") => Some(0),
Some("centered" | "center" | "1") => Some(1),
Some("right" | "2") => Some(2),
@@ -257,7 +271,7 @@ fn read_format(node: roxmltree::Node, d: &mut Dict) {
if let Some(q) = q {
d.set(b"Q".to_vec(), Object::Int(q));
}
if let Some(rd) = node.attribute("fringe").map(csv).filter(|r| r.len() == 4) {
if let Some(rd) = node.attr("fringe").map(csv).filter(|r| r.len() == 4) {
d.set(b"RD".to_vec(), arr(&rd));
}
if let Some(ll) = num("leaderLength") {
@@ -269,27 +283,27 @@ fn read_format(node: roxmltree::Node, d: &mut Dict) {
if let Some(llo) = num("leader-offset").filter(|v| *v >= 0.0) {
d.set(b"LLO".to_vec(), Object::Real(llo));
}
match node.attribute("caption").map(str::trim) {
match node.attr("caption").map(str::trim) {
Some("yes" | "true") => d.set(b"Cap".to_vec(), Object::Bool(true)),
Some("no" | "false") => d.set(b"Cap".to_vec(), Object::Bool(false)),
_ => {}
}
if let Some(cp) = node.attribute("caption-style").map(str::trim).filter(|c| matches!(*c, "Inline" | "Top")) {
if let Some(cp) = node.attr("caption-style").map(str::trim).filter(|c| matches!(*c, "Inline" | "Top")) {
d.set(b"CP".to_vec(), Object::name(cp));
}
let (h, v) = (num("caption-offset-h"), num("caption-offset-v"));
if h.is_some() || v.is_some() {
d.set(b"CO".to_vec(), arr(&[h.unwrap_or(0.0), v.unwrap_or(0.0)]));
}
match node.attribute("symbol").map(str::trim) {
match node.attr("symbol").map(str::trim) {
Some("paragraph" | "P") => d.set(b"Sy".to_vec(), Object::name("P")),
Some("none" | "None") => d.set(b"Sy".to_vec(), Object::name("None")),
_ => {}
}
if let Some(t) = node.attribute("overlay-text") {
if let Some(t) = node.attr("overlay-text") {
d.set(b"OverlayText".to_vec(), PdfString::text(t));
}
match node.attribute("overlay-text-repeat").map(str::trim) {
match node.attr("overlay-text-repeat").map(str::trim) {
Some("true" | "yes") => d.set(b"Repeat".to_vec(), Object::Bool(true)),
Some("false" | "no") => d.set(b"Repeat".to_vec(), Object::Bool(false)),
_ => {}
@@ -298,24 +312,31 @@ fn read_format(node: roxmltree::Node, d: &mut Dict) {
/// A `<contents-richtext>` element's plain text, a line per paragraph: what `/Contents` gets
/// when the element comes without `<contents>`.
fn rich_plain(node: roxmltree::Node) -> String {
let mut out = String::new();
for n in node.descendants() {
// A line break is a line of its own, so two in a row leave a blank line; a paragraph
// starts on a new line.
if n.is_element() && n.tag_name().name() == "br" {
out.push('\r');
} else if n.is_element() && matches!(n.tag_name().name(), "p" | "div" | "li") {
if !out.is_empty() && !out.ends_with('\r') {
out.push('\r');
fn walk_rich_plain(node: &craft_xml::El, out: &mut String) {
for n in &node.children {
match n {
craft_xml::Node::El(el) => {
if el.local() == "br" {
out.push('\r');
} else if matches!(el.local(), "p" | "div" | "li") && !out.is_empty() && !out.ends_with('\r') {
out.push('\r');
}
walk_rich_plain(el, out);
}
} else if let Some(t) = n.text().filter(|_| n.is_text()) {
// Indentation between elements is not text.
if !(t.trim().is_empty() && t.contains('\n')) {
out.push_str(&t.replace("\r\n", "\r").replace('\n', "\r"));
craft_xml::Node::Text(t) => {
// Indentation between elements is not text.
if !(t.trim().is_empty() && t.contains('\n')) {
out.push_str(&t.replace("\r\n", "\r").replace('\n', "\r"));
}
}
_ => {}
}
}
}
fn rich_plain(node: &craft_xml::El) -> String {
let mut out = String::new();
walk_rich_plain(node, &mut out);
out.trim_end_matches('\r').to_string()
}
@@ -627,18 +648,17 @@ pub enum Outline {
}
/// Every comment element of an XFDF text, in order (none when it is no XFDF).
fn with_comment_nodes(text: &str, mut f: impl FnMut(roxmltree::Node<'_, '_>)) {
fn with_comment_nodes(text: &str, mut f: impl FnMut(&craft_xml::El)) {
if check_form_xml_depth(text).is_err() {
return;
}
let Ok(xml) = parse_form_xml(text) else { return };
let root = xml.root_element();
if root.tag_name().name() != "xfdf" {
let Ok(root) = parse_form_xml(text) else { return };
if root.local() != "xfdf" {
return;
}
let Some(annots) = root.children().find(|c| c.has_tag_name("annots")) else { return };
for node in annots.children().filter(|c| c.is_element()) {
if SUBTYPES.iter().any(|(_, t)| *t == node.tag_name().name().to_ascii_lowercase()) {
let Some(annots) = root.child(craft_xml::Local("annots")) else { return };
for node in annots.elements() {
if SUBTYPES.iter().any(|(_, t)| *t == node.local().to_ascii_lowercase()) {
f(node);
}
}
@@ -648,11 +668,7 @@ fn with_comment_nodes(text: &str, mut f: impl FnMut(roxmltree::Node<'_, '_>)) {
fn comment_nodes(text: &str) -> Vec<(String, Option<String>, Vec<f64>)> {
let mut out = Vec::new();
with_comment_nodes(text, |node| {
out.push((
node.tag_name().name().to_owned(),
node.attribute("inreplyto").map(str::to_owned),
node.attribute("rect").map(csv).unwrap_or_default(),
));
out.push((node.local().to_owned(), node.attr("inreplyto").map(str::to_owned), node.attr("rect").map(csv).unwrap_or_default()));
});
out
}
@@ -667,44 +683,41 @@ fn pairs(text: &str) -> Vec<[f64; 2]> {
pub fn comment_outlines(text: &str) -> Vec<Outline> {
let mut out = Vec::new();
with_comment_nodes(text, |node| {
if node.attribute("inreplyto").is_some() {
if node.attr("inreplyto").is_some() {
return;
}
let tag = node.tag_name().name().to_ascii_lowercase();
let color = node.attribute("color").and_then(parse_hex);
let tag = node.local().to_ascii_lowercase();
let color = node.attr("color").and_then(parse_hex);
let rect = node
.attribute("rect")
.attr("rect")
.map(csv)
.filter(|r| r.len() == 4 && r.iter().all(|v| v.is_finite()))
.map(|r| [r[0].min(r[2]), r[1].min(r[3]), r[0].max(r[2]), r[1].max(r[3])]);
match tag.as_str() {
"line" => {
if let (Some(a), Some(b)) = (node.attribute("start").map(csv), node.attribute("end").map(csv))
if let (Some(a), Some(b)) = (node.attr("start").map(csv), node.attr("end").map(csv))
&& let ([ax, ay], [bx, by]) = (a.as_slice(), b.as_slice())
{
out.push(Outline::Line { points: vec![[*ax, *ay], [*bx, *by]], color });
}
}
"polygon" | "polyline" => {
let points = node.children().find(|c| c.has_tag_name("vertices")).and_then(|v| v.text()).map(pairs).unwrap_or_default();
let points = node.child(craft_xml::Local("vertices")).map(|v| pairs(&v.text())).unwrap_or_default();
if points.len() >= 2 {
out.push(Outline::Poly { points, closed: tag == "polygon", color });
}
}
"ink" => {
let strokes: Vec<Vec<[f64; 2]>> = node
.children()
.find(|c| c.has_tag_name("inklist"))
.map(|list| {
list.children().filter(|g| g.has_tag_name("gesture")).filter_map(|g| g.text()).map(pairs).filter(|s| !s.is_empty()).collect()
})
.child(craft_xml::Local("inklist"))
.map(|list| list.children(craft_xml::Local("gesture")).map(|g| pairs(&g.text())).filter(|s| !s.is_empty()).collect())
.unwrap_or_default();
if !strokes.is_empty() {
out.push(Outline::Ink { strokes, color });
}
}
"highlight" | "underline" | "strikeout" | "squiggly" | "redact" => {
let coords = node.attribute("coords").map(csv).unwrap_or_default();
let coords = node.attr("coords").map(csv).unwrap_or_default();
let quads: Vec<[f64; 8]> = coords.as_chunks::<8>().0.to_vec();
if quads.is_empty() {
if let Some(rect) = rect {
@@ -717,7 +730,7 @@ pub fn comment_outlines(text: &str) -> Vec<Outline> {
// A Fill & Sign mark: its tick, cross, dot or line, as its appearance draws it.
"stamp"
if let Some(rect) = rect
&& let Some(mark) = node.attribute("icon").and_then(|i| pdfcraft_annot::FillMark::from_name(i.as_bytes())) =>
&& let Some(mark) = node.attr("icon").and_then(|i| pdfcraft_annot::FillMark::from_name(i.as_bytes())) =>
{
if mark == pdfcraft_annot::FillMark::Dot {
let r = (rect[2] - rect[0]).min(rect[3] - rect[1]) * 0.3;
@@ -733,7 +746,7 @@ pub fn comment_outlines(text: &str) -> Vec<Outline> {
out.push(Outline::Rect { rect, round: tag == "circle", color });
}
// A callout's leader, from the box to what it points at.
let callout = node.attribute("callout").map(csv).filter(|c| c.len() == 4 || c.len() == 6);
let callout = node.attr("callout").map(csv).filter(|c| c.len() == 4 || c.len() == 6);
if tag == "freetext"
&& let Some(cl) = &callout
{
@@ -743,14 +756,17 @@ pub fn comment_outlines(text: &str) -> Vec<Outline> {
// is its rect inset by the fringe).
if tag == "freetext"
&& let Some(rect) = rect
&& let Some(text) = node.children().find(|c| c.has_tag_name("contents")).and_then(|c| c.text()).filter(|t| !t.trim().is_empty())
&& let Some(text) = node.child(craft_xml::Local("contents")).map(|c| c.text().into_owned()).filter(|t| !t.trim().is_empty())
{
let mut da = Dict::new();
if let Some(s) = node.children().find(|c| c.has_tag_name("defaultappearance")).and_then(|c| c.text()) {
da.set(b"DA".to_vec(), PdfString::literal(s.as_bytes().to_vec()));
if let Some(el) = node.child(craft_xml::Local("defaultappearance")) {
let s = el.text();
if !s.is_empty() {
da.set(b"DA".to_vec(), PdfString::literal(s.as_bytes().to_vec()));
}
}
let (text_color, size) = pdfcraft_annot::appearance::parse_da(&da);
let fringe = node.attribute("fringe").map(csv).filter(|f| callout.is_some() && f.len() == 4 && f.iter().all(|v| v.is_finite()));
let fringe = node.attr("fringe").map(csv).filter(|f| callout.is_some() && f.len() == 4 && f.iter().all(|v| v.is_finite()));
let rect = match fringe.as_deref() {
Some([l, b, r, t]) => [rect[0] + l, rect[1] + b, rect[2] - r, rect[3] - t],
_ => rect,
@@ -863,12 +879,12 @@ pub(crate) fn apply_values(doc: &mut Document, values: &[(String, Vec<String>)],
}
/// A new annotation dictionary from an XFDF element.
fn annot_from_xml(node: roxmltree::Node, subtype: &str, page_ref: ObjRef) -> Option<Dict> {
fn annot_from_xml(node: &craft_xml::El, ns: &[(String, String)], subtype: &str, page_ref: ObjRef) -> Option<Dict> {
let mut d = Dict::new();
d.set(b"Type".to_vec(), Object::name("Annot"));
d.set(b"Subtype".to_vec(), Object::name(subtype));
d.set(b"P".to_vec(), Object::Ref(page_ref));
let rect = csv(node.attribute("rect")?);
let rect = csv(node.attr("rect")?);
if rect.len() != 4 {
return None;
}
@@ -882,86 +898,85 @@ fn annot_from_xml(node: roxmltree::Node, subtype: &str, page_ref: ObjRef) -> Opt
("state", b"State"),
("statemodel", b"StateModel"),
] {
if let Some(v) = node.attribute(attr) {
if let Some(v) = node.attr(attr) {
d.set(key.to_vec(), PdfString::text(v));
}
}
if let Some(v) = node.attribute("replyType") {
if let Some(v) = node.attr("replyType") {
d.set(b"RT".to_vec(), Object::name(if v.eq_ignore_ascii_case("group") { "Group" } else { "R" }));
}
if let Some(v) = node.attribute("icon") {
if let Some(v) = node.attr("icon") {
d.set(b"Name".to_vec(), Object::name(v));
}
let flags: i64 = node
.attribute("flags")
.attr("flags")
.unwrap_or("print")
.split(',')
.filter_map(|f| FLAGS.iter().find(|(_, s)| s.eq_ignore_ascii_case(f.trim())).map(|x| x.0))
.fold(0, |bits, flag| bits | flag);
d.set(b"F".to_vec(), Object::Int(flags));
if let Some(c) = node.attribute("color").and_then(parse_hex) {
if let Some(c) = node.attr("color").and_then(parse_hex) {
d.set(b"C".to_vec(), arr(&c));
}
if let Some(c) = node.attribute("interior-color").and_then(parse_hex) {
if let Some(c) = node.attr("interior-color").and_then(parse_hex) {
d.set(b"IC".to_vec(), arr(&c));
}
if let Some(o) = node.attribute("opacity").and_then(|o| o.parse::<f64>().ok()) {
if let Some(o) = node.attr("opacity").and_then(|o| o.parse::<f64>().ok()) {
d.set(b"CA".to_vec(), Object::Real(o.clamp(0.0, 1.0)));
}
read_format(node, &mut d);
if let Some(q) = node.attribute("coords") {
if let Some(q) = node.attr("coords") {
d.set(b"QuadPoints".to_vec(), arr(&csv(q)));
}
if let (Some(s), Some(e)) = (node.attribute("start"), node.attribute("end")) {
if let (Some(s), Some(e)) = (node.attr("start"), node.attr("end")) {
let (s, e) = (csv(s), csv(e));
if s.len() == 2 && e.len() == 2 {
d.set(b"L".to_vec(), arr(&[s[0], s[1], e[0], e[1]]));
}
}
if node.attribute("head").is_some() || node.attribute("tail").is_some() {
if node.attr("head").is_some() || node.attr("tail").is_some() {
d.set(
b"LE".to_vec(),
Object::Array(vec![Object::name(node.attribute("head").unwrap_or("None")), Object::name(node.attribute("tail").unwrap_or("None"))]),
Object::Array(vec![Object::name(node.attr("head").unwrap_or("None")), Object::name(node.attr("tail").unwrap_or("None"))]),
);
}
// The intent (/IT), spelt `intent` or `IT`.
let intent = node.attribute("intent").filter(|it| !it.is_empty()).or_else(|| node.attribute("IT").filter(|it| !it.is_empty()));
let intent = node.attr("intent").filter(|it| !it.is_empty()).or_else(|| node.attr("IT").filter(|it| !it.is_empty()));
if let Some(it) = intent {
d.set(b"IT".to_vec(), Object::name(it));
}
if let Some(cl) = node.attribute("callout").map(csv).filter(|c| c.len() == 4 || c.len() == 6) {
if let Some(cl) = node.attr("callout").map(csv).filter(|c| c.len() == 4 || c.len() == 6) {
d.set(b"CL".to_vec(), arr(&cl));
d.set(b"IT".to_vec(), Object::name(intent.unwrap_or("FreeTextCallout")));
// A callout's /LE is one name, not the head/tail pair of a line.
match node.attribute("head") {
match node.attr("head") {
Some(h) => d.set(b"LE".to_vec(), Object::name(h)),
None => {
d.remove(b"LE");
}
}
}
for child in node.children().filter(|c| c.is_element()) {
match child.tag_name().name() {
for child in node.elements() {
match child.local() {
"contents" => {
d.set(b"Contents".to_vec(), PdfString::text(&child.text().unwrap_or("").replace("\r\n", "\r").replace('\n', "\r")));
d.set(b"Contents".to_vec(), PdfString::text(&child.text().replace("\r\n", "\r").replace('\n', "\r")));
}
"defaultappearance" => {
d.set(b"DA".to_vec(), PdfString::literal(child.text().unwrap_or("").as_bytes().to_vec()));
d.set(b"DA".to_vec(), PdfString::literal(child.text().as_bytes().to_vec()));
}
"defaultstyle" => {
d.set(b"DS".to_vec(), PdfString::text(&child.children().filter(|c| c.is_text()).filter_map(|c| c.text()).collect::<String>()));
d.set(b"DS".to_vec(), PdfString::text(&el_text(child)));
}
"inklist" => {
let list: Vec<Object> = child
.children()
.filter(|g| g.has_tag_name("gesture"))
.map(|g| arr(&g.text().unwrap_or("").split(';').flat_map(|p| csv(p).into_iter()).collect::<Vec<_>>()))
.children(craft_xml::Local("gesture"))
.map(|g| arr(&g.text().split(';').flat_map(|p| csv(p).into_iter()).collect::<Vec<_>>()))
.collect();
d.set(b"InkList".to_vec(), Object::Array(list));
}
"vertices" => {
// Whole x,y pairs only; a stray trailing number is dropped.
let mut v = csv(child.text().unwrap_or(""));
let mut v = csv(&child.text());
v.truncate(v.len() - v.len() % 2);
if !v.is_empty() {
d.set(b"Vertices".to_vec(), arr(&v));
@@ -970,10 +985,14 @@ fn annot_from_xml(node: roxmltree::Node, subtype: &str, page_ref: ObjRef) -> Opt
_ => {}
}
}
if let Some(rich) = node.children().find(|c| c.has_tag_name("contents-richtext")) {
read_rich_text(rich, node.children().any(|c| c.has_tag_name("contents")), &mut d);
if let Some(rich) = node.child(craft_xml::Local("contents-richtext")) {
let mut rich_ns: Vec<(String, String)> = ns.to_vec();
for (p, u) in node.ns_decls().chain(rich.ns_decls()) {
rich_ns.push((p.to_string(), u.to_string()));
}
read_rich_text(rich, &rich_ns, node.child(craft_xml::Local("contents")).is_some(), &mut d);
}
read_own_keys(node, &mut d);
read_own_keys(node, ns, &mut d);
Some(d)
}
@@ -982,22 +1001,20 @@ fn annot_from_xml(node: roxmltree::Node, subtype: &str, page_ref: ObjRef) -> Opt
/// (`/Contents`) when the element comes alone; beside `<contents>` it is kept as `/RC` (rich text
/// PdfCraft exported as text because it was not well-formed XML). Markup without `<contents>`
/// gives `/Contents` its plain text too, so the comment's text shows where rich text is not read.
fn read_rich_text(rich: roxmltree::Node, has_contents: bool, d: &mut Dict) {
let elements: Vec<roxmltree::Node> = rich.children().filter(|c| c.is_element()).collect();
let (Some(first), Some(last)) = (elements.first(), elements.last()) else {
let text: String = rich.children().filter(|c| c.is_text()).filter_map(|c| c.text()).collect();
fn read_rich_text(rich: &craft_xml::El, ns: &[(String, String)], has_contents: bool, d: &mut Dict) {
let elements: Vec<&craft_xml::El> = rich.elements().collect();
if elements.is_empty() {
let text = el_text(rich);
if has_contents {
d.set(b"RC".to_vec(), PdfString::text(&text));
} else {
d.set(b"Contents".to_vec(), PdfString::text(&text.replace("\r\n", "\r").replace('\n', "\r")));
}
return;
};
let source = rich.document().input_text();
let Some(markup) = source.get(first.range().start..last.range().end) else { return };
}
let markup = match elements.as_slice() {
[_] => standalone(rich, markup),
_ => markup.to_string(),
[el] => standalone(el, ns),
_ => elements.iter().map(|e| e.to_xml()).collect(),
};
// Rich text is an XML document of its own; export left its declaration out.
d.set(b"RC".to_vec(), PdfString::text(&format!("<?xml version=\"1.0\"?>{markup}")));
@@ -1050,35 +1067,26 @@ fn start_tag_end(markup: &str) -> usize {
/// the default one (XHTML declared on `<contents-richtext>`, say; not XFDF's own, which a
/// standalone document has no use for) and the prefixes it uses. Unchanged when nothing is
/// inherited, or when the declarations would not make it parse.
fn standalone(rich: roxmltree::Node, markup: &str) -> String {
// The start tag's name ends at white space, `/` or `>`.
let Some(name_end) = markup.get(1..).and_then(|t| t.find(|c: char| c.is_whitespace() || c == '/' || c == '>')).map(|i| i + 1) else {
return markup.to_string();
};
let start_tag = markup.get(..start_tag_end(markup)).unwrap_or(markup);
let mut declared = String::new();
// The default namespace in scope where the element sits: its own when it has no prefix, and
// its unprefixed descendants' when it has one.
if let Some(uri) = rich.namespaces().find(|ns| ns.name().is_none()).map(|ns| ns.uri())
fn standalone(el: &craft_xml::El, ns: &[(String, String)]) -> String {
let mut clone = (*el).clone();
let markup = clone.to_xml();
if let Some((_, uri)) = ns.iter().rfind(|(p, _)| p.is_empty())
&& !uri.is_empty()
&& uri != XFDF_NS
&& !declares(start_tag, "xmlns")
&& !clone.has_attr("xmlns")
{
let _ = write!(declared, " xmlns=\"{}\"", esc(uri));
clone.set_attr("xmlns", uri);
}
for ns in rich.namespaces() {
let Some(prefix) = ns.name() else { continue };
if prefix == "xml" || !markup.contains(&format!("{prefix}:")) || declares(start_tag, &format!("xmlns:{prefix}")) {
for (prefix, uri) in ns {
if prefix.is_empty() || prefix == "xml" {
continue;
}
let _ = write!(declared, " xmlns:{prefix}=\"{}\"", esc(ns.uri()));
let attr_name = format!("xmlns:{prefix}");
if markup.contains(&format!("{prefix}:")) && !clone.has_attr(&attr_name) {
clone.set_attr(&attr_name, uri);
}
}
if declared.is_empty() {
return markup.to_string();
}
let (Some(head), Some(tail)) = (markup.get(..name_end), markup.get(name_end..)) else { return markup.to_string() };
let fixed = format!("{head}{declared}{tail}");
if parse_form_xml(&fixed).is_ok() { fixed } else { markup.to_string() }
clone.to_xml()
}
/// XFDF's own namespace (ISO 19444-1).
@@ -1283,9 +1291,8 @@ pub(crate) fn import_xfdf_with(
comments_only: bool,
) -> Result<Report, DataError> {
check_form_xml_depth(text)?;
let xml = parse_form_xml(text).map_err(|e| DataError::Malformed(e.to_string()))?;
let root = xml.root_element();
if root.tag_name().name() != "xfdf" {
let root = parse_form_xml(text).map_err(|e| DataError::Malformed(e.to_string()))?;
if root.local() != "xfdf" {
return Err(DataError::UnknownFormat);
}
let mut report = Report::default();
@@ -1302,30 +1309,33 @@ pub(crate) fn import_xfdf_with(
// so replies find their copied parent (a name two source comments share, as other tools
// write, still gives two copies).
let mut renamed: HashMap<String, String> = HashMap::new();
if let (Some(prefix), Some(annots)) = (fresh, root.children().find(|c| c.has_tag_name("annots"))) {
for (n, node) in annots.children().filter(|c| c.is_element()).enumerate() {
if let Some(nm) = node.attribute("name") {
let root_ns: Vec<(String, String)> = root.ns_decls().map(|(p, u)| (p.to_string(), u.to_string())).collect();
if let (Some(prefix), Some(annots)) = (fresh, root.child(craft_xml::Local("annots"))) {
for (n, node) in annots.elements().enumerate() {
if let Some(nm) = node.attr("name") {
renamed.entry(nm.to_string()).or_insert_with(|| format!("{prefix}-{}", n + 1));
}
}
}
if let Some(annots) = root.children().find(|c| c.has_tag_name("annots")) {
for (n, node) in annots.children().filter(|c| c.is_element()).enumerate() {
let tag = node.tag_name().name().to_ascii_lowercase();
if let Some(annots) = root.child(craft_xml::Local("annots")) {
let mut annot_ns = root_ns;
annot_ns.extend(annots.ns_decls().map(|(p, u)| (p.to_string(), u.to_string())));
for (n, node) in annots.elements().enumerate() {
let tag = node.local().to_ascii_lowercase();
let Some((subtype, _)) = SUBTYPES.iter().find(|(_, t)| *t == tag) else { continue };
let own_page = node.attribute("page").and_then(|p| p.parse::<usize>().ok());
let own_page = node.attr("page").and_then(|p| p.parse::<usize>().ok());
let Some(page) = target_page.or(own_page).and_then(|p| pages.get(p).copied()) else { continue };
let Some(mut d) = annot_from_xml(node, subtype, page) else { continue };
let Some(mut d) = annot_from_xml(node, &annot_ns, subtype, page) else { continue };
// The appearance the file carries (XFDF 3.0 "appearance"), when it can be read.
// A redaction mark is always drawn by PdfCraft (see `place`): its appearance is not read.
if let Some(ap) = node.children().find(|c| c.has_tag_name("appearance")).filter(|_| *subtype != "Redact") {
let text: String = ap.children().filter(|c| c.is_text()).filter_map(|c| c.text()).collect();
if let Some(ap) = node.child(craft_xml::Local("appearance")).filter(|_| *subtype != "Redact") {
let text = el_text(ap);
match super::appearance::decode(&text, &mut appearance_budget) {
Ok(tree) => {
let ap = super::appearance::materialize(doc, tree);
d.set(b"AP".to_vec(), ap);
}
Err(why) => report.skipped.push(format!("{}: {why}", node.attribute("name").unwrap_or(&tag))),
Err(why) => report.skipped.push(format!("{}: {why}", node.attr("name").unwrap_or(&tag))),
}
}
let assigned = fresh.map(|prefix| format!("{prefix}-{}", n + 1));
@@ -1339,10 +1349,10 @@ pub(crate) fn import_xfdf_with(
let r = place(doc, page, d)?;
// A later same-name record replaces this comment, including its reply relation.
later.remove(&r);
if let Some(name) = assigned.clone().or_else(|| node.attribute("name").map(str::to_owned)) {
if let Some(name) = assigned.clone().or_else(|| node.attr("name").map(str::to_owned)) {
by_name.insert(name, r);
}
if let Some(irt) = node.attribute("inreplyto") {
if let Some(irt) = node.attr("inreplyto") {
match (fresh, renamed.get(irt)) {
// The parent is among the copies: the reply follows it.
(Some(_), Some(parent)) => later.insert(r, parent.clone()),
@@ -1353,15 +1363,15 @@ pub(crate) fn import_xfdf_with(
};
}
// A pop-up window.
if let Some(pop) = node.children().find(|c| c.has_tag_name("popup"))
&& let Some(mut rect) = pop.attribute("rect").map(csv).filter(|r| r.len() == 4)
if let Some(pop) = node.child(craft_xml::Local("popup"))
&& let Some(mut rect) = pop.attr("rect").map(csv).filter(|r| r.len() == 4)
{
if let Some([dx, dy]) = offset {
for (i, v) in rect.iter_mut().enumerate() {
*v += if i % 2 == 0 { dx } else { dy };
}
}
add_popup(doc, page, r, &rect, pop.attribute("open") == Some("yes"))?;
add_popup(doc, page, r, &rect, pop.attr("open") == Some("yes"))?;
}
report.comments += 1;
}
@@ -1376,7 +1386,7 @@ pub(crate) fn import_xfdf_with(
return Ok(report);
}
// Fields.
if let Some(fields) = root.children().find(|c| c.has_tag_name("fields")) {
if let Some(fields) = root.child(craft_xml::Local("fields")) {
let mut values = Vec::new();
walk_xfdf(fields, "", &mut values, 0)?;
apply_values(doc, &values, &mut report);
+1 -1
View File
@@ -13,7 +13,7 @@ pdfcraft-oracle = { workspace = true }
pdfcraft-cos = { workspace = true }
anyhow = "1"
lopdf = "0.45"
png = "0.18"
craft-image-codecs = { workspace = true }
serde_json = "1"
serde = { version = "1", features = ["derive"] }
sha2 = "0.11"
+2 -7
View File
@@ -37,13 +37,8 @@ pub fn mandelbrot_png(size: u32) -> Result<Vec<u8>> {
}
}
let mut png_bytes = Vec::new();
let mut encoder = png::Encoder::new(&mut png_bytes, size, size);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
writer.write_image_data(&rgb)?;
writer.finish()?;
let img = craft_image_codecs::Image::from_u8(size, size, craft_image_codecs::ChannelLayout::Rgb, rgb)?;
let png_bytes = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())?;
Ok(png_bytes)
}