Skip to content

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

Merge remote-tracking branch 'upstream/main' into fix/page-render-warnings

Claude640d5718aad9
diff --cc crates/automation/src/dispatch.rs
index 000000000,26e1f9536..230a9d302
mode 000000,100644..100644
--- a/crates/automation/src/dispatch.rs
+++ b/crates/automation/src/dispatch.rs
@@@ -1,0 -1,784 +1,784 @@@
+ use super::error::{Result, ToolError, failed};
+ use super::json::{Args, document_attachment_keys, info, one_based, parse_style_ranges, security, summary};
+ use super::paths::{read_input, write_atomic};
+ use super::*;
+
+ /// One piece of a tool's result.
+ #[derive(Clone, Debug, PartialEq)]
+ pub enum Content {
+ /// Structured data (MCP: a text block with the JSON plus `structuredContent`).
+ Json(Value),
+ /// A PNG image.
+ Png { data: Vec<u8>, width: u32, height: u32 },
+ }
+
+ impl Automation {
+ /// Run the tool `name` with JSON `args` (an object; `null` means no arguments).
+ pub fn call(&mut self, name: &str, args: &Value) -> Result<Vec<Content>> {
+ let empty = json!({});
+ let args = if args.is_null() { &empty } else { args };
+ if !args.is_object() {
+ return Err(ToolError::InvalidArgs("arguments must be a JSON object".into()));
+ }
+ let def = tools::find(name).ok_or_else(|| ToolError::UnknownTool(name.into()))?;
+ tools::check_args(def, args)?;
+ let a = Args(args);
+ let out = match name {
+ "doc_open" => self.doc_open(&a)?,
+ "doc_batch" => self.doc_batch(&a)?,
+ "doc_list" => json!({ "documents": self.session.docs().iter().map(summary).collect::<Vec<_>>() }),
+ "doc_info" => {
+ let id = self.doc(&a)?.id;
+ self.session.refresh_fonts(id);
+ info(self.doc(&a)?)
+ }
+ "doc_close" => self.doc_close(&a)?,
+ "doc_save" => self.doc_save(&a)?,
+ "doc_set_info" => {
+ let edit = Edit::SetInfo { key: a.str("key")?.into(), value: a.str("value")?.into() };
+ self.apply(&a, edit)?
+ }
- "page_render" => return self.page_render(&a).map(|c| vec![c]),
++ "page_render" => return self.page_render(&a),
+ "comment_image_preview" => return self.comment_image_preview(&a),
+ "text_extract" => self.text_extract(&a)?,
+ "text_word_count" => self.text_word_count(&a)?,
+ "text_find" => self.text_find(&a)?,
+ "page_rotate" => {
+ let degrees = a.int("degrees")?;
+ if degrees % 90 != 0 {
+ return Err(ToolError::InvalidArgs("degrees must be a multiple of 90".into()));
+ }
+ let doc = self.doc(&a)?;
+ let base = match a.opt_ints("pages")? {
+ Some(_) => self.pages(&a, "pages")?,
+ None => (0..doc.info.pages.len()).collect(),
+ };
+ let parity = match a.opt_str("subset")?.unwrap_or("all") {
+ "all" => pdfcraft_engine::PageParity::Both,
+ "even" => pdfcraft_engine::PageParity::Even,
+ "odd" => pdfcraft_engine::PageParity::Odd,
+ s => return Err(ToolError::InvalidArgs(format!("unknown subset {s:?} (all, even, odd)"))),
+ };
+ let orientation = match a.opt_str("orientation")?.unwrap_or("all") {
+ "all" => pdfcraft_engine::PageOrientation::Both,
+ "landscape" => pdfcraft_engine::PageOrientation::Landscape,
+ "portrait" => pdfcraft_engine::PageOrientation::Portrait,
+ o => return Err(ToolError::InvalidArgs(format!("unknown orientation {o:?} (all, landscape, portrait)"))),
+ };
+ let pages = pdfcraft_engine::filter_pages(&self.doc(&a)?.info, &base, parity, orientation);
+ if pages.is_empty() {
+ return Err(failed("no pages match the filters"));
+ }
+ let n = pages.len();
+ let mut out = self.apply(&a, Edit::RotatePages { pages, degrees })?;
+ out["rotated"] = json!(n);
+ out
+ }
+ "page_enhance_camera" => {
+ let page = self.page(&a)?;
+ let given = a.nums::<8>("corners")?;
+ let doc = self.doc(&a)?;
+ let (id, info) = (doc.id, doc.info.pages.get(page).cloned().ok_or_else(|| failed("no such page"))?);
+ let corners = match given {
+ Some(c) => [0, 1, 2, 3].map(|i| {
+ let [x, y] = info.view_to_user(c[2 * i] as f32, c[2 * i + 1] as f32);
+ [x as f64, y as f64]
+ }),
+ None => self.session.detect_camera_corners(id, page).ok_or_else(|| {
+ failed("the document's edges could not be found on this page: pass `corners` [x1, y1, x2, y2, x3, y3, x4, y4]")
+ })?,
+ };
+ let level = match a.opt_int("level")? {
+ None => 0,
+ Some(l @ 0..=100) => l as u8,
+ Some(l) => return Err(ToolError::InvalidArgs(format!("level must be between 0 and 100, not {l}"))),
+ };
+ let look = match a.opt_str("mode")? {
+ None => pdfcraft_engine::camera_clean::Look::Colour,
+ Some(m) => pdfcraft_engine::camera_clean::Look::from_name(m)
+ .ok_or_else(|| ToolError::InvalidArgs(format!("unknown mode {m:?} (original, colour, greyscale, bw)")))?,
+ };
+ self.session.enhance_camera_page_at(id, page, corners, look, level).map_err(failed)?;
+ let r = |v: f32| (v * 100.0).round() / 100.0;
+ let used: Vec<f32> = corners.iter().flat_map(|&[x, y]| info.user_to_view(x as f32, y as f32)).map(r).collect();
+ let mut out = summary(self.doc(&a)?);
+ out["corners"] = json!(used);
+ out["detected"] = json!(given.is_none());
+ out
+ }
+ "page_delete" => {
+ let pages = self.pages(&a, "pages")?;
+ self.apply(&a, Edit::DeletePages { pages })?
+ }
+ "page_move" => {
+ let pages = self.pages(&a, "pages")?;
+ let to = self.position(&a, "to")?;
+ self.apply(&a, Edit::MovePages { pages, to })?
+ }
+ "page_reverse" => {
+ let pages = match a.opt_ints("pages")? {
+ Some(_) => self.pages(&a, "pages")?,
+ None => (0..self.doc(&a)?.info.pages.len()).collect(),
+ };
+ self.apply(&a, Edit::ReversePages { pages })?
+ }
+ "page_insert_blank" => self.insert_blank(&a)?,
+ "page_insert_file" => self.insert_file(&a)?,
+ "page_extract" => self.page_extract(&a)?,
+ "doc_combine" => self.doc_combine(&a)?,
+ "doc_create_multiple" => self.doc_create_multiple(&a)?,
+ "doc_split" => self.doc_split(&a)?,
+ "edit_undo" => {
+ let id = self.doc(&a)?.id;
+ let label = self.session.undo(id).map_err(failed)?.to_string();
+ json!({ "undone": label, "document": summary(self.doc(&a)?) })
+ }
+ "edit_redo" => {
+ let id = self.doc(&a)?.id;
+ let label = self.session.redo(id).map_err(failed)?.to_string();
+ json!({ "redone": label, "document": summary(self.doc(&a)?) })
+ }
+ "command_list" => self.command_list(&a)?,
+ "command_run" => return self.command_run(&a),
+ "command_batch" => self.command_batch(&a)?,
+ "doc_inspect" => self.doc_inspect(&a)?,
- "render_preview" => return self.render_preview(&a).map(|c| vec![c]),
++ "render_preview" => return self.render_preview(&a),
+ "page_number" => {
+ use pdfcraft_organize::LabelStyle as L;
+ let n = self.doc(&a)?.info.pages.len();
+ let (from, to) = (a.int("from")?, a.int("to")?);
+ if from < 1 || to < from || to as usize > n {
+ return Err(ToolError::InvalidArgs(format!("from and to must satisfy 1 ≤ from ≤ to ≤ {n}")));
+ }
+ let style = match a.opt_str("style")?.unwrap_or("decimal") {
+ "decimal" => L::Decimal,
+ "upper-roman" => L::UpperRoman,
+ "lower-roman" => L::LowerRoman,
+ "upper-alpha" => L::UpperAlpha,
+ "lower-alpha" => L::LowerAlpha,
+ "none" => L::None,
+ other => return Err(ToolError::InvalidArgs(format!("unknown style {other:?}"))),
+ };
+ let prefix = a.opt_str("prefix")?.unwrap_or_default().to_string();
+ let first = a.opt_int("start")?.unwrap_or(1).clamp(1, u32::MAX as i64) as u32;
+ let mut out = self.apply(&a, Edit::NumberPages { from: from as usize - 1, to: to as usize - 1, style, prefix, first })?;
+ out["labels"] = json!(self.doc(&a)?.info.pages.iter().map(|p| p.label.clone()).collect::<Vec<_>>());
+ out
+ }
+ "bookmark_list" => self.bookmark_listing(&a)?,
+ "bookmark_add" => {
+ let page = self.page(&a)?;
+ let parent = a.opt_path("parent")?.unwrap_or_default();
+ let index = a.opt_int("position")?.map_or(usize::MAX, |p| (p.max(1) - 1) as usize);
+ let title = a.str("title")?.to_string();
+ let (left, top) = (a.bookmark_coordinate("left")?, a.bookmark_coordinate("top")?);
+ let [left, top] = match self.doc(&a)?.info.pages.get(page) {
+ Some(p) => p.xyz_for_view(left, top),
+ None => [None, None],
+ };
+ let (left, top) = (left.map(f64::from), top.map(f64::from));
+ self.apply(&a, Edit::AddBookmark { parent, index, title, page, left, top })?
+ }
+ "bookmark_rename" => {
+ let (path, title) = (a.path("path")?, a.str("title")?.to_string());
+ self.apply(&a, Edit::RenameBookmark { path, title })?
+ }
+ "bookmark_delete" => {
+ let path = a.path("path")?;
+ self.apply(&a, Edit::DeleteBookmark { path })?
+ }
+ "bookmark_move" => {
+ let from = a.path("path")?;
+ let to_parent = a.opt_path("parent")?.unwrap_or_default();
+ let index = a.opt_int("position")?.map_or(usize::MAX, |p| (p.max(1) - 1) as usize);
+ self.apply(&a, Edit::MoveBookmark { from, to_parent, index })?
+ }
+ "bookmark_set_page" => {
+ let (path, page) = (a.path("path")?, self.page(&a)?);
+ self.apply(&a, Edit::SetBookmarkPage { path, page })?
+ }
+ "bookmark_set_style" => {
+ let path = a.path("path")?;
+ // Left out keeps the colour; [0, 0, 0] is the default black.
+ let color = match a.get("color") {
+ None => None,
+ Some(v) => {
+ let rgb: Option<Vec<f64>> = v.as_array().and_then(|c| c.iter().map(Value::as_f64).collect());
+ match rgb.as_deref() {
+ Some(&[r, g, b]) if [r, g, b].iter().all(|c| (0.0..=1.0).contains(c)) => Some([r, g, b]),
+ _ => return Err(ToolError::InvalidArgs("color must be [r, g, b], each from 0 to 1".into())),
+ }
+ }
+ };
+ let (bold, italic) = (a.opt_bool("bold")?, a.opt_bool("italic")?);
+ if color.is_none() && bold.is_none() && italic.is_none() {
+ return Err(ToolError::InvalidArgs("give color, bold or italic".into()));
+ }
+ let mut edits = Vec::new();
+ if let Some(color) = color {
+ edits.push(Edit::SetBookmarkColor { path: path.clone(), color: Some(color) });
+ }
+ if bold.is_some() || italic.is_some() {
+ edits.push(Edit::SetBookmarkStyle { path, bold, italic });
+ }
+ // A colour and a style given together are one step to undo, applied all or nothing.
+ match edits.len() {
+ 1 => self.apply(&a, edits.remove(0))?,
+ _ => self.apply(&a, Edit::Batch { label: "Style".into(), edits })?,
+ }
+ }
+ "bookmark_from_structure" => {
+ let mut out = self.apply(&a, Edit::BookmarksFromStructure)?;
+ let listing = self.bookmark_listing(&a)?;
+ out["bookmarks"] = listing["bookmarks"].clone();
+ out["next"] = listing["next"].clone();
+ out
+ }
+ "doc_protect" => self.doc_protect(&a)?,
+ "page_replace" => {
+ let pages = self.pages(&a, "pages")?;
+ let path = self.resolve(a.str("path")?, false)?;
+ let bytes = Arc::new(read_input(&path)?);
+ let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
+ let src_pages = match a.opt_ints("from_pages")? {
+ Some(p) => one_based(&p)?,
+ None => (0..pages.len()).collect(),
+ };
+ self.apply(&a, Edit::ReplacePages { pages, name, bytes, src_pages })?
+ }
+ "page_duplicate" => {
+ let pages = self.pages(&a, "pages")?;
+ let at = self.opt_position(&a, "at")?;
+ self.apply(&a, Edit::DuplicatePages { pages, at })?
+ }
+ "page_set_box" => self.page_set_box(&a)?,
+ "page_transition_set" => self.page_transition_set(&a)?,
+ "doc_create" => self.doc_create(&a)?,
+ "xfa_to_static" => self.apply(&a, Edit::XfaToStatic)?,
+ "doc_flatten" => {
+ let (comments, fields) = (a.opt_bool("comments")?.unwrap_or(true), a.opt_bool("fields")?.unwrap_or(true));
+ if !comments && !fields {
+ return Err(ToolError::InvalidArgs("nothing to flatten".into()));
+ }
+ self.apply(&a, Edit::Flatten { comments, fields })?
+ }
+ "doc_reduce" => {
+ let id = self.doc(&a)?.id;
+ let before = self.doc(&a)?.bytes.len();
+ let path = self.resolve(a.str("path")?, true)?;
+ let (bytes, merged) = self.session.reduced_bytes(id).map_err(failed)?;
+ // As in the app, a copy that isn't smaller is not written (#490).
+ let written = bytes.len() < before;
+ if written {
+ write_atomic(&path, &bytes)?;
+ }
+ json!({ "path": path.to_string_lossy(), "written": written, "bytes_before": before, "bytes_after": bytes.len(), "merged_objects": merged })
+ }
+ "doc_optimize" => self.doc_optimize(&a)?,
+ "doc_initial_view" => self.doc_initial_view(&a)?,
+ "doc_revisions" => self.doc_revisions(&a)?,
+ "text_lines" => {
+ let page = self.page(&a)?;
+ let doc = self.doc_to_copy(&a)?;
+ let info = &doc.info.pages[page];
+ let r = |x: f32| (x as f64 * 100.0).round() / 100.0;
+ let lines: Vec<Value> = doc
+ .text_lines(page)
+ .iter()
+ .enumerate()
+ .map(|(i, l)| {
+ let (u, v) = (info.user_to_view(l.rect[0] as f32, l.rect[1] as f32), info.user_to_view(l.rect[2] as f32, l.rect[3] as f32));
+ json!({
+ "line": i + 1,
+ "text": l.text,
+ "rect": [r(u[0].min(v[0])), r(u[1].min(v[1])), r(u[0].max(v[0])), r(u[1].max(v[1]))],
+ "font": l.base_font,
+ "size": (l.size * 100.0).round() / 100.0,
+ })
+ })
+ .collect();
+ json!({ "page": page + 1, "count": lines.len(), "lines": lines })
+ }
+ "image_hit" => {
+ let page = self.page(&a)?;
+ let point = a
+ .get("point")
+ .and_then(Value::as_array)
+ .filter(|p| p.len() == 2)
+ .and_then(|p| Some([p.first()?.as_f64()?, p.get(1)?.as_f64()?]))
+ .filter(|p| p.iter().all(|v| v.is_finite() && v.abs() <= 1e9))
+ .ok_or_else(|| ToolError::InvalidArgs("point must contain two finite coordinates".into()))?;
+ let doc = self.doc(&a)?;
+ let p = &doc.info.pages[page];
+ let user = p.view_to_user(point[0] as f32, point[1] as f32).map(f64::from);
+ let images: Vec<_> = doc.page_images(page).iter().enumerate().filter(|(_, im)| im.contains(user)).map(|(i, _)| i + 1).collect();
+ json!({"page":page+1,"images":images})
+ }
+ "page_images" => {
+ let page = self.page(&a)?;
+ let doc = self.doc(&a)?;
+ let info = &doc.info.pages[page];
+ let r = |x: f32| (x as f64 * 100.0).round() / 100.0;
+ let list: Vec<Value> = doc
+ .page_images(page)
+ .iter()
+ .enumerate()
+ .map(|(i, im)| {
+ let (u, v) = (info.user_to_view(im.rect[0] as f32, im.rect[1] as f32), info.user_to_view(im.rect[2] as f32, im.rect[3] as f32));
+ let visible = im.visible_rect.map(|rect| {
+ let (u, v) = (info.user_to_view(rect[0] as f32, rect[1] as f32), info.user_to_view(rect[2] as f32, rect[3] as f32));
+ [r(u[0].min(v[0])), r(u[1].min(v[1])), r(u[0].max(v[0])), r(u[1].max(v[1]))]
+ });
+ json!({ "visible_rect": visible, "image": i + 1, "rect": [r(u[0].min(v[0])), r(u[1].min(v[1])), r(u[0].max(v[0])), r(u[1].max(v[1]))], "pixels": [im.width, im.height], "name": im.name, "kind": if im.is_form { "form" } else { "image" } })
+ })
+ .collect();
+ json!({ "page": page + 1, "count": list.len(), "images": list })
+ }
+ "image_edit" | "image_save" => {
+ let page = self.page(&a)?;
+ let n = self.doc(&a)?.page_images(page).len();
+ let k = a.int("image")?;
+ if k < 1 || k as usize > n {
+ return Err(ToolError::InvalidArgs(format!("image {k} is out of range: page {} has {n} images", page + 1)));
+ }
+ let index = k as usize - 1;
+ if name == "image_save" {
+ let (ext, bytes) = self.doc(&a)?.page_image_file(page, index).map_err(failed)?;
+ let mut path = self.resolve(a.str("path")?, true)?;
+ if path.is_dir() {
+ // Adding the extension to a folder's name would write beside it: "." is the
+ // root, so that would land outside it.
+ return Err(failed(format!("{}: is a folder, not a file", path.display())));
+ }
+ if path.extension().is_none() {
+ path.set_extension(ext);
+ }
+ write_atomic(&path, &bytes)?;
+ json!({ "path": path.to_string_lossy(), "format": ext, "bytes": bytes.len() })
+ } else {
+ use pdfcraft_engine::ImageEdit;
+ let change = match a.str("action")? {
+ "move" => {
+ let r: Vec<f64> =
+ a.get("rect").and_then(Value::as_array).map(|x| x.iter().filter_map(Value::as_f64).collect()).unwrap_or_default();
+ let r = <[f64; 4]>::try_from(r).map_err(|_| ToolError::InvalidArgs("move needs rect: 4 numbers".into()))?;
+ // Top-left-origin points → user space.
+ let info = &self.doc(&a)?.info.pages[page];
+ let (u0, u1) = (info.view_to_user(r[0] as f32, r[1] as f32), info.view_to_user(r[2] as f32, r[3] as f32));
+ ImageEdit::Move([u0[0].min(u1[0]) as f64, u0[1].min(u1[1]) as f64, u0[0].max(u1[0]) as f64, u0[1].max(u1[1]) as f64])
+ }
+ "rotate_by" => ImageEdit::RotateBy(
+ a.get("degrees")
+ .and_then(Value::as_f64)
+ .ok_or_else(|| ToolError::InvalidArgs("rotate_by needs degrees: a number (clockwise)".into()))?,
+ ),
+ "rotate" => ImageEdit::Rotate(a.opt_int("quarters")?.unwrap_or(1) as i32),
+ "crop" => {
+ let r: Vec<f64> =
+ a.get("rect").and_then(Value::as_array).map(|x| x.iter().filter_map(Value::as_f64).collect()).unwrap_or_default();
+ let r =
+ <[f64; 4]>::try_from(r).map_err(|_| ToolError::InvalidArgs("crop needs rect: 4 numbers, the part to keep".into()))?;
+ let info = &self.doc(&a)?.info.pages[page];
+ let (u0, u1) = (info.view_to_user(r[0] as f32, r[1] as f32), info.view_to_user(r[2] as f32, r[3] as f32));
+ ImageEdit::Crop([u0[0].min(u1[0]) as f64, u0[1].min(u1[1]) as f64, u0[0].max(u1[0]) as f64, u0[1].max(u1[1]) as f64])
+ }
+ "flip_horizontal" => ImageEdit::Flip { horizontal: true },
+ "flip_vertical" => ImageEdit::Flip { horizontal: false },
+ "delete" => ImageEdit::Delete,
+ "replace" => {
+ let path = self.resolve(a.str("path")?, false)?;
+ let bytes = read_input(&path)?;
+ ImageEdit::Replace {
+ name: path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(),
+ bytes: Arc::new(bytes),
+ }
+ }
+ other => return Err(ToolError::InvalidArgs(format!("unknown action {other:?}"))),
+ };
+ self.apply(&a, Edit::EditPageImage { page, index, change })?
+ }
+ }
+ "text_paragraphs" => {
+ let page = self.page(&a)?;
+ let doc = self.doc_to_copy(&a)?;
+ let info = &doc.info.pages[page];
+ let r = |x: f32| (x as f64 * 100.0).round() / 100.0;
+ let current = doc.text_blocks(page);
+ let id = doc.id;
+ let blocks: Vec<Value> = current
+ .iter()
+ .enumerate()
+ .map(|(i, b)| {
+ let (u, v) = (info.user_to_view(b.rect[0] as f32, b.rect[1] as f32), info.user_to_view(b.rect[2] as f32, b.rect[3] as f32));
+ json!({
+ "paragraph": i + 1,
+ "text": b.text,
+ "editor_text": b.editor_text,
+ "rows": b.rows.iter().map(|row| row.iter().map(|i| i + 1).collect::<Vec<_>>()).collect::<Vec<_>>(),
+ "lines": b.lines.iter().map(|l| l + 1).collect::<Vec<_>>(),
+ "rect": [r(u[0].min(v[0])), r(u[1].min(v[1])), r(u[0].max(v[0])), r(u[1].max(v[1]))],
+ "font": b.base_font,
+ "size": (b.size * 100.0).round() / 100.0,
+ })
+ })
+ .collect();
+ self.record_listing(id, page, &current);
+ json!({ "page": page + 1, "count": blocks.len(), "paragraphs": blocks })
+ }
+ "text_font_files" => {
+ let roots = if let Some(paths) = a.get("directories") {
+ let paths = paths
+ .as_array()
+ .filter(|p| p.len() <= 16)
+ .ok_or_else(|| ToolError::InvalidArgs("directories must be at most 16 paths".into()))?;
+ paths
+ .iter()
+ .map(|p| self.resolve(p.as_str().ok_or_else(|| ToolError::InvalidArgs("directory must be a path".into()))?, false))
+ .collect::<Result<Vec<_>>>()?
+ } else if let Some(roots) = &self.roots {
+ // The installed fonts are outside the roots: look in the read root instead.
+ let read = roots
+ .read_root()
+ .ok_or_else(|| failed("automation filesystem access is not granted: read authority is absent: installed fonts"))?;
+ vec![self.resolve(&read.to_string_lossy(), false)?]
+ } else if let Some(root) = &self.root {
+ vec![root.clone()]
+ } else {
+ pdfcraft_engine::text_fonts::installed_directories()
+ };
+ json!({"fonts": pdfcraft_engine::text_fonts::discover(&roots).into_iter().map(|f| json!({"path":f.path,"family":f.family,"font":f.face})).collect::<Vec<_>>()})
+ }
+ "text_font_import" => {
+ let page = self.page(&a)?;
+ let path = self.resolve(a.str("path")?, false)?;
+ let file = std::fs::File::open(&path).map_err(failed)?;
+ use std::io::Read;
+ let mut bytes = Vec::new();
+ file.take((16 << 20) + 1).read_to_end(&mut bytes).map_err(failed)?;
+ if bytes.len() > 16 << 20 {
+ return Err(ToolError::InvalidArgs("font input exceeds 16 MiB".into()));
+ }
+ let before: Vec<_> = self.doc(&a)?.text_editor_fonts(page).into_iter().map(|f| f.resource).collect();
+ let mut out = self.apply(&a, Edit::RegisterTextFont { page, bytes: Arc::new(bytes) })?;
+ let added = self
+ .doc(&a)?
+ .text_editor_fonts(page)
+ .into_iter()
+ .find(|f| !before.contains(&f.resource) && !f.resource.starts_with("PCApproved"));
+ if let Some(f) = added {
+ out["resource"] = json!(f.resource);
+ out["font"] = json!(f.face);
+ }
+ out
+ }
+ "text_fonts" => {
+ let page = self.page(&a)?;
+ json!({"fonts": self.doc(&a)?.text_editor_fonts(page).into_iter().map(|f| json!({"resource":f.resource,"font":f.face,"full_face":f.resource.starts_with("PCFull"),"preview_only":f.resource.starts_with("PCApproved")})).collect::<Vec<_>>()})
+ }
+ "text_edit_layout" => {
+ let page = self.page(&a)?;
+ let doc = self.doc(&a)?;
+ let k = a.int("paragraph")?;
+ let blocks = doc.text_blocks(page);
+ let index = self.paragraph_index(doc.id, page, k, &blocks)?;
+ let source = blocks.get(index).ok_or_else(|| ToolError::InvalidArgs("paragraph is out of range".into()))?;
+ let text = a.opt_str("text")?.unwrap_or(&source.editor_text);
+ let style = pdfcraft_engine::BlockStyle {
+ size: a.opt_num("size")?,
+ width: a.opt_num("width")?,
+ resource: a.opt_str("font_resource")?.map(str::to_owned),
+ ranges: parse_style_ranges(a.get("ranges"))?,
+ ..Default::default()
+ };
+ let layout = doc.text_edit_layout(page, index, text, &style).map_err(failed)?;
+ let info = &doc.info.pages[page];
+ let view_rect = |r: [f64; 4]| {
+ let a = info.user_to_view(r[0] as f32, r[1] as f32);
+ let b = info.user_to_view(r[2] as f32, r[3] as f32);
+ [a[0].min(b[0]), a[1].min(b[1]), a[0].max(b[0]), a[1].max(b[1])]
+ };
+ let fonts: Vec<_> = doc.text_editor_fonts(page).into_iter().map(|f| json!({"resource": f.resource, "face": f.face})).collect();
+ json!({ "page": page + 1, "paragraph": k, "source_rect": view_rect(layout.source_rect),
+ "draft_rect": layout.draft_rect.map(view_rect), "overflow": layout.overflow, "embedded_fonts": fonts })
+ }
+ "text_edit" if a.get("paragraph").is_some() => {
+ let page = self.page(&a)?;
+ let k = a.int("paragraph")?;
+ let (id, blocks) = {
+ let doc = self.doc(&a)?;
+ (doc.id, doc.text_blocks(page))
+ };
+ let index = self.paragraph_index(id, page, k, &blocks)?;
+ let block = blocks.get(index).cloned().ok_or_else(|| ToolError::InvalidArgs(format!("paragraph {k} is out of range")))?;
+ let text = a.opt_str("text")?.map(str::to_owned).unwrap_or(block.text);
+ let mut style = pdfcraft_engine::BlockStyle {
+ ranges: parse_style_ranges(a.get("ranges"))?,
+ resource: a.opt_str("font_resource")?.map(str::to_owned),
+ bold: a.opt_bool("bold")?,
+ size: a.opt_num("size")?,
+ underline: a.opt_bool("underline")?,
+ line_spacing: a.opt_num("line_spacing")?,
+ char_spacing: a.opt_num("char_spacing")?,
+ scale: a.opt_num("scale")?,
+ width: a.opt_num("width")?,
+ ..Default::default()
+ };
+ let (dx, dy) = (a.opt_num("dx")?, a.opt_num("dy")?);
+ if dx.is_some() || dy.is_some() {
+ style.offset = Some([dx.unwrap_or(0.0), dy.unwrap_or(0.0)]);
+ }
+ if let Some(f) = a.opt_str("font")? {
+ let family = match f {
+ "helvetica" => pdfcraft_engine::FontFamily::Helvetica,
+ "times" => pdfcraft_engine::FontFamily::Times,
+ "courier" => pdfcraft_engine::FontFamily::Courier,
+ other => return Err(ToolError::InvalidArgs(format!("unknown font {other:?} (helvetica, times, courier)"))),
+ };
+ style.family = Some((family, a.opt_bool("bold")?.unwrap_or(false), a.opt_bool("italic")?.unwrap_or(false)));
+ }
+ if let Some(c) = a.opt_str("color")? {
+ style.color = Some(comments::parse_color(c)?);
+ }
+ if let Some(al) = a.opt_str("align")? {
+ style.align = Some(match al {
+ "left" => pdfcraft_engine::TextAlign::Left,
+ "center" => pdfcraft_engine::TextAlign::Center,
+ "right" => pdfcraft_engine::TextAlign::Right,
+ "justify" => pdfcraft_engine::TextAlign::Justify,
+ other => return Err(ToolError::InvalidArgs(format!("unknown align {other:?}"))),
+ });
+ }
+ // Where the paragraph is drawn after the edit: moved by dx, dy (up is +dy).
+ let [dx, dy] = style.offset.unwrap_or([0.0, 0.0]);
+ let moved = [block.rect[0] + dx, block.rect[1] + dy, block.rect[2] + dx, block.rect[3] + dy];
+ let wanted: String = text.split_whitespace().collect();
+ let mut out = self.apply(&a, Edit::EditTextBlock { page, block: index, text, style })?;
+ let doc = self.doc(&a)?;
+ let after = doc.text_blocks(page);
+ // Without `text` the paragraph keeps the document's own words: not echoed when
+ // the document doesn't allow copying them.
+ let copy = doc.allows_copying();
+ // Only a paragraph that now shows exactly the requested text is the edited one. A
+ // neighbour that overlaps the old box (overlaid text, a text layer) must not take
+ // its number, and neither must a paragraph the edit merged it into: rewriting that
+ // one by this number would drop the neighbour's text.
+ let edited = block_overlapping(&after, moved)
+ .filter(|&i| !wanted.is_empty() && after.get(i).is_some_and(|b| b.text.split_whitespace().collect::<String>() == wanted));
+ if let Some(b) = edited.and_then(|i| after.get(i)) {
+ out["paragraph"] = json!({ "lines": b.lines.len(), "font": b.base_font });
+ if copy {
+ out["paragraph"]["text"] = json!(b.text);
+ }
+ }
+ let stale = self.follow_edit(id, page, k, edited.and_then(|i| after.get(i)), &after);
+ if !stale.is_empty() {
+ out["stale_paragraphs"] = json!(stale);
+ }
+ out
+ }
+ "text_edit" => {
+ let page = self.page(&a)?;
+ let n = self.doc(&a)?.text_lines(page).len();
+ let line = a.int("line")?;
+ if line < 1 || line as usize > n {
+ return Err(ToolError::InvalidArgs(format!("line {line} is out of range: page {} has {n} lines", page + 1)));
+ }
+ let text = a.str("text")?.to_owned();
+ let mut out = self.apply(&a, Edit::EditTextLine { page, line: line as usize - 1, text })?;
+ let after = self.doc(&a)?.text_lines(page);
+ if let Some(l) = after.get(line as usize - 1) {
+ out["line"] = json!({ "text": l.text, "font": l.base_font });
+ }
+ out
+ }
+ "doc_audit_space" => {
+ let rows: Vec<Value> = self
+ .doc(&a)?
+ .audit_space()
+ .iter()
+ .map(|u| json!({ "category": u.category.label(), "bytes": u.bytes, "percent": (u.percent * 100.0).round() / 100.0 }))
+ .collect();
+ json!({ "categories": rows })
+ }
+ "doc_open_revision" => {
+ let id = self.doc(&a)?.id;
+ let n = usize::try_from(a.opt_int("revision")?.ok_or_else(|| ToolError::InvalidArgs("revision is required".into()))?).unwrap_or(0);
+ let new = self.session.open_revision(id, n).map_err(failed)?;
+ summary(self.session.get(new).ok_or_else(|| failed("the document vanished"))?)
+ }
+ "doc_export_images" | "doc_export_text" | "doc_export_all_images" => self.export(name, &a)?,
+ "doc_header_footer" | "doc_watermark" | "doc_background" | "doc_remove_marks" => self.marks(name, &a)?,
+ "doc_unprotect" => {
+ let mut out = self.apply(&a, Edit::RemoveProtection)?;
+ out["security"] = security(self.doc(&a)?);
+ out
+ }
+ "form_fields" => self.form_fields(&a)?,
+ "form_fill" => self.form_fill(&a)?,
+ "form_set_image" => {
+ let path = self.resolve(a.str("path")?, false)?;
+ let bytes = read_input(&path)?;
+ self.apply(&a, Edit::SetFieldImage { name: a.str("field")?.to_owned(), image: Arc::new(bytes) })?
+ }
+ "form_reset" => self.form_reset(&a)?,
+ "form_add_field" => self.form_add_field(&a)?,
+ "form_add_field_table" => self.form_add_field_table(&a)?,
+ "form_set_props" => self.form_set_props(&a)?,
+ "form_delete_field" => self.form_delete_field(&a)?,
+ "form_tab_order" => self.form_tab_order(&a)?,
+ "form_calculation_order" => self.form_calculation_order(&a)?,
+ "doc_export_data" => self.doc_export_data(&a)?,
+ "doc_import_data" => self.doc_import_data(&a)?,
+ "redact_mark" => self.redact_mark(&a)?,
+ "redact_apply" => self.redact_apply(&a)?,
+ "redact_clear" => self.redact_clear(&a)?,
+ "doc_hidden_info" => self.doc_hidden_info(&a)?,
+ "printers" => self.printers()?,
+ "printer_options" => self.printer_options(&a)?,
+ "link_list" => self.link_list(&a)?,
+ "link_open" => self.link_open(&a)?,
+ "link_add" => self.link_add(&a)?,
+ "link_edit" => self.link_edit(&a)?,
+ "link_delete" => self.link_delete(&a)?,
+ "links_from_urls" => self.links_from_urls(&a)?,
+ "links_remove" => self.links_remove(&a)?,
+ "object_list" => self.object_list(&a)?,
+ "object_move" => self.object_move(&a)?,
+ "content_list" => self.content_list(&a)?,
+ "page_add_text" => self.page_add_text(&a)?,
+ "page_add_image" => self.page_add_image(&a)?,
+ "content_update" => self.content_update(&a)?,
+ "content_delete" => self.content_delete(&a)?,
+ "content_copy" => self.content_copy(&a)?,
+ "doc_print" => self.doc_print(&a)?,
+ "attachment_add" => {
+ let path = self.resolve(a.str("path")?, false)?;
+ let bytes = read_input(&path)?;
+ let name = match a.opt_str("name")? {
+ Some(n) => n.to_string(),
+ None => path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(),
+ };
+ let mime = pdfcraft_engine::mime_for_name(&name).map(str::to_string);
+ let description = a.opt_str("description")?.map(str::to_string);
+ // Only the document-level keys: an annotation attachment of the same name is not the new one.
+ let before = document_attachment_keys(self.doc(&a)?);
+ let out = self.apply(&a, Edit::AddAttachment { name, bytes, description, mime })?;
+ let key = document_attachment_keys(self.doc(&a)?).into_iter().find(|n| !before.contains(n)).unwrap_or_default();
+ json!({ "name": key, "document": out })
+ }
+ "attachment_delete" => {
+ let edit = Edit::DeleteAttachment { key: a.str("name")?.into() };
+ self.apply(&a, edit)?
+ }
+ "doc_remove_hidden" => self.doc_remove_hidden(&a)?,
+ "doc_audit_redactions" => self.doc_audit_redactions(&a)?,
+ "fill_sign_add" => self.fill_sign_add(&a)?,
+ "fill_sign_date_format" => self.fill_sign_date_format(&a)?,
+ "measure_distance" => self.measurement_add(&a, pdfcraft_engine::measure::Kind::Distance)?,
+ "measure_perimeter" => self.measurement_add(&a, pdfcraft_engine::measure::Kind::Perimeter)?,
+ "measure_area" => self.measurement_add(&a, pdfcraft_engine::measure::Kind::Area)?,
+ "measure_info" => self.measurement_info(&a)?,
+ "measure_list" => self.measurement_list(&a)?,
+ "measure_scale" => self.measurement_scale(&a)?,
+ "measure_snap" => self.measurement_snap(&a)?,
+ "measure_export" => self.measurement_export(&a)?,
+ "comment_list" => self.comment_list(&a)?,
+ "comment_add" => self.comment_add(&a)?,
+ "stamp_custom" => self.stamp_custom(&a)?,
+ "comment_reply" => self.comment_reply(&a)?,
+ "comment_set_status" => self.comment_set_status(&a)?,
+ "sign_list" => self.sign_list(&a)?,
+ "accessibility_check" => self.a11y_check(&a)?,
+ "accessibility_find_untagged" => self.a11y_find_untagged(&a)?,
+ "accessibility_autotag_preview" => self.accessibility_autotag_preview(&a)?,
+ "accessibility_autotag_apply" => self.accessibility_autotag_apply(&a)?,
+ "accessibility_autotag_form_fields" => self.accessibility_autotag_form_fields(&a)?,
+ "ocr_recognize" => self.ocr_recognize(&a)?,
+ "js_run" => self.js_run(&a)?,
+ "js_document_scripts" => self.js_document_scripts(&a)?,
+ "js_set_document_script" => self.js_set_document_script(&a)?,
+ "js_enabled" => self.js_enabled(&a)?,
+ "form_set_script" => self.form_set_script(&a)?,
+ "form_merge_data" => self.form_merge_data(&a)?,
+ "form_actions" => self.form_actions(&a)?,
+ "form_detect_fields" => self.form_detect_fields(&a)?,
+ "doc_compare" => self.doc_compare(&a)?,
+ "action_list" => self.action_list()?,
+ "pdfa_verify" => self.pdfa_verify(&a)?,
+ "print_readiness" => self.print_readiness(&a)?,
+ "doc_export_office" => self.doc_export_office(&a)?,
+ "pdfa_convert" => self.pdfa_convert(&a)?,
+ "standards_remove_id" => {
+ let standard = match a.str("standard")? {
+ "pdfa" => pdfcraft_engine::Standard::PdfA,
+ "pdfx" => pdfcraft_engine::Standard::PdfX,
+ other => return Err(ToolError::InvalidArgs(format!("standard must be pdfa or pdfx, not {other}"))),
+ };
+ self.apply(&a, Edit::RemoveStandardId { standard })?
+ }
+ "action_run" => self.action_run(&a)?,
+ "doc_compare_report" => self.doc_compare_report(&a)?,
+ "doc_compare_mark" => self.doc_compare_mark(&a)?,
+ "form_set_actions" => self.form_set_actions(&a)?,
+ "ocr_status" => self.ocr_status()?,
+ "ocr_recognize_files" => self.ocr_recognize_files(&a)?,
+ "accessibility_report" => self.a11y_report(&a)?,
+ "accessibility_review_record" => self.a11y_review_record(&a)?,
+ "accessibility_review_save" => self.a11y_review_save(&a)?,
+ "accessibility_review_load" => self.a11y_review_load(&a)?,
+ "accessibility_review_html" => self.a11y_review_html(&a)?,
+ "accessibility_fix" => self.a11y_fix(&a)?,
+ "accessibility_figures" => self.accessibility_figures(&a)?,
+ "accessibility_set_alt" => self.accessibility_set_alt(&a)?,
+ "accessibility_tags" => self.accessibility_tags(&a)?,
+ "accessibility_tag_content" => self.accessibility_tag_content(&a)?,
+ "accessibility_set_tag_property" => self.accessibility_set_tag_property(&a)?,
+ "accessibility_mark_tag_artifact" => self.accessibility_mark_tag_artifact(&a)?,
+ "accessibility_move_tag" => self.accessibility_move_tag(&a)?,
+ "sign_id_create" => self.sign_id_create(&a)?,
+ "sign_document" => self.sign_document(&a)?,
+ "sign_keychain_ids" => {
+ #[cfg(target_os = "macos")]
+ let ids: Vec<Value> = pdfcraft_engine::sign::keychain::identities(None)
+ .map_err(failed)?
+ .iter()
+ .map(|id| json!({ "id": pdfcraft_engine::sign::keychain::reference(&id.certificate), "certificate": signing::cert_json(&id.certificate) }))
+ .collect();
+ #[cfg(not(target_os = "macos"))]
+ let ids: Vec<Value> = Vec::new();
+ json!({ "count": ids.len(), "ids": ids })
+ }
+ "sign_windows_ids" => {
+ #[cfg(target_os = "windows")]
+ let (ids, unusable): (Vec<Value>, Vec<Value>) = {
+ let listing = pdfcraft_engine::sign::windows::list().map_err(failed)?;
+ let ids = listing
+ .ids
+ .iter()
+ .map(|id| json!({ "id": pdfcraft_engine::sign::windows::reference(&id.certificate), "certificate": signing::cert_json(&id.certificate) }))
+ .collect();
+ let unusable = listing
+ .unusable
+ .iter()
+ .map(|u| json!({ "subject": u.subject, "sha256": u.fingerprint, "reason": u.reason, "no_private_key": u.no_private_key }))
+ .collect();
+ (ids, unusable)
+ };
+ #[cfg(not(target_os = "windows"))]
+ let (ids, unusable): (Vec<Value>, Vec<Value>) = (Vec::new(), Vec::new());
+ json!({ "count": ids.len(), "ids": ids, "unusable": unusable })
+ }
+ "sign_trust" => self.sign_trust(&a)?,
+ "comment_mark" => self.comment_mark(&a)?,
+ "comment_lock" => self.comment_lock(&a)?,
+ "comments_hide" => self.comments_hide(&a)?,
+ "layer_set" => self.layer_set(&a)?,
+ "view_reading_colors" => self.view_reading_colors(&a)?,
+ "comments_summarize" => self.comments_summarize(&a)?,
+ "comment_edit" => self.comment_edit(&a)?,
+ "comment_delete" => self.comment_delete(&a)?,
+ other => return Err(ToolError::UnknownTool(other.into())),
+ };
+ Ok(vec![Content::Json(out)])
+ }
+ }
diff --cc crates/automation/src/lib.rs
index 5b6a0219f,e1ae0141c..f1910867c
--- a/crates/automation/src/lib.rs
+++ b/crates/automation/src/lib.rs
@@@ -48,70 -48,29 +48,29 @@@ pub use tools::{ToolDef, tools}
use file_access::foreign_share;
use text_cache::TextCache;
- type PageTexts = Vec<(usize, std::result::Result<Arc<PageText>, String>)>;
- const FIND_MAX_WARNINGS: usize = 100;
- const FIND_MAX_WARNING_BYTES: usize = 1024;
+ mod assemble;
+ mod dispatch;
+ mod documents;
+ mod error;
+ mod json;
+ mod pages;
+ mod paths;
+
+ pub use dispatch::Content;
+ pub use error::ToolError;
+ use error::{Result, failed};
+ pub(crate) use json::destination;
+ pub use json::view_rect;
+ use json::{Args, info, summary};
-use pages::{DEFAULT_DPI, MAX_DPI, encode_png};
++use pages::{DEFAULT_DPI, MAX_DPI, encode_png, rendered_content};
+ #[cfg(test)]
+ use pages::{FIND_FIRST_BATCH, FIND_MAX_WARNING_BYTES, FIND_MAX_WARNINGS, extract_parallel};
+ #[cfg(test)]
+ pub(crate) use paths::MAX_INPUT;
+ use paths::{child, lexical, unused, write_atomic};
+ pub(crate) use paths::{read_input, read_input_bytes};
+ #[cfg(test)]
+ use paths::{write_atomic_synced, write_atomic_with};
/// A headless PdfCraft session driven by tool calls.
pub struct Automation {
diff --cc crates/automation/src/pages.rs
index 000000000,d9ea9e5c4..d57b2988c
mode 000000,100644..100644
--- a/crates/automation/src/pages.rs
+++ b/crates/automation/src/pages.rs
@@@ -1,0 -1,363 +1,387 @@@
+ use super::dispatch::Content;
+ use super::error::{Result, ToolError, failed};
+ use super::json::{Args, one_based};
+ use super::*;
+
+ /// Default and maximum resolution for `page_render`.
+ pub(crate) const DEFAULT_DPI: f64 = 96.0;
+
+ pub(crate) const MAX_DPI: f64 = 600.0;
+
+ /// `text_find` extracts this many pages first, doubling each batch up to [`FIND_MAX_BATCH`], so a
+ /// match near the start returns without touching the rest of a long document (#971).
+ pub(crate) const FIND_FIRST_BATCH: usize = 8;
+
+ const FIND_MAX_BATCH: usize = 128;
+
+ type PageTexts = Vec<(usize, std::result::Result<Arc<PageText>, String>)>;
+
+ pub(crate) const FIND_MAX_WARNINGS: usize = 100;
+
+ pub(crate) const FIND_MAX_WARNING_BYTES: usize = 1024;
+
+ impl Automation {
+ // ---- pages and text ----------------------------------------------------------------------
+
+ /// 1-based page list → validated 0-based indices.
+ pub(crate) fn pages(&self, a: &Args, key: &str) -> Result<Vec<usize>> {
+ let n = self.doc(a)?.info.pages.len();
+ let pages = one_based(&a.ints(key)?)?;
+ if pages.is_empty() {
+ return Err(ToolError::InvalidArgs(format!("{key} must list at least one page")));
+ }
+ if let Some(p) = pages.iter().find(|p| **p >= n) {
+ return Err(ToolError::InvalidArgs(format!("page {} is out of range: the document has {n} pages", p + 1)));
+ }
+ Ok(pages)
+ }
+
+ /// A 1-based insertion position (1 = before the first page, n+1 = after the last) → 0-based.
+ pub(crate) fn position(&self, a: &Args, key: &str) -> Result<usize> {
+ self.opt_position(a, key)?.ok_or_else(|| Args::missing(key))
+ }
+
+ /// `position`, but `None` when the key is absent.
+ pub(crate) fn opt_position(&self, a: &Args, key: &str) -> Result<Option<usize>> {
+ let Some(at) = a.opt_int(key)? else { return Ok(None) };
+ let n = self.doc(a)?.info.pages.len();
+ if at < 1 || at as usize > n + 1 {
+ return Err(ToolError::InvalidArgs(format!("{key} must be between 1 and {} (the document has {n} pages)", n + 1)));
+ }
+ Ok(Some(at as usize - 1))
+ }
+
+ pub(crate) fn renderer(&mut self, id: DocId) -> Result<&mut PageRenderer> {
+ let doc = self.session.get(id).ok_or_else(|| failed("no such document"))?;
+ // The document's own view settings: layer visibility, hidden comments, reading colours
+ // (tools that change one drop this cache so the next render reads it again).
+ let config =
+ self.session.render_config(id).unwrap_or_else(|| RenderConfig { password: doc.password.as_deref().map(Arc::from), ..Default::default() });
+ let fresh = || (doc.display.clone(), PageRenderer::new(doc.display.clone(), config.clone()));
+ let entry = self.renderers.entry(id).or_insert_with(fresh);
+ if !Arc::ptr_eq(&entry.0, &doc.display) {
+ *entry = fresh();
+ }
+ Ok(&mut entry.1)
+ }
+
- pub(crate) fn page_render(&mut self, a: &Args) -> Result<Content> {
++ /// The page as a PNG, followed by a JSON part listing the render warnings when there are any,
++ /// so a caller can tell that the picture is incomplete, as `pdfcraft-cli render` does (#4340).
++ pub(crate) fn page_render(&mut self, a: &Args) -> Result<Vec<Content>> {
+ let id = self.doc(a)?.id;
+ let page = self.page(a)?;
+ let dpi = a.opt_num("dpi")?.unwrap_or(DEFAULT_DPI);
+ if !(1.0..=MAX_DPI).contains(&dpi) {
+ return Err(ToolError::InvalidArgs(format!("dpi must be between 1 and {MAX_DPI}")));
+ }
+ let scale = (dpi / 72.0) as f32;
+ let block = a.opt_int("hide_text_block")?;
+ let added = a.opt_int("hide_added_text")?;
+ if block.is_some() && added.is_some() {
+ return Err(ToolError::InvalidArgs("choose only one of hide_text_block and hide_added_text".into()));
+ }
+ let out = if let Some(block) = block {
+ if block < 1 {
+ return Err(ToolError::InvalidArgs("hide_text_block must be a positive paragraph index from text_paragraphs".into()));
+ }
+ // The number names the paragraph text_paragraphs listed, as it does for text_edit.
+ let doc = self.doc(a)?;
+ let index = self.paragraph_index(doc.id, page, block, &doc.text_blocks(page))?;
+ doc.text_edit_preview(page, index).map_err(failed)?.render_background(scale).map_err(failed)?
+ } else if let Some(added) = added {
+ let index = usize::try_from(added)
+ .ok()
+ .and_then(|n| n.checked_sub(1))
+ .ok_or_else(|| ToolError::InvalidArgs("hide_added_text must be a positive added text index from object_list".into()))?;
+ self.doc(a)?.added_text_preview(page, index).map_err(failed)?.render_background(scale).map_err(failed)?
+ } else {
+ self.renderer(id)?.render(RenderRequest { page, kind: RequestKind::Pixels, tile: None, scale, tag: 0, preview: false })
+ };
+ if let Some(e) = out.error {
+ return Err(failed(format!("page {} could not be rendered: {e}", page + 1)));
+ }
- let data = encode_png(out.width, out.height, &out.rgba)?;
- Ok(Content::Png { data, width: out.width, height: out.height })
++ rendered_content(&out, page)
+ }
+
+ /// A page as RGBA pixels, without encoding a PNG. `page` is 1-based and `scale` is pixels
+ /// per point (1.0 = 72 dpi). For an embedder that paints the result straight onto a canvas:
+ /// the `page_render` tool only returns PNG, and encoding one to decode it again is wasted
+ /// work on every scroll. It uses the same renderer, and the same per-document cache, as
+ /// `page_render`.
+ pub fn render_pixels(&mut self, doc: u64, page: usize, scale: f32) -> Result<(u32, u32, pdfcraft_render::Pixels)> {
+ if !(scale.is_finite() && scale > 0.0 && scale <= (MAX_DPI / 72.0) as f32) {
+ return Err(ToolError::InvalidArgs(format!("scale must be between 0 and {}", MAX_DPI / 72.0)));
+ }
+ let id = DocId(doc);
+ let n = self.session.get(id).ok_or_else(|| failed(format!("no open document with id {doc} (see doc_list)")))?.info.pages.len();
+ if page < 1 || page > n {
+ return Err(ToolError::InvalidArgs(format!("page {page} is out of range: the document has {n} pages")));
+ }
+ let out = self.renderer(id)?.render(RenderRequest { page: page - 1, kind: RequestKind::Pixels, tile: None, scale, tag: 0, preview: false });
+ if let Some(e) = out.error {
+ return Err(failed(format!("page {page} could not be rendered: {e}")));
+ }
+ Ok((out.width, out.height, out.rgba))
+ }
+
+ pub(crate) fn page(&self, a: &Args) -> Result<usize> {
+ let n = self.doc(a)?.info.pages.len();
+ let p = a.int("page")?;
+ if p < 1 || p as usize > n {
+ return Err(ToolError::InvalidArgs(format!("page {p} is out of range: the document has {n} pages")));
+ }
+ Ok(p as usize - 1)
+ }
+
+ /// The text of `pages` (0-based), from the cache or extracted in parallel.
+ pub(crate) fn page_texts(&mut self, id: DocId, pages: &[usize]) -> Result<Vec<Arc<PageText>>> {
+ self.page_text_results(id, pages, &mut extract_parallel)?
+ .into_iter()
+ .map(|(p, text)| text.map_err(|e| failed(format!("page {}: {e}", p + 1))))
+ .collect()
+ }
+
+ /// Keep per-page failures until the caller chooses strict or partial-result semantics.
+ pub(crate) fn page_text_results(
+ &mut self,
+ id: DocId,
+ pages: &[usize],
+ extract: &mut impl FnMut(&Arc<Vec<u8>>, Option<Arc<str>>, &[usize]) -> PageTexts,
+ ) -> Result<PageTexts> {
+ let doc = self.session.get(id).ok_or_else(|| failed("no such document"))?;
+ let (bytes, n) = (doc.bytes.clone(), doc.info.pages.len());
+ let password: Option<Arc<str>> = doc.password.as_deref().map(Arc::from);
+ self.texts.prepare(id, &bytes);
+ let mut results = HashMap::new();
+ let mut missing = Vec::new();
+ for &p in pages {
+ if p >= n {
+ results.insert(p, Err("page is out of range".to_owned()));
+ } else if let Some(text) = self.texts.get(id, p) {
+ results.insert(p, Ok(text));
+ } else {
+ missing.push(p);
+ }
+ }
+ missing.sort_unstable();
+ missing.dedup();
+ if !missing.is_empty() {
+ for (p, text) in extract(&bytes, password, &missing) {
+ if let Ok(text) = &text {
+ self.texts.insert(id, p, &bytes, text.clone());
+ }
+ results.insert(p, text);
+ }
+ }
+ // The batch owns its Arcs: evicting a cache entry cannot lose a requested result. A
+ // worker that fails to return a page is an explicit failure, never an empty success.
+ Ok(pages.iter().map(|p| (*p, results.get(p).cloned().unwrap_or_else(|| Err("text extraction returned no result".into())))).collect())
+ }
+
+ pub(crate) fn text_word_count(&self, a: &Args) -> Result<Value> {
+ use pdfcraft_engine::word_count::{self, Counts, Options};
+ let doc = self.doc_to_copy(a)?;
+ let pages = match a.opt_ints("pages")? {
+ Some(_) => self.pages(a, "pages")?,
+ None => (0..doc.info.pages.len()).collect(),
+ };
+ let options = Options {
+ exclude_tables: a.opt_bool("exclude_tables")?.unwrap_or(false),
+ exclude_code: a.opt_bool("exclude_code")?.unwrap_or(false),
+ exclude_images: a.opt_bool("exclude_images")?.unwrap_or(false),
+ };
+ let report = word_count::count(doc.export_source(), &pages, options, |_| true).map_err(failed)?;
+ let counts = |c: Counts| json!({ "words": c.words, "characters": c.characters, "characters_without_spaces": c.characters_without_spaces });
+ Ok(json!({
+ "total": counts(report.total),
+ "pages": report.pages.into_iter().map(|(p, c)| json!({ "page": p + 1, "counts": counts(c) })).collect::<Vec<_>>(),
+ "pages_without_text": report.pages_without_text,
+ "unclassified_structure": report.unclassified_structure,
+ }))
+ }
+
+ pub(crate) fn text_extract(&mut self, a: &Args) -> Result<Value> {
+ let doc = self.doc_to_copy(a)?;
+ let id = doc.id;
+ let pages = match a.opt_ints("pages")? {
+ Some(_) => self.pages(a, "pages")?,
+ None => (0..doc.info.pages.len()).collect(),
+ };
+ // Column select (#740): only the text inside the rectangle, row by row.
+ let rect = a.nums::<4>("rect")?.map(|r| r.map(|v| v as f32));
+ let texts = self.page_texts(id, &pages)?;
+ let text = |t: &PageText| match rect {
+ Some(r) => t.column_text(&t.glyphs_in(r)),
+ None => t.plain_text(),
+ };
+ let out: Vec<Value> = pages.iter().zip(texts).map(|(p, t)| json!({ "page": p + 1, "text": text(&t) })).collect();
+ Ok(json!({ "pages": out }))
+ }
+
+ /// Matches in document order, extracting page text in growing batches and stopping as soon as
+ /// `limit` is reached, so a bounded search doesn't extract (and cache) the whole document
+ /// (#971). One match past the limit supplies `next`. Reached extraction failures are reported
+ /// as bounded warnings; `complete` requires both no continuation and no failed pages.
+ pub(crate) fn text_find(&mut self, a: &Args) -> Result<Value> {
+ self.text_find_with(a, &mut extract_parallel)
+ }
+
+ pub(crate) fn text_find_with(
+ &mut self,
+ a: &Args,
+ extract: &mut impl FnMut(&Arc<Vec<u8>>, Option<Arc<str>>, &[usize]) -> PageTexts,
+ ) -> Result<Value> {
+ let doc = self.doc(a)?;
+ let (id, n) = (doc.id, doc.info.pages.len());
+ let query = a.str("query")?;
+ let limit = usize::try_from(a.opt_int("limit")?.unwrap_or(500).max(1)).unwrap_or(usize::MAX);
+ let start = match a.opt_int("start_page")? {
+ None => 0,
+ Some(p) if p >= 1 && usize::try_from(p).is_ok_and(|p| p <= n) => usize::try_from(p - 1).unwrap_or(0),
+ Some(p) => return Err(ToolError::InvalidArgs(format!("start_page {p} is out of range: the document has {n} pages"))),
+ };
+ let skip = usize::try_from(a.opt_int("skip")?.unwrap_or(0).max(0)).unwrap_or(usize::MAX);
+ let mut matches = Vec::new();
+ let mut next = None;
+ let mut warnings = Vec::new();
+ let mut warning_count = 0usize;
+ let (mut first, mut batch) = (start, FIND_FIRST_BATCH);
+ 'search: while first < n {
+ let end = first.saturating_add(batch).min(n);
+ let pages: Vec<usize> = (first..end).collect();
+ let texts = self.page_text_results(id, &pages, extract)?;
+ for (p, text) in texts {
+ let text = match text {
+ Ok(text) => text,
+ Err(mut error) => {
+ warning_count += 1;
+ if warnings.len() < FIND_MAX_WARNINGS {
+ let end = error.floor_char_boundary(FIND_MAX_WARNING_BYTES.min(error.len()));
+ let truncated = end < error.len();
+ error.truncate(end);
+ warnings.push(json!({ "page": p + 1, "error": error, "error_truncated": truncated }));
+ }
+ continue;
+ }
+ };
+ let skip_here = if p == start { skip } else { 0 };
+ // One extra match proves that the caller needs a continuation. Include skipped
+ // matches on the starting page when bounding the temporary per-page result.
+ let needed = limit.saturating_sub(matches.len()).saturating_add(skip_here).saturating_add(1);
+ for (k, r) in text.find_opts_limited(query, false, false, needed).into_iter().enumerate().skip(skip_here) {
+ if matches.len() == limit {
+ next = Some(json!({ "start_page": p + 1, "skip": k }));
+ break 'search;
+ }
+ matches.push(json!({ "page": p + 1, "text": text.text_of(r.clone()), "rects": text.line_rects(r) }));
+ }
+ }
+ first = end;
+ batch = batch.saturating_mul(2).min(FIND_MAX_BATCH);
+ }
+ let mut out = json!({ "query": query, "count": matches.len(), "complete": next.is_none() && warning_count == 0, "matches": matches,
+ "warnings_truncated": warning_count > warnings.len(), "warning_count": warning_count, "warnings": warnings });
+ if let (Some(next), Some(o)) = (next, out.as_object_mut()) {
+ o.insert("next".into(), next);
+ }
+ Ok(out)
+ }
+
+ pub(crate) fn command_list(&self, a: &Args) -> Result<Value> {
+ let active = match a.opt_int("doc")? {
+ Some(_) => Some(self.doc(a)?.id),
+ None => None,
+ };
+ let filter = a.opt_str("filter")?.unwrap_or("").to_lowercase();
+ let enabled_only = a.opt_bool("enabled_only")?.unwrap_or(false);
+ let list: Vec<Value> = commands::COMMANDS
+ .iter()
+ .map(|c| {
+ json!({
+ "id": c.id,
+ "label": commands::current_label(c, &self.session, active),
+ "menu": c.menu,
+ "shortcut": c.shortcut.map(|s| s.label(cfg!(target_os = "macos"))),
+ "enabled": commands::is_enabled(c, &self.session, active),
+ "tool": tools::tool_for_command(c.id),
+ "params": tools::tool_for_command(c.id).and_then(tools::find).map(|t| &t.input_schema),
+ })
+ })
+ .collect();
+ let list: Vec<Value> = list
+ .into_iter()
+ .filter(|c| {
+ (!enabled_only || c.get("enabled").and_then(Value::as_bool) == Some(true))
+ && (filter.is_empty()
+ || ["id", "label", "menu"].iter().any(|k| c.get(k).is_some_and(|v| v.to_string().to_lowercase().contains(&filter))))
+ })
+ .collect();
+ Ok(json!({ "commands": list }))
+ }
+ }
+
+ /// A rendered raster as PNG, through the engine's encoder (Export ▸ Image writes the same).
+ pub(crate) fn encode_png(width: u32, height: u32, premultiplied: &[u8]) -> Result<Vec<u8>> {
+ pdfcraft_engine::export::encode_png(width, height, premultiplied).map_err(failed)
+ }
+
++/// The output of a page-rendering tool: the PNG of the 0-based `page`, followed by a JSON part
++/// listing its render warnings when there are any. Without warnings it is the PNG alone, as it
++/// always was.
++pub(crate) fn rendered_content(out: &pdfcraft_render::RenderedPage, page: usize) -> Result<Vec<Content>> {
++ let data = encode_png(out.width, out.height, &out.rgba)?;
++ let mut content = vec![Content::Png { data, width: out.width, height: out.height }];
++ if !out.warnings.is_empty() {
++ // Sentences, like the other tools' `warnings`, so command_run can add its own to the list.
++ let warnings: Vec<&str> = out.warnings.iter().map(|&w| render_warning_text(w)).collect();
++ content.push(Content::Json(json!({ "page": page.saturating_add(1), "warnings": warnings })));
++ }
++ Ok(content)
++}
++
++/// What a render warning means, in the words `pdfcraft-cli render` prints for it.
++fn render_warning_text(warning: pdfcraft_render::RenderWarning) -> &'static str {
++ use pdfcraft_render::RenderWarning;
++ match warning {
++ RenderWarning::ContentTruncated => "content past the page's safety budget was skipped",
++ RenderWarning::ColourFallback => "a colour that could not be computed was painted in a fallback colour",
++ }
++}
++
+ /// Extract the text of `pages` (0-based) with one renderer per worker thread.
+ pub(crate) fn extract_parallel(bytes: &Arc<Vec<u8>>, password: Option<Arc<str>>, pages: &[usize]) -> PageTexts {
+ let extract = |r: &mut PageRenderer, p: usize| {
+ let out = r.render(RenderRequest { page: p, kind: RequestKind::Text, tile: None, scale: 1.0, tag: 0, preview: false });
+ match out.error {
+ Some(e) => Err(e),
+ None => Ok(out.text.unwrap_or_default()),
+ }
+ };
+ let config = RenderConfig { password, ..Default::default() };
+ let workers = if cfg!(target_arch = "wasm32") { 1 } else { std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get).min(8) };
+ // Small jobs aren't worth a second parse of the document.
+ if workers == 1 || pages.len() < 8 {
+ let mut r = PageRenderer::new(bytes.clone(), config);
+ return pages.iter().map(|p| (*p, extract(&mut r, *p))).collect();
+ }
+ let next = std::sync::atomic::AtomicUsize::new(0);
+ let mut out: PageTexts = std::thread::scope(|s| {
+ let handles: Vec<_> = (0..workers)
+ .map(|_| {
+ s.spawn(|| {
+ let mut r = PageRenderer::new(bytes.clone(), config.clone());
+ let mut done = Vec::new();
+ loop {
+ let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ let Some(p) = pages.get(i) else { break };
+ done.push((*p, extract(&mut r, *p)));
+ }
+ done
+ })
+ })
+ .collect();
+ handles.into_iter().flat_map(|h| h.join().unwrap_or_default()).collect()
+ });
+ out.sort_by_key(|(p, _)| *p);
+ out
+ }