mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-13 01:41:18 +00:00
* M10.20: plan Unicode text for OCR output in Type3 faces (fonts, edit) OCR output used to flatten every character beyond WinAnsi to a silent `?`. This adds the two pieces that let the OCR layers write the recognized words as they are; the OCR code that uses them follows in M10.21. pdfcraft-fonts - `TextPlan::for_words` maps each word to the standard Helvetica when WinAnsi covers it, else to Type3 fonts drawn from the craft-fonts faces (the Japanese document face first, then the rest of the build input). Characters no face has a glyph for are counted (`missing`), never dropped silently. At most 240 characters per font and 2400 per plan, so one hostile word cannot balloon. - Which face has which character is asked once per (face, character) for a plan, not once per word. - `face_has`, `craft_glyph`, `win_ansi_char`, `fits_win_ansi` are public for it. pdfcraft-edit - `add_type3_fonts` / `stamp_with_fonts` register a plan's fonts on a page under a free `PCOcr`, `PCOcr1`, ... name, build each font once and share the object with later pages. A shared face is registered under a free name on a later page that already has a font of the name it was given first, instead of replacing that page's own font. - The fonts carry real advances and a ToUnicode CMap, so extraction reads the text back. pdfcraft-redact - A test shows a hand-built Type3 text layer (the shape the OCR output writes) is redacted like any other text and the verifier passes. Tests: pdfcraft-fonts 62 passed, pdfcraft-edit 129 passed, pdfcraft-redact 33 passed; clippy -D warnings clean on the three crates. The name-reuse and the redaction tests build their fonts by hand, so they run without the craft-fonts build input; the tests that plan real faces skip without it and were not run in this commit's own check. * M12: give the hostile-XFA open test more room on a loaded machine The test opens a form whose calculate script sets a value 50 000 times, against the one-second budget of an unasked script and a five-second wall-clock bound on the open. Both sit close to what a machine running several suites at once can take, so the test can fail without anything being wrong. It now runs 20 000 iterations (the total is 19999) and allows 30 seconds, which still fails for a script that hangs or is not capped. Test-only; unrelated to the OCR work in this series, so it is a commit of its own. Tests: both hostile_xfa tests in pdfcraft-engine pass. * M10.21: pluggable OCR engines, preprocessing, review sessions and the OCR tools (ocr, engine, create, automation) Scan & OCR gets a second engine, optional raster clean-up, a review step before anything is applied, editable output and a batch mode, headlessly first: the interface for them follows in M10.22. pdfcraft-ocr - A `Recognizer` trait with two engines: the built-in ocrs (moved out of lib.rs) and the installed tesseract program, run as a fenced external process: fixed argv, allow-listed language codes, a private temp directory (mode 0700 on Unix), 120 s timeout, 64 MiB output cap, 50 000-word reading cap. A location from PDFCRAFT_TESSERACT is validated (absolute, existing, executable) and an invalid one fails closed instead of falling back to the PATH. `--list-langs` and `--version` are read from stdout or, for older versions, stderr. - Strategies: PrimaryOnly, ConfidenceWeighted (the second engine joins when the first reading's mean is under 70 or unknown) and a ROVER merge (higher confidence wins per word; against an ocrs word of unknown confidence a tesseract word wins only at 70 or more; capped at 100 M comparisons; a failing partner degrades to the primary with a note). - Preprocessing: opt-in auto-rotate, deskew, denoise and binarize on the RGBA raster, each with an exact inverse so word boxes map back to the scan. The 300 dpi normalization the steps are calibrated for runs only when a step is on: with every step off the render is read as rendered, as before. (Enlarging a 72 dpi render 4x on its own made the models read "due: €250" as "ue: ?250".) `OcrImage` is validated by construction (side and 64 MP area caps, checked before any buffer is allocated, including by the resize). - Confidence bands (High >= 90, Medium 70-89, Low < 70) drive review. pdfcraft-engine, pdfcraft-create - `OcrSettings` gains engine, strategy, the four steps, output mode, region, the skip flags and the low-confidence threshold; Automatic reads confidence weighted unless a strategy is chosen. - A recognition is a session: the document guard refuses a stale result with its reason, pages can be applied one at a time (the guard is taken again after each applied page), and applying is one undoable step. A per-language dictionary (a plain-text file: words added from any document stay on disk after it closes). - Output is searchable (an invisible layer) or a NEW editable document of visible text at the recognized boxes (`create::from_contents`); words beyond WinAnsi use the Type3 faces of M10.20 in both, with the shared plan across pages. - Engine discovery never blocks the interface: results are cached, a stale answer is shown while a background probe retries, and the models search and `--version` are cached. Batch: pages or files that already have text are skipped unless forced, and inputs sharing a file name are found up front. pdfcraft-automation - ocr_recognize (engine, strategy, steps, region, output mode), ocr_words (with a retained review handle), ocr_review_apply, ocr_dictionary, ocr_status and ocr_recognize_files. The editable `out` must be a new file inside --root and is refused for a searchable run (it used to be ignored), two batch inputs of one file name are refused before anything is read, and the batch folder is checked for existing outputs. pdfcraft-ui-egui - Only what the new API needs: the Recognize Text dialog reads with `recognizers(&settings)`. `ocr.correct` is registered with its label in every catalog; the screen itself is M10.22. Tests: pdfcraft-ocr 78 passed; pdfcraft-create passed; pdfcraft-engine and pdfcraft-automation 450 passed and 1 failed (the automation lib 17, automation.rs 157, hostile_args 2 among them); pdfcraft-ui-egui lib + ocr + control tests 391 passed; clippy -D warnings clean on the five crates. The one failure is in a test this commit does not change: recognize_text_makes_a_scanned_page_searchable fails where the models are installed (they read "quick" as "auick" at that render size); the roadmap on main already records it. The real tesseract binary ran in tesseract::tests::recognize_runs_the_installed_binary (tesseract 5.4.0, PATH). * M10.22: OCR Verify screen, the Recognize Text options and the interface catalogs (ui-egui) Scan & OCR ▸ Correct recognized text: a full-window screen where a recognition is reviewed before anything is applied, plus the dialog and batch options for the engines of M10.21. OCR Verify screen - Four panes: the page list, the source page with confidence-coloured word boxes (click to select, drag to draw a region), the recognized text, and a zoom plus word inspector. Corrections commit on Enter (an empty commit removes the word), Remove/Restore, "+ Dictionary" (a per-language file in the preferences folder, so a word added from a document stays on disk after it closes), per-word and per-page Verified, F8 / Shift+F8 walk the low-confidence words, Ctrl+T marks the page reviewed, Ctrl+Tab moves the pane focus, and review edits undo within the panel. - Re-OCR of the whole page or a drawn region. A region re-read moves the review marks of the words it keeps to their new positions and selects the first new word; a whole-page re-read clears that page's marks (and the undo brings them back). The region outline is scaled onto the pane, not mapped through user space again, so it is right on rotated and cropped pages. - Accept / Reject per page or whole. Pages accepted one at a time each apply once (the document guard is taken again after each applied page; an edit made elsewhere in between is still refused with its reason), and Accept in searchable mode is one undoable step. Editable mode writes a NEW `<name>_ocr.pdf` through a save dialog and never the source. A real Cancel keeps the pages already read. - The engine and strategy menus disable a choice that cannot run and say why; the check never waits on a process (a background probe fills a cache), and choosing Automatic moves "Primary only" to Confidence weighted. Recognize Text dialog and batch - Engine, strategy, the four clean-up steps and the skip flags are remembered with the other choices (a saved Automatic without a strategy reads confidence weighted). The batch refuses two inputs of one file name and existing outputs before reading anything, notes low-confidence pages at the settings' threshold, and its toast has a Cancel (the file in flight finishes). Catalogs and attribution - The 90 new interface strings are in all 21 catalogs (the Lithuanian ones were written without native review, and the Bulgarian ones avoid U+045D, which the bundled monospace face lacks). ATTRIBUTION.toml carries the new hashes, a note on the Lithuanian entries and the README screenshot; ATTRIBUTION.md is regenerated. - README: a screenshot of the Verify screen, taken with `cargo xtask screenshots` from a synthetic scan (`shot --ocr-verify on`) built from allowed assets; the page reads it with the real engines. Tests: pdfcraft-ui-egui lib 373 passed (1 ignored: the Windows hinting measurement probe), tests ocr 6, control 46, fonts 9; clippy -D warnings clean on the engine, ui-egui, pdfcraft and xtask packages; `cargo xtask assets` ok. The review state machine has unit tests for every fix above; each was seen failing with its fix reverted. * M10.23: parity entries, roadmap and localization notes for the OCR workflow Records what M10.20-M10.22 ship, and what they do not. parity/acrobat-features.toml - Status changes against main: ocr.rotation-detection and ocr.deskew planned -> shipped (the opt-in steps of M10.21, with their tests); ocr.correct-recognized- text and ocr.ocr-engines planned -> partial. Other Area I entries get their evidence and notes brought up to date. - ocr.correct-recognized-text: the evidence includes the one-page-at-a-time accept and the region re-read tests; the notes say what the headless tools cover (ocr_words with a retained review, ocr_review_apply through the same stale- document guard, ocr_dictionary) and what is still UI-only (suggestions, per-word Verified marks, region re-reads). - ocr.ocr-engines stays partial rather than shipped: part of its evidence is conditional (the real-binary test runs only where tesseract is installed and skips elsewhere, and the process-level tests are Unix-only). It cites the tests that always run (location validation and the fail-closed rule, --list-langs parsing from stdout or stderr, the merge rules, the default strategy) and says which part is not proven everywhere. - `cargo xtask parity`: area I 32.7% -> 44.2% weighted (5 -> 7 shipped, 7 -> 9 partial); overall 52.8% -> 53.0% shipped and 58.2% -> 58.5% weighted. ROADMAP.md - One progress-log entry (2026-10-13, newest first) and the banner line. It says the catalogs are 21 with the Lithuanian additions not natively reviewed, that the end-to-end redaction test of the Type3 layer needs the craft-fonts build input, and that recognize_text_makes_a_scanned_page_searchable still fails where the models are installed, as main's log already records. docs/localization.md, docs/localization-parity.md - The Lithuanian entry count (2,578 -> 2,668) and the counts of the other catalogs that carry entry counts are re-measured with the 90 new entries; the Lithuanian OCR entries are marked as not natively reviewed. Checked: `cargo xtask parity` (numbers above, evidence tests exist) and `cargo xtask assets` pass on this tree. * M10.24: ocr: apply a late result only to the document it was read from The Recognize Text dialog closes on Go, so the document can be edited, or have text redacted, while a long run is still reading. The result was applied without the stale check the tools and the Verify screen use, so words read before a redaction could be written back as invisible text. The run now keeps its guard and applies through the checked path. Accept with the editable output in the Verify screen had the same gap (Exit keeps the review open): it now refuses when the document changed since the review started. Both new tests fail with their guard switched off. * M10.25: ocr: ocr_dictionary takes dictionary file names only Updating sorts, lower-cases and rewrites the file it is given, so pointed at .env or a README it would corrupt it. The tool now accepts only ocr-dictionary-<language>.txt style names, for reading as well as writing. The size-limit test used a file name the rule now refuses first, so it renamed its fixture to keep testing the size limit. The new test fails with the rule switched off. * M10.26: pt-BR: keep (s) on the word counts and say mecanismo for engine Two review fixes from a native speaker: the two word(s) lines had lost their (s), so a count of 1 read as 1 palavras, and engine was translated both as motor and mecanismo. Mecanismo is the Brazilian word for a software engine and two lines already used it, so the other six now match. * M10: fit the rebased OCR workflow to current main's structure * Fix OCR workflow issues from the review Honor the Recognize dialog's editable-output choice: its result opens in the review screen (whose Accept writes the editable document) instead of an invisible layer. Make page acceptance idempotent: a second Accept entire page never appends the OCR layer twice and keeps later corrections. Move page and region re-OCR onto a worker thread with a status line and a cancel that discards the reading. Skip the Tesseract probe for jobs that name their engines. Bound Tesseract's stderr while the child runs, not only after it exits. Surface the editable output's substitution notes in the tools' and the run's results. Also repair the OCR batch tool against the current structure: inputs are held to the read root again, and a batch whose inputs share an output name is refused before the output folder is created, compared the way the default filesystem compares names; and the editable writer test's word stays inside the fixture's pages. * Apply pre-review fixes to the OCR workflow Hold the automation OCR tools to the document's copying permission, as the other text tools are: the recognized text is left out of the results when copying is forbidden, and the paths that carry the words out of the document (the editable output, the review word list) are refused. Mark the ocr_dictionary tool as overwriting its path. Close the searchable layer's graphics state around its invisible text so the rendering mode cannot leak into later content. Read the OCR user dictionary folder from the portable data folder in portable mode, like the recovery store does. Resolve the attribution manifest's leftover conflict markers and regenerate the generated attribution. Describe the review flow, the searchable layer's Type3 fonts and the parity evidence references accurately, and drop an undeclared copy of the recovery module. * OCR: reject readings that exceed the word limit Assisted-by: OpenAI Codex * OCR: new documents go through the page tree builder main added Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * OCR: review notes and lints The editable output refuses a file created during recognition. ocr_words takes skip_text_pages and force_ocr. Automatic OCR checks the engine it will use. The stderr test stays below the kill cap, and a failed --ocr-verify shot exits with an error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * OCR: restore the Verify screenshot attribution, follow the moved constants The merge dropped the screenshot row with the translation rows. The model filename constants live in ocrs.rs now, so the license check reads them there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Attribution: the Verify screenshot entry goes after the header comment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Attribution: the screenshot row in the table follows the manifest order Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: eliets2 <287273517+eliets2@users.noreply.github.com> Co-authored-by: FredApps <FredApps@users.noreply.github.com> Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
934 lines
48 KiB
Rust
934 lines
48 KiB
Rust
use super::{
|
|
AddedContent, Arc, Edit, EditError, FieldValue, ImageEdit, NewAnnotation, Shape, a11y, copy_image, decode_data_file, embed_image, fit_picture,
|
|
image_content_type, is_xfa_data_file, js, mark_source, measure, model_poster, open_source, strip_xfa, unrestricted, xfa, xfa_after_data_change,
|
|
};
|
|
use crate::SourceProblem;
|
|
use crate::sources::{comment_source, import_pdf_comments};
|
|
use pdfcraft_content::Matrix;
|
|
|
|
/// Whether the opening password allows an edit (§7.6.4.2, Table 22).
|
|
pub(crate) fn check_permission(edit: &Edit, p: &pdfcraft_cos::Permissions) -> Result<(), EditError> {
|
|
match edit {
|
|
Edit::RotatePages { .. }
|
|
| Edit::DeletePages { .. }
|
|
| Edit::MovePages { .. }
|
|
| Edit::ReversePages { .. }
|
|
| Edit::InsertBlankPage { .. }
|
|
| Edit::DuplicatePages { .. }
|
|
| Edit::ReplacePages { .. }
|
|
| Edit::SetPageBox { .. }
|
|
| Edit::SetTransition { .. }
|
|
| Edit::InsertPagesFrom { .. }
|
|
// "Assemble the document: insert, rotate or delete pages and create bookmarks" (Table 22).
|
|
| Edit::AddBookmark { .. }
|
|
| Edit::RenameBookmark { .. }
|
|
| Edit::DeleteBookmark { .. }
|
|
| Edit::MoveBookmark { .. }
|
|
| Edit::SetBookmarkPage { .. }
|
|
| Edit::SetBookmarkColor { .. }
|
|
| Edit::SetBookmarkStyle { .. }
|
|
| Edit::ImportBookmarks { .. }
|
|
| Edit::BookmarksFromStructure
|
|
| Edit::NumberPages { .. } => {
|
|
if p.assemble() {
|
|
Ok(())
|
|
} else {
|
|
Err(EditError::NotPermitted("page changes"))
|
|
}
|
|
}
|
|
// Creates bookmarks (assembly), and their titles are the document's text, copied out of it
|
|
// (Table 22, bit 5), as an export of it would be.
|
|
Edit::BookmarksFromTextStyles { .. } => {
|
|
if !p.assemble() {
|
|
Err(EditError::NotPermitted("page changes"))
|
|
} else if !p.copy() {
|
|
Err(EditError::NotPermitted("copying text and images"))
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
Edit::AddMeasurement(_)
|
|
| Edit::AddAnnotation(_)
|
|
| Edit::PasteComments { .. }
|
|
| Edit::AddCustomStamp { .. }
|
|
| Edit::AddModel3d { .. }
|
|
| Edit::DeleteAnnotation { .. }
|
|
| Edit::SetAnnotationContents { .. }
|
|
| Edit::ReplyToAnnotation { .. }
|
|
| Edit::SetAnnotationStatus { .. }
|
|
| Edit::MarkAnnotation { .. }
|
|
| Edit::ReplaceText { .. }
|
|
| Edit::EraseInk { .. }
|
|
| Edit::LockAnnotation { .. }
|
|
| Edit::MoveAnnotation { .. }
|
|
| Edit::ReshapeAnnotation { .. }
|
|
| Edit::ResizeAnnotation { .. }
|
|
| Edit::StyleAnnotation { .. }
|
|
| Edit::FillAnnotation { .. }
|
|
| Edit::PartOpacity { .. }
|
|
| Edit::FontSizeAnnotation { .. }
|
|
| Edit::BorderColorAnnotation { .. }
|
|
| Edit::BorderShapeAnnotation { .. }
|
|
| Edit::SetAnnotationInfo { .. }
|
|
| Edit::SetMeasurementScale { .. } => {
|
|
if p.annotate() {
|
|
Ok(())
|
|
} else {
|
|
Err(EditError::NotPermitted("comments"))
|
|
}
|
|
}
|
|
Edit::SetFieldValue { .. } | Edit::ResetForm { .. } | Edit::SetFieldImage { .. } | Edit::ApplyScriptChanges { .. } | Edit::XfaEvent { .. } => {
|
|
if p.fill_forms() {
|
|
Ok(())
|
|
} else {
|
|
Err(EditError::NotPermitted("filling in form fields"))
|
|
}
|
|
}
|
|
Edit::Protect(_) | Edit::RemoveProtection => {
|
|
if unrestricted(p) {
|
|
Ok(())
|
|
} else {
|
|
Err(EditError::NotPermitted("changing security"))
|
|
}
|
|
}
|
|
// Converting to PDF/A takes the encryption off (PDF/A forbids it), so on an encrypted
|
|
// document it is a change of security (#3921).
|
|
Edit::ConvertPdfA { .. } => {
|
|
if unrestricted(p) {
|
|
Ok(())
|
|
} else {
|
|
Err(EditError::NotPermitted("removing the encryption, which PDF/A forbids"))
|
|
}
|
|
}
|
|
Edit::SetInfo { .. }
|
|
| Edit::AddHeaderFooter { .. }
|
|
| Edit::AddWatermark { .. }
|
|
| Edit::AddBackground { .. }
|
|
| Edit::RemoveMarks { .. }
|
|
| Edit::AddField { .. }
|
|
| Edit::ApplyRedactions { .. }
|
|
| Edit::AddText { .. }
|
|
| Edit::AddImage { .. }
|
|
| Edit::UpdateContent { .. }
|
|
| Edit::MoveObjects { .. }
|
|
| Edit::DeleteContent { .. }
|
|
| Edit::ReplaceImage { .. }
|
|
| Edit::PasteContent { .. }
|
|
| Edit::ClearRedactions
|
|
| Edit::RemoveHidden { .. }
|
|
| Edit::ImportData { .. }
|
|
| Edit::AddLink { .. }
|
|
| Edit::SetLink { .. }
|
|
| Edit::DeleteLink { .. }
|
|
| Edit::RemoveLinks { .. }
|
|
| Edit::AddLinks { .. }
|
|
| Edit::Sanitize
|
|
| Edit::XfaToStatic
|
|
| Edit::SetFieldProps { .. }
|
|
| Edit::DeleteField { .. }
|
|
| Edit::DeleteWidget { .. }
|
|
| Edit::DuplicateField { .. }
|
|
| Edit::SetTabOrder { .. }
|
|
| Edit::MoveInTabOrder { .. }
|
|
| Edit::SetCalculationOrder { .. }
|
|
| Edit::SetInitialView(_)
|
|
| Edit::AddAttachment { .. }
|
|
| Edit::DeleteAttachment { .. }
|
|
| Edit::AutoTagText
|
|
| Edit::SetAltText { .. }
|
|
| Edit::AutoTagFormFields
|
|
| Edit::SetTagProperty { .. }
|
|
| Edit::MoveTag { .. }
|
|
| Edit::MarkDecorative { .. }
|
|
| Edit::MarkTagArtifact { .. }
|
|
| Edit::AddOcrText { .. }
|
|
| Edit::EnhanceScan { .. }
|
|
| Edit::SetDocumentScript { .. }
|
|
| Edit::SetFieldScript { .. }
|
|
| Edit::RemoveStandardId { .. }
|
|
| Edit::EditTextLine { .. }
|
|
| Edit::EditTextBlock { .. }
|
|
| Edit::RegisterTextFont { .. }
|
|
| Edit::EditPageImage { .. }
|
|
| Edit::Flatten { .. }
|
|
| Edit::FlattenFillSign => {
|
|
if p.modify() {
|
|
Ok(())
|
|
} else {
|
|
Err(EditError::NotPermitted("changes to the document"))
|
|
}
|
|
}
|
|
Edit::Batch { edits, .. } => edits.iter().try_for_each(|e| check_permission(e, p)),
|
|
}
|
|
}
|
|
|
|
/// Dates and unique ids stamped onto what an edit creates.
|
|
pub(crate) struct EditCtx {
|
|
/// Field scripts run (`None`: JavaScript is off) and what they produced.
|
|
pub(crate) js: Option<js::JsRunner>,
|
|
/// A laid-out XFA form's template (`None`: not an XFA form, or JavaScript is off) and what
|
|
/// its scripts produced.
|
|
pub(crate) xfa: Option<Arc<pdfcraft_xfa::model::Template>>,
|
|
pub(crate) xfa_out: js::JsOutput,
|
|
/// The datasets stream this edit wrote (later writes in the edit replace it in place).
|
|
pub(crate) xfa_datasets: Option<pdfcraft_cos::ObjRef>,
|
|
/// A form script ran too long and was abandoned: the document's scripts go off.
|
|
pub(crate) xfa_ran_away: bool,
|
|
/// What the XFA scripts' time limits are multiplied by ([`Session::with_xfa_time_scale`]).
|
|
pub(crate) xfa_time_scale: u32,
|
|
/// The edit took the XFA out of the form: what went with it, for the document's warnings.
|
|
pub(crate) xfa_removed: Vec<String>,
|
|
/// Pages in the document, for `xfa.layout.pageCount()`.
|
|
pub(crate) pages: usize,
|
|
pub(crate) date: Option<String>,
|
|
/// Today in local time, for date tokens.
|
|
pub(crate) today: (i64, u32, u32),
|
|
pub(crate) seed: u64,
|
|
pub(crate) count: u64,
|
|
/// Platform CSPRNG (`getrandom::fill`). Tests replace it so Protect can fail closed.
|
|
pub(crate) rng: fn(&mut [u8]) -> Result<(), getrandom::Error>,
|
|
/// Fields the edit renamed to keep imported names separate (old → new), for the repair
|
|
/// log and the undo label.
|
|
pub(crate) renamed_fields: Vec<(String, String)>,
|
|
/// The overlay streams the edit's applied redactions added, which alone the renderer's check
|
|
/// leaves out.
|
|
pub(crate) redaction_overlays: Vec<pdfcraft_cos::ObjRef>,
|
|
}
|
|
|
|
impl EditCtx {
|
|
pub(crate) fn new(now: Option<i64>, salt: u64) -> Self {
|
|
let mut seed = 0x9E37_79B9_7F4A_7C15u64 ^ salt;
|
|
if let Some(t) = now {
|
|
seed ^= (t as u64).rotate_left(17);
|
|
}
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
if now.is_some() {
|
|
// Real clock: mix in sub-second time so ids from two sessions don't collide.
|
|
seed ^= std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.subsec_nanos() as u64).unwrap_or(0) << 32;
|
|
}
|
|
Self {
|
|
js: None,
|
|
xfa: None,
|
|
xfa_out: Default::default(),
|
|
xfa_datasets: None,
|
|
xfa_ran_away: false,
|
|
xfa_time_scale: 1,
|
|
xfa_removed: Vec::new(),
|
|
pages: 0,
|
|
date: now.map(pdfcraft_cos::pdf_date),
|
|
today: (1970, 1, 1),
|
|
seed,
|
|
count: 0,
|
|
rng: getrandom::fill,
|
|
renamed_fields: Vec::new(),
|
|
redaction_overlays: Vec::new(),
|
|
}
|
|
}
|
|
|
|
/// 32 bytes of entropy for new encryption keys, salts and generated owner passwords from the
|
|
/// platform CSPRNG. Fails closed when secure randomness is unavailable.
|
|
fn entropy(&mut self) -> Result<[u8; 32], EditError> {
|
|
let mut out = [0u8; 32];
|
|
(self.rng)(&mut out).map_err(|e| EditError::Protection(format!("no secure randomness available: {e}")))?;
|
|
if out == [0u8; 32] {
|
|
return Err(EditError::Protection("no secure randomness available".into()));
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// A fresh `/NM`: a random-looking UUID (version 4 layout) from a splitmix64 stream.
|
|
fn meta(&mut self) -> pdfcraft_annot::Meta {
|
|
let mut next = || {
|
|
self.count += 1;
|
|
let mut z = self.seed.wrapping_add(self.count.wrapping_mul(0x9E37_79B9_7F4A_7C15));
|
|
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
|
|
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
|
|
z ^ (z >> 31)
|
|
};
|
|
let (a, b) = (next(), next());
|
|
let id = format!(
|
|
"{:08x}-{:04x}-4{:03x}-{:04x}-{:012x}",
|
|
a >> 32,
|
|
(a >> 16) & 0xffff,
|
|
a & 0xfff,
|
|
0x8000 | (b >> 48) & 0x3fff,
|
|
b & 0xffff_ffff_ffff
|
|
);
|
|
pdfcraft_annot::Meta { date: self.date.clone(), id }
|
|
}
|
|
}
|
|
|
|
fn run_edit_page_image(doc: &mut pdfcraft_cos::Document, page: usize, index: usize, change: &ImageEdit) -> Result<(), EditError> {
|
|
match change {
|
|
ImageEdit::Move(to) if !to.iter().all(|v| v.is_finite()) => {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid position".into())));
|
|
}
|
|
ImageEdit::Transform(t) => {
|
|
let [a, b, c, d, _, _] = *t;
|
|
let determinant = a * d - b * c;
|
|
if !t.iter().all(|v| v.is_finite()) || !determinant.is_finite() || determinant.abs() < 1e-9 {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("the image transform must be finite and nonsingular".into())));
|
|
}
|
|
}
|
|
ImageEdit::Crop(keep) if !keep.iter().all(|v| v.is_finite()) => {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid crop".into())));
|
|
}
|
|
_ => {}
|
|
}
|
|
let img = pdfcraft_edit::page_images(doc, page)?
|
|
.into_iter()
|
|
.nth(index)
|
|
.ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid(format!("page {} has no image {}", page + 1, index + 1))))?;
|
|
let c = match change {
|
|
ImageEdit::Move(to) => pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::rect_to_rect(img.rect, *to)),
|
|
ImageEdit::Transform(t) => pdfcraft_edit::ImageChange::Transform(*t),
|
|
ImageEdit::Rotate(q) => {
|
|
pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::turn_about_centre(img.visible_rect.unwrap_or(img.rect), *q, false, false))
|
|
}
|
|
ImageEdit::RotateBy(degrees) => {
|
|
let t = pdfcraft_edit::turn_by_degrees(img.visible_rect.unwrap_or(img.rect), *degrees)
|
|
.ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid("the rotation angle must be a finite number".into())))?;
|
|
pdfcraft_edit::ImageChange::Transform(t)
|
|
}
|
|
ImageEdit::Flip { horizontal } => pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::turn_about_centre(
|
|
img.visible_rect.unwrap_or(img.rect),
|
|
0,
|
|
*horizontal,
|
|
!*horizontal,
|
|
)),
|
|
ImageEdit::Replace { name, bytes } => {
|
|
if img.is_form {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("grouped Form artwork cannot be replaced with a bitmap".into())));
|
|
}
|
|
pdfcraft_edit::ImageChange::Replace(embed_image(doc, name, bytes)?.0)
|
|
}
|
|
ImageEdit::Crop(keep) => pdfcraft_edit::ImageChange::Crop(*keep),
|
|
ImageEdit::Delete => pdfcraft_edit::ImageChange::Delete,
|
|
ImageEdit::Arrange(order) => pdfcraft_edit::ImageChange::Arrange(*order),
|
|
ImageEdit::RemoveCrop => pdfcraft_edit::ImageChange::RemoveCrop,
|
|
};
|
|
let page_model = pdfcraft_model::pages(doc)
|
|
.into_iter()
|
|
.nth(page)
|
|
.ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid(format!("page {} not found", page + 1))))?;
|
|
let target_stream_obj = match page_model.dict.get(b"Contents") {
|
|
None => None,
|
|
Some(c) => match &*doc.resolve(c) {
|
|
pdfcraft_cos::Object::Array(a) => a.get(img.stream).and_then(|o| o.as_ref()),
|
|
_ if img.stream == 0 => c.as_ref(),
|
|
_ => None,
|
|
},
|
|
};
|
|
|
|
let added_items: Vec<(usize, pdfcraft_edit::Added)> = pdfcraft_edit::list_added(doc).into_iter().filter(|a| a.page == page).enumerate().collect();
|
|
// Ownership belongs to the drawing stream, not the XObject: original and added images may
|
|
// show the same resource without sharing an editable item.
|
|
let shows_image = |a: &pdfcraft_edit::Added| img.object.is_some_and(|t| matches!(&a.content, AddedContent::Image(ai) if ai.image == t));
|
|
let matched_added = added_items.iter().find(|(_, a)| target_stream_obj == Some(a.obj) && (img.object.is_none() || shows_image(a)));
|
|
|
|
let mut ai_to_update = None;
|
|
if let Some((added_idx, added_item)) = matched_added
|
|
&& let AddedContent::Image(ai) = added_item.content.clone()
|
|
{
|
|
let has_active_crop = ai.crop.iter().any(|v| v.abs() > 1e-6)
|
|
|| img.visible_rect.is_some_and(|v| {
|
|
(v[0] - img.rect[0]).abs() > 1e-4
|
|
|| (v[1] - img.rect[1]).abs() > 1e-4
|
|
|| (v[2] - img.rect[2]).abs() > 1e-4
|
|
|| (v[3] - img.rect[3]).abs() > 1e-4
|
|
});
|
|
let non_axis_transform = matches!(change, ImageEdit::Transform([a, b, c, d, _, _])
|
|
if !((b.abs() < 1e-5 && c.abs() < 1e-5) || (a.abs() < 1e-5 && d.abs() < 1e-5)));
|
|
let should_promote = has_active_crop
|
|
|| non_axis_transform
|
|
|| matches!(change, ImageEdit::Crop(_) | ImageEdit::RemoveCrop | ImageEdit::RotateBy(_) | ImageEdit::Arrange(_) | ImageEdit::Delete);
|
|
|
|
if should_promote {
|
|
doc.update_dict(added_item.obj, |d| {
|
|
d.remove(b"PCAdded");
|
|
d.remove(b"PCMark");
|
|
})
|
|
.map_err(|e| EditError::Edit(pdfcraft_edit::EditError::Invalid(e.to_string())))?;
|
|
} else {
|
|
ai_to_update = Some((*added_idx, ai));
|
|
}
|
|
}
|
|
|
|
if let Some((added_idx, mut ai)) = ai_to_update {
|
|
let view = Matrix(page_model.view_matrix(doc));
|
|
let to_display = view.invert().ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid page transform".into())))?;
|
|
|
|
match change {
|
|
ImageEdit::Move(to) => {
|
|
ai.rect = to_display.bbox(*to);
|
|
pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
|
|
}
|
|
ImageEdit::Transform(_) | ImageEdit::Flip { .. } => {
|
|
let pdfcraft_edit::ImageChange::Transform(t) = c else {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("expected transform".into())));
|
|
};
|
|
let t_display = view.then(&Matrix(t)).then(&to_display);
|
|
let [ta, tb, tc, td, _, _] = t_display.0;
|
|
let is_parallel = tb.abs() < 1e-5 && tc.abs() < 1e-5;
|
|
let is_perpendicular = ta.abs() < 1e-5 && td.abs() < 1e-5;
|
|
|
|
let (quarters_ccw, disp_flip_h, disp_flip_v) = if is_parallel {
|
|
(0, ta < 0.0, td < 0.0)
|
|
} else if is_perpendicular {
|
|
match (tb > 0.0, tc > 0.0) {
|
|
(true, false) => (1, false, false),
|
|
(false, true) => (3, false, false),
|
|
(true, true) => (1, false, true),
|
|
(false, false) => (1, true, false),
|
|
}
|
|
} else {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid(
|
|
"non-axis-aligned transforms are not supported for added images".into(),
|
|
)));
|
|
};
|
|
let (toggle_h, toggle_v) = if ai.rotation % 2 == 1 { (disp_flip_v, disp_flip_h) } else { (disp_flip_h, disp_flip_v) };
|
|
ai.flip_h ^= toggle_h;
|
|
ai.flip_v ^= toggle_v;
|
|
ai.rotation = (ai.rotation + quarters_ccw) % 4;
|
|
ai.rect = t_display.bbox(ai.rect);
|
|
pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
|
|
}
|
|
ImageEdit::Rotate(q) => {
|
|
let k = ((4 - q.rem_euclid(4)) % 4) as u8;
|
|
ai.rotation = (ai.rotation + k) % 4;
|
|
let r = ai.rect;
|
|
let (cx, cy) = ((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0);
|
|
if k % 2 == 1 {
|
|
let (w, h) = (r[2] - r[0], r[3] - r[1]);
|
|
ai.rect = [cx - h / 2.0, cy - w / 2.0, cx + h / 2.0, cy + w / 2.0];
|
|
}
|
|
pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
|
|
}
|
|
ImageEdit::Replace { .. } => {
|
|
let pdfcraft_edit::ImageChange::Replace(new_image) = c else {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("expected replace".into())));
|
|
};
|
|
ai.image = new_image;
|
|
pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
|
|
}
|
|
ImageEdit::Crop(_) | ImageEdit::RemoveCrop | ImageEdit::RotateBy(_) | ImageEdit::Arrange(_) | ImageEdit::Delete => {
|
|
pdfcraft_edit::change_image(doc, page, index, &c)?;
|
|
}
|
|
}
|
|
} else {
|
|
pdfcraft_edit::change_image(doc, page, index, &c)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Perform an edit on a working copy (the caller discards it on error).
|
|
pub(crate) fn run_edit(doc: &mut pdfcraft_cos::Document, edit: &Edit, cx: &mut EditCtx) -> Result<(), EditError> {
|
|
match edit {
|
|
Edit::RotatePages { pages, degrees } => pdfcraft_organize::rotate_pages(doc, pages, *degrees)?,
|
|
Edit::DeletePages { pages } => pdfcraft_organize::delete_pages(doc, pages)?,
|
|
Edit::MovePages { pages, to } => pdfcraft_organize::move_pages(doc, pages, *to)?,
|
|
Edit::ReversePages { pages } => pdfcraft_organize::reverse_pages(doc, pages)?,
|
|
Edit::InsertBlankPage { at, width, height } => {
|
|
pdfcraft_organize::insert_blank_page(doc, *at, *width, *height)?;
|
|
}
|
|
Edit::SetInfo { key, value } => pdfcraft_organize::set_info(doc, key, value)?,
|
|
Edit::DuplicatePages { pages, at } => pdfcraft_organize::duplicate_pages(doc, pages, *at)?,
|
|
Edit::ReplacePages { pages, name, bytes, src_pages } => {
|
|
let src = open_source(name, bytes)?;
|
|
pdfcraft_organize::replace_pages(doc, pages, &src, src_pages)?;
|
|
}
|
|
Edit::SetPageBox { pages, which, spec } => pdfcraft_organize::set_page_box(doc, pages, *which, *spec)?,
|
|
Edit::SetTransition { pages, spec } => pdfcraft_organize::set_transition(doc, pages, spec)?,
|
|
Edit::InsertPagesFrom { name, bytes, pages, at, field_conflicts } => {
|
|
let src = open_source(name, bytes)?;
|
|
let pages = match pages {
|
|
Some(p) => p.clone(),
|
|
None => (0..pdfcraft_organize::page_count(&src)?).collect(),
|
|
};
|
|
// The file's bookmarks come along under one named after it, as in Combine Files.
|
|
let title = std::path::Path::new(name).file_stem().map_or_else(|| name.clone(), |s| s.to_string_lossy().into_owned());
|
|
let outcome = pdfcraft_organize::insert_file_pages(doc, &src, &title, &pages, *at, *field_conflicts)?;
|
|
cx.renamed_fields = outcome.renamed_fields;
|
|
}
|
|
Edit::AddBookmark { parent, index, title, page, left, top } => {
|
|
pdfcraft_organize::add_bookmark_at(doc, parent, *index, title, *page, *left, *top)?;
|
|
}
|
|
Edit::RenameBookmark { path, title } => pdfcraft_organize::rename_bookmark(doc, path, title)?,
|
|
Edit::DeleteBookmark { path } => pdfcraft_organize::delete_bookmark(doc, path)?,
|
|
Edit::MoveBookmark { from, to_parent, index } => {
|
|
pdfcraft_organize::move_bookmark(doc, from, to_parent, *index)?;
|
|
}
|
|
Edit::SetBookmarkPage { path, page } => pdfcraft_organize::set_bookmark_page(doc, path, *page)?,
|
|
Edit::SetBookmarkColor { path, color } => pdfcraft_organize::set_bookmark_color(doc, path, *color)?,
|
|
Edit::SetBookmarkStyle { path, bold, italic } => pdfcraft_organize::set_bookmark_style(doc, path, *bold, *italic)?,
|
|
Edit::ImportBookmarks { parent, index, bookmarks, replace } => {
|
|
pdfcraft_organize::import_outline(doc, parent, *index, bookmarks, *replace)?;
|
|
}
|
|
Edit::BookmarksFromStructure => {
|
|
let entries: Vec<_> = pdfcraft_a11y::headings(doc)
|
|
.into_iter()
|
|
.map(|h| pdfcraft_organize::OutlineEntry { level: h.level, title: h.title, page: h.page, element: h.obj, top: None })
|
|
.collect();
|
|
pdfcraft_organize::add_bookmark_tree(doc, "Untitled", &entries)?;
|
|
}
|
|
Edit::BookmarksFromTextStyles { levels } => {
|
|
if levels.is_empty() {
|
|
return Err(EditError::Invalid("choose a text style for at least one bookmark level".into()));
|
|
}
|
|
// Each line goes to the first level whose style it is in: a style given again, or one
|
|
// within the size tolerance of an earlier level's, would leave its level empty.
|
|
for (i, a) in levels.iter().enumerate() {
|
|
if let Some(j) = levels.iter().skip(i.saturating_add(1)).position(|b| a.matches(b)) {
|
|
let (first, second) = (i.saturating_add(1), i.saturating_add(j).saturating_add(2));
|
|
return Err(EditError::Invalid(format!(
|
|
"bookmark levels {first} and {second} have the same text style: choose a different one for each level"
|
|
)));
|
|
}
|
|
}
|
|
let entries = crate::text_style_bookmarks::headings(doc, levels);
|
|
if entries.is_empty() {
|
|
return Err(EditError::Invalid("no text is set in the chosen styles".into()));
|
|
}
|
|
pdfcraft_organize::add_bookmark_tree(doc, "Untitled", &entries)?;
|
|
}
|
|
Edit::NumberPages { from, to, style, prefix, first } => pdfcraft_organize::number_pages(doc, *from, *to, *style, prefix, *first)?,
|
|
Edit::AddMeasurement(m) => {
|
|
measure::add(doc, m, &cx.meta())?;
|
|
}
|
|
Edit::SetMeasurementScale { page, bbox, name, scale } => measure::set_scale(doc, *page, *bbox, name, scale)?,
|
|
Edit::AddAnnotation(a) => {
|
|
pdfcraft_annot::add_annotation(doc, a, &cx.meta())?;
|
|
}
|
|
Edit::PasteComments { xfdf, page, offset } => {
|
|
// Names from the edit's own id generator: unique to this paste within the document.
|
|
let prefix = cx.meta().id;
|
|
let report = pdfcraft_xfdf::import_comments(doc, xfdf, Some(*page), Some(&prefix), *offset)?;
|
|
if report.comments == 0 {
|
|
return Err(EditError::Invalid("the clipboard holds no comments".into()));
|
|
}
|
|
}
|
|
Edit::AddCustomStamp { page, rect, name, file, author } => {
|
|
let src = mark_source(doc, file)?;
|
|
let (sw, sh) = (src.size.0.max(1.0), src.size.1.max(1.0));
|
|
let natural = rect[2] == rect[0] && rect[3] == rect[1];
|
|
let rotation = pdfcraft_organize::page_rotation(doc, *page)?;
|
|
// The picture as displayed: a quarter turn swaps its sides in user space.
|
|
let (dw, dh) = if matches!(rotation, 90 | 270) { (sh, sw) } else { (sw, sh) };
|
|
let rect = if natural {
|
|
let k = (200.0 / sw.max(sh)).min(1.0);
|
|
let (w, h) = (dw * k, dh * k);
|
|
[rect[0] - w / 2.0, rect[1] - h / 2.0, rect[0] + w / 2.0, rect[1] + h / 2.0]
|
|
} else {
|
|
fit_picture(*rect, dw, dh)
|
|
};
|
|
let shape = Shape::CustomStamp { rect, name: name.clone(), picture: src.xobject, image: src.image, size: (sw, sh) };
|
|
let style = pdfcraft_annot::Style::default_for(&shape);
|
|
let new = NewAnnotation { page: *page, shape, style, contents: name.clone(), author: author.clone() };
|
|
let index = pdfcraft_annot::add_annotation(doc, &new, &cx.meta())?;
|
|
pdfcraft_annot::orient_image_stamp(doc, *page, index, rotation)?;
|
|
}
|
|
Edit::AddModel3d { page, rect, model, poster, activation, name } => {
|
|
// A file that is not a model is refused before its poster is brought in.
|
|
pdfcraft_annot::check_model(model)?;
|
|
let poster = match poster {
|
|
Some(file) => Some(model_poster(doc, *rect, file)?),
|
|
None => None,
|
|
};
|
|
let new = pdfcraft_annot::NewModel3d { page: *page, rect: *rect, model, activation: *activation, poster, contents: name };
|
|
pdfcraft_annot::add_3d_model(doc, &new, &cx.meta())?;
|
|
}
|
|
Edit::DeleteAnnotation { page, index } => pdfcraft_annot::delete_annotation(doc, *page, *index)?,
|
|
Edit::SetAnnotationContents { page, index, text } => pdfcraft_annot::set_contents(doc, *page, *index, text, &cx.meta())?,
|
|
Edit::ReplyToAnnotation { page, index, text, author } => {
|
|
pdfcraft_annot::add_reply(doc, *page, *index, text, author, &cx.meta())?;
|
|
}
|
|
Edit::SetAnnotationStatus { page, index, state, author } => {
|
|
pdfcraft_annot::set_review_state(doc, *page, *index, *state, author, &cx.meta())?;
|
|
}
|
|
Edit::EraseInk { page, index, path, radius } => {
|
|
pdfcraft_annot::erase_ink(doc, *page, *index, path, *radius, &cx.meta())?;
|
|
}
|
|
Edit::AddAttachment { name, bytes, description, mime } => {
|
|
pdfcraft_organize::add_attachment(doc, name, bytes, description.as_deref(), mime.as_deref(), cx.date.as_deref())?;
|
|
}
|
|
Edit::DeleteAttachment { key } => pdfcraft_organize::delete_attachment(doc, key)?,
|
|
Edit::ReplaceText { page, quads, text, author, strike, caret } => {
|
|
// One `/NM` each: the caret replies to the strikeout, and XFDF names that reply by
|
|
// the strikeout's `/NM` (#2666).
|
|
let (strike_meta, caret_meta) = (cx.meta(), cx.meta());
|
|
pdfcraft_annot::add_text_replacement(
|
|
doc,
|
|
pdfcraft_annot::TextReplacement {
|
|
page: *page,
|
|
quads,
|
|
replacement: text,
|
|
author,
|
|
strike,
|
|
caret,
|
|
strike_meta: &strike_meta,
|
|
caret_meta: &caret_meta,
|
|
},
|
|
)?;
|
|
}
|
|
Edit::MarkAnnotation { page, index, marked, author } => {
|
|
pdfcraft_annot::set_marked(doc, *page, *index, *marked, author, &cx.meta())?;
|
|
}
|
|
Edit::LockAnnotation { page, index, locked } => pdfcraft_annot::set_locked(doc, *page, *index, *locked)?,
|
|
Edit::MoveAnnotation { page, index, dx, dy } => pdfcraft_annot::move_annotation(doc, *page, *index, *dx, *dy, &cx.meta())?,
|
|
Edit::ReshapeAnnotation { page, index, points } => pdfcraft_annot::set_points(doc, *page, *index, points, &cx.meta())?,
|
|
Edit::ResizeAnnotation { page, index, rect } => pdfcraft_annot::set_rect(doc, *page, *index, *rect, &cx.meta())?,
|
|
Edit::StyleAnnotation { page, index, color, opacity, width, endings } => {
|
|
pdfcraft_annot::set_style(
|
|
doc,
|
|
*page,
|
|
*index,
|
|
pdfcraft_annot::StyleChange { color: *color, opacity: *opacity, width: *width, endings: endings.clone() },
|
|
&cx.meta(),
|
|
)?;
|
|
}
|
|
Edit::FillAnnotation { page, index, fill } => pdfcraft_annot::set_fill(doc, *page, *index, *fill, &cx.meta())?,
|
|
Edit::PartOpacity { page, index, fill, text } => pdfcraft_annot::set_part_opacity(doc, *page, *index, *fill, *text, &cx.meta())?,
|
|
Edit::FontSizeAnnotation { page, index, size } => pdfcraft_annot::set_font_size(doc, *page, *index, *size, &cx.meta())?,
|
|
Edit::BorderColorAnnotation { page, index, color } => pdfcraft_annot::set_border_color(doc, *page, *index, *color, &cx.meta())?,
|
|
Edit::BorderShapeAnnotation { page, index, shape } => pdfcraft_annot::set_border_shape(doc, *page, *index, *shape, &cx.meta())?,
|
|
Edit::SetAnnotationInfo { page, index, author, subject, icon } => {
|
|
pdfcraft_annot::set_info(doc, *page, *index, author.as_deref(), subject.as_deref(), *icon, &cx.meta())?;
|
|
}
|
|
Edit::SetFieldValue { name, value } => {
|
|
let mut set = |doc: &mut pdfcraft_cos::Document, value: &FieldValue| match cx.js.as_mut() {
|
|
Some(js) => pdfcraft_forms::set_value_with(doc, name, value, js),
|
|
None => pdfcraft_forms::set_value(doc, name, value),
|
|
};
|
|
match set(doc, value) {
|
|
Ok(()) => {}
|
|
// A date the field's format doesn't read may be typed by another of an XFA date
|
|
// field's patterns: it is kept in the first. Only a format refusal is tried
|
|
// again: it comes before any of the field's scripts run, so they run once.
|
|
Err(refused @ pdfcraft_forms::FormError::Format(_)) => match xfa::typed_date(doc, cx.xfa.as_deref(), name, value) {
|
|
Some(typed) => set(doc, &typed)?,
|
|
None => return Err(refused.into()),
|
|
},
|
|
Err(refused) => return Err(refused.into()),
|
|
}
|
|
}
|
|
Edit::ApplyScriptChanges { changes } => match cx.js.as_mut() {
|
|
Some(js) => pdfcraft_forms::apply_script_changes(doc, changes, js)?,
|
|
None => pdfcraft_forms::apply_script_changes(doc, changes, &mut pdfcraft_forms::NoScripts)?,
|
|
},
|
|
Edit::XfaEvent { som, activity } => {
|
|
let tpl = cx.xfa.clone().ok_or_else(|| EditError::Invalid("this is not a laid-out XFA form, or JavaScript is off".into()))?;
|
|
let mut run =
|
|
xfa::XfaRun { page_count: cx.pages, out: &mut cx.xfa_out, datasets: cx.xfa_datasets, ran_away: false, time_scale: cx.xfa_time_scale };
|
|
let ran = xfa::on_event(doc, &tpl, som, activity, &mut run);
|
|
cx.xfa_datasets = run.datasets;
|
|
cx.xfa_ran_away |= run.ran_away;
|
|
ran.map_err(EditError::Invalid)?;
|
|
}
|
|
Edit::SetFieldImage { name, image } => {
|
|
let (img, _) = embed_image(doc, name, image)?;
|
|
let px = match &*doc.get(img) {
|
|
pdfcraft_cos::Object::Stream(s) => (s.dict.int(b"Width").unwrap_or(1) as u32, s.dict.int(b"Height").unwrap_or(1) as u32),
|
|
_ => (1, 1),
|
|
};
|
|
// An XFA image field keeps the picture in the data too (as Designer saves it), and a
|
|
// laid-out form shows it from there: so only pictures the layout can show, and only
|
|
// for image fields (a laid-out one carries its SOM path; a static form's field name
|
|
// is its path).
|
|
let is_xfa = pdfcraft_xfa::read_packets(doc).ok().flatten().is_some();
|
|
let image_field = is_xfa
|
|
&& pdfcraft_forms::fields(doc).iter().any(|f| f.name == *name && f.button == Some(pdfcraft_forms::af::ButtonAction::ImportIcon));
|
|
if image_field {
|
|
let Some(content_type) = image_content_type(image) else {
|
|
return Err(EditError::Invalid(format!("{name}: an XFA form keeps a PNG, JPEG, GIF, BMP or TIFF picture; choose one of those")));
|
|
};
|
|
pdfcraft_forms::set_button_icon(doc, name, img, px)?;
|
|
let som = pdfcraft_xfa::fields_by_som(doc).into_iter().find(|(_, n)| n == name).map(|(s, _)| s).unwrap_or_else(|| name.clone());
|
|
let path = match cx.xfa.as_deref() {
|
|
Some(tpl) => pdfcraft_xfa::data_path(tpl, &som),
|
|
None => pdfcraft_xfa::som_to_path(&som),
|
|
};
|
|
let r =
|
|
pdfcraft_xfa::write_data_image(doc, &path, content_type, image, cx.xfa_datasets).map_err(|e| EditError::Write(e.to_string()))?;
|
|
if !r.written && r.warnings.is_empty() {
|
|
return Err(EditError::Write(format!("{name}: the picture could not be written into the form's data")));
|
|
}
|
|
cx.xfa_datasets = r.stream.or(cx.xfa_datasets);
|
|
cx.xfa_out.errors.extend(r.warnings);
|
|
xfa_after_data_change(doc, cx, false)?;
|
|
} else {
|
|
pdfcraft_forms::set_button_icon(doc, name, img, px)?;
|
|
}
|
|
}
|
|
Edit::ResetForm { names } => {
|
|
pdfcraft_forms::reset(doc, names.as_deref())?;
|
|
}
|
|
Edit::AddField { page, rect, kind, name } => {
|
|
pdfcraft_forms::add_field(doc, *page, *rect, kind, name.as_deref())?;
|
|
}
|
|
Edit::SetFieldProps { name, props } => {
|
|
pdfcraft_forms::set_props(doc, name, props)?;
|
|
}
|
|
Edit::DuplicateField { name, pages } => {
|
|
if pdfcraft_forms::duplicate_field(doc, name, pages)? == 0 {
|
|
return Err(EditError::Form(pdfcraft_forms::FormError::Invalid(format!("{name} is already on those pages"))));
|
|
}
|
|
}
|
|
Edit::DeleteField { name } => pdfcraft_forms::delete_field(doc, name)?,
|
|
Edit::DeleteWidget { name, widget } => pdfcraft_forms::delete_widget(doc, name, *widget)?,
|
|
Edit::SetTabOrder { pages, order } => pdfcraft_forms::set_tab_order(doc, pages, *order)?,
|
|
Edit::MoveInTabOrder { name, earlier } => pdfcraft_forms::move_in_tab_order(doc, name, *earlier)?,
|
|
// As after a script edit: the values the new order gives show at once.
|
|
Edit::SetCalculationOrder { names } => {
|
|
pdfcraft_forms::set_calculation_order(doc, names)?;
|
|
match cx.js.as_mut() {
|
|
Some(js) => pdfcraft_forms::recalculate_with(doc, js)?,
|
|
None => pdfcraft_forms::recalculate(doc)?,
|
|
};
|
|
}
|
|
Edit::SetInitialView(v) => pdfcraft_organize::set_initial_view(doc, v)?,
|
|
Edit::AutoTagText => {
|
|
a11y::autotag_text(doc).map_err(|e| EditError::Accessibility(e.to_string()))?;
|
|
}
|
|
Edit::SetAltText { figure, alt } => {
|
|
let r = pdfcraft_cos::ObjRef::new(*figure, doc.generation(*figure));
|
|
a11y::set_alt(doc, r, alt.as_deref()).map_err(|e| EditError::Accessibility(e.to_string()))?;
|
|
}
|
|
Edit::AutoTagFormFields => {
|
|
a11y::tag_form_fields(doc).map_err(EditError::Accessibility)?;
|
|
}
|
|
Edit::SetTagProperty { object, property, value } => {
|
|
a11y::set_tag_property(doc, *object, *property, value.as_deref()).map_err(|e| EditError::Accessibility(e.to_string()))?;
|
|
}
|
|
Edit::MoveTag { object, anchor, placement } => {
|
|
a11y::move_tag(doc, *object, *anchor, *placement).map_err(|e| EditError::Accessibility(e.to_string()))?;
|
|
}
|
|
Edit::EditTextLine { page, line, text } => {
|
|
pdfcraft_edit::replace_line(doc, *page, *line, text)?;
|
|
}
|
|
Edit::SetFieldScript { name, event, script } => {
|
|
pdfcraft_forms::set_field_script(doc, name, event, script.as_deref())?;
|
|
match cx.js.as_mut() {
|
|
Some(js) => pdfcraft_forms::recalculate_with(doc, js)?,
|
|
None => pdfcraft_forms::recalculate(doc)?,
|
|
};
|
|
}
|
|
Edit::ConvertPdfA { level } => {
|
|
pdfcraft_preflight::convert(doc, *level).map_err(|e| EditError::Invalid(e.to_string()))?;
|
|
}
|
|
Edit::RemoveStandardId { standard } => pdfcraft_organize::remove_standard_id(doc, *standard)?,
|
|
Edit::SetDocumentScript { name, script } => pdfcraft_forms::set_document_script(doc, name, script.as_deref())?,
|
|
Edit::AddOcrText { page, words } => {
|
|
crate::ocr::layer::add_text(doc, *page, words)?;
|
|
}
|
|
Edit::EnhanceScan { images } => crate::scan_clean::write_back(doc, images)?,
|
|
Edit::EditPageImage { page, index, change } => run_edit_page_image(doc, *page, *index, change)?,
|
|
Edit::MoveObjects { page, objects, offset } => pdfcraft_edit::move_objects(doc, *page, objects, *offset)?,
|
|
Edit::EditTextBlock { page, block, text, style } => {
|
|
pdfcraft_edit::rewrite_block(doc, *page, *block, Some(text), style)?;
|
|
}
|
|
Edit::RegisterTextFont { page, bytes } => {
|
|
pdfcraft_edit::text::register_text_font(doc, *page, bytes)?;
|
|
}
|
|
Edit::MarkDecorative { figure } => {
|
|
let r = pdfcraft_cos::ObjRef::new(*figure, doc.generation(*figure));
|
|
a11y::mark_decorative(doc, r).map_err(|e| EditError::Accessibility(e.to_string()))?;
|
|
}
|
|
Edit::MarkTagArtifact { object } => {
|
|
a11y::mark_tag_artifact(doc, *object).map_err(|e| EditError::Accessibility(e.to_string()))?;
|
|
}
|
|
Edit::AddHeaderFooter { pages, settings, replace } => {
|
|
let date = cx.today;
|
|
pdfcraft_edit::add_header_footer(doc, pages, settings, *replace, &pdfcraft_edit::Context { date })?;
|
|
}
|
|
Edit::AddWatermark { pages, settings, replace, file } => {
|
|
let mut s = settings.clone();
|
|
if let Some(f) = file {
|
|
s.source = Some(mark_source(doc, f)?);
|
|
}
|
|
pdfcraft_edit::add_watermark(doc, pages, &s, *replace)?
|
|
}
|
|
Edit::AddBackground { pages, settings, replace, file } => {
|
|
let mut s = settings.clone();
|
|
if let Some(f) = file {
|
|
s.source = Some(mark_source(doc, f)?);
|
|
}
|
|
pdfcraft_edit::add_background(doc, pages, &s, *replace)?
|
|
}
|
|
Edit::RemoveMarks { kind } => {
|
|
let n = pdfcraft_model::pages(doc).len();
|
|
if pdfcraft_edit::remove_marks(doc, &(0..n).collect::<Vec<_>>(), *kind)? == 0 {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("there is nothing to remove".into())));
|
|
}
|
|
}
|
|
Edit::AddText { page, text } => {
|
|
pdfcraft_edit::add_content(doc, *page, &AddedContent::Text(text.clone()))?;
|
|
}
|
|
Edit::AddImage { page, rect, name, bytes } => {
|
|
let (image, natural) = embed_image(doc, name, bytes)?;
|
|
let rect = match rect {
|
|
Some(r) => *r,
|
|
None => {
|
|
let p = pdfcraft_model::pages(doc).swap_remove(*page);
|
|
let (pw, ph) = p.display_size(doc);
|
|
let k = ((pw * 0.8) / natural.0).min((ph * 0.8) / natural.1).min(1.0);
|
|
let (w, h) = (natural.0 * k, natural.1 * k);
|
|
[(pw - w) / 2.0, (ph - h) / 2.0, (pw + w) / 2.0, (ph + h) / 2.0]
|
|
}
|
|
};
|
|
pdfcraft_edit::add_content(doc, *page, &AddedContent::Image(pdfcraft_edit::AddedImage::new(rect, image)))?;
|
|
}
|
|
Edit::UpdateContent { page, index, content } => pdfcraft_edit::update_content(doc, *page, *index, content)?,
|
|
Edit::DeleteContent { page, index } => pdfcraft_edit::delete_content(doc, *page, *index)?,
|
|
Edit::ReplaceImage { page, index, name, bytes } => {
|
|
let item = pdfcraft_edit::list_added(doc).into_iter().filter(|a| a.page == *page).nth(*index);
|
|
let Some(AddedContent::Image(old)) = item.map(|a| a.content) else {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("that item is not an image".into())));
|
|
};
|
|
let (image, _) = embed_image(doc, name, bytes)?;
|
|
pdfcraft_edit::update_content(doc, *page, *index, &AddedContent::Image(pdfcraft_edit::AddedImage { image, ..old }))?;
|
|
}
|
|
Edit::PasteContent { page, content, file } => {
|
|
let content = match content {
|
|
AddedContent::Text(_) => content.clone(),
|
|
AddedContent::Image(img) => {
|
|
let Some((name, bytes)) = file else {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("a pasted image needs its picture".into())));
|
|
};
|
|
// The same picture in this document (copied here, pasted here before, or an
|
|
// identical file): drawn again from the one image object rather than stored twice.
|
|
let image = if copy_image::is_picture(doc, img.image, bytes) {
|
|
img.image
|
|
} else {
|
|
match copy_image::added_picture(doc, bytes) {
|
|
Some(image) => image,
|
|
None => embed_image(doc, name, bytes)?.0,
|
|
}
|
|
};
|
|
AddedContent::Image(pdfcraft_edit::AddedImage { image, ..img.clone() })
|
|
}
|
|
};
|
|
pdfcraft_edit::add_content(doc, *page, &content)?;
|
|
}
|
|
Edit::ApplyRedactions { pages } => {
|
|
cx.redaction_overlays = pdfcraft_redact::apply(doc, pages.as_deref())?.overlays;
|
|
}
|
|
Edit::ClearRedactions => {
|
|
pdfcraft_redact::clear_marks(doc, None)?;
|
|
}
|
|
Edit::AddLink { page, rect, action, style } => {
|
|
pdfcraft_annot::links::add(doc, *page, *rect, action, style)?;
|
|
}
|
|
Edit::SetLink { page, index, rect, action, style } => pdfcraft_annot::links::set(doc, *page, *index, *rect, action.as_ref(), style.as_ref())?,
|
|
Edit::DeleteLink { page, index } => pdfcraft_annot::links::delete(doc, *page, *index)?,
|
|
Edit::RemoveLinks { pages } => {
|
|
if pdfcraft_annot::links::remove_all(doc, pages.as_deref())? == 0 {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("there are no links to remove".into())));
|
|
}
|
|
}
|
|
Edit::AddLinks { links, style } => {
|
|
if pdfcraft_annot::links::add_many(doc, links, style)? == 0 {
|
|
return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("no web addresses were found".into())));
|
|
}
|
|
}
|
|
Edit::ImportData { name, bytes } if bytes.starts_with(b"%PDF-") => {
|
|
let src = comment_source(bytes).map_err(|p| EditError::Source(format!("{name}: {p}")))?;
|
|
import_pdf_comments(doc, &src)?;
|
|
}
|
|
Edit::ImportData { name, bytes } => {
|
|
// An XFA form takes XFA data files (XDP, datasets, or a data document) as its data;
|
|
// XFDF, FDF, Acrobat's XML and tables go the usual way.
|
|
let text = (pdfcraft_xfa::read_packets(doc).ok().flatten().is_some()).then(|| decode_data_file(bytes)).flatten();
|
|
match text {
|
|
Some(Ok(text)) if is_xfa_data_file(&text) => {
|
|
let expect_root = cx.xfa.as_ref().and_then(|t| t.root.common.name.clone());
|
|
let r = pdfcraft_xfa::import_data(doc, &text, expect_root.as_deref(), cx.xfa_datasets)
|
|
.map_err(|e| EditError::Write(e.to_string()))?;
|
|
cx.xfa_datasets = r.stream.or(cx.xfa_datasets);
|
|
cx.xfa_out.errors.extend(r.warnings);
|
|
cx.xfa_out.console.push(format!("Imported XFA data: {} node(s)", r.nodes));
|
|
xfa_after_data_change(doc, cx, true)?;
|
|
}
|
|
Some(Err(why)) if !bytes[..bytes.len().min(1024)].windows(5).any(|w| w == b"%PDF-") => return Err(EditError::Invalid(why)),
|
|
_ => {
|
|
// A PDF with bytes before its header, within the 1,024 the reader allows, is read
|
|
// as a PDF when it is not a data file. A file the PDF reader cannot open keeps
|
|
// the data importer's error.
|
|
let mut data = doc.clone();
|
|
match pdfcraft_xfdf::import(&mut data, bytes) {
|
|
Ok(_) => *doc = data,
|
|
Err(e) if bytes[..bytes.len().min(1024)].windows(5).any(|w| w == b"%PDF-") => match comment_source(bytes) {
|
|
Ok(src) => import_pdf_comments(doc, &src)?,
|
|
Err(SourceProblem::Unreadable(_)) => return Err(e.into()),
|
|
Err(p) => return Err(EditError::Source(format!("{name}: {p}"))),
|
|
},
|
|
Err(e) => return Err(e.into()),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Edit::RemoveHidden { which } => {
|
|
pdfcraft_redact::sanitize::remove_hidden(doc, which)?;
|
|
}
|
|
Edit::Sanitize => {
|
|
pdfcraft_redact::sanitize::sanitize(doc)?;
|
|
}
|
|
Edit::Flatten { comments, fields } => {
|
|
// An XFA form's fields baked into the pages: the XFA goes too, or Adobe's viewers
|
|
// would lay the form out again from its template over the baked pages. A dynamic
|
|
// form that was never laid out keeps its XFA: without it only placeholders remain.
|
|
let shape = (*fields).then(|| pdfcraft_xfa::xfa_shape(doc)).flatten();
|
|
let n = pdfcraft_model::pages(doc).len();
|
|
pdfcraft_edit::flatten(doc, &(0..n).collect::<Vec<_>>(), *comments, *fields)?;
|
|
match shape {
|
|
Some(shape) if shape.strippable() => strip_xfa(doc, cx)?,
|
|
Some(_) => cx.xfa_removed.push("The XFA was kept: this dynamic form was not laid out, so its fields were not baked in".into()),
|
|
None => {}
|
|
}
|
|
}
|
|
Edit::XfaToStatic => {
|
|
let Some(shape) = pdfcraft_xfa::xfa_shape(doc) else {
|
|
return Err(EditError::Invalid("this document is not an XFA form".into()));
|
|
};
|
|
if !shape.strippable() {
|
|
return Err(EditError::Invalid("this dynamic XFA form was not laid out, so there is no static form to keep".into()));
|
|
}
|
|
strip_xfa(doc, cx)?;
|
|
}
|
|
Edit::FlattenFillSign => {
|
|
let n = pdfcraft_model::pages(doc).len();
|
|
pdfcraft_edit::flatten_fill_sign(doc, &(0..n).collect::<Vec<_>>())?;
|
|
}
|
|
Edit::Protect(p) => {
|
|
p.validate()?;
|
|
let seed = cx.entropy()?;
|
|
// Without a permissions password nothing is restricted, so the owner password is a
|
|
// random one nobody needs.
|
|
let random_owner = match p.permissions_password {
|
|
Some(_) => String::new(),
|
|
None => cx.entropy()?.iter().map(|b| format!("{b:02x}")).collect(),
|
|
};
|
|
let params = pdfcraft_cos::NewEncryption {
|
|
algorithm: p.algorithm,
|
|
user_password: p.open_password.as_deref().unwrap_or(""),
|
|
owner_password: p.permissions_password.as_deref().unwrap_or(&random_owner),
|
|
permissions: p.permission_bits(),
|
|
encrypt_metadata: p.encrypt_metadata,
|
|
seed,
|
|
};
|
|
doc.set_encryption(¶ms).map_err(|e| EditError::Protection(e.to_string()))?;
|
|
}
|
|
Edit::RemoveProtection => {
|
|
if doc.output_handler().is_none() {
|
|
return Err(EditError::Protection("the document isn't protected".into()));
|
|
}
|
|
doc.remove_encryption();
|
|
}
|
|
Edit::Batch { edits, .. } => {
|
|
for e in edits {
|
|
run_edit(doc, e, cx)?;
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|