pdfraw 0.1.3

Layout-preserving PDF text extraction in pure Rust (pdfplumber-inspired)
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! `lopdf`-based PDF parser.
//!
//! Walks a page's content stream as a state machine, tracking the text
//! matrix, current font, and graphics state stack, and emits one
//! [`Char`] per glyph drawn by `Tj`/`TJ`/`'`/`"`.

use crate::char::Char;
use crate::error::{Error, Result};
use crate::geom::{Matrix, Point};
use crate::page::{Page, PageMetrics};
use compact_str::CompactString;
use lopdf::content::Content;
use lopdf::{Dictionary, Document as LDoc, Encoding, Object, ObjectId};
use std::collections::HashMap;

/// Read the media box and rotation for the page.
pub(crate) fn page_metrics(
    doc: &LDoc,
    page_id: ObjectId,
    page_index: usize,
) -> Result<PageMetrics> {
    let page = doc.get_dictionary(page_id)?;
    let media_box =
        resolve_inheritable(doc, page, b"MediaBox").ok_or_else(|| Error::ContentStream {
            page: page_index,
            reason: "missing /MediaBox".into(),
        })?;
    let media = media_box_dimensions(doc, page_index, &media_box)?;
    let rotation = resolve_inheritable(doc, page, b"Rotate")
        .and_then(|o| match o {
            Object::Integer(i) => Some(i),
            Object::Real(r) => Some(r as i64),
            _ => None,
        })
        .map(normalize_rotation)
        .unwrap_or(0);
    Ok(PageMetrics {
        media_width: media.width,
        media_height: media.height,
        origin_x: media.origin_x,
        origin_y: media.origin_y,
        rotation,
    })
}

/// Fold an arbitrary `/Rotate` into the `{0, 90, 180, 270}` set that
/// [`PageMetrics::rotation`] promises.
///
/// PDF requires `/Rotate` to be a multiple of 90, and permits negatives.
/// Files get both wrong, so negatives wrap into range and anything that is
/// not a quarter turn is discarded rather than passed through.
fn normalize_rotation(raw: i64) -> i16 {
    let wrapped = raw.rem_euclid(360);
    if wrapped % 90 != 0 {
        log::warn!("/Rotate {raw} is not a multiple of 90 โ€” treating the page as unrotated");
        return 0;
    }
    wrapped as i16
}

/// Hard cap on how far up the `/Parent` chain we walk.
///
/// A well-formed page tree is a handful of levels deep. A malformed (or
/// hostile) PDF can point two `/Pages` nodes at each other, which would
/// otherwise spin forever, so we stop rather than trust the file.
const MAX_PAGE_TREE_DEPTH: usize = 64;

/// Walk the page-tree `/Parent` chain looking for an inheritable key.
///
/// PDF defines `MediaBox`, `Rotate`, `Resources`, and `CropBox` as
/// inheritable from the closest ancestor `/Pages` node that defines them.
///
/// Returns `None` if the key is absent, the chain breaks, or the chain is
/// longer than [`MAX_PAGE_TREE_DEPTH`] (which a cycle always is).
fn resolve_inheritable(doc: &LDoc, page: &Dictionary, key: &[u8]) -> Option<Object> {
    let mut cursor: &Dictionary = page;
    for _ in 0..MAX_PAGE_TREE_DEPTH {
        if let Ok(v) = cursor.get(key) {
            return Some(deref(doc, v.clone()));
        }
        let Ok(Object::Reference(id)) = cursor.get(b"Parent") else {
            return None;
        };
        let Ok(parent) = doc.get_dictionary(*id) else {
            return None;
        };
        cursor = parent;
    }
    log::warn!("/Parent chain exceeded {MAX_PAGE_TREE_DEPTH} levels โ€” treating as cyclic");
    None
}

fn deref(doc: &LDoc, obj: Object) -> Object {
    match obj {
        Object::Reference(id) => doc.get_object(id).cloned().unwrap_or(Object::Null),
        other => other,
    }
}

