oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
//! Font embedding for PDF generation according to ISO 32000-1 Section 9.8
//!
//! This module provides complete font embedding capabilities including:
//! - TrueType font embedding with subsetting
//! - Font descriptor generation  
//! - Character encoding mappings
//! - CID font support for complex scripts

use crate::error::{PdfError, Result};
use crate::objects::{Dictionary, Object, ObjectId};
use crate::text::fonts::truetype::TrueTypeFont;
use std::collections::{HashMap, HashSet};

/// Font type enumeration for embedding
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FontType {
    /// TrueType font
    TrueType,
    /// Type 0 font (composite/CID)
    Type0,
}

/// Font encoding types for embedding
#[derive(Debug, Clone, PartialEq)]
pub enum FontEncoding {
    /// Standard encoding
    StandardEncoding,
    /// MacRoman encoding
    MacRomanEncoding,
    /// WinAnsi encoding
    WinAnsiEncoding,
    /// Custom encoding with differences
    Custom(Vec<EncodingDifference>),
    /// Identity encoding for CID fonts
    Identity,
}

/// CJK font types for proper CIDSystemInfo configuration
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CjkFontType {
    /// Chinese Simplified (Source Han Sans SC, Noto Sans CJK SC, etc.)
    ChineseSimplified,
    /// Chinese Traditional (Source Han Sans TC, Noto Sans CJK TC, etc.)
    ChineseTraditional,
    /// Japanese (Source Han Sans JP, Noto Sans CJK JP, etc.)
    Japanese,
    /// Korean (Source Han Sans KR, Noto Sans CJK KR, etc.)
    Korean,
    /// Generic CJK (fallback)
    Generic,
}

impl CjkFontType {
    /// Get the appropriate CIDSystemInfo values for this font type
    pub fn cid_system_info(&self) -> (&'static str, &'static str, i32) {
        match self {
            CjkFontType::ChineseSimplified => ("Adobe", "Identity", 0), // Use Identity for multi-script fonts
            CjkFontType::ChineseTraditional => ("Adobe", "Identity", 0), // Use Identity for multi-script fonts
            CjkFontType::Japanese => ("Adobe", "Identity", 0), // Use Identity for multi-script fonts
            CjkFontType::Korean => ("Adobe", "Identity", 0), // Use Identity for multi-script fonts
            CjkFontType::Generic => ("Adobe", "Identity", 0),
        }
    }

    /// Detect CJK font type from font name
    pub fn detect_from_name(font_name: &str) -> Option<Self> {
        let name_lower = font_name.to_lowercase();

        // Source Han Sans detection (various naming patterns)
        if name_lower.contains("sourcehansans")
            || name_lower.contains("source han sans")
            || name_lower.contains("hansans")
            || name_lower.contains("han sans")
            || name_lower.contains("sourcehan")
            || name_lower.contains("source han")
        {
            if name_lower.contains("sc") || name_lower.contains("simplifiedchinese") {
                return Some(CjkFontType::ChineseSimplified);
            }
            if name_lower.contains("tc") || name_lower.contains("traditionalchinese") {
                return Some(CjkFontType::ChineseTraditional);
            }
            if name_lower.contains("jp") || name_lower.contains("japanese") {
                return Some(CjkFontType::Japanese);
            }
            if name_lower.contains("kr") || name_lower.contains("korean") {
                return Some(CjkFontType::Korean);
            }
        }

        // Noto Sans CJK detection
        if name_lower.contains("notosanscjk") || name_lower.contains("noto sans cjk") {
            if name_lower.contains("sc") {
                return Some(CjkFontType::ChineseSimplified);
            }
            if name_lower.contains("tc") {
                return Some(CjkFontType::ChineseTraditional);
            }
            if name_lower.contains("jp") {
                return Some(CjkFontType::Japanese);
            }
            if name_lower.contains("kr") {
                return Some(CjkFontType::Korean);
            }
        }

        // Generic patterns
        if name_lower.contains("chinese") || name_lower.contains("zh") || name_lower.contains("gb")
        {
            if name_lower.contains("traditional")
                || name_lower.contains("tw")
                || name_lower.contains("hk")
            {
                return Some(CjkFontType::ChineseTraditional);
            }
            return Some(CjkFontType::ChineseSimplified);
        }

        if name_lower.contains("japanese")
            || name_lower.contains("jp")
            || name_lower.contains("japan")
        {
            return Some(CjkFontType::Japanese);
        }

        if name_lower.contains("korean")
            || name_lower.contains("kr")
            || name_lower.contains("korea")
        {
            return Some(CjkFontType::Korean);
        }

        None
    }

