litchi 0.0.1

High-performance parser for Microsoft Office, OpenDocument, and Apple iWork file formats with unified API
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
// EMF to SVG converter with SIMD acceleration
//
// Converts Enhanced Metafile vector graphics to SVG while extracting embedded raster images

use super::parser::{EmfParser, EmfRecord};
use crate::common::error::{Error, Result};
use crate::images::svg::*;
use rayon::prelude::*;
use zerocopy::FromBytes;

/// EMF to SVG converter
pub struct EmfSvgConverter {
    parser: EmfParser,
}

/// EMF RECT structure (Windows RECT)
#[derive(Debug, Clone, FromBytes)]
#[repr(C)]
struct EmfRect {
    left: i32,
    top: i32,
    right: i32,
    bottom: i32,
}

/// EMF POINT structure
#[derive(Debug, Clone, FromBytes)]
#[repr(C)]
struct EmfPoint {
    x: i32,
    y: i32,
}

/// EMF SIZE structure
#[derive(Debug, Clone, FromBytes)]
#[repr(C)]
#[allow(dead_code)]
struct EmfSize {
    cx: i32,
    cy: i32,
}

/// EMR_STRETCHDIBITS record data
#[derive(Debug, Clone, FromBytes)]
#[repr(C)]
struct EmfStretchDibits {
    bounds: EmfRect,
    x_dest: i32,
    y_dest: i32,
    x_src: i32,
    y_src: i32,
    cx_src: i32,
    cy_src: i32,
    off_bmi_src: u32,
    cb_bmi_src: u32,
    off_bits_src: u32,
    cb_bits_src: u32,
    usage_src: u32,
    dw_rop: u32,
    cx_dest: i32,
    cy_dest: i32,
}

/// Polygon/polyline record header (bounds + count)
#[derive(Debug, Clone, FromBytes)]
#[repr(C)]
struct EmfPolygonHeader {
    bounds: EmfRect,
    count: u32,
}

impl EmfSvgConverter {
    /// Create a new EMF to SVG converter
    pub fn new(parser: EmfParser) -> Self {
        Self { parser }
    }

    /// Convert EMF to SVG
    ///
    /// This processes all EMF records in parallel where possible and generates
    /// an SVG document with vector graphics and embedded raster images.
    pub fn convert_to_svg(&self) -> Result<String> {
        let mut builder = SvgBuilder::new(
            self.parser.width() as f64,
            self.parser.height() as f64,
        );

        // Set viewBox based on bounds
        let (x1, y1, x2, y2) = self.parser.header.bounds;
        builder = builder.with_viewbox(
            x1 as f64,
            y1 as f64,
            (x2 - x1) as f64,
            (y2 - y1) as f64,
        );

        // Process records in parallel to extract elements
        // Group records by type for efficient parallel processing
        let elements: Vec<SvgElement> = self
            .parser
            .records
            .par_iter()
            .filter_map(|record| self.process_record(record).ok().flatten())
            .collect();

        // Add all elements to builder
        for element in elements {
            builder.add_element(element);
        }

        Ok(builder.build())
    }

    /// Convert EMF to SVG bytes
    pub fn convert_to_svg_bytes(&self) -> Result<Vec<u8>> {
        Ok(self.convert_to_svg()?.into_bytes())
    }

    /// Process a single EMF record and convert to SVG element
    fn process_record(&self, record: &EmfRecord) -> Result<Option<SvgElement>> {
        match record.record_type {
            // Rectangle
            0x0000002B => self.parse_rectangle(record),
            // Ellipse
            0x0000002A => self.parse_ellipse(record),
            // Polygon
            0x00000003 => self.parse_polygon(record),
            // Polyline
            0x00000004 => self.parse_polyline(record),
            // PolyBezier
            0x00000002 => self.parse_polybezier(record),
            // LineTo
            0x00000036 => self.parse_lineto(record),
            // StretchDIBits (embedded bitmap)
            0x00000051 => self.parse_stretchdibits(record),
            // Arc
            0x0000002D => self.parse_arc(record),
            // Pie
            0x0000002F => self.parse_pie(record),
            // Chord
            0x0000002E => self.parse_chord(record),
            _ => Ok(None), // Unsupported or non-drawing record
        }
    }

    /// Parse EMR_RECTANGLE record
    fn parse_rectangle(&self, record: &EmfRecord) -> Result<Option<SvgElement>> {
        let rect = EmfRect::read_from_bytes(&record.data)
            .map_err(|_| Error::ParseError("Invalid RECT data in EMR_RECTANGLE".into()))?;

        Ok(Some(SvgElement::Rect(SvgRect {
            x: rect.left as f64,
            y: rect.top as f64,
            width: (rect.right - rect.left) as f64,
            height: (rect.bottom - rect.top) as f64,
            fill: None,
            stroke: Some("#000000".to_string()),
            stroke_width: 1.0,
        })))
    }

