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
//! Bounded LAS/LAZ point-cloud preview.
//!
//! Samples coordinates (and optional RGB) from ASPRS LAS 1.0–1.4 files, then
//! reuses the PLY point renderer. The converter never fetches external files or
//! interprets waveform/extra-byte payloads.

use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::Path;

use crate::convert::{ConvertOptions, PageConsumer};
use crate::error::{Error, Result};
use crate::ir::{Node, Page};

const MAX_LAS_FILE_BYTES: u64 = 2 * 1024 * 1024 * 1024;
const MAX_LAS_VLR_BYTES: u64 = 16 * 1024 * 1024;
const MAX_LAS_EVLRS_BYTES: u64 = 8 * 1024 * 1024;
const MAX_LAS_VLRS: u32 = 100_000;
const MAX_LAS_POINT_RECORD_BYTES: u16 = 4096;
const MAX_LAZ_POINTS_TO_DECODE: u64 = 20_000_000;
const MAX_LAS_PREVIEW_POINTS: u64 = 200_000;
const MAX_LAS_PLY_BYTES: usize = 32 * 1024 * 1024;
const MAX_LAS_COORDINATE: f64 = 1.0e12;
const POINT_BATCH_SIZE: u64 = 8192;

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

impl PageConsumer for LasPageSink<'_> {
    fn consume(&mut self, mut page: Page) -> Result<()> {
        page.source_format = "las".into();
        page.title = "LAS/LAZ Point Cloud".into();
        page.description = format!(
            "LAS/LAZ point cloud preview with {} sampled points",
            self.point_count
        );
        for warning in self.warnings {
            page.warn(warning.clone());
        }
        for node in &mut page.nodes {
            if let Node::Path { meta, .. } = node
                && meta.kind == "ply-point-cloud"
            {
                meta.kind = "las-point-cloud".into();
                meta.semantic_role = "las:point-cloud".into();
            }
        }
        self.inner.consume(page)
    }
}