    /// Determine if a CID font should use CIDFontType2 subtype.
    ///
    /// Per ISO 32000-1 §9.7.4:
    /// - CIDFontType0: CFF/OpenType fonts (PostScript outlines)
    /// - CIDFontType2: TrueType fonts (TrueType outlines)
    ///
    /// Using the wrong subtype causes PDF viewers to fail to render glyphs.
    /// A CFF font declared as CIDFontType2 is structurally invalid.
    ///
    /// # Arguments
    /// * `is_cff` - Whether the font has CFF outlines (OpenType/CFF)
    ///
    /// # Returns
    /// * `true` for TrueType fonts → CIDFontType2
    /// * `false` for CFF/OpenType fonts → CIDFontType0
    pub fn should_use_cidfonttype2(is_cff: bool) -> bool {
        !is_cff
    }
}

/// Encoding difference entry
#[derive(Debug, Clone, PartialEq)]
pub struct EncodingDifference {
    /// Starting character code
    pub code: u8,
    /// Glyph names for consecutive character codes
    pub names: Vec<String>,
}

/// Font flags for font descriptor
#[derive(Debug, Clone, Copy, Default)]
pub struct FontFlags {
    /// All glyphs have the same width
    pub fixed_pitch: bool,
    /// Glyphs have serifs
    pub serif: bool,
    /// Font uses symbolic character set
    pub symbolic: bool,
    /// Font is a script font
    pub script: bool,
    /// Font uses Adobe standard Latin character set
    pub non_symbolic: bool,
    /// Glyphs resemble cursive handwriting
    pub italic: bool,
    /// All glyphs have dominant vertical strokes
    pub all_cap: bool,
    /// Font is a small-cap font
    pub small_cap: bool,
    /// Font weight is bold or black
    pub force_bold: bool,
}

impl FontFlags {
    /// Convert to PDF font flags integer
    pub fn to_flags(&self) -> u32 {
        let mut flags = 0u32;
        if self.fixed_pitch {
            flags |= 1 << 0;
        }
        if self.serif {
            flags |= 1 << 1;
        }
        if self.symbolic {
            flags |= 1 << 2;
        }
        if self.script {
            flags |= 1 << 3;
        }
        if self.non_symbolic {
            flags |= 1 << 5;
        }
        if self.italic {
            flags |= 1 << 6;
        }
        if self.all_cap {
            flags |= 1 << 16;
        }
        if self.small_cap {
            flags |= 1 << 17;
        }
        if self.force_bold {
            flags |= 1 << 18;
        }
        flags
    }
}

/// Font descriptor for PDF embedding
#[derive(Debug, Clone)]
pub struct FontDescriptor {
    /// Font name
    pub font_name: String,
    /// Font flags
    pub flags: FontFlags,
    /// Font bounding box [llx, lly, urx, ury]
    pub bbox: [i32; 4],
    /// Italic angle in degrees
    pub italic_angle: f64,
    /// Maximum height above baseline
    pub ascent: i32,
    /// Maximum depth below baseline (negative)
    pub descent: i32,
    /// Height of capital letters
    pub cap_height: i32,
    /// Thickness of dominant vertical stems
    pub stem_v: i32,
    /// Thickness of dominant horizontal stems
    pub stem_h: i32,
    /// Average character width
    pub avg_width: i32,
    /// Maximum character width
    pub max_width: i32,
    /// Width for missing characters
    pub missing_width: i32,
    /// Font file reference (if embedded)
    pub font_file: Option<ObjectId>,
}

/// Font metrics for embedded fonts
#[derive(Debug, Clone)]
pub struct FontMetrics {
    /// Maximum height above baseline
    pub ascent: i32,
    /// Maximum depth below baseline (negative)
    pub descent: i32,
    /// Height of capital letters
    pub cap_height: i32,
    /// Height of lowercase letters
    pub x_height: i32,
    /// Thickness of dominant vertical stems
    pub stem_v: i32,
    /// Thickness of dominant horizontal stems
    pub stem_h: i32,
    /// Average character width
    pub avg_width: i32,
    /// Maximum character width
    pub max_width: i32,
    /// Width for missing characters
    pub missing_width: i32,
}

