Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(print): page content and label forms share one decode budget per page

aiongg8c8f56a2e29d
diff --git a/crates/print/src/lib.rs b/crates/print/src/lib.rs
index e935cb938..05b0770ba 100644
--- a/crates/print/src/lib.rs
+++ b/crates/print/src/lib.rs
@@ -18,7 +18,7 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
use pdfcraft_content::Matrix;
-use pdfcraft_cos::{Dict, Document, ObjRef, Object, SaveOptions, Stream, write_full};
+use pdfcraft_cos::{DecodeBudget, Dict, Document, ObjRef, Object, SaveOptions, Stream, write_full};
pub mod range;
pub mod spool;
@@ -331,11 +331,14 @@ pub fn text_reads_up(doc: &Document, index: usize) -> bool {
let to_display = Matrix(page.view_matrix(doc)).invert().unwrap_or_default();
let res = page.dict.get(b"Resources").and_then(|r| doc.resolve(r).as_dict().cloned()).unwrap_or_default();
let mut score = 0i64;
- text_direction(doc, &decoded_contents(doc, &page.dict), &res, to_display, 0, &mut score);
+ let mut budget = DecodeBudget::page();
+ // Content past the budget is left out: the answer is a guess from the text read so far.
+ let data = decoded_contents(doc, &page.dict, &mut budget).unwrap_or_default();
+ text_direction(doc, &data, &res, to_display, 0, &mut score, &mut budget);
score > 0
}
-fn text_direction(doc: &Document, data: &[u8], res: &Dict, base: Matrix, depth: usize, score: &mut i64) {
+fn text_direction(doc: &Document, data: &[u8], res: &Dict, base: Matrix, depth: usize, score: &mut i64, budget: &mut DecodeBudget) {
let (mut ctm, mut tm, mut stack) = (base, Matrix::IDENTITY, Vec::new());
for op in pdfcraft_content::parse(data).ops {
match op.op.as_slice() {
@@ -358,8 +361,11 @@ fn text_direction(doc: &Document, data: &[u8], res: &Dict, base: Matrix, depth:
}
let m = form.dict.get(b"Matrix").and_then(|m| doc.resolve(m).as_array().and_then(|a| Matrix::from_operands(a))).unwrap_or_default();
let inner = form.dict.get(b"Resources").and_then(|r| doc.resolve(r).as_dict().cloned()).unwrap_or_else(|| res.clone());
- if let Ok(body) = form.decoded() {
- text_direction(doc, &body, &inner, m.then(&ctm), depth + 1, score);
+ if !budget.enter_form() {
+ return;
+ }
+ if let Ok(body) = budget.decode(form) {
+ text_direction(doc, &body, &inner, m.then(&ctm), depth + 1, score, budget);
}
}
_ => {}
@@ -676,24 +682,22 @@ fn n(v: f64) -> String {
if s == "-0" { "0".into() } else { s.into() }
}
-fn decoded_contents(doc: &Document, page: &Dict) -> Vec<u8> {
- let list: Vec<Object> = match page.get(b"Contents") {
- None => Vec::new(),
- Some(c) => match &*doc.resolve(c) {
- Object::Array(a) => a.clone(),
- _ => vec![c.clone()],
- },
- };
+/// The page's content streams decoded against `budget` and joined; streams that don't decode
+/// are left out. `None` when the budget ran out, so part of the content is missing.
+fn decoded_contents(doc: &Document, page: &Dict, budget: &mut DecodeBudget) -> Option<Vec<u8>> {
+ let Some(contents) = page.get(b"Contents") else { return Some(Vec::new()) };
let mut out = Vec::new();
- for o in list {
- if let Object::Stream(s) = &*doc.resolve(&o)
- && let Ok(d) = s.decoded()
- {
- out.extend_from_slice(&d);
- out.push(b'\n');
+ for data in budget.decode_contents(doc, contents) {
+ match data {
+ Ok(data) => {
+ out.extend_from_slice(&data);
+ out.push(b'\n');
+ }
+ Err(_) if budget.remaining() == 0 => return None,
+ Err(_) => {}
}
}
- out
+ Some(out)
}
/// Does annotation `d` print under `content`?
@@ -760,7 +764,12 @@ fn page_form(
let page = pdfcraft_model::pages(doc).swap_remove(index);
let crop = page.crop(doc);
let mut res = page.dict.get(b"Resources").and_then(|r| doc.resolve(r).as_dict().cloned()).unwrap_or_default();
- let mut body = if content == Content::FormFieldsOnly { Vec::new() } else { decoded_contents(doc, &page.dict) };
+ let mut body = if content == Content::FormFieldsOnly {
+ Vec::new()
+ } else {
+ decoded_contents(doc, &page.dict, &mut DecodeBudget::page())
+ .ok_or_else(|| PrintError::Invalid(format!("page {} has more content than can be printed", index + 1)))?
+ };
// Wrap the page content so a state it leaves behind can't affect the annotations.
if !body.is_empty() {
let mut wrapped = b"q\n".to_vec();
diff --git a/crates/print/src/tests.rs b/crates/print/src/tests.rs
index 99634f1aa..fcf1a6268 100644
--- a/crates/print/src/tests.rs
+++ b/crates/print/src/tests.rs
@@ -877,3 +877,42 @@ fn driver_options_take_the_ppds_own_names_in_the_ui_language() {
}
mod appearances;
+
+#[test]
+fn repeated_content_references_share_one_decode_budget() {
+ let mut doc = Document::new_empty();
+ let stream = doc.add(Stream::flate(Dict::new(), &vec![b' '; 1 << 20]));
+ let page: Dict = [(b"Contents".to_vec(), Object::Array(vec![Object::Ref(stream); 300]))].into_iter().collect();
+ let data = decoded_contents(&doc, &page, &mut DecodeBudget::page());
+ assert!(data.is_none_or(|d| d.len() <= pdfcraft_cos::MAX_PAGE_DECODED + 300));
+}
+
+/// 20 draws of a form that draws the next form 20 times, four levels down: 168,420 form
+/// entries from a few hundred bytes.
+fn form_fanout(doc: &mut Document) -> Dict {
+ let draws = "/X Do ".repeat(20);
+ let mut form = |content: &str, inner: Option<ObjRef>| {
+ let mut d: Dict = [(b"Subtype".to_vec(), Object::Name(b"Form".to_vec()))].into_iter().collect();
+ if let Some(inner) = inner {
+ let xobjects: Dict = [(b"X".to_vec(), Object::Ref(inner))].into_iter().collect();
+ let res: Dict = [(b"XObject".to_vec(), Object::Dict(xobjects))].into_iter().collect();
+ d.set(b"Resources".to_vec(), Object::Dict(res));
+ }
+ doc.add(Stream::from_raw(d, content.as_bytes().to_vec()))
+ };
+ let mut inner = form("BT 0 1 -1 0 0 0 Tm (a) Tj ET", None);
+ for _ in 0..3 {
+ inner = form(&draws, Some(inner));
+ }
+ let xobjects: Dict = [(b"X".to_vec(), Object::Ref(inner))].into_iter().collect();
+ [(b"XObject".to_vec(), Object::Dict(xobjects))].into_iter().collect()
+}
+
+#[test]
+fn text_direction_stops_at_the_form_budget() {
+ let mut doc = Document::new_empty();
+ let res = form_fanout(&mut doc);
+ let mut score = 0;
+ text_direction(&doc, "/X Do ".repeat(20).as_bytes(), &res, Matrix::IDENTITY, 0, &mut score, &mut DecodeBudget::page());
+ assert!(score > 0 && score <= pdfcraft_cos::MAX_PAGE_FORMS as i64, "score {score}");
+}