pdf-inspector 1.25.1

Fast PDF inspection, classification, and text extraction with smart scanned vs text-based detection
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Fixtures shared by the tests of the executed-content scan: synthetic
//! pages with images, forms and patterns bound, text layers to lay over
//! them, and the scan run over a page's content.

use super::*;

/// A scan of `content` on its own — no page, nothing followed through
/// `Do` — as its counts and executed tallies: (counts, text ops,
/// hidden text ops).
pub(super) fn scan_alone(content: &[u8]) -> (ContentCounts, u32, u32) {
    let doc = Document::new();
    let mut state = ContentScanState::new(&doc, PageBox::LETTER, false);
    let counts = scan_content_stream(
        content,
        &mut HashSet::new(),
        &mut HashSet::new(),
        &mut state,
        &[],
    );
    (
        counts,
        state.executed_text_ops,
        state.executed_hidden_text_ops,
    )
}

/// A Form XObject of [`synthetic_page`]: the page's font as `F1`, the
/// `/BBox` and `/Matrix` given, and in its own resources the XObjects of
/// `xobjects` — each a name to bind, and the name of an image or of an
/// earlier form of the page for it to stand for.
#[derive(Clone, Copy)]
pub(super) struct TestForm<'a> {
    pub(super) name: &'a str,
    pub(super) content: &'a str,
    pub(super) matrix: Option<[i64; 6]>,
    pub(super) bbox: &'a [i64],
    pub(super) xobjects: &'a [(&'a str, &'a str)],
}

/// A page-sized form with nothing in it, to fill in.
pub(super) const PAGE_FORM: TestForm<'static> = TestForm {
    name: "",
    content: "",
    matrix: None,
    bbox: &[0, 0, 612, 792],
    xobjects: &[],
};

/// A pattern of [`synthetic_page_with_patterns`]: a tiling pattern whose
/// cell runs `content`, with the page's image as `Im0` and font as `F1`
/// in its resources — or a shading pattern, when `shading`.
#[derive(Clone, Copy)]
pub(super) struct TestPattern<'a> {
    pub(super) name: &'a str,
    pub(super) content: &'a str,
    pub(super) shading: bool,
}

