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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Memory Stress Integration Tests
//!
//! Comprehensive tests for memory-intensive operations and stress scenarios.
//! These tests ensure the library handles memory pressure gracefully without OOM.
//!
//! Test categories:
//! - Large image processing (100+ embedded images)
//! - Stream processing of large PDFs (simulated 1GB+)
//! - Concurrent operations (50+ simultaneous PDFs)
//! - Memory leak detection (repeated create/destroy)
//! - Cache thrashing scenarios
//! - Object deduplication
//! - Font subsetting with full Unicode
//! - Compression stress tests

use oxidize_pdf::graphics::Color;
use oxidize_pdf::memory::MemoryOptions;
use oxidize_pdf::text::Font;
use oxidize_pdf::{Document, Page, Result};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use tempfile::TempDir;

/// Test processing many embedded images
#[test]
fn test_large_image_processing() -> Result<()> {
    let mut doc = Document::new();
    doc.set_title("Large Image Processing Test");

    // Simulate adding 100 images across 10 pages
    for page_num in 0..10 {
        let mut page = Page::a4();

        // Add page title
        page.text()
            .set_font(Font::Helvetica, 14.0)
            .at(50.0, 750.0)
            .write(&format!("Page {} - Image Gallery", page_num + 1))?;

        // Add 10 images per page (100 total)
        for img_num in 0..10 {
            let x = 50.0 + (img_num % 5) as f64 * 100.0;
            let y = 600.0 - (img_num / 5) as f64 * 200.0;

            // Create a colored rectangle to simulate image
            let color = match img_num % 3 {
                0 => Color::rgb(1.0, 0.0, 0.0), // Red
                1 => Color::rgb(0.0, 1.0, 0.0), // Green
                _ => Color::rgb(0.0, 0.0, 1.0), // Blue
            };

            page.graphics()
                .set_fill_color(color)
                .rectangle(x, y, 80.0, 80.0)
                .fill();

            // Add image label
            page.text()
                .set_font(Font::Helvetica, 8.0)
                .at(x, y - 10.0)
                .write(&format!("IMG_{:03}", page_num * 10 + img_num))?;
        }

        doc.add_page(page);
    }

    // Save and measure performance
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("large_images.pdf");

    let start = Instant::now();
    doc.save(&file_path)?;
    let duration = start.elapsed();

    let metadata = std::fs::metadata(&file_path)?;
    println!(
        "Large image document: {} bytes in {:?}",
        metadata.len(),
        duration
    );

    Ok(())
}

/// Test stream processing of very large PDFs
#[test]
fn test_stream_processing_large_pdf() -> Result<()> {
    let temp_dir = TempDir::new().unwrap();

    // First, create a large PDF to process
    let large_pdf_path = temp_dir.path().join("large_source.pdf");
    create_large_test_pdf(&large_pdf_path, 100)?; // 100 pages

    // Process using streaming with memory limits
    let _memory_opts = MemoryOptions::large_file();

    // Process the PDF page by page
    let output_path = temp_dir.path().join("processed_large.pdf");
    let start = Instant::now();

    // Simulate streaming processing
    let mut output_doc = Document::new();

    // Process in chunks of 10 pages
    for chunk_start in (0..100).step_by(10) {
        // In real implementation, this would stream pages
        for page_num in chunk_start..chunk_start.min(chunk_start + 10).min(100) {
            let mut page = Page::a4();
            page.text()
                .set_font(Font::Helvetica, 12.0)
                .at(50.0, 700.0)
                .write(&format!("Processed page {}", page_num + 1))?;

            output_doc.add_page(page);
        }

        // Simulate memory release between chunks
        std::thread::sleep(Duration::from_millis(10));
    }

    output_doc.save(&output_path)?;
    let duration = start.elapsed();

    println!("Streamed 100-page PDF in {duration:?}");

    Ok(())
}

