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
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
494
495
496
497
498
499
500
501
502
503
//! Bounded OpenFOAM vol/surface field previews.
//!
//! This parser summarizes standalone OpenFOAM field files without opening the
//! case, running a solver, evaluating boundary conditions, or displaying the
//! internal value list. It accepts ASCII FoamFile dictionaries and supports
//! uniform and nonuniform scalar/vector fields.

use std::path::Path;

use crate::convert::{ConvertOptions, PageConsumer, read_limited_file};
use crate::document::html::{HtmlBlock, render_blocks_to_pages};
use crate::error::{Error, Result};
use crate::table::{TableAlign, TableData};

const MAX_FIELD_BYTES: u64 = 128 * 1024 * 1024;
const MAX_FIELD_TOKENS: usize = 20_000_000;
const MAX_FIELD_ENTRIES: usize = 1_000_000;
const MAX_FIELD_PATCHES: usize = 100_000;
const MAX_FIELD_STRING_BYTES: usize = 512 * 1024;

pub(crate) fn looks_like_prefix(bytes: &[u8]) -> bool {
    let text = String::from_utf8_lossy(bytes);
    text.contains("FoamFile")
        && text.contains("internalField")
        && (text.contains("volScalarField")
            || text.contains("volVectorField")
            || text.contains("surfaceScalarField")
            || text.contains("surfaceVectorField"))
}

struct FieldPageSink<'a> {
    inner: &'a mut dyn PageConsumer,
    warnings: &'a [String],
}

impl PageConsumer for FieldPageSink<'_> {
    fn consume(&mut self, mut page: crate::ir::Page) -> Result<()> {
        page.source_format = "openfoam-field".into();
        if page.title.is_empty() {
            page.title = "OpenFOAM field".into();
        }
        page.description =
            "OpenFOAM field metadata and bounded value statistics are rendered inertly; solver and boundary operations are never performed".into();
        for warning in self.warnings {
            page.warn(warning.clone());
        }
        self.inner.consume(page)
    }
}

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_FIELD_BYTES),
        "OpenFOAM field input",
    )?;
    let (table, metadata, warnings) = parse(&bytes)?;
    let blocks = vec![
        HtmlBlock::Heading {
            level: 1,
            text: "OpenFOAM field".into(),
        },
        HtmlBlock::Paragraph { text: metadata },
        HtmlBlock::Table(table),
    ];
    let mut page_sink = FieldPageSink {
        inner: sink,
        warnings: &warnings,
    };
    render_blocks_to_pages(&blocks, &mut page_sink, options)?;
    Ok(warnings)
}

#[derive(Clone, Debug)]
struct FieldStats {
    class: String,
    object: String,
    location: String,
    format: String,
    internal: String,
    value_type: String,
    entries: usize,
    minimum: Option<f64>,
    maximum: Option<f64>,
    patches: usize,
}

