pdfpurr 0.4.0

A comprehensive pure-Rust PDF library for reading, writing, rendering, and validating PDF documents
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
//! Invisible text layer generation from OCR results.
//!
//! Converts OCR word bounding boxes into a PDF content stream with
//! invisible text (rendering mode 3) positioned to overlay the
//! original page image.
//!
//! Uses a custom encoding with a `/ToUnicode` CMap so that every
//! Unicode character is preserved — no lossy WinAnsi conversion.
//! Screen readers, copy/paste, and search all see the original text.

use std::collections::BTreeMap;

use crate::content::ContentStreamBuilder;
use crate::core::objects::{Dictionary, Object, PdfName, PdfStream};

use super::config::OcrConfig;
use super::constants::PARAGRAPH_GAP_THRESHOLD;
use super::engine::OcrResult;

/// Font resource name used for OCR invisible text.
pub const OCR_FONT_NAME: &str = "F_OCR";

/// Result of building an OCR text layer, containing the content stream
/// and the ToUnicode CMap needed to decode the custom encoding.
pub struct OcrTextLayer {
    /// The PDF content stream bytes (invisible text with marked content).
    pub content: Vec<u8>,
    /// The ToUnicode CMap stream mapping custom codes → Unicode.
    /// `None` if the text was pure ASCII (no CMap needed).
    pub to_unicode_cmap: Option<PdfStream>,
    /// Character encoding map: Unicode char → code byte(s).
    /// Used internally; exposed for testing.
    pub char_map: BTreeMap<char, u16>,
}

/// Builds a PDF content stream containing invisible text from OCR results.
///
/// Each unique Unicode character gets a sequential code in a custom encoding.
/// A `/ToUnicode` CMap maps these codes back to Unicode for text extraction,
/// copy/paste, and screen readers. This preserves ALL characters — CJK,
/// Arabic, emoji, accented Latin, symbols — without any lossy conversion.
///
/// # Arguments
///
/// * `result` — OCR output with words and bounding boxes in pixel coordinates
/// * `page_width` — Page width in PDF points
/// * `page_height` — Page height in PDF points
/// * `config` — OCR configuration (min confidence, DPI)
pub fn build_ocr_text_layer(
    result: &OcrResult,
    page_width: f64,
    page_height: f64,
    config: &OcrConfig,
) -> OcrTextLayer {
    let mut builder = ContentStreamBuilder::new();

    // Mark the page image as Artifact (not real content for screen readers)
    builder.begin_marked_content("Artifact");
    builder.end_marked_content();

    // Scale factor: OCR pixels → PDF points
    let scale_x = page_width / result.image_width as f64;
    let scale_y = page_height / result.image_height as f64;

    // Group words into paragraphs
    let paragraphs = group_into_paragraphs(&result.words, config.min_confidence);

    // Phase 1: Collect all unique characters and assign codes
    let char_map = build_char_map(&paragraphs);
    let is_two_byte = char_map.len() > 255;

    // Phase 2: Write invisible text using the custom encoding
    builder.begin_text().set_text_rendering_mode(3); // invisible

    for (mcid, para_words) in paragraphs.iter().enumerate() {
        builder.end_text();
        builder.begin_marked_content_with_properties("P", mcid as u32);
        builder.begin_text().set_text_rendering_mode(3);

        for word in para_words {
            let pdf_x = word.x as f64 * scale_x;
            let pdf_y = page_height - (word.y as f64 + word.height as f64) * scale_y;
            let font_size = (word.height as f64 * scale_y).max(1.0);

            let encoded = encode_text(&word.text, &char_map, is_two_byte);

            builder
                .set_font(OCR_FONT_NAME, font_size)
                .set_text_matrix(1.0, 0.0, 0.0, 1.0, pdf_x, pdf_y)
                .show_text_bytes(&encoded);
        }

        builder.end_text();
        builder.end_marked_content();
        builder.begin_text().set_text_rendering_mode(3);
    }

    builder.end_text();

    // Phase 3: Build ToUnicode CMap — essential for text extraction.
    // Without this CMap, screen readers and search cannot decode the
    // custom character codes back to Unicode. The function is infallible.
    let to_unicode_cmap = if !char_map.is_empty() {
        Some(build_ocr_to_unicode_cmap(&char_map, is_two_byte))
    } else {
        None
    };

    OcrTextLayer {
        content: builder.build(),
        to_unicode_cmap,
        char_map,
    }
}