/// Test concurrent PDF operations
#[test]
fn test_concurrent_pdf_operations() -> Result<()> {
    let temp_dir = Arc::new(TempDir::new().unwrap());
    let start = Instant::now();
    let timeout = Duration::from_secs(60);

    // Create 50 threads for concurrent operations
    let handles: Vec<_> = (0..50)
        .map(|thread_id| {
            let temp_dir = Arc::clone(&temp_dir);

            thread::spawn(move || -> Result<()> {
                // Each thread creates its own document
                let mut doc = Document::new();
                doc.set_title(format!("Concurrent Document {thread_id}"));

                // Add 5 pages per document
                for page_num in 0..5 {
                    let mut page = Page::a4();

                    page.text()
                        .set_font(Font::Helvetica, 12.0)
                        .at(100.0, 700.0)
                        .write(&format!("Thread {} - Page {}", thread_id, page_num + 1))?;

                    // Add some graphics
                    page.graphics()
                        .set_stroke_color(Color::rgb(
                            ((thread_id as f32 * 0.02) % 1.0) as f64,
                            0.5,
                            (1.0 - (thread_id as f32 * 0.02) % 1.0) as f64,
                        ))
                        .rectangle(100.0, 500.0, 400.0, 100.0)
                        .stroke();

                    doc.add_page(page);
                }

                // Save document
                let file_path = temp_dir.path().join(format!("concurrent_{thread_id}.pdf"));
                doc.save(&file_path)?;

                Ok(())
            })
        })
        .collect();

    // Wait for all threads with timeout check
    let mut completed = 0;
    for handle in handles {
        if start.elapsed() > timeout {
            panic!("Timeout: Concurrent operations took too long");
        }

        handle.join().unwrap()?;
        completed += 1;
    }

    println!(
        "Completed {} concurrent PDF operations in {:?}",
        completed,
        start.elapsed()
    );

    // Verify all files were created
    for i in 0..50 {
        let file_path = temp_dir.path().join(format!("concurrent_{i}.pdf"));
        assert!(file_path.exists());
    }

    Ok(())
}

/// Test memory leak detection through repeated operations
#[test]
fn test_memory_leak_detection() -> Result<()> {
    let temp_dir = TempDir::new().unwrap();

    // Perform 1000 create/destroy cycles
    for cycle in 0..1000 {
        // Create document
        let mut doc = Document::new();
        doc.set_title(format!("Memory Leak Test {cycle}"));

        // Add content
        let mut page = Page::a4();
        page.text()
            .set_font(Font::Helvetica, 12.0)
            .at(100.0, 700.0)
            .write(&format!("Cycle {cycle}"))?;

        // Add graphics
        for i in 0..10 {
            page.graphics()
                .circle(100.0 + i as f64 * 40.0, 500.0, 20.0)
                .fill();
        }

        doc.add_page(page);

        // Save to force serialization
        let file_path = temp_dir
            .path()
            .join(format!("leak_test_{}.pdf", cycle % 10));
        doc.save(&file_path)?;

        // Document should be dropped here, freeing all memory

        // Small delay to allow OS to reclaim memory
        if cycle % 100 == 0 {
            std::thread::sleep(Duration::from_millis(10));
            println!("Completed {cycle} cycles");
        }
    }

    println!("Memory leak test completed: 1000 cycles");

    Ok(())
}

