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
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
//! Predefined CMap File Loading Tests for Phase 6.2
//!
//! Tests for loading and caching Adobe predefined CMaps:
//! - Adobe-GB1 (Simplified Chinese, CID 0-20,941)
//! - Adobe-CNS1 (Traditional Chinese, CID 0-20,992)
//! - Adobe-Japan1 (Japanese, CID 0-23,057)
//! - Adobe-Korea1 (Korean, CID 0-18,351)
//!
//! Predefined CMaps are standard character mappings maintained by Adobe.
//! Loading them efficiently avoids embedding large CMaps in PDF files.
//!
//! Expected Impact:
//! - Automatic fallback for CJK fonts without embedded ToUnicode
//! - 15-25% reduction in PDF file sizes for CJK documents
//! - Support for predefined CMap names (e.g., "H", "V", "HV")
//!
//! Spec: PDF 32000-1:2008 Section 9.7.5.2 (CIDToGIDMap)
//! Adobe CMap Registry: https://github.com/adobe-type-tools/cmap-resources

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

#[test]
fn test_predefined_cmap_adobe_gb1_loading() {
    //! Test loading Adobe-GB1 predefined CMap for Simplified Chinese.
    //!
    //! Adobe-GB1 maps character collections:
    //! - Supplement 0: 8,078 characters (Chinese General Hanzi)
    //! - CID range: 0x0000 - 0x1F8F (0 - 8,079)
    //! - Common mappings:
    //!   - CID 0x0020 → U+4E00 (一)
    //!   - CID 0x0100 → U+4E00
    //!   - CID 0x0374 → U+FF08 (()
    //!
    //! The predefined CMap should be loadable from Adobe's registry.

    // Simulate what would be in the predefined CMap
    // (In production, this would be loaded from Adobe's CMap files)
    let predefined_gb1_sample = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (GB1)
/Supplement 0
>> def
/CMapName /H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
100 beginbfchar
<0020> <4E00>
<0021> <4E8C>
<0022> <4E09>
<0023> <56DB>
<0024> <4E94>
<0025> <516D>
<0026> <4E03>
<0027> <4E09>
<0028> <4E5D>
<0029> <5341>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#
    .as_bytes()
    .to_vec();

    let font = FontInfo {
        base_font: "SimHei".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("GB1-H".to_string()),
        to_unicode: Some(LazyCMap::new(predefined_gb1_sample)),
        font_weight: Some(400),
        flags: None,
        stem_v: Some(80.0),
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "GB1".to_string(),
            supplement: 0,
        }),
        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: Predefined CMap mappings work
    assert_eq!(font.char_to_unicode(0x0020), Some("".to_string()));
    assert_eq!(font.char_to_unicode(0x0021), Some("".to_string()));
    assert_eq!(font.char_to_unicode(0x0022), Some("".to_string()));
}

#[test]
fn test_predefined_cmap_adobe_cns1_loading() {
    //! Test loading Adobe-CNS1 predefined CMap for Traditional Chinese.
    //!
    //! Adobe-CNS1 (Big5 character collection):
    //! - Supplement 0: 13,648 characters
    //! - Supplement 7: 20,992 characters (total)
    //! - CID range: 0x0000 - 0x51FF (0 - 20,991)
    //! - Similar to GB1 but for Traditional Chinese
    //!
    //! Common mappings should be accessible via predefined CMap.

    let predefined_cns1_sample = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (CNS1)
/Supplement 0
>> def
/CMapName /H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
50 beginbfchar
<0001> <4E00>
<0002> <4E8C>
<0003> <4E09>
<0004> <56DB>
<0005> <4E94>
<0010> <6237>
<0020> <7B2C>
<0030> <4E00>
<0040> <4E8C>
<0050> <4E09>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#
    .as_bytes()
    .to_vec();

    let font = FontInfo {
        base_font: "MingLiU".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("CNS1-H".to_string()),
        to_unicode: Some(LazyCMap::new(predefined_cns1_sample)),
        font_weight: Some(400),
        flags: None,
        stem_v: Some(85.0),
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "CNS1".to_string(),
            supplement: 0,
        }),
        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: CNS1 mappings work
    assert_eq!(font.char_to_unicode(0x0001), Some("".to_string()));
    assert_eq!(font.char_to_unicode(0x0002), Some("".to_string()));
    assert_eq!(font.char_to_unicode(0x0003), Some("".to_string()));
}

