bobine 0.5.10

PDF / Office / text → Markdown ingestion engine with ONNX formula OCR (TexTeller), layout analysis and OCR
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
// PdfSource — the seam between bobine's conversion logic and pdf_oxide.
//
// All converter internals operate on `&mut dyn PdfSource`, so the routing /
// splicing / gallery logic can be exercised against tiny in-memory fakes
// instead of real PDF files (mirrors legacy Python's conftest.py fakes).
//
// The trait deliberately uses bobine-owned types (`SourceChar`, PNG bytes,
// `DynamicImage`) rather than pdf_oxide's — fakes stay decoupled from
// upstream struct churn.

use pdf_oxide::{
    api::Pdf,
    geometry::Rect,
    layout::{RectFilterMode, TextChar},
    rendering::RenderOptions,
};

use crate::error::{BobineError, Result};

/// Minimal per-character info the converter actually consumes.
///
/// (Legacy used pdf_oxide's full `TextChar`; only `char`, `bbox` and
/// `font_name` ever mattered.)
#[derive(Debug, Clone)]
pub struct SourceChar {
    pub char: char,
    pub bbox: Rect,
    pub font_name: String,
}

impl SourceChar {
    pub fn new(ch: char, x: f32, y: f32, width: f32, height: f32, font_name: &str) -> Self {
        Self {
            char: ch,
            bbox: Rect::new(x, y, width, height),
            font_name: font_name.to_string(),
        }
    }
}

/// Bobine-owned mirror of one structured table cell.
///
/// Same decoupling rationale as [`SourceChar`]: pdf_oxide's table types
/// churn upstream, and the fakes must not depend on them.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SourceTableCell {
    pub text: String,
    pub colspan: u32,
    pub rowspan: u32,
}

/// Bobine-owned mirror of one structured table row.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SourceTableRow {
    pub cells: Vec<SourceTableCell>,
    pub is_header: bool,
}

/// Bobine-owned mirror of a structured table extracted from the text layer
/// (Tagged-PDF structure tree or ruled-grid spatial detection).
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SourceTable {
    pub rows: Vec<SourceTableRow>,
    pub has_header: bool,
    pub col_count: usize,
    /// Bounding box in PDF points, when the detector could localize it.
    pub bbox: Option<Rect>,
}

/// Placement info for one embedded raster image of a page.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SourceImage {
    /// Bounding box in PDF points (None when the source cannot localize it —
    /// e.g. images drawn through exotic XObject chains).
    pub bbox: Option<Rect>,
    /// Pixel dimensions of the stored bitmap.
    pub width: u32,
    pub height: u32,
}

/// Abstract view of an opened PDF document.
pub trait PdfSource {
    fn page_count(&mut self) -> Result<usize>;
    /// Lossless text-layer markdown for a page (fast path).
    fn page_markdown(&mut self, index: usize) -> Result<String>;
    /// Plain text extracted from a rectangle (PDF points, top-left origin).
    fn text_in_rect(&mut self, index: usize, rect: Rect) -> Result<String>;
    fn chars(&mut self, index: usize) -> Result<Vec<SourceChar>>;
    /// Number of embedded raster images on a page.
    fn image_count(&mut self, index: usize) -> Result<usize>;
    /// Save embedded raster images of a page into `dir` as
    /// `<prefix><n>.png`; returns the written paths.
    fn extract_image_files(
        &mut self,
        index: usize,
        dir: &std::path::Path,
        prefix: &str,
    ) -> Result<Vec<std::path::PathBuf>>;
    /// Rasterize a page at the requested DPI; returns PNG bytes.
    fn render_png(&mut self, index: usize, dpi: u32) -> Result<Vec<u8>>;
    /// `[x0, y0, width, height]` of the page in points.
    fn media_box(&mut self, index: usize) -> Result<[f32; 4]>;

    /// Structured tables whose bbox intersects `rect` (PDF points), drawn
    /// from the Tagged-PDF structure tree or ruled-grid spatial detection.
    /// Default: none (sources without grid detection). Used by the
    /// born-digital table cascade in the full-structure path.
    fn tables_in_rect(&mut self, _index: usize, _rect: Rect) -> Result<Vec<SourceTable>> {
        Ok(Vec::new())
    }

    /// Placement info for the page's embedded raster images, in extraction
    /// order (the same order `extract_image_files` writes files in).
    /// Default: none.
    fn images(&mut self, _index: usize) -> Result<Vec<SourceImage>> {
        Ok(Vec::new())
    }
}

impl PdfSource for Pdf {
    fn page_count(&mut self) -> Result<usize> {
        self.page_count()
            .map_err(|e| BobineError::PdfOxide(format!("page_count: {e}")))
    }

    fn page_markdown(&mut self, index: usize) -> Result<String> {
        self.to_markdown(index)
            .map_err(|e| BobineError::PdfOxide(format!("to_markdown: {e}")))
    }

    fn text_in_rect(&mut self, index: usize, rect: Rect) -> Result<String> {
        self.extract_text_in_rect(index, rect, RectFilterMode::Intersects)
            .map_err(|e| BobineError::PdfOxide(format!("extract_text_in_rect: {e}")))
    }

