oxidize-pdf 2.4.3

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
//! Standard 14 PDF fonts required by ISO 32000-1:2008
//!
//! These fonts are built into all PDF viewers and don't require embedding.
//! They are: Helvetica, Times-Roman, Courier (with Bold/Italic/BoldItalic variants),
//! plus Symbol and ZapfDingbats.

use crate::fonts::{FontDescriptor, FontFlags, FontMetrics};

/// Standard 14 PDF fonts as defined in ISO 32000-1:2008
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Standard14Font {
    // Helvetica family
    Helvetica,
    HelveticaBold,
    HelveticaOblique,
    HelveticaBoldOblique,

    // Times family
    TimesRoman,
    TimesBold,
    TimesItalic,
    TimesBoldItalic,

    // Courier family
    Courier,
    CourierBold,
    CourierOblique,
    CourierBoldOblique,

    // Symbol fonts
    Symbol,
    ZapfDingbats,
}

impl Standard14Font {
    /// Get the PostScript name for this font
    pub fn postscript_name(&self) -> &'static str {
        match self {
            Standard14Font::Helvetica => "Helvetica",
            Standard14Font::HelveticaBold => "Helvetica-Bold",
            Standard14Font::HelveticaOblique => "Helvetica-Oblique",
            Standard14Font::HelveticaBoldOblique => "Helvetica-BoldOblique",

            Standard14Font::TimesRoman => "Times-Roman",
            Standard14Font::TimesBold => "Times-Bold",
            Standard14Font::TimesItalic => "Times-Italic",
            Standard14Font::TimesBoldItalic => "Times-BoldItalic",

            Standard14Font::Courier => "Courier",
            Standard14Font::CourierBold => "Courier-Bold",
            Standard14Font::CourierOblique => "Courier-Oblique",
            Standard14Font::CourierBoldOblique => "Courier-BoldOblique",

            Standard14Font::Symbol => "Symbol",
            Standard14Font::ZapfDingbats => "ZapfDingbats",
        }
    }

    /// Get all Standard 14 fonts
    pub fn all() -> &'static [Standard14Font] {
        &[
            Standard14Font::Helvetica,
            Standard14Font::HelveticaBold,
            Standard14Font::HelveticaOblique,
            Standard14Font::HelveticaBoldOblique,
            Standard14Font::TimesRoman,
            Standard14Font::TimesBold,
            Standard14Font::TimesItalic,
            Standard14Font::TimesBoldItalic,
            Standard14Font::Courier,
            Standard14Font::CourierBold,
            Standard14Font::CourierOblique,
            Standard14Font::CourierBoldOblique,
            Standard14Font::Symbol,
            Standard14Font::ZapfDingbats,
        ]
    }

    /// Check if a font name is one of the Standard 14
    pub fn from_name(name: &str) -> Option<Self> {
        match name {
            "Helvetica" => Some(Standard14Font::Helvetica),
            "Helvetica-Bold" => Some(Standard14Font::HelveticaBold),
            "Helvetica-Oblique" => Some(Standard14Font::HelveticaOblique),
            "Helvetica-BoldOblique" => Some(Standard14Font::HelveticaBoldOblique),

            "Times-Roman" | "Times" => Some(Standard14Font::TimesRoman),
            "Times-Bold" => Some(Standard14Font::TimesBold),
            "Times-Italic" => Some(Standard14Font::TimesItalic),
            "Times-BoldItalic" => Some(Standard14Font::TimesBoldItalic),

            "Courier" => Some(Standard14Font::Courier),
            "Courier-Bold" => Some(Standard14Font::CourierBold),
            "Courier-Oblique" => Some(Standard14Font::CourierOblique),
            "Courier-BoldOblique" => Some(Standard14Font::CourierBoldOblique),

            "Symbol" => Some(Standard14Font::Symbol),
            "ZapfDingbats" => Some(Standard14Font::ZapfDingbats),

            _ => None,
        }
    }

    /// Get font metrics for this Standard 14 font
    pub fn metrics(&self) -> FontMetrics {
        match self {
            // Helvetica metrics (proportional sans-serif)
            Standard14Font::Helvetica | Standard14Font::HelveticaOblique => FontMetrics {
                ascent: 718,
                descent: -207,
                line_gap: 0,
                cap_height: 718,
                x_height: 523,
                units_per_em: 1000,
            },

            Standard14Font::HelveticaBold | Standard14Font::HelveticaBoldOblique => FontMetrics {
                ascent: 718,
                descent: -207,
                line_gap: 0,
                cap_height: 718,
                x_height: 532,
                units_per_em: 1000,
            },

            // Times metrics (proportional serif)
            Standard14Font::TimesRoman | Standard14Font::TimesItalic => FontMetrics {
                ascent: 683,
                descent: -217,
                line_gap: 0,
                cap_height: 662,
                x_height: 450,
                units_per_em: 1000,
            },

            Standard14Font::TimesBold | Standard14Font::TimesBoldItalic => FontMetrics {
                ascent: 683,
                descent: -217,
                line_gap: 0,
                cap_height: 676,
                x_height: 461,
                units_per_em: 1000,
            },

            // Courier metrics (monospace)
            Standard14Font::Courier
            | Standard14Font::CourierOblique
            | Standard14Font::CourierBold
            | Standard14Font::CourierBoldOblique => FontMetrics {
                ascent: 629,
                descent: -157,
                line_gap: 0,
                cap_height: 562,
                x_height: 426,
                units_per_em: 1000,
            },

            // Symbol font metrics
            Standard14Font::Symbol => FontMetrics {
                ascent: 1010,
                descent: -293,
                line_gap: 0,
                cap_height: 673,
                x_height: 513,
                units_per_em: 1000,
            },

            // ZapfDingbats font metrics
            Standard14Font::ZapfDingbats => FontMetrics {
                ascent: 820,
                descent: -180,
                line_gap: 0,
                cap_height: 820,
                x_height: 820,
                units_per_em: 1000,
            },
        }
    }

    /// Get font descriptor for this Standard 14 font
    pub fn descriptor(&self) -> FontDescriptor {
        let mut flags = FontFlags::empty();

        // Set flags based on font characteristics
        match self {
            // Sans-serif fonts
            Standard14Font::Helvetica
            | Standard14Font::HelveticaOblique
            | Standard14Font::HelveticaBold
            | Standard14Font::HelveticaBoldOblique => {
                // Sans-serif, not symbolic
            }

            // Serif fonts
            Standard14Font::TimesRoman
            | Standard14Font::TimesItalic
            | Standard14Font::TimesBold
            | Standard14Font::TimesBoldItalic => {
                flags |= FontFlags::SERIF;
            }

            // Monospace fonts
            Standard14Font::Courier
            | Standard14Font::CourierOblique
            | Standard14Font::CourierBold
            | Standard14Font::CourierBoldOblique => {
                flags |= FontFlags::FIXED_PITCH;
            }

            // Symbol fonts
            Standard14Font::Symbol | Standard14Font::ZapfDingbats => {
                flags |= FontFlags::SYMBOLIC;
            }
        }

        // Set style flags
        match self {
            Standard14Font::TimesItalic
            | Standard14Font::TimesBoldItalic
            | Standard14Font::HelveticaOblique
            | Standard14Font::HelveticaBoldOblique
            | Standard14Font::CourierOblique
            | Standard14Font::CourierBoldOblique => {
                flags |= FontFlags::ITALIC;
            }
            _ => {}
        }

        match self {
            Standard14Font::HelveticaBold
            | Standard14Font::HelveticaBoldOblique
            | Standard14Font::TimesBold
            | Standard14Font::TimesBoldItalic
            | Standard14Font::CourierBold
            | Standard14Font::CourierBoldOblique => {
                flags |= FontFlags::FORCE_BOLD;
            }
            _ => {}
        }

        let metrics = self.metrics();
        let font_bbox = self.get_font_bbox();

        FontDescriptor {
            font_name: self.postscript_name().to_string(),
            font_family: self.family_name().to_string(),
            flags,
            font_bbox,
            italic_angle: if flags.contains(FontFlags::ITALIC) {
                -12.0
            } else {
                0.0
            },
            ascent: metrics.ascent as f32,
            descent: metrics.descent as f32,
            cap_height: metrics.cap_height as f32,
            stem_v: self.stem_width(),
            missing_width: 250.0, // Standard missing width
        }
    }

    /// Get the font family name
    pub fn family_name(&self) -> &'static str {
        match self {
            Standard14Font::Helvetica
            | Standard14Font::HelveticaBold
            | Standard14Font::HelveticaOblique
            | Standard14Font::HelveticaBoldOblique => "Helvetica",

            Standard14Font::TimesRoman
            | Standard14Font::TimesBold
            | Standard14Font::TimesItalic
            | Standard14Font::TimesBoldItalic => "Times",

            Standard14Font::Courier
            | Standard14Font::CourierBold
            | Standard14Font::CourierOblique
            | Standard14Font::CourierBoldOblique => "Courier",

            Standard14Font::Symbol => "Symbol",
            Standard14Font::ZapfDingbats => "ZapfDingbats",
        }
    }

    /// Get the stem width for the font (used in font descriptor)
    pub fn stem_width(&self) -> f32 {
        match self {
            Standard14Font::HelveticaBold
            | Standard14Font::HelveticaBoldOblique
            | Standard14Font::TimesBold
            | Standard14Font::TimesBoldItalic
            | Standard14Font::CourierBold
            | Standard14Font::CourierBoldOblique => 118.0,

            _ => 88.0,
        }
    }

    /// Check if this is a symbolic font (Symbol or ZapfDingbats)
    pub fn is_symbolic(&self) -> bool {
        matches!(self, Standard14Font::Symbol | Standard14Font::ZapfDingbats)
    }

    /// Check if this is a monospace font
    pub fn is_monospace(&self) -> bool {
        matches!(
            self,
            Standard14Font::Courier
                | Standard14Font::CourierBold
                | Standard14Font::CourierOblique
                | Standard14Font::CourierBoldOblique
        )
    }

    /// Get font bounding box [llx, lly, urx, ury]
    pub fn get_font_bbox(&self) -> [f32; 4] {
        match self {
            // Helvetica bbox
            Standard14Font::Helvetica | Standard14Font::HelveticaOblique => {
                [-166.0, -225.0, 1000.0, 931.0]
            }
            Standard14Font::HelveticaBold | Standard14Font::HelveticaBoldOblique => {
                [-170.0, -228.0, 1003.0, 962.0]
            }

            // Times bbox
            Standard14Font::TimesRoman | Standard14Font::TimesItalic => {
                [-168.0, -218.0, 1000.0, 898.0]
            }
            Standard14Font::TimesBold | Standard14Font::TimesBoldItalic => {
                [-168.0, -218.0, 1000.0, 935.0]
            }

            // Courier bbox (monospace)
            Standard14Font::Courier
            | Standard14Font::CourierOblique
            | Standard14Font::CourierBold
            | Standard14Font::CourierBoldOblique => [-23.0, -250.0, 715.0, 805.0],

            // Symbol bbox
            Standard14Font::Symbol => [-180.0, -293.0, 1090.0, 1010.0],

            // ZapfDingbats bbox
            Standard14Font::ZapfDingbats => [-1.0, -143.0, 981.0, 820.0],
        }
    }
}

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

    #[test]
    fn test_all_fonts_have_unique_names() {
        let fonts = Standard14Font::all();
        let mut names = std::collections::HashSet::new();

        for font in fonts {
            assert!(names.insert(font.postscript_name()));
        }

        assert_eq!(names.len(), 14);
    }

    #[test]
    fn test_font_name_lookup() {
        assert_eq!(
            Standard14Font::from_name("Helvetica"),
            Some(Standard14Font::Helvetica)
        );
        assert_eq!(
            Standard14Font::from_name("Times-Roman"),
            Some(Standard14Font::TimesRoman)
        );
        assert_eq!(
            Standard14Font::from_name("Times"),
            Some(Standard14Font::TimesRoman)
        );
        assert_eq!(Standard14Font::from_name("NonExistent"), None);
    }

    #[test]
    fn test_font_metrics() {
        let helvetica = Standard14Font::Helvetica;
        let metrics = helvetica.metrics();

        assert!(metrics.ascent > 0);
        assert!(metrics.descent < 0);
        assert!(metrics.units_per_em > 0);
    }

    #[test]
    fn test_font_descriptor() {
        let times_bold = Standard14Font::TimesBold;
        let descriptor = times_bold.descriptor();

        assert_eq!(descriptor.font_name, "Times-Bold");
        assert!(descriptor.flags.contains(FontFlags::SERIF));
        assert!(descriptor.flags.contains(FontFlags::FORCE_BOLD));
    }

    #[test]
    fn test_font_characteristics() {
        assert!(Standard14Font::Symbol.is_symbolic());
        assert!(Standard14Font::ZapfDingbats.is_symbolic());
        assert!(!Standard14Font::Helvetica.is_symbolic());

        assert!(Standard14Font::Courier.is_monospace());
        assert!(!Standard14Font::Helvetica.is_monospace());
    }
}