pdf2 1.2.0

A PDF 2.0 (ISO 32000-2) reader, writer and text extractor.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Content-stream interpretation, far enough to extract text (§7.8.2, §9).
//!
//! The interpreter walks a decoded content stream with the same tokenizer the
//! object parser uses: operands are pushed on a stack and the operator that
//! follows consumes them. Only the text operators change what is extracted —
//! `BT`, `ET`, `Tf`, `Td`, `TD`, `T*`, `TL`, `Tm`, `Tc`, `Tw`, `Tz`, `Ts`,
//! `Tj`, `TJ`, `'` and `"` — while everything else is consumed and ignored, so
//! graphics-heavy pages do not disturb the text run.
//!
//! Limits, on purpose and documented in the README: simple fonts (Type1,
//! TrueType, Type3 with a single-byte encoding) and composite (Type0) fonts that
//! carry a `/ToUnicode` CMap over an Identity encoding. Codes for composite fonts
//! are split by the CMap's code space, and their `/W`/`/DW` widths advance the
//! pen so a gap between runs becomes a space; gaps are not guessed for simple
//! fonts, where line breaks still come from text-positioning operators and spaces
//! from the text itself or from wide `TJ` adjustments.
//!
//! Lines are ordered geometrically — descending `y`, then ascending `x` — rather
//! than by the order the content stream paints them, because a writer is free to
//! paint a page bottom-up or in stripes; `ShowText-ShadingPattern.pdf` in the
//! corpus paints 2, 1, 4, 3 and reads 1, 2, 3, 4. The decision and its limits are
//! in `docs/adr/0002-text-extraction-shape.md`.

use alloc::string::String;
use alloc::vec::Vec;

use alloc::collections::BTreeMap;

use crate::document::Document;
use crate::encoding::Encoding;
use crate::error::{Error, Result};
use crate::lexer::Token;
use crate::object::{Dictionary, Object};
use crate::page::Page;
use crate::parser::Parser;

/// A `TJ` adjustment larger than this (in thousandths of an em) is treated as
/// a word gap. Wire values land between 200 and 400 for a real space.
const WORD_GAP: f64 = 200.0;

/// Movement in text space smaller than this is the same line.
const LINE_TOLERANCE: f64 = 0.01;

/// A run of text at one position, before the lines are put in reading order.
#[derive(Debug, Default)]
struct Line {
    /// Text-space position of the line's first character.
    x: f64,
    y: f64,
    text: String,
}

/// How a composite font's codes become text.
#[derive(Debug, Clone)]
pub(crate) enum CodeMap {
    /// The page's `/ToUnicode` CMap.
    ToUnicode(crate::cmap::ToUnicode),
    /// A predefined CMap shipped as data (`/90ms-RKSJ-H` and friends).
    Predefined(crate::cmap::Predefined),
}

impl CodeMap {
    fn text(&self, code: u32, length: u8) -> Option<alloc::string::String> {
        match self {
            Self::ToUnicode(map) => map.text(code, length).map(alloc::borrow::ToOwned::to_owned),
            Self::Predefined(map) => map.text(code, length).map(|text| {
                let mut buffer = alloc::string::String::new();
                buffer.push(text);
                buffer
            }),
        }
    }

    fn code_lengths(&self) -> &[u8] {
        match self {
            Self::ToUnicode(map) => map.code_lengths(),
            Self::Predefined(map) => map.code_lengths(),
        }
    }

    fn max_code_length(&self) -> u8 {
        match self {
            Self::ToUnicode(map) => map.max_code_length(),
            Self::Predefined(map) => map.max_code_length(),
        }
    }
}

/// A CID's advance width, from `/W` with `/DW` as the default (9.7.4.3).
#[derive(Debug, Clone, Default)]
struct Widths {
    /// `/DW`, in thousandths of an em.
    default: f64,
    /// `/W` entries: first CID, last CID, widths in order.
    runs: Vec<(u32, u32, Vec<f64>)>,
}