/// PDF font embedding manager
#[derive(Debug)]
pub struct FontEmbedder {
    /// Font data cache
    embedded_fonts: HashMap<String, EmbeddedFontData>,
    /// Next font ID
    next_font_id: u32,
}

/// Embedded font data for PDF generation
#[derive(Debug, Clone)]
pub struct EmbeddedFontData {
    /// Font name in PDF
    pub pdf_name: String,
    /// Font type
    pub font_type: FontType,
    /// Font descriptor object
    pub descriptor: FontDescriptor,
    /// Font program data (subset or full)
    pub font_program: Vec<u8>,
    /// Character mappings
    pub encoding: FontEncoding,
    /// Font metrics
    pub metrics: FontMetrics,
    /// Subset glyph set (if subsetted)
    pub subset_glyphs: Option<HashSet<u16>>,
    /// Unicode mappings for ToUnicode CMap
    pub unicode_mappings: HashMap<u16, String>,
}

/// Font embedding options
#[derive(Debug, Clone)]
pub struct EmbeddingOptions {
    /// Whether to subset the font
    pub subset: bool,
    /// Maximum number of glyphs in subset
    pub max_subset_size: Option<usize>,
    /// Whether to compress font streams
    pub compress_font_streams: bool,
    /// Whether to embed font license info
    pub embed_license_info: bool,
}

impl Default for EmbeddingOptions {
    fn default() -> Self {
        Self {
            subset: true,
            max_subset_size: Some(256),
            compress_font_streams: true,
            embed_license_info: false,
        }
    }
}

impl FontEmbedder {
    /// Create a new font embedder
    pub fn new() -> Self {
        Self {
            embedded_fonts: HashMap::new(),
            next_font_id: 1,
        }
    }

    /// Embed a TrueType font with optional subsetting
    pub fn embed_truetype_font(
        &mut self,
        font_data: &[u8],
        used_glyphs: &HashSet<u16>,
        options: &EmbeddingOptions,
    ) -> Result<String> {
        // Parse the TrueType font
        let font = TrueTypeFont::from_data(font_data)
            .map_err(|e| PdfError::FontError(format!("Failed to parse font: {e}")))?;

        // Generate unique font name
        let font_name = format!("ABCDEF+Font{next_id}", next_id = self.next_font_id);
        self.next_font_id += 1;

        // Determine if we should subset
        let should_subset =
            options.subset && used_glyphs.len() < options.max_subset_size.unwrap_or(256);

        // Create font program (subset or full)
        let font_program = if should_subset {
            font.create_subset(used_glyphs)
                .map_err(|e| PdfError::FontError(format!("Failed to create subset: {e}")))?
        } else {
            font_data.to_vec()
        };

        // Extract font metrics
        let metrics = self.extract_font_metrics(&font)?;

        // Create font descriptor
        let descriptor = self.create_font_descriptor(&font, &font_name)?;

        // Create character encoding
        let encoding = self.create_encoding_for_font(&font, used_glyphs)?;

        // Create Unicode mappings for ToUnicode CMap
        let unicode_mappings = self.create_unicode_mappings(&font, used_glyphs)?;

        // Store embedded font data
        let embedded_font = EmbeddedFontData {
            pdf_name: font_name.clone(),
            font_type: FontType::TrueType,
            descriptor,
            font_program,
            encoding,
            metrics,
            subset_glyphs: if should_subset {
                Some(used_glyphs.clone())
            } else {
                None
            },
            unicode_mappings,
        };

        self.embedded_fonts.insert(font_name.clone(), embedded_font);
        Ok(font_name)
    }

