docling 1.6.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
//! StarOffice 5 binary backend (`.sdw`/`.sda`/`.sdd`/`.vor`) — a docling.rs
//! extension (#215); docling reaches these only via LibreOffice, whose modern
//! import goes through the reverse-engineered libstaroffice library.
//!
//! The container is CFB (the same [`CompoundFile`] the `.doc`/`.xls`/`.ppt`
//! backends use); the document kind comes from the stream it holds, so a
//! `.vor` template of any application dispatches by content:
//!
//! - **`StarWriterDocument`** (StarWriter 3–5, `.sdw`): a record tree — one
//!   byte of record type plus a 24-bit size, each record opening with a flag
//!   byte whose low nibble is the number of prologue bytes to skip. Text
//!   nodes (`'T'`) carry a Pascal-style string (16-bit length, 8-bit chars);
//!   containers are discovered by validation (a payload that parses as an
//!   exact record sequence is recursed). This is text-level extraction:
//!   paragraphs in document order, tables flattening to their cell texts and
//!   redline-deleted fragments kept (they live inline in the node string).
//! - **`StarDrawDocument3`** (StarDraw / StarImpress 3–5, `.sda`/`.sdd`):
//!   chunked (four ASCII bytes + version + total size). Master pages
//!   (`DrMP`) hold the layout placeholders ("Doubleclick to edit …") and are
//!   skipped whole; each drawing page (`DrPg`) with real objects becomes a
//!   section whose text comes from the embedded outliner blocks (`xV4B`
//!   magic; per paragraph a text string, a style name and an attribute list
//!   whose `0x0f9d` entry is the outline depth). Notes pages — recognized by
//!   their `~LT~Notizen`-styled text objects — are dropped like docling
//!   drops speaker notes elsewhere; object-less pages (the handout) too.
//!
//! Strings decode through Windows-1252 (the format predates Unicode; other
//! source charsets degrade readably). StarCalc (`.sdc`) has a different cell
//! record model and stays a follow-up.

use crate::backend::cfb::CompoundFile;
use crate::backend::rtf::decode_byte;
use crate::backend::DeclarativeBackend;
use crate::error::ConversionError;
use crate::source::SourceDocument;
use docling_core::{DoclingDocument, Node};

pub struct StarOffice5Backend;

impl DeclarativeBackend for StarOffice5Backend {
    fn convert(&self, source: &SourceDocument) -> Result<DoclingDocument, ConversionError> {
        let cfb = CompoundFile::open(&source.bytes).ok_or_else(|| {
            ConversionError::Parse("staroffice: not an OLE2 compound file".into())
        })?;
        if let Some(sw) = cfb.stream("StarWriterDocument") {
            return convert_writer(&sw, &source.name);
        }
        if let Some(draw) = cfb.stream("StarDrawDocument3") {
            return convert_draw(&draw, &source.name);
        }
        if cfb.stream("StarCalcDocument").is_some() {
            return Err(ConversionError::Parse(
                "staroffice: StarCalc spreadsheets (.sdc) are not supported yet — \
                 open the file in LibreOffice and save as .ods"
                    .into(),
            ));
        }
        Err(ConversionError::Parse(
            "staroffice: no StarWriterDocument or StarDrawDocument3 stream — \
             not a StarOffice 5 writer/draw/impress document"
                .into(),
        ))
    }
}

/// One byte → char via the shared Windows-1252 table.
fn latin(bytes: &[u8]) -> String {
    bytes.iter().map(|&b| decode_byte(b, 1252)).collect()
}

// ---------------------------------------------------------------- StarWriter

/// A record's header: type byte + 24-bit little-endian size (the size counts
/// the 4 header bytes too).
fn sw_record(d: &[u8], off: usize) -> Option<(u8, usize)> {
    if off + 4 > d.len() {
        return None;
    }
    let t = d[off];
    let size = d[off + 1] as usize | (d[off + 2] as usize) << 8 | (d[off + 3] as usize) << 16;
    // Record types are printable ASCII ('!', '0', 'N', 'T', …); anything else
    // marks a misparse.
    if size < 4 || off + size > d.len() || !(0x21..=0x7e).contains(&t) {
        return None;
    }
    Some((t, size))
}

