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
//! Font embedding functionality for PDF generation

use super::Font;
use crate::objects::{Dictionary, Object, ObjectId};
use crate::text::fonts::embedding::CjkFontType;
use crate::Result;

/// Font embedding options
#[derive(Debug, Clone)]
pub struct EmbeddingOptions {
    /// Whether to subset the font (only include used glyphs)
    pub subset: bool,
    /// Whether to compress the font data
    pub compress: bool,
    /// Font encoding to use
    pub encoding: FontEncoding,
}

impl Default for EmbeddingOptions {
    fn default() -> Self {
        EmbeddingOptions {
            subset: true,
            compress: true,
            encoding: FontEncoding::WinAnsiEncoding,
        }
    }
}

/// Font encoding options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FontEncoding {
    /// Windows ANSI encoding (CP1252)
    WinAnsiEncoding,
    /// Mac Roman encoding
    MacRomanEncoding,
    /// Standard PDF encoding
    StandardEncoding,
    /// Identity encoding for CID fonts
    IdentityH,
}

impl FontEncoding {
    /// Get the encoding name for PDF
    pub fn name(&self) -> &'static str {
        match self {
            FontEncoding::WinAnsiEncoding => "WinAnsiEncoding",
            FontEncoding::MacRomanEncoding => "MacRomanEncoding",
            FontEncoding::StandardEncoding => "StandardEncoding",
            FontEncoding::IdentityH => "Identity-H",
        }
    }
}

/// Font embedder for creating PDF font objects
pub struct FontEmbedder<'a> {
    font: &'a Font,
    options: EmbeddingOptions,
    used_chars: Vec<char>,
}

impl<'a> FontEmbedder<'a> {
    /// Create a new font embedder
    pub fn new(font: &'a Font, options: EmbeddingOptions) -> Self {
        FontEmbedder {
            font,
            options,
            used_chars: Vec::new(),
        }
    }

    /// Add characters that will be used with this font
    pub fn add_used_chars(&mut self, text: &str) {
        for ch in text.chars() {
            if !self.used_chars.contains(&ch) {
                self.used_chars.push(ch);
            }
        }
    }

    /// Create the font dictionary for embedding
    pub fn create_font_dict(
        &self,
        descriptor_id: ObjectId,
        to_unicode_id: Option<ObjectId>,
    ) -> Dictionary {
        let mut dict = Dictionary::new();

        // Type and Subtype
        dict.set("Type", Object::Name("Font".into()));

        // Determine font type based on encoding
        if self.options.encoding == FontEncoding::IdentityH {
            // Type 0 (composite) font for Unicode support
            self.create_type0_font_dict(&mut dict, descriptor_id, to_unicode_id);
        } else {
            // Type 1 or TrueType font
            self.create_simple_font_dict(&mut dict, descriptor_id, to_unicode_id);
        }

        dict
    }

    /// Create a Type 0 (composite) font dictionary
    fn create_type0_font_dict(
        &self,
        dict: &mut Dictionary,
        descriptor_id: ObjectId,
        to_unicode_id: Option<ObjectId>,
    ) {
        dict.set("Subtype", Object::Name("Type0".into()));
        dict.set("BaseFont", Object::Name(self.font.postscript_name().into()));
        dict.set(
            "Encoding",
            Object::Name(self.options.encoding.name().into()),
        );

        // DescendantFonts array with CIDFont
        let cid_font_dict = self.create_cid_font_dict(descriptor_id);
        dict.set(
            "DescendantFonts",
            Object::Array(vec![Object::Dictionary(cid_font_dict)]),
        );

        if let Some(to_unicode) = to_unicode_id {
            dict.set("ToUnicode", Object::Reference(to_unicode));
        }
    }

