office2pdf 0.6.3

Convert DOCX, XLSX, and PPTX files to PDF using pure Rust
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
use super::*;

#[test]
fn test_shape_triangle() {
    let shape = make_shape(
        0,
        0,
        2_000_000,
        2_000_000,
        "triangle",
        Some("FF0000"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => {
            assert_eq!(vertices.len(), 3, "Triangle should have 3 vertices");
            assert!((vertices[0].0 - 0.5).abs() < 0.01);
            assert!(vertices[0].1.abs() < 0.01);
        }
        other => panic!("Expected Polygon for triangle, got {other:?}"),
    }
    assert_eq!(shape.fill, Some(Color::new(255, 0, 0)));
}

#[test]
fn test_shape_right_triangle() {
    let shape = make_shape(
        0,
        0,
        2_000_000,
        2_000_000,
        "rtTriangle",
        Some("00FF00"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => {
            assert_eq!(vertices.len(), 3, "Right triangle should have 3 vertices");
            assert!(vertices[0].0.abs() < 0.01);
            assert!(vertices[0].1.abs() < 0.01);
        }
        other => panic!("Expected Polygon for rtTriangle, got {other:?}"),
    }
}

#[test]
fn test_shape_round_rect() {
    let shape = make_shape(
        0,
        0,
        2_000_000,
        1_000_000,
        "roundRect",
        Some("0000FF"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::RoundedRectangle { radius_fraction } => {
            assert!(*radius_fraction > 0.0, "Radius fraction should be positive");
        }
        other => panic!("Expected RoundedRectangle for roundRect, got {other:?}"),
    }
    assert_eq!(shape.fill, Some(Color::new(0, 0, 255)));
}

#[test]
fn test_shape_diamond() {
    let shape = make_shape(
        0,
        0,
        2_000_000,
        2_000_000,
        "diamond",
        Some("FFFF00"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 4),
        other => panic!("Expected Polygon for diamond, got {other:?}"),
    }
}

#[test]
fn test_shape_pentagon() {
    let shape = make_shape(0, 0, 2_000_000, 2_000_000, "pentagon", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 5),
        other => panic!("Expected Polygon for pentagon, got {other:?}"),
    }
}

#[test]
fn test_shape_hexagon() {
    let shape = make_shape(0, 0, 2_000_000, 2_000_000, "hexagon", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 6),
        other => panic!("Expected Polygon for hexagon, got {other:?}"),
    }
}

#[test]
fn test_shape_octagon() {
    let shape = make_shape(0, 0, 2_000_000, 2_000_000, "octagon", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 8),
        other => panic!("Expected Polygon for octagon, got {other:?}"),
    }
}

#[test]
fn test_shape_right_arrow() {
    let shape = make_shape(
        0,
        0,
        3_000_000,
        1_500_000,
        "rightArrow",
        Some("FF8800"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => {
            assert_eq!(vertices.len(), 7);
            let rightmost = vertices
                .iter()
                .map(|vertex| vertex.0)
                .fold(f64::NEG_INFINITY, f64::max);
            assert!((rightmost - 1.0).abs() < 0.01);
        }
        other => panic!("Expected Polygon for rightArrow, got {other:?}"),
    }
    assert_eq!(shape.fill, Some(Color::new(255, 136, 0)));
}

#[test]
fn test_shape_left_arrow() {
    let shape = make_shape(0, 0, 3_000_000, 1_500_000, "leftArrow", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => {
            assert_eq!(vertices.len(), 7);
            let leftmost = vertices
                .iter()
                .map(|vertex| vertex.0)
                .fold(f64::INFINITY, f64::min);
            assert!(leftmost.abs() < 0.01);
        }
        other => panic!("Expected Polygon for leftArrow, got {other:?}"),
    }
}

#[test]
fn test_shape_up_arrow() {
    let shape = make_shape(0, 0, 1_500_000, 3_000_000, "upArrow", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 7),
        other => panic!("Expected Polygon for upArrow, got {other:?}"),
    }
}

#[test]
fn test_shape_down_arrow() {
    let shape = make_shape(0, 0, 1_500_000, 3_000_000, "downArrow", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 7),
        other => panic!("Expected Polygon for downArrow, got {other:?}"),
    }
}

#[test]
fn test_shape_star5() {
    let shape = make_shape(
        0,
        0,
        2_000_000,
        2_000_000,
        "star5",
        Some("FFD700"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 10),
        other => panic!("Expected Polygon for star5, got {other:?}"),
    }
    assert_eq!(shape.fill, Some(Color::new(255, 215, 0)));
}

#[test]
fn test_shape_star4() {
    let shape = make_shape(0, 0, 2_000_000, 2_000_000, "star4", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 8),
        other => panic!("Expected Polygon for star4, got {other:?}"),
    }
}

#[test]
fn test_shape_star6() {
    let shape = make_shape(0, 0, 2_000_000, 2_000_000, "star6", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => assert_eq!(vertices.len(), 12),
        other => panic!("Expected Polygon for star6, got {other:?}"),
    }
}