fn parse(bytes: &[u8]) -> Result<(TableData, String, Vec<String>)> {
    if bytes.len() as u64 > MAX_FIELD_BYTES {
        return Err(Error::LimitExceeded(format!(
            "OpenFOAM field exceeds {MAX_FIELD_BYTES} bytes"
        )));
    }
    let text = std::str::from_utf8(bytes).map_err(|error| {
        Error::InvalidInput(format!("OpenFOAM field must be ASCII/UTF-8: {error}"))
    })?;
    let tokens = tokenize(text)?;
    let mut cursor = Cursor {
        tokens: &tokens,
        index: 0,
    };
    let mut stats = FieldStats {
        class: String::new(),
        object: String::new(),
        location: String::new(),
        format: String::new(),
        internal: String::new(),
        value_type: String::new(),
        entries: 0,
        minimum: None,
        maximum: None,
        patches: 0,
    };
    cursor.expect("FoamFile")?;
    cursor.expect("{")?;
    let mut header_depth = 1usize;
    while header_depth > 0 {
        let token = cursor.next().ok_or_else(|| {
            Error::InvalidInput("OpenFOAM field FoamFile header is truncated".into())
        })?;
        match token {
            "{" => header_depth += 1,
            "}" => header_depth -= 1,
            "class" | "object" | "location" | "format" => {
                let key = token;
                let value = cursor.next().ok_or_else(|| {
                    Error::InvalidInput(format!("OpenFOAM field header {key} is missing"))
                })?;
                match key {
                    "class" => stats.class = value.to_owned(),
                    "object" => stats.object = value.to_owned(),
                    "location" => stats.location = value.to_owned(),
                    "format" => stats.format = value.to_owned(),
                    _ => {}
                }
            }
            _ => {}
        }
    }
    if !stats.format.is_empty() && !stats.format.eq_ignore_ascii_case("ascii") {
        return Err(Error::Unsupported(format!(
            "OpenFOAM field format '{}' is unsupported; ASCII is required",
            stats.format
        )));
    }
    while let Some(token) = cursor.next() {
        match token {
            "internalField" => {
                parse_internal_field(&mut cursor, &mut stats)?;
            }
            "boundaryField" => {
                stats.patches = parse_patch_count(&mut cursor)?;
            }
            _ => {}
        }
    }
    if stats.class.is_empty() {
        return Err(Error::InvalidInput(
            "OpenFOAM field header has no class".into(),
        ));
    }
    if stats.internal.is_empty() {
        return Err(Error::InvalidInput(
            "OpenFOAM field has no internalField".into(),
        ));
    }
    let minimum = stats.minimum.map(format_number);
    let maximum = stats.maximum.map(format_number);
    let rows = vec![vec![
        truncate(&stats.object),
        truncate(&stats.class),
        truncate(&stats.internal),
        stats.entries.to_string(),
        minimum.unwrap_or_else(|| "".into()),
        maximum.unwrap_or_else(|| "".into()),
        stats.patches.to_string(),
    ]];
    let object_meta = if stats.object.is_empty() {
        "".into()
    } else {
        truncate(&stats.object)
    };
    let location_meta = if stats.location.is_empty() {
        "".into()
    } else {
        truncate(&stats.location)
    };
    let metadata = format!(
        "Class: {}\nObject: {}\nLocation: {}\nInternal field: {}\nEntries: {}\nBoundary patches: {}",
        truncate(&stats.class),
        object_meta,
        location_meta,
        stats.value_type,
        stats.entries,
        stats.patches
    );
    let warnings = vec![
        "OpenFOAM field values, dimensions, boundary condition dictionaries, patch values, directives and case paths are omitted; solver, filesystem traversal, command and network operations are never performed".into(),
        "Uniform/nonuniform scalar or vector value statistics are computed locally without retaining the value list".into(),
    ];
    Ok((
        TableData {
            headers: vec![
                "Obj".into(),
                "Cls".into(),
                "Mode".into(),
                "N".into(),
                "Min".into(),
                "Max".into(),
                "Pch".into(),
            ],
            rows,
            alignments: vec![TableAlign::Left; 7],
            raw_source: String::new(),
        },
        metadata,
        warnings,
    ))
}