pub(crate) fn convert(
    path: &Path,
    options: &ConvertOptions,
    sink: &mut dyn PageConsumer,
) -> Result<Vec<String>> {
    let mut file = File::open(path)?;
    let mut warnings = preflight(&mut file, path)?;
    file.seek(SeekFrom::Start(0))?;
    let mut reader = las::Reader::new(BufReader::new(file))
        .map_err(|error| Error::InvalidInput(format!("invalid LAS/LAZ file: {error}")))?;
    let header = reader.header();
    let point_count = header.number_of_points();
    let compressed = header.point_format().is_compressed;
    if point_count == 0 {
        return Err(Error::InvalidInput(
            "LAS/LAZ file contains no points".into(),
        ));
    }
    if compressed && point_count > MAX_LAZ_POINTS_TO_DECODE {
        return Err(Error::LimitExceeded(format!(
            "LAZ contains {point_count} points; maximum sequential decode count is {MAX_LAZ_POINTS_TO_DECODE}"
        )));
    }

    if header.has_crs_vlrs() {
        warnings.push(
            "LAS/LAZ CRS metadata was detected but coordinates were not transformed; stored XY coordinates were projected as-is".into(),
        );
    } else {
        warnings.push(
            "LAS/LAZ coordinates were projected as stored; no CRS transformation was applied"
                .into(),
        );
    }
    warnings.push(
        "LAS/LAZ waveform, classification, intensity, GPS time, NIR, and extra-byte attributes are not shown".into(),
    );
    if header.evlrs().len() > 1 {
        warnings.push("additional LAS/LAZ extended variable-length records were ignored".into());
    }

    let mut ply_data = String::new();
    let has_color = header.point_format().has_color;
    let mut point_data = las::PointDataBuilder::new()
        .for_header(reader.header())
        .build();

    let mut sampled = 0usize;
    let mut invalid_points = 0u64;
    let target_points = point_count.min(MAX_LAS_PREVIEW_POINTS);
    if compressed {
        let mut index = 0u64;
        let mut sample_index = 0u64;
        while index < point_count {
            let read_count = reader
                .fill_points(POINT_BATCH_SIZE.min(point_count - index), &mut point_data)
                .map_err(|error| {
                    Error::InvalidInput(format!("failed to decode LAZ points: {error}"))
                })?;
            if read_count == 0 {
                return Err(Error::InvalidInput(format!(
                    "LAZ ended after {index} points; header declares {point_count}"
                )));
            }
            let mut xs = point_data.x();
            let mut ys = point_data.y();
            let mut zs = point_data.z();
            let mut colors = point_data.rgb();
            for _ in 0..read_count {
                let coordinates = next_coordinates(&mut xs, &mut ys, &mut zs)?;
                let color = next_color(&mut colors);
                if sample_index < target_points
                    && index == sample_index * point_count / target_points
                {
                    if append_point(&mut ply_data, coordinates, color, has_color)? {
                        sampled += 1;
                    } else {
                        invalid_points += 1;
                    }
                    sample_index += 1;
                }
                index += 1;
            }
        }
    } else {
        for sample_index in 0..target_points {
            let index = sample_index * point_count / target_points;
            reader.seek(index).map_err(|error| {
                Error::InvalidInput(format!("failed to seek LAS point {index}: {error}"))
            })?;
            let read_count = reader.fill_points(1, &mut point_data).map_err(|error| {
                Error::InvalidInput(format!("failed to decode LAS point {index}: {error}"))
            })?;
            if read_count == 0 {
                return Err(Error::InvalidInput(format!(
                    "LAS ended before point {index}"
                )));
            }
            let mut xs = point_data.x();
            let mut ys = point_data.y();
            let mut zs = point_data.z();
            let mut colors = point_data.rgb();
            let coordinates = next_coordinates(&mut xs, &mut ys, &mut zs)?;
            if append_point(
                &mut ply_data,
                coordinates,
                next_color(&mut colors),
                has_color,
            )? {
                sampled += 1;
            } else {
                invalid_points += 1;
            }
        }
    }
    if sampled == 0 {
        return Err(Error::InvalidInput(
            "LAS/LAZ contains no finite, in-range XYZ points".into(),
        ));
    }
    let mut ply = String::with_capacity(ply_data.len().saturating_add(160));
    ply.push_str("ply\nformat ascii 1.0\n");
    ply.push_str(&format!(
        "element vertex {sampled}\nproperty double x\nproperty double y\nproperty double z\n"
    ));
    if has_color {
        ply.push_str("property uchar red\nproperty uchar green\nproperty uchar blue\n");
    }
    ply.push_str("end_header\n");
    ply.push_str(&ply_data);
    if invalid_points > 0 {
        warnings.push(format!(
            "{invalid_points} sampled LAS/LAZ point(s) with non-finite or out-of-range coordinates were omitted"
        ));
    }
    if point_count > MAX_LAS_PREVIEW_POINTS {
        warnings.push(format!(
            "LAS/LAZ point cloud was evenly sampled from {point_count} source points to at most {MAX_LAS_PREVIEW_POINTS} preview points"
        ));
    }
    if ply.len() > MAX_LAS_PLY_BYTES {
        return Err(Error::LimitExceeded(format!(
            "LAS/LAZ point preview intermediate exceeds {MAX_LAS_PLY_BYTES} bytes"
        )));
    }
    let mut page_sink = LasPageSink {
        inner: sink,
        warnings: &warnings,
        point_count: sampled,
    };
    let render_warnings = crate::cad::ply::convert(ply.as_bytes(), options, &mut page_sink)?;
    warnings.extend(render_warnings);
    Ok(warnings)
}

fn next_coordinates(
    xs: &mut impl Iterator<Item = f64>,
    ys: &mut impl Iterator<Item = f64>,
    zs: &mut impl Iterator<Item = f64>,
) -> Result<[f64; 3]> {
    Ok([
        xs.next().ok_or_else(|| {
            Error::InvalidInput("LAS point batch is missing x coordinates".into())
        })?,
        ys.next().ok_or_else(|| {
            Error::InvalidInput("LAS point batch is missing y coordinates".into())
        })?,
        zs.next().ok_or_else(|| {
            Error::InvalidInput("LAS point batch is missing z coordinates".into())
        })?,
    ])
}