#[test]
fn test_predefined_cmap_adobe_japan1_loading() {
    //! Test loading Adobe-Japan1 predefined CMap for Japanese.
    //!
    //! Adobe-Japan1 (Japanese character collection):
    //! - Supplement 0-7: 23,057 characters total
    //! - Includes Hiragana, Katakana, Kanji, symbols
    //! - CID range: 0x0000 - 0x59FF (0 - 22,783 + extensions)
    //! - Common mappings:
    //!   - CID 0x0001-0x0085: Hiragana (ぁ-ん)
    //!   - CID 0x0086-0x0109: Katakana (ァ-ン)
    //!   - CID 0x010A+: Kanji

    let predefined_japan1_sample = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (Japan1)
/Supplement 0
>> def
/CMapName /H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
30 beginbfchar
<0001> <3041>
<0002> <3043>
<0003> <3045>
<0004> <3047>
<0005> <3049>
<0086> <30A1>
<0087> <30A3>
<0088> <30A5>
<0089> <30A7>
<008A> <30A9>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#
    .as_bytes()
    .to_vec();

    let font = FontInfo {
        base_font: "Hiragino".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("Japan1-H".to_string()),
        to_unicode: Some(LazyCMap::new(predefined_japan1_sample)),
        font_weight: Some(400),
        flags: None,
        stem_v: Some(82.0),
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "Japan1".to_string(),
            supplement: 0,
        }),
        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: Hiragana characters
    assert_eq!(font.char_to_unicode(0x0001), Some("".to_string())); // Small a
    assert_eq!(font.char_to_unicode(0x0002), Some("".to_string())); // Small i

    // Verify: Katakana characters
    assert_eq!(font.char_to_unicode(0x0086), Some("".to_string())); // Small A
    assert_eq!(font.char_to_unicode(0x0087), Some("".to_string())); // Small I
}

#[test]
fn test_predefined_cmap_adobe_korea1_loading() {
    //! Test loading Adobe-Korea1 predefined CMap for Korean.
    //!
    //! Adobe-Korea1 (Korean character collection):
    //! - 18,352 characters (KS X 1001)
    //! - CID range: 0x0000 - 0x475F (0 - 18,271)
    //! - Hangul (Korean alphabet) and Hanja (Chinese characters)
    //! - Common mappings for Hangul syllables

    let predefined_korea1_sample = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (Korea1)
/Supplement 0
>> def
/CMapName /H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
20 beginbfchar
<0001> <AC00>
<0002> <AC01>
<0003> <AC02>
<0004> <AC03>
<0005> <AC04>
<0020> <C911>
<0030> <B098>
<0040> <B2E4>
<0050> <B77C>
<0060> <B9C8>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#
    .as_bytes()
    .to_vec();

    let font = FontInfo {
        base_font: "NotoSansKR".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("Korea1-H".to_string()),
        to_unicode: Some(LazyCMap::new(predefined_korea1_sample)),
        font_weight: Some(400),
        flags: None,
        stem_v: Some(85.0),
        embedded_font_data: None,
        truetype_cmap: std::sync::OnceLock::new(),
        is_truetype_font: false,
        cid_to_gid_map: None,
        cid_system_info: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "Korea1".to_string(),
            supplement: 0,
        }),
        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: Hangul syllables
    assert_eq!(font.char_to_unicode(0x0001), Some("".to_string())); // First Hangul syllable (U+AC00)
    assert_eq!(font.char_to_unicode(0x0005), Some("".to_string())); // Fifth syllable (U+AC04)
}

#[test]
fn test_predefined_cmap_caching_same_identity() {
    //! Test that predefined CMaps are cached and reused.
    //!
    //! When multiple fonts reference the same predefined CMap:
    //! 1. First font loads the CMap
    //! 2. CMap is cached in global registry
    //! 3. Second font reuses cached CMap (no reload)
    //! 4. Performance benefit from avoiding redundant parsing
    //!
    //! This is critical for documents with repeated fonts like:
    //! - Multi-page documents with "SimHei-GB1-H" on every page
    //! - CMap loaded once, reused 100+ times

    let cmap_bytes = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (GB1)
/Supplement 0
>> def
/CMapName /H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
10 beginbfchar
<0020> <4E00>
<0021> <4E8C>
<0022> <4E09>
<0023> <56DB>
<0024> <4E94>
<0025> <516D>
<0026> <4E03>
<0027> <4E5D>
<0028> <5341>
<0029> <5343>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#
    .as_bytes()
    .to_vec();

    // Create two fonts with the same predefined CMap
    let font1 = FontInfo {
        base_font: "SimHei-Page1".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("GB1-H".to_string()),
        to_unicode: Some(LazyCMap::new(cmap_bytes.clone())),
        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: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "GB1".to_string(),
            supplement: 0,
        }),
        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(),
    };

    let font2 = FontInfo {
        base_font: "SimHei-Page2".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("GB1-H".to_string()),
        to_unicode: Some(LazyCMap::new(cmap_bytes.clone())),
        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: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "GB1".to_string(),
            supplement: 0,
        }),
        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(),
    };

    // Both fonts should access the same CMap
    // (In production, cache would ensure it's loaded once)
    assert_eq!(font1.char_to_unicode(0x0020), font2.char_to_unicode(0x0020));
    assert_eq!(font1.char_to_unicode(0x0021), font2.char_to_unicode(0x0021));
}

