oxidize-pdf 2.5.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
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
//! Memory optimization integration tests
//!
//! Tests that validate memory-efficient PDF processing workflows including
//! lazy loading, caching, streaming, and large document handling.

use oxidize_pdf::document::Document;
use oxidize_pdf::error::Result;
use oxidize_pdf::memory::{MemoryManager, MemoryOptions};
use oxidize_pdf::page::Page;
use oxidize_pdf::parser::optimized_reader::OptimizedPdfReader;
use oxidize_pdf::parser::ParseOptions;
use oxidize_pdf::text::Font;
use std::fs;
use std::io::Cursor;
use tempfile::TempDir;

/// Test memory-optimized document creation workflow
#[test]
fn test_memory_optimized_document_creation() -> Result<()> {
    let temp_dir = TempDir::new().unwrap();

    // Create memory options for different scenarios
    let memory_configs = vec![
        ("small_file", MemoryOptions::small_file()),
        ("large_file", MemoryOptions::large_file()),
        (
            "custom",
            MemoryOptions::default()
                .with_cache_size(500)
                .with_lazy_loading(false),
        ),
    ];

    for (config_name, _memory_options) in memory_configs {
        let mut doc = Document::new();
        doc.set_title(format!("Memory Test - {config_name}"));

        // Create substantial content to test memory usage
        for page_num in 1..=5 {
            let mut page = Page::a4();

            page.text()
                .set_font(Font::Helvetica, 12.0)
                .at(50.0, 750.0)
                .write(&format!("Memory config: {config_name} - Page {page_num}"))?;

            // Add content that would benefit from memory optimization
            let content = format!("Content for page {page_num} with config {config_name}");
            for line in 0..20 {
                let y_pos = 700.0 - (line as f64 * 15.0);
                page.text()
                    .set_font(Font::Helvetica, 10.0)
                    .at(50.0, y_pos)
                    .write(&format!("{} - Line {}", content, line + 1))?;
            }

            doc.add_page(page);
        }

        // Test memory-aware saving
        let file_path = temp_dir.path().join(format!("memory_{config_name}.pdf"));
        doc.save(&file_path)?;

        assert!(file_path.exists());
        let file_size = fs::metadata(&file_path).unwrap().len();
        println!("Generated PDF size for {config_name}: {file_size} bytes");
        assert!(file_size > 2000); // Should be substantial (adjusted from 5000)

        // Test in-memory generation with memory awareness
        let pdf_bytes = doc.to_bytes()?;
        assert!(!pdf_bytes.is_empty());
        assert!(pdf_bytes.len() as u64 <= file_size + 1000); // Should be similar size
    }

    Ok(())
}

/// Test optimized PDF reader with different memory configurations
#[test]
fn test_optimized_reader_memory_workflows() -> Result<()> {
    let temp_dir = TempDir::new().unwrap();

    // Create a test PDF to read
    let mut test_doc = Document::new();
    test_doc.set_title("Optimized Reader Test");

    let mut page = Page::a4();
    page.text()
        .set_font(Font::Helvetica, 12.0)
        .at(100.0, 700.0)
        .write("Test content for optimized reader")?;
    test_doc.add_page(page);

    let test_path = temp_dir.path().join("optimized_reader_test.pdf");
    test_doc.save(&test_path)?;

    // Test different memory configurations with optimized reader
    let memory_configs = vec![
        MemoryOptions::default().with_cache_size(10),
        MemoryOptions::default().with_cache_size(100),
        MemoryOptions::default().with_cache_size(1000),
    ];

    for (i, memory_options) in memory_configs.into_iter().enumerate() {
        // Read the PDF content for testing
        let pdf_data = fs::read(&test_path)?;
        let cursor = Cursor::new(pdf_data);

        // Test optimized reader creation with memory options
        let parse_options = ParseOptions::default();
        let reader_result =
            OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options.clone());

        if reader_result.is_ok() {
            let reader = reader_result.unwrap();

            // Test memory statistics
            let stats = reader.memory_stats();
            assert_eq!(stats.cache_hits, 0); // Initial state
            assert_eq!(stats.cache_misses, 0); // Initial state

            // Test memory options integration
            assert!(memory_options.cache_size > 0);

            println!("Successfully created optimized reader with config {i}");
        } else {
            // If optimized reader creation fails, that's expected for incomplete implementation
            println!("Optimized reader creation failed for config {i} (expected)");
        }
    }

    Ok(())
}

