pdf_oxide 0.3.28

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
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
//! Document complexity estimation for hybrid ML/classical routing.
//!
//! This module analyzes PDF pages to determine their layout complexity,
//! which is used to decide whether to use classical algorithms (fast)
//! or ML models (accurate but slower).

use crate::layout::text_block::TextBlock;

/// Page complexity classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Complexity {
    /// Simple single-column layout with uniform formatting.
    /// Use classical algorithms (fastest).
    Simple,

    /// Moderate complexity with some multi-column or varied formatting.
    /// Either approach works well.
    Moderate,

    /// Complex multi-column, irregular, or heavily formatted layout.
    /// ML models recommended for best accuracy.
    Complex,
}

/// Estimates document complexity to route between classical and ML approaches.
///
/// # Algorithm
///
/// Analyzes multiple factors weighted by importance:
/// - Column count (30%): More columns = more complex
/// - Font diversity (20%): More unique fonts = more complex
/// - Y-position variance (20%): Higher variance = irregular layout
/// - Block size variance (15%): Varied sizes = complex formatting
/// - Density (15%): Very sparse or dense = complex
///
/// # Example
///
/// ```ignore
/// use pdf_oxide::hybrid::ComplexityEstimator;
/// # use pdf_oxide::layout::text_block::TextBlock;
/// # use pdf_oxide::geometry::Rect;
/// # use pdf_oxide::layout::text_block::{TextChar, FontWeight, Color};
/// #
/// # fn create_block(x: f32, y: f32) -> TextBlock {
/// #     let char_data = TextChar {
/// #         char: 'A',
/// #         bbox: Rect { x, y, width: 10.0, height: 10.0 },
/// #         font_name: "Arial".to_string(),
/// #         font_size: 12.0,
/// #         font_weight: FontWeight::Normal,
/// #         color: Color::black(),
/// #     };
/// #     TextBlock {
/// #         chars: vec![char_data],
/// #         bbox: Rect { x, y, width: 100.0, height: 20.0 },
/// #         text: "Test".to_string(),
/// #         avg_font_size: 12.0,
/// #         dominant_font: "Arial".to_string(),
/// #         is_bold: false,
/// #     }
/// # }
///
/// let blocks = vec![create_block(0.0, 0.0), create_block(0.0, 30.0)];
/// let complexity = ComplexityEstimator::estimate_page_complexity(&blocks, 612.0, 792.0);
/// ```
pub struct ComplexityEstimator;

impl ComplexityEstimator {
    /// Estimate page complexity based on text block analysis.
    ///
    /// # Arguments
    ///
    /// * `blocks` - Text blocks on the page
    /// * `page_width` - Width of the page in points
    /// * `page_height` - Height of the page in points
    ///
    /// # Returns
    ///
    /// Returns a Complexity classification:
    /// - Simple: Score < 0.3
    /// - Moderate: Score 0.3 - 0.6
    /// - Complex: Score > 0.6
    pub fn estimate_page_complexity(
        blocks: &[TextBlock],
        page_width: f32,
        page_height: f32,
    ) -> Complexity {
        let score = Self::calculate_complexity_score(blocks, page_width, page_height);

        if score < 0.3 {
            Complexity::Simple
        } else if score < 0.6 {
            Complexity::Moderate
        } else {
            Complexity::Complex
        }
    }

    /// Calculate numeric complexity score in [0, 1].
    ///
    /// # Arguments
    ///
    /// * `blocks` - Text blocks on the page
    /// * `page_width` - Width of the page in points
    /// * `page_height` - Height of the page in points
    ///
    /// # Returns
    ///
    /// Returns a score in [0, 1] where higher values indicate more complexity.
    pub fn calculate_complexity_score(
        blocks: &[TextBlock],
        page_width: f32,
        page_height: f32,
    ) -> f32 {
        if blocks.is_empty() {
            return 0.0;
        }

        let mut score = 0.0;

        // Factor 1: Column detection (30% weight)
        // More columns = more complex
        let columns = Self::estimate_columns(blocks, page_width);
        score += (columns.saturating_sub(1) as f32 * 0.15).min(0.3);

        // Factor 2: Font diversity (20% weight)
        // More unique fonts = more complex typography
        let unique_fonts = Self::count_unique_fonts(blocks);
        score += (unique_fonts.saturating_sub(2) as f32 * 0.05).min(0.2);

        // Factor 3: Y-position variance (20% weight)
        // Higher variance = irregular layout
        let y_variance = Self::calculate_y_variance(blocks, page_height);
        score += y_variance.min(0.2);

        // Factor 4: Block size variance (15% weight)
        // Varied block sizes = complex formatting
        let size_variance = Self::calculate_size_variance(blocks);
        score += size_variance.min(0.15);

        // Factor 5: Density (15% weight)
        // Very sparse or very dense layouts are complex
        let density = Self::calculate_density(blocks, page_width, page_height);
        if !(0.2..=0.8).contains(&density) {
            score += 0.15; // Extreme densities add complexity
        }

        score.min(1.0)
    }

