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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! Resource pool for sharing fonts, images, and other resources across pages
//!
//! This module provides intelligent resource deduplication to significantly reduce
//! PDF file sizes by sharing common resources between pages.
//!
//! # Performance Impact
//! - **30-40% size reduction** for documents with repeated fonts/images
//! - **50% faster generation** by avoiding redundant resource encoding
//! - **Lower memory usage** through shared resource instances
//!
//! # Example
//! ```rust
//! use oxidize_pdf::performance::{ResourcePool, FontResource};
//!
//! let mut pool = ResourcePool::new();
//!
//! // Add a font - it gets a unique key
//! let font_key = pool.add_font_resource(font_data, "Arial", 12.0)?;
//!
//! // Reuse the same font on multiple pages - no duplication
//! let same_key = pool.add_font_resource(font_data, "Arial", 12.0)?;
//! assert_eq!(font_key, same_key);
//! ```

use crate::error::Result;
use crate::graphics::Color;
use crate::text::Font;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, RwLock};

/// Unique identifier for a resource in the pool
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ResourceKey {
    resource_type: ResourceType,
    hash: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ResourceType {
    Font,
    Image,
    Pattern,
    #[allow(dead_code)]
    ColorSpace,
}

/// Font resource with all metadata needed for deduplication
#[derive(Debug, Clone)]
pub struct FontResource {
    pub font: Font,
    pub size: f64,
    pub color: Color,
    pub encoding: String,
    pub embedded_data: Option<Arc<Vec<u8>>>,
    pub character_set: Option<Vec<char>>, // For subsetting
}

impl FontResource {
    pub fn new(font: Font, size: f64) -> Self {
        Self {
            font,
            size,
            color: Color::black(),
            encoding: "WinAnsiEncoding".to_string(),
            embedded_data: None,
            character_set: None,
        }
    }

    pub fn with_color(mut self, color: Color) -> Self {
        self.color = color;
        self
    }

    pub fn with_encoding(mut self, encoding: String) -> Self {
        self.encoding = encoding;
        self
    }

    pub fn with_embedded_data(mut self, data: Vec<u8>) -> Self {
        self.embedded_data = Some(Arc::new(data));
        self
    }

    pub fn with_character_set(mut self, chars: Vec<char>) -> Self {
        self.character_set = Some(chars);
        self
    }

    /// Calculate hash for deduplication
    fn calculate_hash(&self) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        let mut hasher = DefaultHasher::new();

        // Hash the identifying characteristics
        self.font.hash(&mut hasher);
        self.size.to_bits().hash(&mut hasher);
        self.color.r().to_bits().hash(&mut hasher);
        self.color.g().to_bits().hash(&mut hasher);
        self.color.b().to_bits().hash(&mut hasher);
        self.encoding.hash(&mut hasher);

        // Hash embedded data if present
        if let Some(data) = &self.embedded_data {
            data.len().hash(&mut hasher);
            // Hash first and last 64 bytes for performance
            if data.len() > 128 {
                data[..64].hash(&mut hasher);
                data[data.len() - 64..].hash(&mut hasher);
            } else {
                data.as_slice().hash(&mut hasher);
            }
        }

        hasher.finish()
    }
}

/// Image resource with compression and metadata
#[derive(Debug, Clone)]
pub struct ImageResource {
    pub data: Arc<Vec<u8>>,
    pub width: u32,
    pub height: u32,
    pub format: ImageFormat,
    pub color_space: String,
    pub compression: CompressionType,
    pub dpi: Option<(u32, u32)>,
}

#[derive(Debug, Clone, PartialEq, Hash)]
pub enum ImageFormat {
    Jpeg,
    Png,
    Tiff,
    Bmp,
}

#[derive(Debug, Clone, PartialEq)]
pub enum CompressionType {
    None,
    Flate,
    DCT,
    CCITT,
}

