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
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
// SVG generation module
//
// Provides high-performance SVG generation from vector graphics operations

use std::fmt::Write;

/// SVG path commands
#[derive(Debug, Clone)]
pub enum PathCommand {
    /// Move to absolute position
    MoveTo { x: f64, y: f64 },
    /// Line to absolute position
    LineTo { x: f64, y: f64 },
    /// Cubic Bezier curve
    CubicBezier { x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64 },
    /// Quadratic Bezier curve
    QuadraticBezier { x1: f64, y1: f64, x: f64, y: f64 },
    /// Arc
    Arc { rx: f64, ry: f64, x_axis_rotation: f64, large_arc: bool, sweep: bool, x: f64, y: f64 },
    /// Close path
    ClosePath,
}

impl PathCommand {
    /// Convert to SVG path string
    pub fn to_svg(&self) -> String {
        match self {
            Self::MoveTo { x, y } => format!("M {} {}", x, y),
            Self::LineTo { x, y } => format!("L {} {}", x, y),
            Self::CubicBezier { x1, y1, x2, y2, x, y } => {
                format!("C {} {} {} {} {} {}", x1, y1, x2, y2, x, y)
            }
            Self::QuadraticBezier { x1, y1, x, y } => {
                format!("Q {} {} {} {}", x1, y1, x, y)
            }
            Self::Arc { rx, ry, x_axis_rotation, large_arc, sweep, x, y } => {
                format!(
                    "A {} {} {} {} {} {} {}",
                    rx,
                    ry,
                    x_axis_rotation,
                    if *large_arc { 1 } else { 0 },
                    if *sweep { 1 } else { 0 },
                    x,
                    y
                )
            }
            Self::ClosePath => "Z".to_string(),
        }
    }
}

/// SVG path element
#[derive(Debug, Clone)]
pub struct SvgPath {
    /// Path commands
    pub commands: Vec<PathCommand>,
    /// Stroke color (RGB hex)
    pub stroke: Option<String>,
    /// Stroke width
    pub stroke_width: f64,
    /// Fill color (RGB hex)
    pub fill: Option<String>,
    /// Fill opacity
    pub fill_opacity: f64,
    /// Stroke opacity
    pub stroke_opacity: f64,
}

impl Default for SvgPath {
    fn default() -> Self {
        Self {
            commands: Vec::new(),
            stroke: Some("#000000".to_string()),
            stroke_width: 1.0,
            fill: None,
            fill_opacity: 1.0,
            stroke_opacity: 1.0,
        }
    }
}

impl SvgPath {
    /// Create new path with commands
    pub fn new(commands: Vec<PathCommand>) -> Self {
        Self {
            commands,
            ..Default::default()
        }
    }

    /// Set stroke color
    pub fn with_stroke(mut self, color: String) -> Self {
        self.stroke = Some(color);
        self
    }

    /// Set stroke width
    pub fn with_stroke_width(mut self, width: f64) -> Self {
        self.stroke_width = width;
        self
    }

    /// Set fill color
    pub fn with_fill(mut self, color: String) -> Self {
        self.fill = Some(color);
        self
    }

    /// Set fill opacity
    pub fn with_fill_opacity(mut self, opacity: f64) -> Self {
        self.fill_opacity = opacity;
        self
    }

    /// Set stroke opacity
    pub fn with_stroke_opacity(mut self, opacity: f64) -> Self {
        self.stroke_opacity = opacity;
        self
    }

