pdq 0.4.0

PDF operations library
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
423
424
425
426
427
428
429
430
431
432
433
434
435
use std::{
    fs,
    path::Path,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
};

use hayro::hayro_interpret::{
    font::Glyph,
    hayro_cmap::BfString,
    hayro_syntax::{page::Page, LoadPdfError, Pdf},
    interpret_page, BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache,
    InterpreterSettings, InterpreterWarning, Paint, PathDrawMode, SoftMask, TransformExt,
};
use kurbo::{Affine, BezPath, Point, Rect, Vec2};

use crate::{
    range::{dedupe_preserving_order, PageRangeGroup},
    PdfOpsError, Result,
};

/// Emitted `y` is the approximate glyph top: baseline minus this fraction of
/// the on-page em size (pdf.js's text-layer fallback ascent).
const ASCENT_FACTOR: f64 = 0.8;
/// hayro glyph transforms take font units (thousandths of an em) as input.
const FONT_UNITS_PER_EM: f64 = 1000.0;
/// Gap past a glyph's advance (in em) that reads as a word space rather than
/// kerning or tracking, synthesized as ' ' for PDFs that encode word gaps as
/// TJ offsets instead of space glyphs (LaTeX). Poppler's `minWordBreakSpace`
/// and pdf.js's `SPACE_IN_FLOW_MIN_FACTOR` both sit near 0.1 em; kerning
/// pairs stay well below it, word spaces (~0.2-0.5 em) well above.
const WORD_GAP_MIN: f64 = 0.1;
/// Widest gap still treated as an in-flow word space (pdf.js's
/// `SPACE_IN_FLOW_MAX_FACTOR`); anything wider starts a new run.
const WORD_GAP_MAX: f64 = 0.6;

#[derive(Debug, Clone, Default)]
pub struct ExtractTextOptions {
    pub pages: Option<PageRangeGroup>,
    pub password: Option<String>,
}

/// A horizontal (or uniformly-oriented) sequence of glyphs positioned on the
/// page. Coordinates are PDF points at 72 dpi with a top-left origin, after
/// the same rotation and cropbox handling `render` applies. `x`/`y` is the
/// top-left of the run's axis-aligned bounding box; for horizontal text
/// `width` is the sum of glyph advances and `height` equals `font_size`,
/// while rotated/vertical text yields a narrow, tall box.
#[derive(Debug, Clone, PartialEq)]
pub struct TextRun {
    pub text: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub font_size: f32,
}

#[derive(Debug, Clone, PartialEq)]
pub struct PageText {
    pub page: usize,
    pub width: f32,
    pub height: f32,
    /// True when at least one glyph on the page could not be mapped to
    /// Unicode (emitted as U+FFFD) or the interpreter reported a font it
    /// could not decode. Distinguishes "extraction failed" from "no text".
    pub degraded: bool,
    pub runs: Vec<TextRun>,
}

pub fn extract_text(input: &Path, options: &ExtractTextOptions) -> Result<Vec<PageText>> {
    let data = fs::read(input)?;
    let pdf = load_pdf(data, options.password.as_deref(), input)?;
    let pages = pdf.pages();

    let selected = match &options.pages {
        Some(range) => dedupe_preserving_order(&range.resolve(pages.len())?),
        None => (1..=pages.len()).collect(),
    };

    let cache = InterpreterCache::new();
    Ok(selected
        .iter()
        .map(|&page_number| extract_page(&pages[page_number - 1], page_number, &cache))
        .collect())
}

fn load_pdf(data: Vec<u8>, password: Option<&str>, input: &Path) -> Result<Pdf> {
    let result = match password {
        Some(password) => Pdf::new_with_password(data, password),
        None => Pdf::new(data),
    };
    result.map_err(|err| match err {
        LoadPdfError::Decryption(_) => PdfOpsError::Password(match password {
            Some(_) => format!("invalid password for {}", input.display()),
            None => format!(
                "{} is encrypted; supply --password to decrypt it",
                input.display()
            ),
        }),
        LoadPdfError::Invalid => PdfOpsError::InvalidStructure(format!(
            "failed to parse PDF for text extraction: {}",
            input.display()
        )),
    })
}