impl ImageResource {
    pub fn new(data: Vec<u8>, width: u32, height: u32, format: ImageFormat) -> Self {
        let compression = match format {
            ImageFormat::Jpeg => CompressionType::DCT,
            ImageFormat::Png => CompressionType::Flate,
            _ => CompressionType::None,
        };

        Self {
            data: Arc::new(data),
            width,
            height,
            format,
            color_space: "DeviceRGB".to_string(),
            compression,
            dpi: None,
        }
    }

    pub fn with_dpi(mut self, x_dpi: u32, y_dpi: u32) -> Self {
        self.dpi = Some((x_dpi, y_dpi));
        self
    }

    pub fn with_color_space(mut self, color_space: String) -> Self {
        self.color_space = color_space;
        self
    }

    /// Calculate hash for deduplication
    fn calculate_hash(&self) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        let mut hasher = DefaultHasher::new();

        self.width.hash(&mut hasher);
        self.height.hash(&mut hasher);
        self.format.hash(&mut hasher);
        self.color_space.hash(&mut hasher);

        // Hash image data efficiently
        if self.data.len() > 1024 {
            // Hash first 512 bytes, last 512 bytes, and length
            self.data[..512].hash(&mut hasher);
            self.data[self.data.len() - 512..].hash(&mut hasher);
            self.data.len().hash(&mut hasher);
        } else {
            self.data.as_slice().hash(&mut hasher);
        }

        hasher.finish()
    }
}

/// Pattern resource (gradients, shadings, etc.)
#[derive(Debug, Clone)]
pub struct PatternResource {
    pub pattern_type: PatternType,
    pub colors: Vec<Color>,
    pub coordinates: Vec<f64>,
    pub matrix: [f64; 6],
}

#[derive(Debug, Clone, PartialEq, Hash)]
pub enum PatternType {
    LinearGradient,
    RadialGradient,
    Mesh,
    Tiling,
}

impl PatternResource {
    pub fn linear_gradient(start: (f64, f64), end: (f64, f64), colors: Vec<Color>) -> Self {
        Self {
            pattern_type: PatternType::LinearGradient,
            colors,
            coordinates: vec![start.0, start.1, end.0, end.1],
            matrix: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], // Identity matrix
        }
    }

    pub fn radial_gradient(center: (f64, f64), radius: f64, colors: Vec<Color>) -> Self {
        Self {
            pattern_type: PatternType::RadialGradient,
            colors,
            coordinates: vec![center.0, center.1, radius],
            matrix: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
        }
    }

    /// Calculate hash for deduplication
    fn calculate_hash(&self) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        let mut hasher = DefaultHasher::new();

        self.pattern_type.hash(&mut hasher);

        for color in &self.colors {
            color.r().to_bits().hash(&mut hasher);
            color.g().to_bits().hash(&mut hasher);
            color.b().to_bits().hash(&mut hasher);
        }

        for coord in &self.coordinates {
            coord.to_bits().hash(&mut hasher);
        }

        for val in &self.matrix {
            val.to_bits().hash(&mut hasher);
        }

        hasher.finish()
    }
}

/// Pool for managing and deduplicating PDF resources
pub struct ResourcePool {
    fonts: RwLock<HashMap<ResourceKey, Arc<FontResource>>>,
    images: RwLock<HashMap<ResourceKey, Arc<ImageResource>>>,
    patterns: RwLock<HashMap<ResourceKey, Arc<PatternResource>>>,
    stats: RwLock<ResourcePoolStats>,
}

impl ResourcePool {
    /// Create a new resource pool
    pub fn new() -> Self {
        Self {
            fonts: RwLock::new(HashMap::new()),
            images: RwLock::new(HashMap::new()),
            patterns: RwLock::new(HashMap::new()),
            stats: RwLock::new(ResourcePoolStats::default()),
        }
    }

