PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M10: review of #1609: judge a page that is no scan again once the document changes
Philip703db23de301
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 3ad431797..6c6b9fb34 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -476,6 +476,9 @@ pub struct DocView {shown: bool,/// Pages that Edit already tried to turn into editable text (once each, so Undo sticks).pub(crate) auto_ocr_tried: std::collections::HashSet<usize>,+ /// Pages Edit looked at and found not to be a scan, with the document's edit generation then:+ /// they are looked at again only after the document changes (an image added, say).+ pub(crate) auto_ocr_skipped: std::collections::HashMap<usize, u64>,/// Turns wheel input into page turns in single-page view.wheel: crate::wheel_pager::WheelPager,/// Until this time, apply precise (touchpad) wheel input 1:1 and suppress egui's eased copy.@@ -760,6 +763,7 @@ impl DocView {zoom_anchor: None,shown: false,auto_ocr_tried: Default::default(),+ auto_ocr_skipped: Default::default(),wheel: Default::default(),precise_scroll_until: 0.0,wheel_boost_x: Default::default(),diff --git a/crates/ui-egui/src/ocr_ui.rs b/crates/ui-egui/src/ocr_ui.rsindex 64d44e714..0dcd5c9a6 100644--- a/crates/ui-egui/src/ocr_ui.rs+++ b/crates/ui-egui/src/ocr_ui.rs@@ -174,8 +174,8 @@ impl PdfCraftApp {/// while Edit is on (its tab, or the Edit text & images or Add text tool). The page must be a/// picture (an image covering at least half of it) with no text of its own: comments and text/// boxes over it do not count as text, but real page text does, and such a page (a flyer with a- /// background picture) is left alone. It is one undoable step, "Recognize text", and is not- /// run again for that page after Undo.+ /// background picture) is left alone, and is judged again only when the document changes. It+ /// is one undoable step, "Recognize text", and is not run again for that page after Undo.pub(crate) fn auto_ocr_scanned_page(&mut self) {// Edit is on when its panel is open (the Edit tab) or Edit text & images or Add text is picked,// the same test the page view uses for editing content.@@ -193,16 +193,23 @@ impl PdfCraftApp {if doc.read_only_reason.is_some() || !doc.allows_modification() {return;}+ // A page found not to be a scan is judged again only once the document has changed, not+ // every frame (listing its images parses the page).+ let generation = doc.edit_generation();+ if self.views[vi].auto_ocr_skipped.get(&page) == Some(&generation) {+ return;+ }let Some(info) = doc.info.pages.get(page) else { return };let page_area = f64::from(info.width) * f64::from(info.height);let covered = doc.page_images(page).iter().any(|i| {let [x0, y0, x1, y1] = i.visible_rect.unwrap_or(i.rect);page_area > 0.0 && ((x1 - x0) * (y1 - y0)).abs() >= 0.5 * page_area});- self.views[vi].auto_ocr_tried.insert(page);if !covered || !pdfcraft_engine::ocr::available() {+ self.views[vi].auto_ocr_skipped.insert(page, generation);return;}+ self.views[vi].auto_ocr_tried.insert(page);// Only a page with no text of its own is read. Comments and text boxes over the picture do// not count as its text (a scan with a few comments is still a scan, as in Acrobat), but// a page with real text and a background picture is left alone.diff --git a/crates/ui-egui/tests/ocr.rs b/crates/ui-egui/tests/ocr.rsindex 906551554..531209634 100644--- a/crates/ui-egui/tests/ocr.rs+++ b/crates/ui-egui/tests/ocr.rs@@ -275,6 +275,37 @@ fn editing_a_page_with_text_over_a_background_picture_changes_nothing() {assert!(!h.state().session.get(id).unwrap().dirty, "the file is not marked modified");}+/// A page that is not a scan when Edit looks at it is looked at again after the document changes:+/// adding a picture of text that covers it turns it into a scan, and Edit then reads it (and does+/// so once).+#[test]+fn a_page_that_becomes_a_scan_while_edit_is_on_is_recognized() {+ if !pdfcraft_engine::ocr::available() {+ eprintln!("skipped: OCR models not installed");+ return;+ }+ let png = {+ let mut s = Session::new();+ let text = s.create_from_text("t", "Recognize these scanned words").unwrap();+ let id = s.open("text.pdf", None, text, None).unwrap();+ export::Exporter::new(s.get(id).unwrap()).png(0, 150.0).unwrap()+ };+ let blank = Session::new().create_blank(595.0, 842.0, 1).unwrap().to_vec();+ let mut h = edit_harness(blank);+ let id = h.state().active_ids().unwrap().1;+ assert!(h.state_mut().execute("edit.edit_text"));+ h.run_steps(4);+ assert_eq!(h.state().session.get(id).unwrap().can_undo(), None, "a blank page is no scan: nothing happened");+ // The same page, now with a picture of text over all of it.+ h.state_mut()+ .session+ .apply(id, Edit::AddImage { page: 0, rect: Some([0.0, 0.0, 595.0, 842.0]), name: "scan.png".into(), bytes: std::sync::Arc::new(png) })+ .unwrap();+ h.run_steps(6);+ assert!(h.state().ocr_run.is_none(), "finished");+ assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Recognize text"));+}+#[test]fn editing_a_page_that_has_text_does_not_recognize_anything() {let mut h = edit_harness(text_page());