ppt-rs 0.2.37

Create, read, and update PowerPoint 2007+ (.pptx) files with rich formatting, bullet styles, themes, and templates.
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
//! Embedded font support for PPTX output
//!
//! Allows embedding font files into the presentation so they render correctly
//! on systems that don't have the fonts installed. Generates proper
//! `<p:embeddedFontLst>` XML in presentation.xml.

/// Font style variant
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
pub enum FontStyle {
    Regular,
    Bold,
    Italic,
    BoldItalic,
}

impl FontStyle {
    pub fn xml_element(&self) -> &'static str {
        match self {
            FontStyle::Regular => "regular",
            FontStyle::Bold => "bold",
            FontStyle::Italic => "italic",
            FontStyle::BoldItalic => "boldItalic",
        }
    }

    pub fn is_bold(&self) -> bool {
        matches!(self, FontStyle::Bold | FontStyle::BoldItalic)
    }

    pub fn is_italic(&self) -> bool {
        matches!(self, FontStyle::Italic | FontStyle::BoldItalic)
    }
}

/// Character set for the font
#[derive(Clone, Debug, Copy, PartialEq, Eq, Default)]
pub enum FontCharset {
    #[default]
    Ansi,
    Symbol,
    ShiftJis,
    Hangul,
    Gb2312,
    ChineseBig5,
    Greek,
    Turkish,
    Hebrew,
    Arabic,
    Baltic,
    Russian,
    Thai,
    EastEurope,
}

impl FontCharset {
    pub fn code(&self) -> u8 {
        match self {
            FontCharset::Ansi => 0x00,
            FontCharset::Symbol => 0x02,
            FontCharset::ShiftJis => 0x80,
            FontCharset::Hangul => 0x81,
            FontCharset::Gb2312 => 0x86,
            FontCharset::ChineseBig5 => 0x88,
            FontCharset::Greek => 0xA1,
            FontCharset::Turkish => 0xA2,
            FontCharset::Hebrew => 0xB1,
            FontCharset::Arabic => 0xB2,
            FontCharset::Baltic => 0xBA,
            FontCharset::Russian => 0xCC,
            FontCharset::Thai => 0xDE,
            FontCharset::EastEurope => 0xEE,
        }
    }
}

/// A single embedded font entry
#[derive(Clone, Debug)]
pub struct EmbeddedFont {
    pub typeface: String,
    pub style: FontStyle,
    pub charset: FontCharset,
    pub panose: Option<String>,
    pub pitch_family: u8,
    pub data: Vec<u8>,
    pub relationship_id: String,
}

impl EmbeddedFont {
    /// Create a new embedded font entry
    pub fn new(typeface: &str, style: FontStyle, data: Vec<u8>, rel_id: &str) -> Self {
        Self {
            typeface: typeface.to_string(),
            style,
            charset: FontCharset::default(),
            panose: None,
            pitch_family: 0x22, // Variable pitch, Roman family
            data,
            relationship_id: rel_id.to_string(),
        }
    }

    pub fn charset(mut self, charset: FontCharset) -> Self {
        self.charset = charset;
        self
    }

    pub fn panose(mut self, panose: &str) -> Self {
        self.panose = Some(panose.to_string());
        self
    }

    pub fn pitch_family(mut self, pf: u8) -> Self {
        self.pitch_family = pf;
        self
    }

    /// Font data size in bytes
    pub fn data_size(&self) -> usize {
        self.data.len()
    }

    /// Content type for the embedded font part
    pub fn content_type() -> &'static str {
        "application/x-fontdata"
    }

    /// Part name in the ZIP archive
    pub fn part_name(&self) -> String {
        format!(
            "ppt/fonts/{}-{}.fntdata",
            self.typeface.replace(' ', ""),
            self.style.xml_element()
        )
    }

    /// Relationship target relative to `ppt/`.
    pub fn rel_target(&self) -> String {
        format!(
            "fonts/{}-{}.fntdata",
            self.typeface.replace(' ', ""),
            self.style.xml_element()
        )
    }
}

/// Manages all embedded fonts for a presentation
#[derive(Clone, Debug, Default)]
pub struct EmbeddedFontList {
    fonts: Vec<EmbeddedFont>,
}

impl EmbeddedFontList {
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an embedded font
    pub fn add(&mut self, font: EmbeddedFont) {
        self.fonts.push(font);
    }

    /// Get all fonts
    pub fn fonts(&self) -> &[EmbeddedFont] {
        &self.fonts
    }