impl Widths {
    /// The advance for `cid`, in thousandths of an em.
    fn width(&self, cid: u32) -> f64 {
        for (first, last, widths) in &self.runs {
            if cid >= *first && cid <= *last {
                let index = usize::try_from(cid - first).unwrap_or(usize::MAX);
                if let Some(width) = widths.get(index) {
                    return *width;
                }
            }
        }
        self.default
    }
}

/// A font, and the way its codes turn into text.
#[derive(Debug, Clone)]
enum Font {
    /// One byte per code, resolved through an encoding table.
    Simple {
        /// Code → character table.
        encoding: Encoding,
    },
    /// A composite font: codes split by the CMap's code space, text from
    /// `/ToUnicode` or a predefined CMap, advances from `/W` and `/DW`.
    Composite {
        /// Code → text, from `/ToUnicode` when present, otherwise from the
        /// predefined CMap the `/Encoding` names.
        map: CodeMap,
        /// CID advances.
        widths: Widths,
    },
}

impl Font {
    /// The text `bytes` stand for, and the advance in thousandths of an em.
    ///
    /// Simple fonts report no advance: their `/Widths` are not read, so pen
    /// tracking would be a guess, and a guess becomes a wrong space.
    fn decode(&self, bytes: &[u8]) -> (String, f64) {
        let Self::Composite { map, widths } = self else {
            let Self::Simple { encoding } = self else {
                return (String::new(), 0.0);
            };
            return (
                bytes
                    .iter()
                    .filter_map(|code| encoding.character(*code))
                    .collect(),
                0.0,
            );
        };
        let mut text = String::new();
        let mut advance = 0.0;
        let lengths = map.code_lengths();
        let narrowest = lengths.first().map_or(1, |length| usize::from(*length));
        let widest = usize::from(map.max_code_length());
        let mut position = 0;
        while position < bytes.len() {
            // A code space with one width is the common case and splits exactly;
            // a variable one is tried widest first, because a variable code space
            // means "the longest code that matches wins".
            let mut consumed = 0;
            let mut width = if lengths.len() == 1 {
                narrowest
            } else {
                widest
            };
            while width >= narrowest {
                if position + width <= bytes.len() {
                    let code = pack(&bytes[position..position + width]);
                    let matched = lengths.len() == 1
                        || map.text(code, u8::try_from(width).unwrap_or(2)).is_some();
                    if matched {
                        consumed = width;
                        break;
                    }
                }
                if width == narrowest {
                    break;
                }
                width -= 1;
            }
            if consumed == 0 {
                // Nothing maps this position: step over the narrowest code so an
                // unmapped byte cannot stall the loop.
                if position + narrowest > bytes.len() {
                    break;
                }
                position += narrowest;
                continue;
            }
            let code = pack(&bytes[position..position + consumed]);
            if let Some(mapped) = map.text(code, u8::try_from(consumed).unwrap_or(2)) {
                text.push_str(&mapped);
            }
            advance += widths.width(code);
            position += consumed;
        }
        (text, advance / 1000.0)
    }

    /// Whether `decode` reports advances, so the pen can be tracked.
    fn tracks_advances(&self) -> bool {
        matches!(self, Self::Composite { .. })
    }
}

/// A character code packed big-endian into a `u32`.
fn pack(bytes: &[u8]) -> u32 {
    bytes
        .iter()
        .fold(0_u32, |value, &byte| (value << 8) | u32::from(byte))
}

/// The text state and the glyph mapping needed to read a page.
struct TextInterpreter<'a> {
    document: &'a Document,
    resources: Option<Dictionary>,
    fonts: BTreeMap<Vec<u8>, Font>,
    font: Option<Font>,
    leading: f64,
    /// Font size from `Tf`, needed to turn a pen position into an em.
    size: f64,
    /// Current text line origin, in text space.
    line_x: f64,
    line_y: f64,
    /// Where the pen is after the text shown since the line was placed.
    pen_x: f64,
    /// Whether `pen_x` is meaningful for the current font.
    pen_tracked: bool,
    /// Lines in the order they were painted; sorted for the result.
    lines: Vec<Line>,
}

