oxidize-pdf 2.4.2

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
/// PDF font encoding types
///
/// Specifies how text characters are encoded in the PDF document.
/// Different encodings support different character sets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FontEncoding {
    /// WinAnsiEncoding - Windows ANSI encoding (CP1252)
    /// Supports Western European characters, most common for standard fonts
    WinAnsiEncoding,
    /// MacRomanEncoding - Apple Macintosh Roman encoding
    /// Legacy encoding for Macintosh systems
    MacRomanEncoding,
    /// StandardEncoding - Adobe Standard encoding
    /// Basic ASCII plus some additional characters
    StandardEncoding,
    /// MacExpertEncoding - Macintosh Expert encoding
    /// For expert typography with additional symbols
    MacExpertEncoding,
    /// Custom encoding specified by name
    /// Use this for custom or non-standard encodings
    Custom(&'static str),
}

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

    /// Get the recommended encoding for a specific font
    /// Returns None if the font doesn't typically need explicit encoding
    pub fn recommended_for_font(font: &Font) -> Option<Self> {
        match font {
            // Text fonts typically use WinAnsiEncoding for broad compatibility
            Font::Helvetica
            | Font::HelveticaBold
            | Font::HelveticaOblique
            | Font::HelveticaBoldOblique
            | Font::TimesRoman
            | Font::TimesBold
            | Font::TimesItalic
            | Font::TimesBoldItalic
            | Font::Courier
            | Font::CourierBold
            | Font::CourierOblique
            | Font::CourierBoldOblique => Some(FontEncoding::WinAnsiEncoding),
            // Symbol fonts don't use text encodings
            Font::Symbol | Font::ZapfDingbats => None,
            // Custom fonts typically use Identity-H for full Unicode support
            Font::Custom(_) => Some(FontEncoding::Custom("Identity-H")),
        }
    }
}

/// A font with optional encoding specification
///
/// This allows specifying encoding for fonts when needed, while maintaining
/// backward compatibility with the simple Font enum.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FontWithEncoding {
    /// The font to use
    pub font: Font,
    /// Optional encoding specification
    /// If None, no encoding will be set in the PDF (reader's default)
    pub encoding: Option<FontEncoding>,
}

impl FontWithEncoding {
    /// Create a new font with encoding
    pub fn new(font: Font, encoding: Option<FontEncoding>) -> Self {
        Self { font, encoding }
    }

    /// Create a font with recommended encoding
    pub fn with_recommended_encoding(font: Font) -> Self {
        Self {
            font: font.clone(),
            encoding: FontEncoding::recommended_for_font(&font),
        }
    }

    /// Create a font with specific encoding
    pub fn with_encoding(font: Font, encoding: FontEncoding) -> Self {
        Self {
            font,
            encoding: Some(encoding),
        }
    }

    /// Create a font without encoding (reader's default)
    pub fn without_encoding(font: Font) -> Self {
        Self {
            font,
            encoding: None,
        }
    }
}

// Implement From trait for easy conversion
impl From<Font> for FontWithEncoding {
    fn from(font: Font) -> Self {
        Self::without_encoding(font)
    }
}

/// PDF fonts - either standard Type 1 fonts or custom fonts.
///
/// Standard fonts are guaranteed to be available in all PDF readers
/// and don't need to be embedded. Custom fonts must be loaded and embedded.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Font {
    // Standard 14 PDF fonts
    /// Helvetica (sans-serif)
    Helvetica,
    /// Helvetica Bold
    HelveticaBold,
    /// Helvetica Oblique (italic)
    HelveticaOblique,
    /// Helvetica Bold Oblique
    HelveticaBoldOblique,
    /// Times Roman (serif)
    TimesRoman,
    /// Times Bold
    TimesBold,
    /// Times Italic
    TimesItalic,
    /// Times Bold Italic
    TimesBoldItalic,
    /// Courier (monospace)
    Courier,
    /// Courier Bold
    CourierBold,
    /// Courier Oblique
    CourierOblique,
    /// Courier Bold Oblique
    CourierBoldOblique,
    /// Symbol font (mathematical symbols)
    Symbol,
    /// ZapfDingbats (decorative symbols)
    ZapfDingbats,
    /// Custom font loaded from file or bytes
    Custom(String),
}

