document-svg 2.0.2

Convert PDF, Word, Excel, PowerPoint, diagram and CAD files into one SVG per page, locally — a Rust library and the docsvg command
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
//! Bounded JATS (Journal Article Tag Suite) article preview.
//!
//! JATS is XML used for journal articles and technical reports. This reader
//! renders article metadata, abstract, sections, paragraphs, figure captions,
//! and simple tables. `graphic`/`inline-graphic` references are confined to the
//! input directory and use the shared PNG/JPEG validation budget; XML, MathML,
//! external links, scripts, and publisher-specific extensions remain inert.

use std::collections::HashMap;
use std::fs;
use std::path::Path;

use quick_xml::Reader;
use quick_xml::events::Event;

use crate::convert::{ConvertOptions, PageConsumer, read_limited_file};
use crate::document::html::{
    HtmlBlock, InlineHtmlImage, load_local_image_sources, render_blocks_to_pages_with_warnings,
};
use crate::error::{Error, Result};
use crate::ooxml::{attribute, local_name};
use crate::table::{TableAlign, TableData};

const MAX_JATS_INPUT_BYTES: u64 = 128 * 1024 * 1024;
const MAX_JATS_XML_DEPTH: usize = 256;
const MAX_JATS_TEXT_BYTES: usize = 64 * 1024 * 1024;
const MAX_JATS_TABLE_CELLS: usize = 200_000;
const MAX_JATS_IMAGE_REFERENCES: usize = 10_000;

#[derive(Default)]
struct JatsTable {
    rows: Vec<Vec<String>>,
    row: Vec<String>,
    cell: String,
    in_cell: bool,
}

pub(crate) fn looks_like_prefix(bytes: &[u8]) -> bool {
    let text = String::from_utf8_lossy(bytes);
    let lower = text.to_ascii_lowercase();
    lower.contains("<article")
        && (lower.contains("jats")
            || lower.contains("niso.org")
            || lower.contains("journal-meta")
            || lower.contains("article-meta"))
}

pub(crate) fn convert(
    path: &Path,
    options: &ConvertOptions,
    sink: &mut dyn PageConsumer,
) -> Result<Vec<String>> {
    let bytes = read_limited_file(
        path,
        options.max_input_bytes.min(MAX_JATS_INPUT_BYTES),
        "JATS input",
    )?;
    let xml = String::from_utf8(bytes)
        .map_err(|error| Error::InvalidInput(format!("JATS input is not UTF-8: {error}")))?;
    let parent = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."));
    let base_dir = fs::canonicalize(parent)?;
    let (sources, source_limit_exceeded) = collect_graphic_sources(&xml, options.max_xml_events)?;
    let (images, mut warnings) =
        load_local_image_sources(&base_dir, sources, source_limit_exceeded)?;
    let (blocks, parser_warnings) = parse_blocks(&xml, &images, options.max_xml_events)?;
    warnings.extend(parser_warnings);
    if blocks.is_empty() {
        return Err(Error::InvalidInput(
            "JATS article contains no renderable content".into(),
        ));
    }
    render_blocks_to_pages_with_warnings(&blocks, sink, options, &warnings)?;
    Ok(warnings)
}