    /// Create a CIDFont dictionary
    fn create_cid_font_dict(&self, descriptor_id: ObjectId) -> Dictionary {
        let mut dict = Dictionary::new();

        dict.set("Type", Object::Name("Font".into()));

        let font_name = self.font.postscript_name();
        let is_cff = matches!(self.font.format, super::FontFormat::OpenType);
        let cid_font_subtype = if CjkFontType::should_use_cidfonttype2(is_cff) {
            "CIDFontType2" // TrueType fonts
        } else {
            "CIDFontType0" // CFF/OpenType fonts
        };

        dict.set("Subtype", Object::Name(cid_font_subtype.into()));
        dict.set("BaseFont", Object::Name(font_name.into()));

        // CIDSystemInfo - Use appropriate values for CJK fonts
        let mut cid_system_info = Dictionary::new();
        let font_name = self.font.postscript_name();
        let (registry, ordering, supplement) =
            if let Some(cjk_type) = CjkFontType::detect_from_name(font_name) {
                cjk_type.cid_system_info()
            } else {
                ("Adobe", "Identity", 0)
            };

        cid_system_info.set("Registry", Object::String(registry.into()));
        cid_system_info.set("Ordering", Object::String(ordering.into()));
        cid_system_info.set("Supplement", Object::Integer(supplement as i64));
        dict.set("CIDSystemInfo", Object::Dictionary(cid_system_info));

        dict.set("FontDescriptor", Object::Reference(descriptor_id));

        // Default width
        dict.set("DW", Object::Integer(1000));

        // Width array with actual glyph widths
        let widths_array = self.create_cid_widths_array();
        dict.set("W", Object::Array(widths_array));

        dict
    }

    /// Create a simple font dictionary (Type1/TrueType)
    fn create_simple_font_dict(
        &self,
        dict: &mut Dictionary,
        descriptor_id: ObjectId,
        to_unicode_id: Option<ObjectId>,
    ) {
        dict.set("Subtype", Object::Name("TrueType".into()));
        dict.set("BaseFont", Object::Name(self.font.postscript_name().into()));
        dict.set(
            "Encoding",
            Object::Name(self.options.encoding.name().into()),
        );

        dict.set("FontDescriptor", Object::Reference(descriptor_id));

        // FirstChar and LastChar
        let (first_char, last_char) = self.get_char_range();
        dict.set("FirstChar", Object::Integer(first_char as i64));
        dict.set("LastChar", Object::Integer(last_char as i64));

        // Widths array
        let widths = self.create_widths_array(first_char, last_char);
        dict.set("Widths", Object::Array(widths));

        if let Some(to_unicode) = to_unicode_id {
            dict.set("ToUnicode", Object::Reference(to_unicode));
        }
    }

    /// Get the range of characters used
    fn get_char_range(&self) -> (u8, u8) {
        if self.used_chars.is_empty() {
            return (32, 126); // Default ASCII range
        }

        let mut min = 255;
        let mut max = 0;

        for &ch in &self.used_chars {
            if ch as u32 <= 255 {
                let byte = ch as u8;
                if byte < min {
                    min = byte;
                }
                if byte > max {
                    max = byte;
                }
            }
        }

        (min, max)
    }

    /// Create widths array for the font
    fn create_widths_array(&self, first_char: u8, last_char: u8) -> Vec<Object> {
        let mut widths = Vec::new();

        for ch in first_char..=last_char {
            if let Some(width) = self.font.glyph_mapping.get_char_width(char::from(ch)) {
                // Convert from font units to PDF units (1/1000)
                let pdf_width = (width as f64 * 1000.0) / self.font.metrics.units_per_em as f64;
                widths.push(Object::Integer(pdf_width as i64));
            } else {
                // Default width for missing glyphs
                widths.push(Object::Integer(600));
            }
        }

        widths
    }