    fn chars(&mut self, index: usize) -> Result<Vec<SourceChar>> {
        let chars: Vec<TextChar> = self
            .extract_chars(index)
            .map_err(|e| BobineError::PdfOxide(format!("extract_chars: {e}")))?;
        Ok(chars
            .into_iter()
            .map(|c| SourceChar {
                char: c.char,
                bbox: c.bbox,
                font_name: c.font_name,
            })
            .collect())
    }

    fn image_count(&mut self, index: usize) -> Result<usize> {
        Ok(self
            .extract_images(index)
            .map_err(|e| BobineError::PdfOxide(format!("extract_images: {e}")))?
            .len())
    }

    fn extract_image_files(
        &mut self,
        index: usize,
        dir: &std::path::Path,
        prefix: &str,
    ) -> Result<Vec<std::path::PathBuf>> {
        let objs = self
            .extract_images(index)
            .map_err(|e| BobineError::PdfOxide(format!("extract_images: {e}")))?;
        std::fs::create_dir_all(dir)
            .map_err(|e| BobineError::Io(e))?;
        let mut out = Vec::with_capacity(objs.len());
        for (n, obj) in objs.iter().enumerate() {
            // Preserve original bytes for JPEG-encoded images (the dominant
            // embed type): no recompression, smaller files. Everything else
            // is transcoded to lossless PNG.
            let (p, res) = match obj.data() {
                pdf_oxide::extractors::ImageData::Jpeg(bytes) => {
                    let p = dir.join(format!("{prefix}{n}.jpg"));
                    let res = std::fs::write(&p, bytes).map_err(BobineError::Io);
                    (p, res)
                }
                _ => {
                    let p = dir.join(format!("{prefix}{n}.png"));
                    let res = obj
                        .save_as_png(&p)
                        .map_err(|e| BobineError::PdfOxide(format!("save image: {e}")));
                    (p, res)
                }
            };
            res?;
            out.push(p);
        }
        Ok(out)
    }

    fn render_png(&mut self, index: usize, dpi: u32) -> Result<Vec<u8>> {
        let opts = RenderOptions::with_dpi(dpi);
        let rendered = self
            .render_page(index, Some(&opts))
            .map_err(|e| BobineError::PdfOxide(format!("render: {e}")))?;
        Ok(rendered.data)
    }

    fn media_box(&mut self, index: usize) -> Result<[f32; 4]> {
        self.page_media_box(index)
            .map_err(|e| BobineError::PdfOxide(format!("media_box: {e}")))
    }

    fn tables_in_rect(&mut self, index: usize, rect: Rect) -> Result<Vec<SourceTable>> {
        // Whole-page extraction with the balanced default config, then scope
        // to the requested rect by bbox. (extract_tables_in_rect would apply
        // the relaxed text-only strategy, which misses ruled grids.)
        let tables = self
            .extract_tables(index)
            .map_err(|e| BobineError::PdfOxide(format!("extract_tables: {e}")))?;
        Ok(tables
            .into_iter()
            .filter(|t| t.bbox.map_or(false, |b| b.intersects(&rect)))
            .map(|t| SourceTable {
                has_header: t.has_header,
                col_count: t.col_count,
                bbox: t.bbox,
                rows: t
                    .rows
                    .into_iter()
                    .map(|r| SourceTableRow {
                        is_header: r.is_header,
                        cells: r
                            .cells
                            .into_iter()
                            .map(|c| SourceTableCell {
                                text: c.text,
                                colspan: c.colspan,
                                rowspan: c.rowspan,
                            })
                            .collect(),
                    })
                    .collect(),
            })
            .collect())
    }

    fn images(&mut self, index: usize) -> Result<Vec<SourceImage>> {
        let objs = self
            .extract_images(index)
            .map_err(|e| BobineError::PdfOxide(format!("extract_images: {e}")))?;
        Ok(objs
            .iter()
            .map(|o| SourceImage {
                bbox: o.bbox().cloned(),
                width: o.width(),
                height: o.height(),
            })
            .collect())
    }
}

// ======================================================================
// Test fakes
// ======================================================================

#[cfg(test)]
pub(crate) mod fake {
    use super::*;

    /// One synthetic page: canned markdown, chars, images and region texts.
    #[derive(Default, Clone)]
    pub(crate) struct FakePage {
        pub md: Option<String>,
        pub chars: Vec<SourceChar>,
        pub images: Vec<image::DynamicImage>,
        /// Placement bboxes (PDF points) aligned with `images`.
        pub image_bboxes: Vec<Option<Rect>>,
        pub regions: Vec<(Rect, String)>,
        /// Structured tables served by `tables_in_rect` when their bbox
        /// intersects the query rect.
        pub tables: Vec<(Rect, SourceTable)>,
        pub media_box: [f32; 4],
        /// PNG bytes returned by `render_png` (a tiny valid PNG by default).
        pub rendered: Vec<u8>,
    }

    const TINY_PNG: &[u8] = &[
        0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44,
        0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F,
        0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x62, 0x00,
        0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49,
        0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
    ];

