docling 2.84.0

DocumentConverter and format backends for docling.rs (a Rust port of docling).
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
//! Apple Numbers — docling's `IWorkNumbersDocumentBackend` (docling#4113,
//! docling#5101, docling#5087; #1031): the content model both container
//! generations are read into (`iwork/numbers_content.py`) and its emission
//! into a `DoclingDocument`.
//!
//! Each sheet is a page and a `sheet` group. Tables, charts and pictures on
//! it become table and picture items in the order they are laid out down the
//! page; each sticky note becomes a comment in the notes layer. A table's
//! name is its caption; a cell filled with an image is a rich cell holding
//! the cell's text and the picture. Every item keeps the frame of the
//! drawable it came from as its provenance, top-left points on the sheet.
//!
//! The readers live beside this module: `numbers_iwa.rs` (Numbers 3+,
//! `Index/*.iwa`) and `numbers_xml.rs` (iWork '09, `index.xml`).

use docling_core::tree::{ItemTree, TreeKind, TreeProv};
use docling_core::{ContentLayer, DoclingDocument, Node, Table, TableCell};

use super::keynote::Geometry;
use super::pages::{Chart, Picture};

/// `MAX_TABLE_CELLS`: cells one table may declare before it is rejected as
/// implausible — the counts come from the document, and Numbers' own ceiling
/// is a million per table.
pub(crate) const MAX_TABLE_CELLS: usize = 4_000_000;

/// One cell of a table, already rendered to text; `picture` is the image the
/// cell is filled with (Numbers draws it behind the text).
#[derive(Debug, Clone)]
pub(crate) struct Cell {
    pub row: usize,
    pub col: usize,
    pub text: String,
    pub col_span: usize,
    pub picture: Option<Picture>,
}

/// One table on a sheet; `geometry` is its frame on the sheet canvas.
#[derive(Debug, Clone)]
pub(crate) struct SheetTable {
    pub name: String,
    pub num_rows: usize,
    pub num_cols: usize,
    pub header_rows: usize,
    pub header_cols: usize,
    pub cells: Vec<Cell>,
    pub geometry: Option<Geometry>,
}

/// One sticky note: Numbers 3+ records who left it and when, iWork '09
/// neither. `timestamp` is the ISO instant.
#[derive(Debug, Clone)]
pub(crate) struct Comment {
    pub text: String,
    pub author: String,
    pub timestamp: Option<String>,
    pub geometry: Option<Geometry>,
}

/// One sheet, with what is drawn on it, each kind sorted by [`sheet_order`].
#[derive(Debug, Clone, Default)]
pub(crate) struct Sheet {
    pub name: String,
    pub tables: Vec<SheetTable>,
    pub charts: Vec<(Chart, Option<Geometry>)>,
    pub pictures: Vec<(Picture, Option<Geometry>)>,
    pub comments: Vec<Comment>,
}

/// `sheet_order`: top edge, then left edge — Numbers keeps a sheet's
/// drawables in z-order, not reading order; an unplaced one sorts first.
pub(crate) fn sheet_order(geometry: Option<&Geometry>) -> (f64, f64) {
    geometry.map_or((0.0, 0.0), |g| (g.top, g.left))
}

/// Stable sort of `items` by the [`sheet_order`] of their geometry.
pub(crate) fn sort_by_sheet<T>(items: &mut [T], geometry: impl Fn(&T) -> Option<&Geometry>) {
    items.sort_by(|a, b| {
        sheet_order(geometry(a))
            .partial_cmp(&sheet_order(geometry(b)))
            .unwrap_or(std::cmp::Ordering::Equal)
    });
}

enum Drawn {
    Table(SheetTable),
    Chart(Chart),
    Picture(Picture),
}

