office2pdf 0.5.0

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
use super::*;

// --- substitutes() tests ---

#[test]
fn test_calibri_substitutes() {
    let subs = substitutes("Calibri").expect("Calibri should have substitutes");
    assert!(subs.contains(&"Carlito"), "Calibri should map to Carlito");
    assert!(
        subs.contains(&"Liberation Sans"),
        "Calibri should have Liberation Sans as fallback"
    );
    assert_eq!(subs[0], "Carlito", "Carlito should be first preference");
}

#[test]
fn test_cambria_substitutes() {
    let subs = substitutes("Cambria").expect("Cambria should have substitutes");
    assert!(subs.contains(&"Caladea"));
    assert!(subs.contains(&"Liberation Serif"));
}

#[test]
fn test_arial_substitutes() {
    let subs = substitutes("Arial").expect("Arial should have substitutes");
    assert!(subs.contains(&"Liberation Sans"));
    assert!(subs.contains(&"Arimo"));
}

#[test]
fn test_times_new_roman_substitutes() {
    let subs = substitutes("Times New Roman").expect("TNR should have substitutes");
    assert!(subs.contains(&"Liberation Serif"));
    assert!(subs.contains(&"Tinos"));
}

#[test]
fn test_courier_new_substitutes() {
    let subs = substitutes("Courier New").expect("Courier New should have substitutes");
    assert!(subs.contains(&"Liberation Mono"));
    assert!(subs.contains(&"Cousine"));
}

#[test]
fn test_comic_sans_substitutes() {
    let subs = substitutes("Comic Sans MS").expect("Comic Sans MS should have substitutes");
    assert!(subs.contains(&"Comic Neue"));
}

#[test]
fn test_verdana_substitutes() {
    let subs = substitutes("Verdana").expect("Verdana should have substitutes");
    assert!(subs.contains(&"DejaVu Sans"));
}

#[test]
fn test_georgia_substitutes() {
    let subs = substitutes("Georgia").expect("Georgia should have substitutes");
    assert!(subs.contains(&"DejaVu Serif"));
}

#[test]
fn test_unknown_font_returns_none() {
    assert!(
        substitutes("Papyrus").is_none(),
        "Unknown fonts should return None"
    );
    assert!(substitutes("Helvetica").is_none());
    assert!(substitutes("").is_none());
}

#[test]
fn test_case_insensitive_lookup() {
    assert!(substitutes("calibri").is_some(), "lowercase should match");
    assert!(substitutes("CALIBRI").is_some(), "uppercase should match");
    assert!(substitutes("Calibri").is_some(), "title case should match");
    assert!(substitutes("cAlIbRi").is_some(), "mixed case should match");
    assert!(
        substitutes("times new roman").is_some(),
        "lowercase multi-word should match"
    );
    assert!(
        substitutes("TIMES NEW ROMAN").is_some(),
        "uppercase multi-word should match"
    );
}

#[test]
fn test_at_least_8_fonts_mapped() {
    let known_fonts = [
        "Calibri",
        "Cambria",
        "Arial",
        "Times New Roman",
        "Courier New",
        "Comic Sans MS",
        "Verdana",
        "Georgia",
    ];
    let mut mapped = 0;
    for font in &known_fonts {
        if substitutes(font).is_some() {
            mapped += 1;
        }
    }
    assert!(
        mapped >= 8,
        "At least 8 common Microsoft fonts should be mapped, got {mapped}"
    );
}

#[test]
fn test_consolas_substitutes() {
    let subs = substitutes("Consolas").expect("Consolas should have substitutes");
    assert!(subs.contains(&"Inconsolata"));
}

#[test]
fn test_trebuchet_ms_substitutes() {
    let subs = substitutes("Trebuchet MS").expect("Trebuchet MS should have substitutes");
    assert!(subs.contains(&"Ubuntu"));
}

#[test]
fn test_impact_substitutes() {
    let subs = substitutes("Impact").expect("Impact should have substitutes");
    assert!(subs.contains(&"Oswald"));
}

#[test]
fn test_raleway_substitutes() {
    let subs = substitutes("Raleway").expect("Raleway should have substitutes");
    assert!(subs.contains(&"Helvetica"));
    assert!(subs.contains(&"Arial"));
    assert!(subs.contains(&"Arial Unicode MS"));
    assert!(subs.contains(&"Apple SD Gothic Neo"));
    assert_eq!(subs[0], "Helvetica");
}

