pdf_oxide 0.3.22

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
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
//! Lazy CMap Loading Tests for Phase 5.1
//!
//! Tests for lazy loading of ToUnicode CMaps to improve performance:
//! - CMaps not parsed immediately on font creation
//! - Parsed only on first character lookup
//! - Result cached for subsequent lookups
//! - Proper thread-safe access via Mutex
//!
//! Expected Impact:
//! - 30-40% faster initial font parsing
//! - Reduced memory usage for fonts with large CMaps
//! - Faster document opening experience
//!
//! Spec: PDF 32000-1:2008 Section 9.10.3 (ToUnicode CMaps)

use pdf_oxide::fonts::cmap::LazyCMap;
use pdf_oxide::fonts::FontInfo;
use std::collections::HashMap;

#[test]
fn test_lazy_cmap_not_parsed_on_creation() {
    //! Test that ToUnicode CMaps are not parsed when FontInfo is created,
    //! only when first character lookup occurs.
    //!
    //! Current behavior: CMaps are fully parsed on font dictionary parsing
    //! Desired behavior: Store raw stream, parse on first access
    //!
    //! This test verifies:
    //! 1. Font can be created with CMap stream data
    //! 2. Parsing is deferred until char_to_unicode() is called
    //! 3. No performance penalty on font loading

    let simple_cmap = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (Identity)
/Supplement 0
>> def
/CMapName /Identity-H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
3 beginbfchar
<0041> <0041>
<0042> <0042>
<0043> <0043>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#;

    // Create font with lazy CMap wrapper
    let font = FontInfo {
        base_font: "TestFont".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Identity,
        // This is a lazy wrapper - CMap will be parsed on first character lookup
        to_unicode: Some(LazyCMap::new(simple_cmap.as_bytes().to_vec())),
        font_weight: None,
        flags: None,
        stem_v: None,
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: None,
        cid_font_type: None,
        cid_widths: None,
        cid_default_width: 1000.0,
        widths: None,
        first_char: None,
        last_char: None,
        default_width: 500.0,
        cff_gid_map: None,
        multi_char_map: HashMap::new(),
        byte_to_char_table: std::sync::OnceLock::new(),
        byte_to_width_table: std::sync::OnceLock::new(),
    };

    // Verify: Character lookup should still work (lazy parsing on first access)
    let result = font.char_to_unicode(0x0041);
    assert_eq!(result, Some("A".to_string()), "First lookup should trigger lazy parse");

    // Verify: Subsequent lookups should use cached result (no re-parsing)
    let result2 = font.char_to_unicode(0x0041);
    assert_eq!(result2, Some("A".to_string()), "Cached result should be available");

    let result3 = font.char_to_unicode(0x0042);
    assert_eq!(result3, Some("B".to_string()), "Other characters should also work");
}

#[test]
fn test_lazy_cmap_thread_safe_parsing() {
    //! Test that lazy CMap parsing is thread-safe.
    //!
    //! The lazy wrapper uses Mutex<Option<CMap>> to ensure:
    //! - Multiple threads can safely access the same CMap
    //! - Parsing happens only once, even with concurrent access
    //! - Results are consistent across threads

    let cmap_data = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (Identity)
/Supplement 0
>> def
/CMapName /Identity-H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
2 beginbfchar
<0061> <0061>
<0062> <0062>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#;

    let font = FontInfo {
        base_font: "TestFont".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Identity,
        to_unicode: Some(LazyCMap::new(cmap_data.as_bytes().to_vec())),
        font_weight: None,
        flags: None,
        stem_v: None,
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: None,
        cid_font_type: None,
        cid_widths: None,
        cid_default_width: 1000.0,
        widths: None,
        first_char: None,
        last_char: None,
        default_width: 500.0,
        cff_gid_map: None,
        multi_char_map: HashMap::new(),
        byte_to_char_table: std::sync::OnceLock::new(),
        byte_to_width_table: std::sync::OnceLock::new(),
    };

    // Verify same result from sequential calls
    assert_eq!(font.char_to_unicode(0x0061), Some("a".to_string()));
    assert_eq!(font.char_to_unicode(0x0061), Some("a".to_string()));
    assert_eq!(font.char_to_unicode(0x0062), Some("b".to_string()));
}

