pdf-print-ocr-w9: net change of 3 commits

- pdf-print-ocr-w9: written from its sheet
- pdf-print-ocr-w9: repair 1
- pdf-print-ocr-w9: review fixes (review-1)
This commit is contained in:
Dylan M. Taylor
2026-11-18 16:15:24 -05:00
parent 338902dd80
commit 7c1ac076c2
3 changed files with 166 additions and 162 deletions
+137 -142
View File
@@ -1,106 +1,78 @@
//! Scan & OCR ▸ Enhance scanned file: the dialog (pages, background removal level, edge
//! shadows, despeckle) and the background run with its progress card. The scans are cleaned
//! on a worker thread ([`pdfcraft_engine::scan_clean`]); the result is applied as one undoable
//! on the host's job queue ([`pdfcraft_engine::scan_clean`]); the result is applied as one undoable
//! step when it is done, unless the document changed meanwhile.
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use egui::{Align, Layout};
use craft::pages::PageRange;
use craft::pages::ui::{PageRangeChoices, PageRangeOptions};
use craft::ui::dialog::{DialogResult, dialog_id};
use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind};
use craft::ui::value_drag::SliderScale;
use pdfcraft_engine::scan_clean::{ScanCleanReport, ScanCleanSettings, ScanCleaned};
use pdfcraft_engine::{DocId, EditError};
use crate::ocr_ui::OcrPages;
use crate::theme::{self, Tokens};
use crate::{PdfCraftApp, widgets};
use crate::notices::{self, ProgressKey};
use super::job_count::JobCount;
use crate::PdfCraftApp;
use crate::notices::ProgressNotice;
/// The key the dialog and its result share.
const DIALOG_KEY: &str = "enhance-scan";
/// The dialog's choices (kept between runs).
#[derive(Clone, Debug, PartialEq)]
pub struct ScanCleanDraft {
pub pages: OcrPages,
pub from: usize,
pub to: usize,
pub range: PageRange,
pub settings: ScanCleanSettings,
}
impl Default for ScanCleanDraft {
fn default() -> Self {
ScanCleanDraft { pages: OcrPages::All, from: 1, to: 1, settings: ScanCleanSettings::default() }
ScanCleanDraft { range: PageRange::default(), settings: ScanCleanSettings::default() }
}
}
/// Progress of a run: images done, total, the result once finished, and a cancel request.
#[derive(Default)]
pub struct ScanCleanProgress {
pub done: usize,
pub total: usize,
pub result: Option<Result<ScanCleaned, EditError>>,
pub cancel: bool,
}
/// A running clean-up: its document and the count the job and the progress card share.
pub struct ScanCleanRun {
pub doc: DocId,
pub progress: Arc<Mutex<ScanCleanProgress>>,
pub progress: Arc<JobCount>,
}
pub(crate) fn body(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tokens) -> (bool, bool) {
let pages = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len().max(1));
let d = &mut app.scan_clean_draft;
d.to = d.to.clamp(1, pages);
d.from = d.from.clamp(1, d.to);
ui.label(egui::RichText::new(tl!("Enhance Scanned File")).font(theme::semibold(18.0)));
ui.add_space(4.0);
ui.label(
egui::RichText::new(tl!("Cleans up the scanned pictures in place: recognized text, comments and links on the pages stay as they are."))
.small()
.color(t.text_muted),
);
ui.add_space(8.0);
let group = |ui: &mut egui::Ui, title: &str, body: &mut dyn FnMut(&mut egui::Ui)| {
ui.label(egui::RichText::new(tl!(title)).font(theme::semibold(13.0)));
egui::Frame::new().fill(t.hover).corner_radius(egui::CornerRadius::same(6)).inner_margin(egui::Margin::same(10)).show(ui, |ui| {
ui.set_width(ui.available_width());
body(ui);
});
ui.add_space(8.0);
};
group(ui, "Pages", &mut |ui| {
widgets::radio_value(ui, &mut d.pages, OcrPages::All, tl!("All pages"));
widgets::radio_value(ui, &mut d.pages, OcrPages::Current, tl!("Current page"));
ui.horizontal(|ui| {
let from = widgets::radio_value(ui, &mut d.pages, OcrPages::Range, tl!("From"));
let on = d.pages == OcrPages::Range;
ui.add_enabled(on, egui::DragValue::new(&mut d.from).range(1..=pages)).labelled_by(from.id);
let to = ui.label(tl!("to"));
ui.add_enabled(on, egui::DragValue::new(&mut d.to).range(1..=pages)).labelled_by(to.id);
});
});
group(ui, "Filters", &mut |ui| {
ui.horizontal(|ui| {
let label = ui.label(tl!("Background removal"));
ui.spacing_mut().slider_width = 220.0;
ui.add(egui::Slider::new(&mut d.settings.level, 0..=pdfcraft_engine::camera_clean::MAX_LEVEL).trailing_fill(true))
.labelled_by(label.id)
.on_hover_text(tl!("Evens out the paper so a grey or yellowed background turns white, and darkens faint strokes. 0 is off."));
});
ui.add(widgets::checkbox(&mut d.settings.edge_shadows, tl!("Edge shadow removal")))
.on_hover_text(tl!("Lightens shadows along the edges of the scan, such as a book's gutter or a copier's dark border"));
ui.add(widgets::checkbox(&mut d.settings.despeckle, tl!("Despeckle")))
.on_hover_text(tl!("Removes isolated dark specks such as dust and scanner noise. The only filter black and white scans take."));
});
ui.add_space(6.0);
let (mut go, mut cancel) = (false, false);
ui.horizontal(|ui| {
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
let ready = d.settings.changes_something();
if ui.add_enabled_ui(ready, |ui| widgets::pill_button(ui, tl!("Enhance"), true)).inner.clicked() {
go = true;
}
if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
cancel = true;
}
})
});
(go, cancel)
/// The settings the dialog's fields hold; a field that is absent keeps `base`'s value.
fn settings_from(fields: &DialogFields, base: ScanCleanSettings) -> ScanCleanSettings {
let max = f64::from(pdfcraft_engine::camera_clean::MAX_LEVEL);
ScanCleanSettings {
level: fields.number("level").map_or(base.level, |n| n.round().clamp(0.0, max) as _),
edge_shadows: fields.bool("edge_shadows").unwrap_or(base.edge_shadows),
despeckle: fields.bool("despeckle").unwrap_or(base.despeckle),
}
}
/// The dialog over `fields`: OK is on only while a filter would change something.
fn build_dialog(fields: &mut DialogFields, base: ScanCleanSettings, count: usize, current: usize) -> ParamDialog<'_> {
let max = f64::from(pdfcraft_engine::camera_clean::MAX_LEVEL);
let params = vec![
Param::read_only_text(
"note",
String::new(),
tl!("Cleans up the scanned pictures in place: recognized text, comments and links on the pages stay as they are."),
)
.full_width(),
Param::section(tl!("Pages")),
PageRangeOptions::param("pages", tl!("Pages"), count, current, PageRangeChoices::Current),
Param::section(tl!("Filters")),
Param::new("level", tl!("Background removal"), ParamKind::range(0.0, max, f64::from(base.level), SliderScale::Linear))
.decimals(0)
.tooltip(tl!("Evens out the paper so a grey or yellowed background turns white, and darkens faint strokes. 0 is off.")),
Param::new("edge_shadows", tl!("Edge shadow removal"), ParamKind::bool(base.edge_shadows))
.tooltip(tl!("Lightens shadows along the edges of the scan, such as a book's gutter or a copier's dark border")),
Param::new("despeckle", tl!("Despeckle"), ParamKind::bool(base.despeckle))
.tooltip(tl!("Removes isolated dark specks such as dust and scanner noise. The only filter black and white scans take.")),
];
let ready = settings_from(fields, base).changes_something();
ParamDialog::new(DIALOG_KEY, tl!("Enhance Scanned File"), params).ok_label(tl!("Enhance").into()).ok_enabled(ready)
}
/// What a finished run says: what was cleaned, what was not a scan, and what was left and why.
@@ -133,6 +105,34 @@ fn summary(r: &ScanCleanReport) -> String {
}
impl PdfCraftApp {
/// Opens the Enhance Scanned File dialog over the remembered choices.
pub fn open_enhance_scan(&mut self, ctx: &egui::Context) {
let Some((vi, id)) = self.active_ids() else { return };
let count = self.session.get(id).map_or(1, |d| d.info.pages.len().max(1));
let current = self.views.get(vi).map_or(0, |v| v.current);
let draft = self.scan_clean_draft.clone();
let fields = DialogFields::from_pairs([
("pages", PageRangeOptions::field_value(&draft.range)),
("level", f64::from(draft.settings.level).into()),
("edge_shadows", draft.settings.edge_shadows.into()),
("despeckle", draft.settings.despeckle.into()),
]);
let base = draft.settings;
let dialog = HostedParamDialog::new(ctx, DIALOG_KEY, fields, move |fields| build_dialog(fields, base, count, current));
self.dialogs.open(dialog);
}
/// Once per frame: takes the dialog's answer, remembers it and starts the run.
pub(crate) fn enhance_scan_pass(&mut self, _ctx: &egui::Context) {
let Some(DialogResult::Ok(answer)) = self.dialogs.take_by_id::<HostedParamDialog>(dialog_id(DIALOG_KEY)) else { return };
let d = &mut self.scan_clean_draft;
if let Some(range) = PageRangeOptions::value(&answer.fields, "pages") {
d.range = range;
}
d.settings = settings_from(&answer.fields, d.settings);
self.start_scan_clean();
}
/// Clean up the scans on the pages chosen in the dialog, in the background.
pub fn start_scan_clean(&mut self) {
let Some((vi, id)) = self.active_ids() else { return };
@@ -149,85 +149,77 @@ impl PdfCraftApp {
if !self.commit_form_typing() {
return;
}
let count = self.session.get(id).map_or(0, |d| d.info.pages.len());
let current = self.views.get(vi).map_or(0, |v| v.current);
let d = &self.scan_clean_draft;
let pages: Vec<usize> = match d.pages {
OcrPages::All => Vec::new(),
OcrPages::Current => self.views.get(vi).map(|v| vec![v.current]).unwrap_or_default(),
OcrPages::Range => (d.from.saturating_sub(1)..d.to).collect(),
};
let pages = d.range.pages(count, current, &[]);
// The engine reads an empty list as every page, so a range that picks none must not reach it.
if pages.is_empty() {
self.notify_tr("There are no pages in that range");
return;
}
let job = match self.session.scan_clean_job(id, &pages, d.settings) {
Ok(job) => job,
Err(e) => return self.notify(e.to_string()),
};
let progress = Arc::new(Mutex::new(ScanCleanProgress { total: job.total(), ..Default::default() }));
let p = progress.clone();
let work = move || {
let result = job.run(|done, total| {
let Ok(mut s) = p.lock() else { return false };
s.done = done;
s.total = total;
!s.cancel
});
if let Ok(mut s) = p.lock() {
s.result = Some(result);
}
};
#[cfg(not(target_arch = "wasm32"))]
if self.run_inline {
work();
} else if let Err(e) = std::thread::Builder::new().name("pdfcraft-scan-clean".into()).spawn(work) {
// No worker: report the failure instead of waiting for a result that never comes.
if let Ok(mut s) = progress.lock() {
s.result = Some(Err(EditError::Invalid(e.to_string())));
}
}
#[cfg(target_arch = "wasm32")]
work();
let progress = Arc::new(JobCount::default());
progress.set(0, job.total());
let (count, wake) = (progress.clone(), self.wake.clone());
self.scan_cleans.submit(id, Default::default(), move |_| {
pdfcraft_engine::guard(|| {
job.run(|done, total| {
count.set(done, total);
wake.wake();
!count.cancelled()
})
})
.unwrap_or_else(|message| Err(EditError::Invalid(message)))
});
self.scan_clean_run = Some(ScanCleanRun { doc: id, progress });
self.poll_scan_clean();
self.post_scan_clean_progress();
}
/// Stop a running clean-up (nothing changes).
pub fn cancel_scan_clean(&mut self) {
if let Some(r) = &self.scan_clean_run
&& let Ok(mut s) = r.progress.lock()
{
s.cancel = true;
}
let Some(run) = &self.scan_clean_run else { return };
run.progress.cancel();
self.post_scan_clean_progress();
}
/// Show progress; apply the result once the worker is done.
/// Shows the run's progress card.
fn post_scan_clean_progress(&mut self) {
let Some(run) = &self.scan_clean_run else { return };
let (done, total) = run.progress.read();
let cancelling = run.progress.cancelled();
let label = if cancelling {
tl!("Cancelling…").to_string()
} else {
crate::i18n::fmt(
tl!("Enhancing scans… image {d} of {t}"),
&[("d", &(done + 1).min(total.max(1)).to_string()), ("t", &total.max(1).to_string())],
)
};
let fraction = if total == 0 { 1.0 } else { done.min(total) as f32 / total as f32 };
notices::set_progress(self, ProgressKey::ScanClean, ProgressNotice { label, fraction, cancellable: !cancelling });
}
/// Show progress; apply the result once the job is done.
pub(crate) fn poll_scan_clean(&mut self) {
let Some(run) = self.scan_clean_run.as_ref() else { return };
let (doc, progress) = (run.doc, run.progress.clone());
let Ok(mut s) = progress.lock() else {
// The worker panicked while holding the lock: it will never report back.
self.scan_clean_run = None;
self.progress_notice = None;
self.notify_tr("Couldn't enhance the scanned pages");
return;
};
let Some(result) = s.result.take() else {
let (cancelling, done, total) = (s.cancel, s.done, s.total);
drop(s);
let label = if cancelling {
tl!("Cancelling…").to_string()
} else {
crate::i18n::fmt(
tl!("Enhancing scans… image {d} of {t}"),
&[("d", &(done + 1).min(total.max(1)).to_string()), ("t", &total.max(1).to_string())],
)
};
let fraction = if total == 0 { 1.0 } else { done.min(total) as f32 / total as f32 };
self.progress_notice = Some(crate::widgets::ProgressNotice { label, fraction, cancellable: !cancelling });
if let Some(ctx) = &self.ctx {
ctx.request_repaint_after(std::time::Duration::from_millis(50));
let progress = run.progress.clone();
let Some(finished) = self.scan_cleans.try_recv() else {
if progress.take_change().is_some() {
self.post_scan_clean_progress();
}
return;
};
drop(s);
notices::end_progress(self, ProgressKey::ScanClean);
self.scan_clean_run = None;
self.progress_notice = None;
self.land_scan_clean(finished.key, finished.result);
}
/// Applies a finished clean-up to `doc` as one undoable step and says what it did.
pub(crate) fn land_scan_clean(&mut self, doc: DocId, result: Result<ScanCleaned, EditError>) {
match result.and_then(|cleaned| self.session.apply_scan_clean(doc, cleaned)) {
Ok(report) => {
// The page rasters, tiles and thumbnails show the old pixels at the same scale.
@@ -256,7 +248,10 @@ mod tests {
not_scanned: vec![1],
skipped: vec![SkippedImage { pages: vec![3], name: "Im0".into(), reason: "it is CMYK".into() }],
};
assert_eq!(summary(&r), "Cleaned up the scans on 2 pages. 1 page is not a scan. 1 image was left as it was (page 4: it is CMYK).");
assert_eq!(summary(&ScanCleanReport::default()), "No scanned pages were cleaned up.");
let said = summary(&r);
assert!(said.contains('2') && said.contains('1') && said.contains('4'), "cleaned, not-a-scan and the skipped page: {said}");
let nothing = summary(&ScanCleanReport::default());
assert_ne!(said, nothing);
assert!(!nothing.chars().any(|c| c.is_ascii_digit()), "{nothing}");
}
}
+5 -3
View File
@@ -1,8 +1,9 @@
//! Push buttons run their actions (Reset form, Named, …) without a JavaScript engine.
use craft::print::ui::HostedPrint;
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use pdfcraft_ui_egui::{Dialog, PdfCraftApp};
use pdfcraft_ui_egui::PdfCraftApp;
/// Two pages; page 1 has a text field "name" (filled), a Reset button, a Next page button and a
/// Print button (a JavaScript one-liner).
@@ -70,8 +71,9 @@ fn buttons_reset_navigate_and_print() {
assert!(doc.form.iter().find(|f| f.name == "name").unwrap().value.is_empty(), "Reset form cleared the field");
}
click_field(&mut h, "print");
assert_eq!(h.state().dialog, Some(Dialog::Print), "this.print() opens the Print dialog");
h.state_mut().dialog = None;
assert!(h.state().dialogs.is_open::<HostedPrint>(), "this.print() opens the Print dialog");
let ctx = h.ctx.clone();
h.state_mut().dialogs.cancel::<HostedPrint>(&ctx);
h.run_steps(2);
// A document's Save named action never writes the file unasked (PdfCraft's XFA buttons
// use SaveAs, which opens the Save As dialog, as a script's `execMenuItem` does): the
+24 -17
View File
@@ -1,8 +1,11 @@
//! Scan & OCR ▸ Enhance scanned file in the real shell (egui_kittest): the dialog, the run and
//! its report, and that the page keeps its comment while its scan is cleaned in place.
use craft::pages::ui::PageRangeOptions;
use craft::pages::{PageRange, PageScope};
use craft::ui::dialog::dialog_id;
use craft::ui::patterns::form_dialogs::HostedParamDialog;
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use pdfcraft_engine::{Edit, NewAnnotation, NoteIcon, Session, Shape, Style};
use pdfcraft_ui_egui::PdfCraftApp;
@@ -30,13 +33,19 @@ fn harness() -> Harness<'static, PdfCraftApp> {
let mut app = PdfCraftApp::new();
app.set_option("language", "en").unwrap();
app.open_bytes("scan.pdf", None, scan()).unwrap();
app.run_inline = true;
app
});
h.run_steps(4);
h
}
/// Runs the host's queued jobs, then lets the app poll them.
// PLAN: assumed `PdfCraftApp::manual_jobs() -> &ManualJobs` (pdf-shell S-7).
fn run_jobs(h: &mut Harness<'static, PdfCraftApp>) {
h.state().manual_jobs().run_all();
h.run_steps(3);
}
#[test]
fn enhance_scanned_file_cleans_the_chosen_pages_and_says_what_it_did() {
let mut h = harness();
@@ -44,19 +53,19 @@ fn enhance_scanned_file_cleans_the_chosen_pages_and_says_what_it_did() {
assert!(h.state().session.get(id).unwrap().comment_props(0, 0).is_some());
assert!(h.state_mut().execute("ocr.enhance"));
h.run_steps(2);
assert_eq!(h.query_all_by_label("Background removal").count(), 2, "the label and the slider it names");
h.get_by_label("Edge shadow removal");
h.get_by_label("Despeckle");
h.get_by_role_and_label(egui::accesskit::Role::RadioButton, "Current page").click();
let ctx = h.ctx.clone();
let current = PageRange { scope: PageScope::Current, ..PageRange::default() };
let value = serde_json::to_value(PageRangeOptions::field_value(&current)).unwrap();
h.state_mut().dialogs.set_json_field(&ctx, dialog_id("enhance-scan"), "pages", value).unwrap();
h.run_steps(1);
h.get_by_label("Enhance").click();
h.run_steps(3);
assert!(h.state_mut().dialogs.confirm::<HostedParamDialog>(&ctx));
h.run_steps(2);
run_jobs(&mut h);
let app = h.state();
assert!(app.scan_clean_run.is_none() && app.dialog.is_none(), "finished and closed");
let toast = app.toast.clone().unwrap().0;
assert_eq!(toast, "Cleaned up the scan on 1 page.");
assert!(app.scan_clean_run.is_none() && !app.dialogs.is_open::<HostedParamDialog>(), "finished and closed");
assert!(app.toast.is_some(), "a notice says what was done");
let doc = app.session.get(id).unwrap();
assert_eq!(doc.can_undo(), Some("Enhance scanned file"));
assert!(doc.can_undo().is_some(), "one undo step restores the page");
assert_eq!(doc.info.pages.len(), 2);
assert_eq!(doc.comment_props(0, 0).map(|p| p.author), Some("Tester".to_string()), "the sticky note stays");
}
@@ -66,12 +75,10 @@ fn nothing_to_enhance_is_said_not_done() {
let mut h = harness();
let id = h.state().active_ids().unwrap().1;
h.state_mut().scan_clean_draft.settings = pdfcraft_engine::scan_clean::ScanCleanSettings { level: 0, edge_shadows: false, despeckle: false };
assert!(h.state_mut().execute("ocr.enhance"));
h.run_steps(2);
// With every filter off the button is disabled; starting anyway changes nothing.
// With every filter off the dialog's OK is disabled; starting anyway changes nothing.
h.state_mut().start_scan_clean();
h.run_steps(2);
run_jobs(&mut h);
let app = h.state();
assert!(app.toast.clone().unwrap().0.starts_with("No scanned pages were cleaned up."), "{:?}", app.toast);
assert!(app.toast.is_some(), "a notice says nothing was cleaned");
assert_eq!(app.session.get(id).unwrap().can_undo(), None);
}