/// docling's `IWorkNumbersDocumentBackend.convert`. `page_range` is the
/// 1-based inclusive window of sheets to keep (a sheet keeps its number).
pub(crate) fn emit(
    sheets: Vec<Sheet>,
    page_range: Option<(usize, usize)>,
    doc: &mut DoclingDocument,
) {
    let mut tree = ItemTree {
        inline_whitespace: true,
        ..ItemTree::default()
    };
    let mut table_count = 0usize;
    for (ix, sheet) in sheets.into_iter().enumerate() {
        let page_no = ix + 1;
        if let Some((first, last)) = page_range {
            if page_no < first || page_no > last {
                continue;
            }
        }
        let (width, height) = sheet_extent(&sheet);
        doc.push(Node::PageInfo {
            page_no,
            width: width as f32,
            height: height as f32,
        });
        let name = if sheet.name.is_empty() {
            format!("Sheet {page_no}")
        } else {
            sheet.name.clone()
        };
        let group = tree.add(
            None,
            None,
            TreeKind::Group {
                label: "sheet".into(),
                name: name.clone(),
            },
        );
        // Tables, charts and pictures share the sheet canvas: one pass down
        // the page, not one kind after the other.
        let mut drawn: Vec<(Drawn, Option<Geometry>)> = Vec::new();
        drawn.extend(sheet.tables.into_iter().map(|t| {
            let g = t.geometry;
            (Drawn::Table(t), g)
        }));
        drawn.extend(sheet.charts.into_iter().map(|(c, g)| (Drawn::Chart(c), g)));
        drawn.extend(
            sheet
                .pictures
                .into_iter()
                .map(|(p, g)| (Drawn::Picture(p), g)),
        );
        sort_by_sheet(&mut drawn, |(_, g)| g.as_ref());
        let mut out = SheetOut {
            tree: &mut tree,
            group,
            page_no,
            size: (width, height),
            nodes: Vec::new(),
        };
        for (item, geometry) in drawn {
            match item {
                Drawn::Table(t) => {
                    table_count += 1;
                    out.add_table(t, table_count);
                }
                Drawn::Chart(c) => out.add_chart(c, geometry),
                Drawn::Picture(p) => out.add_picture(&p, geometry),
            }
        }
        let children = out.nodes;
        doc.push(Node::Group {
            label: "sheet".into(),
            name: Some(name),
            layer: None,
            children,
        });
        // `_add_sheet_comment`: each sticky note under a `comment_section` of
        // its own in the notes layer, with who left it and when.
        for (position, comment) in sheet.comments.iter().enumerate() {
            let mut meta = Vec::new();
            if !comment.author.is_empty() {
                meta.push(format!("author: {}", comment.author));
            }
            if let Some(time) = &comment.timestamp {
                meta.push(format!("time: {time}"));
            }
            let text = if meta.is_empty() {
                comment.text.clone()
            } else {
                format!("[{}]: {}", meta.join(", "), comment.text)
            };
            let section = tree.add(
                None,
                Some(ContentLayer::Notes),
                TreeKind::Group {
                    label: "comment_section".into(),
                    name: format!("comment-{}-{}", sheet.name, position + 1),
                },
            );
            tree.add(
                Some(section),
                Some(ContentLayer::Notes),
                text_kind("text", &text),
            );
            doc.push(Node::CommentSection {
                name: format!("comment-{}-{}", sheet.name, position + 1),
                text: super::markdown::escape_text(&text),
                refs_note_text: true,
                grouped: true,
            });
        }
        if ix > 0 {
            doc.push(Node::PageBreak);
        }
    }
    doc.tree = Some(tree);
}

/// `_sheet_extent`: how far a sheet's contents reach, from its top left.
fn sheet_extent(sheet: &Sheet) -> (f64, f64) {
    let geometries = sheet
        .tables
        .iter()
        .map(|t| t.geometry.as_ref())
        .chain(sheet.charts.iter().map(|(_, g)| g.as_ref()))
        .chain(sheet.pictures.iter().map(|(_, g)| g.as_ref()))
        .chain(sheet.comments.iter().map(|c| c.geometry.as_ref()));
    let (mut width, mut height) = (0f64, 0f64);
    for g in geometries.flatten() {
        width = width.max(g.left + g.width);
        height = height.max(g.top + g.height);
    }
    (width, height)
}

fn text_kind(label: &str, text: &str) -> TreeKind {
    TreeKind::Text {
        label: label.into(),
        text: text.into(),
        orig: None,
        formatting: None,
        hyperlink: None,
        level: None,
        list: None,
    }
}