fn media_box_dimensions(doc: &LDoc, page_index: usize, o: &Object) -> Result<MediaBox> {
    let arr = match o {
        Object::Array(a) => a,
        _ => {
            return Err(Error::ContentStream {
                page: page_index,
                reason: "/MediaBox is not an array".into(),
            });
        }
    };
    if arr.len() != 4 {
        return Err(Error::ContentStream {
            page: page_index,
            reason: format!("/MediaBox has {} elements, expected 4", arr.len()),
        });
    }
    // Any of the four may be an indirect reference. Reading them literally
    // failed the whole lookup, and the caller then silently fell back to a
    // default page size โ€” so every glyph on the page came out mispositioned.
    let n = |o: &Object| -> Result<f32> {
        match deref(doc, o.clone()) {
            Object::Integer(i) => Ok(i as f32),
            Object::Real(r) => Ok(r),
            _ => Err(Error::ContentStream {
                page: page_index,
                reason: "/MediaBox contains a non-numeric value".into(),
            }),
        }
    };
    let x0 = n(&arr[0])?;
    let y0 = n(&arr[1])?;
    let x1 = n(&arr[2])?;
    let y1 = n(&arr[3])?;
    let (width, height) = ((x1 - x0).abs(), (y1 - y0).abs());

    // A degenerate box would give a 0-wide page and negative tops. A *missing*
    // /MediaBox already falls back to a usable default, so a useless one
    // should not be treated as more trustworthy than no box at all.
    if !width.is_finite() || !height.is_finite() || width <= 0.0 || height <= 0.0 {
        return Err(Error::ContentStream {
            page: page_index,
            reason: format!("/MediaBox has no usable area ({width} x {height})"),
        });
    }
    // The lower-left corner is the page's origin, and it is not always
    // (0, 0). Dropping it shifted every glyph by that corner, which could
    // even put an on-page glyph at a negative `top`.
    Ok(MediaBox {
        origin_x: x0.min(x1),
        origin_y: y0.min(y1),
        width,
        height,
    })
}

/// A page's `/MediaBox`, split into where it starts and how big it is.
struct MediaBox {
    origin_x: f32,
    origin_y: f32,
    width: f32,
    height: f32,
}

