img2svg 0.1.9

A rust native image to SVG converter in CLI/MCP/Library
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
#[cfg(test)]
mod tests {
    use super::super::*;
    use crate::vectorizer::{Point, Curve, VectorizedData};
    use std::fs;
    use std::path::PathBuf;

    fn create_test_vectorized_data() -> VectorizedData {
        VectorizedData {
            width: 100,
            height: 100,
            background_color: (255, 255, 255, 255),
            curves: vec![
                Curve {
                    points: vec![
                        Point { x: 10.0, y: 10.0 },
                        Point { x: 90.0, y: 10.0 },
                        Point { x: 90.0, y: 90.0 },
                        Point { x: 10.0, y: 90.0 },
                    ],
                    color: (255, 0, 0, 255),
                    is_closed: true,
                    subpaths: vec![],
                },
                Curve {
                    points: vec![
                        Point { x: 30.0, y: 30.0 },
                        Point { x: 70.0, y: 30.0 },
                        Point { x: 70.0, y: 70.0 },
                        Point { x: 30.0, y: 70.0 },
                    ],
                    color: (0, 0, 255, 255),
                    is_closed: true,
                    subpaths: vec![],
                },
            ],
        }
    }

    fn create_test_vectorized_data_with_subpaths() -> VectorizedData {
        VectorizedData {
            width: 100,
            height: 100,
            background_color: (255, 255, 255, 255),
            curves: vec![
                Curve {
                    points: vec![],
                    color: (255, 0, 0, 255),
                    is_closed: true,
                    subpaths: vec![
                        vec![
                            Point { x: 10.0, y: 10.0 },
                            Point { x: 40.0, y: 10.0 },
                            Point { x: 40.0, y: 40.0 },
                            Point { x: 10.0, y: 40.0 },
                        ],
                        vec![
                            Point { x: 60.0, y: 60.0 },
                            Point { x: 90.0, y: 60.0 },
                            Point { x: 90.0, y: 90.0 },
                            Point { x: 60.0, y: 90.0 },
                        ],
                    ],
                },
            ],
        }
    }

    #[test]
    fn test_generate_svg_basic() {
        let data = create_test_vectorized_data();
        let output_path = PathBuf::from("/tmp/test_output.svg");

        let result = generate_svg(&data, &output_path);
        assert!(result.is_ok());

        // Check that file was created
        assert!(output_path.exists());

        // Read and verify content
        let content = fs::read_to_string(&output_path).unwrap();
        assert!(content.contains("<svg"));
        assert!(content.contains("width=\"100\""));
        assert!(content.contains("height=\"100\""));
        assert!(content.contains("</svg>"));

        // Clean up
        let _ = fs::remove_file(&output_path);
    }

    #[test]
    fn test_generate_svg_with_background_color() {
        let data = create_test_vectorized_data();
        let output_path = PathBuf::from("/tmp/test_background.svg");

        let result = generate_svg(&data, &output_path);
        assert!(result.is_ok());

        let content = fs::read_to_string(&output_path).unwrap();
        // Check for background rect with white color
        assert!(content.contains("#ffffff"));

        let _ = fs::remove_file(&output_path);
    }

    #[test]
    fn test_generate_svg_with_curves() {
        let data = create_test_vectorized_data();
        let output_path = PathBuf::from("/tmp/test_curves.svg");

        let result = generate_svg(&data, &output_path);
        assert!(result.is_ok());

        let content = fs::read_to_string(&output_path).unwrap();
        // Check for curve colors
        assert!(content.contains("#ff0000")); // Red
        assert!(content.contains("#0000ff")); // Blue
        // Check for path elements
        assert!(content.contains("<path"));

        let _ = fs::remove_file(&output_path);
    }

    #[test]
    fn test_generate_svg_advanced() {
        let data = create_test_vectorized_data();
        let output_path = PathBuf::from("/tmp/test_advanced.svg");

        let result = generate_svg_advanced(&data, &output_path);
        assert!(result.is_ok());
        assert!(output_path.exists());

        let _ = fs::remove_file(&output_path);
    }

    #[test]
    fn test_generate_svg_with_subpaths() {
        let data = create_test_vectorized_data_with_subpaths();
        let output_path = PathBuf::from("/tmp/test_subpaths.svg");

        let result = generate_svg(&data, &output_path);
        assert!(result.is_ok());

        let content = fs::read_to_string(&output_path).unwrap();
        // Should have path elements
        assert!(content.contains("<path"));

        let _ = fs::remove_file(&output_path);
    }

    #[test]
    fn test_generate_svg_empty_curves() {
        let data = VectorizedData {
            width: 50,
            height: 50,
            background_color: (128, 128, 128, 255),
            curves: vec![],
        };
        let output_path = PathBuf::from("/tmp/test_empty.svg");

        let result = generate_svg(&data, &output_path);
        assert!(result.is_ok());

        let content = fs::read_to_string(&output_path).unwrap();
        assert!(content.contains("<svg"));
        assert!(content.contains("#808080")); // Gray background

        let _ = fs::remove_file(&output_path);
    }

    // === fmt_coord Tests ===