fn next_color(colors: &mut Option<impl Iterator<Item = (u16, u16, u16)>>) -> Option<[u16; 3]> {
    colors
        .as_mut()
        .and_then(Iterator::next)
        .map(|(red, green, blue)| [red, green, blue])
}

fn append_point(
    output: &mut String,
    [x, y, z]: [f64; 3],
    color: Option<[u16; 3]>,
    has_color: bool,
) -> Result<bool> {
    if ![x, y, z]
        .iter()
        .all(|value| value.is_finite() && value.abs() <= MAX_LAS_COORDINATE)
    {
        return Ok(false);
    }
    let color = color
        .map(|color| color.map(|channel| ((u32::from(channel) * 255 + 32_767) / 65_535) as u8));
    let record = if has_color {
        let [red, green, blue] = color.unwrap_or([37, 99, 235]);
        format!("{:.17e} {:.17e} {:.17e} {red} {green} {blue}\n", x, y, z)
    } else {
        format!("{x:.17e} {y:.17e} {z:.17e}\n")
    };
    if output.len().saturating_add(record.len()) > MAX_LAS_PLY_BYTES {
        return Err(Error::LimitExceeded(format!(
            "LAS/LAZ point preview intermediate exceeds {MAX_LAS_PLY_BYTES} bytes"
        )));
    }
    output.push_str(&record);
    Ok(true)
}