struct SheetOut<'t> {
    tree: &'t mut ItemTree,
    group: usize,
    page_no: usize,
    size: (f64, f64),
    nodes: Vec<Node>,
}

impl SheetOut<'_> {
    /// `_sheet_prov`: the sheet's page and the drawable's box — an empty one
    /// for a drawable Numbers did not position — with a span over `text`.
    fn prov(&self, geometry: Option<&Geometry>, text: &str) -> TreeProv {
        TreeProv {
            page_no: self.page_no,
            bbox: geometry
                .map(|g| [g.left, g.top, g.left + g.width, g.top + g.height])
                .unwrap_or([0.0; 4]),
            bottom_left: false,
            charspan: [0, text.chars().count()],
        }
    }

    /// The DocLang `<location>` of a drawable on the 0–511 grid of the sheet.
    fn location(&self, g: &Geometry) -> [u16; 4] {
        let (w, h) = self.size;
        let grid = |v: f64, extent: f64| -> u16 {
            if extent <= 0.0 {
                return 0;
            }
            (v / extent * 511.0).round().clamp(0.0, 511.0) as u16
        };
        [
            grid(g.left, w),
            grid(g.top, h),
            grid(g.left + g.width, w),
            grid(g.top + g.height, h),
        ]
    }

    fn push(&mut self, node: Node, geometry: Option<&Geometry>) {
        match geometry {
            Some(g) => {
                let location = self.location(g);
                self.nodes.push(Node::Located {
                    location,
                    inner: Box::new(node),
                })
            }
            None => self.nodes.push(node),
        }
    }

    /// `_add_sheet_table`. `ordinal` is docling's `len(doc.tables)` once the
    /// table is added, which names its rich-cell groups.
    fn add_table(&mut self, t: SheetTable, ordinal: usize) {
        let geometry = t.geometry;
        let prov = self.prov(geometry.as_ref(), "");
        let captions: Vec<usize> = (!t.name.is_empty())
            .then(|| {
                let prov = self.prov(geometry.as_ref(), &t.name);
                self.tree
                    .add_with_prov(Some(self.group), None, text_kind("caption", &t.name), prov)
            })
            .into_iter()
            .collect();
        let mut table = grid(&t);
        let id = self.tree.add_with_prov(
            Some(self.group),
            None,
            TreeKind::Table {
                table: table.clone(),
                rich_cells: Vec::new(),
                captions,
                footnotes: Vec::new(),
            },
            prov.clone(),
        );
        // A cell filled with a picture is a rich cell: a group under the
        // table holds the cell's text and its picture, both in the frame of
        // the table.
        let mut rich = Vec::new();
        let mut blocks: Vec<Vec<Vec<Node>>> = vec![vec![Vec::new(); t.num_cols]; t.num_rows];
        for cell in t.cells.iter().filter(|c| c.picture.is_some()) {
            let group = self.tree.add(
                Some(id),
                None,
                TreeKind::Group {
                    label: "unspecified".into(),
                    name: format!("rich_cell_group_{ordinal}_{}_{}", cell.col, cell.row),
                },
            );
            let mut cell_nodes = Vec::new();
            if !cell.text.is_empty() {
                let p = self.prov(geometry.as_ref(), &cell.text);
                self.tree
                    .add_with_prov(Some(group), None, text_kind("text", &cell.text), p);
                cell_nodes.push(Node::Paragraph {
                    text: super::markdown::escape_text(&cell.text),
                });
            }
            let image = cell.picture.as_ref().and_then(picture_image);
            self.tree.add_with_prov(
                Some(group),
                None,
                picture_kind(image.clone(), Vec::new(), None, None),
                prov.clone(),
            );
            cell_nodes.push(picture_node(image));
            rich.push((cell.row, cell.col, group));
            if let Some(slot) = blocks.get_mut(cell.row).and_then(|r| r.get_mut(cell.col)) {
                *slot = cell_nodes;
            }
            if let Some(slot) = table
                .rows
                .get_mut(cell.row)
                .and_then(|r| r.get_mut(cell.col))
            {
                *slot = if cell.text.is_empty() {
                    "<!-- image -->".to_string()
                } else {
                    format!("{}  <!-- image -->", cell.text)
                };
            }
        }
        if !rich.is_empty() {
            table.cell_blocks = Some(blocks);
        }
        if let TreeKind::Table {
            rich_cells,
            table: tree_table,
            ..
        } = &mut self.tree.items[id].kind
        {
            *rich_cells = rich;
            tree_table.cell_blocks = table.cell_blocks.clone();
        }
        table.caption = (!t.name.is_empty()).then(|| super::markdown::escape_text(&t.name));
        self.push(Node::Table(table), geometry.as_ref());
    }

    /// `_add_chart`: a picture classified by the chart's kind, carrying its
    /// data as a table, captioned with its title — as on a Keynote slide.
    fn add_chart(&mut self, chart: Chart, geometry: Option<Geometry>) {
        let caption: Vec<usize> = chart
            .title
            .as_deref()
            .map(|title| {
                let prov = self.prov(geometry.as_ref(), title);
                self.tree
                    .add_with_prov(Some(self.group), None, text_kind("caption", title), prov)
            })
            .into_iter()
            .collect();
        let table = chart.table();
        let prov = self.prov(geometry.as_ref(), "");
        self.tree.add_with_prov(
            Some(self.group),
            None,
            picture_kind(None, caption, Some(chart.label.clone()), table.clone()),
            prov,
        );
        self.push(
            Node::Chart {
                kind: chart.label,
                table: table.unwrap_or_default(),
                caption: chart.title,
                location: None,
            },
            geometry.as_ref(),
        );
    }

    /// `_add_picture`.
    fn add_picture(&mut self, picture: &Picture, geometry: Option<Geometry>) {
        let image = picture_image(picture);
        let prov = self.prov(geometry.as_ref(), "");
        self.tree.add_with_prov(
            Some(self.group),
            None,
            picture_kind(image.clone(), Vec::new(), None, None),
            prov,
        );
        self.push(picture_node(image), geometry.as_ref());
    }
}