    impl FakePage {
        /// A page whose chars mirror `text`, laid out left→right at y=700.
        pub fn from_text(text: &str, font: &str) -> Self {
            let mut page = Self::default();
            page.media_box = [0.0, 0.0, 612.0, 792.0];
            page.md = Some(text.to_string());
            for (i, ch) in text.chars().enumerate() {
                page.chars.push(SourceChar::new(
                    ch,
                    50.0 + i as f32 * 8.0,
                    700.0,
                    7.0,
                    11.0,
                    font,
                ));
            }
            page.rendered = TINY_PNG.to_vec();
            page
        }

        pub fn with_image(mut self, img: image::DynamicImage) -> Self {
            self.images.push(img);
            self.image_bboxes.push(None);
            self
        }

        /// Embedded image with a known placement bbox (PDF points).
        pub fn with_image_at(mut self, img: image::DynamicImage, rect: Rect) -> Self {
            self.images.push(img);
            self.image_bboxes.push(Some(rect));
            self
        }

        pub fn with_region(mut self, rect: Rect, text: &str) -> Self {
            self.regions.push((rect, text.to_string()));
            self
        }

        pub fn with_table(mut self, rect: Rect, table: SourceTable) -> Self {
            self.tables.push((rect, table));
            self
        }

        pub fn with_markdown(mut self, md: &str) -> Self {
            self.md = Some(md.to_string());
            self
        }
    }

    /// A synthetic multi-page document.
    pub(crate) struct FakePdf {
        pub pages: Vec<FakePage>,
    }

    impl FakePdf {
        pub fn new(pages: Vec<FakePage>) -> Self {
            Self { pages }
        }
    }

    impl Default for FakePdf {
        fn default() -> Self {
            Self::new(vec![FakePage::from_text("Hello world", "Helvetica")])
        }
    }

    impl PdfSource for FakePdf {
        fn page_count(&mut self) -> Result<usize> {
            Ok(self.pages.len())
        }

        fn page_markdown(&mut self, index: usize) -> Result<String> {
            self.pages[index]
                .md
                .clone()
                .ok_or_else(|| BobineError::PdfOxide("no markdown".into()))
        }

        fn text_in_rect(&mut self, index: usize, rect: Rect) -> Result<String> {
            // Intersects semantics over the registered regions; fall back to
            // joining chars whose bbox intersects.
            for (r, text) in self.pages[index].regions.iter().rev() {
                let hit = !(rect.x > r.x + r.width
                    || rect.x + rect.width < r.x
                    || rect.y > r.y + r.height
                    || rect.y + rect.height < r.y);
                if hit {
                    return Ok(text.clone());
                }
            }
            Ok(self.pages[index]
                .chars
                .iter()
                .filter(|c| {
                    !(rect.x > c.bbox.x + c.bbox.width
                        || rect.x + rect.width < c.bbox.x
                        || rect.y > c.bbox.y + c.bbox.height
                        || rect.y + rect.height < c.bbox.y)
                })
                .map(|c| c.char)
                .collect())
        }

        fn chars(&mut self, index: usize) -> Result<Vec<SourceChar>> {
            Ok(self.pages[index].chars.clone())
        }

        fn image_count(&mut self, index: usize) -> Result<usize> {
            Ok(self.pages[index].images.len())
        }

        fn extract_image_files(
            &mut self,
            index: usize,
            dir: &std::path::Path,
            prefix: &str,
        ) -> Result<Vec<std::path::PathBuf>> {
            std::fs::create_dir_all(dir).map_err(|e| BobineError::Other(format!("fake mkdir: {e}")))?;
            let mut out = Vec::new();
            for (n, img) in self.pages[index].images.iter().enumerate() {
                let p = dir.join(format!("{prefix}{n}.png"));
                img.save_with_format(&p, image::ImageFormat::Png)
                    .map_err(|e| BobineError::Other(format!("fake image save: {e}")))?;
                out.push(p);
            }
            Ok(out)
        }

        fn render_png(&mut self, index: usize, _dpi: u32) -> Result<Vec<u8>> {
            Ok(self.pages[index].rendered.clone())
        }

        fn media_box(&mut self, index: usize) -> Result<[f32; 4]> {
            Ok(self.pages[index].media_box)
        }

        fn images(&mut self, index: usize) -> Result<Vec<SourceImage>> {
            Ok(self.pages[index]
                .images
                .iter()
                .zip(self.pages[index].image_bboxes.iter())
                .map(|(img, bbox)| SourceImage {
                    bbox: *bbox,
                    width: img.width(),
                    height: img.height(),
                })
                .collect())
        }

        fn tables_in_rect(&mut self, index: usize, rect: Rect) -> Result<Vec<SourceTable>> {
            let hit = |r: &Rect| {
                !(rect.x > r.x + r.width
                    || rect.x + rect.width < r.x
                    || rect.y > r.y + r.height
                    || rect.y + rect.height < r.y)
            };
            Ok(self.pages[index]
                .tables
                .iter()
                .filter(|(r, _)| hit(r))
                .map(|(_, t)| t.clone())
                .collect())
        }
    }
}