impl<'a> TextInterpreter<'a> {
    fn new(document: &'a Document, resources: Option<Dictionary>) -> Self {
        Self {
            document,
            resources,
            fonts: BTreeMap::new(),
            font: None,
            leading: 0.0,
            size: 0.0,
            line_x: 0.0,
            line_y: 0.0,
            pen_x: 0.0,
            pen_tracked: false,
            lines: Vec::new(),
        }
    }

    fn run(mut self, content: &[u8]) -> Result<String> {
        let mut parser = Parser::new(content);
        let mut operands: Vec<Object> = Vec::new();
        loop {
            match parser.peek_token()? {
                Token::Eof => break,
                Token::Keyword(_) => {
                    let keyword = match parser.next_token()? {
                        Token::Keyword(keyword) => keyword,
                        _ => return Err(Error::malformed("content-stream operator")),
                    };
                    self.execute(&keyword, &mut operands, &mut parser)?;
                    operands.clear();
                }
                _ => {
                    let object = parser.parse_object()?;
                    operands.push(object);
                }
            }
        }
        Ok(self.reading_order())
    }

    /// The painted lines, ordered for a reader rather than for the painter.
    ///
    /// Descending `y` is top to bottom in default user space; `x` breaks ties so
    /// that a line's fragments stay left to right. A page with several columns
    /// sorts left-to-right within a band — an approximation this crate states in
    /// the ADR rather than guesses at with a layout engine.
    fn reading_order(mut self) -> String {
        self.lines.retain(|line| !line.text.is_empty());
        self.lines.sort_by(|left, right| {
            right
                .y
                .partial_cmp(&left.y)
                .unwrap_or(core::cmp::Ordering::Equal)
                .then(
                    left.x
                        .partial_cmp(&right.x)
                        .unwrap_or(core::cmp::Ordering::Equal),
                )
        });
        let mut output = String::new();
        for line in &self.lines {
            if !output.is_empty() {
                output.push('\n');
            }
            output.push_str(&line.text);
        }
        output
    }

    /// The line at the current text position, created on first use.
    fn line(&mut self) -> &mut Line {
        if let Some(index) = self
            .lines
            .iter()
            .position(|line| difference(line.y, self.line_y) <= LINE_TOLERANCE)
        {
            return &mut self.lines[index];
        }
        self.lines.push(Line {
            x: self.line_x,
            y: self.line_y,
            text: String::new(),
        });
        #[allow(clippy::unwrap_used)]
        self.lines.last_mut().unwrap()
    }

    fn execute(
        &mut self,
        operator: &[u8],
        operands: &mut [Object],
        parser: &mut Parser<'_>,
    ) -> Result<()> {
        match operator {
            b"BT" => {
                self.line_x = 0.0;
                self.line_y = 0.0;
            }
            b"ET" => {}
            b"Tf" => {
                let name = match operands.first() {
                    Some(Object::Name(name)) => name.clone(),
                    _ => return Err(Error::malformed("/Tf needs a font name")),
                };
                self.size = number(operands.get(1));
                self.font = Some(self.load_font(&name)?);
                self.pen_tracked = self.font.as_ref().is_some_and(Font::tracks_advances);
                self.pen_x = self.line_x;
            }
            b"Td" | b"TD" => {
                let tx = number(operands.first());
                let ty = number(operands.get(1));
                let previous_y = self.line_y;
                self.line_x += tx;
                self.line_y += ty;
                // A move along the line leaves the pen where the text stopped,
                // which is what lets a gap become a space; a move down starts a
                // new line, so the pen starts there.
                if difference(previous_y, self.line_y) > LINE_TOLERANCE {
                    self.pen_x = self.line_x;
                }
                if operator == b"TD" {
                    self.leading = -ty;
                }
            }
            b"T*" => {
                self.line_y -= self.leading;
                self.pen_x = self.line_x;
            }
            b"TL" => {
                self.leading = number(operands.first());
            }
            b"Tm" => {
                // Only the translation part matters for text extraction.
                let previous_y = self.line_y;
                self.line_x = number(operands.get(4));
                self.line_y = number(operands.get(5));
                if difference(previous_y, self.line_y) > LINE_TOLERANCE {
                    self.pen_x = self.line_x;
                }
            }
            // Character spacing, word spacing, horizontal scaling and rise
            // do not change which characters a text run produces.
            b"Tc" | b"Tw" | b"Tz" | b"Ts" => {}
            b"Tj" => {
                if let Some(Object::String(string)) = operands.first() {
                    self.show(string.as_bytes());
                }
            }
            b"'" => {
                self.line_y -= self.leading;
                self.pen_x = self.line_x;
                if let Some(Object::String(string)) = operands.first() {
                    self.show(string.as_bytes());
                }
            }
            b"\"" => {
                // Set word and character spacing, then show on the next line.
                self.line_y -= self.leading;
                self.pen_x = self.line_x;
                if let Some(Object::String(string)) = operands.get(2) {
                    self.show(string.as_bytes());
                }
            }
            b"TJ" => {
                if let Some(Object::Array(items)) = operands.first() {
                    for item in items {
                        match item {
                            Object::String(string) => self.show(string.as_bytes()),
                            Object::Integer(adjustment) => self.adjust(*adjustment as f64),
                            Object::Real(adjustment) => self.adjust(*adjustment),
                            _ => return Err(Error::malformed("unsupported /TJ array entry")),
                        }
                    }
                }
            }
            b"BI" => {
                // Inline images are opaque data; text cannot hide inside them,
                // so skip to the `EI` that closes them.
                skip_inline_image(parser)?;
            }
            _ => {}
        }
        Ok(())
    }