    /// Create a Type0 (CID) font for complex scripts
    pub fn embed_cid_font(
        &mut self,
        font_data: &[u8],
        used_chars: &HashSet<u32>,
        _cmap_name: &str,
        options: &EmbeddingOptions,
    ) -> Result<String> {
        // Parse the font
        let font = TrueTypeFont::from_data(font_data)
            .map_err(|e| PdfError::FontError(format!("Failed to parse font: {e}")))?;

        // Generate unique font name
        let font_name = format!("ABCDEF+CIDFont{next_id}", next_id = self.next_font_id);
        self.next_font_id += 1;

        // Convert character codes to glyph indices
        let used_glyphs = self.chars_to_glyphs(&font, used_chars)?;

        // Create subset if requested
        let font_program = if options.subset {
            font.create_subset(&used_glyphs)
                .map_err(|e| PdfError::FontError(format!("Failed to create subset: {e}")))?
        } else {
            font_data.to_vec()
        };

        // Extract metrics
        let metrics = self.extract_font_metrics(&font)?;

        // Create CID font descriptor
        let descriptor = self.create_cid_font_descriptor(&font, &font_name)?;

        // Create Identity encoding for CID fonts
        let encoding = FontEncoding::Identity;

        // Create Unicode mappings
        let unicode_mappings = self.create_cid_unicode_mappings(&font, used_chars)?;

        let embedded_font = EmbeddedFontData {
            pdf_name: font_name.clone(),
            font_type: FontType::Type0,
            descriptor,
            font_program,
            encoding,
            metrics,
            subset_glyphs: Some(used_glyphs),
            unicode_mappings,
        };

        self.embedded_fonts.insert(font_name.clone(), embedded_font);
        Ok(font_name)
    }

    /// Generate PDF font dictionary for embedded font
    pub fn generate_font_dictionary(&self, font_name: &str) -> Result<Dictionary> {
        let font_data = self
            .embedded_fonts
            .get(font_name)
            .ok_or_else(|| PdfError::FontError(format!("Font {font_name} not found")))?;

        match font_data.font_type {
            FontType::TrueType => self.generate_truetype_dictionary(font_data),
            FontType::Type0 => self.generate_type0_dictionary(font_data),
            // _ => Err(PdfError::FontError("Unsupported font type for embedding".to_string())),
        }
    }

    /// Generate TrueType font dictionary
    fn generate_truetype_dictionary(&self, font_data: &EmbeddedFontData) -> Result<Dictionary> {
        let mut font_dict = Dictionary::new();

        // Basic font properties
        font_dict.set("Type", Object::Name("Font".to_string()));
        font_dict.set("Subtype", Object::Name("TrueType".to_string()));
        font_dict.set("BaseFont", Object::Name(font_data.pdf_name.clone()));

        // Font descriptor reference (would be resolved during PDF generation)
        font_dict.set("FontDescriptor", Object::Reference(ObjectId::new(0, 0))); // Placeholder

        // Encoding
        match &font_data.encoding {
            FontEncoding::WinAnsiEncoding => {
                font_dict.set("Encoding", Object::Name("WinAnsiEncoding".to_string()));
            }
            FontEncoding::MacRomanEncoding => {
                font_dict.set("Encoding", Object::Name("MacRomanEncoding".to_string()));
            }
            FontEncoding::StandardEncoding => {
                font_dict.set("Encoding", Object::Name("StandardEncoding".to_string()));
            }
            FontEncoding::Custom(differences) => {
                let mut encoding_dict = Dictionary::new();
                encoding_dict.set("Type", Object::Name("Encoding".to_string()));
                encoding_dict.set("BaseEncoding", Object::Name("WinAnsiEncoding".to_string()));

                // Add differences array
                let mut diff_array = Vec::new();
                for diff in differences {
                    diff_array.push(Object::Integer(diff.code as i64));
                    for name in &diff.names {
                        diff_array.push(Object::Name(name.clone()));
                    }
                }
                encoding_dict.set("Differences", Object::Array(diff_array));
                font_dict.set("Encoding", Object::Dictionary(encoding_dict));
            }
            _ => {}
        }

        // First and last character codes
        font_dict.set("FirstChar", Object::Integer(32));
        font_dict.set("LastChar", Object::Integer(255));

        // Character widths (simplified - would need actual glyph widths)
        let widths: Vec<Object> = (32..=255)
            .map(|_| Object::Integer(500)) // Default width
            .collect();
        font_dict.set("Widths", Object::Array(widths));

        Ok(font_dict)
    }