/// Test memory manager functionality
#[test]
fn test_memory_manager_integration() -> Result<()> {
    // Test different memory manager configurations
    let configs = vec![
        MemoryOptions::default(),
        MemoryOptions::small_file(),
        MemoryOptions::large_file(),
        MemoryOptions::default().with_cache_size(0), // No cache
    ];

    for (i, config) in configs.into_iter().enumerate() {
        let manager = MemoryManager::new(config.clone());

        // Test initial statistics
        let initial_stats = manager.stats();
        assert_eq!(initial_stats.allocated_bytes, 0);
        assert_eq!(initial_stats.cache_hits, 0);
        assert_eq!(initial_stats.cache_misses, 0);

        // Test recording operations
        manager.record_allocation(1024);
        manager.record_cache_hit();
        manager.record_cache_miss();
        manager.record_cache_miss();

        let updated_stats = manager.stats();
        assert_eq!(updated_stats.allocated_bytes, 1024);
        assert_eq!(updated_stats.cache_hits, 1);
        assert_eq!(updated_stats.cache_misses, 2);

        // Test cache availability based on configuration
        if config.cache_size > 0 {
            assert!(manager.cache().is_some());
        } else {
            assert!(manager.cache().is_none());
        }

        println!("Memory manager test {i} completed successfully");
    }

    Ok(())
}

/// Test memory-efficient large document processing
#[test]
fn test_large_document_memory_efficiency() -> Result<()> {
    let temp_dir = TempDir::new().unwrap();

    // Create a large document to test memory efficiency
    let mut large_doc = Document::new();
    large_doc.set_title("Large Document Memory Efficiency Test");

    // Configure for memory efficiency
    large_doc.set_compress(true); // Enable compression to save memory

    let page_count = 25;
    for page_num in 1..=page_count {
        let mut page = Page::a4();

        page.text()
            .set_font(Font::Helvetica, 14.0)
            .at(50.0, 750.0)
            .write(&format!("Large Document - Page {page_num}/{page_count}"))?;

        // Add substantial content to each page
        for section in 0..5 {
            let section_title = format!("Section {} on page {}", section + 1, page_num);
            let y_start = 650.0 - (section as f64 * 120.0);

            page.text()
                .set_font(Font::HelveticaBold, 12.0)
                .at(50.0, y_start)
                .write(&section_title)?;

            // Add content lines
            for line in 0..8 {
                let y_pos = y_start - 20.0 - (line as f64 * 12.0);
                if y_pos > 50.0 {
                    // Stay within page bounds
                    page.text()
                        .set_font(Font::Helvetica, 10.0)
                        .at(70.0, y_pos)
                        .write(&format!(
                            "Content line {} of section {}",
                            line + 1,
                            section + 1
                        ))?;
                }
            }
        }

        large_doc.add_page(page);

        // Periodically test memory usage during construction
        if page_num % 5 == 0 {
            let partial_bytes = large_doc.to_bytes()?;
            assert!(!partial_bytes.is_empty());
            println!(
                "Generated {} pages, current size: {} bytes",
                page_num,
                partial_bytes.len()
            );
        }
    }

    // Test final memory-efficient operations
    let start_time = std::time::Instant::now();

    // Save to file
    let large_path = temp_dir.path().join("large_memory_efficient.pdf");
    large_doc.save(&large_path)?;

    let save_duration = start_time.elapsed();
    assert!(save_duration.as_secs() < 10); // Should complete reasonably quickly

    // Verify file size is reasonable with compression
    let file_size = fs::metadata(&large_path).unwrap().len();
    assert!(file_size > 20000); // Should be substantial
    assert!(file_size < 10_000_000); // But not excessive (under 10MB)

    // Test in-memory generation efficiency
    let memory_start = std::time::Instant::now();
    let pdf_bytes = large_doc.to_bytes()?;
    let memory_duration = memory_start.elapsed();

    assert!(!pdf_bytes.is_empty());
    assert!(memory_duration.as_secs() < 10);

    println!("Large document test completed:");
    println!("  Pages: {page_count}");
    println!("  File size: {file_size} bytes");
    println!("  Save time: {save_duration:?}");
    println!("  Memory generation time: {memory_duration:?}");

    Ok(())
}