/// Extract every glyph on the page as a [`Char`].
pub(crate) fn extract_chars(page: &Page<'_>) -> Result<Vec<Char>> {
    let doc = &page.document().inner;
    let page_id = page.page_id();
    let metrics = page.metrics();
    let geom = PageGeometry {
        height: metrics.display_height(),
        doctop_offset: page.document().doctop_offset(page.index()),
    };
    // The media-box origin and `/Rotate` are folded in as the outermost
    // transform, so every glyph position, size and uprightness comes out
    // already in displayed space.
    let page_rotation = metrics.page_transform();

    let raw = doc.get_page_content(page_id);
    let content = Content::decode(&raw).map_err(|e| Error::ContentStream {
        page: page.index(),
        reason: format!("decode content: {e}"),
    })?;

    let mut fonts = FontTable::default();
    if let Ok(map) = doc.get_page_fonts(page_id) {
        for (name, dict) in map {
            fonts.insert(name, FontInfo::from_dict(doc, dict));
        }
    }

    let mut tm = TextMatrices::default();
    let mut gs_stack: Vec<GraphicsState> = vec![GraphicsState::default()];
    let mut out = Vec::new();

    // The stack is never allowed to empty (see the "Q" arm), so these two
    // helpers can always hand back a live graphics state.
    macro_rules! gs {
        () => {
            gs_stack.last().expect("graphics stack is never empty")
        };
    }
    macro_rules! gs_mut {
        () => {
            gs_stack.last_mut().expect("graphics stack is never empty")
        };
    }

    for op in content.operations {
        match op.operator.as_str() {
            // graphics state โ€” q/Q save and restore the CTM *and* the text
            // state parameters (PDF 32000-1, Table 52).
            "q" => {
                let top = gs!().clone();
                gs_stack.push(top);
            }
            "Q" => {
                gs_stack.pop();
                if gs_stack.is_empty() {
                    gs_stack.push(GraphicsState::default());
                }
            }
            "cm" => {
                if let Some(m) = matrix_from_operands(&op.operands) {
                    let top = gs_mut!();
                    top.ctm = m.then(top.ctm);
                }
            }

            // Text object. `BT` resets the text matrices; `ET` carries no
            // state of its own now that showing text is not gated on being
            // inside a text object.
            "BT" => tm = TextMatrices::default(),
            "ET" => {}

            // text state
            "Tf" => {
                if let (Some(name), Some(size)) = (
                    op.operands.first().and_then(name_of),
                    op.operands.get(1).and_then(num_of),
                ) {
                    let t = &mut gs_mut!().text;
                    t.font_name = name;
                    t.font_size = size;
                }
            }
            "Tc" => gs_mut!().text.char_space = first_num(&op.operands).unwrap_or(0.0),
            "Tw" => gs_mut!().text.word_space = first_num(&op.operands).unwrap_or(0.0),
            "Tz" => gs_mut!().text.h_scale = first_num(&op.operands).unwrap_or(100.0) / 100.0,
            "TL" => gs_mut!().text.leading = first_num(&op.operands).unwrap_or(0.0),
            "Ts" => gs_mut!().text.rise = first_num(&op.operands).unwrap_or(0.0),
            "Tr" => { /* rendering mode ignored โ€” we emit all glyphs */ }

            // text positioning
            "Tm" => {
                if let Some(m) = matrix_from_operands(&op.operands) {
                    tm.set_line(m);
                }
            }
            "Td" => {
                if let (Some(tx), Some(ty)) = (
                    op.operands.first().and_then(num_of),
                    op.operands.get(1).and_then(num_of),
                ) {
                    tm.next_line(tx, ty);
                }
            }
            "TD" => {
                if let (Some(tx), Some(ty)) = (
                    op.operands.first().and_then(num_of),
                    op.operands.get(1).and_then(num_of),
                ) {
                    gs_mut!().text.leading = -ty;
                    tm.next_line(tx, ty);
                }
            }
            "T*" => {
                let leading = gs!().text.leading;
                tm.next_line(0.0, -leading);
            }

            // Text showing.
            //
            // Deliberately not gated on being inside a BT/ET pair. The spec
            // says a show operator belongs to a text object, but files in the
            // wild omit the BT or close it early, and every reader that
            // matters still draws that text. Refusing to would lose the whole
            // page silently โ€” and `is_scanned` would not flag it either,
            // because it does count the Tj.
            "Tj" => {
                if let Some(bytes) = first_string(&op.operands) {
                    emit_string(
                        &bytes,
                        &mut tm,
                        gs!(),
                        &fonts,
                        &geom,
                        page_rotation,
                        &mut out,
                    );
                }
            }
            "'" => {
                // next line + show
                let leading = gs!().text.leading;
                tm.next_line(0.0, -leading);
                if let Some(bytes) = first_string(&op.operands) {
                    emit_string(
                        &bytes,
                        &mut tm,
                        gs!(),
                        &fonts,
                        &geom,
                        page_rotation,
                        &mut out,
                    );
                }
            }
            "\"" => {
                // operands: aw ac string
                if op.operands.len() == 3 {
                    let t = &mut gs_mut!().text;
                    t.word_space = num_of(&op.operands[0]).unwrap_or(0.0);
                    t.char_space = num_of(&op.operands[1]).unwrap_or(0.0);
                }
                let leading = gs!().text.leading;
                tm.next_line(0.0, -leading);
                if let Some(bytes) = op.operands.last().and_then(string_bytes) {
                    emit_string(
                        &bytes,
                        &mut tm,
                        gs!(),
                        &fonts,
                        &geom,
                        page_rotation,
                        &mut out,
                    );
                }
            }
            "TJ" => {
                if let Some(Object::Array(arr)) = op.operands.first() {
                    for item in arr {
                        match item {
                            Object::String(bytes, _) => emit_string(
                                bytes,
                                &mut tm,
                                gs!(),
                                &fonts,
                                &geom,
                                page_rotation,
                                &mut out,
                            ),
                            Object::Integer(_) | Object::Real(_) => {
                                let t = &gs!().text;
                                let adj = num_of(item).unwrap_or(0.0);
                                let tx = -(adj / 1000.0) * t.font_size * t.h_scale;
                                tm.advance(tx);
                            }
                            _ => {}
                        }
                    }
                }
            }

            _ => { /* unsupported operator โ€” silently skip operands */ }
        }
    }

    Ok(out)
}

// ============================================================================
// Helpers
// ============================================================================

fn matrix_from_operands(operands: &[Object]) -> Option<Matrix> {
    if operands.len() < 6 {
        return None;
    }
    let n = |i: usize| num_of(&operands[i]);
    Some(Matrix::new(n(0)?, n(1)?, n(2)?, n(3)?, n(4)?, n(5)?))
}

fn num_of(obj: &Object) -> Option<f32> {
    match obj {
        Object::Integer(i) => Some(*i as f32),
        Object::Real(r) => Some(*r),
        _ => None,
    }
}