    /// Generate Type0 (CID) font dictionary
    fn generate_type0_dictionary(&self, font_data: &EmbeddedFontData) -> Result<Dictionary> {
        let mut font_dict = Dictionary::new();

        // Type0 font properties
        font_dict.set("Type", Object::Name("Font".to_string()));
        font_dict.set("Subtype", Object::Name("Type0".to_string()));
        font_dict.set("BaseFont", Object::Name(font_data.pdf_name.clone()));

        // Encoding (CMap)
        font_dict.set("Encoding", Object::Name("Identity-H".to_string()));

        // DescendantFonts array (would contain CIDFont reference)
        font_dict.set(
            "DescendantFonts",
            Object::Array(vec![
                Object::Reference(ObjectId::new(0, 0)), // Placeholder for CIDFont reference
            ]),
        );

        // ToUnicode CMap reference (if needed)
        if !font_data.unicode_mappings.is_empty() {
            font_dict.set("ToUnicode", Object::Reference(ObjectId::new(0, 0))); // Placeholder
        }

        Ok(font_dict)
    }

    /// Generate font descriptor dictionary
    pub fn generate_font_descriptor(&self, font_name: &str) -> Result<Dictionary> {
        let font_data = self
            .embedded_fonts
            .get(font_name)
            .ok_or_else(|| PdfError::FontError(format!("Font {font_name} not found")))?;

        let mut desc_dict = Dictionary::new();

        desc_dict.set("Type", Object::Name("FontDescriptor".to_string()));
        desc_dict.set("FontName", Object::Name(font_data.pdf_name.clone()));

        // Font flags
        desc_dict.set(
            "Flags",
            Object::Integer(font_data.descriptor.flags.to_flags() as i64),
        );

        // Font metrics
        desc_dict.set("Ascent", Object::Integer(font_data.metrics.ascent as i64));
        desc_dict.set("Descent", Object::Integer(font_data.metrics.descent as i64));
        desc_dict.set(
            "CapHeight",
            Object::Integer(font_data.metrics.cap_height as i64),
        );
        desc_dict.set(
            "ItalicAngle",
            Object::Real(font_data.descriptor.italic_angle),
        );
        desc_dict.set("StemV", Object::Integer(font_data.descriptor.stem_v as i64));

        // Font bounding box
        let bbox = vec![
            Object::Integer(font_data.descriptor.bbox[0] as i64),
            Object::Integer(font_data.descriptor.bbox[1] as i64),
            Object::Integer(font_data.descriptor.bbox[2] as i64),
            Object::Integer(font_data.descriptor.bbox[3] as i64),
        ];
        desc_dict.set("FontBBox", Object::Array(bbox));

        // Font file reference (would be set during PDF generation)
        match font_data.font_type {
            FontType::TrueType => {
                desc_dict.set("FontFile2", Object::Reference(ObjectId::new(0, 0)));
                // Placeholder
            }
            FontType::Type0 => {
                desc_dict.set("FontFile2", Object::Reference(ObjectId::new(0, 0)));
                // Placeholder
            }
        }

        Ok(desc_dict)
    }

    /// Generate ToUnicode CMap stream
    pub fn generate_tounicode_cmap(&self, font_name: &str) -> Result<String> {
        let font_data = self
            .embedded_fonts
            .get(font_name)
            .ok_or_else(|| PdfError::FontError(format!("Font {font_name} not found")))?;

        if font_data.unicode_mappings.is_empty() {
            return Err(PdfError::FontError(
                "No Unicode mappings available".to_string(),
            ));
        }

        let mut cmap_content = String::new();

        // CMap header
        cmap_content.push_str("/CIDInit /ProcSet findresource begin\n");
        cmap_content.push_str("12 dict begin\n");
        cmap_content.push_str("begincmap\n");
        cmap_content.push_str("/CIDSystemInfo\n");
        cmap_content.push_str("<<\n");
        cmap_content.push_str("/Registry (Adobe)\n");
        cmap_content.push_str("/Ordering (UCS)\n");
        cmap_content.push_str("/Supplement 0\n");
        cmap_content.push_str(">> def\n");
        cmap_content.push_str("/CMapName /Adobe-Identity-UCS def\n");
        cmap_content.push_str("/CMapType 2 def\n");
        cmap_content.push_str("1 begincodespacerange\n");
        cmap_content.push_str("<0000> <FFFF>\n");
        cmap_content.push_str("endcodespacerange\n");

        // Unicode mappings
        cmap_content.push_str(&format!(
            "{} beginbfchar\n",
            font_data.unicode_mappings.len()
        ));
        for (glyph_id, unicode_string) in &font_data.unicode_mappings {
            cmap_content.push_str(&format!(
                "<{:04X}> <{}>\n",
                glyph_id,
                unicode_string
                    .chars()
                    .map(|c| format!("{c:04X}", c = c as u32))
                    .collect::<String>()
            ));
        }
        cmap_content.push_str("endbfchar\n");

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

        Ok(cmap_content)
    }