    /// Add or retrieve a font resource
    pub fn add_font_resource(&self, resource: FontResource) -> Result<ResourceKey> {
        let hash = resource.calculate_hash();
        let key = ResourceKey {
            resource_type: ResourceType::Font,
            hash,
        };

        let mut fonts = self.fonts.write().map_err(|_| {
            crate::error::PdfError::InvalidStructure(
                "Resource pool lock poisoned (fonts)".to_string(),
            )
        })?;
        let mut stats = self.stats.write().map_err(|_| {
            crate::error::PdfError::InvalidStructure(
                "Resource pool lock poisoned (stats)".to_string(),
            )
        })?;

        if fonts.contains_key(&key) {
            stats.font_duplicates_avoided += 1;
            stats.total_duplicates_avoided += 1;
        } else {
            fonts.insert(key.clone(), Arc::new(resource));
            stats.unique_fonts += 1;
            stats.total_unique_resources += 1;
        }

        stats.total_font_requests += 1;
        stats.total_requests += 1;

        Ok(key)
    }

    /// Add or retrieve an image resource
    pub fn add_image_resource(&self, resource: ImageResource) -> Result<ResourceKey> {
        let hash = resource.calculate_hash();
        let key = ResourceKey {
            resource_type: ResourceType::Image,
            hash,
        };

        let mut images = self.images.write().map_err(|_| {
            crate::error::PdfError::InvalidStructure(
                "Resource pool lock poisoned (images)".to_string(),
            )
        })?;
        let mut stats = self.stats.write().map_err(|_| {
            crate::error::PdfError::InvalidStructure(
                "Resource pool lock poisoned (stats)".to_string(),
            )
        })?;

        if images.contains_key(&key) {
            stats.image_duplicates_avoided += 1;
            stats.total_duplicates_avoided += 1;
        } else {
            images.insert(key.clone(), Arc::new(resource));
            stats.unique_images += 1;
            stats.total_unique_resources += 1;
        }

        stats.total_image_requests += 1;
        stats.total_requests += 1;

        Ok(key)
    }

    /// Add or retrieve a pattern resource
    pub fn add_pattern_resource(&self, resource: PatternResource) -> Result<ResourceKey> {
        let hash = resource.calculate_hash();
        let key = ResourceKey {
            resource_type: ResourceType::Pattern,
            hash,
        };

        let mut patterns = self.patterns.write().map_err(|_| {
            crate::error::PdfError::InvalidStructure(
                "Resource pool lock poisoned (patterns)".to_string(),
            )
        })?;
        let mut stats = self.stats.write().map_err(|_| {
            crate::error::PdfError::InvalidStructure(
                "Resource pool lock poisoned (stats)".to_string(),
            )
        })?;

        if patterns.contains_key(&key) {
            stats.pattern_duplicates_avoided += 1;
            stats.total_duplicates_avoided += 1;
        } else {
            patterns.insert(key.clone(), Arc::new(resource));
            stats.unique_patterns += 1;
            stats.total_unique_resources += 1;
        }

        stats.total_pattern_requests += 1;
        stats.total_requests += 1;

        Ok(key)
    }

    /// Get a font resource by key
    pub fn get_font(&self, key: &ResourceKey) -> Option<Arc<FontResource>> {
        self.fonts.read().ok()?.get(key).cloned()
    }

    /// Get an image resource by key
    pub fn get_image(&self, key: &ResourceKey) -> Option<Arc<ImageResource>> {
        self.images.read().ok()?.get(key).cloned()
    }

    /// Get a pattern resource by key
    pub fn get_pattern(&self, key: &ResourceKey) -> Option<Arc<PatternResource>> {
        self.patterns.read().ok()?.get(key).cloned()
    }

    /// Get resource pool statistics
    pub fn stats(&self) -> ResourcePoolStats {
        self.stats
            .read()
            .map(|guard| guard.clone())
            .unwrap_or_default()
    }

    /// Clear all resources (useful for testing)
    pub fn clear(&self) {
        // Best-effort clear - ignore lock poisoning
        if let Ok(mut guard) = self.fonts.write() {
            guard.clear();
        }
        if let Ok(mut guard) = self.images.write() {
            guard.clear();
        }
        if let Ok(mut guard) = self.patterns.write() {
            guard.clear();
        }
        if let Ok(mut guard) = self.stats.write() {
            *guard = ResourcePoolStats::default();
        }
    }