    fn adjust(&mut self, adjustment: f64) {
        // Positive values move the pen left, so a wide negative adjustment is
        // how writers usually encode a space inside a `TJ` array.
        if -adjustment < WORD_GAP {
            return;
        }
        let line = self.line();
        if !line.text.is_empty() && !line.text.ends_with(' ') {
            line.text.push(' ');
        }
        if self.pen_tracked {
            // A positive adjustment moves the pen left, so the sign is inverted.
            self.pen_x -= adjustment / 1000.0 * self.size;
        }
    }

    fn show(&mut self, bytes: &[u8]) {
        // Collect first so the borrow of `font` ends before `line()` is called.
        let Some(font) = &self.font else {
            return;
        };
        let (run, advance) = font.decode(bytes);
        // A new run placed well to the right of where the pen stopped is a gap a
        // reader sees as a space; only fonts whose widths are known are trusted
        // with it, and a quarter of an em is the threshold a word space clears.
        let gap =
            self.pen_tracked && self.size > 0.0 && self.line_x > self.pen_x + 0.25 * self.size;
        let line = self.line();
        if gap && !line.text.is_empty() && !line.text.ends_with(' ') {
            line.text.push(' ');
        }
        line.text.push_str(&run);
        if self.pen_tracked {
            self.pen_x = self.line_x + advance * self.size;
        }
    }

    fn load_font(&mut self, name: &[u8]) -> Result<Font> {
        if let Some(font) = self.fonts.get(name) {
            return Ok(font.clone());
        }
        let font = self.read_font(name)?;
        self.fonts.insert(name.to_vec(), font.clone());
        Ok(font)
    }

    fn read_font(&self, name: &[u8]) -> Result<Font> {
        let font = match self
            .resources
            .as_ref()
            .and_then(|resources| resources.get(b"Font"))
        {
            Some(fonts) => {
                let fonts = self.document.resolve(Some(fonts))?.unwrap_or(Object::Null);
                match fonts {
                    Object::Dictionary(fonts) => fonts.get(name).cloned(),
                    _ => None,
                }
            }
            None => None,
        };
        let Some(font) = font else {
            return Err(Error::Malformed {
                what: "content stream selects a font the page does not declare",
                offset: None,
            });
        };
        let font = self.document.resolve(Some(&font))?.unwrap_or(Object::Null);
        let Object::Dictionary(dict) = font else {
            return Err(Error::Malformed {
                what: "font resource is not a dictionary",
                offset: None,
            });
        };
        if let Some(Object::Name(subtype)) = dict.get(b"Subtype") {
            if subtype == b"Type0" {
                return self.composite_font(&dict);
            }
        }
        Ok(Font::Simple {
            encoding: self.encoding_of(&dict)?,
        })
    }