    /// Number of embedded fonts
    pub fn len(&self) -> usize {
        self.fonts.len()
    }

    pub fn is_empty(&self) -> bool {
        self.fonts.is_empty()
    }

    /// Assign sequential `rIdN` relationship IDs to fonts that don't already
    /// have one. `start` is the first relationship number to use.
    pub fn assign_relationship_ids(&mut self, start: usize) {
        for (i, font) in self.fonts.iter_mut().enumerate() {
            if font.relationship_id.is_empty() {
                font.relationship_id = format!("rId{}", start + i);
            }
        }
    }

    /// Total size of all font data
    pub fn total_size(&self) -> usize {
        self.fonts.iter().map(|f| f.data_size()).sum()
    }

    /// Find fonts by typeface name
    pub fn find_by_typeface(&self, typeface: &str) -> Vec<&EmbeddedFont> {
        self.fonts
            .iter()
            .filter(|f| f.typeface == typeface)
            .collect()
    }

    /// Generate `<p:embeddedFontLst>` XML for presentation.xml
    pub fn to_xml(&self) -> String {
        if self.fonts.is_empty() {
            return String::new();
        }

        let mut xml = String::from("<p:embeddedFontLst>");

        // Group by typeface
        let mut seen_typefaces: Vec<String> = Vec::new();
        for font in &self.fonts {
            if !seen_typefaces.contains(&font.typeface) {
                seen_typefaces.push(font.typeface.clone());
            }
        }

        for typeface in &seen_typefaces {
            let variants: Vec<&EmbeddedFont> = self
                .fonts
                .iter()
                .filter(|f| &f.typeface == typeface)
                .collect();

            xml.push_str("<p:embeddedFont>");

            // Font descriptor (from first variant)
            if let Some(first) = variants.first() {
                let panose_attr = first
                    .panose
                    .as_ref()
                    .map(|p| format!(r#" panose="{}""#, p))
                    .unwrap_or_default();
                xml.push_str(&format!(
                    r#"<p:font typeface="{}" charset="{}" pitchFamily="{}"{}/>"#,
                    xml_escape(typeface),
                    first.charset.code(),
                    first.pitch_family,
                    panose_attr,
                ));
            }

            // Font data references per style
            for font in &variants {
                xml.push_str(&format!(
                    r#"<p:{} r:id="{}"/>"#,
                    font.style.xml_element(),
                    font.relationship_id,
                ));
            }

            xml.push_str("</p:embeddedFont>");
        }

        xml.push_str("</p:embeddedFontLst>");
        xml
    }
}

fn xml_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

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

    #[test]
    fn test_font_style_xml() {
        assert_eq!(FontStyle::Regular.xml_element(), "regular");
        assert_eq!(FontStyle::Bold.xml_element(), "bold");
        assert_eq!(FontStyle::Italic.xml_element(), "italic");
        assert_eq!(FontStyle::BoldItalic.xml_element(), "boldItalic");
    }

    #[test]
    fn test_font_style_flags() {
        assert!(!FontStyle::Regular.is_bold());
        assert!(!FontStyle::Regular.is_italic());
        assert!(FontStyle::Bold.is_bold());
        assert!(!FontStyle::Bold.is_italic());
        assert!(!FontStyle::Italic.is_bold());
        assert!(FontStyle::Italic.is_italic());
        assert!(FontStyle::BoldItalic.is_bold());
        assert!(FontStyle::BoldItalic.is_italic());
    }

    #[test]
    fn test_font_charset_default() {
        assert_eq!(FontCharset::default(), FontCharset::Ansi);
        assert_eq!(FontCharset::Ansi.code(), 0x00);
    }

    #[test]
    fn test_font_charset_codes() {
        assert_eq!(FontCharset::Symbol.code(), 0x02);
        assert_eq!(FontCharset::ShiftJis.code(), 0x80);
        assert_eq!(FontCharset::Arabic.code(), 0xB2);
        assert_eq!(FontCharset::Russian.code(), 0xCC);
    }

    #[test]
    fn test_embedded_font_new() {
        let font = EmbeddedFont::new("Arial", FontStyle::Regular, vec![1, 2, 3], "rId10");
        assert_eq!(font.typeface, "Arial");
        assert_eq!(font.style, FontStyle::Regular);
        assert_eq!(font.data_size(), 3);
        assert_eq!(font.relationship_id, "rId10");
    }

    #[test]
    fn test_embedded_font_builder() {
        let font = EmbeddedFont::new("Calibri", FontStyle::Bold, vec![0; 100], "rId1")
            .charset(FontCharset::Russian)
            .panose("020F0502020204030204")
            .pitch_family(0x34);
        assert_eq!(font.charset, FontCharset::Russian);
        assert_eq!(font.panose.as_deref(), Some("020F0502020204030204"));
        assert_eq!(font.pitch_family, 0x34);
    }

    #[test]
    fn test_embedded_font_part_name() {
        let font = EmbeddedFont::new("Times New Roman", FontStyle::BoldItalic, vec![], "rId1");
        assert_eq!(
            font.part_name(),
            "ppt/fonts/TimesNewRoman-boldItalic.fntdata"
        );
    }

    #[test]
    fn test_embedded_font_content_type() {
        assert_eq!(EmbeddedFont::content_type(), "application/x-fontdata");
    }

    #[test]
    fn test_embedded_font_list_new() {
        let list = EmbeddedFontList::new();
        assert!(list.is_empty());
        assert_eq!(list.len(), 0);
        assert_eq!(list.total_size(), 0);
    }

    #[test]
    fn test_embedded_font_list_add() {
        let mut list = EmbeddedFontList::new();
        list.add(EmbeddedFont::new(
            "Arial",
            FontStyle::Regular,
            vec![0; 50],
            "rId1",
        ));
        list.add(EmbeddedFont::new(
            "Arial",
            FontStyle::Bold,
            vec![0; 60],
            "rId2",
        ));
        assert_eq!(list.len(), 2);
        assert_eq!(list.total_size(), 110);
    }

    #[test]
    fn test_embedded_font_list_find() {
        let mut list = EmbeddedFontList::new();
        list.add(EmbeddedFont::new(
            "Arial",
            FontStyle::Regular,
            vec![],
            "rId1",
        ));
        list.add(EmbeddedFont::new(
            "Calibri",
            FontStyle::Regular,
            vec![],
            "rId2",
        ));
        list.add(EmbeddedFont::new("Arial", FontStyle::Bold, vec![], "rId3"));
        assert_eq!(list.find_by_typeface("Arial").len(), 2);
        assert_eq!(list.find_by_typeface("Calibri").len(), 1);
        assert_eq!(list.find_by_typeface("Missing").len(), 0);
    }

    #[test]
    fn test_embedded_font_list_xml_empty() {
        let list = EmbeddedFontList::new();
        assert_eq!(list.to_xml(), "");
    }

    #[test]
    fn test_embedded_font_list_xml() {
        let mut list = EmbeddedFontList::new();
        list.add(EmbeddedFont::new(
            "Arial",
            FontStyle::Regular,
            vec![0; 10],
            "rId10",
        ));
        list.add(EmbeddedFont::new(
            "Arial",
            FontStyle::Bold,
            vec![0; 10],
            "rId11",
        ));
        let xml = list.to_xml();
        assert!(xml.contains("<p:embeddedFontLst>"));
        assert!(xml.contains("</p:embeddedFontLst>"));
        assert!(xml.contains(r#"typeface="Arial""#));
        assert!(xml.contains(r#"r:id="rId10""#));
        assert!(xml.contains(r#"r:id="rId11""#));
        assert!(xml.contains("<p:regular"));
        assert!(xml.contains("<p:bold"));
        // Should be grouped under one <p:embeddedFont>
        assert_eq!(xml.matches("<p:embeddedFont>").count(), 1);
    }

    #[test]
    fn test_embedded_font_list_xml_multiple_typefaces() {
        let mut list = EmbeddedFontList::new();
        list.add(EmbeddedFont::new(
            "Arial",
            FontStyle::Regular,
            vec![],
            "rId1",
        ));
        list.add(EmbeddedFont::new(
            "Calibri",
            FontStyle::Regular,
            vec![],
            "rId2",
        ));
        let xml = list.to_xml();
        assert_eq!(xml.matches("<p:embeddedFont>").count(), 2);
        assert!(xml.contains("Arial"));
        assert!(xml.contains("Calibri"));
    }

    #[test]
    fn test_embedded_font_list_xml_with_panose() {
        let mut list = EmbeddedFontList::new();
        list.add(
            EmbeddedFont::new("Calibri", FontStyle::Regular, vec![], "rId1")
                .panose("020F0502020204030204"),
        );
        let xml = list.to_xml();
        assert!(xml.contains(r#"panose="020F0502020204030204""#));
    }
}