fn parse_internal_field(cursor: &mut Cursor<'_>, stats: &mut FieldStats) -> Result<()> {
    let kind = cursor
        .next()
        .ok_or_else(|| Error::InvalidInput("OpenFOAM internalField kind is missing".into()))?;
    match kind {
        "uniform" => {
            stats.internal = "uniform".into();
            if stats.class.contains("Vector") {
                let vector = parse_vector(cursor)?;
                stats.value_type = "uniform vector magnitude".into();
                stats.entries = 1;
                stats.minimum = Some(vector);
                stats.maximum = Some(vector);
            } else {
                let value = parse_number(cursor.next().ok_or_else(|| {
                    Error::InvalidInput("OpenFOAM uniform scalar is missing".into())
                })?)?;
                stats.value_type = "uniform scalar".into();
                stats.entries = 1;
                stats.minimum = Some(value);
                stats.maximum = Some(value);
            }
        }
        "nonuniform" => {
            stats.internal = "nonuniform".into();
            let _list = cursor.next();
            let _less = cursor.next();
            let value_type = cursor.next().unwrap_or("scalar");
            let _greater = cursor.next();
            let count = cursor
                .next()
                .ok_or_else(|| {
                    Error::InvalidInput("OpenFOAM nonuniform field count is missing".into())
                })?
                .parse::<usize>()
                .map_err(|_| {
                    Error::InvalidInput("OpenFOAM nonuniform field count is invalid".into())
                })?;
            if count > MAX_FIELD_ENTRIES {
                return Err(Error::LimitExceeded(format!(
                    "OpenFOAM field entries exceed {MAX_FIELD_ENTRIES}"
                )));
            }
            cursor.expect("(")?;
            stats.entries = count;
            stats.value_type = format!("nonuniform {value_type}");
            for _ in 0..count {
                let value = if value_type.eq_ignore_ascii_case("vector")
                    || stats.class.contains("Vector")
                {
                    parse_vector(cursor)?
                } else {
                    parse_number(cursor.next().ok_or_else(|| {
                        Error::InvalidInput("OpenFOAM nonuniform value is missing".into())
                    })?)?
                };
                stats.minimum = Some(stats.minimum.map_or(value, |current| current.min(value)));
                stats.maximum = Some(stats.maximum.map_or(value, |current| current.max(value)));
            }
            cursor.expect(")")?;
        }
        _ => {
            return Err(Error::Unsupported(format!(
                "OpenFOAM internalField kind '{kind}' is unsupported"
            )));
        }
    }
    Ok(())
}

fn parse_patch_count(cursor: &mut Cursor<'_>) -> Result<usize> {
    cursor.expect("{")?;
    let mut depth = 1usize;
    let mut patches = 0usize;
    while depth > 0 {
        let token = cursor
            .next()
            .ok_or_else(|| Error::InvalidInput("OpenFOAM boundaryField is truncated".into()))?;
        match token {
            "{" => depth += 1,
            "}" => depth -= 1,
            _ if depth == 1 => {
                patches = patches.saturating_add(1);
                if patches > MAX_FIELD_PATCHES {
                    return Err(Error::LimitExceeded(format!(
                        "OpenFOAM field patches exceed {MAX_FIELD_PATCHES}"
                    )));
                }
            }
            _ => {}
        }
    }
    Ok(patches)
}

struct Cursor<'a> {
    tokens: &'a [String],
    index: usize,
}

impl<'a> Cursor<'a> {
    fn next(&mut self) -> Option<&'a str> {
        let token = self.tokens.get(self.index)?;
        self.index += 1;
        Some(token)
    }

    fn expect(&mut self, expected: &str) -> Result<()> {
        let actual = self
            .next()
            .ok_or_else(|| Error::InvalidInput(format!("OpenFOAM field expected '{expected}'")))?;
        if actual != expected {
            return Err(Error::InvalidInput(format!(
                "OpenFOAM field expected '{expected}', found '{actual}'"
            )));
        }
        Ok(())
    }
}

fn tokenize(text: &str) -> Result<Vec<String>> {
    let mut tokens = Vec::new();
    let mut chars = text.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch.is_whitespace() {
            continue;
        }
        if ch == '/' && chars.peek() == Some(&'/') {
            chars.next();
            for next in chars.by_ref() {
                if next == '\n' {
                    break;
                }
            }
            continue;
        }
        if ch == '/' && chars.peek() == Some(&'*') {
            chars.next();
            let mut previous = '\0';
            loop {
                let next = chars.next().ok_or_else(|| {
                    Error::InvalidInput("unterminated OpenFOAM field comment".into())
                })?;
                if previous == '*' && next == '/' {
                    break;
                }
                previous = next;
            }
            continue;
        }
        if matches!(ch, '{' | '}' | '(' | ')' | '[' | ']' | '<' | '>' | ';') {
            tokens.push(ch.to_string());
        } else if ch == '"' || ch == '\'' {
            let quote = ch;
            let mut value = String::new();
            for next in chars.by_ref() {
                if next == quote {
                    break;
                }
                value.push(next);
                if value.len() > MAX_FIELD_STRING_BYTES {
                    return Err(Error::LimitExceeded(format!(
                        "OpenFOAM field token exceeds {MAX_FIELD_STRING_BYTES} bytes"
                    )));
                }
            }
            tokens.push(value);
        } else {
            let mut value = String::from(ch);
            while let Some(&next) = chars.peek() {
                if next.is_whitespace()
                    || matches!(next, '{' | '}' | '(' | ')' | '[' | ']' | '<' | '>' | ';')
                {
                    break;
                }
                value.push(next);
                chars.next();
                if value.len() > MAX_FIELD_STRING_BYTES {
                    return Err(Error::LimitExceeded(format!(
                        "OpenFOAM field token exceeds {MAX_FIELD_STRING_BYTES} bytes"
                    )));
                }
            }
            tokens.push(value);
        }
        if tokens.len() > MAX_FIELD_TOKENS {
            return Err(Error::LimitExceeded(format!(
                "OpenFOAM field tokens exceed {MAX_FIELD_TOKENS}"
            )));
        }
    }
    Ok(tokens)
}