    /// A Type0 font: an Identity encoding, a `/ToUnicode` map, and its widths.
    ///
    /// Only Identity encodings are read. Any other `/Encoding` names a CMap
    /// resource — `/90ms-RKSJ-H` and friends — whose bytes this crate does not
    /// ship yet, and an embedded CMap stream is the same problem from the inside;
    /// guessing a code space would produce confidently wrong text, so those are
    /// refused by name instead.
    fn composite_font(&self, dict: &Dictionary) -> Result<Font> {
        match dict.get(b"Encoding") {
            Some(Object::Name(name)) if name == b"Identity-H" => {}
            Some(Object::Name(name)) if name == b"Identity-V" => {
                return Err(Error::Unsupported {
                    what: "vertical composite (Type0) fonts",
                })
            }
            Some(Object::Name(name)) => {
                // A predefined CMap is data: when we ship it, the encoding is
                // readable; when we do not, the name says which one is missing.
                if let Some(predefined) = crate::cmap::Predefined::by_name(name) {
                    return Ok(Font::Composite {
                        map: CodeMap::Predefined(predefined),
                        widths: self.widths_of(dict)?,
                    });
                }
                return Err(Error::Unsupported {
                    what: "CMap-encoded composite (Type0) fonts",
                });
            }
            Some(Object::Reference(_)) | Some(Object::Stream(_)) => {
                return Err(Error::Unsupported {
                    what: "embedded CMap-encoded composite (Type0) fonts",
                })
            }
            _ => {
                return Err(Error::Unsupported {
                    what: "composite (Type0) fonts without an /Encoding",
                })
            }
        }
        let Some(to_unicode) = dict.get(b"ToUnicode") else {
            return Err(Error::Unsupported {
                what: "composite (Type0) fonts without a /ToUnicode map",
            });
        };
        let Object::Reference(id) = to_unicode else {
            return Err(Error::Unsupported {
                what: "a direct /ToUnicode stream",
            });
        };
        let map = crate::cmap::ToUnicode::parse(&self.document.stream_data(*id)?)?;
        Ok(Font::Composite {
            map: CodeMap::ToUnicode(map),
            widths: self.widths_of(dict)?,
        })
    }

    /// `/W` and `/DW` from the first `/DescendantFonts` entry (9.7.4.3).
    fn widths_of(&self, dict: &Dictionary) -> Result<Widths> {
        let mut widths = Widths {
            default: 1000.0,
            runs: Vec::new(),
        };
        let Some(descendants) = dict.get(b"DescendantFonts") else {
            return Ok(widths);
        };
        let descendants = self
            .document
            .resolve(Some(descendants))?
            .unwrap_or(Object::Null);
        let Object::Array(descendants) = descendants else {
            return Err(Error::Malformed {
                what: "/DescendantFonts must be an array",
                offset: None,
            });
        };
        let Some(first) = descendants.first() else {
            return Ok(widths);
        };
        let first = self.document.resolve(Some(first))?.unwrap_or(Object::Null);
        let Object::Dictionary(descendant) = first else {
            return Ok(widths);
        };
        if let Some(default) = descendant.get(b"DW") {
            widths.default = number(Some(default));
        }
        let Some(w_array) = descendant.get(b"W") else {
            return Ok(widths);
        };
        let w_array = self
            .document
            .resolve(Some(w_array))?
            .unwrap_or(Object::Null);
        let Object::Array(items) = w_array else {
            return Err(Error::Malformed {
                what: "/W must be an array",
                offset: None,
            });
        };
        let mut index = 0;
        while index < items.len() {
            let Some(Object::Integer(first)) = items.get(index) else {
                return Err(Error::Malformed {
                    what: "/W entries start with a CID",
                    offset: None,
                });
            };
            let first = u32::try_from(*first).map_err(|_| Error::Malformed {
                what: "/W CIDs must not be negative",
                offset: None,
            })?;
            match items.get(index + 1) {
                Some(Object::Array(list)) => {
                    let run: Vec<f64> = list.iter().map(|item| number(Some(item))).collect();
                    let last = first.saturating_add(
                        u32::try_from(run.len().saturating_sub(1)).unwrap_or(u32::MAX),
                    );
                    widths.runs.push((first, last, run));
                    index += 2;
                }
                Some(Object::Integer(last)) => {
                    let width = number(items.get(index + 2));
                    let last = u32::try_from(*last).map_err(|_| Error::Malformed {
                        what: "/W CIDs must not be negative",
                        offset: None,
                    })?;
                    widths.runs.push((first, last, alloc::vec![width]));
                    index += 3;
                }
                _ => {
                    return Err(Error::Malformed {
                        what: "/W entry is neither an array nor a range",
                        offset: None,
                    })
                }
            }
        }
        Ok(widths)
    }