impl Font {
    /// Get the metrics for this font if it's a standard font
    pub fn get_metrics(&self) -> Option<&'static crate::text::fonts::StandardFontMetrics> {
        crate::text::fonts::get_standard_font_metrics(self)
    }

    /// Get the width of a character in font units (1000 units = 1 em)
    pub fn get_char_width(&self, ch: u8) -> Option<i32> {
        self.get_metrics().map(|m| m.get_char_width(ch))
    }

    /// Get the width of a string in user space units at the given font size
    pub fn get_string_width(&self, text: &str, font_size: f64) -> Option<f64> {
        self.get_metrics().map(|m| {
            let width_units = m.get_string_width(text);
            m.to_user_space(width_units, font_size)
        })
    }

    /// Get the PDF name for this font
    pub fn pdf_name(&self) -> String {
        match self {
            Font::Helvetica => "Helvetica".to_string(),
            Font::HelveticaBold => "Helvetica-Bold".to_string(),
            Font::HelveticaOblique => "Helvetica-Oblique".to_string(),
            Font::HelveticaBoldOblique => "Helvetica-BoldOblique".to_string(),
            Font::TimesRoman => "Times-Roman".to_string(),
            Font::TimesBold => "Times-Bold".to_string(),
            Font::TimesItalic => "Times-Italic".to_string(),
            Font::TimesBoldItalic => "Times-BoldItalic".to_string(),
            Font::Courier => "Courier".to_string(),
            Font::CourierBold => "Courier-Bold".to_string(),
            Font::CourierOblique => "Courier-Oblique".to_string(),
            Font::CourierBoldOblique => "Courier-BoldOblique".to_string(),
            Font::Symbol => "Symbol".to_string(),
            Font::ZapfDingbats => "ZapfDingbats".to_string(),
            Font::Custom(name) => name.clone(),
        }
    }

    /// Check if this font is symbolic (doesn't use text encodings)
    pub fn is_symbolic(&self) -> bool {
        matches!(self, Font::Symbol | Font::ZapfDingbats)
    }

    /// Create this font with a specific encoding
    pub fn with_encoding(self, encoding: FontEncoding) -> FontWithEncoding {
        FontWithEncoding::with_encoding(self, encoding)
    }

    /// Create this font with recommended encoding
    pub fn with_recommended_encoding(self) -> FontWithEncoding {
        FontWithEncoding::with_recommended_encoding(self)
    }

    /// Create this font without explicit encoding
    pub fn without_encoding(self) -> FontWithEncoding {
        FontWithEncoding::without_encoding(self)
    }

    /// Check if this is a custom font
    pub fn is_custom(&self) -> bool {
        matches!(self, Font::Custom(_))
    }

    /// Create a custom font reference
    pub fn custom(name: impl Into<String>) -> Self {
        Font::Custom(name.into())
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FontFamily {
    Helvetica,
    Times,
    Courier,
}

impl FontFamily {
    pub fn regular(self) -> Font {
        match self {
            FontFamily::Helvetica => Font::Helvetica,
            FontFamily::Times => Font::TimesRoman,
            FontFamily::Courier => Font::Courier,
        }
    }

    pub fn bold(self) -> Font {
        match self {
            FontFamily::Helvetica => Font::HelveticaBold,
            FontFamily::Times => Font::TimesBold,
            FontFamily::Courier => Font::CourierBold,
        }
    }

    pub fn italic(self) -> Font {
        match self {
            FontFamily::Helvetica => Font::HelveticaOblique,
            FontFamily::Times => Font::TimesItalic,
            FontFamily::Courier => Font::CourierOblique,
        }
    }

    pub fn bold_italic(self) -> Font {
        match self {
            FontFamily::Helvetica => Font::HelveticaBoldOblique,
            FontFamily::Times => Font::TimesBoldItalic,
            FontFamily::Courier => Font::CourierBoldOblique,
        }
    }
}

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

    #[test]
    fn test_font_pdf_names() {
        assert_eq!(Font::Helvetica.pdf_name(), "Helvetica");
        assert_eq!(Font::HelveticaBold.pdf_name(), "Helvetica-Bold");
        assert_eq!(Font::HelveticaOblique.pdf_name(), "Helvetica-Oblique");
        assert_eq!(
            Font::HelveticaBoldOblique.pdf_name(),
            "Helvetica-BoldOblique"
        );

        assert_eq!(Font::TimesRoman.pdf_name(), "Times-Roman");
        assert_eq!(Font::TimesBold.pdf_name(), "Times-Bold");
        assert_eq!(Font::TimesItalic.pdf_name(), "Times-Italic");
        assert_eq!(Font::TimesBoldItalic.pdf_name(), "Times-BoldItalic");

        assert_eq!(Font::Courier.pdf_name(), "Courier");
        assert_eq!(Font::CourierBold.pdf_name(), "Courier-Bold");
        assert_eq!(Font::CourierOblique.pdf_name(), "Courier-Oblique");
        assert_eq!(Font::CourierBoldOblique.pdf_name(), "Courier-BoldOblique");

        assert_eq!(Font::Symbol.pdf_name(), "Symbol");
        assert_eq!(Font::ZapfDingbats.pdf_name(), "ZapfDingbats");
    }

    #[test]
    fn test_font_is_symbolic() {
        assert!(!Font::Helvetica.is_symbolic());
        assert!(!Font::HelveticaBold.is_symbolic());
        assert!(!Font::TimesRoman.is_symbolic());
        assert!(!Font::Courier.is_symbolic());

        assert!(Font::Symbol.is_symbolic());
        assert!(Font::ZapfDingbats.is_symbolic());
    }

    #[test]
    fn test_font_equality() {
        assert_eq!(Font::Helvetica, Font::Helvetica);
        assert_ne!(Font::Helvetica, Font::HelveticaBold);
        assert_ne!(Font::TimesRoman, Font::TimesBold);
    }

    #[test]
    fn test_font_debug() {
        let font = Font::HelveticaBold;
        let debug_str = format!("{font:?}");
        assert_eq!(debug_str, "HelveticaBold");
    }

    #[test]
    fn test_font_clone() {
        let font1 = Font::TimesItalic;
        let font2 = font1.clone();
        assert_eq!(font1, font2);
    }

    #[test]
    fn test_font_hash() {
        use std::collections::HashSet;

        let mut fonts = HashSet::new();
        fonts.insert(Font::Helvetica);
        fonts.insert(Font::HelveticaBold);
        fonts.insert(Font::Helvetica); // Duplicate

        assert_eq!(fonts.len(), 2);
        assert!(fonts.contains(&Font::Helvetica));
        assert!(fonts.contains(&Font::HelveticaBold));
        assert!(!fonts.contains(&Font::TimesRoman));
    }

    #[test]
    fn test_font_family_regular() {
        assert_eq!(FontFamily::Helvetica.regular(), Font::Helvetica);
        assert_eq!(FontFamily::Times.regular(), Font::TimesRoman);
        assert_eq!(FontFamily::Courier.regular(), Font::Courier);
    }

    #[test]
    fn test_font_family_bold() {
        assert_eq!(FontFamily::Helvetica.bold(), Font::HelveticaBold);
        assert_eq!(FontFamily::Times.bold(), Font::TimesBold);
        assert_eq!(FontFamily::Courier.bold(), Font::CourierBold);
    }

    #[test]
    fn test_font_family_italic() {
        assert_eq!(FontFamily::Helvetica.italic(), Font::HelveticaOblique);
        assert_eq!(FontFamily::Times.italic(), Font::TimesItalic);
        assert_eq!(FontFamily::Courier.italic(), Font::CourierOblique);
    }

    #[test]
    fn test_font_family_bold_italic() {
        assert_eq!(
            FontFamily::Helvetica.bold_italic(),
            Font::HelveticaBoldOblique
        );
        assert_eq!(FontFamily::Times.bold_italic(), Font::TimesBoldItalic);
        assert_eq!(FontFamily::Courier.bold_italic(), Font::CourierBoldOblique);
    }

    #[test]
    fn test_font_family_equality() {
        assert_eq!(FontFamily::Helvetica, FontFamily::Helvetica);
        assert_ne!(FontFamily::Helvetica, FontFamily::Times);
        assert_ne!(FontFamily::Times, FontFamily::Courier);
    }

    #[test]
    fn test_font_family_debug() {
        let family = FontFamily::Times;
        let debug_str = format!("{family:?}");
        assert_eq!(debug_str, "Times");
    }

    #[test]
    fn test_font_family_clone() {
        let family1 = FontFamily::Courier;
        let family2 = family1;
        assert_eq!(family1, family2);
    }

    #[test]
    fn test_font_family_copy() {
        let family1 = FontFamily::Helvetica;
        let family2 = family1; // Copy semantics
        assert_eq!(family1, family2);

        // Both variables should still be usable
        assert_eq!(family1, FontFamily::Helvetica);
        assert_eq!(family2, FontFamily::Helvetica);
    }

    #[test]
    fn test_all_helvetica_variants() {
        let helvetica = FontFamily::Helvetica;

        assert_eq!(helvetica.regular(), Font::Helvetica);
        assert_eq!(helvetica.bold(), Font::HelveticaBold);
        assert_eq!(helvetica.italic(), Font::HelveticaOblique);
        assert_eq!(helvetica.bold_italic(), Font::HelveticaBoldOblique);
    }

    #[test]
    fn test_all_times_variants() {
        let times = FontFamily::Times;

        assert_eq!(times.regular(), Font::TimesRoman);
        assert_eq!(times.bold(), Font::TimesBold);
        assert_eq!(times.italic(), Font::TimesItalic);
        assert_eq!(times.bold_italic(), Font::TimesBoldItalic);
    }

    #[test]
    fn test_all_courier_variants() {
        let courier = FontFamily::Courier;

        assert_eq!(courier.regular(), Font::Courier);
        assert_eq!(courier.bold(), Font::CourierBold);
        assert_eq!(courier.italic(), Font::CourierOblique);
        assert_eq!(courier.bold_italic(), Font::CourierBoldOblique);
    }

    // FontEncoding tests

    #[test]
    fn test_font_encoding_pdf_names() {
        assert_eq!(FontEncoding::WinAnsiEncoding.pdf_name(), "WinAnsiEncoding");
        assert_eq!(
            FontEncoding::MacRomanEncoding.pdf_name(),
            "MacRomanEncoding"
        );
        assert_eq!(
            FontEncoding::StandardEncoding.pdf_name(),
            "StandardEncoding"
        );
        assert_eq!(
            FontEncoding::MacExpertEncoding.pdf_name(),
            "MacExpertEncoding"
        );
        assert_eq!(FontEncoding::Custom("MyEncoding").pdf_name(), "MyEncoding");
    }

    #[test]
    fn test_font_encoding_recommended_for_font() {
        // Text fonts should have recommended encoding
        assert_eq!(
            FontEncoding::recommended_for_font(&Font::Helvetica),
            Some(FontEncoding::WinAnsiEncoding)
        );
        assert_eq!(
            FontEncoding::recommended_for_font(&Font::TimesRoman),
            Some(FontEncoding::WinAnsiEncoding)
        );
        assert_eq!(
            FontEncoding::recommended_for_font(&Font::CourierBold),
            Some(FontEncoding::WinAnsiEncoding)
        );

        // Symbol fonts should not have recommended encoding
        assert_eq!(FontEncoding::recommended_for_font(&Font::Symbol), None);
        assert_eq!(
            FontEncoding::recommended_for_font(&Font::ZapfDingbats),
            None
        );
    }

    #[test]
    fn test_font_encoding_equality() {
        assert_eq!(FontEncoding::WinAnsiEncoding, FontEncoding::WinAnsiEncoding);
        assert_ne!(
            FontEncoding::WinAnsiEncoding,
            FontEncoding::MacRomanEncoding
        );
        assert_eq!(FontEncoding::Custom("Test"), FontEncoding::Custom("Test"));
        assert_ne!(FontEncoding::Custom("Test1"), FontEncoding::Custom("Test2"));
    }

    // FontWithEncoding tests

    #[test]
    fn test_font_with_encoding_new() {
        let font_enc = FontWithEncoding::new(Font::Helvetica, Some(FontEncoding::WinAnsiEncoding));
        assert_eq!(font_enc.font, Font::Helvetica);
        assert_eq!(font_enc.encoding, Some(FontEncoding::WinAnsiEncoding));

        let font_no_enc = FontWithEncoding::new(Font::Symbol, None);
        assert_eq!(font_no_enc.font, Font::Symbol);
        assert_eq!(font_no_enc.encoding, None);
    }

    #[test]
    fn test_font_with_encoding_with_recommended() {
        let helvetica = FontWithEncoding::with_recommended_encoding(Font::Helvetica);
        assert_eq!(helvetica.font, Font::Helvetica);
        assert_eq!(helvetica.encoding, Some(FontEncoding::WinAnsiEncoding));

        let symbol = FontWithEncoding::with_recommended_encoding(Font::Symbol);
        assert_eq!(symbol.font, Font::Symbol);
        assert_eq!(symbol.encoding, None);
    }

    #[test]
    fn test_font_with_encoding_with_specific() {
        let font_enc =
            FontWithEncoding::with_encoding(Font::TimesRoman, FontEncoding::MacRomanEncoding);
        assert_eq!(font_enc.font, Font::TimesRoman);
        assert_eq!(font_enc.encoding, Some(FontEncoding::MacRomanEncoding));
    }

    #[test]
    fn test_font_with_encoding_without_encoding() {
        let font_no_enc = FontWithEncoding::without_encoding(Font::Courier);
        assert_eq!(font_no_enc.font, Font::Courier);
        assert_eq!(font_no_enc.encoding, None);
    }

    #[test]
    fn test_font_with_encoding_from_font() {
        let font_enc: FontWithEncoding = Font::HelveticaBold.into();
        assert_eq!(font_enc.font, Font::HelveticaBold);
        assert_eq!(font_enc.encoding, None);
    }

    #[test]
    fn test_font_convenience_methods() {
        let helvetica_with_enc = Font::Helvetica.with_encoding(FontEncoding::MacRomanEncoding);
        assert_eq!(helvetica_with_enc.font, Font::Helvetica);
        assert_eq!(
            helvetica_with_enc.encoding,
            Some(FontEncoding::MacRomanEncoding)
        );

        let times_recommended = Font::TimesRoman.with_recommended_encoding();
        assert_eq!(times_recommended.font, Font::TimesRoman);
        assert_eq!(
            times_recommended.encoding,
            Some(FontEncoding::WinAnsiEncoding)
        );

        let courier_no_enc = Font::Courier.without_encoding();
        assert_eq!(courier_no_enc.font, Font::Courier);
        assert_eq!(courier_no_enc.encoding, None);
    }

    #[test]
    fn test_font_with_encoding_equality() {
        let font1 = FontWithEncoding::with_encoding(Font::Helvetica, FontEncoding::WinAnsiEncoding);
        let font2 = FontWithEncoding::with_encoding(Font::Helvetica, FontEncoding::WinAnsiEncoding);
        let font3 =
            FontWithEncoding::with_encoding(Font::Helvetica, FontEncoding::MacRomanEncoding);
        let font4 =
            FontWithEncoding::with_encoding(Font::TimesRoman, FontEncoding::WinAnsiEncoding);

        assert_eq!(font1, font2);
        assert_ne!(font1, font3);
        assert_ne!(font1, font4);
    }

    #[test]
    fn test_font_with_encoding_debug() {
        let font_enc =
            FontWithEncoding::with_encoding(Font::Helvetica, FontEncoding::WinAnsiEncoding);
        let debug_str = format!("{font_enc:?}");
        assert!(debug_str.contains("Helvetica"));
        assert!(debug_str.contains("WinAnsiEncoding"));
    }

    #[test]
    fn test_font_with_encoding_clone() {
        let font1 =
            FontWithEncoding::with_encoding(Font::TimesRoman, FontEncoding::StandardEncoding);
        let font2 = font1.clone();
        assert_eq!(font1, font2);
    }

    #[test]
    fn test_font_with_encoding_copy() {
        let font1 = FontWithEncoding::with_encoding(Font::Courier, FontEncoding::WinAnsiEncoding);
        let font2 = font1.clone(); // Clone instead of Copy
        assert_eq!(font1, font2);

        // Both variables should still be usable
        assert_eq!(font1.font, Font::Courier);
        assert_eq!(font2.font, Font::Courier);
    }

    #[test]
    fn test_custom_encoding() {
        let custom_enc = FontEncoding::Custom("MyCustomEncoding");
        assert_eq!(custom_enc.pdf_name(), "MyCustomEncoding");

        let font_with_custom = FontWithEncoding::with_encoding(Font::Helvetica, custom_enc);
        assert_eq!(font_with_custom.encoding, Some(custom_enc));
    }
}