oxidize-pdf 2.6.0

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
//! Integration tests for font embedding with Document API
//!
//! These tests verify that font embedding works end-to-end with the
//! Document creation and PDF generation workflow.

use oxidize_pdf::error::Result;
use oxidize_pdf::{Document, EmbeddingOptions, Font, FontEmbedder, FontEncoding, FontFlags, Page};
use std::collections::HashSet;
use tempfile::TempDir;

/// Helper to create minimal font data for testing
fn create_minimal_font_data() -> Vec<u8> {
    // This creates a minimal TrueType font structure for testing
    // In production, you would use actual TrueType font files
    let mut font_data = Vec::new();

    // TrueType signature (0x00010000)
    font_data.extend_from_slice(&[0x00, 0x01, 0x00, 0x00]);

    // Number of tables (minimum required tables)
    font_data.extend_from_slice(&[0x00, 0x04]); // 4 tables

    // Search range, entry selector, range shift
    font_data.extend_from_slice(&[0x00, 0x40, 0x00, 0x02, 0x00, 0x00]);

    // Table directory entries for head, hhea, maxp, cmap tables
    let tables = [
        (b"head", 0x36u32, 0x36u32),
        (b"hhea", 0x6C, 0x24),
        (b"maxp", 0x90, 0x06),
        (b"cmap", 0x96, 0x20),
    ];

    for (tag, offset, length) in &tables {
        font_data.extend_from_slice(*tag);
        font_data.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // checksum
        font_data.extend_from_slice(&offset.to_be_bytes());
        font_data.extend_from_slice(&length.to_be_bytes());
    }

    // Pad to minimum required size
    while font_data.len() < 0xB6 {
        font_data.push(0);
    }

    font_data
}

#[test]
fn test_font_embedder_api_functionality() -> Result<()> {
    // Test basic FontEmbedder API
    let mut embedder = FontEmbedder::new();

    // Verify initial state
    assert_eq!(embedder.embedded_fonts().len(), 0);

    // Test embedding options
    let options = EmbeddingOptions {
        subset: true,
        max_subset_size: Some(128),
        compress_font_streams: true,
        embed_license_info: false,
    };

    // Test font flags
    let flags = FontFlags {
        non_symbolic: true,
        serif: false,
        fixed_pitch: false,
        ..Default::default()
    };

    // Verify flag conversion
    let flag_value = flags.to_flags();
    assert_eq!(flag_value & 32, 32); // non_symbolic bit
    assert_eq!(flag_value & 2, 0); // serif bit should be 0

    // Test encoding types
    let encodings = [
        FontEncoding::WinAnsiEncoding,
        FontEncoding::MacRomanEncoding,
        FontEncoding::StandardEncoding,
        FontEncoding::Identity,
    ];

    for encoding in &encodings {
        match encoding {
            FontEncoding::WinAnsiEncoding => {
                assert!(matches!(encoding, FontEncoding::WinAnsiEncoding))
            }
            FontEncoding::MacRomanEncoding => {
                assert!(matches!(encoding, FontEncoding::MacRomanEncoding))
            }
            FontEncoding::StandardEncoding => {
                assert!(matches!(encoding, FontEncoding::StandardEncoding))
            }
            FontEncoding::Identity => assert!(matches!(encoding, FontEncoding::Identity)),
            _ => {}
        }
    }

    // Test with minimal font data (will fail but tests the API)
    let font_data = create_minimal_font_data();
    let mut used_glyphs = HashSet::new();
    used_glyphs.insert(0); // .notdef
    used_glyphs.insert(65); // A
    used_glyphs.insert(66); // B

    // This will fail with our minimal test data, but verifies the API exists
    let result = embedder.embed_truetype_font(&font_data, &used_glyphs, &options);
    assert!(result.is_err()); // Expected with test data

    Ok(())
}