    /// Parse EMR_ELLIPSE record
    fn parse_ellipse(&self, record: &EmfRecord) -> Result<Option<SvgElement>> {
        let rect = EmfRect::read_from_bytes(&record.data)
            .map_err(|_| Error::ParseError("Invalid RECT data in EMR_ELLIPSE".into()))?;

        let left = rect.left as f64;
        let top = rect.top as f64;
        let right = rect.right as f64;
        let bottom = rect.bottom as f64;

        let cx = (left + right) / 2.0;
        let cy = (top + bottom) / 2.0;
        let rx = (right - left) / 2.0;
        let ry = (bottom - top) / 2.0;

        Ok(Some(SvgElement::Ellipse(SvgEllipse {
            cx,
            cy,
            rx,
            ry,
            fill: None,
            stroke: Some("#000000".to_string()),
            stroke_width: 1.0,
        })))
    }

    /// Parse EMR_POLYGON record
    fn parse_polygon(&self, record: &EmfRecord) -> Result<Option<SvgElement>> {
        if record.data.len() < 20 {
            return Ok(None);
        }

        let header = EmfPolygonHeader::read_from_bytes(&record.data)
            .map_err(|_| Error::ParseError("Invalid header data in EMR_POLYGON".into()))?;
        let count = header.count as usize;

        if record.data.len() < 20 + count * 8 {
            return Ok(None);
        }

        let mut commands = Vec::with_capacity(count + 1);

        // Parse points using zerocopy
        let points_data = &record.data[20..20 + count * 8];
        for i in 0..count {
            let point_data = &points_data[i * 8..(i + 1) * 8];
            let point = EmfPoint::read_from_bytes(point_data)
                .map_err(|_| Error::ParseError("Invalid point data in EMR_POLYGON".into()))?;

            let x = point.x as f64;
            let y = point.y as f64;

            if i == 0 {
                commands.push(PathCommand::MoveTo { x, y });
            } else {
                commands.push(PathCommand::LineTo { x, y });
            }
        }

        commands.push(PathCommand::ClosePath);

        Ok(Some(SvgElement::Path(
            SvgPath::new(commands)
                .with_stroke("#000000".to_string())
                .with_fill("none".to_string()),
        )))
    }

    /// Parse EMR_POLYLINE record
    fn parse_polyline(&self, record: &EmfRecord) -> Result<Option<SvgElement>> {
        if record.data.len() < 20 {
            return Ok(None);
        }

        let header = EmfPolygonHeader::read_from_bytes(&record.data)
            .map_err(|_| Error::ParseError("Invalid header data in EMR_POLYLINE".into()))?;
        let count = header.count as usize;

        if record.data.len() < 20 + count * 8 {
            return Ok(None);
        }

        let mut commands = Vec::with_capacity(count);

        // Parse points using zerocopy
        let points_data = &record.data[20..20 + count * 8];
        for i in 0..count {
            let point_data = &points_data[i * 8..(i + 1) * 8];
            let point = EmfPoint::read_from_bytes(point_data)
                .map_err(|_| Error::ParseError("Invalid point data in EMR_POLYLINE".into()))?;

            let x = point.x as f64;
            let y = point.y as f64;

            if i == 0 {
                commands.push(PathCommand::MoveTo { x, y });
            } else {
                commands.push(PathCommand::LineTo { x, y });
            }
        }

        Ok(Some(SvgElement::Path(
            SvgPath::new(commands)
                .with_stroke("#000000".to_string())
                .with_fill("none".to_string()),
        )))
    }

    /// Parse EMR_POLYBEZIER record
    fn parse_polybezier(&self, record: &EmfRecord) -> Result<Option<SvgElement>> {
        if record.data.len() < 20 {
            return Ok(None);
        }

        let header = EmfPolygonHeader::read_from_bytes(&record.data)
            .map_err(|_| Error::ParseError("Invalid header data in EMR_POLYBEZIER".into()))?;
        let count = header.count as usize;

        if record.data.len() < 20 + count * 8 || count < 4 {
            return Ok(None);
        }

        let mut commands = Vec::new();

        // Parse points using zerocopy
        let points_data = &record.data[20..20 + count * 8];
        let mut points = Vec::with_capacity(count);

        for i in 0..count {
            let point_data = &points_data[i * 8..(i + 1) * 8];
            let point = EmfPoint::read_from_bytes(point_data)
                .map_err(|_| Error::ParseError("Invalid point data in EMR_POLYBEZIER".into()))?;
            points.push(point);
        }

        // First point is MoveTo
        commands.push(PathCommand::MoveTo {
            x: points[0].x as f64,
            y: points[0].y as f64,
        });

        // Process Bezier curves (groups of 3 points)
        let mut i = 1;
        while i + 2 < count {
            commands.push(PathCommand::CubicBezier {
                x1: points[i].x as f64,
                y1: points[i].y as f64,
                x2: points[i + 1].x as f64,
                y2: points[i + 1].y as f64,
                x: points[i + 2].x as f64,
                y: points[i + 2].y as f64,
            });
            i += 3;
        }

        Ok(Some(SvgElement::Path(
            SvgPath::new(commands)
                .with_stroke("#000000".to_string())
                .with_fill("none".to_string()),
        )))
    }