/// Assigns a sequential code to each unique character across all paragraphs.
///
/// Code 0 is reserved (`.notdef`). Codes start at 1.
fn build_char_map(paragraphs: &[Vec<&super::engine::OcrWord>]) -> BTreeMap<char, u16> {
    let mut map = BTreeMap::new();
    let mut next_code: u16 = 1;

    for para in paragraphs {
        for word in para {
            for ch in word.text.chars() {
                map.entry(ch).or_insert_with(|| {
                    let code = next_code;
                    next_code = next_code.saturating_add(1);
                    code
                });
            }
        }
    }

    map
}

/// Encodes a string using the character map.
///
/// Each character is replaced by its assigned code. Unknown characters
/// (shouldn't happen since we built the map from this text) get code 0.
fn encode_text(text: &str, char_map: &BTreeMap<char, u16>, two_byte: bool) -> Vec<u8> {
    let mut bytes = Vec::with_capacity(text.len() * if two_byte { 2 } else { 1 });
    for ch in text.chars() {
        let code = char_map.get(&ch).copied().unwrap_or(0);
        if two_byte {
            bytes.push((code >> 8) as u8);
            bytes.push((code & 0xFF) as u8);
        } else {
            bytes.push(code as u8);
        }
    }
    bytes
}

/// Builds a ToUnicode CMap stream for the OCR font.
///
/// Maps each assigned code back to its Unicode code point so that
/// PDF viewers, screen readers, and copy/paste decode correctly.
///
/// This function is infallible — the CMap is a plain text string
/// stored uncompressed. No I/O, no compression, no external deps.
fn build_ocr_to_unicode_cmap(char_map: &BTreeMap<char, u16>, two_byte: bool) -> PdfStream {
    use std::fmt::Write;

    let hex_width = if two_byte { 4 } else { 2 };
    let mut cmap = String::with_capacity(512 + char_map.len() * 30);

    cmap.push_str("/CIDInit /ProcSet findresource begin\n");
    cmap.push_str("12 dict begin\n");
    cmap.push_str("begincmap\n");
    cmap.push_str("/CIDSystemInfo\n");
    cmap.push_str("<< /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n");
    cmap.push_str("/CMapName /Adobe-Identity-UCS def\n");
    cmap.push_str("/CMapType 2 def\n");
    cmap.push_str("1 begincodespacerange\n");
    if two_byte {
        cmap.push_str("<0000> <FFFF>\n");
    } else {
        cmap.push_str("<00> <FF>\n");
    }
    cmap.push_str("endcodespacerange\n");

    // Sort by code for deterministic output
    let mut mappings: Vec<(u16, char)> = char_map.iter().map(|(&ch, &code)| (code, ch)).collect();
    mappings.sort_by_key(|&(code, _)| code);

    for chunk in mappings.chunks(100) {
        let _ = writeln!(cmap, "{} beginbfchar", chunk.len());
        for &(code, ch) in chunk {
            let unicode = ch as u32;
            if unicode > 0xFFFF {
                // Surrogate pair for non-BMP characters
                let u = unicode - 0x10000;
                let high = 0xD800 + (u >> 10);
                let low = 0xDC00 + (u & 0x3FF);
                let _ = writeln!(
                    cmap,
                    "<{:0>width$X}> <{:04X}{:04X}>",
                    code,
                    high,
                    low,
                    width = hex_width
                );
            } else {
                let _ = writeln!(
                    cmap,
                    "<{:0>width$X}> <{:04X}>",
                    code,
                    unicode,
                    width = hex_width
                );
            }
        }
        cmap.push_str("endbfchar\n");
    }

    cmap.push_str("endcmap\n");
    cmap.push_str("CMapName currentdict /CMap defineresource pop\n");
    cmap.push_str("end\n");
    cmap.push_str("end\n");

    let data = cmap.into_bytes();
    let mut dict = Dictionary::new();
    dict.insert(PdfName::new("Length"), Object::Integer(data.len() as i64));
    PdfStream::new(dict, data)
}