/// Test memory optimization with different content types
#[test]
fn test_content_type_memory_optimization() -> Result<()> {
    let temp_dir = TempDir::new().unwrap();

    // Test different content types using function pointers
    fn setup_text_heavy(page: &mut Page) -> Result<()> {
        // Text-heavy content
        for i in 0..50 {
            let y_pos = 750.0 - (i as f64 * 15.0);
            if y_pos > 50.0 {
                page.text()
                    .set_font(Font::Helvetica, 10.0)
                    .at(50.0, y_pos)
                    .write(&format!(
                        "Text line {} with substantial content for memory testing",
                        i + 1
                    ))?;
            }
        }
        Ok(())
    }

    fn setup_graphics_heavy(page: &mut Page) -> Result<()> {
        // Graphics-heavy content
        for i in 0..20 {
            let x = 50.0 + (i % 10) as f64 * 50.0;
            let y = 600.0 - (i / 10) as f64 * 100.0;
            page.graphics()
                .set_fill_color(oxidize_pdf::graphics::Color::rgb(
                    (i as f64) / 20.0,
                    0.5,
                    1.0 - (i as f64) / 20.0,
                ))
                .rectangle(x, y, 40.0, 40.0)
                .fill();
        }
        Ok(())
    }

    fn setup_mixed_content(page: &mut Page) -> Result<()> {
        // Mixed content
        page.text()
            .set_font(Font::HelveticaBold, 16.0)
            .at(50.0, 750.0)
            .write("Mixed Content Page")?;

        for i in 0..10 {
            let y_text = 700.0 - (i as f64 * 60.0);
            let y_graphics = y_text - 30.0;

            if y_graphics > 50.0 {
                page.text()
                    .set_font(Font::Helvetica, 12.0)
                    .at(50.0, y_text)
                    .write(&format!("Mixed content section {}", i + 1))?;

                page.graphics()
                    .set_fill_color(oxidize_pdf::graphics::Color::rgb(0.8, 0.8, 0.9))
                    .rectangle(50.0, y_graphics, 200.0, 20.0)
                    .fill();
            }
        }
        Ok(())
    }

    let content_types: Vec<(&str, fn(&mut Page) -> Result<()>)> = vec![
        ("text_heavy", setup_text_heavy),
        ("graphics_heavy", setup_graphics_heavy),
        ("mixed_content", setup_mixed_content),
    ];

    for (content_type, content_fn) in content_types {
        let mut doc = Document::new();
        doc.set_title(format!("Memory Test - {content_type}"));

        // Test with memory optimization enabled
        doc.set_compress(true);

        // Create pages with specific content type
        for _page_num in 1..=5 {
            let mut page = Page::a4();
            content_fn(&mut page)?;
            doc.add_page(page);
        }

        // Test memory-efficient operations
        let file_path = temp_dir
            .path()
            .join(format!("memory_content_{content_type}.pdf"));

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

        assert!(file_path.exists());
        let file_size = fs::metadata(&file_path).unwrap().len();

        // Test in-memory generation
        let memory_bytes = doc.to_bytes()?;
        assert!(!memory_bytes.is_empty());

        println!("Content type '{content_type}' test results:");
        println!("  File size: {file_size} bytes");
        println!("  Save duration: {save_duration:?}");
        println!("  Memory size: {} bytes", memory_bytes.len());

        // Verify reasonable performance
        assert!(save_duration.as_secs() < 5);
        assert!(file_size > 1000); // Should have substantial content
        assert!(file_size < 5_000_000); // But not excessive
    }

    Ok(())
}