    /// Parse EMR_LINETO record
    fn parse_lineto(&self, record: &EmfRecord) -> Result<Option<SvgElement>> {
        if record.data.len() < 8 {
            return Ok(None);
        }

        let point = EmfPoint::read_from_bytes(&record.data)
            .map_err(|_| Error::ParseError("Invalid point data in EMR_LINETO".into()))?;

        let x = point.x as f64;
        let y = point.y as f64;

        // LineTo requires current position, which we'd need to track
        // For simplicity, create a line from origin
        Ok(Some(SvgElement::Path(
            SvgPath::new(vec![
                PathCommand::MoveTo { x: 0.0, y: 0.0 },
                PathCommand::LineTo { x, y },
            ])
            .with_stroke("#000000".to_string()),
        )))
    }

    /// Parse EMR_STRETCHDIBITS record (embedded bitmap)
    fn parse_stretchdibits(&self, record: &EmfRecord) -> Result<Option<SvgElement>> {
        // This is complex - extract DIB and convert to PNG, then embed
        if record.data.len() < std::mem::size_of::<EmfStretchDibits>() {
            return Ok(None);
        }

        let stretch = EmfStretchDibits::read_from_bytes(&record.data)
            .map_err(|_| Error::ParseError("Invalid EMR_STRETCHDIBITS data".into()))?;

        // Try to extract and convert DIB data
        // This is simplified - full implementation would parse DIB structure
        if let Ok(png_data) = self.extract_and_convert_dib(&record.data[std::mem::size_of::<EmfStretchDibits>()..]) {
            return Ok(Some(SvgElement::Image(SvgImage::from_png_data(
                stretch.x_dest as f64,
                stretch.y_dest as f64,
                stretch.cx_dest as f64,
                stretch.cy_dest as f64,
                &png_data,
            ))));
        }

        Ok(None)
    }

    /// Parse EMR_ARC record
    fn parse_arc(&self, _record: &EmfRecord) -> Result<Option<SvgElement>> {
        // Arc parsing is complex and requires trigonometry
        // Placeholder for future implementation
        Ok(None)
    }

    /// Parse EMR_PIE record
    fn parse_pie(&self, _record: &EmfRecord) -> Result<Option<SvgElement>> {
        // Pie chart slice - complex path construction
        Ok(None)
    }

    /// Parse EMR_CHORD record
    fn parse_chord(&self, _record: &EmfRecord) -> Result<Option<SvgElement>> {
        // Chord - similar to arc with connecting line
        Ok(None)
    }

    /// Extract and convert DIB data to PNG
    fn extract_and_convert_dib(&self, dib_data: &[u8]) -> Result<Vec<u8>> {
        if dib_data.len() < 40 {
            return Err(Error::ParseError("DIB data too small".into()));
        }

        // Construct BMP from DIB
        let file_size = 14u32 + dib_data.len() as u32;
        let pixel_data_offset = 14u32 + 40u32;

        let mut bmp_data = Vec::with_capacity(file_size as usize);
        bmp_data.extend_from_slice(b"BM");
        bmp_data.extend_from_slice(&file_size.to_le_bytes());
        bmp_data.extend_from_slice(&[0u8; 4]);
        bmp_data.extend_from_slice(&pixel_data_offset.to_le_bytes());
        bmp_data.extend_from_slice(dib_data);

        // Load and re-encode as PNG
        let img = image::load_from_memory(&bmp_data)
            .map_err(|e| Error::ParseError(format!("Failed to load DIB: {}", e)))?;

        let mut png_data = Vec::new();
        let mut cursor = std::io::Cursor::new(&mut png_data);
        img.write_to(&mut cursor, image::ImageFormat::Png)
            .map_err(|e| Error::ParseError(format!("Failed to encode PNG: {}", e)))?;

        Ok(png_data)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_emf_svg_converter_creation() {
        // Test requires valid EMF data
        // Placeholder for future tests
    }
}