fn extract_page<'a>(page: &Page<'a>, page_number: usize, cache: &InterpreterCache<'a>) -> PageText {
    let (width, height) = page.render_dimensions();
    let initial_transform = page.initial_transform(true).to_kurbo();

    let font_warning = Arc::new(AtomicBool::new(false));
    let sink_flag = font_warning.clone();
    let settings = InterpreterSettings {
        warning_sink: Arc::new(move |warning| {
            if matches!(warning, InterpreterWarning::UnsupportedFont) {
                sink_flag.store(true, Ordering::Relaxed);
            }
        }),
        // The text layer covers page content only; annotation appearance
        // streams get their own layer in viewers (pdf.js does the same).
        render_annotations: false,
        ..Default::default()
    };

    let mut context = Context::new(
        initial_transform,
        Rect::new(0.0, 0.0, width as f64, height as f64),
        cache,
        page.xref(),
        settings,
    );
    let mut device = TextDevice::default();
    interpret_page(page, &mut context, &mut device);

    let (runs, missing_unicode) = device.finish();
    PageText {
        page: page_number,
        width,
        height,
        degraded: missing_unicode || font_warning.load(Ordering::Relaxed),
        runs,
    }
}

struct RunState {
    text: String,
    /// Baseline origin of the first glyph.
    start: Point,
    /// Baseline point past the last glyph's advance.
    end: Point,
    font_size: f64,
    /// Unit vector pointing from the baseline toward the glyph top.
    up: Vec2,
    /// Unit vector along the advance direction.
    dir: Vec2,
    /// Where the next glyph's origin should land if the text continues
    /// uninterrupted; `None` when the glyph's advance width is unknown.
    expected: Option<Point>,
    last_origin: Point,
}

#[derive(Default)]
struct TextDevice {
    runs: Vec<TextRun>,
    current: Option<RunState>,
    missing_unicode: bool,
    last_fill_origin: Option<Point>,
}

impl TextDevice {
    fn finish(&mut self) -> (Vec<TextRun>, bool) {
        self.flush();
        (std::mem::take(&mut self.runs), self.missing_unicode)
    }

    fn flush(&mut self) {
        if let Some(run) = self.current.take() {
            if !run.text.trim().is_empty() {
                let ascent = run.up * (ASCENT_FACTOR * run.font_size);
                let descent = run.up * ((ASCENT_FACTOR - 1.0) * run.font_size);
                let corners = [
                    run.start + ascent,
                    run.start + descent,
                    run.end + ascent,
                    run.end + descent,
                ];
                let (mut min, mut max) = (corners[0], corners[0]);
                for corner in &corners[1..] {
                    min.x = min.x.min(corner.x);
                    min.y = min.y.min(corner.y);
                    max.x = max.x.max(corner.x);
                    max.y = max.y.max(corner.y);
                }
                self.runs.push(TextRun {
                    text: run.text,
                    x: min.x as f32,
                    y: min.y as f32,
                    width: (max.x - min.x) as f32,
                    height: (max.y - min.y) as f32,
                    font_size: run.font_size as f32,
                });
            }
        }
    }

    /// Whether the glyph at `origin` continues the current run; `Some(true)`
    /// means it continues after a word-sized gap (synthesize a space).
    fn continues_current(
        &self,
        origin: Point,
        font_size: f64,
        up: Vec2,
        dir: Vec2,
    ) -> Option<bool> {
        let current = self.current.as_ref()?;
        if (font_size - current.font_size).abs() > 0.02 * current.font_size
            || current.dir.dot(dir) < 0.99
            || current.up.dot(up) < 0.99
        {
            return None;
        }
        let (reference, gap_range, has_advance) = match current.expected {
            Some(expected) => (expected, (-0.25, WORD_GAP_MAX), true),
            // Without advance metrics (Type3 fonts), measure from the
            // previous origin and accept anything up to a wide glyph plus
            // a word gap; the glyph width is indistinguishable from the
            // gap, so no space can be synthesized.
            None => (current.last_origin, (-0.05, 1.6), false),
        };
        let delta = origin - reference;
        let along = delta.dot(current.dir) / current.font_size;
        let perpendicular = delta.dot(current.up) / current.font_size;
        if perpendicular.abs() > 0.15 || along < gap_range.0 || along > gap_range.1 {
            return None;
        }
        Some(has_advance && along >= WORD_GAP_MIN)
    }
}

