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
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
//! Synthetic structure generation for untagged PDFs.
//!
//! This module creates hierarchical document structures for PDFs that lack
//! explicit structure trees (untagged PDFs) using geometric analysis and clustering.
//!
//! The synthetic structure uses:
//! - **Document** as root
//! - **Sections** detected via heading sizes
//! - **Paragraphs** grouped by vertical proximity
//! - **Individual content** (text, images) as leaf elements

use crate::elements::{ContentElement, StructureElement};
use crate::error::Result;
use crate::geometry::Rect;

/// Configuration for synthetic structure generation.
#[derive(Debug, Clone)]
pub struct SyntheticStructureConfig {
    /// Vertical gap threshold for grouping into paragraphs (in points)
    pub paragraph_gap_threshold: f32,

    /// Font size threshold multiplier for heading detection
    /// (heading if size > avg_size * multiplier)
    pub heading_size_multiplier: f32,

    /// Minimum vertical distance to start a new section
    pub section_break_threshold: f32,
}

impl Default for SyntheticStructureConfig {
    fn default() -> Self {
        Self {
            paragraph_gap_threshold: 4.0,  // ~4 points
            heading_size_multiplier: 1.3,  // 30% larger than average
            section_break_threshold: 50.0, // ~50 points
        }
    }
}

/// Generates synthetic hierarchical structure for untagged PDFs.
pub struct SyntheticStructureGenerator {
    config: SyntheticStructureConfig,
}

impl SyntheticStructureGenerator {
    /// Create a new synthetic structure generator with default configuration.
    pub fn new() -> Self {
        Self {
            config: SyntheticStructureConfig::default(),
        }
    }

    /// Create with custom configuration.
    pub fn with_config(config: SyntheticStructureConfig) -> Self {
        Self { config }
    }

    /// Generate synthetic document structure.
    ///
    /// # Arguments
    ///
    /// * `content_elements` - Extracted page content in reading order
    /// * `page_bbox` - Bounding box of the page
    ///
    /// # Returns
    ///
    /// A StructureElement with synthetic Document hierarchy
    ///
    /// # Algorithm
    ///
    /// 1. Analyze content positioning and styling
    /// 2. Detect section breaks based on large gaps
    /// 3. Detect headings based on font size
    /// 4. Group paragraphs based on proximity
    /// 5. Build hierarchical structure: Document → Sections → Paragraphs → Content
    pub fn generate(
        &self,
        content_elements: &[ContentElement],
        page_bbox: Rect,
    ) -> Result<StructureElement> {
        if content_elements.is_empty() {
            return Ok(StructureElement {
                structure_type: "Document".to_string(),
                bbox: page_bbox,
                children: Vec::new(),
                reading_order: Some(0),
                alt_text: None,
                language: None,
            });
        }

        // Step 1: Group content into paragraphs based on proximity
        let paragraphs = self.group_into_paragraphs(content_elements);

        // Step 2: Detect headings and section breaks
        let sections = self.group_into_sections(&paragraphs);

        // Step 3: Build hierarchical structure
        let children = sections
            .into_iter()
            .map(ContentElement::Structure)
            .collect();

        Ok(StructureElement {
            structure_type: "Document".to_string(),
            bbox: page_bbox,
            children,
            reading_order: Some(0),
            alt_text: None,
            language: None,
        })
    }

    /// Group content elements into paragraphs based on vertical proximity.
    fn group_into_paragraphs(&self, elements: &[ContentElement]) -> Vec<StructureElement> {
        let mut paragraphs = Vec::new();
        let mut current_paragraph: Vec<ContentElement> = Vec::new();
        let mut last_y_end = f32::MAX;

        for element in elements {
            let bbox = element.bbox();

            // Calculate gap from last element
            let gap = if last_y_end != f32::MAX {
                (last_y_end - bbox.y).abs()
            } else {
                0.0
            };

            // If gap is too large, start a new paragraph
            if gap > self.config.paragraph_gap_threshold && !current_paragraph.is_empty() {
                paragraphs.push(self.create_paragraph(std::mem::take(&mut current_paragraph)));
            }

            current_paragraph.push(element.clone());
            last_y_end = bbox.y;
        }

        // Add final paragraph
        if !current_paragraph.is_empty() {
            paragraphs.push(self.create_paragraph(current_paragraph));
        }

        paragraphs
    }