#[test]
fn test_document_with_standard_fonts() -> Result<()> {
    // Test document creation with standard PDF fonts (no embedding needed)
    let mut doc = Document::new();
    doc.set_title("Font Test Document");
    doc.set_author("oxidize-pdf");

    let mut page = Page::a4();

    // Test all standard fonts
    let fonts = [
        (Font::Helvetica, "Helvetica"),
        (Font::HelveticaBold, "Helvetica Bold"),
        (Font::HelveticaOblique, "Helvetica Oblique"),
        (Font::TimesRoman, "Times Roman"),
        (Font::TimesBold, "Times Bold"),
        (Font::TimesItalic, "Times Italic"),
        (Font::Courier, "Courier"),
        (Font::CourierBold, "Courier Bold"),
        (Font::Symbol, "Symbol"),
        (Font::ZapfDingbats, "ZapfDingbats"),
    ];

    let mut y = 750.0;
    for (font, name) in &fonts {
        page.text()
            .set_font(font.clone(), 12.0)
            .at(50.0, y)
            .write(&format!("{name}: Test text"))?;
        y -= 20.0;
    }

    doc.add_page(page);

    // Save to temporary file
    let temp_dir = TempDir::new()?;
    let output_path = temp_dir.path().join("font_test.pdf");

    doc.save(&output_path)?;

    // Verify file was created and has content
    assert!(output_path.exists());
    let file_data = std::fs::read(&output_path)?;
    assert!(file_data.len() > 1000); // Should be a decent-sized PDF

    // Verify PDF starts with correct header
    assert!(file_data.starts_with(b"%PDF-"));

    Ok(())
}

#[test]
fn test_font_embedding_workflow_structure() -> Result<()> {
    // Test the structure of font embedding workflow
    // (without actual font data which would require external files)

    let embedder = FontEmbedder::new();

    // Test different embedding options
    let subset_options = EmbeddingOptions {
        subset: true,
        max_subset_size: Some(256),
        compress_font_streams: true,
        embed_license_info: false,
    };

    let full_embed_options = EmbeddingOptions {
        subset: false,
        max_subset_size: None,
        compress_font_streams: false,
        embed_license_info: true,
    };

    // Test that options can be created and modified
    assert!(subset_options.subset);
    assert!(!full_embed_options.subset);
    assert!(subset_options.compress_font_streams);
    assert!(!full_embed_options.compress_font_streams);

    // Test font flag combinations
    let serif_flags = FontFlags {
        serif: true,
        non_symbolic: true,
        ..Default::default()
    };

    let monospace_flags = FontFlags {
        fixed_pitch: true,
        non_symbolic: true,
        ..Default::default()
    };

    let italic_flags = FontFlags {
        italic: true,
        non_symbolic: true,
        ..Default::default()
    };

    // Verify flag bits
    assert!(serif_flags.to_flags() & 2 != 0); // serif bit
    assert!(monospace_flags.to_flags() & 1 != 0); // fixed_pitch bit
    assert!(italic_flags.to_flags() & 64 != 0); // italic bit

    // All should have non_symbolic bit set
    assert!(serif_flags.to_flags() & 32 != 0);
    assert!(monospace_flags.to_flags() & 32 != 0);
    assert!(italic_flags.to_flags() & 32 != 0);

    // Test glyph set creation
    let mut ascii_glyphs = HashSet::new();
    for i in 32..127 {
        // Basic ASCII printable characters
        ascii_glyphs.insert(i);
    }
    ascii_glyphs.insert(0); // Always include .notdef

    assert!(ascii_glyphs.len() > 90); // Should have most ASCII chars
    assert!(ascii_glyphs.contains(&65)); // Should contain 'A'
    assert!(ascii_glyphs.contains(&97)); // Should contain 'a'
    assert!(ascii_glyphs.contains(&32)); // Should contain space

    // Test that embedder starts empty
    assert_eq!(embedder.embedded_fonts().len(), 0);

    Ok(())
}

#[test]
fn test_font_embedding_error_handling() -> Result<()> {
    let embedder = FontEmbedder::new();

    // Test error handling for non-existent fonts
    let result = embedder.generate_font_dictionary("NonExistentFont");
    assert!(result.is_err());

    let result = embedder.generate_font_descriptor("NonExistentFont");
    assert!(result.is_err());

    let result = embedder.generate_tounicode_cmap("NonExistentFont");
    assert!(result.is_err());

    Ok(())
}