/// Test cache thrashing scenarios
#[test]
fn test_cache_thrashing() -> Result<()> {
    let mut doc = Document::new();

    // Add many fonts to thrash font cache
    let _font_names = [
        "TestFont1",
        "TestFont2",
        "TestFont3",
        "TestFont4",
        "TestFont5",
        "TestFont6",
        "TestFont7",
        "TestFont8",
        "TestFont9",
        "TestFont10",
    ];

    // Create 50 pages with rapidly changing fonts
    for page_num in 0..50 {
        let mut page = Page::a4();

        // Use different fonts in rapid succession
        let mut y = 750.0;
        for (i, line) in (0..40).enumerate() {
            let font_idx = (page_num * 40 + line) % 14; // Cycle through standard fonts
            let font = match font_idx {
                0 => Font::Helvetica,
                1 => Font::HelveticaBold,
                2 => Font::HelveticaOblique,
                3 => Font::HelveticaBoldOblique,
                4 => Font::TimesRoman,
                5 => Font::TimesBold,
                6 => Font::TimesItalic,
                7 => Font::TimesBoldItalic,
                8 => Font::Courier,
                9 => Font::CourierBold,
                10 => Font::CourierOblique,
                11 => Font::CourierBoldOblique,
                12 => Font::Symbol,
                _ => Font::ZapfDingbats,
            };

            page.text()
                .set_font(font, 8.0)
                .at(50.0, y)
                .write(&format!("Cache thrash test - Page {page_num} Line {i}"))?;

            y -= 15.0;
        }

        doc.add_page(page);
    }

    // Save and measure
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("cache_thrash.pdf");

    let start = Instant::now();
    doc.save(&file_path)?;
    let duration = start.elapsed();

    println!("Cache thrashing test completed in {duration:?}");

    Ok(())
}

/// Test object deduplication with many duplicate objects
#[test]
fn test_object_deduplication_stress() -> Result<()> {
    let mut doc = Document::new();

    // Create a pattern that will be reused many times
    let repeated_text = "This is a repeated string that should be deduplicated. ";
    let repeated_text_long = repeated_text.repeat(10);

    // Add 100 pages with duplicate content
    for _page_num in 0..100 {
        let mut page = Page::a4();

        // Add the same text many times
        for i in 0..20 {
            page.text()
                .set_font(Font::Helvetica, 10.0)
                .at(50.0, 750.0 - i as f64 * 30.0)
                .write(&repeated_text_long)?;
        }

        // Add identical graphics
        for _i in 0..10 {
            page.graphics()
                .rectangle(100.0, 100.0, 100.0, 100.0)
                .stroke();
        }

        doc.add_page(page);
    }

    // Save and check file size
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("deduplication_test.pdf");
    doc.save(&file_path)?;

    let metadata = std::fs::metadata(&file_path)?;
    let size_mb = metadata.len() as f64 / 1_048_576.0;

    println!("Deduplication test: {size_mb:.2} MB (should be optimized)");

    // File should be reasonably sized despite duplicate content
    assert!(metadata.len() < 10_000_000); // Less than 10MB

    Ok(())
}

/// Test font subsetting with full Unicode range
#[test]
fn test_unicode_font_subsetting_stress() -> Result<()> {
    let mut doc = Document::new();
    let mut page = Page::a4();

    // Test various Unicode blocks
    let unicode_tests = vec![
        // Basic Latin
        (0x0020..0x007F, "Basic Latin"),
        // Latin-1 Supplement
        (0x00A0..0x00FF, "Latin-1 Supplement"),
        // Latin Extended-A
        (0x0100..0x017F, "Latin Extended-A"),
        // Greek
        (0x0370..0x03FF, "Greek"),
        // Cyrillic
        (0x0400..0x04FF, "Cyrillic"),
        // Hebrew
        (0x0590..0x05FF, "Hebrew"),
        // Arabic
        (0x0600..0x06FF, "Arabic"),
        // CJK Unified Ideographs (sample)
        (0x4E00..0x4E20, "CJK Sample"),
    ];

    let mut y = 750.0;

    for (range, name) in unicode_tests {
        // Add section header
        page.text()
            .set_font(Font::HelveticaBold, 12.0)
            .at(50.0, y)
            .write(&format!("{name} Test:"))?;
        y -= 20.0;

        // Build string with characters from range
        let mut test_string = String::new();
        for code_point in range {
            if let Some(ch) = char::from_u32(code_point) {
                test_string.push(ch);
                if test_string.len() >= 50 {
                    break; // Limit line length
                }
            }
        }

        // Try to render (may show replacement chars for unsupported)
        if !test_string.is_empty() {
            page.text()
                .set_font(Font::Helvetica, 10.0)
                .at(50.0, y)
                .write(&test_string)?;
            y -= 20.0;
        }

        if y < 100.0 {
            break; // Page full
        }
    }

    doc.add_page(page);

    // Save and verify
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("unicode_subsetting.pdf");
    doc.save(&file_path)?;

    Ok(())
}