#[test]
fn test_predefined_cmap_vertical_writing_support() {
    //! Test loading predefined CMaps for vertical writing systems.
    //!
    //! CJK languages support both horizontal and vertical writing:
    //! - Horizontal: "H" suffix (e.g., "GB1-H")
    //! - Vertical: "V" suffix (e.g., "GB1-V")
    //! - Hybrid: "HV" suffix (supports both)
    //!
    //! Vertical CMaps may have different character mappings for
    //! layout-sensitive characters (punctuation, symbols).

    let vertical_cmap = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (GB1)
/Supplement 0
>> def
/CMapName /V def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
10 beginbfchar
<0020> <4E00>
<0021> <4E8C>
<0022> <4E09>
<0023> <FF0C>
<0024> <3001>
<0025> <3002>
<0026> <4E03>
<0027> <FF1A>
<0028> <FF1F>
<0029> <3006>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#
    .as_bytes()
    .to_vec();

    let font = FontInfo {
        base_font: "SimHei-V".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("GB1-V".to_string()), // Vertical
        to_unicode: Some(LazyCMap::new(vertical_cmap)),
        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: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "GB1".to_string(),
            supplement: 0,
        }),
        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: Vertical writing support
    assert_eq!(font.char_to_unicode(0x0023), Some("".to_string())); // Vertical comma
    assert_eq!(font.char_to_unicode(0x0024), Some("".to_string())); // Vertical enumeration comma
}

#[test]
fn test_predefined_cmap_large_supplement_versions() {
    //! Test loading predefined CMaps with higher supplement versions.
    //!
    //! Adobe CMap Registry includes multiple supplement versions:
    //! - Adobe-GB1 Supplement 0: 8,078 chars
    //! - Adobe-GB1 Supplement 2: 20,933 chars (extends coverage)
    //! - Adobe-CNS1 Supplement 7: 20,992 chars (standard modern)
    //! - Adobe-Japan1 Supplement 7: 23,057 chars
    //!
    //! Higher supplements add new characters without breaking compatibility.

    let supplement_2_cmap = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (GB1)
/Supplement 2
>> def
/CMapName /H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
5 beginbfchar
<1F8E> <2F808>
<1F8F> <2F809>
<2000> <4E00>
<2001> <4E8C>
<2002> <4E09>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#
    .as_bytes()
    .to_vec();

    let font = FontInfo {
        base_font: "SimHei-Sup2".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("GB1-H".to_string()),
        to_unicode: Some(LazyCMap::new(supplement_2_cmap)),
        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: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "GB1".to_string(),
            supplement: 2,
        }),
        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: Supplement 2 extended characters
    assert_eq!(font.char_to_unicode(0x2000), Some("".to_string()));
    assert_eq!(font.char_to_unicode(0x2001), Some("".to_string()));
}

#[test]
fn test_predefined_cmap_fallback_when_embedding_unavailable() {
    //! Test that predefined CMaps serve as fallback when ToUnicode unavailable.
    //!
    //! Workflow:
    //! 1. PDF has Type0 font with CIDSystemInfo (Adobe, GB1)
    //! 2. PDF has no embedded ToUnicode CMap
    //! 3. System loads predefined "Adobe-GB1-H" CMap from registry
    //! 4. Character mapping works without ToUnicode
    //!
    //! This is the primary use case for predefined CMaps.
    //! Many PDFs omit ToUnicode to reduce file size (can be 5-20KB per CMap).

    // Note: In production, this would be loaded from Adobe's registry
    // For testing, we use a minimal sample representing what's in registry
    let predefined_cmap = r#"
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (GB1)
/Supplement 0
>> def
/CMapName /H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
10 beginbfchar
<0001> <4E00>
<0002> <4E8C>
<0003> <4E09>
<0004> <56DB>
<0005> <4E94>
<0020> <FF08>
<0021> <FF09>
<0022> <A1A1>
<0023> <A1A2>
<0024> <A1A3>
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end
"#
    .as_bytes()
    .to_vec();

    let font = FontInfo {
        base_font: "SimHei".to_string(),
        subtype: "Type0".to_string(),
        encoding: pdf_oxide::fonts::Encoding::Standard("GB1-H".to_string()),
        to_unicode: Some(LazyCMap::new(predefined_cmap)), // Simulating fallback to predefined
        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: Some(pdf_oxide::fonts::CIDSystemInfo {
            registry: "Adobe".to_string(),
            ordering: "GB1".to_string(),
            supplement: 0,
        }),
        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: Text extraction works even without custom ToUnicode
    assert_eq!(font.char_to_unicode(0x0001), Some("".to_string()));
    assert_eq!(font.char_to_unicode(0x0002), Some("".to_string()));
    assert_eq!(font.char_to_unicode(0x0003), Some("".to_string()));
}