mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-20 19:18:41 +00:00
M8: a long form /Matrix is read cheaply, an unreadable one leaves layer removal alone
Review of the walk's read of a form's `/Matrix`. Removing hidden layers alone never asks where content lands, but a form whose `/Matrix` isn't six numbers was counted in its scan and refused, in a document without layers too. Such a form is now walked as it stands there; Apply Redactions, and Remove Hidden Information with hidden text, which tells hidden text by where it lands, still refuse it. `matrix_of` resolved and copied every entry before checking there were six, and the walk reads it at every `Do`. Measured in review (debug build), 20,000 draws of a form whose `/Matrix` is 20,000 entries long took 9 s by reference (16 ms before the walk read entries) and 12 s written directly (2.6 s before, the array being copied by `resolve`). It now reads an array written directly in place, and checks the length before resolving any entry. Tests: `a_form_whose_matrix_is_not_six_numbers_leaves_removing_hidden_layers_alone`, and `tests/matrix_work.rs` (a binary of its own), whose `a_long_form_matrix_costs_no_more_than_a_short_one` times 4,000 draws of a form with a 4,000-entry `/Matrix` against a 7-entry one, written directly and by reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY
This commit is contained in:
1 parent
d2c87ae789
commit
cf26980471
4 files changed
+106
-24
No files matched your search
+20
-14
@@ -1073,21 +1073,27 @@ fn xobject(
|
||||
Some(b"Form") => {
|
||||
// Read as the proof reads it, entries by reference included: read as the identity, a
|
||||
// form moved onto a mark would be tested against the wrong place and never walked.
|
||||
let Ok(m) = crate::verify::matrix_of(doc, &s.dict) else {
|
||||
// Not six numbers: where the form draws, and so whether it reaches a mark, can't be
|
||||
// told. Apply refuses the page, and the scans count it, as for a form past the
|
||||
// page's budget below.
|
||||
if scope.mode == Mode::Apply {
|
||||
out.error = Some(CosError::Syntax {
|
||||
offset: 0,
|
||||
detail: "a form XObject's /Matrix is not six numbers, so where it draws can't be told".into(),
|
||||
});
|
||||
} else if scope.everywhere() {
|
||||
scope.layer_blocks += 1;
|
||||
} else {
|
||||
out.residue += 1;
|
||||
let m = match crate::verify::matrix_of(doc, &s.dict) {
|
||||
Ok(m) => m,
|
||||
// Not six numbers. Removing hidden layers alone never asks where content lands, so
|
||||
// the form is walked as it stands.
|
||||
Err(()) if scope.everywhere() && scope.hidden_text.is_none() => Matrix::IDENTITY,
|
||||
// Otherwise where the form draws, and so whether it reaches a mark or leaves the
|
||||
// page, can't be told. Apply refuses the page, and the scans count it, as for a
|
||||
// form past the page's budget below.
|
||||
Err(()) => {
|
||||
if scope.mode == Mode::Apply {
|
||||
out.error = Some(CosError::Syntax {
|
||||
offset: 0,
|
||||
detail: "a form XObject's /Matrix is not six numbers, so where it draws can't be told".into(),
|
||||
});
|
||||
} else if scope.everywhere() {
|
||||
scope.layer_blocks += 1;
|
||||
} else {
|
||||
out.residue += 1;
|
||||
}
|
||||
return None;
|
||||
}
|
||||
return None;
|
||||
};
|
||||
let fctm = m.then(&gs.ctm);
|
||||
let bbox = s.dict.get(b"BBox").and_then(|b| {
|
||||
|
||||
@@ -85,6 +85,17 @@ fn a_form_whose_matrix_is_not_six_numbers_counts_in_the_scans() {
|
||||
assert!(matches!(&removed, Err(RedactError::Cos(pdfcraft_cos::CosError::Syntax { detail, .. })) if detail.contains("/Matrix")), "{removed:?}");
|
||||
}
|
||||
|
||||
/// Removing hidden layers alone never asks where content lands, so such a form is walked as it
|
||||
/// stands: nothing is counted or refused over it in a document without layers.
|
||||
#[test]
|
||||
fn a_form_whose_matrix_is_not_six_numbers_leaves_removing_hidden_layers_alone() {
|
||||
use crate::sanitize::{Hidden, remove_hidden, scan};
|
||||
let mut doc = moved_form("/Matrix [1 0 0 1 0 (up)]", b"null");
|
||||
let counts: std::collections::HashMap<Hidden, usize> = scan(&doc).into_iter().collect();
|
||||
assert_eq!(counts[&Hidden::HiddenLayers], 0);
|
||||
assert_eq!(remove_hidden(&mut doc, &[Hidden::HiddenLayers]), Err(RedactError::NothingToApply));
|
||||
}
|
||||
|
||||
/// Remove Hidden Information reads it the same way: text a matrix entry by reference moves off the
|
||||
/// page is hidden text, and goes.
|
||||
#[test]
|
||||
|
||||
@@ -400,18 +400,24 @@ pub(super) fn push_squashed(into: &mut String, s: &str) {
|
||||
/// is the identity, present but malformed is an error (guessing the identity would put its content in the
|
||||
/// wrong place). The redaction walk reads a form's with it too.
|
||||
pub(crate) fn matrix_of(doc: &Document, dict: &Dict) -> Result<Matrix, ()> {
|
||||
match dict.get(b"Matrix") {
|
||||
None => Ok(Matrix::IDENTITY),
|
||||
Some(m) => {
|
||||
let m = doc.resolve(m);
|
||||
// A null value is no entry (ISO 32000-2 §7.3.9), whether given directly or by reference.
|
||||
if matches!(*m, Object::Null) {
|
||||
return Ok(Matrix::IDENTITY);
|
||||
}
|
||||
let items: Vec<Object> = m.as_array().ok_or(())?.iter().map(|x| (*doc.resolve(x)).clone()).collect();
|
||||
Matrix::from_operands(&items).filter(|m| m.0.iter().all(|v| v.is_finite())).ok_or(())
|
||||
let Some(m) = dict.get(b"Matrix") else { return Ok(Matrix::IDENTITY) };
|
||||
// Read in place when written directly, and the length checked before any entry is resolved:
|
||||
// the walk reads it at every `Do`, and an array of any length would be copied each time.
|
||||
let held;
|
||||
let m = match m {
|
||||
Object::Ref(_) => {
|
||||
held = doc.resolve(m);
|
||||
&*held
|
||||
}
|
||||
direct => direct,
|
||||
};
|
||||
// A null value is no entry (ISO 32000-2 §7.3.9), whether given directly or by reference.
|
||||
if matches!(m, Object::Null) {
|
||||
return Ok(Matrix::IDENTITY);
|
||||
}
|
||||
let items = m.as_array().filter(|a| a.len() == 6).ok_or(())?;
|
||||
let items: Vec<Object> = items.iter().map(|x| (*doc.resolve(x)).clone()).collect();
|
||||
Matrix::from_operands(&items).filter(|m| m.0.iter().all(|v| v.is_finite())).ok_or(())
|
||||
}
|
||||
|
||||
/// A form's `/BBox` as `[x0 y0 x1 y1]`: absent is `None`, present but malformed is an error.
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
//! Work regression for a form's `/Matrix`, which the redaction walk reads at every `Do` of the
|
||||
//! form: an array of any length costs no more than one of seven entries, written directly or by
|
||||
//! reference. A test binary of its own, so that none of the crate's other tests shares its process.
|
||||
//! The test compares two timings of its own, the least of three runs each, not a fixed time.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use pdfcraft_annot::{Meta, NewAnnotation, Shape, Style, add_annotation, rect_quad};
|
||||
use pdfcraft_cos::Document;
|
||||
use pdfcraft_redact::apply;
|
||||
use pdfcraft_testkit::{Pdf, stream_obj};
|
||||
|
||||
/// One page drawing a form `draws` times under a mark, the form's `/Matrix` an array of `entries`
|
||||
/// zeros, written in its dictionary or, `by_reference`, as object 6.
|
||||
fn document(draws: usize, entries: usize, by_reference: bool) -> Document {
|
||||
let zeros = format!("[{}]", vec!["0"; entries].join(" "));
|
||||
let matrix = if by_reference { "6 0 R".to_string() } else { zeros.clone() };
|
||||
let objs = vec![
|
||||
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
|
||||
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
|
||||
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 4 0 R /Resources << /XObject << /Fm 5 0 R >> >> >>".to_vec(),
|
||||
stream_obj("", "/Fm Do ".repeat(draws).as_bytes()),
|
||||
stream_obj(&format!("/Type /XObject /Subtype /Form /BBox [0 0 300 300] /Matrix {matrix}"), b"0 0 10 10 re f"),
|
||||
zeros.into_bytes(),
|
||||
];
|
||||
let mut doc = Document::open(Arc::new(Pdf::new().objs(objs).build())).unwrap();
|
||||
let shape = Shape::Redact { quads: vec![rect_quad([0.0, 0.0, 300.0, 300.0])], overlay: String::new(), look: Default::default() };
|
||||
let style = Style::default_for(&shape);
|
||||
add_annotation(&mut doc, &NewAnnotation { page: 0, shape, style, contents: String::new(), author: "Synthetic test".into() }, &Meta::default())
|
||||
.unwrap();
|
||||
doc
|
||||
}
|
||||
|
||||
/// How long redacting `document(draws, entries, by_reference)` takes, the least of three runs. A
|
||||
/// matrix that isn't six numbers is refused.
|
||||
fn redaction(draws: usize, entries: usize, by_reference: bool) -> Duration {
|
||||
(0..3)
|
||||
.map(|_| {
|
||||
let mut doc = document(draws, entries, by_reference);
|
||||
let start = Instant::now();
|
||||
let refused = apply(&mut doc, None).is_err();
|
||||
let took = start.elapsed();
|
||||
assert!(refused, "a /Matrix of {entries} entries is refused");
|
||||
took
|
||||
})
|
||||
.min()
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Written when the walk resolved and copied every entry before checking there were six: 4,000
|
||||
/// draws of a form whose `/Matrix` is 4,000 entries long read sixteen million entries.
|
||||
#[test]
|
||||
fn a_long_form_matrix_costs_no_more_than_a_short_one() {
|
||||
for by_reference in [false, true] {
|
||||
let (short, long) = (redaction(4_000, 7, by_reference), redaction(4_000, 4_000, by_reference));
|
||||
assert!(long < short * 3 + Duration::from_millis(50), "by reference: {by_reference}: long {long:?}, short {short:?}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user