fn parse_number(value: &str) -> Result<f64> {
    let value = value.trim_end_matches(';');
    let number = value
        .parse::<f64>()
        .map_err(|_| Error::InvalidInput(format!("invalid OpenFOAM field scalar '{value}'")))?;
    if !number.is_finite() || number.abs() > 1e12 {
        return Err(Error::InvalidInput(
            "OpenFOAM field scalar is non-finite or outside ±1e12".into(),
        ));
    }
    Ok(number)
}

fn parse_vector(cursor: &mut Cursor<'_>) -> Result<f64> {
    cursor.expect("(")?;
    let x = parse_number(
        cursor
            .next()
            .ok_or_else(|| Error::InvalidInput("OpenFOAM vector x is missing".into()))?,
    )?;
    let y = parse_number(
        cursor
            .next()
            .ok_or_else(|| Error::InvalidInput("OpenFOAM vector y is missing".into()))?,
    )?;
    let z = parse_number(
        cursor
            .next()
            .ok_or_else(|| Error::InvalidInput("OpenFOAM vector z is missing".into()))?,
    )?;
    cursor.expect(")")?;
    let magnitude = x.hypot(y).hypot(z);
    if !magnitude.is_finite() || magnitude > 1e12 {
        return Err(Error::InvalidInput(
            "OpenFOAM vector magnitude is non-finite or outside ±1e12".into(),
        ));
    }
    Ok(magnitude)
}

fn format_number(value: f64) -> String {
    if value == 0.0 {
        "0".into()
    } else {
        format!("{value:.6}")
    }
}

fn truncate(value: &str) -> String {
    if value.len() <= MAX_FIELD_STRING_BYTES {
        return value.to_owned();
    }
    let mut end = MAX_FIELD_STRING_BYTES;
    while !value.is_char_boundary(end) {
        end -= 1;
    }
    format!("{}", &value[..end])
}

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

    #[test]
    fn recognizes_field_header() {
        assert!(looks_like_prefix(
            b"FoamFile { class volScalarField; } internalField uniform 0;"
        ));
        assert!(!looks_like_prefix(b"FoamFile { class dictionary; }"));
    }

    #[test]
    fn summarizes_scalar_and_vector_values() {
        let scalar = b"FoamFile { version 2.0; format ascii; class volScalarField; object p; } internalField nonuniform List<scalar> 2 ( 1.0 -2.0 ); boundaryField { inlet { type fixedValue; } outlet { type zeroGradient; } }";
        let (table, metadata, warnings) = parse(scalar).unwrap();
        assert!(metadata.contains("Entries: 2"));
        assert_eq!(table.rows[0][4], "-2.000000");
        assert_eq!(table.rows[0][6], "2");
        assert!(warnings.iter().any(|warning| warning.contains("never")));
        let vector = b"FoamFile { format ascii; class volVectorField; object U; } internalField uniform ( 3 4 0 ); boundaryField { inlet {} }";
        let (table, _, _) = parse(vector).unwrap();
        assert_eq!(table.rows[0][3], "1");
        assert_eq!(table.rows[0][4], "5.000000");
    }
}