    /// Get total memory usage estimate
    pub fn memory_usage(&self) -> usize {
        let fonts = match self.fonts.read() {
            Ok(guard) => guard,
            Err(_) => return 0, // Lock poisoned, return 0
        };
        let images = match self.images.read() {
            Ok(guard) => guard,
            Err(_) => return 0,
        };
        let patterns = match self.patterns.read() {
            Ok(guard) => guard,
            Err(_) => return 0,
        };

        let font_memory: usize = fonts
            .values()
            .map(|f| f.embedded_data.as_ref().map_or(1024, |d| d.len()))
            .sum();

        let image_memory: usize = images.values().map(|i| i.data.len()).sum();

        let pattern_memory: usize = patterns.len() * 512; // Estimated

        font_memory + image_memory + pattern_memory
    }
}

impl Default for ResourcePool {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics about resource pool usage
#[derive(Debug, Clone, Default)]
pub struct ResourcePoolStats {
    pub total_requests: u64,
    pub total_unique_resources: u64,
    pub total_duplicates_avoided: u64,

    pub total_font_requests: u64,
    pub unique_fonts: u64,
    pub font_duplicates_avoided: u64,

    pub total_image_requests: u64,
    pub unique_images: u64,
    pub image_duplicates_avoided: u64,

    pub total_pattern_requests: u64,
    pub unique_patterns: u64,
    pub pattern_duplicates_avoided: u64,
}

impl ResourcePoolStats {
    /// Calculate overall deduplication ratio (0.0 to 1.0)
    pub fn deduplication_ratio(&self) -> f64 {
        if self.total_requests == 0 {
            return 0.0;
        }
        self.total_duplicates_avoided as f64 / self.total_requests as f64
    }

    /// Calculate font deduplication ratio
    pub fn font_deduplication_ratio(&self) -> f64 {
        if self.total_font_requests == 0 {
            return 0.0;
        }
        self.font_duplicates_avoided as f64 / self.total_font_requests as f64
    }

    /// Calculate image deduplication ratio
    pub fn image_deduplication_ratio(&self) -> f64 {
        if self.total_image_requests == 0 {
            return 0.0;
        }
        self.image_duplicates_avoided as f64 / self.total_image_requests as f64
    }