fn picture_image(picture: &Picture) -> Option<docling_core::PictureImage> {
    picture
        .data
        .as_ref()
        .and_then(|d| super::ooxml::picture_image(&picture.name, d.clone()))
}

fn picture_kind(
    image: Option<docling_core::PictureImage>,
    captions: Vec<usize>,
    classification: Option<String>,
    chart: Option<Table>,
) -> TreeKind {
    TreeKind::Picture {
        captions,
        image,
        classification,
        extra_classes: Vec::new(),
        description: None,
        confidence: None,
        chart,
        dpi: None,
    }
}

fn picture_node(image: Option<docling_core::PictureImage>) -> Node {
    Node::Picture {
        caption: None,
        caption_href: None,
        image,
        classification: None,
        description: None,
        caption_parent: Default::default(),
        caption_location: None,
    }
}

/// The table's grid: every cell at its place, spans repeated across the
/// columns they cover, with docling's header flags — a column header in the
/// header rows, a row header in the header columns below them.
fn grid(t: &SheetTable) -> Table {
    let mut rows = vec![vec![String::new(); t.num_cols]; t.num_rows];
    let mut cells = Vec::with_capacity(t.cells.len());
    for cell in &t.cells {
        if cell.row >= t.num_rows || cell.col >= t.num_cols {
            continue;
        }
        let span = cell.col_span.max(1).min(t.num_cols - cell.col);
        for slot in &mut rows[cell.row][cell.col..cell.col + span] {
            slot.clone_from(&cell.text);
        }
        cells.push(TableCell {
            text: cell.text.clone(),
            bbox: None,
            start_row: cell.row,
            start_col: cell.col,
            row_span: 1,
            col_span: span,
            column_header: cell.row < t.header_rows,
            row_header: cell.row >= t.header_rows && cell.col < t.header_cols,
            row_section: false,
        });
    }
    Table {
        rows,
        cells: Some(cells),
        ..Default::default()
    }
}