/// Groups OCR words into paragraphs based on vertical proximity.
///
/// Filters out low-confidence and empty words first, then groups by
/// vertical gap exceeding [`PARAGRAPH_GAP_THRESHOLD`].
fn group_into_paragraphs(
    words: &[super::engine::OcrWord],
    _min_confidence: f32,
) -> Vec<Vec<&super::engine::OcrWord>> {
    // Include ALL non-empty words regardless of confidence.
    // The text layer must match the structure builder's word set so that
    // every ActualText entry has a corresponding content stream operator.
    // Low-confidence words may be inaccurate, but omitting them entirely
    // makes the page invisible to screen readers.
    let filtered: Vec<&super::engine::OcrWord> =
        words.iter().filter(|w| !w.text.trim().is_empty()).collect();

    if filtered.is_empty() {
        return Vec::new();
    }

    let mut paragraphs: Vec<Vec<&super::engine::OcrWord>> = Vec::new();
    let mut current: Vec<&super::engine::OcrWord> = vec![filtered[0]];
    let mut prev_bottom = filtered[0].y + filtered[0].height;

    for &word in &filtered[1..] {
        let gap = word.y.saturating_sub(prev_bottom);
        if gap > PARAGRAPH_GAP_THRESHOLD {
            paragraphs.push(std::mem::take(&mut current));
        }
        current.push(word);
        prev_bottom = word.y + word.height;
    }

    if !current.is_empty() {
        paragraphs.push(current);
    }

    paragraphs
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ocr::engine::{OcrResult, OcrWord};

    fn make_result(words: Vec<OcrWord>) -> OcrResult {
        OcrResult {
            words,
            image_width: 2550,  // 8.5" at 300 DPI
            image_height: 3300, // 11" at 300 DPI
        }
    }

    fn default_config() -> OcrConfig {
        OcrConfig::default()
    }

    #[test]
    fn empty_result_produces_minimal_stream() {
        let result = make_result(vec![]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());
        let text = String::from_utf8_lossy(&layer.content);
        assert!(text.contains("Artifact"));
        assert!(layer.to_unicode_cmap.is_none());
    }

    #[test]
    fn ascii_text_encoded_correctly() {
        let result = make_result(vec![OcrWord {
            text: "Hello".to_string(),
            x: 100,
            y: 100,
            width: 200,
            height: 30,
            confidence: 0.9,
        }]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());

        // Char map should have H=1, e=2, l=3, o=4
        assert_eq!(layer.char_map.len(), 4); // H, e, l, o
        assert!(layer.to_unicode_cmap.is_some(), "CMap should be generated");
    }

    #[test]
    fn bullet_character_preserved_in_cmap() {
        let result = make_result(vec![OcrWord {
            text: "item \u{2022} value".to_string(),
            x: 100,
            y: 100,
            width: 300,
            height: 30,
            confidence: 0.9,
        }]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());

        // Bullet should be in char map
        assert!(
            layer.char_map.contains_key(&'\u{2022}'),
            "Bullet should be mapped"
        );

        // CMap should contain the Unicode mapping
        let cmap = layer.to_unicode_cmap.unwrap();
        let cmap_text = String::from_utf8_lossy(&cmap.data);
        assert!(cmap_text.contains("2022"), "CMap should map to U+2022");
    }

    #[test]
    fn cjk_characters_preserved() {
        let result = make_result(vec![OcrWord {
            text: "\u{4E2D}\u{6587}".to_string(), // 中文
            x: 100,
            y: 100,
            width: 200,
            height: 30,
            confidence: 0.9,
        }]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());

        assert!(
            layer.char_map.contains_key(&'\u{4E2D}'),
            "中 should be mapped"
        );
        assert!(
            layer.char_map.contains_key(&'\u{6587}'),
            "文 should be mapped"
        );

        let cmap = layer.to_unicode_cmap.unwrap();
        let cmap_text = String::from_utf8_lossy(&cmap.data);
        assert!(cmap_text.contains("4E2D"), "CMap should map 中");
        assert!(cmap_text.contains("6587"), "CMap should map 文");
    }

    #[test]
    fn smart_quotes_preserved() {
        let result = make_result(vec![OcrWord {
            text: "\u{201C}hello\u{201D}".to_string(),
            x: 100,
            y: 100,
            width: 200,
            height: 30,
            confidence: 0.9,
        }]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());

        let cmap = layer.to_unicode_cmap.unwrap();
        let cmap_text = String::from_utf8_lossy(&cmap.data);
        assert!(cmap_text.contains("201C"), "CMap should map left quote");
        assert!(cmap_text.contains("201D"), "CMap should map right quote");
    }

    #[test]
    fn encode_text_produces_correct_bytes() {
        let mut map = BTreeMap::new();
        map.insert('H', 1u16);
        map.insert('i', 2u16);

        let bytes = encode_text("Hi", &map, false);
        assert_eq!(bytes, vec![1, 2]);
    }

    #[test]
    fn encode_text_two_byte_mode() {
        let mut map = BTreeMap::new();
        map.insert('A', 0x0100u16);

        let bytes = encode_text("A", &map, true);
        assert_eq!(bytes, vec![0x01, 0x00]);
    }

    #[test]
    fn char_map_assigns_sequential_codes() {
        let words = vec![OcrWord {
            text: "abc".to_string(),
            x: 0,
            y: 0,
            width: 100,
            height: 20,
            confidence: 0.9,
        }];
        let paragraphs = group_into_paragraphs(&words, 0.0);
        let map = build_char_map(&paragraphs);

        assert_eq!(map.get(&'a'), Some(&1));
        assert_eq!(map.get(&'b'), Some(&2));
        assert_eq!(map.get(&'c'), Some(&3));
    }

    #[test]
    fn duplicate_chars_share_same_code() {
        let words = vec![OcrWord {
            text: "hello".to_string(),
            x: 0,
            y: 0,
            width: 100,
            height: 20,
            confidence: 0.9,
        }];
        let paragraphs = group_into_paragraphs(&words, 0.0);
        let map = build_char_map(&paragraphs);

        // 'l' appears twice but should have one code
        assert_eq!(map.len(), 4); // h, e, l, o
        let l_code = map.get(&'l').unwrap();
        let encoded = encode_text("hello", &map, false);
        assert_eq!(encoded[2], *l_code as u8);
        assert_eq!(encoded[3], *l_code as u8); // same code for both 'l's
    }

    #[test]
    fn non_bmp_emoji_in_cmap() {
        let result = make_result(vec![OcrWord {
            text: "ok\u{1F44D}".to_string(), // ok👍
            x: 100,
            y: 100,
            width: 200,
            height: 30,
            confidence: 0.9,
        }]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());

        assert!(
            layer.char_map.contains_key(&'\u{1F44D}'),
            "Emoji should be mapped"
        );

        let cmap = layer.to_unicode_cmap.unwrap();
        let cmap_text = String::from_utf8_lossy(&cmap.data);
        // Non-BMP: U+1F44D → surrogate pair D83D DC4D
        assert!(
            cmap_text.contains("D83DDC4D"),
            "CMap should use surrogate pair for emoji"
        );
    }

    #[test]
    fn low_confidence_words_included_for_screen_readers() {
        let result = make_result(vec![
            OcrWord {
                text: "Good".to_string(),
                x: 100,
                y: 100,
                width: 100,
                height: 30,
                confidence: 0.9,
            },
            OcrWord {
                text: "Low".to_string(),
                x: 300,
                y: 100,
                width: 80,
                height: 30,
                confidence: 0.1,
            },
        ]);
        let config = OcrConfig {
            min_confidence: 0.3,
            ..Default::default()
        };
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &config);
        let text = String::from_utf8_lossy(&layer.content);

        // Both words should produce Tj operators
        let tj_count = text.matches("Tj").count();
        assert!(
            tj_count >= 2,
            "Both words should appear, got {} Tj operators",
            tj_count
        );
    }

    #[test]
    fn artifact_marker_for_image() {
        let result = make_result(vec![OcrWord {
            text: "test".to_string(),
            x: 100,
            y: 100,
            width: 100,
            height: 30,
            confidence: 0.9,
        }]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());
        let text = String::from_utf8_lossy(&layer.content);
        assert!(
            text.contains("/Artifact BMC"),
            "Should mark image as artifact"
        );
    }

    #[test]
    fn marked_content_ids_wrap_paragraphs() {
        let result = make_result(vec![
            OcrWord {
                text: "First".to_string(),
                x: 100,
                y: 100,
                width: 200,
                height: 20,
                confidence: 0.9,
            },
            OcrWord {
                text: "Second".to_string(),
                x: 100,
                y: 200,
                width: 200,
                height: 20,
                confidence: 0.9,
            },
        ]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());
        let text = String::from_utf8_lossy(&layer.content);

        assert!(
            text.contains("/P <</MCID 0>> BDC"),
            "First paragraph MCID 0"
        );
        assert!(
            text.contains("/P <</MCID 1>> BDC"),
            "Second paragraph MCID 1"
        );
    }

    #[test]
    fn y_coordinate_is_flipped() {
        let result = make_result(vec![OcrWord {
            text: "top".to_string(),
            x: 100,
            y: 10,
            width: 100,
            height: 20,
            confidence: 0.9,
        }]);
        let layer = build_ocr_text_layer(&result, 612.0, 792.0, &default_config());
        let text = String::from_utf8_lossy(&layer.content);

        let tm_line = text.lines().find(|l| l.contains("Tm")).unwrap();
        let parts: Vec<&str> = tm_line.split_whitespace().collect();
        let y_val: f64 = parts[parts.len() - 2].parse().unwrap();
        assert!(
            y_val > 700.0,
            "Top-of-image word should map to high PDF y, got {}",
            y_val
        );
    }
}