#[test]
fn test_shape_home_plate() {
    // homePlate: pentagon arrow shape (rect with pointed right edge)
    // Wide shape: cx=1980000 (wider than tall), cy=584391
    let shape = make_shape(
        0,
        0,
        1_980_000,
        584_391,
        "homePlate",
        Some("00259A"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => {
            assert_eq!(vertices.len(), 5, "homePlate should have 5 vertices");
            // First vertex is top-left (0, 0)
            assert!(vertices[0].0.abs() < 0.01);
            assert!(vertices[0].1.abs() < 0.01);
            // Last vertex is bottom-left (0, 1)
            assert!(vertices[4].0.abs() < 0.01);
            assert!((vertices[4].1 - 1.0).abs() < 0.01);
            // Middle vertex is the rightmost point at (1.0, 0.5)
            assert!((vertices[2].0 - 1.0).abs() < 0.01);
            assert!((vertices[2].1 - 0.5).abs() < 0.01);
            // Arrow notch vertices should be between 0 and 1 on x
            assert!(vertices[1].0 > 0.5 && vertices[1].0 < 1.0);
            assert!(vertices[3].0 > 0.5 && vertices[3].0 < 1.0);
        }
        other => panic!("Expected Polygon for homePlate, got {other:?}"),
    }
    assert_eq!(shape.fill, Some(Color::new(0, 37, 154)));
}

#[test]
fn test_shape_home_plate_square() {
    // Square bounding box: the notch should be at x = 0.5
    let shape = make_shape(0, 0, 1_000_000, 1_000_000, "homePlate", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => {
            assert_eq!(vertices.len(), 5);
            // For square with default adj=50000: notch_x = 1.0 - 0.5 = 0.5
            assert!((vertices[1].0 - 0.5).abs() < 0.01);
        }
        other => panic!("Expected Polygon for homePlate square, got {other:?}"),
    }
}

#[test]
fn test_unsupported_preset_falls_back_to_rectangle() {
    let shape = make_shape(
        0,
        0,
        2_000_000,
        2_000_000,
        "cloudCallout",
        Some("AABBCC"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    assert!(matches!(shape.kind, ShapeKind::Rectangle));
}

#[test]
fn test_regular_polygons_fill_their_bounding_box() {
    // Preset geometries span the full shape box (issue #319): an inscribed
    // pentagon leaves ~10% slack at the bottom, printing the shape short.
    for prst in ["pentagon", "hexagon", "octagon"] {
        let kind = prst_to_shape_kind(
            prst,
            100.0,
            100.0,
            false,
            false,
            ArrowHead::None,
            ArrowHead::None,
            &[],
        );
        let ShapeKind::Polygon { vertices } = kind else {
            panic!("{prst} should be a polygon");
        };
        let min_x = vertices.iter().map(|v| v.0).fold(f64::MAX, f64::min);
        let max_x = vertices.iter().map(|v| v.0).fold(f64::MIN, f64::max);
        let min_y = vertices.iter().map(|v| v.1).fold(f64::MAX, f64::min);
        let max_y = vertices.iter().map(|v| v.1).fold(f64::MIN, f64::max);
        assert!(
            min_x.abs() < 1e-9 && (max_x - 1.0).abs() < 1e-9,
            "{prst} x span {min_x}..{max_x}"
        );
        assert!(
            min_y.abs() < 1e-9 && (max_y - 1.0).abs() < 1e-9,
            "{prst} y span {min_y}..{max_y}"
        );
    }
}

#[test]
fn test_shape_chevron() {
    // chevron: arrow band with a pointed right edge and a matching notch cut
    // into the left edge; rendered as a plain rectangle before (issue #358).
    let shape = make_shape(
        0,
        0,
        1_980_000,
        584_391,
        "chevron",
        Some("00259A"),
        None,
        None,
    );
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::Polygon { vertices } => {
            assert_eq!(vertices.len(), 6, "chevron should have 6 vertices");
            // Pointed right edge at (1.0, 0.5)
            assert!((vertices[2].0 - 1.0).abs() < 0.01);
            assert!((vertices[2].1 - 0.5).abs() < 0.01);
            // Notch cut into the left edge, between x=0 and the point
            assert!(vertices[5].0 > 0.0 && vertices[5].0 < 0.5);
            assert!((vertices[5].1 - 0.5).abs() < 0.01);
            // Top-left corner starts at x=0
            assert!(vertices[0].0.abs() < 0.01);
        }
        other => panic!("Expected Polygon for chevron, got {other:?}"),
    }
}

#[test]
fn test_shape_round_rect_honors_adj() {
    // roundRect corner radius comes from the adj value (fraction of the
    // short side, in 100k units); it was hardcoded to 0.1 (issue #361).
    let shape = r##"<p:sp><p:nvSpPr><p:cNvPr id="3" name="Card"/><p:cNvSpPr/><p:nvPr/></p:nvSpPr><p:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="2000000" cy="1000000"/></a:xfrm><a:prstGeom prst="roundRect"><a:avLst><a:gd name="adj" fmla="val 3333"/></a:avLst></a:prstGeom><a:solidFill><a:srgbClr val="EEF2FA"/></a:solidFill></p:spPr></p:sp>"##.to_string();
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::RoundedRectangle { radius_fraction } => {
            assert!(
                (radius_fraction - 0.03333).abs() < 0.001,
                "adj 3333 must give a 3.3% radius, got {radius_fraction}"
            );
        }
        other => panic!("Expected RoundedRectangle, got {other:?}"),
    }
}

#[test]
fn test_shape_round_rect_default_adj() {
    // Without avLst, the OOXML default adj is 16667 (1/6 of the short side).
    let shape = make_shape(0, 0, 2_000_000, 1_000_000, "roundRect", None, None, None);
    let slide = make_slide_xml(&[shape]);
    let data = build_test_pptx(SLIDE_CX, SLIDE_CY, &[slide]);
    let parser = PptxParser;
    let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

    let page = first_fixed_page(&doc);
    let shape = get_shape(&page.elements[0]);
    match &shape.kind {
        ShapeKind::RoundedRectangle { radius_fraction } => {
            assert!(
                (radius_fraction - 0.16667).abs() < 0.001,
                "default adj must give a 16.7% radius, got {radius_fraction}"
            );
        }
        other => panic!("Expected RoundedRectangle, got {other:?}"),
    }
}