fn first_num(operands: &[Object]) -> Option<f32> {
    operands.first().and_then(num_of)
}

fn name_of(obj: &Object) -> Option<Vec<u8>> {
    match obj {
        Object::Name(n) => Some(n.clone()),
        _ => None,
    }
}

fn string_bytes(obj: &Object) -> Option<Vec<u8>> {
    match obj {
        Object::String(b, _) => Some(b.clone()),
        _ => None,
    }
}

fn first_string(operands: &[Object]) -> Option<Vec<u8>> {
    operands.first().and_then(string_bytes)
}

// ============================================================================
// Text state and fonts
// ============================================================================

/// Where the page sits, so glyph coordinates can be flipped to top-down and
/// offset into document space.
struct PageGeometry {
    /// Page height in points, used to flip PDF's bottom-up y axis.
    height: f32,
    /// Sum of the heights of every preceding page, for `Char::doctop`.
    doctop_offset: f32,
}

/// The text-state parameters that PDF stores in the graphics state and that
/// `q`/`Q` therefore save and restore (PDF 32000-1, Table 52).
///
/// Note that `Tm`/`Tlm` are deliberately *not* here: the text matrices are
/// reset by `BT` and are not part of the saved graphics state.
#[derive(Debug, Clone)]
struct TextParams {
    font_name: Vec<u8>,
    font_size: f32,
    char_space: f32,
    word_space: f32,
    h_scale: f32,
    leading: f32,
    rise: f32,
}

impl Default for TextParams {
    fn default() -> Self {
        Self {
            font_name: Vec::new(),
            font_size: 1.0,
            char_space: 0.0,
            word_space: 0.0,
            h_scale: 1.0,
            leading: 0.0,
            rise: 0.0,
        }
    }
}

/// One entry of the `q`/`Q` stack.
#[derive(Debug, Clone, Default)]
struct GraphicsState {
    ctm: Matrix,
    text: TextParams,
}

/// The text matrix and text line matrix, which live outside the graphics
/// state and are reset by every `BT`.
#[derive(Debug, Clone, Copy)]
struct TextMatrices {
    tm: Matrix,
    tlm: Matrix,
}

impl Default for TextMatrices {
    fn default() -> Self {
        Self {
            tm: Matrix::IDENTITY,
            tlm: Matrix::IDENTITY,
        }
    }
}

impl TextMatrices {
    /// `Tm`: replace both matrices.
    fn set_line(&mut self, m: Matrix) {
        self.tm = m;
        self.tlm = m;
    }

    /// `Td`/`TD`/`T*`/`'`: move to a new line relative to the line matrix.
    fn next_line(&mut self, tx: f32, ty: f32) {
        let new = Matrix::translation(tx, ty).then(self.tlm);
        self.set_line(new);
    }

    /// Advance the text matrix horizontally within the current line.
    fn advance(&mut self, tx: f32) {
        self.tm = Matrix::translation(tx, 0.0).then(self.tm);
    }
}

/// Per-page font cache: name (e.g. b"F1") -> decoded info.
#[derive(Default)]
struct FontTable<'doc> {
    by_name: HashMap<Vec<u8>, FontInfo<'doc>>,
}

impl<'doc> FontTable<'doc> {
    fn insert(&mut self, name: Vec<u8>, info: FontInfo<'doc>) {
        self.by_name.insert(name, info);
    }

    fn get(&self, name: &[u8]) -> Option<&FontInfo<'doc>> {
        self.by_name.get(name)
    }
}

struct FontInfo<'doc> {
    encoding: Option<Encoding<'doc>>,
    widths: Widths,
    is_composite: bool,
    /// `/Encoding /Differences` overrides for byte-code โ†’ Unicode.
    differences: Option<crate::parser::fonts::differences::Differences>,
}

/// One glyph code from a shown string, with the text it decodes to.
///
/// The text is a `String`, not a `char`, because a `/ToUnicode` entry may map
/// one code to several characters (`<0041>` โ†’ `"fi"`), and it may be empty
/// when the encoding defines no glyph for the code.
struct DecodedCode {
    code: u32,
    text: String,
}

