Skip to content

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

fix(automation): an emptied paragraph does not pass its number to an overlapping neighbour (#2516)

hcchung1570e6c056451
diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rs
index 067c9dac3..2fc017088 100644
--- a/crates/automation/src/lib.rs
+++ b/crates/automation/src/lib.rs
@@ -681,9 +681,13 @@ impl Automation {
// Where the paragraph is drawn after the edit: moved by dx, dy (up is +dy).
let [dx, dy] = style.offset.unwrap_or([0.0, 0.0]);
let moved = [block.rect[0] + dx, block.rect[1] + dy, block.rect[2] + dx, block.rect[3] + dy];
+ let wanted: String = text.split_whitespace().collect();
let mut out = self.apply(&a, Edit::EditTextBlock { page, block: index, text, style })?;
let after = self.doc(&a)?.text_blocks(page);
- let edited = block_overlapping(&after, moved);
+ // Only a paragraph that now shows the requested text is the edited one. A neighbour
+ // that overlaps the old box (overlaid text, a text layer) must not take its number.
+ let edited = block_overlapping(&after, moved)
+ .filter(|&i| !wanted.is_empty() && after.get(i).is_some_and(|b| b.text.split_whitespace().collect::<String>().contains(&wanted)));
if let Some(b) = edited.and_then(|i| after.get(i)) {
out["paragraph"] = json!({ "text": b.text, "lines": b.lines.len(), "font": b.base_font });
}
diff --git a/crates/automation/tests/automation.rs b/crates/automation/tests/automation.rs
index ad9c84c14..de470b968 100644
--- a/crates/automation/tests/automation.rs
+++ b/crates/automation/tests/automation.rs
@@ -6871,6 +6871,39 @@ fn three_paragraph_pdf() -> Vec<u8> {
write_full(&doc, &SaveOptions::default()).unwrap()
}
+/// A one-page PDF whose second paragraph is drawn over the first one's box, as an OCR text layer
+/// or a stamp would be.
+fn overlaid_paragraph_pdf() -> Vec<u8> {
+ use pdfcraft_cos::{Dict, Document, ObjRef, Object, SaveOptions, Stream, write_full};
+ use std::sync::Arc;
+
+ let content = "BT /F1 12 Tf 14.4 TL 1 0 0 1 10 250 Tm (Alpha paragraph, first row.) Tj T* (Alpha paragraph, second row.) Tj ET \
+ BT /F1 12 Tf 14.4 TL 1 0 0 1 10 243 Tm (Zulu overlay.) Tj ET";
+ let mut doc = Document::open(Arc::new(fixture(1))).unwrap();
+ doc.set(ObjRef::new(5, 0), Object::Stream(Stream::flate(Dict::new(), content.as_bytes())));
+ write_full(&doc, &SaveOptions::default()).unwrap()
+}
+
+/// Emptying a paragraph that another paragraph overlaps must not hand the emptied paragraph's
+/// number to the neighbour that took over its box: the next edit by that number would rewrite it.
+#[test]
+fn an_emptied_paragraph_does_not_pass_its_number_to_an_overlapping_neighbour() {
+ let dir = workdir("text-edit-overlap-number");
+ std::fs::write(dir.join("p.pdf"), overlaid_paragraph_pdf()).unwrap();
+ let mut a = auto(&dir);
+ let doc = ok(&mut a, "doc_open", json!({"path":"p.pdf"}))["doc"].as_u64().unwrap();
+ let listed = ok(&mut a, "text_paragraphs", json!({"doc":doc,"page":1}));
+ assert_eq!(listed["count"], 2, "{listed}");
+ let k = listed["paragraphs"].as_array().unwrap().iter().position(|p| p["text"].as_str().unwrap().starts_with("Alpha")).unwrap() + 1;
+
+ let first = ok(&mut a, "text_edit", json!({"doc":doc,"page":1,"paragraph":k,"text":""}));
+ assert_eq!(first["stale_paragraphs"], json!([k]), "the emptied paragraph's number is gone: {first}");
+ let again = a.call("text_edit", &json!({"doc":doc,"page":1,"paragraph":k,"text":"Rewritten."}));
+ assert!(matches!(&again, Err(ToolError::InvalidArgs(m)) if m.contains("text_paragraphs")), "{again:?}");
+ assert!(page_text(&mut a, doc).join(" ").contains("Zulu overlay."), "the neighbour is untouched");
+ std::fs::remove_dir_all(dir).unwrap();
+}
+
/// #2516: a paragraph number names the paragraph `text_paragraphs` listed. When an edit regroups
/// the page so that paragraph no longer exists on its own, the next edit by that number is
/// refused. It used to rewrite whichever paragraph had taken the number, and that text was gone.