/// A one-page 612×792 document whose content stream is set with
/// [`set_page_content`]: a 2×2 gray image `Im0` when `image`; a
/// 1500×2383 flat gray image `ImBig`, bound whether or not the content
/// draws it, when `large_image`; and the given forms, bound whether or
/// not the content invokes them.
pub(super) fn synthetic_page(
    image: bool,
    large_image: bool,
    forms: &[TestForm<'_>],
) -> (Document, ObjectId, ObjectId) {
    synthetic_page_with_patterns(image, large_image, forms, &[])
}

/// [`synthetic_page`] with the given patterns bound as well.
pub(super) fn synthetic_page_with_patterns(
    image: bool,
    large_image: bool,
    forms: &[TestForm<'_>],
    patterns: &[TestPattern<'_>],
) -> (Document, ObjectId, ObjectId) {
    use lopdf::dictionary;
    let mut doc = Document::with_version("1.4");
    let pages_id = doc.new_object_id();
    let page_id = doc.new_object_id();
    let font_id = doc.add_object(dictionary! {
        "Type" => "Font",
        "Subtype" => Object::Name(b"Type1".to_vec()),
        "BaseFont" => Object::Name(b"Helvetica".to_vec()),
    });
    let mut xobjects = dictionary! {};
    let mut add_image = |doc: &mut Document, name: &str, width: i64, height: i64, data: Vec<u8>| {
        let mut stream = lopdf::Stream::new(
            dictionary! {
                "Type" => "XObject",
                "Subtype" => Object::Name(b"Image".to_vec()),
                "Width" => Object::Integer(width),
                "Height" => Object::Integer(height),
                "ColorSpace" => Object::Name(b"DeviceGray".to_vec()),
                "BitsPerComponent" => Object::Integer(8),
            },
            data,
        );
        // A raster of scan size is deflated, as a producer stores it.
        stream.compress().expect("a raster deflates");
        let image_id = doc.add_object(Object::Stream(stream));
        xobjects.set(name, Object::Reference(image_id));
        image_id
    };
    // The images and forms made so far, by name, for a form's own
    // resources to bind.
    let mut named: Vec<(&str, ObjectId)> = Vec::new();
    let mut page_image = None;
    if image {
        let image_id = add_image(&mut doc, "Im0", 2, 2, vec![200, 60, 60, 200]);
        page_image = Some(image_id);
        named.push(("Im0", image_id));
    }
    if large_image {
        let image_id = add_image(&mut doc, "ImBig", 1500, 2383, vec![128; 1500 * 2383]);
        named.push(("ImBig", image_id));
    }
    for form in forms {
        let mut resources = dictionary! {
            "Font" => dictionary! { "F1" => Object::Reference(font_id) },
        };
        if !form.xobjects.is_empty() {
            let mut bound = dictionary! {};
            for &(name, stands_for) in form.xobjects {
                let &(_, id) = named
                    .iter()
                    .find(|(known, _)| *known == stands_for)
                    .expect("an image or an earlier form of the page");
                bound.set(name, Object::Reference(id));
            }
            resources.set("XObject", bound);
        }
        let mut dict = dictionary! {
            "Type" => "XObject",
            "Subtype" => Object::Name(b"Form".to_vec()),
            "BBox" => form
                .bbox
                .iter()
                .map(|&value| Object::Integer(value))
                .collect::<Vec<_>>(),
            "Resources" => resources,
        };
        if let Some(matrix) = form.matrix {
            dict.set(
                "Matrix",
                matrix
                    .iter()
                    .map(|&value| Object::Integer(value))
                    .collect::<Vec<_>>(),
            );
        }
        let form_id = doc.add_object(Object::Stream(lopdf::Stream::new(
            dict,
            form.content.as_bytes().to_vec(),
        )));
        xobjects.set(form.name, Object::Reference(form_id));
        named.push((form.name, form_id));
    }
    let mut pattern_dict = dictionary! {};
    for pattern in patterns {
        let object = if pattern.shading {
            Object::Dictionary(dictionary! {
                "Type" => "Pattern",
                "PatternType" => Object::Integer(2),
                "Shading" => dictionary! {
                    "ShadingType" => Object::Integer(2),
                    "ColorSpace" => Object::Name(b"DeviceGray".to_vec()),
                    "Coords" => vec![0.into(), 0.into(), 612.into(), 792.into()],
                },
            })
        } else {
            let mut resources = dictionary! {
                "Font" => dictionary! { "F1" => Object::Reference(font_id) },
            };
            if let Some(image_id) = page_image {
                resources.set(
                    "XObject",
                    dictionary! { "Im0" => Object::Reference(image_id) },
                );
            }
            Object::Stream(lopdf::Stream::new(
                dictionary! {
                    "Type" => "Pattern",
                    "PatternType" => Object::Integer(1),
                    "PaintType" => Object::Integer(1),
                    "TilingType" => Object::Integer(1),
                    "BBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
                    "XStep" => Object::Integer(612),
                    "YStep" => Object::Integer(792),
                    "Resources" => resources,
                },
                pattern.content.as_bytes().to_vec(),
            ))
        };
        let pattern_id = doc.add_object(object);
        pattern_dict.set(pattern.name, Object::Reference(pattern_id));
    }
    let content_id = doc.add_object(Object::Stream(lopdf::Stream::new(
        dictionary! {},
        Vec::new(),
    )));

    doc.objects.insert(
        page_id,
        Object::Dictionary(dictionary! {
            "Type" => "Page",
            "Parent" => Object::Reference(pages_id),
            "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
            "Resources" => dictionary! {
                "Font" => dictionary! { "F1" => Object::Reference(font_id) },
                "XObject" => xobjects,
                "Pattern" => pattern_dict,
            },
            "Contents" => Object::Reference(content_id),
        }),
    );
    doc.objects.insert(
        pages_id,
        Object::Dictionary(dictionary! {
            "Type" => "Pages",
            "Kids" => vec![Object::Reference(page_id)],
            "Count" => Object::Integer(1),
        }),
    );
    let catalog_id = doc.add_object(dictionary! {
        "Type" => "Catalog",
        "Pages" => Object::Reference(pages_id),
    });
    doc.trailer.set("Root", Object::Reference(catalog_id));
    (doc, page_id, content_id)
}

pub(super) fn set_page_content(doc: &mut Document, content_id: ObjectId, content: &str) {
    doc.objects.insert(
        content_id,
        Object::Stream(lopdf::Stream::new(
            lopdf::dictionary! {},
            content.as_bytes().to_vec(),
        )),
    );
}

/// The executed tallies of `streams` run, in order, as the page's
/// content: (text ops, hidden text ops, covered image area).
pub(super) fn executed(doc: &Document, page_id: ObjectId, streams: &[&str]) -> (u32, u32, f64) {
    let state = executed_state(doc, page_id, streams);
    (
        state.executed_text_ops,
        state.executed_hidden_text_ops,
        state.covered_image_area(),
    )
}

/// The scan's state after `streams` ran, in order, as the page's content.
pub(super) fn executed_state<'a>(
    doc: &'a Document,
    page_id: ObjectId,
    streams: &[&str],
) -> ContentScanState<'a> {
    let page_box = visible_page_box(doc, page_id).unwrap_or(PageBox::LETTER);
    let (own, ancestors) = doc.get_page_resources(page_id).unwrap();
    let resources: Vec<&lopdf::Dictionary> = own
        .into_iter()
        .chain(
            ancestors
                .iter()
                .filter_map(|id| doc.get_dictionary(*id).ok()),
        )
        .collect();
    let mut state = ContentScanState::new(doc, page_box, true);
    for stream in streams {
        scan_content_stream(
            stream.as_bytes(),
            &mut HashSet::new(),
            &mut HashSet::new(),
            &mut state,
            &resources,
        );
    }
    state
}

pub(super) const PAGE_AREA: f64 = 612.0 * 792.0;
pub(super) const FULL_PAGE_IMAGE: &str = "q 612 0 0 792 0 0 cm /Im0 Do Q\n";

/// 120 one-glyph `Tj` blocks under `mode`, as a producer writes a text
/// layer.
pub(super) fn glyph_layer(mode: u8) -> String {
    let mut layer = format!("{mode} Tr\n");
    let glyphs = "thepagecarriesalayernobodysees".chars().cycle().take(120);
    for (n, glyph) in glyphs.enumerate() {
        let x = 72 + (n % 40) * 12;
        let y = 720 - (n / 40) * 14;
        layer.push_str(&format!(
            "BT 1 0 0 1 {x} {y} Tm /F1 10 Tf ({glyph}) Tj ET\n"
        ));
    }
    layer
}

pub(super) fn close(a: f64, b: f64) -> bool {
    (a - b).abs() < 1e-6
}

/// Within a tenth of the expected area — the grid's resolution costs a
/// cell along each edge — and nothing but zero for none.
pub(super) fn about(covered: f64, expected: f64) -> bool {
    if expected == 0.0 {
        covered == 0.0
    } else {
        (covered - expected).abs() <= expected * 0.1
    }
}

/// A one-page document: a 2×2 gray image drawn over the whole page when
/// `covering_image`; a layer of 120 one-glyph `Tj` blocks under
/// `layer_mode` when given, in the page's content or, when
/// `layer_in_form`, in a Form XObject the page invokes; and a visible
/// caption line when given.
pub(super) fn layered_scan_page(
    covering_image: bool,
    layer_mode: Option<u8>,
    layer_in_form: bool,
    caption: Option<&str>,
) -> (Document, ObjectId) {
    let layer = layer_mode.map(glyph_layer).unwrap_or_default();
    let forms: Vec<TestForm> = if layer_in_form {
        vec![TestForm {
            name: "Fm0",
            content: layer.as_str(),
            ..PAGE_FORM
        }]
    } else {
        Vec::new()
    };
    let (mut doc, page_id, content_id) = synthetic_page(covering_image, false, &forms);
    let mut content = String::new();
    if covering_image {
        content.push_str(FULL_PAGE_IMAGE);
    }
    if layer_in_form {
        content.push_str("/Fm0 Do\n");
    } else {
        content.push_str(&layer);
    }
    if let Some(caption) = caption {
        content.push_str(&format!("BT 0 Tr /F1 12 Tf 72 40 Td ({caption}) Tj ET\n"));
    }
    set_page_content(&mut doc, content_id, &content);
    (doc, page_id)
}

/// The object id of the form `name` bound in the page's resources.
pub(super) fn bound_form_id(doc: &Document, page_id: ObjectId, name: &[u8]) -> ObjectId {
    doc.get_dictionary(page_id)
        .unwrap()
        .get(b"Resources")
        .unwrap()
        .as_dict()
        .unwrap()
        .get(b"XObject")
        .unwrap()
        .as_dict()
        .unwrap()
        .get(name)
        .unwrap()
        .as_reference()
        .unwrap()
}

/// Binds `name` to the colour space object `space` in the `/ColorSpace`
/// dictionary of the page's resources, or of the form's own when `form`
/// is given.
pub(super) fn bind_colour_space(
    doc: &mut Document,
    page_id: ObjectId,
    form: Option<ObjectId>,
    name: &str,
    space: Object,
) {
    let mut resources = match form {
        None => doc
            .get_dictionary(page_id)
            .unwrap()
            .get(b"Resources")
            .unwrap(),
        Some(id) => doc
            .get_object(id)
            .unwrap()
            .as_stream()
            .unwrap()
            .dict
            .get(b"Resources")
            .unwrap(),
    }
    .as_dict()
    .unwrap()
    .clone();
    let mut spaces = resources
        .get(b"ColorSpace")
        .ok()
        .and_then(|spaces| spaces.as_dict().ok())
        .cloned()
        .unwrap_or_else(lopdf::Dictionary::new);
    spaces.set(name, space);
    resources.set("ColorSpace", spaces);
    match form {
        None => doc
            .get_dictionary_mut(page_id)
            .unwrap()
            .set("Resources", resources),
        Some(id) => doc
            .get_object_mut(id)
            .unwrap()
            .as_stream_mut()
            .unwrap()
            .dict
            .set("Resources", resources),
    }
}