    #[test]
    fn test_fmt_coord_integer() {
        assert_eq!(fmt_coord(5.0), "5");
        assert_eq!(fmt_coord(10.0), "10");
        assert_eq!(fmt_coord(0.0), "0");
    }

    #[test]
    fn test_fmt_coord_half() {
        assert_eq!(fmt_coord(5.5), "5.5");
        assert_eq!(fmt_coord(10.5), "10.5");
    }

    #[test]
    fn test_fmt_coord_rounding() {
        // Should round to nearest 0.5, then format as integer if close to whole
        assert_eq!(fmt_coord(5.24), "5");    // Rounds to 5.0, formats as "5"
        assert_eq!(fmt_coord(5.26), "5.5");  // Rounds to 5.5
        assert_eq!(fmt_coord(5.74), "5.5");  // Rounds to 5.5
        assert_eq!(fmt_coord(5.76), "6");    // Rounds to 6.0, formats as "6"
    }

    #[test]
    fn test_fmt_coord_negative() {
        assert_eq!(fmt_coord(-5.0), "-5");
        assert_eq!(fmt_coord(-5.5), "-5.5");
    }

    // === create_subpath_string Tests ===

    #[test]
    fn test_create_subpath_string_empty() {
        let points: Vec<Point> = vec![];
        let result = create_subpath_string(&points, true);
        assert_eq!(result, "");
    }

    #[test]
    fn test_create_subpath_string_single_point() {
        let points = vec![Point { x: 10.0, y: 20.0 }];
        let result = create_subpath_string(&points, false);
        assert_eq!(result, "M10 20");
    }

    #[test]
    fn test_create_subpath_string_two_points() {
        let points = vec![
            Point { x: 10.0, y: 20.0 },
            Point { x: 30.0, y: 40.0 },
        ];
        let result = create_subpath_string(&points, false);
        assert_eq!(result, "M10 20L30 40");
    }

    #[test]
    fn test_create_subpath_string_closed() {
        let points = vec![
            Point { x: 10.0, y: 10.0 },
            Point { x: 20.0, y: 10.0 },
            Point { x: 20.0, y: 20.0 },
        ];
        let result = create_subpath_string(&points, true);
        assert!(result.contains("M10 10"));
        assert!(result.contains("L20 10"));
        assert!(result.contains("L20 20"));
        assert!(result.ends_with('Z'));
    }

    #[test]
    fn test_create_subpath_string_not_closed() {
        let points = vec![
            Point { x: 10.0, y: 10.0 },
            Point { x: 20.0, y: 10.0 },
            Point { x: 20.0, y: 20.0 },
        ];
        let result = create_subpath_string(&points, false);
        assert!(result.contains("M10 10"));
        assert!(result.contains("L20 10"));
        assert!(result.contains("L20 20"));
        assert!(!result.ends_with('Z'));
    }

    #[test]
    fn test_create_subpath_string_with_decimals() {
        let points = vec![
            Point { x: 10.5, y: 20.5 },
            Point { x: 30.25, y: 40.75 },
        ];
        let result = create_subpath_string(&points, false);
        assert_eq!(result, "M10.5 20.5L30.5 41"); // Note the rounding
    }

    #[test]
    fn test_generate_svg_with_alpha() {
        let data = VectorizedData {
            width: 50,
            height: 50,
            background_color: (255, 255, 255, 128),
            curves: vec![
                Curve {
                    points: vec![
                        Point { x: 10.0, y: 10.0 },
                        Point { x: 40.0, y: 10.0 },
                        Point { x: 40.0, y: 40.0 },
                        Point { x: 10.0, y: 40.0 },
                    ],
                    color: (255, 0, 0, 64),
                    is_closed: true,
                    subpaths: vec![],
                },
            ],
        };
        let output_path = PathBuf::from("/tmp/test_alpha.svg");
        let result = generate_svg(&data, &output_path);
        assert!(result.is_ok());

        let content = fs::read_to_string(&output_path).unwrap();
        assert!(content.contains("fill-opacity=\"0.5\""));

        let _ = fs::remove_file(&output_path);
    }

    #[test]
    fn test_generate_svg_hierarchical_evenodd() {
        let data = VectorizedData {
            width: 100,
            height: 100,
            background_color: (255, 255, 255, 255),
            curves: vec![
                Curve {
                    points: vec![],
                    color: (255, 0, 0, 255),
                    is_closed: true,
                    subpaths: vec![
                        vec![
                            Point { x: 10.0, y: 10.0 },
                            Point { x: 90.0, y: 10.0 },
                            Point { x: 90.0, y: 90.0 },
                            Point { x: 10.0, y: 90.0 },
                        ],
                        vec![
                            Point { x: 30.0, y: 30.0 },
                            Point { x: 30.0, y: 70.0 },
                            Point { x: 70.0, y: 70.0 },
                            Point { x: 70.0, y: 30.0 },
                        ],
                    ],
                },
            ],
        };
        let output_path = PathBuf::from("/tmp/test_evenodd.svg");
        let result = generate_svg(&data, &output_path);
        assert!(result.is_ok());

        let content = fs::read_to_string(&output_path).unwrap();
        assert!(content.contains("fill-rule=\"evenodd\""));

        let _ = fs::remove_file(&output_path);
    }

