Skip to main content

rustium_pdf/
page.rs

1//! Page primitives: interpreting content streams into glyphs, paths and images.
2//!
3//! The interpreter walks a page's operators maintaining the graphics and text state the PDF
4//! specification defines, and emits flat primitives in **user space** — PDF's y-up coordinate
5//! system with the origin at the media box's lower-left corner. [`Page::page_matrix`] converts
6//! to the y-down device space a rasteriser or a layout consumer wants, applying `/Rotate` and
7//! the crop box in one step.
8
9use std::collections::{HashMap, HashSet};
10use std::sync::Arc;
11
12use crate::content::ContentParser;
13use crate::document::{page_content, Document, PageNode};
14use crate::error::{Error, Result};
15use crate::font::glyph::PathCmd;
16use crate::font::{CodeItem, Font, FontFlags};
17use crate::geom::{Matrix, Point, Rect};
18use crate::object::{text_string, Dict, ObjRef, Object};
19
20/// Nesting limit for form XObjects and Type3 glyph procedures.
21const MAX_DEPTH: usize = 12;
22/// A horizontal gap this many ems wide, on an unchanged baseline, reads as a word break.
23const SPACE_GAP_EM: f32 = 0.12;
24/// Baselines within this fraction of an em count as the same line for gap detection.
25const BASELINE_EPSILON_EM: f32 = 0.10;
26
27/// One glyph placed on the page.
28#[derive(Debug, Clone)]
29pub struct Glyph {
30    /// The text this glyph contributes. Usually one character; ligatures expand to several.
31    ///
32    /// Inside a marked-content sequence that carries `/ActualText`, the document has said what
33    /// the sequence reads as, and that replaces what its glyphs would say one by one: the first
34    /// glyph of the sequence holds the whole replacement and the rest hold nothing. Every glyph
35    /// keeps its own geometry either way.
36    pub text: String,
37    /// The baseline origin, in user space.
38    pub origin: Point,
39    /// The glyph's bounding box in user space: tight around the outline when the font program
40    /// is available, and a nominal em box derived from the advance when it is not.
41    pub bbox: Rect,
42    /// The advance from this glyph's origin to the next, in user space units.
43    pub advance: f32,
44    /// The effective on-page font size: `/Tf` size scaled by the text and current matrices.
45    pub font_size: f32,
46    /// The resource name the font was reached by (`/F1`).
47    pub font_name: String,
48    /// The font's `/BaseFont`.
49    pub base_font: String,
50    /// Typeface properties of the font this glyph came from.
51    pub flags: FontFlags,
52    /// Baseline rotation in radians, counter-clockwise from the positive x-axis.
53    pub rotation: f32,
54    /// True when this glyph was synthesized from a positioning gap rather than read from a
55    /// string, so that a word break the page draws as a gap is still a space in the output.
56    pub is_generated_space: bool,
57    /// The text rendering mode in force (`/Tr`). Mode 3 and 7 are invisible.
58    pub render_mode: i32,
59    /// Fill colour as linear RGB in 0..=1.
60    pub color: [f32; 3],
61    /// Constant fill alpha from the graphics state (`/ca`), in 0..=1.
62    pub alpha: f32,
63    /// Paint order across every primitive on the page. Sorting glyphs, paths and images by
64    /// this reproduces the order the content stream drew them in, which is what a renderer
65    /// needs and what the separate per-type vectors would otherwise lose.
66    pub order: u32,
67
68    /// The font and code this glyph came from, kept so [`Glyph::outline`] can resolve the
69    /// shape on demand. Storing the outline itself would mean tens of thousands of copies of
70    /// paths the font already caches once each.
71    font: Option<Arc<Font>>,
72    code: CodeItem,
73    /// Maps glyph-space outline coordinates to user space.
74    glyph_matrix: Matrix,
75}
76
77impl Glyph {
78    /// Whether the glyph paints anything. Invisible text is what scanned pages put under their
79    /// page image, so extraction keeps it and rendering skips it.
80    pub fn is_visible(&self) -> bool {
81        self.render_mode != 3 && self.render_mode != 7
82    }
83
84    /// The glyph's outline in glyph space, resolved through the font's cache.
85    ///
86    /// `None` for a generated space, a Type3 glyph (whose marks the interpreter already emitted
87    /// as paths) or a font whose program is absent or uninterpretable.
88    pub fn outline(&self) -> Option<Arc<crate::font::glyph::Outline>> {
89        if self.is_generated_space {
90            return None;
91        }
92        self.font.as_ref()?.outline(&self.code)
93    }
94
95    /// Maps this glyph's outline coordinates into user space.
96    pub fn outline_matrix(&self) -> Matrix {
97        self.glyph_matrix
98    }
99
100    /// The character code this glyph was selected by, as it appeared in the content stream.
101    ///
102    /// Useful when [`Glyph::text`] is empty: the code plus the font is what identifies which
103    /// mapping is missing.
104    pub fn code(&self) -> u32 {
105        self.code.code
106    }
107
108    /// The glyph selector: the code itself for a simple font, the CID for a composite one.
109    pub fn cid(&self) -> u32 {
110        self.code.cid
111    }
112}
113
114/// A filled and/or stroked path.
115#[derive(Debug, Clone)]
116pub struct PathItem {
117    /// Path commands in user space.
118    pub cmds: Vec<PathCmd>,
119    pub bbox: Rect,
120    pub fill: Option<[f32; 3]>,
121    pub stroke: Option<[f32; 3]>,
122    /// Stroke width in user space units.
123    pub line_width: f32,
124    /// True when the fill uses the even-odd rule rather than nonzero winding.
125    pub even_odd: bool,
126    /// Constant alphas from the graphics state (`/ca` and `/CA`), in 0..=1.
127    pub fill_alpha: f32,
128    pub stroke_alpha: f32,
129    /// Paint order; see [`Glyph::order`].
130    pub order: u32,
131}
132
133/// The pixel payload of an image, as far as the byte-level filters could take it.
134#[derive(Debug, Clone)]
135pub enum ImageData {
136    /// Fully decoded samples, row-major, `bits_per_component` per component.
137    Raw {
138        bits_per_component: u8,
139        components: u8,
140        data: Vec<u8>,
141    },
142    /// A codec payload the byte filters do not decode (`DCTDecode`, `JPXDecode`, the fax and
143    /// JBIG2 filters); `data` is that codec's own bitstream.
144    Encoded { filter: String, data: Vec<u8> },
145}
146
147/// An image drawn onto the page.
148#[derive(Debug, Clone)]
149pub struct ImageItem {
150    /// The XObject resource name, or `"inline"` for a `BI ... EI` image.
151    pub name: String,
152    /// Maps the image's unit square onto the page, in user space. The image's first row is at
153    /// the top of the unit square, so this matrix already includes the y flip.
154    pub ctm: Matrix,
155    pub bbox: Rect,
156    pub width: u32,
157    pub height: u32,
158    /// True for a stencil mask (`/ImageMask`), which paints the fill colour through 1-bit data.
159    pub is_mask: bool,
160    /// Bits per component and component count, known without touching the pixels.
161    pub bits_per_component: u8,
162    pub components: u8,
163    /// The colour a stencil mask paints through, and the constant alpha in force.
164    pub fill: [f32; 3],
165    pub alpha: f32,
166    /// Paint order; see [`Glyph::order`].
167    pub order: u32,
168    source: ImageSource,
169}
170
171/// Where an image's pixels come from.
172///
173/// Image XObjects are referenced rather than carried: a page of photographs holds tens of
174/// megabytes of samples, and a layout pass that only needs each image's box should not pay for
175/// them. Inline images are already bounded by the content stream, so they are kept as decoded.
176#[derive(Debug, Clone)]
177enum ImageSource {
178    Object(ObjRef),
179    Inline(Arc<ImageData>),
180}
181
182impl ImageItem {
183    /// Decodes this image's pixels, reading them from the document if they were not retained.
184    pub fn decode(&self, doc: &Document) -> Result<ImageData> {
185        match &self.source {
186            ImageSource::Inline(data) => Ok((**data).clone()),
187            ImageSource::Object(r) => {
188                let obj = doc.resolve(&Object::Ref(*r));
189                let stream = obj.as_stream().ok_or_else(|| {
190                    Error::Parse(format!("image object {} is not a stream", r.num))
191                })?;
192                let decoded = doc.file.decode_stream(stream)?;
193                Ok(match decoded.image_filter {
194                    Some(filter) => ImageData::Encoded {
195                        filter,
196                        data: decoded.data,
197                    },
198                    None => ImageData::Raw {
199                        bits_per_component: self.bits_per_component,
200                        components: self.components,
201                        data: decoded.data,
202                    },
203                })
204            }
205        }
206    }
207}
208
209/// A page's primitives, with the geometry needed to place them.
210pub struct Page {
211    pub index: usize,
212    pub media_box: Rect,
213    pub crop_box: Rect,
214    /// Clockwise display rotation in degrees: 0, 90, 180 or 270.
215    pub rotation: i32,
216    pub glyphs: Vec<Glyph>,
217    pub paths: Vec<PathItem>,
218    pub images: Vec<ImageItem>,
219}
220
221impl Page {
222    pub(crate) fn build(doc: &Document, node: &PageNode, index: usize) -> Result<Page> {
223        let content = page_content(doc, &node.dict);
224        let mut interp = Interpreter::new(doc);
225        interp.run(&content, &node.resources, State::new(node.crop_box), 0);
226
227        Ok(Page {
228            index,
229            media_box: node.media_box,
230            crop_box: node.crop_box,
231            rotation: node.rotation,
232            glyphs: interp.glyphs,
233            paths: interp.paths,
234            images: interp.images,
235        })
236    }
237
238    /// The page's displayed width in points, after rotation.
239    pub fn width(&self) -> f32 {
240        if self.rotation % 180 == 0 {
241            self.crop_box.width()
242        } else {
243            self.crop_box.height()
244        }
245    }
246
247    /// The page's displayed height in points, after rotation.
248    pub fn height(&self) -> f32 {
249        if self.rotation % 180 == 0 {
250            self.crop_box.height()
251        } else {
252            self.crop_box.width()
253        }
254    }
255
256    /// Maps user space to y-down device space at `scale` pixels per point, with the origin at
257    /// the top-left of the displayed page and `/Rotate` applied.
258    pub fn page_matrix(&self, scale: f32) -> Matrix {
259        // Shift the crop box to the origin, flip y, then rotate about the resulting page.
260        let to_origin = Matrix::translate(-self.crop_box.x0, -self.crop_box.y0);
261        let w = self.crop_box.width();
262        let h = self.crop_box.height();
263        let flip = Matrix::new(1.0, 0.0, 0.0, -1.0, 0.0, h);
264        let rotate = match self.rotation.rem_euclid(360) {
265            90 => Matrix::new(0.0, 1.0, -1.0, 0.0, h, 0.0),
266            180 => Matrix::new(-1.0, 0.0, 0.0, -1.0, w, h),
267            270 => Matrix::new(0.0, -1.0, 1.0, 0.0, 0.0, w),
268            _ => Matrix::IDENTITY,
269        };
270        to_origin
271            .concat(&flip)
272            .concat(&rotate)
273            .concat(&Matrix::scale(scale, scale))
274    }
275
276    /// Whether this page looks like a scan: a page-sized image and no text worth speaking of.
277    ///
278    /// Reading a scan needs OCR, which is out of scope — but a caller that cannot tell a scan
279    /// from a failed extraction reports an empty page as success, and a user who uploaded a
280    /// photocopied paper is told nothing at all. Detecting the case costs a comparison against
281    /// geometry already computed, and lets the caller say why it produced nothing.
282    ///
283    /// A page carrying an invisible OCR text layer is *not* reported as scanned: its text is
284    /// real and extraction works, which is the whole point of that layer.
285    pub fn is_likely_scanned(&self) -> bool {
286        const MIN_COVERAGE: f32 = 0.5;
287        const MAX_GLYPHS_PER_PAGE: usize = 32;
288
289        if self
290            .glyphs
291            .iter()
292            .filter(|g| !g.text.trim().is_empty())
293            .count()
294            > MAX_GLYPHS_PER_PAGE
295        {
296            return false;
297        }
298        let page = self.crop_box;
299        let area = page.width() * page.height();
300        if area <= 0.0 {
301            return false;
302        }
303        // One image covering most of the page, rather than the total over many, so that a
304        // figure-heavy page with little text is not mistaken for a scan.
305        self.images.iter().any(|img| {
306            let visible = img.bbox.intersect(&page);
307            !visible.is_empty() && (visible.width() * visible.height()) / area >= MIN_COVERAGE
308        })
309    }
310
311    /// The page's text in content-stream order, with soft hyphens removed.
312    ///
313    /// Content order is the order operators appear, which is not always reading order; callers
314    /// that need reading order sort [`Page::glyphs`] by geometry themselves.
315    pub fn text(&self) -> String {
316        let mut out = String::new();
317        let mut prev: Option<&Glyph> = None;
318        for g in &self.glyphs {
319            // A content stream carries no line breaks: a new line is just a glyph placed at a
320            // different baseline. Concatenating blindly fuses the last word of one line onto
321            // the first of the next — "of" + "the" becomes "ofthe" — which silently destroys
322            // the commonest words in the document.
323            if let Some(p) = prev {
324                if starts_new_line(p, g) {
325                    out.push('\n');
326                }
327            }
328            out.push_str(&g.text);
329            if !g.text.is_empty() {
330                prev = Some(g);
331            }
332        }
333        out
334    }
335}
336
337/// Whether `g` begins a new line relative to the glyph before it.
338///
339/// Judged geometrically rather than from the operators, because `Td`, `TD`, `T*`, `Tm` and a
340/// fresh `BT` can all start a line, and plenty of producers use `Tm` for every single line.
341fn starts_new_line(prev: &Glyph, g: &Glyph) -> bool {
342    let size = prev.font_size.max(g.font_size);
343    if size <= 0.0 {
344        return false;
345    }
346    // Rotated runs are compared along their own baseline direction, so a rotated column does
347    // not read as one break per glyph.
348    if (prev.rotation - g.rotation).abs() > 0.01 {
349        return true;
350    }
351    let (dx, dy) = (g.origin.x - prev.origin.x, g.origin.y - prev.origin.y);
352    let (cos, sin) = (prev.rotation.cos(), prev.rotation.sin());
353    let along = dx * cos + dy * sin;
354    let across = -dx * sin + dy * cos;
355
356    // A baseline shift of a third of an em is a new line; so is any backward jump larger than
357    // one em, which is how a wrapped line returns to the left margin.
358    across.abs() > size * 0.55 || along < -size
359}
360
361// ---- interpreter ----------------------------------------------------------------------------
362
363/// The graphics state, saved and restored by `q`/`Q`.
364#[derive(Clone)]
365struct State {
366    ctm: Matrix,
367    clip: Rect,
368    fill: [f32; 3],
369    stroke: [f32; 3],
370    line_width: f32,
371    /// Number of components in the current fill colour space, to interpret `sc`/`scn`.
372    fill_components: usize,
373    stroke_components: usize,
374    /// Constant alphas from `/ca` and `/CA`.
375    fill_alpha: f32,
376    stroke_alpha: f32,
377    // Text state persists across BT/ET, so it lives here rather than in the text object.
378    font: Option<Arc<Font>>,
379    font_name: String,
380    font_size: f32,
381    char_spacing: f32,
382    word_spacing: f32,
383    /// `/Tz` as a plain factor, not a percentage.
384    horizontal_scale: f32,
385    leading: f32,
386    rise: f32,
387    render_mode: i32,
388}
389
390impl State {
391    fn new(clip: Rect) -> State {
392        State {
393            ctm: Matrix::IDENTITY,
394            clip,
395            fill: [0.0; 3],
396            stroke: [0.0; 3],
397            line_width: 1.0,
398            fill_components: 1,
399            stroke_components: 1,
400            fill_alpha: 1.0,
401            stroke_alpha: 1.0,
402            font: None,
403            font_name: String::new(),
404            font_size: 0.0,
405            char_spacing: 0.0,
406            word_spacing: 0.0,
407            horizontal_scale: 1.0,
408            leading: 0.0,
409            rise: 0.0,
410            render_mode: 0,
411        }
412    }
413}
414
415/// Where the previous glyph ended, for synthesizing word breaks.
416#[derive(Clone, Copy)]
417struct PenTrail {
418    end: Point,
419    baseline_y: f32,
420    size: f32,
421}
422
423struct Interpreter<'a> {
424    doc: &'a Document,
425    glyphs: Vec<Glyph>,
426    paths: Vec<PathItem>,
427    images: Vec<ImageItem>,
428    /// Fonts keyed by object number, so a font shared by many pages parses once per page build.
429    font_cache: HashMap<u32, Arc<Font>>,
430    /// Form XObjects currently being executed, to stop a self-referential form.
431    active_forms: HashSet<u32>,
432    trail: Option<PenTrail>,
433    /// Whether the text most recently emitted ended in whitespace, so a gap after it does not
434    /// earn a second space. Glyphs silenced by an `/ActualText` span leave it alone.
435    last_was_space: bool,
436    /// One entry per open marked-content sequence (`BMC`/`BDC`), true for the one that opened
437    /// `span`. Kept on the interpreter rather than in `run` because a form XObject invoked
438    /// inside a sequence is part of it.
439    marked: Vec<bool>,
440    /// The `/ActualText` replacement in force, if any.
441    span: Option<ActualText>,
442    /// Monotonic paint-order counter, shared by every primitive type.
443    order: u32,
444}
445
446/// An open marked-content sequence whose `/ActualText` replaces the text of the glyphs in it.
447///
448/// Producers use it where a glyph cannot speak for itself — Chromium's PDF backend ships
449/// ligatures and some digits with no `/ToUnicode` entry and only this to say what they are —
450/// and where what is drawn differs from what is meant, such as a hyphen at a line break.
451struct ActualText {
452    /// The replacement, until the first glyph of the sequence takes it; empty from then on.
453    /// Empty from the start when the document declares that the glyphs carry no text.
454    pending: String,
455}
456
457/// The state of the current text object, reset by `BT`.
458struct TextState {
459    matrix: Matrix,
460    line_matrix: Matrix,
461}
462
463impl<'a> Interpreter<'a> {
464    fn new(doc: &'a Document) -> Self {
465        Interpreter {
466            doc,
467            glyphs: Vec::new(),
468            paths: Vec::new(),
469            images: Vec::new(),
470            font_cache: HashMap::new(),
471            active_forms: HashSet::new(),
472            trail: None,
473            last_was_space: false,
474            marked: Vec::new(),
475            span: None,
476            order: 0,
477        }
478    }
479
480    /// Opens a marked-content sequence. `properties` is the `BDC` operand — an inline
481    /// dictionary or a name into the `/Properties` resources — and absent for `BMC`.
482    fn begin_marked(&mut self, properties: Option<&Object>, resources: &Dict) {
483        // Sequences nest, and the outermost `/ActualText` speaks for everything inside it, so
484        // one that opens while another is in force changes nothing.
485        let replacement = match properties {
486            Some(props) if self.span.is_none() => {
487                let dict = match props {
488                    Object::Name(name) => self
489                        .doc
490                        .dict_get(resources, "Properties")
491                        .and_then(|p| p.as_dict().and_then(|d| self.doc.dict_get(d, name))),
492                    other => Some(self.doc.resolve(other)),
493                };
494                dict.and_then(|d| self.doc.dict_get(d.as_dict()?, "ActualText"))
495                    .and_then(|o| o.as_string().map(text_string))
496            }
497            _ => None,
498        };
499        self.marked.push(replacement.is_some());
500        if let Some(text) = replacement {
501            // Soft hyphens are no more content here than in a glyph's own mapping.
502            //
503            // A line break in the replacement becomes a space. Chromium tags space glyphs with
504            // an `/ActualText` of a line feed wherever the HTML it printed had a block or
505            // `<br>` boundary, and those fall in the middle of a visual line as often as at its
506            // end — "yi ding ( name@host )" carries four. Lines here are a matter of geometry,
507            // as everywhere else in this module; what the tag reliably says is that the glyph
508            // is whitespace.
509            let pending = text
510                .chars()
511                .filter(|&c| c != '\u{00AD}')
512                .map(|c| if matches!(c, '\n' | '\r') { ' ' } else { c })
513                .collect();
514            self.span = Some(ActualText { pending });
515        }
516    }
517
518    /// Closes the innermost marked-content sequence opened at or above `floor`, the depth the
519    /// current content stream started at: a stream cannot close a sequence its caller opened.
520    fn end_marked(&mut self, floor: usize) {
521        if self.marked.len() > floor && self.marked.pop() == Some(true) {
522            // A replacement no glyph claimed has no geometry to attach to, and is dropped.
523            self.span = None;
524        }
525    }
526
527    fn run(&mut self, content: &[u8], resources: &Dict, initial: State, depth: usize) {
528        if depth > MAX_DEPTH {
529            return;
530        }
531        let marked_floor = self.marked.len();
532        let mut stack: Vec<State> = Vec::new();
533        let mut gs = initial;
534        let mut text = TextState {
535            matrix: Matrix::IDENTITY,
536            line_matrix: Matrix::IDENTITY,
537        };
538        // The path under construction, in user space, plus the point a `h` closes back to.
539        let mut path: Vec<PathCmd> = Vec::new();
540        let mut subpath_start = Point::default();
541        let mut current = Point::default();
542        let mut pending_clip: Option<bool> = None;
543
544        for op in ContentParser::new(content) {
545            let n = |i: usize| op.operands.get(i).and_then(|o| o.as_f32());
546            match op.operator.as_str() {
547                // ---- graphics state ----
548                "q" => stack.push(gs.clone()),
549                "Q" => {
550                    if let Some(prev) = stack.pop() {
551                        gs = prev;
552                    }
553                }
554                "cm" => {
555                    if let (Some(a), Some(b), Some(c), Some(d), Some(e), Some(f)) =
556                        (n(0), n(1), n(2), n(3), n(4), n(5))
557                    {
558                        gs.ctm = Matrix::new(a, b, c, d, e, f).concat(&gs.ctm);
559                    }
560                }
561                "w" => {
562                    if let Some(v) = n(0) {
563                        gs.line_width = v;
564                    }
565                }
566                "gs" => {
567                    if let Some(name) = op.operands.first().and_then(|o| o.as_name()) {
568                        self.apply_ext_gstate(resources, name, &mut gs);
569                    }
570                }
571
572                // ---- colour ----
573                "g" | "G" => self.set_color(&mut gs, op.operator == "g", &op.operands, 1),
574                "rg" | "RG" => self.set_color(&mut gs, op.operator == "rg", &op.operands, 3),
575                "k" | "K" => self.set_color(&mut gs, op.operator == "k", &op.operands, 4),
576                "cs" | "CS" => {
577                    let comps = self.colorspace_components(resources, &op.operands);
578                    if op.operator == "cs" {
579                        gs.fill_components = comps;
580                        gs.fill = [0.0; 3];
581                    } else {
582                        gs.stroke_components = comps;
583                        gs.stroke = [0.0; 3];
584                    }
585                }
586                "sc" | "scn" | "SC" | "SCN" => {
587                    let is_fill = op.operator.starts_with('s');
588                    // A pattern operand carries a name and no usable colour; leave the previous
589                    // one in place rather than painting black.
590                    let numeric = op.operands.iter().filter_map(|o| o.as_f32()).count();
591                    if numeric > 0 {
592                        // The space declared by `cs`/`CS` says how to read the operands. A
593                        // producer that disagrees with its own declaration is trusted on the
594                        // operands it actually wrote, since that is what it meant to paint.
595                        let declared = if is_fill {
596                            gs.fill_components
597                        } else {
598                            gs.stroke_components
599                        };
600                        let components = if (1..=numeric).contains(&declared) {
601                            declared
602                        } else {
603                            numeric
604                        };
605                        self.set_color(&mut gs, is_fill, &op.operands, components);
606                    }
607                }
608
609                // ---- path construction ----
610                "m" => {
611                    if let (Some(x), Some(y)) = (n(0), n(1)) {
612                        current = gs.ctm.apply(Point::new(x, y));
613                        subpath_start = current;
614                        path.push(PathCmd::MoveTo(current));
615                    }
616                }
617                "l" => {
618                    if let (Some(x), Some(y)) = (n(0), n(1)) {
619                        current = gs.ctm.apply(Point::new(x, y));
620                        path.push(PathCmd::LineTo(current));
621                    }
622                }
623                "c" => {
624                    if let (Some(a), Some(b), Some(c), Some(d), Some(e), Some(f)) =
625                        (n(0), n(1), n(2), n(3), n(4), n(5))
626                    {
627                        let c1 = gs.ctm.apply(Point::new(a, b));
628                        let c2 = gs.ctm.apply(Point::new(c, d));
629                        current = gs.ctm.apply(Point::new(e, f));
630                        path.push(PathCmd::CurveTo(c1, c2, current));
631                    }
632                }
633                "v" => {
634                    // The first control point is the current point.
635                    if let (Some(c), Some(d), Some(e), Some(f)) = (n(0), n(1), n(2), n(3)) {
636                        let c2 = gs.ctm.apply(Point::new(c, d));
637                        let end = gs.ctm.apply(Point::new(e, f));
638                        path.push(PathCmd::CurveTo(current, c2, end));
639                        current = end;
640                    }
641                }
642                "y" => {
643                    // The second control point is the endpoint.
644                    if let (Some(a), Some(b), Some(e), Some(f)) = (n(0), n(1), n(2), n(3)) {
645                        let c1 = gs.ctm.apply(Point::new(a, b));
646                        let end = gs.ctm.apply(Point::new(e, f));
647                        path.push(PathCmd::CurveTo(c1, end, end));
648                        current = end;
649                    }
650                }
651                "h" => {
652                    path.push(PathCmd::Close);
653                    current = subpath_start;
654                }
655                "re" => {
656                    if let (Some(x), Some(y), Some(w), Some(h)) = (n(0), n(1), n(2), n(3)) {
657                        let p0 = gs.ctm.apply(Point::new(x, y));
658                        let p1 = gs.ctm.apply(Point::new(x + w, y));
659                        let p2 = gs.ctm.apply(Point::new(x + w, y + h));
660                        let p3 = gs.ctm.apply(Point::new(x, y + h));
661                        path.extend([
662                            PathCmd::MoveTo(p0),
663                            PathCmd::LineTo(p1),
664                            PathCmd::LineTo(p2),
665                            PathCmd::LineTo(p3),
666                            PathCmd::Close,
667                        ]);
668                        subpath_start = p0;
669                        current = p0;
670                    }
671                }
672
673                // ---- path painting ----
674                "n" | "f" | "F" | "f*" | "S" | "s" | "B" | "B*" | "b" | "b*" => {
675                    let o = op.operator.as_str();
676                    if matches!(o, "s" | "b" | "b*") {
677                        path.push(PathCmd::Close);
678                    }
679                    let even_odd = o.ends_with('*');
680                    let fills = matches!(o, "f" | "F" | "f*" | "B" | "B*" | "b" | "b*");
681                    let strokes = matches!(o, "S" | "s" | "B" | "B*" | "b" | "b*");
682                    // `W` names the path just painted, so the clip region must be measured
683                    // before the path is moved into the emitted item.
684                    let region = pending_clip.take().map(|_| bounds(&path));
685                    if !path.is_empty() && (fills || strokes) {
686                        let bbox = bounds(&path);
687                        let order = self.next_order();
688                        self.paths.push(PathItem {
689                            cmds: std::mem::take(&mut path),
690                            bbox,
691                            fill: fills.then_some(gs.fill),
692                            stroke: strokes.then_some(gs.stroke),
693                            // Line width is a user-space quantity scaled by the CTM.
694                            line_width: gs.line_width * gs.ctm.x_scale().max(gs.ctm.y_scale()),
695                            even_odd,
696                            fill_alpha: gs.fill_alpha,
697                            stroke_alpha: gs.stroke_alpha,
698                            order,
699                        });
700                    }
701                    // `W` takes effect only once the path is painted or discarded. The clip is
702                    // tracked as a bounding rectangle, which over-covers a non-rectangular
703                    // region — callers use it to reject content, never to include it.
704                    if let Some(region) = region {
705                        if !region.is_empty() {
706                            gs.clip = gs.clip.intersect(&region);
707                        }
708                    }
709                    path.clear();
710                }
711                "W" => pending_clip = Some(false),
712                "W*" => pending_clip = Some(true),
713
714                // ---- text objects ----
715                "BT" => {
716                    text.matrix = Matrix::IDENTITY;
717                    text.line_matrix = Matrix::IDENTITY;
718                    // The pen trail deliberately survives BT/ET. A text object boundary says
719                    // nothing about the page: producers routinely wrap every table cell, and
720                    // sometimes every word, in its own BT. Clearing here loses the gap between
721                    // them and fuses adjacent cells into one token. Geometry decides instead —
722                    // a different baseline is a line break, a wide gap on one is a space.
723                }
724                "ET" => {}
725                "Tc" => gs.char_spacing = n(0).unwrap_or(0.0),
726                "Tw" => gs.word_spacing = n(0).unwrap_or(0.0),
727                "Tz" => gs.horizontal_scale = n(0).unwrap_or(100.0) / 100.0,
728                "TL" => gs.leading = n(0).unwrap_or(0.0),
729                "Ts" => gs.rise = n(0).unwrap_or(0.0),
730                "Tr" => {
731                    gs.render_mode =
732                        op.operands.first().and_then(|o| o.as_int()).unwrap_or(0) as i32
733                }
734                "Tf" => {
735                    gs.font_size = n(1).unwrap_or(0.0);
736                    if let Some(name) = op.operands.first().and_then(|o| o.as_name()) {
737                        gs.font_name = name.to_string();
738                        gs.font = self.font_for(resources, name);
739                    }
740                }
741                "Td" => {
742                    if let (Some(tx), Some(ty)) = (n(0), n(1)) {
743                        text.line_matrix = Matrix::translate(tx, ty).concat(&text.line_matrix);
744                        text.matrix = text.line_matrix;
745                    }
746                }
747                "TD" => {
748                    if let (Some(tx), Some(ty)) = (n(0), n(1)) {
749                        gs.leading = -ty;
750                        text.line_matrix = Matrix::translate(tx, ty).concat(&text.line_matrix);
751                        text.matrix = text.line_matrix;
752                    }
753                }
754                "Tm" => {
755                    if let (Some(a), Some(b), Some(c), Some(d), Some(e), Some(f)) =
756                        (n(0), n(1), n(2), n(3), n(4), n(5))
757                    {
758                        text.line_matrix = Matrix::new(a, b, c, d, e, f);
759                        text.matrix = text.line_matrix;
760                    }
761                }
762                "T*" => next_line(&mut text, gs.leading),
763                "Tj" => {
764                    if let Some(Object::String(s)) = op.operands.first() {
765                        let s = s.clone();
766                        self.show(&s, &gs, &mut text, resources, depth);
767                    }
768                }
769                "'" => {
770                    next_line(&mut text, gs.leading);
771                    if let Some(Object::String(s)) = op.operands.first() {
772                        let s = s.clone();
773                        self.show(&s, &gs, &mut text, resources, depth);
774                    }
775                }
776                "\"" => {
777                    // `aw ac string "`: sets word and char spacing, then shows on a new line.
778                    gs.word_spacing = n(0).unwrap_or(gs.word_spacing);
779                    gs.char_spacing = n(1).unwrap_or(gs.char_spacing);
780                    next_line(&mut text, gs.leading);
781                    if let Some(Object::String(s)) = op.operands.get(2) {
782                        let s = s.clone();
783                        self.show(&s, &gs, &mut text, resources, depth);
784                    }
785                }
786                "TJ" => {
787                    let Some(Object::Array(items)) = op.operands.first().cloned() else {
788                        continue;
789                    };
790                    for item in items {
791                        match item {
792                            Object::String(s) => self.show(&s, &gs, &mut text, resources, depth),
793                            other => {
794                                if let Some(adj) = other.as_f32() {
795                                    // A positive number moves left, hence the negation.
796                                    let tx = -adj / 1000.0 * gs.font_size * gs.horizontal_scale;
797                                    text.matrix = Matrix::translate(tx, 0.0).concat(&text.matrix);
798                                }
799                            }
800                        }
801                    }
802                }
803
804                // ---- XObjects and inline images ----
805                "Do" => {
806                    if let Some(name) = op.operands.first().and_then(|o| o.as_name()) {
807                        let name = name.to_string();
808                        self.do_xobject(&name, resources, &gs, depth);
809                    }
810                }
811                "BI" => {
812                    if let (Some(dict), Some(data)) = (
813                        op.operands.first().and_then(|o| o.as_dict()),
814                        op.inline_data.as_ref(),
815                    ) {
816                        let dict = dict.clone();
817                        let data = data.clone();
818                        self.record_inline_image(&dict, &data, &gs, resources);
819                    }
820                }
821
822                // ---- marked content ----
823                "BMC" => self.begin_marked(None, resources),
824                "BDC" => {
825                    self.begin_marked(Some(op.operands.get(1).unwrap_or(&Object::Null)), resources)
826                }
827                "EMC" => self.end_marked(marked_floor),
828                _ => {}
829            }
830        }
831        // Sequences must balance within a stream; one left open by a damaged stream ends here
832        // rather than swallowing the text of everything drawn afterwards.
833        while self.marked.len() > marked_floor {
834            self.end_marked(marked_floor);
835        }
836    }
837
838    // ---- text ---------------------------------------------------------------------------
839
840    /// Shows one string, emitting a glyph per character code and advancing the text matrix.
841    fn show(
842        &mut self,
843        bytes: &[u8],
844        gs: &State,
845        text: &mut TextState,
846        resources: &Dict,
847        depth: usize,
848    ) {
849        let Some(font) = gs.font.clone() else {
850            return;
851        };
852        for item in font.decode(bytes) {
853            // The text rendering matrix places glyph space on the page:
854            // [size*Th, 0, 0, size, 0, rise] x Tm x CTM
855            let param = Matrix::new(
856                gs.font_size * gs.horizontal_scale,
857                0.0,
858                0.0,
859                gs.font_size,
860                0.0,
861                gs.rise,
862            );
863            let trm = param.concat(&text.matrix).concat(&gs.ctm);
864
865            let advance_text = (item.width / 1000.0 * gs.font_size
866                + gs.char_spacing
867                + if item.is_word_space {
868                    gs.word_spacing
869                } else {
870                    0.0
871                })
872                * gs.horizontal_scale;
873
874            // The advance is a text-space vector, so it reaches user space through Tm and the
875            // CTM — but not through `param`, which would apply the font size a second time.
876            let text_to_user = text.matrix.concat(&gs.ctm);
877            self.emit_glyph(&font, &item, &trm, &text_to_user, gs, advance_text);
878
879            if font.is_type3 {
880                self.draw_type3_glyph(&font, &item, &trm, gs, resources, depth);
881            }
882            text.matrix = Matrix::translate(advance_text, 0.0).concat(&text.matrix);
883        }
884    }
885
886    #[allow(clippy::too_many_arguments)]
887    fn emit_glyph(
888        &mut self,
889        font: &Font,
890        item: &CodeItem,
891        trm: &Matrix,
892        text_to_user: &Matrix,
893        gs: &State,
894        advance_text: f32,
895    ) {
896        // Soft hyphens mark optional break points and are not content; producers emit them at
897        // line ends where they would otherwise appear as stray hyphens in extracted text.
898        let own: String = item
899            .unicode
900            .as_deref()
901            .unwrap_or("")
902            .chars()
903            .filter(|&c| c != '\u{00AD}')
904            .collect();
905        // Inside an `/ActualText` span the document's replacement outranks the glyph's own
906        // mapping: the first glyph takes all of it and the rest go silent. A silent glyph is
907        // skipped by the gap logic below exactly as an unmapped one is, so the replacement is
908        // spaced as a unit — from its first glyph's origin and from its last glyph's end —
909        // and never broken up by gaps between the glyphs it replaces.
910        let in_span = self.span.is_some();
911        let text = match self.span.as_mut() {
912            Some(span) => std::mem::take(&mut span.pending),
913            None => own,
914        };
915
916        let origin = trm.apply(Point::default());
917        // The rendering matrix already folds in `/Tf` size, `Tm` and the CTM, so its vertical
918        // scale is the size the glyph actually appears at.
919        let effective_size = trm.y_scale();
920        // Transforming the advance as a vector keeps rotated and skewed text correct, and is
921        // exactly the displacement the interpreter applies to the text matrix after this glyph.
922        let (advance_dx, advance_dy) = text_to_user.apply_vector(advance_text, 0.0);
923        let advance_user = advance_dx.hypot(advance_dy);
924
925        // A gap on an unchanged baseline is a word break the string itself does not contain.
926        if !text.is_empty() {
927            if let Some(prev) = self.trail {
928                let same_line =
929                    (origin.y - prev.baseline_y).abs() <= BASELINE_EPSILON_EM * prev.size;
930                let gap = origin.x - prev.end.x;
931                if same_line
932                    && prev.size > 0.0
933                    && gap > SPACE_GAP_EM * prev.size
934                    && !self.last_was_space
935                    && !text.starts_with(char::is_whitespace)
936                {
937                    let order = self.next_order();
938                    self.glyphs.push(Glyph {
939                        text: " ".into(),
940                        origin: prev.end,
941                        bbox: Rect::from_corners(
942                            prev.end.x,
943                            prev.baseline_y,
944                            origin.x,
945                            prev.baseline_y + prev.size,
946                        ),
947                        advance: gap,
948                        font_size: prev.size,
949                        font_name: gs.font_name.clone(),
950                        base_font: font.base_font.clone(),
951                        flags: font.flags,
952                        rotation: trm.rotation(),
953                        is_generated_space: true,
954                        render_mode: gs.render_mode,
955                        color: gs.fill,
956                        alpha: gs.fill_alpha,
957                        order,
958                        font: None,
959                        code: item.clone(),
960                        glyph_matrix: *trm,
961                    });
962                }
963            }
964        }
965
966        if in_span {
967            // A silenced glyph says nothing about how the text before it ended.
968            if !text.is_empty() {
969                self.last_was_space = text.ends_with(char::is_whitespace);
970            }
971        } else {
972            // An unmapped glyph counts as a space here, as it always has: no gap after it
973            // generates one.
974            self.last_was_space = text.chars().all(char::is_whitespace);
975        }
976
977        let (bbox, glyph_matrix) = self.glyph_bbox(font, item, trm);
978        let order = self.next_order();
979        self.glyphs.push(Glyph {
980            text,
981            origin,
982            bbox,
983            advance: advance_user,
984            font_size: effective_size,
985            font_name: gs.font_name.clone(),
986            base_font: font.base_font.clone(),
987            flags: font.flags,
988            rotation: trm.rotation(),
989            is_generated_space: false,
990            render_mode: gs.render_mode,
991            color: gs.fill,
992            alpha: gs.fill_alpha,
993            order,
994            font: gs.font.clone(),
995            code: item.clone(),
996            glyph_matrix,
997        });
998
999        self.trail = Some(PenTrail {
1000            end: Point::new(origin.x + advance_dx, origin.y + advance_dy),
1001            baseline_y: origin.y,
1002            size: effective_size,
1003        });
1004    }
1005
1006    /// A glyph's box: tight around the outline when the font program yields one, otherwise a
1007    /// nominal em box from the advance, which is what a metrics-only font can support.
1008    ///
1009    /// A substituted outline is deliberately not trusted horizontally. Its letters are another
1010    /// typeface's, so their ink widths are not this font's — and horizontally the document has
1011    /// already given the exact answer in the advance. Taking the substitute's widths made the
1012    /// gap between two glyphs depend on which fonts happen to be installed, which is what turned
1013    /// a letterspaced `REFERENCES` into `REFEREN CES`. Vertically the substitute is kept: cap
1014    /// height, x-height and descender depth carry across text faces well enough to be worth far
1015    /// more than one nominal height applied to every glyph alike.
1016    fn glyph_bbox(&self, font: &Font, item: &CodeItem, trm: &Matrix) -> (Rect, Matrix) {
1017        // Type3 glyph space is arbitrary, so the font matrix rather than /1000 applies.
1018        let to_text = if font.is_type3 {
1019            font.font_matrix
1020        } else {
1021            Matrix::scale(0.001, 0.001)
1022        };
1023        let m = to_text.concat(trm);
1024        let advance = item.width.max(1.0);
1025        if let Some(outline) = font.outline(item) {
1026            if let Some(b) = outline.bbox() {
1027                let b = if outline.is_substitute {
1028                    Rect {
1029                        x0: 0.0,
1030                        x1: advance,
1031                        ..b
1032                    }
1033                } else {
1034                    b
1035                };
1036                return (m.apply_rect(&b), m);
1037            }
1038        }
1039        // Nominal box: baseline to ascender, descender below, spanning the advance.
1040        let nominal = Rect::from_corners(0.0, -200.0, advance, 800.0);
1041        (m.apply_rect(&nominal), m)
1042    }
1043
1044    /// Runs a Type3 glyph's procedure so the marks it makes become page primitives.
1045    fn draw_type3_glyph(
1046        &mut self,
1047        font: &Font,
1048        item: &CodeItem,
1049        trm: &Matrix,
1050        gs: &State,
1051        resources: &Dict,
1052        depth: usize,
1053    ) {
1054        if depth >= MAX_DEPTH {
1055            return;
1056        }
1057        let Some(proc_obj) = font.type3_proc(self.doc, item.code) else {
1058            return;
1059        };
1060        let Some(stream) = proc_obj.as_stream() else {
1061            return;
1062        };
1063        let Ok(decoded) = self.doc.file.decode_stream(stream) else {
1064            return;
1065        };
1066        let mut inner = gs.clone();
1067        // Inside the procedure, glyph space maps to the page through the font matrix.
1068        inner.ctm = font.font_matrix.concat(trm);
1069        inner.font = None;
1070        self.run(&decoded.data, resources, inner, depth + 1);
1071    }
1072
1073    // ---- resources ----------------------------------------------------------------------
1074
1075    fn font_for(&mut self, resources: &Dict, name: &str) -> Option<Arc<Font>> {
1076        let fonts = self.doc.dict_get(resources, "Font")?;
1077        let entry = fonts.as_dict()?.get(name)?.clone();
1078        if let Object::Ref(r) = &entry {
1079            if let Some(hit) = self.font_cache.get(&r.num) {
1080                return Some(hit.clone());
1081            }
1082        }
1083        let dict = self.doc.resolve(&entry).as_dict()?.clone();
1084        let font = Arc::new(Font::load(self.doc, &dict));
1085        if let Object::Ref(r) = &entry {
1086            self.font_cache.insert(r.num, font.clone());
1087        }
1088        Some(font)
1089    }
1090
1091    /// `/ExtGState` carries a handful of parameters that change geometry or visibility.
1092    fn apply_ext_gstate(&self, resources: &Dict, name: &str, gs: &mut State) {
1093        let Some(ext) = self
1094            .doc
1095            .dict_get(resources, "ExtGState")
1096            .and_then(|o| o.as_dict().and_then(|d| d.get(name).cloned()))
1097        else {
1098            return;
1099        };
1100        let Some(dict) = self.doc.resolve(&ext).as_dict().cloned() else {
1101            return;
1102        };
1103        if let Some(lw) = self.doc.dict_get(&dict, "LW").and_then(|o| o.as_f32()) {
1104            gs.line_width = lw;
1105        }
1106        // Constant alpha. Watermarks and highlight boxes are drawn with these, so a consumer
1107        // that ignores them cannot tell a faint overlay from solid page content.
1108        if let Some(ca) = self.doc.dict_get(&dict, "ca").and_then(|o| o.as_f32()) {
1109            gs.fill_alpha = ca.clamp(0.0, 1.0);
1110        }
1111        if let Some(ca) = self.doc.dict_get(&dict, "CA").and_then(|o| o.as_f32()) {
1112            gs.stroke_alpha = ca.clamp(0.0, 1.0);
1113        }
1114        // `/Font` in an ExtGState is `[fontRef size]`.
1115        if let Some(Object::Array(items)) = self.doc.dict_get(&dict, "Font") {
1116            if let Some(size) = items.get(1).and_then(|o| o.as_f32()) {
1117                gs.font_size = size;
1118            }
1119        }
1120    }
1121
1122    /// How many components a named colour space has, so `scn` operands can be read.
1123    fn colorspace_components(&self, resources: &Dict, operands: &[Object]) -> usize {
1124        let Some(name) = operands.first().and_then(|o| o.as_name()) else {
1125            return 1;
1126        };
1127        match name {
1128            "DeviceGray" | "CalGray" | "G" => return 1,
1129            "DeviceRGB" | "CalRGB" | "Lab" | "RGB" => return 3,
1130            "DeviceCMYK" | "CMYK" => return 4,
1131            "Pattern" => return 0,
1132            _ => {}
1133        }
1134        // A named space resolves through the resource dictionary.
1135        let Some(space) = self
1136            .doc
1137            .dict_get(resources, "ColorSpace")
1138            .and_then(|o| o.as_dict().and_then(|d| d.get(name).cloned()))
1139            .map(|o| self.doc.resolve(&o))
1140        else {
1141            return 1;
1142        };
1143        self.components_of(&space, 0)
1144    }
1145
1146    fn components_of(&self, space: &Object, depth: usize) -> usize {
1147        if depth > 4 {
1148            return 1;
1149        }
1150        match space {
1151            Object::Name(n) => match n.as_str() {
1152                "DeviceRGB" | "CalRGB" | "Lab" => 3,
1153                "DeviceCMYK" => 4,
1154                _ => 1,
1155            },
1156            Object::Array(items) => {
1157                let head = items.first().and_then(|o| o.as_name()).unwrap_or("");
1158                match head {
1159                    // Indexed and separation spaces take a single index or tint value.
1160                    "Indexed" | "I" | "Separation" => 1,
1161                    "DeviceN" => items
1162                        .get(1)
1163                        .map(|o| self.doc.resolve(o))
1164                        .and_then(|o| o.as_array().map(<[Object]>::len))
1165                        .unwrap_or(1),
1166                    "ICCBased" => items
1167                        .get(1)
1168                        .map(|o| self.doc.resolve(o))
1169                        .and_then(|o| o.as_dict().and_then(|d| d.get("N").cloned()))
1170                        .and_then(|o| o.as_int())
1171                        .unwrap_or(3)
1172                        .clamp(1, 4) as usize,
1173                    "CalRGB" | "Lab" => 3,
1174                    "CalGray" => 1,
1175                    "Pattern" => 0,
1176                    _ => self
1177                        .components_of(&items.first().cloned().unwrap_or(Object::Null), depth + 1),
1178                }
1179            }
1180            _ => 1,
1181        }
1182    }
1183
1184    fn set_color(&self, gs: &mut State, is_fill: bool, operands: &[Object], components: usize) {
1185        let v: Vec<f32> = operands.iter().filter_map(|o| o.as_f32()).collect();
1186        let rgb = match components.min(v.len()) {
1187            1 => {
1188                let g = v[0].clamp(0.0, 1.0);
1189                [g, g, g]
1190            }
1191            3 => [
1192                v[0].clamp(0.0, 1.0),
1193                v[1].clamp(0.0, 1.0),
1194                v[2].clamp(0.0, 1.0),
1195            ],
1196            4 => {
1197                // The standard naive CMYK conversion; a colour-managed one needs the ICC profile.
1198                let k = v[3].clamp(0.0, 1.0);
1199                [
1200                    (1.0 - v[0].clamp(0.0, 1.0)) * (1.0 - k),
1201                    (1.0 - v[1].clamp(0.0, 1.0)) * (1.0 - k),
1202                    (1.0 - v[2].clamp(0.0, 1.0)) * (1.0 - k),
1203                ]
1204            }
1205            _ => return,
1206        };
1207        if is_fill {
1208            gs.fill = rgb;
1209            gs.fill_components = components;
1210        } else {
1211            gs.stroke = rgb;
1212            gs.stroke_components = components;
1213        }
1214    }
1215
1216    // ---- XObjects -----------------------------------------------------------------------
1217
1218    fn do_xobject(&mut self, name: &str, resources: &Dict, gs: &State, depth: usize) {
1219        let Some(xobjects) = self.doc.dict_get(resources, "XObject") else {
1220            return;
1221        };
1222        let Some(entry) = xobjects.as_dict().and_then(|d| d.get(name).cloned()) else {
1223            return;
1224        };
1225        let obj_num = entry.as_ref_id().map(|r| r.num);
1226        let resolved = self.doc.resolve(&entry);
1227        let Some(stream) = resolved.as_stream() else {
1228            return;
1229        };
1230        let subtype = stream.dict.get("Subtype").and_then(|o| o.as_name());
1231
1232        match subtype {
1233            Some("Image") => self.record_image(name, stream, entry.as_ref_id(), gs),
1234            Some("Form") => {
1235                // A form that draws itself would recurse forever.
1236                if let Some(num) = obj_num {
1237                    if !self.active_forms.insert(num) {
1238                        return;
1239                    }
1240                }
1241                let Ok(decoded) = self.doc.file.decode_stream(stream) else {
1242                    if let Some(num) = obj_num {
1243                        self.active_forms.remove(&num);
1244                    }
1245                    return;
1246                };
1247                let mut inner = gs.clone();
1248                if let Some(m) = stream
1249                    .dict
1250                    .get("Matrix")
1251                    .map(|o| self.doc.resolve(o))
1252                    .as_ref()
1253                    .and_then(|o| o.as_array())
1254                {
1255                    let v: Vec<f32> = m.iter().filter_map(|o| o.as_f32()).collect();
1256                    if v.len() >= 6 {
1257                        inner.ctm = Matrix::new(v[0], v[1], v[2], v[3], v[4], v[5]).concat(&gs.ctm);
1258                    }
1259                }
1260                if let Some(bbox) = stream
1261                    .dict
1262                    .get("BBox")
1263                    .map(|o| self.doc.resolve(o))
1264                    .as_ref()
1265                    .and_then(|o| o.as_array())
1266                {
1267                    let v: Vec<f32> = bbox.iter().filter_map(|o| o.as_f32()).collect();
1268                    if v.len() >= 4 {
1269                        let r = inner
1270                            .ctm
1271                            .apply_rect(&Rect::from_corners(v[0], v[1], v[2], v[3]));
1272                        if !r.is_empty() {
1273                            inner.clip = inner.clip.intersect(&r);
1274                        }
1275                    }
1276                }
1277                // A form without its own /Resources inherits the page's.
1278                let inner_resources = self
1279                    .doc
1280                    .dict_get(&stream.dict, "Resources")
1281                    .and_then(|o| o.as_dict().cloned())
1282                    .unwrap_or_else(|| resources.clone());
1283                let data = decoded.data;
1284                self.run(&data, &inner_resources, inner, depth + 1);
1285                if let Some(num) = obj_num {
1286                    self.active_forms.remove(&num);
1287                }
1288            }
1289            _ => {}
1290        }
1291    }
1292
1293    fn record_image(
1294        &mut self,
1295        name: &str,
1296        stream: &crate::object::Stream,
1297        obj_ref: Option<ObjRef>,
1298        gs: &State,
1299    ) {
1300        let dict = &stream.dict;
1301        let width = self
1302            .doc
1303            .dict_get(dict, "Width")
1304            .and_then(|o| o.as_int())
1305            .unwrap_or(0)
1306            .max(0) as u32;
1307        let height = self
1308            .doc
1309            .dict_get(dict, "Height")
1310            .and_then(|o| o.as_int())
1311            .unwrap_or(0)
1312            .max(0) as u32;
1313        if width == 0 || height == 0 {
1314            return;
1315        }
1316        let is_mask = self
1317            .doc
1318            .dict_get(dict, "ImageMask")
1319            .and_then(|o| o.as_bool())
1320            .unwrap_or(false);
1321        let bpc = self
1322            .doc
1323            .dict_get(dict, "BitsPerComponent")
1324            .and_then(|o| o.as_int())
1325            .unwrap_or(if is_mask { 1 } else { 8 })
1326            .clamp(1, 16) as u8;
1327        let components = if is_mask {
1328            1
1329        } else {
1330            let space = self
1331                .doc
1332                .dict_get(dict, "ColorSpace")
1333                .unwrap_or(Object::Null);
1334            self.components_of(&space, 0).max(1) as u8
1335        };
1336
1337        // The pixels stay in the file until someone asks for them. When the image is not
1338        // reachable by reference the samples have to be carried, but a direct image XObject is
1339        // rare enough that the cost does not matter.
1340        let source = match obj_ref {
1341            Some(r) => ImageSource::Object(r),
1342            None => {
1343                let Ok(decoded) = self.doc.file.decode_stream(stream) else {
1344                    return;
1345                };
1346                ImageSource::Inline(Arc::new(match decoded.image_filter {
1347                    Some(filter) => ImageData::Encoded {
1348                        filter,
1349                        data: decoded.data,
1350                    },
1351                    None => ImageData::Raw {
1352                        bits_per_component: bpc,
1353                        components,
1354                        data: decoded.data,
1355                    },
1356                }))
1357            }
1358        };
1359        self.push_image(name, gs, width, height, is_mask, bpc, components, source);
1360    }
1361
1362    fn record_inline_image(&mut self, dict: &Dict, raw: &[u8], gs: &State, resources: &Dict) {
1363        let width = self
1364            .doc
1365            .dict_get(dict, "Width")
1366            .and_then(|o| o.as_int())
1367            .unwrap_or(0)
1368            .max(0) as u32;
1369        let height = self
1370            .doc
1371            .dict_get(dict, "Height")
1372            .and_then(|o| o.as_int())
1373            .unwrap_or(0)
1374            .max(0) as u32;
1375        if width == 0 || height == 0 {
1376            return;
1377        }
1378        let is_mask = self
1379            .doc
1380            .dict_get(dict, "ImageMask")
1381            .and_then(|o| o.as_bool())
1382            .unwrap_or(false);
1383        let bpc = self
1384            .doc
1385            .dict_get(dict, "BitsPerComponent")
1386            .and_then(|o| o.as_int())
1387            .unwrap_or(if is_mask { 1 } else { 8 })
1388            .clamp(1, 16) as u8;
1389        // An inline image's colour space may name an entry in the page's /ColorSpace resources.
1390        let components = if is_mask {
1391            1
1392        } else {
1393            match self.doc.dict_get(dict, "ColorSpace") {
1394                Some(Object::Name(n)) => {
1395                    self.colorspace_components(resources, &[Object::Name(n)]) as u8
1396                }
1397                Some(other) => self.components_of(&other, 0) as u8,
1398                None => 1,
1399            }
1400            .max(1)
1401        };
1402
1403        let resolve = |o: &Object| self.doc.resolve(o);
1404        let data = match crate::filters::decode(dict, raw, &resolve) {
1405            Ok(d) => match d.image_filter {
1406                Some(filter) => ImageData::Encoded {
1407                    filter,
1408                    data: d.data,
1409                },
1410                None => ImageData::Raw {
1411                    bits_per_component: bpc,
1412                    components,
1413                    data: d.data,
1414                },
1415            },
1416            Err(_) => return,
1417        };
1418        let source = ImageSource::Inline(Arc::new(data));
1419        self.push_image(
1420            "inline", gs, width, height, is_mask, bpc, components, source,
1421        );
1422    }
1423
1424    #[allow(clippy::too_many_arguments)]
1425    fn push_image(
1426        &mut self,
1427        name: &str,
1428        gs: &State,
1429        width: u32,
1430        height: u32,
1431        is_mask: bool,
1432        bits_per_component: u8,
1433        components: u8,
1434        source: ImageSource,
1435    ) {
1436        // Image space has its first row at the top of the unit square, so flip y into the CTM.
1437        let ctm = Matrix::new(1.0, 0.0, 0.0, -1.0, 0.0, 1.0).concat(&gs.ctm);
1438        let bbox = gs.ctm.apply_rect(&Rect::from_corners(0.0, 0.0, 1.0, 1.0));
1439        let order = self.next_order();
1440        self.images.push(ImageItem {
1441            name: name.to_string(),
1442            ctm,
1443            bbox,
1444            width,
1445            height,
1446            is_mask,
1447            bits_per_component,
1448            components,
1449            fill: gs.fill,
1450            alpha: gs.fill_alpha,
1451            order,
1452            source,
1453        });
1454    }
1455
1456    /// Allocates the next paint-order stamp.
1457    fn next_order(&mut self) -> u32 {
1458        let n = self.order;
1459        self.order = self.order.saturating_add(1);
1460        n
1461    }
1462}
1463
1464fn next_line(text: &mut TextState, leading: f32) {
1465    text.line_matrix = Matrix::translate(0.0, -leading).concat(&text.line_matrix);
1466    text.matrix = text.line_matrix;
1467}
1468
1469/// The bounding box of a path, control points included.
1470fn bounds(cmds: &[PathCmd]) -> Rect {
1471    let mut out: Option<Rect> = None;
1472    let mut add = |p: Point| {
1473        out = Some(match out {
1474            None => Rect {
1475                x0: p.x,
1476                y0: p.y,
1477                x1: p.x,
1478                y1: p.y,
1479            },
1480            Some(r) => Rect {
1481                x0: r.x0.min(p.x),
1482                y0: r.y0.min(p.y),
1483                x1: r.x1.max(p.x),
1484                y1: r.y1.max(p.y),
1485            },
1486        });
1487    };
1488    for cmd in cmds {
1489        match *cmd {
1490            PathCmd::MoveTo(p) | PathCmd::LineTo(p) => add(p),
1491            PathCmd::CurveTo(a, b, c) => {
1492                add(a);
1493                add(b);
1494                add(c);
1495            }
1496            PathCmd::Close => {}
1497        }
1498    }
1499    out.unwrap_or(Rect::EMPTY)
1500}
1501
1502#[cfg(test)]
1503pub(crate) mod tests {
1504    use super::*;
1505    use crate::parser::tests::tiny_pdf;
1506
1507    /// Builds a one-page document whose content stream is `content`, using Helvetica as /F1.
1508    pub(crate) fn doc_with(content: &str) -> Document {
1509        let base = String::from_utf8(tiny_pdf()).unwrap();
1510        let old = "BT /F1 12 Tf 72 720 Td (Hi) Tj ET";
1511        // The /Length is wrong after substitution, which the endstream scan recovers from — the
1512        // same path a real producer's off-by-one length takes.
1513        Document::from_bytes(base.replace(old, content).into_bytes(), None).unwrap()
1514    }
1515
1516    #[test]
1517    fn extracts_text_with_positions() {
1518        let doc = doc_with("BT /F1 12 Tf 72 720 Td (Hi) Tj ET");
1519        let page = doc.page(0).unwrap();
1520        assert_eq!(page.text(), "Hi");
1521        assert_eq!(page.glyphs.len(), 2);
1522
1523        let h = &page.glyphs[0];
1524        assert_eq!(h.text, "H");
1525        assert_eq!(h.base_font, "Helvetica");
1526        assert!((h.origin.x - 72.0).abs() < 0.01);
1527        assert!((h.origin.y - 720.0).abs() < 0.01);
1528        assert!((h.font_size - 12.0).abs() < 0.01);
1529        // Helvetica's H advances 722/1000 em.
1530        assert!((h.advance - 722.0 / 1000.0 * 12.0).abs() < 0.01);
1531        // The next glyph starts exactly one advance along.
1532        assert!((page.glyphs[1].origin.x - (72.0 + 8.664)).abs() < 0.01);
1533    }
1534
1535    fn text_of(content: &str) -> String {
1536        doc_with(content).page(0).unwrap().text()
1537    }
1538
1539    #[test]
1540    fn actual_text_replaces_what_its_glyphs_say_and_keeps_where_they_are() {
1541        let plain = doc_with("BT /F1 12 Tf 72 720 Td (a) Tj (xy) Tj (b) Tj ET");
1542        let tagged = doc_with(
1543            "BT /F1 12 Tf 72 720 Td (a) Tj /Span <</ActualText (fi)>> BDC (xy) Tj EMC (b) Tj ET",
1544        );
1545        let (plain, tagged) = (plain.page(0).unwrap(), tagged.page(0).unwrap());
1546        assert_eq!(plain.text(), "axyb");
1547        assert_eq!(tagged.text(), "afib");
1548
1549        // The first glyph of the span carries the replacement and the rest carry nothing.
1550        let texts: Vec<&str> = tagged.glyphs.iter().map(|g| g.text.as_str()).collect();
1551        assert_eq!(texts, ["a", "fi", "", "b"]);
1552
1553        // Geometry and identity are untouched: same glyphs, in the same places.
1554        assert_eq!(plain.glyphs.len(), tagged.glyphs.len());
1555        for (p, t) in plain.glyphs.iter().zip(&tagged.glyphs) {
1556            assert_eq!((p.origin, p.bbox, p.advance), (t.origin, t.bbox, t.advance));
1557            assert_eq!(p.code(), t.code());
1558        }
1559    }
1560
1561    #[test]
1562    fn actual_text_spans_several_show_operators_as_one_unit() {
1563        // Three operators, a line-internal move and a word-sized TJ gap, all inside one span:
1564        // the replacement is the whole of what they say, with no space invented within it.
1565        let doc = doc_with(
1566            "BT /F1 12 Tf 72 720 Td (A) Tj /Span <</ActualText (68)>> BDC \
1567             (x) Tj 20 0 Td (y) Tj [(z) -500 (w)] TJ EMC (B) Tj ET",
1568        );
1569        let page = doc.page(0).unwrap();
1570        assert_eq!(page.text(), "A68B");
1571        assert_eq!(page.glyphs.len(), 6);
1572        assert!(page.glyphs.iter().all(|g| !g.is_generated_space));
1573    }
1574
1575    #[test]
1576    fn the_outermost_actual_text_wins() {
1577        // The inner span neither adds its text nor ends the outer one early.
1578        assert_eq!(
1579            text_of(
1580                "BT /F1 12 Tf 72 720 Td /Span <</ActualText (out)>> BDC (a) Tj \
1581                 /Span <</ActualText (in)>> BDC (b) Tj EMC (c) Tj EMC (d) Tj ET"
1582            ),
1583            "outd"
1584        );
1585        // Enclosing sequences without ActualText — structure tags, a bare BMC — are transparent,
1586        // and their EMC does not disturb text after the span.
1587        assert_eq!(
1588            text_of(
1589                "/P <</MCID 0>> BDC /Artifact BMC BT /F1 12 Tf 72 720 Td (a) Tj \
1590                 /Span <</ActualText (X)>> BDC (b) Tj EMC (c) Tj ET EMC EMC \
1591                 BT 1 0 0 1 100 720 Tm (d) Tj ET"
1592            ),
1593            "aXc d"
1594        );
1595    }
1596
1597    #[test]
1598    fn an_empty_actual_text_silences_its_glyphs() {
1599        for empty in ["()", "<FEFF>", "<>"] {
1600            let doc = doc_with(&format!(
1601                "BT /F1 12 Tf 72 720 Td (a) Tj /Span <</ActualText {empty}>> BDC (-) Tj EMC \
1602                 (b) Tj ET"
1603            ));
1604            let page = doc.page(0).unwrap();
1605            assert_eq!(page.text(), "ab", "ActualText {empty}");
1606            // The hyphen is still on the page, it just reads as nothing.
1607            assert_eq!(page.glyphs.len(), 3);
1608            assert_eq!(page.glyphs[1].text, "");
1609            assert_eq!(page.glyphs[1].code(), u32::from(b'-'));
1610        }
1611    }
1612
1613    #[test]
1614    fn actual_text_is_decoded_as_a_text_string() {
1615        let span = |value: &str| {
1616            text_of(&format!(
1617                "BT /F1 12 Tf 72 720 Td /Span <</ActualText {value}>> BDC (x) Tj EMC ET"
1618            ))
1619        };
1620        // UTF-16BE behind a byte-order mark, as Chromium writes it.
1621        assert_eq!(span("<FEFF00660069>"), "fi");
1622        assert_eq!(span("<FEFF03B1D835DC00>"), "\u{3b1}\u{1D400}");
1623        // No mark: PDFDocEncoding, where 0x93 is the fi ligature and not Latin-1's control code.
1624        assert_eq!(span("(fi)"), "fi");
1625        assert_eq!(span("(\\223 \\351)"), "\u{FB01} \u{e9}");
1626        assert_eq!(span("<93>"), "\u{FB01}");
1627    }
1628
1629    #[test]
1630    fn a_gap_beside_a_span_is_one_space() {
1631        // Word gaps on both sides of a span whose glyph says something else entirely.
1632        assert_eq!(
1633            text_of(
1634                "BT /F1 12 Tf 72 720 Td [(A) -500] TJ /Span <</ActualText (6)>> BDC (x) Tj EMC \
1635                 [-500 (B)] TJ ET"
1636            ),
1637            "A 6 B"
1638        );
1639        // The same gaps where the replacement brings its own spaces: none is added.
1640        assert_eq!(
1641            text_of(
1642                "BT /F1 12 Tf 72 720 Td [(A) -500] TJ /Span <</ActualText ( 6 )>> BDC (x) Tj \
1643                 EMC [-500 (B)] TJ ET"
1644            ),
1645            "A 6 B"
1646        );
1647        // No gap, no space.
1648        assert_eq!(
1649            text_of(
1650                "BT /F1 12 Tf 72 720 Td (A) Tj /Span <</ActualText (6)>> BDC (x) Tj EMC (B) Tj ET"
1651            ),
1652            "A6B"
1653        );
1654        // A gap after a span of several glyphs is measured from the last of them, and the
1655        // silent ones do not hide that the replacement ended without a space.
1656        assert_eq!(
1657            text_of(
1658                "BT /F1 12 Tf 72 720 Td (A) Tj /Span <</ActualText (fi)>> BDC (xy) Tj EMC \
1659                 [-500 (B)] TJ ET"
1660            ),
1661            "Afi B"
1662        );
1663        // Silenced glyphs between two words leave the one space the gaps amount to.
1664        assert_eq!(
1665            text_of(
1666                "BT /F1 12 Tf 72 720 Td [(A) -500] TJ /Span <</ActualText ()>> BDC (x) Tj EMC \
1667                 [-500 (B)] TJ ET"
1668            ),
1669            "A B"
1670        );
1671    }
1672
1673    /// Chromium tags space glyphs with a line feed at HTML block boundaries, which are not
1674    /// reliably where the visual line ends. The tag reads as the space it sits on, and line
1675    /// breaks stay a matter of geometry.
1676    #[test]
1677    fn an_actual_text_line_feed_is_a_space_not_a_line_break() {
1678        // Mid-line: no break is invented.
1679        assert_eq!(
1680            text_of(
1681                "BT /F1 12 Tf 72 720 Td (one) Tj /Span <</ActualText <FEFF000A>>> BDC ( ) Tj EMC \
1682                 (two) Tj ET"
1683            ),
1684            "one two"
1685        );
1686        // At a real line end: the geometric break is the only one.
1687        assert_eq!(
1688            text_of(
1689                "BT /F1 12 Tf 72 720 Td (one) Tj /Span <</ActualText <FEFF000A>>> BDC ( ) Tj EMC \
1690                 1 0 0 1 72 700 Tm (two) Tj ET"
1691            ),
1692            "one \ntwo"
1693        );
1694    }
1695
1696    #[test]
1697    fn actual_text_is_found_through_the_properties_resource() {
1698        let base = String::from_utf8(tiny_pdf()).unwrap();
1699        let pdf = base
1700            .replace(
1701                "/Resources << /Font",
1702                "/Resources << /Properties << /MC0 << /ActualText (fl) >> >> /Font",
1703            )
1704            .replace(
1705                "BT /F1 12 Tf 72 720 Td (Hi) Tj ET",
1706                "BT /F1 12 Tf 72 720 Td /Span /MC0 BDC (x) Tj EMC /Span /Nope BDC (y) Tj EMC ET",
1707            );
1708        let doc = Document::from_bytes(pdf.into_bytes(), None).unwrap();
1709        // A name the resources do not define is a sequence with no properties at all.
1710        assert_eq!(doc.page(0).unwrap().text(), "fly");
1711    }
1712
1713    #[test]
1714    fn unbalanced_marked_content_does_not_leak_or_panic() {
1715        // Never closed: the span covers the rest of the stream, and nothing breaks.
1716        assert_eq!(
1717            text_of("BT /F1 12 Tf 72 720 Td (a) Tj /Span <</ActualText (X)>> BDC (bc) Tj ET"),
1718            "aX"
1719        );
1720        // Closed too often, and opened with operands that are not what the operator takes.
1721        assert_eq!(
1722            text_of(
1723                "EMC EMC BT /F1 12 Tf 72 720 Td BDC (a) Tj EMC /Span 7 BDC (b) Tj EMC \
1724                 /Span <</ActualText 7>> BDC (c) Tj EMC EMC ET"
1725            ),
1726            "abc"
1727        );
1728    }
1729
1730    #[test]
1731    fn page_geometry_matches_media_box() {
1732        let doc = doc_with("BT /F1 12 Tf 72 720 Td (Hi) Tj ET");
1733        let page = doc.page(0).unwrap();
1734        assert_eq!((page.width(), page.height()), (612.0, 792.0));
1735        // User-space y-up maps to device y-down: the baseline at y=720 lands 72pt from the top.
1736        let m = page.page_matrix(1.0);
1737        let p = m.apply(Point::new(72.0, 720.0));
1738        assert!((p.x - 72.0).abs() < 0.01 && (p.y - 72.0).abs() < 0.01);
1739    }
1740
1741    #[test]
1742    fn tj_offsets_shift_the_pen_and_generate_spaces() {
1743        // -500 thousandths at 12pt is a 6pt gap, far over the word-break threshold.
1744        let doc = doc_with("BT /F1 12 Tf 72 720 Td [(A) -500 (B)] TJ ET");
1745        let page = doc.page(0).unwrap();
1746        assert_eq!(page.text(), "A B");
1747        assert!(page.glyphs[1].is_generated_space);
1748
1749        // A small adjustment is kerning, not a word break.
1750        let doc = doc_with("BT /F1 12 Tf 72 720 Td [(A) -20 (B)] TJ ET");
1751        assert_eq!(doc.page(0).unwrap().text(), "AB");
1752    }
1753
1754    /// Justified text squeezes its word spaces, and they still have to register as spaces.
1755    ///
1756    /// A real line of ICLR body text was found setting them at 0.19 em — under the 0.20 em the
1757    /// threshold started at, so *every* word gap on that line was missed and the line extracted
1758    /// as one run-on word. The threshold has to sit below what justification compresses a space
1759    /// to, and above what kerning ever opens up.
1760    #[test]
1761    fn a_compressed_word_space_is_still_a_word_space() {
1762        let doc = doc_with("BT /F1 12 Tf 72 720 Td [(A) -190 (B)] TJ ET");
1763        assert_eq!(
1764            doc.page(0).unwrap().text(),
1765            "A B",
1766            "0.19 em is a squeezed space"
1767        );
1768
1769        // Well below any space, and comfortably above ordinary kerning: still one word.
1770        let doc = doc_with("BT /F1 12 Tf 72 720 Td [(A) -80 (B)] TJ ET");
1771        assert_eq!(doc.page(0).unwrap().text(), "AB", "0.08 em is not a space");
1772    }
1773
1774    /// A glyph drawn from a substitute face must report the document's advance, not the
1775    /// substitute's ink width.
1776    ///
1777    /// Helvetica is one of the standard 14 and embeds nothing, so its outlines come from
1778    /// whatever system face is installed. Letting that face's ink decide the box made the gap
1779    /// between two glyphs depend on which fonts the machine happened to have, and a letterspaced
1780    /// heading then picked up a word break that is not in the document (`REFEREN CES`).
1781    #[test]
1782    fn a_substituted_glyph_reports_the_documents_advance() {
1783        let doc = doc_with("BT /F1 12 Tf 72 720 Td (Hi) Tj ET");
1784        let page = doc.page(0).unwrap();
1785
1786        for glyph in &page.glyphs {
1787            let width = glyph.bbox.x1 - glyph.bbox.x0;
1788            assert!(
1789                (width - glyph.advance).abs() < 0.01,
1790                "{:?}: box {width} should span the advance {}",
1791                glyph.text,
1792                glyph.advance
1793            );
1794        }
1795
1796        // Consecutive boxes therefore abut exactly, leaving no gap to mistake for a space.
1797        let gap = page.glyphs[1].bbox.x0 - page.glyphs[0].bbox.x1;
1798        assert!(
1799            gap.abs() < 0.01,
1800            "adjacent letters must not show an ink gap"
1801        );
1802    }
1803
1804    #[test]
1805    fn text_state_operators_apply() {
1806        // Tz halves the advance; the second glyph must land half as far along.
1807        let doc = doc_with("BT /F1 12 Tf 50 Tz 72 720 Td (Hi) Tj ET");
1808        let page = doc.page(0).unwrap();
1809        assert!((page.glyphs[1].origin.x - (72.0 + 8.664 / 2.0)).abs() < 0.01);
1810
1811        // TL + T* moves down one leading.
1812        let doc = doc_with("BT /F1 12 Tf 14 TL 72 720 Td (A) Tj T* (B) Tj ET");
1813        let page = doc.page(0).unwrap();
1814        let b = page.glyphs.iter().find(|g| g.text == "B").unwrap();
1815        assert!((b.origin.y - 706.0).abs() < 0.01);
1816    }
1817
1818    #[test]
1819    fn a_scaled_text_matrix_does_not_manufacture_spaces() {
1820        // `Tm` carries its own scale, so the pen advance must travel through Tm as well as the
1821        // CTM. Scaling by the CTM alone under-counts it, and the shortfall reads as a gap wide
1822        // enough to be a word break — a space between every single character.
1823        let doc = doc_with("BT /F1 12 Tf 2 0 0 2 72 720 Tm (Hi) Tj ET");
1824        let page = doc.page(0).unwrap();
1825        assert_eq!(page.text(), "Hi");
1826        assert!(!page.glyphs.iter().any(|g| g.is_generated_space));
1827        // Helvetica's H advances 722/1000 em, doubled by the text matrix.
1828        assert!((page.glyphs[1].origin.x - (72.0 + 0.722 * 12.0 * 2.0)).abs() < 0.05);
1829    }
1830
1831    #[test]
1832    fn an_oversized_advance_does_not_swallow_word_breaks() {
1833        // The mirror of the case above: over-counting the advance pushes the pen past the next
1834        // glyph, every gap computes as negative, and no word break is ever emitted.
1835        let doc = doc_with("BT /F1 12 Tf 0.5 0 0 0.5 72 720 Tm (a) Tj 40 0 Td (b) Tj ET");
1836        let page = doc.page(0).unwrap();
1837        assert!(
1838            page.glyphs.iter().any(|g| g.is_generated_space),
1839            "a 20pt gap at 6pt effective size is a word break: {:?}",
1840            page.text()
1841        );
1842    }
1843
1844    #[test]
1845    fn text_breaks_lines_instead_of_fusing_words() {
1846        // Content streams have no newlines; a new line is a glyph at another baseline. Without
1847        // a break the last word of one line fuses onto the first of the next.
1848        let doc = doc_with("BT /F1 12 Tf 72 720 Td (of) Tj 0 -14 Td (the) Tj ET");
1849        assert_eq!(doc.page(0).unwrap().text(), "of\nthe");
1850    }
1851
1852    #[test]
1853    fn a_wrapped_line_returning_to_the_margin_breaks() {
1854        // Some producers set an explicit `Tm` per line rather than using `Td`/`T*`.
1855        let doc =
1856            doc_with("BT /F1 12 Tf 1 0 0 1 300 720 Tm (end) Tj 1 0 0 1 72 720 Tm (start) Tj ET");
1857        assert_eq!(doc.page(0).unwrap().text(), "end\nstart");
1858    }
1859
1860    #[test]
1861    fn a_page_sized_image_with_no_text_reads_as_scanned() {
1862        let doc =
1863            doc_with("q 612 0 0 792 0 0 cm BI /W 4 /H 4 /BPC 8 /CS /G ID 0123456789abcdef EI Q");
1864        assert!(doc.page(0).unwrap().is_likely_scanned());
1865    }
1866
1867    #[test]
1868    fn text_over_an_image_is_not_scanned() {
1869        // A full-page figure with a caption, and an OCR text layer, must both extract normally.
1870        let body = (0..40)
1871            .map(|i| {
1872                format!(
1873                    "BT /F1 9 Tf 72 {} Td (line of real text) Tj ET",
1874                    700 - i * 12
1875                )
1876            })
1877            .collect::<Vec<_>>()
1878            .join(" ");
1879        let doc = doc_with(&format!(
1880            "q 612 0 0 792 0 0 cm BI /W 4 /H 4 /BPC 8 /CS /G ID 0123456789abcdef EI Q {body}"
1881        ));
1882        assert!(!doc.page(0).unwrap().is_likely_scanned());
1883    }
1884
1885    #[test]
1886    fn an_ordinary_text_page_is_not_scanned() {
1887        let doc = doc_with("BT /F1 12 Tf 72 720 Td (Hi) Tj ET");
1888        assert!(!doc.page(0).unwrap().is_likely_scanned());
1889    }
1890
1891    #[test]
1892    fn cells_in_separate_text_objects_stay_separate_words() {
1893        // Producers routinely wrap each table cell in its own BT/ET. The gap between them is
1894        // the only thing separating the cells, so it has to survive the object boundary —
1895        // otherwise a row fuses into one token.
1896        let doc =
1897            doc_with("BT /F1 10 Tf 72 700 Td (alpha) Tj ET BT /F1 10 Tf 300 700 Td (beta) Tj ET");
1898        let text = doc.page(0).unwrap().text();
1899        assert!(
1900            text.contains("alpha") && text.contains("beta") && !text.contains("alphabeta"),
1901            "cells fused: {text:?}"
1902        );
1903    }
1904
1905    #[test]
1906    fn a_superscript_stays_on_its_line() {
1907        // A footnote marker or exponent sits above the baseline but continues the line. Treating
1908        // that shift as a line break splits `x2` into `x` and `2`, which is how sub- and
1909        // superscripts get separated from what they modify all through a mathematics paper.
1910        let doc = doc_with("BT /F1 10 Tf 72 700 Td (x) Tj 4 Ts /F1 7 Tf (2) Tj ET");
1911        assert_eq!(doc.page(0).unwrap().text(), "x2");
1912    }
1913
1914    #[test]
1915    fn ordinary_spacing_within_a_line_is_not_a_break() {
1916        let doc = doc_with("BT /F1 12 Tf 72 720 Td (a) Tj (b) Tj ET");
1917        assert_eq!(doc.page(0).unwrap().text(), "ab");
1918    }
1919
1920    #[test]
1921    fn invisible_text_is_kept_but_flagged() {
1922        let doc = doc_with("BT /F1 12 Tf 3 Tr 72 720 Td (Hi) Tj ET");
1923        let page = doc.page(0).unwrap();
1924        assert_eq!(page.text(), "Hi");
1925        assert!(!page.glyphs[0].is_visible());
1926    }
1927
1928    #[test]
1929    fn paths_are_recorded_in_user_space() {
1930        let doc = doc_with("0 0 1 rg 10 20 100 50 re f");
1931        let page = doc.page(0).unwrap();
1932        assert_eq!(page.paths.len(), 1);
1933        let p = &page.paths[0];
1934        assert_eq!(p.bbox, Rect::from_corners(10.0, 20.0, 110.0, 70.0));
1935        assert_eq!(p.fill, Some([0.0, 0.0, 1.0]));
1936        assert_eq!(p.stroke, None);
1937    }
1938
1939    #[test]
1940    fn cm_transforms_geometry_and_q_restores_it() {
1941        let doc = doc_with("q 2 0 0 2 5 5 cm 0 0 10 10 re f Q 0 0 10 10 re S");
1942        let page = doc.page(0).unwrap();
1943        assert_eq!(page.paths.len(), 2);
1944        // Scaled by 2 and shifted by 5.
1945        assert_eq!(page.paths[0].bbox, Rect::from_corners(5.0, 5.0, 25.0, 25.0));
1946        // After Q the CTM is back to identity.
1947        assert_eq!(page.paths[1].bbox, Rect::from_corners(0.0, 0.0, 10.0, 10.0));
1948        assert!(page.paths[1].fill.is_none() && page.paths[1].stroke.is_some());
1949    }
1950
1951    #[test]
1952    fn cmyk_and_gray_colors_convert() {
1953        let doc = doc_with("0.5 g 0 0 1 1 re f 0 1 1 0 k 0 0 1 1 re f");
1954        let page = doc.page(0).unwrap();
1955        assert_eq!(page.paths[0].fill, Some([0.5, 0.5, 0.5]));
1956        // Cyan=0, magenta=1, yellow=1, black=0 is red.
1957        assert_eq!(page.paths[1].fill, Some([1.0, 0.0, 0.0]));
1958    }
1959
1960    #[test]
1961    fn inline_images_are_recorded() {
1962        let doc =
1963            doc_with("q 100 0 0 50 10 10 cm BI /W 2 /H 2 /BPC 8 /CS /G ID \x01\x02\x03\x04 EI Q");
1964        let page = doc.page(0).unwrap();
1965        assert_eq!(page.images.len(), 1);
1966        let img = &page.images[0];
1967        assert_eq!((img.width, img.height), (2, 2));
1968        assert_eq!(img.bbox, Rect::from_corners(10.0, 10.0, 110.0, 60.0));
1969        assert_eq!((img.components, img.bits_per_component), (1, 8));
1970        // Pixels are fetched on demand rather than carried on the item.
1971        match img.decode(&doc).unwrap() {
1972            ImageData::Raw { data, .. } => assert_eq!(data.len(), 4),
1973            other => panic!("expected raw samples, got {other:?}"),
1974        }
1975    }
1976
1977    #[test]
1978    fn malformed_content_does_not_panic() {
1979        for junk in [
1980            "BT (unterminated",
1981            "q q q q 1 0 0 1 cm",
1982            "BT /Missing 12 Tf (x) Tj ET",
1983            "[(A) (B)] TJ",
1984            ") ] >> garbage f",
1985            "BT /F1 0 Tf 72 720 Td (Hi) Tj ET",
1986        ] {
1987            let page = doc_with(junk).page(0).unwrap();
1988            let _ = page.text();
1989        }
1990    }
1991}
1992
1993#[cfg(test)]
1994mod thread_safety {
1995    /// The crate's premise is that a document is usable from several threads at once. Assert it
1996    /// at compile time so a stray `Rc` or `Cell` cannot silently take it away.
1997    const _: fn() = || {
1998        fn assert_send_sync<T: Send + Sync>() {}
1999        assert_send_sync::<crate::Document>();
2000        assert_send_sync::<crate::Font>();
2001        assert_send_sync::<crate::Page>();
2002    };
2003}
2004
2005#[cfg(test)]
2006mod colorspace_tests {
2007    use super::tests::doc_with;
2008
2009    #[test]
2010    fn scn_reads_operands_through_the_declared_space() {
2011        // An ICCBased 3-component space makes `scn` an RGB triple.
2012        let doc = doc_with("/DeviceRGB cs 1 0 0 scn 0 0 1 1 re f");
2013        assert_eq!(doc.page(0).unwrap().paths[0].fill, Some([1.0, 0.0, 0.0]));
2014
2015        // A Separation space takes one tint, read as gray.
2016        let doc = doc_with("/DeviceGray cs 0.25 scn 0 0 1 1 re f");
2017        assert_eq!(doc.page(0).unwrap().paths[0].fill, Some([0.25, 0.25, 0.25]));
2018    }
2019
2020    #[test]
2021    fn pattern_name_operand_leaves_the_color_alone() {
2022        // `/P0 scn` names a pattern and carries no numeric operands. There is no colour to
2023        // read, so the current one must survive rather than collapsing to black.
2024        let doc = doc_with("1 0 0 rg /P0 scn 0 0 1 1 re f");
2025        assert_eq!(doc.page(0).unwrap().paths[0].fill, Some([1.0, 0.0, 0.0]));
2026    }
2027
2028    #[test]
2029    fn cs_resets_to_the_spaces_initial_color() {
2030        // Selecting a colour space resets the current colour to that space's initial value,
2031        // which for the device spaces is black.
2032        let doc = doc_with("1 0 0 rg /DeviceRGB cs 0 0 1 1 re f");
2033        assert_eq!(doc.page(0).unwrap().paths[0].fill, Some([0.0, 0.0, 0.0]));
2034    }
2035}