impl FontInfo<'_> {
    /// Split a shown string into its glyph codes, decoding each one on its own.
    ///
    /// Decoding the whole string at once and then zipping the result against
    /// the code list assumed the two lined up index for index. They do not:
    /// an encoding that defines no glyph for a code yields a shorter string,
    /// and a one-to-many `/ToUnicode` entry yields a longer one. Either way
    /// every following glyph took the wrong code โ€” and so the wrong width and
    /// the wrong advance. Decoding per code removes the assumption entirely.
    fn decode_codes(&self, bytes: &[u8]) -> Vec<DecodedCode> {
        // Composite fonts are read two bytes at a time. This is still an
        // approximation โ€” a real CMap may define variable-length ranges โ€” but
        // it is now applied consistently to codes *and* text.
        let chunk = if self.is_composite { 2 } else { 1 };
        bytes
            .chunks(chunk)
            .map(|c| {
                let code = c.iter().fold(0u32, |acc, &b| (acc << 8) | b as u32);
                DecodedCode {
                    code,
                    text: self.decode_one(c, code),
                }
            })
            .collect()
    }

    /// Decode a single code's bytes into the text it produces.
    fn decode_one(&self, bytes: &[u8], code: u32) -> String {
        // A /Differences override wins, but only for simple fonts: the array
        // is indexed by single byte codes.
        if !self.is_composite
            && let Some(diffs) = &self.differences
            && let Ok(byte) = u8::try_from(code)
            && let Some(c) = diffs.get(&byte)
        {
            return c.to_string();
        }
        match &self.encoding {
            Some(enc) => enc.bytes_to_string(bytes).unwrap_or_else(|_| latin1(bytes)),
            None => latin1(bytes),
        }
    }
}

fn latin1(bytes: &[u8]) -> String {
    bytes.iter().map(|&b| b as char).collect()
}

/// Glyph-width table. PDF widths are stored in units of 1/1000 of the font
/// size; values here are pre-divided by 1000 (i.e. width = ratio of font
/// size). Missing entries fall back to `default_width`.
struct Widths {
    by_code: HashMap<u32, f32>,
    default_width: f32,
}

impl Widths {
    fn empty() -> Self {
        Self {
            by_code: HashMap::new(),
            default_width: 0.5,
        }
    }

    fn width_of(&self, code: u32) -> f32 {
        self.by_code
            .get(&code)
            .copied()
            .unwrap_or(self.default_width)
    }
}

impl<'doc> FontInfo<'doc> {
    fn from_dict(doc: &'doc LDoc, dict: &'doc Dictionary) -> Self {
        let encoding = dict.get_font_encoding(doc).ok();
        let is_composite = matches!(
            dict.get(b"Subtype").and_then(Object::as_name).ok(),
            Some(b"Type0")
        );
        let widths = if is_composite {
            crate::parser::fonts::widths::extract_type0(doc, dict)
                .map(|by_code| Widths {
                    by_code,
                    default_width: 0.5,
                })
                .unwrap_or_else(Widths::empty)
        } else {
            simple_widths(dict).unwrap_or_else(Widths::empty)
        };
        let differences = crate::parser::fonts::differences::extract(doc, dict);
        Self {
            encoding,
            widths,
            is_composite,
            differences,
        }
    }
}

fn simple_widths(dict: &Dictionary) -> Option<Widths> {
    // A simple font is indexed by single bytes, so /FirstChar outside 0..=255
    // is malformed. `as u32` would have turned -1 into u32::MAX and then
    // overflowed the addition below (a debug panic, a silent wrap in release).
    let first_char = dict.get(b"FirstChar").ok().and_then(|o| match o {
        Object::Integer(i) => u8::try_from(*i).ok().map(u32::from),
        _ => None,
    })?;
    let widths_arr = match dict.get(b"Widths").ok()? {
        Object::Array(a) => a,
        _ => return None,
    };
    let mut by_code = HashMap::with_capacity(widths_arr.len());
    for (i, w) in widths_arr.iter().enumerate() {
        let Some(code) = u32::try_from(i)
            .ok()
            .and_then(|i| first_char.checked_add(i))
        else {
            break;
        };
        if let Some(v) = num_of(w) {
            by_code.insert(code, v / 1000.0);
        }
    }
    let default_width = dict
        .get(b"MissingWidth")
        .ok()
        .and_then(num_of)
        .map(|v| v / 1000.0)
        .unwrap_or(0.5);
    Some(Widths {
        by_code,
        default_width,
    })
}

// ============================================================================
// Glyph emission
// ============================================================================

