Skip to content

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

M6: doc comments for the functions around this change

Eric Kadenb6cd6b44ca57
diff --git a/crates/automation/src/a11y.rs b/crates/automation/src/a11y.rs
index 19afb16a4..a11f1132f 100644
--- a/crates/automation/src/a11y.rs
+++ b/crates/automation/src/a11y.rs
@@ -575,6 +575,8 @@ impl Automation {
}
impl Automation {
+ /// Recognition settings from the arguments: `dpi` (clamped to 72–600), `language` (refused
+ /// unless the models read it) and `skip_text_pages`, each left at its default when absent.
pub(crate) fn ocr_settings(&self, a: &Args) -> Result<pdfcraft_engine::ocr::OcrSettings> {
let mut settings = pdfcraft_engine::ocr::OcrSettings::default();
if let Some(d) = a.opt_num("dpi")? {
@@ -592,6 +594,9 @@ impl Automation {
Ok(settings)
}
+ /// Recognizes text in each of `paths` and writes the result under the same file name in
+ /// `folder`, reporting per file its output, word count and skipped pages, or its error. A
+ /// path outside the root refuses the whole call before any work starts.
pub(crate) fn ocr_recognize_files(&mut self, a: &Args) -> Result<Value> {
let settings = self.ocr_settings(a)?;
let folder = self.resolve(a.str("folder")?, true)?;
@@ -623,6 +628,8 @@ impl Automation {
Ok(json!({ "files": out }))
}
+ /// Recognizes text on the open document's `pages` (every page when absent) and adds it,
+ /// reporting the total word count and, per page, why it was skipped or what it found.
pub(crate) fn ocr_recognize(&mut self, a: &Args) -> Result<Value> {
let settings = self.ocr_settings(a)?;
let pages = if a.opt_ints("pages")?.is_some() { self.pages(a, "pages")? } else { Vec::new() };
@@ -638,6 +645,8 @@ impl Automation {
Ok(json!({ "words": found.iter().map(|p| p.words.len()).sum::<usize>(), "pages": list }))
}
+ /// Whether text recognition's models are installed, the directories searched for them (in
+ /// order), and the languages it reads, each as a code and a name.
pub(crate) fn ocr_status(&self) -> Result<Value> {
use pdfcraft_engine::ocr;
let dirs: Vec<String> = ocr::Models::search_dirs().iter().map(|d| d.to_string_lossy().into_owned()).collect();
diff --git a/crates/engine/src/js.rs b/crates/engine/src/js.rs
index 9b057900f..025037b8f 100644
--- a/crates/engine/src/js.rs
+++ b/crates/engine/src/js.rs
@@ -22,6 +22,7 @@ pub struct JsOutput {
}
impl JsOutput {
+ /// Whether scripts produced nothing: no alerts, console lines, requests or errors.
pub fn is_empty(&self) -> bool {
self.alerts.is_empty() && self.console.is_empty() && self.requests.is_empty() && self.errors.is_empty()
}
diff --git a/crates/js/src/lib.rs b/crates/js/src/lib.rs
index 1eb3d60e8..6b4e7f875 100644
--- a/crates/js/src/lib.rs
+++ b/crates/js/src/lib.rs
@@ -573,6 +573,8 @@ fn message(v: &JsValue, key: &str, ctx: &mut Context) -> JsResult<String> {
}
}
+/// `app.alert(cMsg)`: records the message, given as a string or as an object with `cMsg`, and
+/// returns 1 (the OK button).
fn alert(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let m = message(&arg(args, 0), "cMsg", ctx)?;
host(ctx)?.borrow_mut().alerts.push(m);
@@ -580,11 +582,14 @@ fn alert(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue>
Ok(JsValue::from(1))
}
+/// `app.beep()`: queues a request to beep.
fn beep(_: &JsValue, _: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
host(ctx)?.borrow_mut().requests.push(Request::Beep);
Ok(JsValue::undefined())
}
+/// `app.launchURL(cURL)`: queues a request to open the URL, given as a string or as an object
+/// with `cURL`.
fn launch_url(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let u = message(&arg(args, 0), "cURL", ctx)?;
host(ctx)?.borrow_mut().requests.push(Request::LaunchUrl(u));
@@ -618,6 +623,8 @@ fn exec_menu_item(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<
Ok(JsValue::undefined())
}
+/// `app.response(…)`: always `null`, the answer for a canceled dialog, since no one is there to
+/// type one.
fn response(_: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// No one to answer: as if the dialog was cancelled.
Ok(JsValue::null())
@@ -920,6 +927,8 @@ fn printx(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue>
Ok(s(&out))
}
+/// The `color` object: each named color (`transparent`, `black`, `red`, `cyan`, `gray`, …) as
+/// an array of its color space (`T`, `G`, `RGB` or `CMYK`) followed by its components.
fn color_object(ctx: &mut Context) -> JsObject {
let mut entries: Vec<(&str, Vec<JsValue>)> = vec![
("transparent", vec![s("T")]),
diff --git a/crates/js/src/tests.rs b/crates/js/src/tests.rs
index a78ce67de..e3ce86efe 100644
--- a/crates/js/src/tests.rs
+++ b/crates/js/src/tests.rs
@@ -1040,6 +1040,8 @@ mod session {
use crate::xfa::*;
use crate::xfa_session::{Script, XfaSession};
+ /// A JavaScript or FormCalc script for `activity` on `target`, against the test form with
+ /// default limits and a 3 s timeout.
fn script(source: &str, formcalc: bool, target: &str, activity: &str) -> Script {
Script {
source: source.into(),
@@ -1051,6 +1053,9 @@ mod session {
}
}
+ /// JavaScript and FormCalc scripts run in turn in one session, each on the form the caller
+ /// sends: nothing an earlier script changed or defined leaks into the next, a failing script
+ /// doesn't end the session, and hundreds of scripts run on its one thread.
#[test]
fn scripts_of_one_event_run_on_one_thread_each_on_the_form_the_caller_sends() {
let mut s = XfaSession::new(XfaDoc { page_count: 3, ..Default::default() });
@@ -1090,6 +1095,8 @@ mod session {
assert!(started.elapsed() < std::time::Duration::from_secs(10), "{:?}", started.elapsed());
}
+ /// A JavaScript or FormCalc script whose engine takes longer to be ready than the script's
+ /// time limit still runs and returns its result, and the session stays alive.
#[test]
fn a_scripts_time_starts_when_its_context_is_ready() {
// The engine made slow to be ready (300 ms) under a 50 ms script limit: the script is
@@ -1110,6 +1117,9 @@ mod session {
assert_eq!(s.dead(), None);
}
+ /// An endless JavaScript loop past its 200 ms limit is abandoned within 2 s and leaves the
+ /// session dead, so nothing more runs in it; an endless FormCalc loop is abandoned or
+ /// reported as running too long.
#[test]
fn a_script_past_its_time_is_abandoned_and_the_session_is_dead_after_it() {
let mut s = XfaSession::new(XfaDoc::default());
diff --git a/crates/js/src/xfa.rs b/crates/js/src/xfa.rs
index 093ba633b..6b87b2e57 100644
--- a/crates/js/src/xfa.rs
+++ b/crates/js/src/xfa.rs
@@ -997,17 +997,22 @@ fn resolver(ctx: &mut Context, from: usize, many: bool) -> JsResult<JsValue> {
// ── xfa.host, xfa.layout, xfa.event, app, console ───────────────────────────────────────────
+/// Records effect `e` on the host, as [`XHost::push`] does.
fn effect(ctx: &mut Context, e: XfaEffect) -> JsResult<()> {
host(ctx)?.borrow_mut().push(e);
Ok(())
}
+/// `xfa.host.messageBox(message)`: records the message, cut to [`MAX_LINE`] characters, and
+/// returns 1.
fn message_box(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let m = clip(text(&arg(args, 0), ctx)?);
effect(ctx, XfaEffect::MessageBox(m))?;
Ok(JsValue::from(1.0))
}
+/// `xfa.host.resetData(names)`: asks to reset the comma-separated fields, or every field when
+/// the list is empty.
fn reset_data(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let list = text(&arg(args, 0), ctx)?;
let names: Vec<String> = list.split(',').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect();
@@ -1015,22 +1020,28 @@ fn reset_data(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsVa
Ok(JsValue::undefined())
}
+/// `xfa.host.print(…)`: asks to print; its arguments are ignored.
fn print(_: &JsValue, _: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
effect(ctx, XfaEffect::Print)?;
Ok(JsValue::undefined())
}
+/// `xfa.host.beep()`: asks to beep.
fn beep(_: &JsValue, _: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
effect(ctx, XfaEffect::Beep)?;
Ok(JsValue::undefined())
}
+/// `xfa.host.gotoURL(url)`: asks to open the URL.
fn goto_url(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let u = text(&arg(args, 0), ctx)?;
effect(ctx, XfaEffect::LaunchUrl(u))?;
Ok(JsValue::undefined())
}
+/// `xfa.host.setFocus(target)`: asks to focus the node, given as a node object or as a SOM
+/// expression resolved from the current node; an expression that names no node is passed on
+/// as written.
fn set_focus(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let target = arg(args, 0);
let som = match target.as_object() {
diff --git a/crates/ui-egui/tests/forms.rs b/crates/ui-egui/tests/forms.rs
index 47a5c8b52..85b0ac03d 100644
--- a/crates/ui-egui/tests/forms.rs
+++ b/crates/ui-egui/tests/forms.rs
@@ -1050,6 +1050,9 @@ fn on_screen_fit(h: &Harness<'static, PdfCraftApp>, name: &str, text: &str) -> (
}
}
+/// Text typed into an auto-size field shrinks to fit the box's width, while short text keeps a
+/// larger size; a multiline auto-size field wraps and shrinks only as far as the box's height
+/// needs, and a fixed 10 pt field keeps its size.
#[test]
fn auto_size_fields_shrink_and_wrap_typed_text_to_fit_their_box() {
let mut h = auto_size_harness();