#[test]
fn test_lato_substitutes() {
    let subs = substitutes("Lato").expect("Lato should have substitutes");
    assert!(subs.contains(&"Helvetica"));
    assert!(subs.contains(&"Arial"));
    assert!(subs.contains(&"Arial Unicode MS"));
    assert!(subs.contains(&"Apple SD Gothic Neo"));
}

#[test]
fn test_pretendard_substitutes() {
    let subs = substitutes("Pretendard").expect("Pretendard should have substitutes");
    assert_eq!(subs[0], "Apple SD Gothic Neo");
    assert!(subs.contains(&"Noto Sans CJK KR"));
    assert!(subs.contains(&"Malgun Gothic"));
}

// --- font_with_fallbacks() tests ---

#[test]
fn test_font_with_fallbacks_known_font() {
    let result = font_with_fallbacks("Calibri");
    assert_eq!(
        result, r#"("Calibri", "Carlito", "Liberation Sans")"#,
        "Known font should produce Typst array with original + substitutes"
    );
}

#[test]
fn test_font_with_fallbacks_unknown_font() {
    let result = font_with_fallbacks("Helvetica");
    assert_eq!(
        result, "\"Helvetica\"",
        "Unknown font should produce simple quoted string"
    );
}

#[test]
fn test_font_with_fallbacks_single_substitute() {
    let result = font_with_fallbacks("Comic Sans MS");
    assert_eq!(result, r#"("Comic Sans MS", "Comic Neue")"#);
}

#[test]
fn test_font_with_fallbacks_preserves_original_case() {
    // The original font name should appear as-is (not lowercased)
    let result = font_with_fallbacks("CALIBRI");
    assert!(
        result.starts_with("(\"CALIBRI\""),
        "Original case should be preserved: {result}"
    );
}

#[test]
fn test_font_with_fallbacks_pretendard_variant_includes_base_family() {
    let result = font_with_fallbacks("Pretendard SemiBold");
    assert!(
        result.contains("\"Pretendard\""),
        "Pretendard variants should fall back to the base family: {result}"
    );
    assert!(
        result.contains("\"Apple SD Gothic Neo\""),
        "Pretendard variants should include Korean-capable fallbacks: {result}"
    );
}

#[test]
fn test_resolve_available_fallback_prefers_alias_before_system_fallback() {
    let context =
        FontSearchContext::for_test(Vec::new(), &["Pretendard", "Apple SD Gothic Neo"], &[], &[]);
    let fallback = resolve_available_fallback("Pretendard Medium", &context);
    assert_eq!(fallback.as_deref(), Some("Pretendard"));
}

#[test]
fn test_font_with_fallbacks_prefers_office_source_rank_over_static_substitute_order() {
    let context = FontSearchContext::for_test(
        Vec::new(),
        &["Apple SD Gothic Neo", "Malgun Gothic"],
        &["Malgun Gothic"],
        &[],
    );
    let result = with_font_search_context(Some(&context), || font_with_fallbacks("Pretendard"));
    let apple_index = result
        .find("\"Apple SD Gothic Neo\"")
        .expect("Apple SD Gothic Neo should appear in fallback list");
    let malgun_index = result
        .find("\"Malgun Gothic\"")
        .expect("Malgun Gothic should appear in fallback list");
    assert!(
        malgun_index < apple_index,
        "office-resolved font should outrank static substitute order: {result}"
    );
}

#[test]
fn test_detect_missing_font_fallbacks_with_context_prefers_office_font() {
    let context = FontSearchContext::for_test(
        Vec::new(),
        &["Malgun Gothic", "Apple SD Gothic Neo"],
        &["Malgun Gothic"],
        &[],
    );
    let doc = Document {
        metadata: crate::ir::Metadata::default(),
        pages: vec![Page::Flow(crate::ir::FlowPage {
            size: crate::ir::PageSize::default(),
            margins: crate::ir::Margins::default(),
            content: vec![Block::Paragraph(Paragraph {
                style: crate::ir::ParagraphStyle::default(),
                runs: vec![crate::ir::Run {
                    text: "Title".to_string(),
                    style: crate::ir::TextStyle {
                        font_family: Some("Pretendard Medium".to_string()),
                        ..crate::ir::TextStyle::default()
                    },
                    href: None,
                    footnote: None,
                }],
            })],
            header: None,
            footer: None,
            columns: None,
        })],
        styles: crate::ir::StyleSheet::default(),
    };

    let fallbacks = detect_missing_font_fallbacks_with_context(&doc, &context);
    assert_eq!(
        fallbacks,
        vec![("Pretendard Medium".to_string(), "Malgun Gothic".to_string())]
    );
}

#[test]
fn test_document_requests_font_families_false_when_all_runs_use_defaults() {
    let doc = Document {
        metadata: crate::ir::Metadata::default(),
        pages: vec![Page::Flow(crate::ir::FlowPage {
            size: crate::ir::PageSize::default(),
            margins: crate::ir::Margins::default(),
            content: vec![Block::Paragraph(Paragraph {
                style: crate::ir::ParagraphStyle::default(),
                runs: vec![crate::ir::Run {
                    text: "Plain text".to_string(),
                    style: crate::ir::TextStyle::default(),
                    href: None,
                    footnote: None,
                }],
            })],
            header: None,
            footer: None,
            columns: None,
        })],
        styles: crate::ir::StyleSheet::default(),
    };

    assert!(!document_requests_font_families(&doc));
}

#[test]
fn test_document_requests_font_families_true_when_any_run_sets_family() {
    let doc = Document {
        metadata: crate::ir::Metadata::default(),
        pages: vec![Page::Flow(crate::ir::FlowPage {
            size: crate::ir::PageSize::default(),
            margins: crate::ir::Margins::default(),
            content: vec![Block::Paragraph(Paragraph {
                style: crate::ir::ParagraphStyle::default(),
                runs: vec![crate::ir::Run {
                    text: "Styled text".to_string(),
                    style: crate::ir::TextStyle {
                        font_family: Some("Pretendard".to_string()),
                        ..crate::ir::TextStyle::default()
                    },
                    href: None,
                    footnote: None,
                }],
            })],
            header: None,
            footer: None,
            columns: None,
        })],
        styles: crate::ir::StyleSheet::default(),
    };

    assert!(document_requests_font_families(&doc));
}

// --- Korean / CJK font name tests ---

#[test]
fn test_korean_malgun_gothic_name_has_substitutes() {
    let subs = substitutes("맑은 고딕").expect("Korean Malgun Gothic name should have substitutes");
    assert!(
        subs.contains(&"Malgun Gothic"),
        "Should include English name as fallback: {subs:?}"
    );
}

#[test]
fn test_korean_gulim_name_has_substitutes() {
    let subs = substitutes("굴림").expect("Korean Gulim name should have substitutes");
    assert!(subs.contains(&"Gulim"));
}

#[test]
fn test_font_with_fallbacks_korean_malgun_gothic_includes_english_name() {
    let result = font_with_fallbacks("맑은 고딕");
    assert!(
        result.contains("\"Malgun Gothic\""),
        "Should include English name in fallback list: {result}"
    );
    assert!(
        result.starts_with("(\"맑은 고딕\""),
        "Original name should be preserved first: {result}"
    );
}

#[test]
fn test_japanese_font_name_has_substitutes() {
    let subs = substitutes("メイリオ").expect("Japanese Meiryo name should have substitutes");
    assert!(subs.contains(&"Meiryo"));
}

#[test]
fn test_chinese_font_name_has_substitutes() {
    let subs = substitutes("微软雅黑").expect("Chinese YaHei name should have substitutes");
    assert!(subs.contains(&"Microsoft YaHei"));
}

// --- is_primary_font_available() tests ---

#[test]
fn test_is_primary_font_available_returns_true_when_no_context() {
    // When no font context is active (e.g. WASM), assume available.
    assert!(is_primary_font_available("Anything"));
}

#[test]
fn test_is_primary_font_available_returns_true_when_font_exists() {
    let context = FontSearchContext::for_test(Vec::new(), &["Pretendard"], &[], &[]);
    let result =
        with_font_search_context(Some(&context), || is_primary_font_available("Pretendard"));
    assert!(result);
}

#[test]
fn test_is_primary_font_available_returns_true_via_alias() {
    // "Pretendard ExtraBold" → alias "Pretendard" → available
    let context = FontSearchContext::for_test(Vec::new(), &["Pretendard"], &[], &[]);
    let result = with_font_search_context(Some(&context), || {
        is_primary_font_available("Pretendard ExtraBold")
    });
    assert!(result);
}

#[test]
fn test_is_primary_font_available_returns_false_when_missing() {
    let context = FontSearchContext::for_test(Vec::new(), &["Arial"], &[], &[]);
    let result = with_font_search_context(Some(&context), || {
        is_primary_font_available("Pretendard ExtraBold")
    });
    assert!(!result);
}