diff --git a/crates/pdf/README.md b/crates/pdf/README.md index 84d15fa7ae..8d561ecfd8 100644 --- a/crates/pdf/README.md +++ b/crates/pdf/README.md @@ -165,14 +165,17 @@ interpretation needs. reaches and, by default, packs objects into object streams. Both return the bytes. - `Document` is cheap to clone: the bytes, index and caches are shared, so a clone is a snapshot for undo or a background job. +- `Document::decoding_view` gives a stream to decode with its `/DecodeParms` made direct, + sharing its data and copying no more of its dictionary than `/Filter` and the parameters + (made public for storytold/pdfcraft#6821). - Images: `ImageBudget::decode` gives an image's samples after its filters, CCITT fax and JBIG2 included, all images of one operation spending from one allowance. DCT and JPX are not decoded here. - Not here: streaming files larger than memory, linearized output. The code comes from PdfCraft's `pdfcraft-cos` crate (revision -`1d2af9907ba772b368f6b54811359805fa5428e9`), unchanged apart from module paths and doc -comments. Its test helpers come along: `src/cos/testkit.rs` (synthetic files, from +`1d2af9907ba772b368f6b54811359805fa5428e9`), unchanged apart from module paths, doc comments +and the changes noted above. Its test helpers come along: `src/cos/testkit.rs` (synthetic files, from `pdfcraft-testkit`) and `tests/cos/oracle.rs` (qpdf runs, from `pdfcraft-oracle`; a missing `qpdf` skips those tests, or fails them when `CRAFT_PDF_ORACLES_REQUIRED` is set). Tests: `cargo test -p craft-pdf --no-default-features --features cos`. Other crates' tests use the diff --git a/crates/pdf/src/cos/document/objects.rs b/crates/pdf/src/cos/document/objects.rs index a6f077d358..c51b7edf83 100644 --- a/crates/pdf/src/cos/document/objects.rs +++ b/crates/pdf/src/cos/document/objects.rs @@ -389,11 +389,12 @@ impl Document { Cow::Owned(s) } - /// [`Document::with_direct_parms`] for decoding alone: where that copies `s`, this makes a - /// stream of `s`'s data, shared, and a dictionary of its `/Filter` and the parameters made - /// direct, all its filters read. A stream named again and again, as a page's `/Contents` may - /// list one, isn't copied whole each time. - pub(crate) fn decoding_view<'s>(&self, s: &'s Stream) -> Cow<'s, Stream> { + /// [`Document::with_direct_parms`] for decoding alone: where that copies `s`'s whole + /// dictionary, this gives `s`'s data, shared, under a dictionary of only its `/Filter` and the + /// parameters made direct, all its filters read; `s` itself where that keeps `s`. A stream + /// named again and again, as a page's `/Contents` may list one, doesn't have its dictionary + /// copied each time. + pub fn decoding_view<'s>(&self, s: &'s Stream) -> Cow<'s, Stream> { let Some(made) = self.direct_parms(s) else { return Cow::Borrowed(s) }; let mut dict = Dict::new(); if let Some(f) = s.dict.get(b"Filter") { diff --git a/crates/pdf/tests/cos/shared_streams.rs b/crates/pdf/tests/cos/shared_streams.rs index 90c2ad29b9..6df15c88dd 100644 --- a/crates/pdf/tests/cos/shared_streams.rs +++ b/crates/pdf/tests/cos/shared_streams.rs @@ -5,6 +5,8 @@ use std::sync::Arc; use craft_pdf::cos::{Document, ObjRef, Object, SaveOptions, write_full, write_incremental}; +use crate::testkit::Pdf; + const CONTENT: &[u8] = b"0 0 1 rg 10 10 30 20 re f"; fn fixture() -> Vec { @@ -76,3 +78,29 @@ fn streams_moved_to_another_document_get_their_own_bytes() { assert_eq!(Arc::strong_count(&file), 1); assert_eq!(stream_bytes(&target, r).0, CONTENT); } + +/// A stream whose `/DecodeParms` is given by reference decodes through `decoding_view` as through +/// `with_direct_parms`, which `decoded` alone can't (it reads only parameters given directly); the +/// view shares the stream's data, as a copy would too, and carries no more of its dictionary than +/// `/Filter` and `/DecodeParms`. +#[test] +fn a_decoding_view_reads_parameters_by_reference_and_shares_the_data() { + // Two rows of two bytes, each behind a PNG predictor tag of 0 (None). + let packed = craft_pdf::filters::encode_flate(&[0, 1, 2, 0, 3, 4]); + let file = Pdf::new() + .version("1.4") + .obj("<< /Type /Catalog /Pages 2 0 R >>") + .obj("<< /Type /Pages /Kids [] /Count 0 >>") + .obj("<< /Predictor 12 /Columns 2 >>") + .stream("/Filter /FlateDecode /DecodeParms 3 0 R", packed) + .build(); + let doc = Document::open(Arc::new(file)).unwrap(); + let held = doc.get(ObjRef { num: 4, generation: 0 }); + let Object::Stream(s) = &*held else { panic!("{held:?}") }; + let view = doc.decoding_view(s); + assert_eq!(view.decoded().unwrap(), [1, 2, 3, 4]); + assert_eq!(view.decoded().unwrap(), doc.with_direct_parms(s).decoded().unwrap()); + assert_eq!(s.decoded().unwrap(), [0, 1, 2, 0, 3, 4], "decoded alone takes no parameters by reference"); + assert_eq!(view.raw.as_ptr(), s.raw.as_ptr(), "the view shares the stream's data"); + assert_eq!(view.dict.len(), 2, "only /Filter and /DecodeParms, not /Length: {:?}", view.dict); +}