    /// Get all embedded fonts
    pub fn embedded_fonts(&self) -> &HashMap<String, EmbeddedFontData> {
        &self.embedded_fonts
    }

    /// Extract font metrics from TrueType font
    fn extract_font_metrics(&self, _font: &TrueTypeFont) -> Result<FontMetrics> {
        // This would extract actual metrics from font tables
        // For now, return default metrics
        Ok(FontMetrics {
            ascent: 750,
            descent: -250,
            cap_height: 700,
            x_height: 500,
            stem_v: 100,
            stem_h: 50,
            avg_width: 500,
            max_width: 1000,
            missing_width: 500,
        })
    }

    /// Create font descriptor from TrueType font
    fn create_font_descriptor(
        &self,
        _font: &TrueTypeFont,
        font_name: &str,
    ) -> Result<FontDescriptor> {
        Ok(FontDescriptor {
            font_name: font_name.to_string(),
            flags: FontFlags {
                non_symbolic: true,
                ..Default::default()
            },
            bbox: [-100, -250, 1000, 750], // Default bounding box
            italic_angle: 0.0,
            ascent: 750,
            descent: -250,
            cap_height: 700,
            stem_v: 100,
            stem_h: 50,
            avg_width: 500,
            max_width: 1000,
            missing_width: 500,
            font_file: None,
        })
    }

    /// Create CID font descriptor
    fn create_cid_font_descriptor(
        &self,
        font: &TrueTypeFont,
        font_name: &str,
    ) -> Result<FontDescriptor> {
        // Similar to create_font_descriptor but for CID fonts
        self.create_font_descriptor(font, font_name)
    }

    /// Create encoding for font
    fn create_encoding_for_font(
        &self,
        _font: &TrueTypeFont,
        _used_glyphs: &HashSet<u16>,
    ) -> Result<FontEncoding> {
        // For now, return WinAnsi encoding
        // In a full implementation, this would analyze the font and create appropriate encoding
        Ok(FontEncoding::WinAnsiEncoding)
    }

    /// Create Unicode mappings for simple fonts
    fn create_unicode_mappings(
        &self,
        _font: &TrueTypeFont,
        used_glyphs: &HashSet<u16>,
    ) -> Result<HashMap<u16, String>> {
        let mut mappings = HashMap::new();

        // Create basic ASCII mappings
        for glyph_id in used_glyphs {
            if *glyph_id < 256 {
                let unicode_char = char::from(*glyph_id as u8);
                if unicode_char.is_ascii_graphic() || unicode_char == ' ' {
                    mappings.insert(*glyph_id, unicode_char.to_string());
                }
            }
        }

        Ok(mappings)
    }

    /// Create Unicode mappings for CID fonts
    fn create_cid_unicode_mappings(
        &self,
        _font: &TrueTypeFont,
        used_chars: &HashSet<u32>,
    ) -> Result<HashMap<u16, String>> {
        let mut mappings = HashMap::new();

        // Convert character codes to Unicode strings
        for &char_code in used_chars {
            if let Some(unicode_char) = char::from_u32(char_code) {
                // Find glyph ID for this character (simplified)
                let glyph_id = char_code as u16; // Simplified mapping
                mappings.insert(glyph_id, unicode_char.to_string());
            }
        }

        Ok(mappings)
    }

    /// Convert character codes to glyph indices
    fn chars_to_glyphs(&self, _font: &TrueTypeFont, chars: &HashSet<u32>) -> Result<HashSet<u16>> {
        let mut glyphs = HashSet::new();

        // Always include glyph 0 (missing glyph)
        glyphs.insert(0);

        // Convert characters to glyph indices using font's character map
        for &char_code in chars {
            // This is simplified - a real implementation would use the font's cmap table
            let glyph_id = if char_code < 65536 {
                char_code as u16
            } else {
                0 // Missing glyph for characters outside BMP
            };
            glyphs.insert(glyph_id);
        }

        Ok(glyphs)
    }
}