    /// Estimate number of columns using X-position clustering.
    fn estimate_columns(blocks: &[TextBlock], page_width: f32) -> usize {
        if blocks.is_empty() {
            return 0;
        }

        // Collect and sort X positions
        let mut x_positions: Vec<f32> = blocks.iter().map(|b| b.bbox.x).collect();
        x_positions.sort_by(|a, b| crate::utils::safe_float_cmp(*a, *b));

        // Count gaps larger than 20% of page width as column separators
        let mut columns = 1;
        let threshold = page_width * 0.2;

        for window in x_positions.windows(2) {
            if (window[1] - window[0]) > threshold {
                columns += 1;
            }
        }

        columns.min(4) // Cap at 4 columns
    }

    /// Count unique fonts in blocks.
    fn count_unique_fonts(blocks: &[TextBlock]) -> usize {
        let mut fonts: Vec<&str> = blocks.iter().map(|b| b.dominant_font.as_str()).collect();
        fonts.sort_unstable();
        fonts.dedup();
        fonts.len()
    }

    /// Calculate normalized Y-position variance.
    ///
    /// Higher variance indicates blocks are spread irregularly across the page.
    fn calculate_y_variance(blocks: &[TextBlock], page_height: f32) -> f32 {
        if blocks.is_empty() {
            return 0.0;
        }

        let mean_y: f32 = blocks.iter().map(|b| b.bbox.y).sum::<f32>() / blocks.len() as f32;
        let variance: f32 = blocks
            .iter()
            .map(|b| (b.bbox.y - mean_y).powi(2))
            .sum::<f32>()
            / blocks.len() as f32;

        // Normalize by page height
        (variance.sqrt() / page_height).min(1.0)
    }

    /// Calculate coefficient of variation for block sizes.
    ///
    /// Higher values indicate varied block sizes (complex formatting).
    fn calculate_size_variance(blocks: &[TextBlock]) -> f32 {
        if blocks.is_empty() {
            return 0.0;
        }

        let mean_size: f32 =
            blocks.iter().map(|b| b.avg_font_size).sum::<f32>() / blocks.len() as f32;

        if mean_size == 0.0 {
            return 0.0;
        }

        let variance: f32 = blocks
            .iter()
            .map(|b| (b.avg_font_size - mean_size).powi(2))
            .sum::<f32>()
            / blocks.len() as f32;

        // Coefficient of variation (normalized standard deviation)
        (variance.sqrt() / mean_size).min(1.0)
    }

