Skip to content

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

fix(print): read the text after a form the budget refuses

aiongg2430d53f9e19
diff --git a/crates/print/src/lib.rs b/crates/print/src/lib.rs
index a6337026b..dc5ac307c 100644
--- a/crates/print/src/lib.rs
+++ b/crates/print/src/lib.rs
@@ -362,7 +362,7 @@ 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 !budget.enter_form() {
- return;
+ continue;
}
if let Ok(body) = budget.decode(form) {
text_direction(doc, &body, &inner, m.then(&ctm), depth + 1, score, budget);
diff --git a/crates/print/src/tests.rs b/crates/print/src/tests.rs
index fcf1a6268..4cbed38ce 100644
--- a/crates/print/src/tests.rs
+++ b/crates/print/src/tests.rs
@@ -916,3 +916,16 @@ fn text_direction_stops_at_the_form_budget() {
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}");
}
+
+/// A form refused by the spent budget is left out; the text drawn after it still counts.
+#[test]
+fn text_direction_reads_on_past_a_refused_form() {
+ let mut doc = Document::new_empty();
+ let form = doc.add(Stream::from_raw([(b"Subtype".to_vec(), Object::Name(b"Form".to_vec()))].into_iter().collect(), Vec::new()));
+ let xobjects: Dict = [(b"X".to_vec(), Object::Ref(form))].into_iter().collect();
+ let res: Dict = [(b"XObject".to_vec(), Object::Dict(xobjects))].into_iter().collect();
+ let mut score = 0;
+ let content = b"/X Do BT 0 1 -1 0 0 0 Tm (a) Tj ET";
+ text_direction(&doc, content, &res, Matrix::IDENTITY, 0, &mut score, &mut DecodeBudget::new(1 << 20, 0));
+ assert_eq!(score, 1);
+}