/// Walk a record sequence spanning exactly `[off, end)`, collecting `'T'` text
/// nodes; returns `false` when the bytes do not chain as records (the caller's
/// signal that this payload is a leaf, not a container). Containers are
/// discovered by that same validation, recursively.
fn sw_walk(d: &[u8], mut off: usize, end: usize, depth: usize, out: &mut Vec<String>) -> bool {
    if depth > 24 {
        return false;
    }
    let mut any = false;
    while off < end {
        let Some((t, size)) = sw_record(d, off) else {
            return false;
        };
        if off + size > end {
            return false;
        }
        let payload = &d[off + 4..off + size];
        if t == b'T' {
            if let Some(text) = sw_text(payload) {
                out.push(text);
            }
        } else if !payload.is_empty() {
            // The flag byte's low nibble is the prologue length; a payload
            // that then parses as an exact record sequence is a container.
            let skip = 1 + (payload[0] & 0x0f) as usize;
            if skip <= payload.len() {
                let mut sub = Vec::new();
                if sw_walk(d, off + 4 + skip, off + size, depth + 1, &mut sub) {
                    out.extend(sub);
                }
            }
        }
        any = true;
        off += size;
    }
    any && off == end
}

/// A `'T'` text node's string: flag byte, `flags & 0x0f` prologue bytes, then
/// a 16-bit length and that many 8-bit characters.
fn sw_text(payload: &[u8]) -> Option<String> {
    let flags = *payload.first()?;
    let p = 1 + (flags & 0x0f) as usize;
    let len = u16::from_le_bytes([*payload.get(p)?, *payload.get(p + 1)?]) as usize;
    let bytes = payload.get(p + 2..p + 2 + len)?;
    let text = latin(bytes);
    let trimmed = text.trim();
    (!trimmed.is_empty()).then(|| trimmed.to_string())
}

fn convert_writer(d: &[u8], name: &str) -> Result<DoclingDocument, ConversionError> {
    if d.len() < 8 || &d[..2] != b"SW" || &d[3..6] != b"HDR" {
        return Err(ConversionError::Parse(
            "staroffice: StarWriterDocument stream without an SW*HDR header".into(),
        ));
    }
    // The fixed header's length varies by version; the record area is the
    // first offset from which the whole stream chains as records.
    let mut texts = Vec::new();
    for start in 8..d.len().min(0x100) {
        let mut trial = Vec::new();
        if sw_walk(d, start, d.len(), 0, &mut trial) && !trial.is_empty() {
            texts = trial;
            break;
        }
    }
    if texts.is_empty() {
        return Err(ConversionError::Parse(
            "staroffice: no text records found in the StarWriter document".into(),
        ));
    }
    let mut doc = DoclingDocument::new(name);
    for text in texts {
        doc.push(Node::Paragraph { text });
    }
    Ok(doc)
}

// ---------------------------------------------------------- StarDraw/Impress

/// One outliner paragraph: its text, style name and outline depth.
struct DrawPara {
    text: String,
    style: String,
    depth: u16,
}

/// Parse an outliner block at `off` (the `xV4B` magic): version char, one
/// byte, 32-bit body size, then a sync word, three bytes, a paragraph count
/// and per paragraph text + style strings, an `0xaffe` marker and an
/// attribute list (`which` 0x0f9d carries the outline depth in its value's
/// high word).
fn draw_outliner(d: &[u8], off: usize) -> Option<Vec<DrawPara>> {
    let size = u32::from_le_bytes(d.get(off + 6..off + 10)?.try_into().ok()?) as usize;
    let body = d.get(off + 10..off + 10 + size)?;
    let u16_at = |p: usize| -> Option<u16> {
        Some(u16::from_le_bytes(body.get(p..p + 2)?.try_into().ok()?))
    };
    let mut p = 2 + 3; // sync word + three bytes
    let count = u16_at(p)?;
    p += 2;
    let mut paras = Vec::new();
    for _ in 0..count {
        let len = u16_at(p)? as usize;
        p += 2;
        let text = latin(body.get(p..p + len)?);
        p += len;
        let slen = u16_at(p)? as usize;
        p += 2;
        let style = latin(body.get(p..p + slen)?);
        p += slen;
        if body.get(p..p + 2) != Some(&[0xfe, 0xaf]) {
            return None;
        }
        p += 2;
        let nattr = u16_at(p)? as usize;
        p += 2;
        let mut depth = 0u16;
        for _ in 0..nattr {
            let which = u16_at(p)?;
            let value = u32::from_le_bytes(body.get(p + 2..p + 6)?.try_into().ok()?);
            if which == 0x0f9d {
                depth = (value >> 16) as u16;
            }
            p += 6;
        }
        p += 2;
        paras.push(DrawPara { text, style, depth });
    }
    Some(paras)
}