    /// Generate SVG path string
    pub fn to_svg(&self) -> String {
        let mut path_data = String::new();
        for cmd in &self.commands {
            if !path_data.is_empty() {
                path_data.push(' ');
            }
            path_data.push_str(&cmd.to_svg());
        }

        let mut attrs = format!(r#"d="{}""#, path_data);

        if let Some(ref stroke) = self.stroke {
            write!(attrs, r#" stroke="{}""#, stroke).unwrap();
        } else {
            attrs.push_str(r#" stroke="none""#);
        }

        write!(attrs, r#" stroke-width="{}""#, self.stroke_width).unwrap();

        if let Some(ref fill) = self.fill {
            write!(attrs, r#" fill="{}""#, fill).unwrap();
        } else {
            attrs.push_str(r#" fill="none""#);
        }

        if self.fill_opacity < 1.0 {
            write!(attrs, r#" fill-opacity="{}""#, self.fill_opacity).unwrap();
        }

        if self.stroke_opacity < 1.0 {
            write!(attrs, r#" stroke-opacity="{}""#, self.stroke_opacity).unwrap();
        }

        format!(r#"<path {} />"#, attrs)
    }
}

/// SVG rectangle element
#[derive(Debug, Clone)]
pub struct SvgRect {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
    pub fill: Option<String>,
    pub stroke: Option<String>,
    pub stroke_width: f64,
}

impl SvgRect {
    /// Generate SVG rect string
    pub fn to_svg(&self) -> String {
        let mut attrs = format!(
            r#"x="{}" y="{}" width="{}" height="{}""#,
            self.x, self.y, self.width, self.height
        );

        if let Some(ref fill) = self.fill {
            write!(attrs, r#" fill="{}""#, fill).unwrap();
        } else {
            attrs.push_str(r#" fill="none""#);
        }

        if let Some(ref stroke) = self.stroke {
            write!(attrs, r#" stroke="{}""#, stroke).unwrap();
            write!(attrs, r#" stroke-width="{}""#, self.stroke_width).unwrap();
        }

        format!(r#"<rect {} />"#, attrs)
    }
}

/// SVG ellipse element
#[derive(Debug, Clone)]
pub struct SvgEllipse {
    pub cx: f64,
    pub cy: f64,
    pub rx: f64,
    pub ry: f64,
    pub fill: Option<String>,
    pub stroke: Option<String>,
    pub stroke_width: f64,
}

impl SvgEllipse {
    /// Generate SVG ellipse string
    pub fn to_svg(&self) -> String {
        let mut attrs = format!(
            r#"cx="{}" cy="{}" rx="{}" ry="{}""#,
            self.cx, self.cy, self.rx, self.ry
        );

        if let Some(ref fill) = self.fill {
            write!(attrs, r#" fill="{}""#, fill).unwrap();
        } else {
            attrs.push_str(r#" fill="none""#);
        }

        if let Some(ref stroke) = self.stroke {
            write!(attrs, r#" stroke="{}""#, stroke).unwrap();
            write!(attrs, r#" stroke-width="{}""#, self.stroke_width).unwrap();
        }

        format!(r#"<ellipse {} />"#, attrs)
    }
}

/// SVG text element
#[derive(Debug, Clone)]
pub struct SvgText {
    pub x: f64,
    pub y: f64,
    pub text: String,
    pub font_size: f64,
    pub font_family: Option<String>,
    pub fill: Option<String>,
}

impl SvgText {
    /// Generate SVG text string
    pub fn to_svg(&self) -> String {
        let mut attrs = format!(r#"x="{}" y="{}" font-size="{}""#, self.x, self.y, self.font_size);

        if let Some(ref family) = self.font_family {
            write!(attrs, r#" font-family="{}""#, family).unwrap();
        }

        if let Some(ref fill) = self.fill {
            write!(attrs, r#" fill="{}""#, fill).unwrap();
        }

        format!(r#"<text {}>{}</text>"#, attrs, self.text)
    }
}

/// SVG image element (for embedded raster images)
#[derive(Debug, Clone)]
pub struct SvgImage {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
    /// Base64-encoded image data with data URI scheme
    pub href: String,
}

impl SvgImage {
    /// Create from PNG data
    pub fn from_png_data(x: f64, y: f64, width: f64, height: f64, png_data: &[u8]) -> Self {
        use base64::Engine;
        let base64_engine = base64::engine::general_purpose::STANDARD;
        let encoded = base64_engine.encode(png_data);
        let href = format!("data:image/png;base64,{}", encoded);
        
        Self {
            x,
            y,
            width,
            height,
            href,
        }
    }

    /// Create from JPEG data
    pub fn from_jpeg_data(x: f64, y: f64, width: f64, height: f64, jpeg_data: &[u8]) -> Self {
        use base64::Engine;
        let base64_engine = base64::engine::general_purpose::STANDARD;
        let encoded = base64_engine.encode(jpeg_data);
        let href = format!("data:image/jpeg;base64,{}", encoded);
        
        Self {
            x,
            y,
            width,
            height,
            href,
        }
    }

    /// Generate SVG image string
    pub fn to_svg(&self) -> String {
        format!(
            r#"<image x="{}" y="{}" width="{}" height="{}" href="{}" />"#,
            self.x, self.y, self.width, self.height, self.href
        )
    }
}

/// SVG element types
#[derive(Debug, Clone)]
pub enum SvgElement {
    Path(SvgPath),
    Rect(SvgRect),
    Ellipse(SvgEllipse),
    Text(SvgText),
    Image(SvgImage),
}

impl SvgElement {
    /// Convert to SVG string
    pub fn to_svg(&self) -> String {
        match self {
            Self::Path(p) => p.to_svg(),
            Self::Rect(r) => r.to_svg(),
            Self::Ellipse(e) => e.to_svg(),
            Self::Text(t) => t.to_svg(),
            Self::Image(i) => i.to_svg(),
        }
    }
}

/// SVG document builder
#[derive(Debug, Clone)]
pub struct SvgBuilder {
    /// Document width
    pub width: f64,
    /// Document height
    pub height: f64,
    /// ViewBox (x, y, width, height)
    pub viewbox: Option<(f64, f64, f64, f64)>,
    /// SVG elements
    pub elements: Vec<SvgElement>,
}

impl SvgBuilder {
    /// Create new SVG builder
    pub fn new(width: f64, height: f64) -> Self {
        Self {
            width,
            height,
            viewbox: None,
            elements: Vec::new(),
        }
    }

    /// Set viewBox
    pub fn with_viewbox(mut self, x: f64, y: f64, width: f64, height: f64) -> Self {
        self.viewbox = Some((x, y, width, height));
        self
    }

    /// Add an element
    pub fn add_element(&mut self, element: SvgElement) {
        self.elements.push(element);
    }

    /// Add a path
    pub fn add_path(&mut self, path: SvgPath) {
        self.elements.push(SvgElement::Path(path));
    }

    /// Add a rectangle
    pub fn add_rect(&mut self, rect: SvgRect) {
        self.elements.push(SvgElement::Rect(rect));
    }

    /// Add an ellipse
    pub fn add_ellipse(&mut self, ellipse: SvgEllipse) {
        self.elements.push(SvgElement::Ellipse(ellipse));
    }

    /// Add text
    pub fn add_text(&mut self, text: SvgText) {
        self.elements.push(SvgElement::Text(text));
    }

    /// Add an embedded image
    pub fn add_image(&mut self, image: SvgImage) {
        self.elements.push(SvgElement::Image(image));
    }

    /// Generate complete SVG document
    pub fn build(&self) -> String {
        let mut svg = String::new();
        
        // XML declaration
        svg.push_str(r#"<?xml version="1.0" encoding="UTF-8"?>"#);
        svg.push('\n');

        // SVG opening tag
        svg.push_str(r#"<svg xmlns="http://www.w3.org/2000/svg" "#);
        write!(svg, r#"width="{}" height="{}""#, self.width, self.height).unwrap();

        if let Some((x, y, w, h)) = self.viewbox {
            write!(svg, r#" viewBox="{} {} {} {}""#, x, y, w, h).unwrap();
        }

        svg.push_str(">\n");

        // Add elements
        for element in &self.elements {
            svg.push_str("  ");
            svg.push_str(&element.to_svg());
            svg.push('\n');
        }

        // SVG closing tag
        svg.push_str("</svg>");

        svg
    }

    /// Build and return as bytes
    pub fn build_bytes(&self) -> Vec<u8> {
        self.build().into_bytes()
    }
}

/// Color conversion utilities
pub mod color {
    /// Convert RGB color to hex string
    pub fn rgb_to_hex(r: u8, g: u8, b: u8) -> String {
        format!("#{:02X}{:02X}{:02X}", r, g, b)
    }

    /// Convert COLORREF (Windows color format) to hex string
    pub fn colorref_to_hex(colorref: u32) -> String {
        let r = (colorref & 0xFF) as u8;
        let g = ((colorref >> 8) & 0xFF) as u8;
        let b = ((colorref >> 16) & 0xFF) as u8;
        rgb_to_hex(r, g, b)
    }
}

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

    #[test]
    fn test_svg_path() {
        let path = SvgPath::new(vec![
            PathCommand::MoveTo { x: 0.0, y: 0.0 },
            PathCommand::LineTo { x: 100.0, y: 100.0 },
        ])
        .with_stroke("#000000".to_string());

        let svg = path.to_svg();
        assert!(svg.contains("M 0 0 L 100 100"));
    }

    #[test]
    fn test_svg_builder() {
        let mut builder = SvgBuilder::new(100.0, 100.0);
        builder.add_rect(SvgRect {
            x: 10.0,
            y: 10.0,
            width: 80.0,
            height: 80.0,
            fill: Some("#FF0000".to_string()),
            stroke: None,
            stroke_width: 0.0,
        });

        let svg = builder.build();
        assert!(svg.contains("<svg"));
        assert!(svg.contains("</svg>"));
        assert!(svg.contains("<rect"));
    }

    #[test]
    fn test_color_conversion() {
        assert_eq!(color::rgb_to_hex(255, 0, 0), "#FF0000");
        assert_eq!(color::colorref_to_hex(0x0000FF), "#FF0000");
    }
}