fn preflight(file: &mut File, path: &Path) -> Result<Vec<String>> {
    let file_len = file.metadata()?.len();
    if file_len > MAX_LAS_FILE_BYTES {
        return Err(Error::LimitExceeded(format!(
            "LAS/LAZ file is {file_len} bytes; maximum is {MAX_LAS_FILE_BYTES}"
        )));
    }
    let raw = las::raw::Header::read_from(&mut *file)
        .map_err(|error| Error::InvalidInput(format!("invalid LAS/LAZ header: {error}")))?;
    if raw.version.major != 1 || raw.version.minor > 4 {
        return Err(Error::Unsupported(format!(
            "LAS version {}.{} is unsupported; supported versions are 1.0 through 1.4",
            raw.version.major, raw.version.minor
        )));
    }
    let minimum_header_size = raw.version.header_size();
    if raw.header_size < minimum_header_size || u64::from(raw.header_size) > file_len {
        return Err(Error::InvalidInput(format!(
            "LAS header size {} is invalid for version {}.{}",
            raw.header_size, raw.version.major, raw.version.minor
        )));
    }
    let point_offset = u64::from(raw.offset_to_point_data);
    if point_offset < u64::from(raw.header_size) || point_offset > file_len {
        return Err(Error::InvalidInput(
            "LAS point-data offset is outside the file".into(),
        ));
    }
    let vlr_bytes = point_offset - u64::from(raw.header_size);
    if vlr_bytes > MAX_LAS_VLR_BYTES {
        return Err(Error::LimitExceeded(format!(
            "LAS variable-length-record area exceeds {MAX_LAS_VLR_BYTES} bytes"
        )));
    }
    if raw.number_of_variable_length_records > MAX_LAS_VLRS
        || u64::from(raw.number_of_variable_length_records) > vlr_bytes / 54
    {
        return Err(Error::LimitExceeded(
            "LAS declares more variable-length records than fit in the bounded header area".into(),
        ));
    }
    let format_id = raw.point_data_record_format & 0x3f;
    if raw.point_data_record_format & 0x40 != 0 {
        return Err(Error::Unsupported(
            "LAS point format uses a reserved compression flag".into(),
        ));
    }
    let format = las::point::Format::new(format_id)
        .map_err(|error| Error::Unsupported(format!("unsupported LAS point format: {error}")))?;
    if !raw.version.supports_point_format(format) {
        return Err(Error::InvalidInput(format!(
            "LAS point format {format_id} is not valid for version {}.{}",
            raw.version.major, raw.version.minor
        )));
    }
    if raw.point_data_record_length < format.len() {
        return Err(Error::InvalidInput(format!(
            "LAS point record length {} is shorter than point format {} minimum {}",
            raw.point_data_record_length,
            format_id,
            format.len()
        )));
    }
    if raw.point_data_record_length > MAX_LAS_POINT_RECORD_BYTES {
        return Err(Error::LimitExceeded(format!(
            "LAS point record length {} exceeds {MAX_LAS_POINT_RECORD_BYTES} bytes",
            raw.point_data_record_length
        )));
    }

    let point_count = if raw.number_of_point_records > 0 {
        u64::from(raw.number_of_point_records)
    } else {
        raw.large_file
            .map(|header| header.number_of_point_records)
            .unwrap_or(0)
    };
    if point_count == 0 {
        return Err(Error::InvalidInput(
            "LAS/LAZ file contains no points".into(),
        ));
    }
    let compressed = raw.point_data_record_format & 0x80 != 0;
    if compressed && point_count > MAX_LAZ_POINTS_TO_DECODE {
        return Err(Error::LimitExceeded(format!(
            "LAZ declares {point_count} points; maximum sequential decode count is {MAX_LAZ_POINTS_TO_DECODE}"
        )));
    }
    if !compressed {
        let point_bytes = point_count
            .checked_mul(u64::from(raw.point_data_record_length))
            .ok_or_else(|| Error::LimitExceeded("LAS point-data size overflowed".into()))?;
        let end = point_offset
            .checked_add(point_bytes)
            .ok_or_else(|| Error::LimitExceeded("LAS point-data offset overflowed".into()))?;
        if end > file_len {
            return Err(Error::InvalidInput(format!(
                "LAS point records require {end} bytes but file contains {file_len}"
            )));
        }
    }
    let mut warnings = Vec::new();
    if raw.point_data_record_format & 0x80 != 0
        && path
            .extension()
            .and_then(|ext| ext.to_str())
            .is_some_and(|ext| ext.eq_ignore_ascii_case("las"))
    {
        warnings.push("compressed LAS point data was detected despite the .las extension".into());
    } else if raw.point_data_record_format & 0x80 == 0
        && path
            .extension()
            .and_then(|ext| ext.to_str())
            .is_some_and(|ext| ext.eq_ignore_ascii_case("laz"))
    {
        warnings.push("uncompressed LAS point data was detected despite the .laz extension".into());
    }
    if let Some(evlr) = raw.evlr {
        if evlr.number_of_evlrs > 100_000 {
            return Err(Error::LimitExceeded(
                "LAS extended variable-length-record count exceeds 100000".into(),
            ));
        }
        if evlr.number_of_evlrs > 0 {
            let offset = evlr.start_of_first_evlr;
            if offset.checked_add(60).is_none_or(|end| end > file_len) {
                return Err(Error::InvalidInput(
                    "LAS extended variable-length-record header is outside the file".into(),
                ));
            }
            file.seek(SeekFrom::Start(offset))?;
            let mut record_header = [0u8; 60];
            file.read_exact(&mut record_header)?;
            let record_bytes = u64::from_le_bytes(record_header[20..28].try_into().unwrap());
            if record_bytes > MAX_LAS_EVLRS_BYTES {
                return Err(Error::LimitExceeded(format!(
                    "LAS first extended variable-length record exceeds {MAX_LAS_EVLRS_BYTES} bytes"
                )));
            }
            if offset
                .checked_add(60)
                .and_then(|start| start.checked_add(record_bytes))
                .is_none_or(|end| end > file_len)
            {
                return Err(Error::InvalidInput(
                    "LAS extended variable-length record extends beyond the file".into(),
                ));
            }
            if !compressed {
                let point_bytes = point_count
                    .checked_mul(u64::from(raw.point_data_record_length))
                    .ok_or_else(|| Error::LimitExceeded("LAS point-data size overflowed".into()))?;
                let end_of_points = point_offset.checked_add(point_bytes).ok_or_else(|| {
                    Error::LimitExceeded("LAS point-data offset overflowed".into())
                })?;
                if offset < end_of_points {
                    return Err(Error::InvalidInput(
                        "LAS extended variable-length record overlaps point data".into(),
                    ));
                }
                if offset - end_of_points > MAX_LAS_VLR_BYTES {
                    return Err(Error::LimitExceeded(format!(
                        "LAS padding before extended records exceeds {MAX_LAS_VLR_BYTES} bytes"
                    )));
                }
            }
        }
    }
    Ok(warnings)
}