/// The `[start, end)` spans of every `DrPg` (page) chunk: four ASCII tag
/// bytes, a 16-bit version and a 32-bit size that counts the whole chunk.
/// `DrMP` master-page chunks are recognized the same way but only skipped —
/// their placeholder texts must never leak into a page.
fn draw_spans(d: &[u8]) -> Vec<(usize, usize)> {
    let mut pages = Vec::new();
    let mut off = 0usize;
    while off + 10 <= d.len() {
        let tag = &d[off..off + 4];
        if tag == b"DrPg" || tag == b"DrMP" {
            let ver = u16::from_le_bytes([d[off + 4], d[off + 5]]);
            let size = u32::from_le_bytes(d[off + 6..off + 10].try_into().unwrap()) as usize;
            if ver < 0x100 && size >= 10 && off + size <= d.len() {
                if tag == b"DrPg" {
                    pages.push((off, off + size));
                }
                // Page chunks never nest in each other; skip the whole span.
                off += size;
                continue;
            }
        }
        off += 1;
    }
    pages
}

fn convert_draw(d: &[u8], name: &str) -> Result<DoclingDocument, ConversionError> {
    let pages = draw_spans(d);
    if pages.is_empty() {
        return Err(ConversionError::Parse(
            "staroffice: no DrPg page chunks in the StarDraw document".into(),
        ));
    }

    let mut doc = DoclingDocument::new(name);
    let mut emitted = 0usize;
    for &(start, end) in &pages {
        let span = &d[start..end];
        // The handout page carries no drawing objects at all.
        if !contains(span, b"DrOb") {
            continue;
        }
        // Collect this page's outliner texts.
        let mut paras: Vec<DrawPara> = Vec::new();
        let mut off = 0usize;
        while off + 10 <= span.len() {
            if &span[off..off + 4] == b"xV4B" {
                if let Some(mut block) = draw_outliner(span, off) {
                    paras.append(&mut block);
                }
            }
            off += 1;
        }
        // A notes page announces itself through its placeholder style — and
        // docling drops speaker notes across formats, so the page goes whole.
        if paras.iter().any(|p| p.style.contains("~LT~Notizen")) {
            continue;
        }
        emitted += 1;
        doc.push(Node::Heading {
            level: 1,
            text: format!("page-{emitted}"),
        });
        let mut first = true;
        for para in &paras {
            let text = para.text.trim();
            if text.is_empty() {
                continue;
            }
            if para.depth > 0 {
                doc.push(Node::ListItem {
                    ordered: false,
                    number: 0,
                    first_in_list: std::mem::take(&mut first),
                    text: text.to_string(),
                    level: para.depth as u8 - 1,
                    marker: None,
                    location: None,
                    dclx: None,
                    href: None,
                    layer: None,
                });
            } else {
                first = true;
                doc.push(Node::Paragraph {
                    text: text.to_string(),
                });
            }
        }
    }
    if emitted == 0 {
        return Err(ConversionError::Parse(
            "staroffice: no content pages in the StarDraw document".into(),
        ));
    }
    Ok(doc)
}