#[test]
fn test_font_embedding_with_document_integration() -> Result<()> {
    // Test that font embedding can be used alongside document creation
    let mut doc = Document::new();
    doc.set_title("Font Embedding Integration Test");

    let mut page = Page::a4();

    // Add content using standard fonts
    page.text()
        .set_font(Font::Helvetica, 16.0)
        .at(50.0, 750.0)
        .write("Standard Font: Helvetica")?;

    page.text()
        .set_font(Font::TimesRoman, 14.0)
        .at(50.0, 720.0)
        .write("Standard Font: Times Roman")?;

    page.text()
        .set_font(Font::Courier, 12.0)
        .at(50.0, 690.0)
        .write("Standard Font: Courier (monospace)")?;

    // Create a font embedder (for future use)
    let embedder = FontEmbedder::new();

    // Verify embedder is ready for use
    assert_eq!(embedder.embedded_fonts().len(), 0);

    // Add informational text about font embedding
    page.text()
        .set_font(Font::Helvetica, 10.0)
        .at(50.0, 650.0)
        .write("Font embedding system ready for TrueType/OpenType fonts")?;

    page.text()
        .set_font(Font::Helvetica, 10.0)
        .at(50.0, 635.0)
        .write("Features: subsetting, compression, CID fonts, ToUnicode CMap")?;

    doc.add_page(page);

    // Save document
    let temp_dir = TempDir::new()?;
    let output_path = temp_dir.path().join("integration_test.pdf");

    doc.save(&output_path)?;

    // Verify file creation
    assert!(output_path.exists());
    let file_data = std::fs::read(&output_path)?;
    assert!(file_data.len() > 500);

    Ok(())
}

#[test]
fn test_cid_font_embedding_structure() -> Result<()> {
    let mut embedder = FontEmbedder::new();

    // Test CID font embedding structure (without real font data)
    let font_data = create_minimal_font_data();
    let mut used_chars = HashSet::new();

    // Add some Unicode characters that would require CID fonts
    used_chars.insert(0x4E00); // CJK character
    used_chars.insert(0x4E01);
    used_chars.insert(0x4E02);

    let options = EmbeddingOptions::default();

    // This will fail with test data but verifies the API
    let result = embedder.embed_cid_font(&font_data, &used_chars, "Identity-H", &options);
    assert!(result.is_err()); // Expected with minimal test data

    Ok(())
}

#[test]
fn test_unicode_mapping_creation() -> Result<()> {
    // Test Unicode mapping structures
    let mut glyph_to_unicode = std::collections::HashMap::new();

    // Create basic ASCII mappings
    glyph_to_unicode.insert(65u16, "A".to_string());
    glyph_to_unicode.insert(66u16, "B".to_string());
    glyph_to_unicode.insert(67u16, "C".to_string());
    glyph_to_unicode.insert(32u16, " ".to_string());

    // Verify mappings
    assert_eq!(glyph_to_unicode.get(&65), Some(&"A".to_string()));
    assert_eq!(glyph_to_unicode.get(&66), Some(&"B".to_string()));
    assert_eq!(glyph_to_unicode.get(&67), Some(&"C".to_string()));
    assert_eq!(glyph_to_unicode.get(&32), Some(&" ".to_string()));

    // Test Unicode string formatting (as would be used in ToUnicode CMap)
    for (glyph_id, unicode_str) in &glyph_to_unicode {
        let hex_unicode: String = unicode_str
            .chars()
            .map(|c| format!("{:04X}", c as u32))
            .collect();

        // Verify format for basic ASCII
        match *glyph_id {
            65 => assert_eq!(hex_unicode, "0041"),
            66 => assert_eq!(hex_unicode, "0042"),
            67 => assert_eq!(hex_unicode, "0043"),
            32 => assert_eq!(hex_unicode, "0020"),
            _ => {}
        }
    }

    Ok(())
}

#[test]
fn test_font_subset_glyph_management() -> Result<()> {
    // Test glyph subset management
    let mut full_glyph_set = HashSet::new();
    let mut subset_glyph_set = HashSet::new();

    // Create full character set (all printable ASCII)
    for i in 32..127 {
        full_glyph_set.insert(i);
    }

    // Create subset (just uppercase letters and space)
    for i in 65..91 {
        // A-Z
        subset_glyph_set.insert(i);
    }
    subset_glyph_set.insert(32); // space
    subset_glyph_set.insert(0); // .notdef

    // Test subset properties
    assert!(full_glyph_set.len() > subset_glyph_set.len());
    assert!(subset_glyph_set.len() == 28); // 26 letters + space + .notdef

    // Test that subset is contained in full set
    for &glyph in &subset_glyph_set {
        if glyph != 0 {
            // .notdef might not be in the range
            assert!((32..=90).contains(&glyph));
        }
    }

    // Test subset criteria (what EmbeddingOptions might use)
    let subset_threshold = 256;
    assert!(subset_glyph_set.len() < subset_threshold);
    assert!(full_glyph_set.len() < subset_threshold); // ASCII fits in subset

    Ok(())
}