Skip to content

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

fix(engine): compare only documents that allow copying their text

aiongg30300ae71be5
diff --git a/crates/engine/src/compare.rs b/crates/engine/src/compare.rs
index 50508e20e..c23db91f1 100644
--- a/crates/engine/src/compare.rs
+++ b/crates/engine/src/compare.rs
@@ -46,10 +46,13 @@ impl crate::Document {
}
impl Session {
- /// Compare files: the text differences from document `old` to document `new`.
+ /// Compare files: the text differences from document `old` to document `new`. The result
+ /// carries both documents' text, so both must allow copying it.
pub fn compare(&self, old: DocId, new: DocId) -> Result<Comparison, EditError> {
let a = self.get(old).ok_or(EditError::NoDocument)?;
let b = self.get(new).ok_or(EditError::NoDocument)?;
+ a.check_copying()?;
+ b.check_copying()?;
Ok(pdfcraft_compare::compare(&a.words(), &b.words()))
}
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 0df176226..f93e76526 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -1311,6 +1311,12 @@ fn copy_restricted_documents_refuse_extraction_unless_opened_by_the_owner() {
let oid = s.open("r.pdf", None, bytes, Some("owner")).unwrap();
assert!(s.get(oid).unwrap().allows_copying());
assert!(extracting.iter().all(|c| enabled(&s, oid, c)));
+ // Comparing reads both documents' text out, into the result, the report and the comments.
+ let refused = || Err(EditError::NotPermitted("copying text and images"));
+ assert_eq!((s.compare(id, oid).map(|_| ()), s.compare(oid, id).map(|_| ())), (refused(), refused()));
+ assert_eq!(s.compare_report(oid, id).map(|_| ()), refused());
+ assert!(s.compare(oid, oid).is_ok());
+ assert_eq!(s.mark_differences(id, oid).map(|_| ()), refused());
// Unencrypted documents allow everything.
let (s, plain) = session_with(1);
assert!(s.get(plain).unwrap().check_copying().is_ok() && extracting.iter().all(|c| enabled(&s, plain, c)));