impl Default for FontEmbedder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_font_embedder_creation() {
        let embedder = FontEmbedder::new();
        assert_eq!(embedder.embedded_fonts.len(), 0);
        assert_eq!(embedder.next_font_id, 1);
    }

    #[test]
    fn test_embedding_options_default() {
        let options = EmbeddingOptions::default();
        assert!(options.subset);
        assert_eq!(options.max_subset_size, Some(256));
        assert!(options.compress_font_streams);
        assert!(!options.embed_license_info);
    }

    #[test]
    fn test_generate_tounicode_cmap_empty() {
        let mut embedder = FontEmbedder::new();

        // Create a font with no Unicode mappings
        let font_data = EmbeddedFontData {
            pdf_name: "TestFont".to_string(),
            font_type: FontType::TrueType,
            descriptor: FontDescriptor {
                font_name: "TestFont".to_string(),
                flags: FontFlags::default(),
                bbox: [0, 0, 1000, 1000],
                italic_angle: 0.0,
                ascent: 750,
                descent: -250,
                cap_height: 700,
                stem_v: 100,
                stem_h: 50,
                avg_width: 500,
                max_width: 1000,
                missing_width: 500,
                font_file: None,
            },
            font_program: vec![],
            encoding: FontEncoding::WinAnsiEncoding,
            metrics: FontMetrics {
                ascent: 750,
                descent: -250,
                cap_height: 700,
                x_height: 500,
                stem_v: 100,
                stem_h: 50,
                avg_width: 500,
                max_width: 1000,
                missing_width: 500,
            },
            subset_glyphs: None,
            unicode_mappings: HashMap::new(),
        };

        embedder
            .embedded_fonts
            .insert("TestFont".to_string(), font_data);

        let result = embedder.generate_tounicode_cmap("TestFont");
        assert!(result.is_err());
    }

    #[test]
    fn test_generate_truetype_dictionary() {
        let embedder = FontEmbedder::new();

        let font_data = EmbeddedFontData {
            pdf_name: "TestFont".to_string(),
            font_type: FontType::TrueType,
            descriptor: FontDescriptor {
                font_name: "TestFont".to_string(),
                flags: FontFlags::default(),
                bbox: [0, 0, 1000, 1000],
                italic_angle: 0.0,
                ascent: 750,
                descent: -250,
                cap_height: 700,
                stem_v: 100,
                stem_h: 50,
                avg_width: 500,
                max_width: 1000,
                missing_width: 500,
                font_file: None,
            },
            font_program: vec![],
            encoding: FontEncoding::WinAnsiEncoding,
            metrics: FontMetrics {
                ascent: 750,
                descent: -250,
                cap_height: 700,
                x_height: 500,
                stem_v: 100,
                stem_h: 50,
                avg_width: 500,
                max_width: 1000,
                missing_width: 500,
            },
            subset_glyphs: None,
            unicode_mappings: HashMap::new(),
        };

        let dict = embedder.generate_truetype_dictionary(&font_data).unwrap();

        // Verify basic font properties
        if let Some(Object::Name(font_type)) = dict.get("Type") {
            assert_eq!(font_type, "Font");
        }
        if let Some(Object::Name(subtype)) = dict.get("Subtype") {
            assert_eq!(subtype, "TrueType");
        }
        if let Some(Object::Name(base_font)) = dict.get("BaseFont") {
            assert_eq!(base_font, "TestFont");
        }
    }

    #[test]
    fn test_generate_type0_dictionary() {
        let embedder = FontEmbedder::new();

        let font_data = EmbeddedFontData {
            pdf_name: "TestCIDFont".to_string(),
            font_type: FontType::Type0,
            descriptor: FontDescriptor {
                font_name: "TestCIDFont".to_string(),
                flags: FontFlags::default(),
                bbox: [0, 0, 1000, 1000],
                italic_angle: 0.0,
                ascent: 750,
                descent: -250,
                cap_height: 700,
                stem_v: 100,
                stem_h: 50,
                avg_width: 500,
                max_width: 1000,
                missing_width: 500,
                font_file: None,
            },
            font_program: vec![],
            encoding: FontEncoding::Identity,
            metrics: FontMetrics {
                ascent: 750,
                descent: -250,
                cap_height: 700,
                x_height: 500,
                stem_v: 100,
                stem_h: 50,
                avg_width: 500,
                max_width: 1000,
                missing_width: 500,
            },
            subset_glyphs: None,
            unicode_mappings: HashMap::new(),
        };

        let dict = embedder.generate_type0_dictionary(&font_data).unwrap();

        // Verify Type0 font properties
        if let Some(Object::Name(subtype)) = dict.get("Subtype") {
            assert_eq!(subtype, "Type0");
        }
        if let Some(Object::Name(encoding)) = dict.get("Encoding") {
            assert_eq!(encoding, "Identity-H");
        }
        if let Some(Object::Array(descendant_fonts)) = dict.get("DescendantFonts") {
            assert_eq!(descendant_fonts.len(), 1);
        }
    }

    #[test]
    fn test_chars_to_glyphs_conversion() {
        let _embedder = FontEmbedder::new();
        let _font_data = vec![0; 100]; // Dummy font data

        // This would fail in real implementation due to invalid font data
        // but tests the function structure
        let chars: HashSet<u32> = [65, 66, 67].iter().cloned().collect(); // A, B, C

        // Test would require valid font data to complete
        // For now, test that the function exists and compiles
        assert!(chars.len() == 3);
    }

    #[test]
    fn test_unicode_mappings_creation() {
        let _embedder = FontEmbedder::new();
        let glyphs: HashSet<u16> = [65, 66, 67].iter().cloned().collect();

        // Create dummy font for testing
        let _font_data = vec![0; 100];

        // Test would require valid TrueType font parsing
        // For now, verify function signature
        assert!(glyphs.len() == 3);
    }

    #[test]
    fn test_font_descriptor_generation() {
        let _embedder = FontEmbedder::new();

        let font_data = EmbeddedFontData {
            pdf_name: "TestFont".to_string(),
            font_type: FontType::TrueType,
            descriptor: FontDescriptor {
                font_name: "TestFont".to_string(),
                flags: FontFlags {
                    non_symbolic: true,
                    serif: true,
                    ..Default::default()
                },
                bbox: [-100, -250, 1000, 750],
                italic_angle: 0.0,
                ascent: 750,
                descent: -250,
                cap_height: 700,
                stem_v: 100,
                stem_h: 50,
                avg_width: 500,
                max_width: 1000,
                missing_width: 500,
                font_file: None,
            },
            font_program: vec![],
            encoding: FontEncoding::WinAnsiEncoding,
            metrics: FontMetrics {
                ascent: 750,
                descent: -250,
                cap_height: 700,
                x_height: 500,
                stem_v: 100,
                stem_h: 50,
                avg_width: 500,
                max_width: 1000,
                missing_width: 500,
            },
            subset_glyphs: None,
            unicode_mappings: HashMap::new(),
        };

        let mut embedder_with_font = FontEmbedder::new();
        embedder_with_font
            .embedded_fonts
            .insert("TestFont".to_string(), font_data);

        let desc_dict = embedder_with_font
            .generate_font_descriptor("TestFont")
            .unwrap();

        // Verify font descriptor properties
        if let Some(Object::Name(font_name)) = desc_dict.get("FontName") {
            assert_eq!(font_name, "TestFont");
        }
        if let Some(Object::Integer(flags)) = desc_dict.get("Flags") {
            assert!(*flags > 0); // Should have some flags set
        }
        if let Some(Object::Array(bbox)) = desc_dict.get("FontBBox") {
            assert_eq!(bbox.len(), 4);
        }
    }

    // =========================================================================
    // CIDFontType selection tests (Issue #165)
    // =========================================================================

    #[test]
    fn test_cff_font_uses_cidfonttype0() {
        // CFF/OpenType fonts MUST use CIDFontType0 per ISO 32000-1 §9.7.4
        assert!(
            !CjkFontType::should_use_cidfonttype2(true),
            "CFF → CIDFontType0"
        );
    }

    #[test]
    fn test_truetype_font_uses_cidfonttype2() {
        // TrueType fonts MUST use CIDFontType2 per ISO 32000-1 §9.7.4
        assert!(
            CjkFontType::should_use_cidfonttype2(false),
            "TrueType → CIDFontType2"
        );
    }
}