    /// Group paragraphs into sections based on heading detection.
    fn group_into_sections(&self, paragraphs: &[StructureElement]) -> Vec<StructureElement> {
        let mut sections = Vec::new();
        let mut current_section: Vec<StructureElement> = Vec::new();

        for paragraph in paragraphs {
            // Check if this paragraph represents a heading
            if self.is_heading_paragraph(paragraph) {
                // Start new section
                if !current_section.is_empty() {
                    sections.push(self.create_section(std::mem::take(&mut current_section)));
                }
                // Add heading as first element of new section
                current_section.push(paragraph.clone());
            } else {
                current_section.push(paragraph.clone());
            }
        }

        // Add final section
        if !current_section.is_empty() {
            sections.push(self.create_section(current_section));
        }

        sections
    }

    /// Create a paragraph structure element.
    fn create_paragraph(&self, children: Vec<ContentElement>) -> StructureElement {
        let bbox = Self::calculate_bbox(&children);

        StructureElement {
            structure_type: "P".to_string(),
            bbox,
            children,
            reading_order: None,
            alt_text: None,
            language: None,
        }
    }

    /// Create a section structure element.
    fn create_section(&self, children: Vec<StructureElement>) -> StructureElement {
        let bbox =
            Self::calculate_struct_bbox(&children.iter().map(|s| s.bbox).collect::<Vec<_>>());

        let children_as_content: Vec<ContentElement> = children
            .into_iter()
            .map(ContentElement::Structure)
            .collect();

        StructureElement {
            structure_type: "Sect".to_string(),
            bbox,
            children: children_as_content,
            reading_order: None,
            alt_text: None,
            language: None,
        }
    }

    /// Check if a paragraph represents a heading.
    ///
    /// A simple heuristic: if the paragraph contains only one text element
    /// with significantly larger font size than others, it's a heading.
    fn is_heading_paragraph(&self, _paragraph: &StructureElement) -> bool {
        // This is a placeholder - full implementation would analyze text styling
        // For now, return false (all paragraphs are treated as body text)
        false
    }

    /// Calculate bounding box from content elements.
    fn calculate_bbox(elements: &[ContentElement]) -> Rect {
        if elements.is_empty() {
            return Rect::new(0.0, 0.0, 0.0, 0.0);
        }

        let mut min_x = f32::MAX;
        let mut min_y = f32::MAX;
        let mut max_x = f32::MIN;
        let mut max_y = f32::MIN;

        for element in elements {
            let bbox = element.bbox();
            min_x = min_x.min(bbox.x);
            min_y = min_y.min(bbox.y);
            max_x = max_x.max(bbox.x + bbox.width);
            max_y = max_y.max(bbox.y + bbox.height);
        }

        if min_x == f32::MAX {
            Rect::new(0.0, 0.0, 0.0, 0.0)
        } else {
            Rect::new(min_x, min_y, max_x - min_x, max_y - min_y)
        }
    }