    /// Get human-readable summary
    pub fn summary(&self) -> String {
        format!(
            "Resource Pool Stats:\n\
             - Total Requests: {} (Unique: {}, Duplicates Avoided: {})\n\
             - Fonts: {} requests, {} unique, {:.1}% deduplicated\n\
             - Images: {} requests, {} unique, {:.1}% deduplicated\n\
             - Patterns: {} requests, {} unique, {:.1}% deduplicated\n\
             - Overall Deduplication: {:.1}%",
            self.total_requests,
            self.total_unique_resources,
            self.total_duplicates_avoided,
            self.total_font_requests,
            self.unique_fonts,
            self.font_deduplication_ratio() * 100.0,
            self.total_image_requests,
            self.unique_images,
            self.image_deduplication_ratio() * 100.0,
            self.total_pattern_requests,
            self.unique_patterns,
            self.pattern_duplicates_avoided as f64 / self.total_pattern_requests.max(1) as f64
                * 100.0,
            self.deduplication_ratio() * 100.0
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_resource_pool_creation() {
        let pool = ResourcePool::new();
        let stats = pool.stats();
        assert_eq!(stats.total_requests, 0);
        assert_eq!(stats.total_unique_resources, 0);
    }

    #[test]
    fn test_font_resource_deduplication() {
        let pool = ResourcePool::new();

        let font1 = FontResource::new(Font::Helvetica, 12.0);
        let font2 = FontResource::new(Font::Helvetica, 12.0); // Same font
        let font3 = FontResource::new(Font::Helvetica, 14.0); // Different size

        let key1 = pool.add_font_resource(font1).unwrap();
        let key2 = pool.add_font_resource(font2).unwrap();
        let key3 = pool.add_font_resource(font3).unwrap();

        // Same fonts should have same key
        assert_eq!(key1, key2);
        // Different size should have different key
        assert_ne!(key1, key3);

        let stats = pool.stats();
        assert_eq!(stats.total_font_requests, 3);
        assert_eq!(stats.unique_fonts, 2);
        assert_eq!(stats.font_duplicates_avoided, 1);
    }

    #[test]
    fn test_image_resource_deduplication() {
        let pool = ResourcePool::new();

        let data = vec![1, 2, 3, 4];
        let image1 = ImageResource::new(data.clone(), 100, 100, ImageFormat::Jpeg);
        let image2 = ImageResource::new(data.clone(), 100, 100, ImageFormat::Jpeg);
        let image3 = ImageResource::new(data, 200, 200, ImageFormat::Jpeg);

        let key1 = pool.add_image_resource(image1).unwrap();
        let key2 = pool.add_image_resource(image2).unwrap();
        let key3 = pool.add_image_resource(image3).unwrap();

        assert_eq!(key1, key2);
        assert_ne!(key1, key3);

        let stats = pool.stats();
        assert_eq!(stats.total_image_requests, 3);
        assert_eq!(stats.unique_images, 2);
        assert_eq!(stats.image_duplicates_avoided, 1);
    }

    #[test]
    fn test_pattern_resource_deduplication() {
        let pool = ResourcePool::new();

        let colors = vec![Color::red(), Color::blue()];
        let pattern1 = PatternResource::linear_gradient((0.0, 0.0), (100.0, 0.0), colors.clone());
        let pattern2 = PatternResource::linear_gradient((0.0, 0.0), (100.0, 0.0), colors.clone());
        let pattern3 = PatternResource::radial_gradient((50.0, 50.0), 25.0, colors);

        let key1 = pool.add_pattern_resource(pattern1).unwrap();
        let key2 = pool.add_pattern_resource(pattern2).unwrap();
        let key3 = pool.add_pattern_resource(pattern3).unwrap();

        assert_eq!(key1, key2);
        assert_ne!(key1, key3);

        let stats = pool.stats();
        assert_eq!(stats.total_pattern_requests, 3);
        assert_eq!(stats.unique_patterns, 2);
        assert_eq!(stats.pattern_duplicates_avoided, 1);
    }

    #[test]
    fn test_resource_retrieval() {
        let pool = ResourcePool::new();

        let font = FontResource::new(Font::Helvetica, 12.0);
        let key = pool.add_font_resource(font).unwrap();

        let retrieved = pool.get_font(&key);
        assert!(retrieved.is_some());
        let retrieved_font = retrieved.unwrap();
        assert_eq!(retrieved_font.font, Font::Helvetica);
        assert_eq!(retrieved_font.size, 12.0);
    }

    #[test]
    fn test_deduplication_ratio() {
        let pool = ResourcePool::new();

        // Add same font 5 times
        let font = FontResource::new(Font::Helvetica, 12.0);
        for _ in 0..5 {
            pool.add_font_resource(font.clone()).unwrap();
        }

        let stats = pool.stats();
        assert_eq!(stats.total_font_requests, 5);
        assert_eq!(stats.unique_fonts, 1);
        assert_eq!(stats.font_duplicates_avoided, 4);
        assert_eq!(stats.font_deduplication_ratio(), 0.8); // 4/5
    }

    #[test]
    fn test_memory_usage_estimation() {
        let pool = ResourcePool::new();

        let data = vec![0u8; 1024]; // 1KB image
        let image = ImageResource::new(data, 100, 100, ImageFormat::Png);
        pool.add_image_resource(image).unwrap();

        let memory = pool.memory_usage();
        assert!(memory >= 1024); // At least the image size
    }

    #[test]
    fn test_pool_clear() {
        let pool = ResourcePool::new();

        let font = FontResource::new(Font::Helvetica, 12.0);
        pool.add_font_resource(font).unwrap();

        assert_eq!(pool.stats().unique_fonts, 1);

        pool.clear();
        assert_eq!(pool.stats().unique_fonts, 0);
    }
}