    // === create_multi_path_string Tests ===

    #[test]
    fn test_create_multi_path_string_empty() {
        let subpaths: Vec<Vec<Point>> = vec![];
        let result = create_multi_path_string(&subpaths);
        assert_eq!(result, "");
    }

    #[test]
    fn test_create_multi_path_string_single_subpath() {
        let subpaths = vec![
            vec![
                Point { x: 0.0, y: 0.0 },
                Point { x: 10.0, y: 0.0 },
                Point { x: 10.0, y: 10.0 },
            ],
        ];
        let result = create_multi_path_string(&subpaths);
        assert!(!result.is_empty());
        assert!(result.contains("M0 0"));
    }

    #[test]
    fn test_create_multi_path_string_multiple_subpaths() {
        let subpaths = vec![
            vec![
                Point { x: 0.0, y: 0.0 },
                Point { x: 10.0, y: 0.0 },
                Point { x: 10.0, y: 10.0 },
            ],
            vec![
                Point { x: 20.0, y: 20.0 },
                Point { x: 30.0, y: 20.0 },
                Point { x: 30.0, y: 30.0 },
            ],
        ];
        let result = create_multi_path_string(&subpaths);
        // Should have space-separated subpaths
        assert!(result.contains("M0 0"));
        assert!(result.contains("M20 20"));
    }

    #[test]
    fn test_create_multi_path_string_skips_small_subpaths() {
        let subpaths = vec![
            vec![
                Point { x: 0.0, y: 0.0 },
                Point { x: 10.0, y: 0.0 },
            ], // Only 2 points, should be skipped
            vec![
                Point { x: 20.0, y: 20.0 },
                Point { x: 30.0, y: 20.0 },
                Point { x: 30.0, y: 30.0 },
            ], // 3 points, should be included
        ];
        let result = create_multi_path_string(&subpaths);
        // First subpath should be skipped (less than 3 points)
        assert!(!result.contains("M0 0"));
        // Second subpath should be included
        assert!(result.contains("M20 20"));
    }

    #[test]
    fn test_create_subpath_string_removes_zero_length_segments() {
        // Points that format to the same coordinate should produce no L segment
        let pts = vec![
            Point { x: 0.0, y: 0.0 },
            Point { x: 0.1, y: 0.1 }, // fmt_coord snaps to 0.0, 0.0
            Point { x: 0.0, y: 0.0 }, // duplicate
            Point { x: 10.0, y: 10.0 },
        ];
        let result = create_subpath_string(&pts, true);
        // Should only have M and one L, not three L commands
        assert_eq!(result, "M0 0L10 10Z");
    }

    #[test]
    fn test_generate_svg_to_matches_file_output() {
        let data = VectorizedData {
            width: 100,
            height: 100,
            background_color: (255, 255, 255, 255),
            curves: vec![
                Curve {
                    points: vec![],
                    color: (255, 0, 0, 255),
                    is_closed: true,
                    subpaths: vec![
                        vec![
                            Point { x: 10.0, y: 10.0 },
                            Point { x: 90.0, y: 10.0 },
                            Point { x: 90.0, y: 90.0 },
                            Point { x: 10.0, y: 90.0 },
                        ],
                    ],
                },
            ],
        };
        let file_path = PathBuf::from("/tmp/test_svg_to.svg");
        generate_svg(&data, &file_path).unwrap();
        let from_file = std::fs::read_to_string(&file_path).unwrap();

        let mut buffer = Vec::new();
        generate_svg_to(&data, &mut buffer).unwrap();
        let from_writer = String::from_utf8(buffer).unwrap();

        assert_eq!(from_file, from_writer);
        let _ = std::fs::remove_file(&file_path);
    }

    #[test]
    fn test_write_subpath_to_matches_string_version() {
        let pts = vec![
            Point { x: 0.0, y: 0.0 },
            Point { x: 5.0, y: 0.0 },
            Point { x: 5.0, y: 5.0 },
        ];
        let string_version = create_subpath_string(&pts, true);

        let mut buf = Vec::new();
        write_subpath_to(&mut buf, &pts, true).unwrap();
        let writer_version = String::from_utf8(buf).unwrap();

        assert_eq!(string_version, writer_version);
    }

    #[test]
    fn test_write_multi_path_to_matches_string_version() {
        let subpaths = vec![
            vec![
                Point { x: 0.0, y: 0.0 },
                Point { x: 10.0, y: 0.0 },
                Point { x: 10.0, y: 10.0 },
            ],
            vec![
                Point { x: 20.0, y: 20.0 },
                Point { x: 30.0, y: 20.0 },
                Point { x: 30.0, y: 30.0 },
            ],
        ];
        let string_version = create_multi_path_string(&subpaths);

        let mut buf = Vec::new();
        write_multi_path_to(&mut buf, &subpaths).unwrap();
        let writer_version = String::from_utf8(buf).unwrap();

        assert_eq!(string_version, writer_version);
    }
}