Skip to content

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

M10: recognize a scanned page when the Edit tab is opened, not only the Edit text tool

Philipf9c1ddb5f9bf
diff --git a/crates/ui-egui/src/ocr_ui.rs b/crates/ui-egui/src/ocr_ui.rs
index 68f525acc..27778957c 100644
--- a/crates/ui-egui/src/ocr_ui.rs
+++ b/crates/ui-egui/src/ocr_ui.rs
@@ -171,11 +171,15 @@ impl PdfCraftApp {
/// Edit on a scanned page: like Acrobat's "Converting scanned page to Editable Text and
/// Images", the page's words are recognized first, so that they open for editing (Edit text &
/// images groups the recognised words into phrases). Runs once per page, for the page in view
- /// while the Edit tool is on, and only for a page that is a picture (an image covering at least
+ /// while Edit is on (its tab, or the Edit text & images or Add text tool), and only for a page that is a picture (an image covering at least
/// half of it, no text of its own: a page with text is skipped by the run). It is one undoable
/// step, "Recognize text", and Undo is not offered again for that page.
pub(crate) fn auto_ocr_scanned_page(&mut self) {
- if self.quick_tool != crate::QuickTool::EditText || self.ocr_run.is_some() || self.ocr_batch.is_some() {
+ // 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.
+ let editing = (self.left_open && self.left == crate::LeftPanel::Tool("edit"))
+ || matches!(self.quick_tool, crate::QuickTool::EditText | crate::QuickTool::AddText);
+ if !editing || self.ocr_run.is_some() || self.ocr_batch.is_some() {
return;
}
let Some((vi, id)) = self.active_ids() else { return };
diff --git a/crates/ui-egui/tests/ocr.rs b/crates/ui-egui/tests/ocr.rs
index ee1256a49..415b3eea2 100644
--- a/crates/ui-egui/tests/ocr.rs
+++ b/crates/ui-egui/tests/ocr.rs
@@ -205,6 +205,23 @@ fn editing_a_scanned_page_recognizes_its_text_first_and_only_once() {
assert_eq!(h.state().session.get(id).unwrap().can_undo(), None, "recognized only once per page");
}
+/// The Edit tab (its tool panel) turns Edit on without picking Edit text & images, and Acrobat's
+/// conversion happens there too.
+#[test]
+fn the_edit_panel_recognizes_a_scanned_page_too() {
+ if !pdfcraft_engine::ocr::available() {
+ eprintln!("skipped: OCR models not installed");
+ return;
+ }
+ let mut h = edit_harness(scan());
+ let id = h.state().active_ids().unwrap().1;
+ assert_eq!(h.state().session.get(id).unwrap().can_undo(), None);
+ h.state_mut().set_option("tool", "edit").unwrap();
+ h.run_steps(4);
+ 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());