#[test]
fn test_lazy_cmap_large_map_deferred_parsing() {
    //! Test that large CMaps benefit from lazy loading by deferring
    //! expensive parsing operations.
    //!
    //! Large CMaps (>10k entries) should:
    //! - Not be parsed during font dictionary loading
    //! - Only be parsed when first character lookup occurs
    //! - Show measurable performance improvement
    //!
    //! This test creates a reasonably large CMap to verify lazy behavior

    let mut large_cmap_entries = String::from(
        r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (Identity)
/Supplement 0
>> def
/CMapName /Identity-H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
"#,
    );

    // Add 100 bfchar entries to simulate larger CMap
    large_cmap_entries.push_str("100 beginbfchar\n");
    for i in 0..100 {
        large_cmap_entries.push_str(&format!("<{:04X}> <{:04X}>\n", 0x0100 + i, 0x0100 + i));
    }
    large_cmap_entries.push_str("endbfchar\n");

    large_cmap_entries.push_str(
        r#"
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#,
    );

    let font = FontInfo {
        base_font: "LargeMapFont".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Identity,
        // Create lazy CMap from raw bytes
        to_unicode: Some(LazyCMap::new(large_cmap_entries.as_bytes().to_vec())),
        font_weight: None,
        flags: None,
        stem_v: None,
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: None,
        cid_font_type: None,
        cid_widths: None,
        cid_default_width: 1000.0,
        widths: None,
        first_char: None,
        last_char: None,
        default_width: 500.0,
        cff_gid_map: None,
        multi_char_map: HashMap::new(),
        byte_to_char_table: std::sync::OnceLock::new(),
        byte_to_width_table: std::sync::OnceLock::new(),
    };

    // Verify: All entries should be accessible
    assert_eq!(
        font.char_to_unicode(0x0100),
        Some("\u{0100}".to_string()),
        "First entry accessible"
    );
    assert_eq!(
        font.char_to_unicode(0x0150),
        Some("\u{0150}".to_string()),
        "Middle entry accessible"
    );
    assert_eq!(
        font.char_to_unicode(0x0163),
        Some("\u{0163}".to_string()),
        "Last entry accessible"
    );
}

#[test]
fn test_lazy_cmap_cache_hit_performance() {
    //! Test that lazy CMap caching provides performance benefits.
    //!
    //! Expected behavior:
    //! - First access: Parse CMap (slower)
    //! - Subsequent accesses: Use cached result (faster)
    //! - Multiple character lookups hit same cached CMap
    //!
    //! This verifies the caching mechanism works correctly

    let cmap_data = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (Identity)
/Supplement 0
>> def
/CMapName /Identity-H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
10 beginbfchar
<0030> <0030>
<0031> <0031>
<0032> <0032>
<0033> <0033>
<0034> <0034>
<0035> <0035>
<0036> <0036>
<0037> <0037>
<0038> <0038>
<0039> <0039>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#;

    let font = FontInfo {
        base_font: "DigitFont".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Identity,
        to_unicode: Some(LazyCMap::new(cmap_data.as_bytes().to_vec())),
        font_weight: None,
        flags: None,
        stem_v: None,
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: None,
        cid_font_type: None,
        cid_widths: None,
        cid_default_width: 1000.0,
        widths: None,
        first_char: None,
        last_char: None,
        default_width: 500.0,
        cff_gid_map: None,
        multi_char_map: HashMap::new(),
        byte_to_char_table: std::sync::OnceLock::new(),
        byte_to_width_table: std::sync::OnceLock::new(),
    };

    // Multiple lookups should all hit the cache
    for code in 0x30..=0x39 {
        let result = font.char_to_unicode(code);
        assert!(result.is_some(), "Digit 0x{:02X} should be mapped", code);
    }

    // Repeated accesses should be very fast (from cache)
    for code in 0x30..=0x39 {
        let result = font.char_to_unicode(code);
        assert!(result.is_some(), "Cached access should work");
    }
}

#[test]
fn test_lazy_cmap_with_notdefrange_lazy_parsing() {
    //! Test that lazy loading works with advanced CMap features
    //! (added in Phase 4.1).
    //!
    //! Even with beginnotdefrange and escape sequences,
    //! the lazy loading should defer all parsing until first access

    let cmap_with_notdef = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (Identity)
/Supplement 0
>> def
/CMapName /Identity-H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
2 beginbfchar
<0041> <0041>
<0042> <0042>
endbfchar
1 beginnotdefrange
<0000> <0040> <FFFD>
endnotdefrange
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#;

    let font = FontInfo {
        base_font: "TestFont".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Identity,
        to_unicode: Some(LazyCMap::new(cmap_with_notdef.as_bytes().to_vec())),
        font_weight: None,
        flags: None,
        stem_v: None,
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: None,
        cid_font_type: None,
        cid_widths: None,
        cid_default_width: 1000.0,
        widths: None,
        first_char: None,
        last_char: None,
        default_width: 500.0,
        cff_gid_map: None,
        multi_char_map: HashMap::new(),
        byte_to_char_table: std::sync::OnceLock::new(),
        byte_to_width_table: std::sync::OnceLock::new(),
    };

    // Verify: Explicit mappings work
    assert_eq!(
        font.char_to_unicode(0x0041),
        Some("A".to_string()),
        "Explicit mapping should work"
    );

    // Verify: Notdefrange fallback works (from lazy-loaded CMap)
    assert_eq!(
        font.char_to_unicode(0x0001),
        Some("\u{FFFD}".to_string()),
        "Notdefrange should work with lazy loading"
    );
}