fn emit_string(
    bytes: &[u8],
    tm: &mut TextMatrices,
    gs: &GraphicsState,
    fonts: &FontTable,
    geom: &PageGeometry,
    page_rotation: Matrix,
    out: &mut Vec<Char>,
) {
    let ctm = gs.ctm.then(page_rotation);
    let ts = &gs.text;
    let font = fonts.get(&ts.font_name);

    let fontname_str = String::from_utf8_lossy(&ts.font_name).into_owned();
    let fontname = CompactString::from(&fontname_str);

    let is_composite = font.is_some_and(|f| f.is_composite);

    // One entry per glyph code, each carrying its own decoded text. Codes and
    // text can no longer drift apart, however the encoding behaves.
    let decoded: Vec<DecodedCode> = match font {
        Some(f) => f.decode_codes(bytes),
        None => bytes
            .iter()
            .map(|&b| DecodedCode {
                code: b as u32,
                text: (b as char).to_string(),
            })
            .collect(),
    };

    for DecodedCode { code, text } in decoded {
        let w = font.map(|f| f.widths.width_of(code)).unwrap_or(0.5);
        let glyph_width = w * ts.font_size;

        // Position in text space is (0, rise); transform through tm then ctm.
        let trm = tm.tm.then(ctm);
        let origin = trm.transform(Point::new(0.0, ts.rise));
        let upper = trm.transform(Point::new(0.0, ts.rise + ts.font_size));

        let x_scale = trm.x_scale();
        // `Tf` sets the size in text space; the matrices then scale it onto
        // the page. Reporting only the matrix scale would say "0.75" for a
        // 12pt font drawn under a 0.75 CTM.
        let effective_size = ts.font_size * trm.y_scale().max(x_scale);
        // Top-down conversion: PDF y grows upwards from page bottom.
        let top = geom.height - upper.y;
        let bottom = geom.height - origin.y;

        // `Tz` scales glyphs horizontally. It lives in the text state rather
        // than in `tm`, so it has to be applied to the box explicitly โ€” the
        // advance below already carries it, which is why the two disagreed:
        // with `200 Tz` advances doubled while reported widths did not move.
        let glyph_render_width = glyph_width * ts.h_scale * x_scale;
        let x0 = origin.x;
        let x1 = x0 + glyph_render_width;

        let upright = trm.is_upright();

        // A negative font size (or a mirroring matrix) runs the box backwards.
        // `Char` documents x0 <= x1 and top <= bottom, and `build_word` folds
        // these with min/max, so hand back an ordered box either way.
        let (x0, x1) = if x0 <= x1 { (x0, x1) } else { (x1, x0) };
        let (top, bottom) = if top <= bottom {
            (top, bottom)
        } else {
            (bottom, top)
        };

        // A code the encoding defines no glyph for still occupies a position
        // and still advances โ€” it just draws nothing, so no Char is emitted.
        // A code that decodes to several characters produces one Char holding
        // them all, which is what `Char::text` already documents, and advances
        // once rather than once per character.
        let visible: String = text.chars().filter(|c| !c.is_control()).collect();
        if !visible.is_empty() {
            out.push(Char {
                text: CompactString::from(visible.as_str()),
                x0,
                x1,
                top,
                bottom,
                doctop: top + geom.doctop_offset,
                size: effective_size,
                fontname: fontname.clone(),
                upright,
            });
        }

        // Advance text matrix in *text* space (before ctm).
        let word_space = word_space_for(code, is_composite, ts);
        let adv_t = (w * ts.font_size + ts.char_space + word_space) * ts.h_scale;
        tm.advance(adv_t);
    }
}

/// Word spacing per PDF 32000-1 ยง9.3.3: it applies to the single-byte code
/// 32, whatever that byte happens to decode to.
///
/// Keying it on the decoded character instead meant a font whose
/// `/Differences` remaps code 32 to some other glyph never received `Tw` at
/// all. The mirror case matters too: in a composite font a two-byte CID may
/// contain `0x20` without being a word space, and must not receive it.
fn word_space_for(code: u32, is_composite: bool, ts: &TextParams) -> f32 {
    // Composite fonts only get word spacing for a genuine single-byte 32,
    // which `codes` never produces from a two-byte CID.
    if is_composite {
        return 0.0;
    }
    if code == 32 { ts.word_space } else { 0.0 }
}