fn collect_graphic_sources(xml: &str, max_events: usize) -> Result<(Vec<String>, bool)> {
    let mut reader = Reader::from_str(xml);
    reader.config_mut().trim_text(true);
    let mut buffer = Vec::new();
    let mut sources = Vec::new();
    let mut exceeded = false;
    let mut events = 0usize;
    loop {
        events = events.saturating_add(1);
        if events > max_events {
            return Err(Error::LimitExceeded(format!(
                "JATS XML exceeds {max_events} parser events"
            )));
        }
        match reader.read_event_into(&mut buffer)? {
            Event::Start(element) | Event::Empty(element) => {
                let qualified_name = element.name();
                let name = local_name(qualified_name.as_ref());
                if (name == b"graphic" || name == b"inline-graphic")
                    && let Some(href) = attribute(&element, b"href")
                    && !href.trim().is_empty()
                {
                    if sources.len() >= MAX_JATS_IMAGE_REFERENCES {
                        exceeded = true;
                    } else {
                        sources.push(href);
                    }
                }
            }
            Event::DocType(_) => {
                return Err(Error::InvalidInput(
                    "JATS document type declarations are not supported".into(),
                ));
            }
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    Ok((sources, exceeded))
}

fn parse_blocks(
    xml: &str,
    images: &HashMap<String, InlineHtmlImage>,
    max_events: usize,
) -> Result<(Vec<HtmlBlock>, Vec<String>)> {
    let mut reader = Reader::from_str(xml);
    reader.config_mut().trim_text(false);
    let mut buffer = Vec::new();
    let mut stack = Vec::<String>::new();
    let mut blocks = Vec::new();
    let mut warnings = Vec::new();
    let mut section_depth = 0usize;
    let mut heading = None::<String>;
    let mut paragraph = None::<String>;
    let mut caption = None::<String>;
    let mut label = None::<String>;
    let mut table = None::<JatsTable>;
    let mut rendered_text_bytes = 0usize;
    let mut event_count = 0usize;
    let mut title_info = false;

    loop {
        event_count = event_count.saturating_add(1);
        if event_count > max_events {
            return Err(Error::LimitExceeded(format!(
                "JATS XML exceeds {max_events} parser events"
            )));
        }
        match reader.read_event_into(&mut buffer)? {
            Event::Start(element) => {
                let qualified = element.name();
                let name = local_name(qualified.as_ref());
                let local = String::from_utf8_lossy(name).into_owned();
                match name {
                    b"title-group" => title_info = true,
                    b"article-title" if title_info => heading = Some(String::new()),
                    b"abstract" => {
                        push_warning_once(
                            &mut warnings,
                            "JATS abstract and article text layout is approximated",
                        );
                    }
                    b"sec" => section_depth = section_depth.saturating_add(1),
                    b"title" if !title_info => heading = Some(String::new()),
                    b"p" | b"subtitle" | b"named-content" | b"disp-quote" => {
                        if caption.is_none() && label.is_none() {
                            paragraph = Some(String::new());
                        }
                    }
                    b"caption" => caption = Some(String::new()),
                    b"label" => label = Some(String::new()),
                    b"table" => table = Some(JatsTable::default()),
                    b"tr" if table.is_some() => {
                        if let Some(table) = table.as_mut() {
                            table.row.clear();
                        }
                    }
                    b"th" | b"td" if table.is_some() => {
                        if let Some(table) = table.as_mut() {
                            table.cell.clear();
                            table.in_cell = true;
                        }
                    }
                    _ => {}
                }
                if stack.len() >= MAX_JATS_XML_DEPTH {
                    return Err(Error::LimitExceeded(format!(
                        "JATS XML nesting exceeds {MAX_JATS_XML_DEPTH}"
                    )));
                }
                stack.push(local);
            }
            Event::Empty(element) => {
                let qualified_name = element.name();
                let name = local_name(qualified_name.as_ref());
                if name == b"graphic" || name == b"inline-graphic" {
                    if let Some(href) = attribute(&element, b"href") {
                        append_graphic(&mut blocks, &mut warnings, images, &href, &mut paragraph)?;
                    } else {
                        push_warning_once(
                            &mut warnings,
                            "JATS graphic without xlink:href was omitted",
                        );
                    }
                } else if name == b"break" || name == b"hr" {
                    flush_paragraph(&mut blocks, &mut paragraph, &mut rendered_text_bytes)?;
                    blocks.push(HtmlBlock::HorizontalRule);
                }
            }
            Event::Text(text) => {
                let value = text
                    .decode()
                    .map_err(|error| Error::InvalidInput(format!("invalid JATS text: {error}")))?;
                let value = quick_xml::escape::unescape(&value).map_err(|error| {
                    Error::InvalidInput(format!("invalid JATS XML text: {error}"))
                })?;
                append_jats_text(
                    &value,
                    &mut heading,
                    &mut paragraph,
                    &mut caption,
                    &mut label,
                    &mut table,
                );
            }
            Event::CData(text) => {
                let value = text
                    .decode()
                    .map_err(|error| Error::InvalidInput(format!("invalid JATS CDATA: {error}")))?;
                append_jats_text(
                    &value,
                    &mut heading,
                    &mut paragraph,
                    &mut caption,
                    &mut label,
                    &mut table,
                );
            }
            Event::GeneralRef(reference) => {
                let value = reference.decode().map_err(|error| {
                    Error::InvalidInput(format!("invalid JATS reference: {error}"))
                })?;
                append_jats_text(
                    &format!("&{value};"),
                    &mut heading,
                    &mut paragraph,
                    &mut caption,
                    &mut label,
                    &mut table,
                );
            }
            Event::End(element) => {
                let qualified = element.name();
                let name = local_name(qualified.as_ref());
                match name {
                    b"article-title" | b"title" if heading.is_some() => {
                        if let Some(text) = heading.take() {
                            let text = clean_jats_text(&text);
                            if !text.is_empty() {
                                blocks.push(HtmlBlock::Heading {
                                    level: if name == b"article-title" {
                                        1
                                    } else {
                                        section_depth.clamp(1, 6) as u8
                                    },
                                    text,
                                });
                            }
                        }
                    }
                    b"p" | b"subtitle" | b"named-content" | b"disp-quote" => {
                        if let Some(text) = paragraph.take() {
                            let text = clean_jats_text(&text);
                            if !text.is_empty() {
                                rendered_text_bytes =
                                    rendered_text_bytes.saturating_add(text.len());
                                if rendered_text_bytes > MAX_JATS_TEXT_BYTES {
                                    return Err(Error::LimitExceeded(format!(
                                        "JATS rendered text exceeds {MAX_JATS_TEXT_BYTES} bytes"
                                    )));
                                }
                                blocks.push(HtmlBlock::Paragraph { text });
                            }
                        }
                    }
                    b"caption" => {
                        if let Some(text) = caption.take() {
                            let text = clean_jats_text(&text);
                            if !text.is_empty() {
                                blocks.push(HtmlBlock::Paragraph {
                                    text: format!("Figure: {text}"),
                                });
                            }
                        }
                    }
                    b"label" => {
                        label = None;
                    }
                    b"th" | b"td" if table.is_some() => {
                        if let Some(table) = table.as_mut() {
                            table.row.push(clean_jats_text(&table.cell));
                            table.cell.clear();
                            table.in_cell = false;
                            if table.row.len() > MAX_JATS_TABLE_CELLS {
                                return Err(Error::LimitExceeded(
                                    "JATS table cell limit exceeded".into(),
                                ));
                            }
                        }
                    }
                    b"tr" if table.is_some() => {
                        if let Some(table) = table.as_mut()
                            && !table.row.is_empty()
                        {
                            table.rows.push(std::mem::take(&mut table.row));
                        }
                    }
                    b"table" => {
                        if let Some(table) = table.take() {
                            let data = finish_jats_table(table);
                            if !data.headers.is_empty() {
                                blocks.push(HtmlBlock::Table(data));
                            }
                        }
                    }
                    b"sec" => section_depth = section_depth.saturating_sub(1),
                    b"title-group" => title_info = false,
                    _ => {}
                }
                stack.pop();
            }
            Event::DocType(_) => {
                return Err(Error::InvalidInput(
                    "JATS document type declarations are not supported".into(),
                ));
            }
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    Ok((blocks, warnings))
}

fn append_jats_text(
    value: &str,
    heading: &mut Option<String>,
    paragraph: &mut Option<String>,
    caption: &mut Option<String>,
    label: &mut Option<String>,
    table: &mut Option<JatsTable>,
) {
    if let Some(table) = table.as_mut()
        && table.in_cell
    {
        table.cell.push_str(value);
        return;
    }
    if let Some(heading) = heading.as_mut() {
        heading.push_str(value);
    } else if let Some(label) = label.as_mut() {
        label.push_str(value);
    } else if let Some(caption) = caption.as_mut() {
        caption.push_str(value);
    } else if let Some(paragraph) = paragraph.as_mut() {
        paragraph.push_str(value);
    }
}

fn append_graphic(
    blocks: &mut Vec<HtmlBlock>,
    warnings: &mut Vec<String>,
    images: &HashMap<String, InlineHtmlImage>,
    href: &str,
    paragraph: &mut Option<String>,
) -> Result<()> {
    let Some(image) = images.get(href) else {
        push_warning_once(
            warnings,
            "JATS graphic was omitted because it was not a validated local PNG/JPEG resource",
        );
        return Ok(());
    };
    if let Some(text) = paragraph.take()
        && !text.trim().is_empty()
    {
        blocks.push(HtmlBlock::Paragraph {
            text: clean_jats_text(&text),
        });
    }
    blocks.push(HtmlBlock::Image {
        href: image.href.clone(),
        pixel_width: image.pixel_width,
        pixel_height: image.pixel_height,
        alt: "JATS graphic".into(),
    });
    Ok(())
}

fn flush_paragraph(
    blocks: &mut Vec<HtmlBlock>,
    paragraph: &mut Option<String>,
    _bytes: &mut usize,
) -> Result<()> {
    if let Some(text) = paragraph.take()
        && !text.trim().is_empty()
    {
        blocks.push(HtmlBlock::Paragraph {
            text: clean_jats_text(&text),
        });
    }
    Ok(())
}

fn finish_jats_table(table: JatsTable) -> TableData {
    let mut rows = table.rows;
    let mut headers = rows.first().cloned().unwrap_or_default();
    if !rows.is_empty() {
        rows.remove(0);
    }
    let columns = headers
        .len()
        .max(rows.iter().map(Vec::len).max().unwrap_or(0))
        .max(1);
    headers.resize(columns, String::new());
    for row in &mut rows {
        row.resize(columns, String::new());
    }
    TableData {
        headers,
        rows,
        alignments: vec![TableAlign::Left; columns],
        raw_source: String::new(),
    }
}

fn clean_jats_text(value: &str) -> String {
    value.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn push_warning_once(warnings: &mut Vec<String>, warning: &str) {
    if !warnings.iter().any(|existing| existing == warning) {
        warnings.push(warning.to_owned());
    }
}