    /// Create CID widths array for CID fonts
    fn create_cid_widths_array(&self) -> Vec<Object> {
        let mut width_array = Vec::new();

        // Create a map of character widths
        let mut char_widths = std::collections::HashMap::new();

        // For each used character, get its width
        for &ch in &self.used_chars {
            if let Some(width) = self.font.glyph_mapping.get_char_width(ch) {
                // Convert from font units to PDF units (1/1000)
                let pdf_width = (width as f64 * 1000.0) / self.font.metrics.units_per_em as f64;
                char_widths.insert(ch as u32, pdf_width as i64);
            }
        }

        // Group consecutive characters with same width for efficiency
        let mut sorted_chars: Vec<_> = char_widths.iter().collect();
        sorted_chars.sort_by_key(|(code, _)| *code);

        let mut current_range_start = None;
        let mut current_range_end = None; // Track the end of the current range
        let mut current_width = None;

        for (&code, &width) in sorted_chars {
            match (current_range_start, current_range_end) {
                (None, _) => {
                    // Start a new range
                    current_range_start = Some(code);
                    current_range_end = Some(code);
                    current_width = Some(width);
                }
                (Some(_start), Some(end)) => {
                    // Check if we can continue the current range
                    // (consecutive code AND same width)
                    if current_width == Some(width) && code == end + 1 {
                        // Continue the range
                        current_range_end = Some(code);
                    } else {
                        // End current range and add to array
                        if let (Some(start_code), Some(w)) = (current_range_start, current_width) {
                            width_array.push(Object::Integer(start_code as i64));
                            width_array.push(Object::Array(vec![Object::Integer(w)]));
                        }

                        // Start new range
                        current_range_start = Some(code);
                        current_range_end = Some(code);
                        current_width = Some(width);
                    }
                }
                (Some(_), None) => {
                    // This should never happen (invariant: if start is Some, end must be Some)
                    unreachable!("Range start without range end (this is a bug)")
                }
            }
        }

        // Don't forget the last range
        if let (Some(start_code), Some(w)) = (current_range_start, current_width) {
            width_array.push(Object::Integer(start_code as i64));
            width_array.push(Object::Array(vec![Object::Integer(w)]));
        }

        width_array
    }

    /// Create ToUnicode CMap for text extraction
    pub fn create_to_unicode_cmap(&self) -> Vec<u8> {
        let mut cmap = String::new();

        // CMap header
        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)\n");
        cmap.push_str("   /Ordering (UCS)\n");
        cmap.push_str("   /Supplement 0\n");
        cmap.push_str(">> def\n");
        cmap.push_str("/CMapName /Adobe-Identity-UCS def\n");
        cmap.push_str("/CMapType 2 def\n");
        cmap.push_str("1 begincodespacerange\n");
        cmap.push_str("<0000> <FFFF>\n");
        cmap.push_str("endcodespacerange\n");

        // Character mappings
        let mut mappings = Vec::new();
        for &ch in &self.used_chars {
            if let Some(glyph) = self.font.glyph_mapping.char_to_glyph(ch) {
                mappings.push((glyph, ch));
            }
        }

        if !mappings.is_empty() {
            cmap.push_str(&format!("{} beginbfchar\n", mappings.len()));
            for (glyph, ch) in mappings {
                cmap.push_str(&format!("<{:04X}> <{:04X}>\n", glyph, ch as u32));
            }
            cmap.push_str("endbfchar\n");
        }

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