/// Test compression stress with highly redundant data
#[test]
fn test_compression_stress() -> Result<()> {
    // Test both compressed and uncompressed versions
    for compressed in [true, false] {
        let mut doc = Document::new();
        doc.set_compress(compressed);
        doc.set_title(format!(
            "Compression Test - {}",
            if compressed { "ON" } else { "OFF" }
        ));

        // Create highly redundant content
        let redundant_text = "AAAAAAAAAA"; // Highly compressible
        let random_text = "AbCdEfGhIj"; // Less compressible

        // Add 50 pages of redundant content
        for page_num in 0..50 {
            let mut page = Page::a4();

            // Alternate between redundant and random
            let text = if page_num % 2 == 0 {
                redundant_text.repeat(100)
            } else {
                random_text.repeat(100)
            };

            // Fill page with text
            for i in 0..60 {
                page.text()
                    .set_font(Font::Courier, 10.0)
                    .at(50.0, 750.0 - i as f64 * 12.0)
                    .write(&text)?;
            }

            doc.add_page(page);
        }

        // Save and compare sizes
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join(format!(
            "compression_{}.pdf",
            if compressed { "on" } else { "off" }
        ));

        let start = Instant::now();
        doc.save(&file_path)?;
        let duration = start.elapsed();

        let metadata = std::fs::metadata(&file_path)?;
        let size_mb = metadata.len() as f64 / 1_048_576.0;

        println!(
            "Compression {}: {:.2} MB in {:?}",
            if compressed { "ON" } else { "OFF" },
            size_mb,
            duration
        );
    }

    Ok(())
}

/// Test page tree balancing with unbalanced trees
#[test]
fn test_page_tree_balancing_stress() -> Result<()> {
    let mut doc = Document::new();

    // Create an extremely unbalanced page tree by adding pages in groups
    let start = Instant::now();

    // Add 1000 pages in an unbalanced way
    for group in 0..100 {
        // Add 10 pages per group
        for page_in_group in 0..10 {
            let mut page = Page::a4();

            page.text()
                .set_font(Font::Helvetica, 10.0)
                .at(50.0, 700.0)
                .write(&format!("Group {group} - Page {page_in_group}"))?;

            // Add nested graphics states to stress the tree
            let graphics = page.graphics();
            for _ in 0..5 {
                graphics.save_state();
            }

            graphics.circle(300.0, 400.0, 50.0).fill();

            for _ in 0..5 {
                graphics.restore_state();
            }

            doc.add_page(page);
        }

        // Check timeout
        if start.elapsed() > Duration::from_secs(30) {
            panic!("Timeout: Page tree balancing took too long");
        }
    }

    // Save and verify
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("page_tree_stress.pdf");

    let save_start = Instant::now();
    doc.save(&file_path)?;
    let save_duration = save_start.elapsed();

    println!("Saved 1000-page unbalanced tree in {save_duration:?}");

    Ok(())
}

// Helper functions

/// Create a large test PDF with specified number of pages
fn create_large_test_pdf(path: &std::path::Path, page_count: usize) -> Result<()> {
    let mut doc = Document::new();

    for i in 0..page_count {
        let mut page = Page::a4();

        // Add some content
        page.text()
            .set_font(Font::Helvetica, 24.0)
            .at(200.0, 400.0)
            .write(&format!("Page {}", i + 1))?;

        // Add some graphics
        page.graphics().circle(300.0, 400.0, 100.0).stroke();

        doc.add_page(page);
    }

    doc.save(path)?;
    Ok(())
}

/// Simulate memory pressure by allocating and releasing memory
#[allow(dead_code)]
fn simulate_memory_pressure() {
    // Allocate 100MB temporarily
    let _large_vec: Vec<u8> = vec![0; 100_000_000];

    // Let it go out of scope to release
}