/// Naive subsequence search (the spans are small; no need for memmem).
fn contains(haystack: &[u8], needle: &[u8]) -> bool {
    haystack.windows(needle.len()).any(|w| w == needle)
}

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

    /// Assemble an SW record: type + 24-bit size + payload.
    fn rec(t: u8, payload: &[u8]) -> Vec<u8> {
        let size = payload.len() + 4;
        let mut out = vec![t, size as u8, (size >> 8) as u8, (size >> 16) as u8];
        out.extend_from_slice(payload);
        out
    }

    /// A text node payload: flag byte (low nibble = prologue length),
    /// prologue, u16 length, bytes.
    fn text_payload(flags: u8, text: &[u8]) -> Vec<u8> {
        let mut p = vec![flags];
        p.extend(std::iter::repeat_n(0u8, (flags & 0x0f) as usize));
        p.extend_from_slice(&(text.len() as u16).to_le_bytes());
        p.extend_from_slice(text);
        p
    }

    #[test]
    fn sw_walk_extracts_text_through_containers() {
        // N{ T"Erster" T"" T"Zwei\x94ter" } — the flag byte's low nibble
        // varies, the empty node drops out and 0x94 decodes as cp1252 ”.
        let mut body = Vec::new();
        body.extend(rec(b'T', &text_payload(0x02, b"Erster")));
        body.extend(rec(b'T', &text_payload(0x13, b"")));
        body.extend(rec(b'T', &text_payload(0x02, b"Zwei\x94ter")));
        let mut container_payload = vec![0x04u8, 0, 0, 0, 0]; // flags 4 + prologue
        container_payload.extend(&body);
        let stream = rec(b'N', &container_payload);
        let mut out = Vec::new();
        assert!(sw_walk(&stream, 0, stream.len(), 0, &mut out));
        assert_eq!(
            out,
            vec!["Erster".to_string(), "Zwei\u{201d}ter".to_string()]
        );
    }

    #[test]
    fn writer_needs_the_sw_header() {
        let err = convert_writer(b"NOTSWFILE___", "x").unwrap_err();
        assert!(err.to_string().contains("SW*HDR"), "{err}");
    }

    /// A `DrPg` with an object and an outliner block converts to a page
    /// section; the master (`DrMP`) placeholder and the notes page
    /// (`~LT~Notizen` style) are skipped.
    #[test]
    fn draw_pages_keep_content_and_skip_masters_and_notes() {
        fn outliner(paras: &[(&str, &str, u16)]) -> Vec<u8> {
            let mut body = vec![0x2d, 0x01, 0, 0, 0]; // sync + three bytes
            body.extend((paras.len() as u16).to_le_bytes());
            for (text, style, depth) in paras {
                body.extend((text.len() as u16).to_le_bytes());
                body.extend_from_slice(text.as_bytes());
                body.extend((style.len() as u16).to_le_bytes());
                body.extend_from_slice(style.as_bytes());
                body.extend([0xfe, 0xaf]);
                body.extend(1u16.to_le_bytes()); // one attribute
                body.extend(0x0f9du16.to_le_bytes());
                body.extend(((*depth as u32) << 16).to_le_bytes());
                body.extend([0, 0]);
            }
            let mut out = b"xV4B1\0".to_vec();
            out.extend((body.len() as u32).to_le_bytes());
            out.extend(body);
            out
        }
        fn chunk(tag: &[u8; 4], content: &[u8]) -> Vec<u8> {
            let mut out = tag.to_vec();
            out.extend(0x0cu16.to_le_bytes());
            out.extend(((content.len() + 10) as u32).to_le_bytes());
            out.extend_from_slice(content);
            out
        }
        let mut stream = Vec::new();
        stream.extend(chunk(
            b"DrMP",
            &[
                b"DrOb".to_vec(),
                outliner(&[("Doubleclick", "std~LT~Titel", 0)]),
            ]
            .concat(),
        ));
        stream.extend(chunk(
            b"DrPg",
            &[
                b"DrOb".to_vec(),
                outliner(&[("Titel der Seite", "std~LT~Titel", 0), ("Punkt", "std", 1)]),
            ]
            .concat(),
        ));
        stream.extend(chunk(b"DrPg", b"DrOb".as_ref())); // shapes, no text
        stream.extend(chunk(
            b"DrPg",
            &[
                b"DrOb".to_vec(),
                outliner(&[("Notiz", "std~LT~Notizen", 0)]),
            ]
            .concat(),
        ));
        let doc = convert_draw(&stream, "d").unwrap();
        let md = doc.export_to_markdown();
        assert!(md.contains("# page-1"), "{md}");
        assert!(md.contains("Titel der Seite"), "{md}");
        assert!(md.contains("- Punkt"), "{md}");
        assert!(md.contains("# page-2"), "second page kept:\n{md}");
        assert!(!md.contains("Doubleclick"), "master skipped:\n{md}");
        assert!(!md.contains("Notiz"), "notes page skipped:\n{md}");
        assert!(!md.contains("page-3"), "notes page not counted:\n{md}");
    }
}