        cmap.into_bytes()
    }

    /// Get the font data for embedding
    pub fn get_font_data(&self) -> Result<Vec<u8>> {
        if self.options.subset {
            // Basic subsetting: currently returns full font
            // Full TrueType subsetting requires complex table manipulation:
            // - Reordering glyphs in glyf/loca tables
            // - Updating glyph indices in cmap table
            // - Recalculating table checksums
            // - Updating cross-references between tables
            //
            // For now, return the full font data but track used characters
            // for proper width array generation
            if !self.used_chars.is_empty() {
                // Font will be optimized with proper width arrays
                Ok(self.font.data.clone())
            } else {
                // No characters used - return minimal font
                Ok(self.font.data.clone())
            }
        } else {
            Ok(self.font.data.clone())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fonts::{Font, FontDescriptor, FontFormat, FontMetrics, GlyphMapping};

    fn create_test_font() -> Font {
        let mut glyph_mapping = GlyphMapping::default();
        for ch in 32..127 {
            glyph_mapping.add_mapping(char::from(ch), ch as u16);
            glyph_mapping.set_glyph_width(ch as u16, 600);
        }

        Font {
            name: "TestFont".to_string(),
            data: vec![0; 1000],
            format: FontFormat::TrueType,
            metrics: FontMetrics {
                units_per_em: 1000,
                ascent: 800,
                descent: -200,
                line_gap: 200,
                cap_height: 700,
                x_height: 500,
            },
            descriptor: FontDescriptor::new("TestFont"),
            glyph_mapping,
        }
    }

    #[test]
    fn test_font_embedder_creation() {
        let font = create_test_font();
        let options = EmbeddingOptions::default();
        let embedder = FontEmbedder::new(&font, options);

        assert_eq!(embedder.used_chars.len(), 0);
    }

    #[test]
    fn test_add_used_chars() {
        let font = create_test_font();
        let options = EmbeddingOptions::default();
        let mut embedder = FontEmbedder::new(&font, options);

        embedder.add_used_chars("Hello");
        assert_eq!(embedder.used_chars.len(), 4); // H, e, l, o (l appears twice but is deduplicated)

        embedder.add_used_chars("World");
        assert_eq!(embedder.used_chars.len(), 7); // H,e,l,o,W,r,d (o and l overlap between Hello and World)
    }

    #[test]
    fn test_char_range() {
        let font = create_test_font();
        let options = EmbeddingOptions::default();
        let mut embedder = FontEmbedder::new(&font, options);

        embedder.add_used_chars("AZ");
        let (first, last) = embedder.get_char_range();
        assert_eq!(first, b'A');
        assert_eq!(last, b'Z');
    }

    #[test]
    fn test_font_encoding_names() {
        assert_eq!(FontEncoding::WinAnsiEncoding.name(), "WinAnsiEncoding");
        assert_eq!(FontEncoding::MacRomanEncoding.name(), "MacRomanEncoding");
        assert_eq!(FontEncoding::StandardEncoding.name(), "StandardEncoding");
        assert_eq!(FontEncoding::IdentityH.name(), "Identity-H");
    }

    #[test]
    fn test_create_simple_font_dict() {
        let font = create_test_font();
        let options = EmbeddingOptions {
            subset: false,
            compress: false,
            encoding: FontEncoding::WinAnsiEncoding,
        };
        let mut embedder = FontEmbedder::new(&font, options);
        embedder.add_used_chars("ABC");

        let font_dict = embedder.create_font_dict(ObjectId::new(10, 0), Some(ObjectId::new(11, 0)));

        assert_eq!(font_dict.get("Type").unwrap(), &Object::Name("Font".into()));
        assert_eq!(
            font_dict.get("Subtype").unwrap(),
            &Object::Name("TrueType".into())
        );
        assert!(font_dict.get("FirstChar").is_some());
        assert!(font_dict.get("LastChar").is_some());
        assert!(font_dict.get("Widths").is_some());
    }

    #[test]
    fn test_create_type0_font_dict() {
        let font = create_test_font();
        let options = EmbeddingOptions {
            subset: false,
            compress: false,
            encoding: FontEncoding::IdentityH,
        };
        let embedder = FontEmbedder::new(&font, options);

        let font_dict = embedder.create_font_dict(ObjectId::new(10, 0), Some(ObjectId::new(11, 0)));

        assert_eq!(font_dict.get("Type").unwrap(), &Object::Name("Font".into()));
        assert_eq!(
            font_dict.get("Subtype").unwrap(),
            &Object::Name("Type0".into())
        );
        assert_eq!(
            font_dict.get("Encoding").unwrap(),
            &Object::Name("Identity-H".into())
        );
        assert!(font_dict.get("DescendantFonts").is_some());
    }

    #[test]
    fn test_create_widths_array() {
        let font = create_test_font();
        let options = EmbeddingOptions::default();
        let embedder = FontEmbedder::new(&font, options);

        let widths = embedder.create_widths_array(65, 67); // A, B, C
        assert_eq!(widths.len(), 3);
        for width in &widths {
            if let Object::Integer(w) = width {
                assert_eq!(*w, 600); // All test glyphs have width 600
            } else {
                panic!("Expected Integer object");
            }
        }
    }

    #[test]
    fn test_create_to_unicode_cmap() {
        let font = create_test_font();
        let options = EmbeddingOptions::default();
        let mut embedder = FontEmbedder::new(&font, options);
        embedder.add_used_chars("Hello");

        let cmap = embedder.create_to_unicode_cmap();
        let cmap_str = String::from_utf8(cmap).unwrap();

        assert!(cmap_str.contains("begincmap"));
        assert!(cmap_str.contains("endcmap"));
        assert!(cmap_str.contains("beginbfchar"));
        assert!(cmap_str.contains("endbfchar"));
    }

    #[test]
    fn test_get_font_data() {
        let font = create_test_font();
        let options = EmbeddingOptions {
            subset: false,
            compress: false,
            encoding: FontEncoding::WinAnsiEncoding,
        };
        let embedder = FontEmbedder::new(&font, options);

        let font_data = embedder.get_font_data().unwrap();
        assert_eq!(font_data.len(), 1000);
    }

    #[test]
    fn test_embedding_options_default() {
        let options = EmbeddingOptions::default();
        assert!(options.subset);
        assert!(options.compress);
        assert_eq!(options.encoding, FontEncoding::WinAnsiEncoding);
    }

    #[test]
    fn test_char_range_empty() {
        let font = create_test_font();
        let options = EmbeddingOptions::default();
        let embedder = FontEmbedder::new(&font, options);

        let (first, last) = embedder.get_char_range();
        assert_eq!(first, 32); // Default ASCII range
        assert_eq!(last, 126);
    }

    #[test]
    fn test_char_range_with_unicode() {
        let font = create_test_font();
        let options = EmbeddingOptions::default();
        let mut embedder = FontEmbedder::new(&font, options);

        // Add characters including non-ASCII
        embedder.add_used_chars("A€B"); // Euro sign is > 255
        let (first, last) = embedder.get_char_range();

        // Should only consider ASCII characters
        assert_eq!(first, b'A');
        assert_eq!(last, b'B');
    }

    #[test]
    fn test_cid_font_dict_creation() {
        let font = create_test_font();
        let options = EmbeddingOptions {
            subset: false,
            compress: false,
            encoding: FontEncoding::IdentityH,
        };
        let embedder = FontEmbedder::new(&font, options);

        let cid_dict = embedder.create_cid_font_dict(ObjectId::new(10, 0));

        assert_eq!(cid_dict.get("Type").unwrap(), &Object::Name("Font".into()));
        assert_eq!(
            cid_dict.get("Subtype").unwrap(),
            &Object::Name("CIDFontType2".into())
        );
        assert!(cid_dict.get("CIDSystemInfo").is_some());
        assert_eq!(cid_dict.get("DW").unwrap(), &Object::Integer(1000));

        // Check CIDSystemInfo
        if let Object::Dictionary(sys_info) = cid_dict.get("CIDSystemInfo").unwrap() {
            assert_eq!(
                sys_info.get("Registry").unwrap(),
                &Object::String("Adobe".into())
            );
            assert_eq!(
                sys_info.get("Ordering").unwrap(),
                &Object::String("Identity".into())
            );
            assert_eq!(sys_info.get("Supplement").unwrap(), &Object::Integer(0));
        } else {
            panic!("Expected Dictionary for CIDSystemInfo");
        }
    }

    #[test]
    fn test_font_encoding_equality() {
        assert_eq!(FontEncoding::WinAnsiEncoding, FontEncoding::WinAnsiEncoding);
        assert_ne!(
            FontEncoding::WinAnsiEncoding,
            FontEncoding::MacRomanEncoding
        );
        assert_ne!(FontEncoding::StandardEncoding, FontEncoding::IdentityH);
    }

    #[test]
    fn test_add_duplicate_chars() {
        let font = create_test_font();
        let options = EmbeddingOptions::default();
        let mut embedder = FontEmbedder::new(&font, options);

        embedder.add_used_chars("AAA");
        assert_eq!(embedder.used_chars.len(), 1); // Only one 'A' should be stored

        embedder.add_used_chars("ABBA");
        assert_eq!(embedder.used_chars.len(), 2); // 'A' and 'B'
    }

    #[test]
    fn test_widths_array_missing_glyphs() {
        let mut font = create_test_font();
        // Clear all glyph mappings to test missing glyph handling
        font.glyph_mapping = GlyphMapping::default();

        let options = EmbeddingOptions::default();
        let embedder = FontEmbedder::new(&font, options);

        let widths = embedder.create_widths_array(65, 67); // A, B, C
        assert_eq!(widths.len(), 3);

        // Should use default width of 600 for missing glyphs
        for width in &widths {
            if let Object::Integer(w) = width {
                assert_eq!(*w, 600);
            }
        }
    }
}