    fn encoding_of(&self, dict: &Dictionary) -> Result<Encoding> {
        let Some(encoding_object) = dict.get(b"Encoding") else {
            return Ok(Encoding::standard());
        };
        let encoding_object = self
            .document
            .resolve(Some(encoding_object))?
            .unwrap_or(Object::Null);
        let mut encoding = Encoding::from_object(Some(&encoding_object))?;
        if let Object::Dictionary(differences) = &encoding_object {
            if let Some(differences) = differences.get(b"Differences") {
                let differences = self
                    .document
                    .resolve(Some(differences))?
                    .unwrap_or(Object::Null);
                let Object::Array(items) = differences else {
                    return Err(Error::Malformed {
                        what: "/Differences must be an array",
                        offset: None,
                    });
                };
                encoding.apply_differences(&items)?;
            }
        }
        Ok(encoding)
    }
}

/// Skip inline image data: everything up to the `EI` operator.
fn skip_inline_image(parser: &mut Parser<'_>) -> Result<()> {
    let search = parser.raw_position();
    let found = parser
        .content()
        .get(search..)
        .and_then(|rest| find_operator(rest, b"EI"))
        .ok_or(Error::malformed("inline image without a closing EI"))?;
    parser.seek(search + found + 2);
    Ok(())
}

/// Find `needle` as a whole operator: preceded by whitespace or a delimiter.
fn find_operator(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    let mut index = 0;
    while index + needle.len() <= haystack.len() {
        if &haystack[index..index + needle.len()] == needle {
            let before_ok = index == 0
                || crate::lexer::is_whitespace(haystack[index - 1])
                || crate::lexer::is_delimiter(haystack[index - 1]);
            let after = index + needle.len();
            let after_ok = after >= haystack.len()
                || crate::lexer::is_whitespace(haystack[after])
                || crate::lexer::is_delimiter(haystack[after]);
            if before_ok && after_ok {
                return Some(index);
            }
        }
        index += 1;
    }
    None
}

/// `|left - right|` without the `std`-only `f64::abs`.
fn difference(left: f64, right: f64) -> f64 {
    if left >= right {
        left - right
    } else {
        right - left
    }
}

fn number(operand: Option<&Object>) -> f64 {
    match operand {
        Some(Object::Integer(value)) => *value as f64,
        Some(Object::Real(value)) => *value,
        _ => 0.0,
    }
}

impl Document {
    /// Extract the text of a page's content streams.
    pub fn extract_text(&self, page: &Page) -> Result<String> {
        let content = self.page_content(page)?;
        self.extract_text_from(&content, page.resources.as_ref())
    }

    /// Extract text from a decoded content stream and its resource dictionary.
    pub fn extract_text_from(
        &self,
        content: &[u8],
        resources: Option<&Dictionary>,
    ) -> Result<String> {
        TextInterpreter::new(self, resources.cloned()).run(content)
    }
}

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

    #[test]
    fn inline_image_terminator_must_be_a_whole_operator() {
        assert_eq!(find_operator(b"abc EI more", b"EI"), Some(4));
        assert_eq!(find_operator(b"EI", b"EI"), Some(0));
        assert_eq!(
            find_operator(b"tailEIx", b"EI"),
            None,
            "EI inside a word is not the operator"
        );
        assert_eq!(find_operator(b"no terminator", b"EI"), None);
    }
}