/// Test memory statistics tracking through workflows
#[test]
fn test_memory_statistics_tracking() -> Result<()> {
    let temp_dir = TempDir::new().unwrap();

    // Create a document and track memory statistics
    let mut doc = Document::new();
    doc.set_title("Memory Statistics Tracking Test");

    // Create memory manager to track statistics
    let memory_options = MemoryOptions::default().with_cache_size(100);
    let manager = MemoryManager::new(memory_options);

    // Simulate memory operations during document creation
    let page_count = 10;
    for page_num in 1..=page_count {
        // Record allocation for page creation
        manager.record_allocation(8192); // Simulate page allocation

        let mut page = Page::a4();
        page.text()
            .set_font(Font::Helvetica, 12.0)
            .at(50.0, 750.0)
            .write(&format!("Statistics tracking page {page_num}"))?;

        // Simulate cache operations
        if page_num % 3 == 0 {
            manager.record_cache_hit();
        } else {
            manager.record_cache_miss();
        }

        doc.add_page(page);
    }

    // Check final statistics
    let final_stats = manager.stats();

    assert_eq!(final_stats.allocated_bytes, 8192 * page_count as usize);
    assert!(final_stats.cache_hits > 0);
    assert!(final_stats.cache_misses > 0);
    assert_eq!(
        final_stats.cache_hits + final_stats.cache_misses,
        page_count as usize
    );

    // Test that document operations still work with statistics tracking
    let stats_path = temp_dir.path().join("memory_statistics.pdf");
    doc.save(&stats_path)?;

    assert!(stats_path.exists());
    let pdf_bytes = doc.to_bytes()?;
    assert!(!pdf_bytes.is_empty());

    println!("Memory statistics tracking test results:");
    println!("  Total allocated: {} bytes", final_stats.allocated_bytes);
    println!("  Cache hits: {}", final_stats.cache_hits);
    println!("  Cache misses: {}", final_stats.cache_misses);
    println!(
        "  Hit ratio: {:.2}%",
        (final_stats.cache_hits as f64
            / (final_stats.cache_hits + final_stats.cache_misses) as f64)
            * 100.0
    );

    Ok(())
}

/// Test memory-efficient batch processing
#[test]
fn test_batch_processing_memory_efficiency() -> Result<()> {
    let temp_dir = TempDir::new().unwrap();

    // Test processing multiple documents efficiently
    let batch_size = 5;
    let mut generated_files = Vec::new();

    for batch_num in 1..=batch_size {
        let mut doc = Document::new();
        doc.set_title(format!("Batch Document {batch_num}"));
        doc.set_compress(true); // Enable compression for memory efficiency

        // Add content proportional to batch number
        for page_num in 1..=batch_num {
            let mut page = Page::a4();

            page.text()
                .set_font(Font::Helvetica, 12.0)
                .at(50.0, 750.0)
                .write(&format!("Batch {batch_num} - Page {page_num}"))?;

            // Add scaling content
            for line in 0..(batch_num * 5) {
                let y_pos = 700.0 - (line as f64 * 12.0);
                if y_pos > 50.0 {
                    page.text()
                        .set_font(Font::Helvetica, 10.0)
                        .at(50.0, y_pos)
                        .write(&format!("Batch content line {}", line + 1))?;
                }
            }

            doc.add_page(page);
        }

        // Save efficiently
        let batch_path = temp_dir.path().join(format!("batch_{batch_num}.pdf"));
        let start_time = std::time::Instant::now();
        doc.save(&batch_path)?;
        let save_duration = start_time.elapsed();

        assert!(batch_path.exists());
        let file_size = fs::metadata(&batch_path).unwrap().len();

        generated_files.push((batch_path, file_size, save_duration));

        // Verify reasonable performance scaling
        assert!(save_duration.as_secs() < 3); // Each document should save quickly
        assert!(file_size > (batch_num as u64 * 500)); // Size should scale with content
    }

    // Verify batch processing was efficient
    println!("Batch processing results:");
    for (i, (path, size, duration)) in generated_files.iter().enumerate() {
        println!("  Batch {}: {} bytes in {:?}", i + 1, size, duration);
        assert!(path.exists());
    }

    // Test memory cleanup between batches (implicit in Rust)
    println!("Batch processing memory efficiency test completed successfully");

    Ok(())
}