    /// Calculate bounding box from structure element rects.
    fn calculate_struct_bbox(rects: &[Rect]) -> Rect {
        if rects.is_empty() {
            return Rect::new(0.0, 0.0, 0.0, 0.0);
        }

        let mut min_x = f32::MAX;
        let mut min_y = f32::MAX;
        let mut max_x = f32::MIN;
        let mut max_y = f32::MIN;

        for bbox in rects {
            min_x = min_x.min(bbox.x);
            min_y = min_y.min(bbox.y);
            max_x = max_x.max(bbox.x + bbox.width);
            max_y = max_y.max(bbox.y + bbox.height);
        }

        if min_x == f32::MAX {
            Rect::new(0.0, 0.0, 0.0, 0.0)
        } else {
            Rect::new(min_x, min_y, max_x - min_x, max_y - min_y)
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::elements::{FontSpec, TextContent, TextStyle};

    fn make_text_element(text: &str, x: f32, y: f32, w: f32, h: f32) -> ContentElement {
        ContentElement::Text(TextContent::new(
            text,
            Rect::new(x, y, w, h),
            FontSpec::default(),
            TextStyle::default(),
        ))
    }

    #[test]
    fn test_generator_creation() {
        let _generator = SyntheticStructureGenerator::new();
    }

    #[test]
    fn test_generator_default() {
        let gen = SyntheticStructureGenerator::default();
        let page_bbox = Rect::new(0.0, 0.0, 595.0, 842.0);
        let result = gen.generate(&[], page_bbox).unwrap();
        assert_eq!(result.structure_type, "Document");
    }

    #[test]
    fn test_empty_content() {
        let generator = SyntheticStructureGenerator::new();
        let page_bbox = Rect::new(0.0, 0.0, 595.0, 842.0);
        let result = generator.generate(&[], page_bbox).unwrap();

        assert_eq!(result.structure_type, "Document");
        assert!(result.children.is_empty());
        assert_eq!(result.reading_order, Some(0));
        assert!(result.alt_text.is_none());
        assert!(result.language.is_none());
    }

    #[test]
    fn test_config_defaults() {
        let config = SyntheticStructureConfig::default();
        assert_eq!(config.paragraph_gap_threshold, 4.0);
        assert_eq!(config.heading_size_multiplier, 1.3);
        assert_eq!(config.section_break_threshold, 50.0);
    }

    #[test]
    fn test_custom_config() {
        let config = SyntheticStructureConfig {
            paragraph_gap_threshold: 10.0,
            heading_size_multiplier: 2.0,
            section_break_threshold: 100.0,
        };
        let gen = SyntheticStructureGenerator::with_config(config);
        // Just verify it can be created and used
        let page_bbox = Rect::new(0.0, 0.0, 595.0, 842.0);
        let result = gen.generate(&[], page_bbox).unwrap();
        assert_eq!(result.structure_type, "Document");
    }

    #[test]
    fn test_config_debug_clone() {
        let config = SyntheticStructureConfig::default();
        let cloned = config.clone();
        assert_eq!(cloned.paragraph_gap_threshold, 4.0);
        let debug = format!("{:?}", config);
        assert!(debug.contains("SyntheticStructureConfig"));
    }

    #[test]
    fn test_single_element() {
        let generator = SyntheticStructureGenerator::new();
        let page_bbox = Rect::new(0.0, 0.0, 595.0, 842.0);
        let elements = vec![make_text_element("Hello", 10.0, 100.0, 50.0, 12.0)];
        let result = generator.generate(&elements, page_bbox).unwrap();

        assert_eq!(result.structure_type, "Document");
        assert!(!result.children.is_empty());
    }

    #[test]
    fn test_close_elements_grouped_into_paragraph() {
        let generator = SyntheticStructureGenerator::new();
        let page_bbox = Rect::new(0.0, 0.0, 595.0, 842.0);
        // Two elements very close together (gap < 4.0)
        let elements = vec![
            make_text_element("Line 1", 10.0, 100.0, 200.0, 12.0),
            make_text_element("Line 2", 10.0, 101.0, 200.0, 12.0),
        ];
        let result = generator.generate(&elements, page_bbox).unwrap();
        assert_eq!(result.structure_type, "Document");
        // Both should be in same paragraph -> 1 section -> 1 paragraph
        // Document -> 1 Sect -> 1 P with 2 children
    }

    #[test]
    fn test_distant_elements_separate_paragraphs() {
        let generator = SyntheticStructureGenerator::new();
        let page_bbox = Rect::new(0.0, 0.0, 595.0, 842.0);
        // Two elements far apart (gap > 4.0)
        let elements = vec![
            make_text_element("Paragraph 1", 10.0, 100.0, 200.0, 12.0),
            make_text_element("Paragraph 2", 10.0, 200.0, 200.0, 12.0),
        ];
        let result = generator.generate(&elements, page_bbox).unwrap();
        assert_eq!(result.structure_type, "Document");
        // Should have at least 1 section child
        assert!(!result.children.is_empty());
    }

    #[test]
    fn test_calculate_bbox_empty() {
        let bbox = SyntheticStructureGenerator::calculate_bbox(&[]);
        assert_eq!(bbox.x, 0.0);
        assert_eq!(bbox.y, 0.0);
        assert_eq!(bbox.width, 0.0);
        assert_eq!(bbox.height, 0.0);
    }

    #[test]
    fn test_calculate_bbox_single_element() {
        let elements = vec![make_text_element("Test", 10.0, 20.0, 100.0, 12.0)];
        let bbox = SyntheticStructureGenerator::calculate_bbox(&elements);
        assert_eq!(bbox.x, 10.0);
        assert_eq!(bbox.y, 20.0);
        assert_eq!(bbox.width, 100.0);
        assert_eq!(bbox.height, 12.0);
    }

    #[test]
    fn test_calculate_bbox_multiple_elements() {
        let elements = vec![
            make_text_element("A", 10.0, 20.0, 50.0, 12.0),
            make_text_element("B", 100.0, 50.0, 80.0, 14.0),
        ];
        let bbox = SyntheticStructureGenerator::calculate_bbox(&elements);
        assert_eq!(bbox.x, 10.0);
        assert_eq!(bbox.y, 20.0);
        // max_x = max(10+50, 100+80) = 180; width = 180-10 = 170
        assert_eq!(bbox.width, 170.0);
        // max_y = max(20+12, 50+14) = 64; height = 64-20 = 44
        assert_eq!(bbox.height, 44.0);
    }

    #[test]
    fn test_calculate_struct_bbox_empty() {
        let bbox = SyntheticStructureGenerator::calculate_struct_bbox(&[]);
        assert_eq!(bbox.x, 0.0);
        assert_eq!(bbox.width, 0.0);
    }

    #[test]
    fn test_calculate_struct_bbox_multiple() {
        let rects = vec![
            Rect::new(0.0, 0.0, 100.0, 50.0),
            Rect::new(50.0, 30.0, 120.0, 40.0),
        ];
        let bbox = SyntheticStructureGenerator::calculate_struct_bbox(&rects);
        assert_eq!(bbox.x, 0.0);
        assert_eq!(bbox.y, 0.0);
        // max_x = max(100, 170) = 170
        assert_eq!(bbox.width, 170.0);
        // max_y = max(50, 70) = 70
        assert_eq!(bbox.height, 70.0);
    }

    #[test]
    fn test_many_close_elements() {
        let generator = SyntheticStructureGenerator::new();
        let page_bbox = Rect::new(0.0, 0.0, 595.0, 842.0);
        // 10 elements very close together (should all be in one paragraph)
        let elements: Vec<ContentElement> = (0..10)
            .map(|i| {
                make_text_element(&format!("Line {}", i), 10.0, 100.0 + i as f32 * 1.0, 200.0, 12.0)
            })
            .collect();
        let result = generator.generate(&elements, page_bbox).unwrap();
        assert_eq!(result.structure_type, "Document");
        assert!(!result.children.is_empty());
    }

    #[test]
    fn test_custom_high_paragraph_gap() {
        let config = SyntheticStructureConfig {
            paragraph_gap_threshold: 1000.0, // Very high - everything in one paragraph
            heading_size_multiplier: 1.3,
            section_break_threshold: 50.0,
        };
        let generator = SyntheticStructureGenerator::with_config(config);
        let page_bbox = Rect::new(0.0, 0.0, 595.0, 842.0);
        let elements = vec![
            make_text_element("Far 1", 10.0, 100.0, 200.0, 12.0),
            make_text_element("Far 2", 10.0, 500.0, 200.0, 12.0),
        ];
        let result = generator.generate(&elements, page_bbox).unwrap();
        assert_eq!(result.structure_type, "Document");
    }

    #[test]
    fn test_is_heading_paragraph_returns_false() {
        // The placeholder always returns false
        let generator = SyntheticStructureGenerator::new();
        let para = StructureElement {
            structure_type: "P".to_string(),
            bbox: Rect::new(0.0, 0.0, 100.0, 12.0),
            children: Vec::new(),
            reading_order: None,
            alt_text: None,
            language: None,
        };
        assert!(!generator.is_heading_paragraph(&para));
    }
}