    /// Calculate text density (coverage of page).
    ///
    /// Extreme densities (very sparse or very dense) indicate complex layouts.
    fn calculate_density(blocks: &[TextBlock], page_width: f32, page_height: f32) -> f32 {
        if page_width == 0.0 || page_height == 0.0 {
            return 0.0;
        }

        let total_area: f32 = blocks.iter().map(|b| b.bbox.width * b.bbox.height).sum();

        let page_area = page_width * page_height;
        (total_area / page_area).min(1.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geometry::Rect;
    use crate::layout::text_block::{Color, FontWeight, TextBlock, TextChar};

    fn create_test_block(
        x: f32,
        y: f32,
        width: f32,
        height: f32,
        font_size: f32,
        font: &str,
    ) -> TextBlock {
        let bbox = Rect {
            x,
            y,
            width: 10.0,
            height: 10.0,
        };
        let char_data = TextChar {
            char: 'A',
            bbox,
            font_name: font.to_string(),
            font_size,
            font_weight: FontWeight::Normal,
            is_italic: false,
            is_monospace: false,
            color: Color::black(),
            mcid: None,
            origin_x: bbox.x,
            origin_y: bbox.y,
            rotation_degrees: 0.0,
            advance_width: bbox.width,
            matrix: None,
        };

        TextBlock {
            chars: vec![char_data],
            bbox: Rect {
                x,
                y,
                width,
                height,
            },
            text: "Test".to_string(),
            avg_font_size: font_size,
            dominant_font: font.to_string(),
            is_bold: false,
            is_italic: false,
            mcid: None,
        }
    }

    #[test]
    fn test_simple_layout() {
        // Single column, uniform font, regular spacing
        let blocks = vec![
            create_test_block(50.0, 100.0, 500.0, 20.0, 12.0, "Arial"),
            create_test_block(50.0, 130.0, 500.0, 20.0, 12.0, "Arial"),
            create_test_block(50.0, 160.0, 500.0, 20.0, 12.0, "Arial"),
        ];

        let complexity = ComplexityEstimator::estimate_page_complexity(&blocks, 612.0, 792.0);
        assert_eq!(complexity, Complexity::Simple);
    }

    #[test]
    fn test_multi_column_layout() {
        // Two columns
        let blocks = vec![
            create_test_block(50.0, 100.0, 200.0, 20.0, 12.0, "Arial"),
            create_test_block(350.0, 100.0, 200.0, 20.0, 12.0, "Arial"),
            create_test_block(50.0, 130.0, 200.0, 20.0, 12.0, "Arial"),
            create_test_block(350.0, 130.0, 200.0, 20.0, 12.0, "Arial"),
        ];

        let complexity = ComplexityEstimator::estimate_page_complexity(&blocks, 612.0, 792.0);
        assert!(complexity >= Complexity::Moderate);
    }

    #[test]
    fn test_mixed_fonts() {
        // Multiple fonts
        let blocks = vec![
            create_test_block(50.0, 100.0, 500.0, 20.0, 12.0, "Arial"),
            create_test_block(50.0, 130.0, 500.0, 20.0, 14.0, "Times"),
            create_test_block(50.0, 160.0, 500.0, 20.0, 10.0, "Courier"),
            create_test_block(50.0, 190.0, 500.0, 20.0, 16.0, "Helvetica"),
        ];

        let complexity = ComplexityEstimator::estimate_page_complexity(&blocks, 612.0, 792.0);
        assert!(complexity >= Complexity::Moderate);
    }

    #[test]
    fn test_irregular_layout() {
        // Irregular Y positions and varied sizes
        let blocks = vec![
            create_test_block(50.0, 100.0, 500.0, 20.0, 24.0, "Arial"),
            create_test_block(100.0, 300.0, 400.0, 15.0, 12.0, "Times"), // Different font
            create_test_block(50.0, 600.0, 300.0, 10.0, 8.0, "Courier"), // Different font
        ];

        let complexity = ComplexityEstimator::estimate_page_complexity(&blocks, 612.0, 792.0);
        // Should be at least Moderate due to irregular layout and font diversity
        assert!(complexity >= Complexity::Moderate);
    }

    #[test]
    fn test_empty_page() {
        let blocks: Vec<TextBlock> = vec![];
        let complexity = ComplexityEstimator::estimate_page_complexity(&blocks, 612.0, 792.0);
        assert_eq!(complexity, Complexity::Simple);
    }

    #[test]
    fn test_estimate_columns() {
        // Single column
        let single_col = vec![
            create_test_block(50.0, 100.0, 500.0, 20.0, 12.0, "Arial"),
            create_test_block(50.0, 130.0, 500.0, 20.0, 12.0, "Arial"),
        ];
        assert_eq!(ComplexityEstimator::estimate_columns(&single_col, 612.0), 1);

        // Two columns (gap > 20% of page width = 122.4pt)
        let two_col = vec![
            create_test_block(50.0, 100.0, 200.0, 20.0, 12.0, "Arial"),
            create_test_block(350.0, 100.0, 200.0, 20.0, 12.0, "Arial"),
        ];
        assert_eq!(ComplexityEstimator::estimate_columns(&two_col, 612.0), 2);
    }

    #[test]
    fn test_count_unique_fonts() {
        let blocks = vec![
            create_test_block(0.0, 0.0, 100.0, 20.0, 12.0, "Arial"),
            create_test_block(0.0, 0.0, 100.0, 20.0, 12.0, "Arial"),
            create_test_block(0.0, 0.0, 100.0, 20.0, 12.0, "Times"),
            create_test_block(0.0, 0.0, 100.0, 20.0, 12.0, "Courier"),
        ];

        assert_eq!(ComplexityEstimator::count_unique_fonts(&blocks), 3);
    }

    #[test]
    fn test_density_calculation() {
        // Sparse layout
        let sparse = vec![create_test_block(0.0, 0.0, 50.0, 20.0, 12.0, "Arial")];
        let density = ComplexityEstimator::calculate_density(&sparse, 612.0, 792.0);
        assert!(density < 0.01); // Very low density

        // Dense layout
        let dense = vec![create_test_block(0.0, 0.0, 600.0, 700.0, 12.0, "Arial")];
        let density = ComplexityEstimator::calculate_density(&dense, 612.0, 792.0);
        assert!(density > 0.8); // High density
    }

    #[test]
    fn test_complexity_ordering() {
        assert!(Complexity::Simple < Complexity::Moderate);
        assert!(Complexity::Moderate < Complexity::Complex);
        assert!(Complexity::Simple < Complexity::Complex);
    }

    #[test]
    fn test_complexity_score_range() {
        let blocks = vec![
            create_test_block(0.0, 0.0, 100.0, 20.0, 12.0, "Arial"),
            create_test_block(0.0, 30.0, 100.0, 20.0, 12.0, "Arial"),
        ];

        let score = ComplexityEstimator::calculate_complexity_score(&blocks, 612.0, 792.0);

        // Score should always be in [0, 1]
        assert!(score >= 0.0);
        assert!(score <= 1.0);
    }
}