impl<'a> Device<'a> for TextDevice {
    fn draw_glyph(
        &mut self,
        glyph: &Glyph<'a>,
        transform: Affine,
        glyph_transform: Affine,
        _paint: &Paint<'a>,
        draw_mode: &GlyphDrawMode,
    ) {
        let to_device = transform * glyph_transform;
        let origin = to_device * Point::ZERO;
        let em_x = to_device * Point::new(FONT_UNITS_PER_EM, 0.0) - origin;
        let em_y = to_device * Point::new(0.0, FONT_UNITS_PER_EM) - origin;
        let font_size = em_y.hypot();
        if !origin.x.is_finite()
            || !origin.y.is_finite()
            || !font_size.is_finite()
            || font_size <= 0.0
            || em_x.hypot() <= 0.0
        {
            return;
        }

        // FillStroke rendering modes draw the same glyph twice; keep only
        // the fill pass. Stroke-only text still comes through because no
        // fill preceded it at the same origin.
        if matches!(draw_mode, GlyphDrawMode::Stroke(_)) {
            if self.last_fill_origin == Some(origin) {
                return;
            }
        } else {
            self.last_fill_origin = Some(origin);
        }

        let text = match glyph.as_unicode() {
            Some(BfString::Char(c)) => c.to_string(),
            Some(BfString::String(s)) => s,
            None => {
                self.missing_unicode = true;
                '\u{FFFD}'.to_string()
            }
        };

        let up = em_y / font_size;
        let dir = em_x.normalize();
        let advance = match glyph {
            Glyph::Outline(outline) => outline
                .advance_width()
                .map(|w| to_device * Point::new(w as f64, 0.0) - origin),
            Glyph::Type3(_) => None,
        };
        let expected = advance.map(|a| origin + a);
        // Without an advance, estimate the glyph's extent as half an em so
        // the box still covers most of the final glyph.
        let end = expected.unwrap_or(origin + dir * (0.5 * font_size));

        if let Some(word_gap) = self.continues_current(origin, font_size, up, dir) {
            let current = self.current.as_mut().unwrap();
            if word_gap
                && !current.text.ends_with(char::is_whitespace)
                && !text.starts_with(char::is_whitespace)
            {
                current.text.push(' ');
            }
            current.text.push_str(&text);
            current.last_origin = origin;
            current.expected = expected;
            current.end = end;
        } else {
            self.flush();
            self.current = Some(RunState {
                text,
                start: origin,
                end,
                font_size,
                up,
                dir,
                expected,
                last_origin: origin,
            });
        }
    }

    fn set_soft_mask(&mut self, _: Option<SoftMask<'a>>) {}
    fn set_blend_mode(&mut self, _: BlendMode) {}
    fn draw_path(&mut self, _: &BezPath, _: Affine, _: &Paint<'a>, _: &PathDrawMode) {}
    fn push_clip_path(&mut self, _: &ClipPath) {}
    fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'a>>, _: BlendMode) {}
    fn draw_image(&mut self, _: Image<'a, '_>, _: Affine) {}
    fn pop_clip_path(&mut self) {}
    fn pop_transparency_group(&mut self) {}
}

/// Serialize pages as the `pdq text` stdout JSON (an array of page objects).
pub fn pages_to_json(pages: &[PageText]) -> String {
    let mut out = String::from("[");
    for (i, page) in pages.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        out.push_str(&format!(
            "{{\"page\":{},\"page_width\":{},\"page_height\":{},\"degraded\":{},\"runs\":[",
            page.page,
            format_number(page.width),
            format_number(page.height),
            page.degraded
        ));
        for (j, run) in page.runs.iter().enumerate() {
            if j > 0 {
                out.push(',');
            }
            out.push_str(&format!(
                "{{\"text\":\"{}\",\"x\":{},\"y\":{},\"width\":{},\"height\":{},\"font_size\":{}}}",
                escape_json(&run.text),
                format_number(run.x),
                format_number(run.y),
                format_number(run.width),
                format_number(run.height),
                format_number(run.font_size)
            ));
        }
        out.push_str("]}");
    }
    out.push(']');
    out
}

/// Numbers rounded to 1/1000 pt; f64 Display keeps them free of float noise.
fn format_number(value: f32) -> String {
    let rounded = (value as f64 * 1000.0).round() / 1000.0;
    if rounded.is_finite() {
        format!("{rounded}")
    } else {
        "0".to_string()
    }
}

fn escape_json(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for c in text.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn json_escapes_control_and_quote_characters() {
        assert_eq!(escape_json("a\"b\\c\nd\u{1}e"), "a\\\"b\\\\c\\nd\\u0001e");
    }

    #[test]
    fn numbers_render_without_float_noise() {
        assert_eq!(format_number(72.0), "72");
        assert_eq!(format_number(57.599_996), "57.6");
        assert_eq!(format_number(f32::NAN), "0");
    }

    #[test]
    fn json_shape_matches_schema() {
        let pages = vec![PageText {
            page: 1,
            width: 612.0,
            height: 792.0,
            degraded: false,
            runs: vec![TextRun {
                text: "Invoice".to_string(),
                x: 72.0,
                y: 70.5,
                width: 57.0,
                height: 18.0,
                font_size: 18.0,
            }],
        }];
        assert_eq!(
            pages_to_json(&pages),
            "[{\"page\":1,\"page_width\":612,\"page_height\":792,\"degraded\":false,\
             \"runs\":[{\"text\":\"Invoice\",\"x\":72,\"y\":70.5,\
             \"width\":57,\"height\":18,\"font_size\":18}]}]"
        );
    }
}