galago 1.4.2

Galago svg editor
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
//! Path
//! Good reading <https://razrfalcon.github.io/notes-on-svg-parsing/path-data.html>

use std::fmt;
use std::fmt::Write;
use svgtypes::{PathParser, PathSegment};

/// Represents a single SVG path segment.
#[derive(Debug, Clone)]
pub struct SvgItem {
    /// Represents a single SVG path segment.
    pub inner: PathSegment,
}

fn round_to(value: f64, decimals: u64) -> f64 {
    let decimals = i32::try_from(decimals).unwrap_or(1);
    let factor = 10f64.powi(decimals);
    (value * factor).round() / factor
}

impl SvgItem {
    /// Returns the letter
    #[must_use]
    pub fn get_letter(&self) -> char {
        self.inner.command() as char
    }

    /// Returns the value of the segment as a string.
    /// # Errors
    /// Returns an error if the segment cannot be parsed.
    pub fn set_value(&mut self, to_add: SvgItem) {
        *self = to_add;
    }
}

impl fmt::Display for SvgItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let fmt = match self.inner {
            PathSegment::MoveTo { abs: _, x, y } | PathSegment::LineTo { abs: _, x, y } => {
                // move to
                format!("{}{} {}", self.get_letter(), x, y)
            }
            PathSegment::HorizontalLineTo { abs: _, x } => {
                format!("{}{}", self.get_letter(), x)
            }
            PathSegment::VerticalLineTo { abs: _, y } => {
                format!("{}{}", self.get_letter(), y)
            }
            PathSegment::CurveTo {
                abs: _,
                x1,
                y1,
                x2,
                y2,
                x,
                y,
            } => format!(
                "{}{} {} {} {} {} {}",
                self.get_letter(),
                x1,
                y1,
                x2,
                y2,
                x,
                y
            ),
            PathSegment::SmoothCurveTo {
                abs: _,
                x2,
                y2,
                x,
                y,
            } => {
                format!("{}{} {} {} {}", self.get_letter(), x2, y2, x, y)
            }
            PathSegment::Quadratic {
                abs: _,
                x1,
                y1,
                x,
                y,
            } => {
                format!("{}{} {} {} {}", self.get_letter(), x1, y1, x, y)
            }
            PathSegment::SmoothQuadratic { abs: _, x, y } => {
                // smooth quadratic
                format!("{}{} {}", self.get_letter(), x, y)
            }
            PathSegment::EllipticalArc {
                abs: _,
                rx,
                ry,
                x_axis_rotation,
                large_arc,
                sweep,
                x,
                y,
            } => format!(
                "{}{} {} {} {} {} {} {}",
                self.get_letter(),
                rx,
                ry,
                x_axis_rotation,
                if large_arc { "1" } else { "0" },
                if sweep { "1" } else { "0" },
                x,
                y
            ),
            PathSegment::ClosePath { abs } => {
                if abs {
                    "Z".to_string()
                } else {
                    "z".to_string()
                }
            }
        };
        write!(f, "{fmt}")?;
        Ok(())
    }
}

/// Represents an SVG path, which is a collection of SVG path items.
#[derive(Debug, Clone)]
pub struct SvgPath {
    /// A vector of SVG path items.
    pub items: Vec<SvgItem>,
}

impl SvgPath {
    /// Parses a string into an SVG path.
    /// # Errors
    /// Returns an error if the string cannot be parsed into a valid SVG path.
    pub fn parse(path_str: &str) -> Result<Self, String> {
        let s = PathParser::from(path_str);
        let mut items = Vec::new();

        for segment in s {
            let one_segment = segment
                .map_err(|e| format!("Failed to parse SVG path segment: {e} in {path_str}"))?;
            let to_add = SvgItem { inner: one_segment };
            items.push(to_add);
        }

        Ok(SvgPath { items })
    }

    /// Converts the path to absolute coordinates.
    #[allow(clippy::too_many_lines)]
    pub fn absolute(&mut self) {
        let mut pos = (0.0, 0.0);
        let mut subpath_start = (0.0, 0.0);

        for item in &mut self.items {
            match &mut item.inner {
                PathSegment::MoveTo { abs, x, y } => {
                    if !*abs {
                        *x += pos.0;
                        *y += pos.1;
                        *abs = true;
                    }
                    pos.0 = *x;
                    pos.1 = *y;
                    subpath_start = pos;
                }
                PathSegment::LineTo { abs, x, y } => {
                    if !*abs {
                        *x += pos.0;
                        *y += pos.1;
                        *abs = true;
                    }
                    pos.0 = *x;
                    pos.1 = *y;
                }
                PathSegment::HorizontalLineTo { abs, x } => {
                    if !*abs {
                        *x += pos.0;
                        *abs = true;
                    }
                    pos.0 = *x;
                }
                PathSegment::VerticalLineTo { abs, y } => {
                    if !*abs {
                        *y += pos.1;
                        *abs = true;
                    }
                    pos.1 = *y;
                }
                PathSegment::CurveTo {
                    abs,
                    x1,
                    y1,
                    x2,
                    y2,
                    x,
                    y,
                } => {
                    if !*abs {
                        *x1 += pos.0;
                        *y1 += pos.1;
                        *x2 += pos.0;
                        *y2 += pos.1;
                        *x += pos.0;
                        *y += pos.1;
                        *abs = true;
                    }
                    pos.0 = *x;
                    pos.1 = *y;
                }
                PathSegment::SmoothCurveTo { abs, x2, y2, x, y } => {
                    if !*abs {
                        *x2 += pos.0;
                        *y2 += pos.1;
                        *x += pos.0;
                        *y += pos.1;
                        *abs = true;
                    }
                    pos.0 = *x;
                    pos.1 = *y;
                }
                PathSegment::Quadratic { abs, x1, y1, x, y } => {
                    if !*abs {
                        *x1 += pos.0;
                        *y1 += pos.1;
                        *x += pos.0;
                        *y += pos.1;
                        *abs = true;
                    }
                    pos.0 = *x;
                    pos.1 = *y;
                }
                PathSegment::SmoothQuadratic { abs, x, y } => {
                    // quadratic
                    if !*abs {
                        *x += pos.0;
                        *y += pos.1;
                        *abs = true;
                    }
                    pos.0 = *x;
                    pos.1 = *y;
                }
                PathSegment::EllipticalArc {
                    abs,
                    rx: _,
                    ry: _,
                    x_axis_rotation: _,
                    large_arc: _,
                    sweep: _,
                    x,
                    y,
                } => {
                    // Ellipse
                    if !*abs {
                        *x += pos.0;
                        *y += pos.1;
                        *abs = true;
                    }
                    pos.0 = *x;
                    pos.1 = *y;
                }
                PathSegment::ClosePath { abs } => {
                    *abs = true;
                    pos = subpath_start;
                }
            }
        }
    }

    /// Converts the path to relative coordinates.
    #[allow(clippy::too_many_lines)]
    pub fn relative(&mut self) {
        let mut pos = (0.0, 0.0);
        let mut subpath_start = (0.0, 0.0);

        for item in &mut self.items {
            match &mut item.inner {
                PathSegment::MoveTo { abs, x, y } => {
                    if *abs {
                        let rel = (*x - pos.0, *y - pos.1);
                        *x = rel.0;
                        *y = rel.1;
                        *abs = false;
                    }
                    pos.0 += *x;
                    pos.1 += *y;
                    subpath_start = pos;
                }
                PathSegment::LineTo { abs, x, y } => {
                    if *abs {
                        let rel = (*x - pos.0, *y - pos.1);
                        *x = rel.0;
                        *y = rel.1;
                        *abs = false;
                    }
                    pos.0 += *x;
                    pos.1 += *y;
                }
                PathSegment::HorizontalLineTo { abs, x } => {
                    if *abs {
                        let rel = *x - pos.0;
                        *x = rel;
                        *abs = false;
                    }
                    pos.0 += *x;
                }
                PathSegment::VerticalLineTo { abs, y } => {
                    if *abs {
                        let rel = *y - pos.1;
                        *y = rel;
                        *abs = false;
                    }
                    pos.1 += *y;
                }
                PathSegment::CurveTo {
                    abs,
                    x1,
                    y1,
                    x2,
                    y2,
                    x,
                    y,
                } => {
                    if *abs {
                        *x1 -= pos.0;
                        *y1 -= pos.1;
                        *x2 -= pos.0;
                        *y2 -= pos.1;
                        *x -= pos.0;
                        *y -= pos.1;
                        *abs = false;
                    }
                    pos.0 += *x;
                    pos.1 += *y;
                }
                PathSegment::SmoothCurveTo { abs, x2, y2, x, y } => {
                    if *abs {
                        *x2 -= pos.0;
                        *y2 -= pos.1;
                        *x -= pos.0;
                        *y -= pos.1;
                        *abs = false;
                    }
                    pos.0 += *x;
                    pos.1 += *y;
                }
                PathSegment::Quadratic { abs, x1, y1, x, y } => {
                    if *abs {
                        *x1 -= pos.0;
                        *y1 -= pos.1;
                        *x -= pos.0;
                        *y -= pos.1;
                        *abs = false;
                    }
                    pos.0 += *x;
                    pos.1 += *y;
                }
                PathSegment::SmoothQuadratic { abs, x, y } => {
                    // smooth quadratic
                    if *abs {
                        *x -= pos.0;
                        *y -= pos.1;
                        *abs = false;
                    }
                    pos.0 += *x;
                    pos.1 += *y;
                }
                PathSegment::EllipticalArc {
                    abs,
                    rx: _,
                    ry: _,
                    x_axis_rotation: _,
                    large_arc: _,
                    sweep: _,
                    x,
                    y,
                } => {
                    if *abs {
                        *x -= pos.0;
                        *y -= pos.1;
                        *abs = false;
                    }
                    pos.0 += *x;
                    pos.1 += *y;
                }
                PathSegment::ClosePath { abs } => {
                    *abs = false;
                    pos = subpath_start;
                }
            }
        }
    }

    /// Translates the path by (dx, dy).
    pub fn translate(&mut self, dx: f64, dy: f64) {
        for item in &mut self.items {
            match &mut item.inner {
                PathSegment::MoveTo { x, y, .. }
                | PathSegment::LineTo { x, y, .. }
                | PathSegment::SmoothQuadratic { x, y, .. }
                | PathSegment::EllipticalArc { x, y, .. } => {
                    *x += dx;
                    *y += dy;
                }
                PathSegment::HorizontalLineTo { x, .. } => {
                    *x += dx;
                }
                PathSegment::VerticalLineTo { y, .. } => {
                    *y += dy;
                }
                PathSegment::CurveTo {
                    x1,
                    y1,
                    x2,
                    y2,
                    x,
                    y,
                    ..
                } => {
                    *x1 += dx;
                    *y1 += dy;
                    *x2 += dx;
                    *y2 += dy;
                    *x += dx;
                    *y += dy;
                }
                PathSegment::SmoothCurveTo { x2, y2, x, y, .. } => {
                    *x2 += dx;
                    *y2 += dy;
                    *x += dx;
                    *y += dy;
                }
                PathSegment::Quadratic { x1, y1, x, y, .. } => {
                    *x1 += dx;
                    *y1 += dy;
                    *x += dx;
                    *y += dy;
                }

                PathSegment::ClosePath { .. } => {} // No coordinates to modify
            }
        }
    }

    /// Scales the path by (sx, sy) relative to the origin (0, 0).
    pub fn scale(&mut self, sx: f64, sy: f64) {
        for item in &mut self.items {
            match &mut item.inner {
                PathSegment::MoveTo { x, y, .. }
                | PathSegment::LineTo { x, y, .. }
                | PathSegment::SmoothQuadratic { x, y, .. } => {
                    *x *= sx;
                    *y *= sy;
                }
                PathSegment::HorizontalLineTo { x, .. } => {
                    *x *= sx;
                }
                PathSegment::VerticalLineTo { y, .. } => {
                    *y *= sy;
                }
                PathSegment::CurveTo {
                    x1,
                    y1,
                    x2,
                    y2,
                    x,
                    y,
                    ..
                } => {
                    *x1 *= sx;
                    *y1 *= sy;
                    *x2 *= sx;
                    *y2 *= sy;
                    *x *= sx;
                    *y *= sy;
                }
                PathSegment::SmoothCurveTo { x2, y2, x, y, .. } => {
                    *x2 *= sx;
                    *y2 *= sy;
                    *x *= sx;
                    *y *= sy;
                }
                PathSegment::Quadratic { x1, y1, x, y, .. } => {
                    *x1 *= sx;
                    *y1 *= sy;
                    *x *= sx;
                    *y *= sy;
                }
                PathSegment::EllipticalArc { rx, ry, x, y, .. } => {
                    *rx *= sx.abs();
                    *ry *= sy.abs();
                    *x *= sx;
                    *y *= sy;
                }
                PathSegment::ClosePath { .. } => {}
            }
        }
    }

    /// Rotates the path around (cx, cy) by `angle_deg` degrees.
    pub fn rotate(&mut self, angle_deg: f64, cx: f64, cy: f64) {
        let angle_rad = angle_deg.to_radians();
        let (sin, cos) = angle_rad.sin_cos();

        let rotate_point = |x: &mut f64, y: &mut f64| {
            let dx = *x - cx;
            let dy = *y - cy;
            let x_new = dx * cos - dy * sin + cx;
            let y_new = dx * sin + dy * cos + cy;
            *x = x_new;
            *y = y_new;
        };

        for item in &mut self.items {
            match &mut item.inner {
                PathSegment::MoveTo { x, y, .. }
                | PathSegment::LineTo { x, y, .. }
                | PathSegment::SmoothQuadratic { x, y, .. } => {
                    rotate_point(x, y);
                }
                PathSegment::HorizontalLineTo { .. } | PathSegment::VerticalLineTo { .. } => {
                    // You could convert these to LineTo or skip them
                }
                PathSegment::CurveTo {
                    x1,
                    y1,
                    x2,
                    y2,
                    x,
                    y,
                    ..
                } => {
                    rotate_point(x1, y1);
                    rotate_point(x2, y2);
                    rotate_point(x, y);
                }
                PathSegment::SmoothCurveTo { x2, y2, x, y, .. } => {
                    rotate_point(x2, y2);
                    rotate_point(x, y);
                }
                PathSegment::Quadratic { x1, y1, x, y, .. } => {
                    rotate_point(x1, y1);
                    rotate_point(x, y);
                }
                PathSegment::EllipticalArc {
                    x,
                    y,
                    x_axis_rotation,
                    ..
                } => {
                    rotate_point(x, y);
                    *x_axis_rotation += angle_deg;
                }
                #[allow(clippy::match_same_arms)]
                PathSegment::ClosePath { .. } => {
                    // close path
                }
            }
        }
    }

    /// Rounds all coordinates and values in the path to the given number of decimal places.
    pub fn round(&mut self, decimal: u64) {
        for item in &mut self.items {
            match &mut item.inner {
                PathSegment::MoveTo { x, y, .. }
                | PathSegment::LineTo { x, y, .. }
                | PathSegment::SmoothQuadratic { x, y, .. } => {
                    *x = round_to(*x, decimal);
                    *y = round_to(*y, decimal);
                }
                PathSegment::HorizontalLineTo { x, .. } => {
                    *x = round_to(*x, decimal);
                }
                PathSegment::VerticalLineTo { y, .. } => {
                    *y = round_to(*y, decimal);
                }
                PathSegment::CurveTo {
                    x1,
                    y1,
                    x2,
                    y2,
                    x,
                    y,
                    ..
                } => {
                    *x1 = round_to(*x1, decimal);
                    *y1 = round_to(*y1, decimal);
                    *x2 = round_to(*x2, decimal);
                    *y2 = round_to(*y2, decimal);
                    *x = round_to(*x, decimal);
                    *y = round_to(*y, decimal);
                }
                PathSegment::SmoothCurveTo { x2, y2, x, y, .. } => {
                    *x2 = round_to(*x2, decimal);
                    *y2 = round_to(*y2, decimal);
                    *x = round_to(*x, decimal);
                    *y = round_to(*y, decimal);
                }
                PathSegment::Quadratic { x1, y1, x, y, .. } => {
                    *x1 = round_to(*x1, decimal);
                    *y1 = round_to(*y1, decimal);
                    *x = round_to(*x, decimal);
                    *y = round_to(*y, decimal);
                }
                PathSegment::EllipticalArc {
                    rx,
                    ry,
                    x_axis_rotation,
                    x,
                    y,
                    ..
                } => {
                    *rx = round_to(*rx, decimal);
                    *ry = round_to(*ry, decimal);
                    *x_axis_rotation = round_to(*x_axis_rotation, decimal);
                    *x = round_to(*x, decimal);
                    *y = round_to(*y, decimal);
                }
                PathSegment::ClosePath { .. } => {}
            }
        }
    }

    /// Toggles the coordinate type (absolute/relative) of the path segment at the given index.
    #[allow(clippy::too_many_lines)]
    pub fn toggle_coord_type_at(&mut self, index: usize) {
        let mut pos = (0.0, 0.0);
        let mut subpath_start = (0.0, 0.0);

        // We need to recalculate the position up to item i
        for (j, item) in self.items.iter_mut().enumerate() {
            if j == index {
                match &mut item.inner {
                    PathSegment::MoveTo { abs, x, y } => {
                        if *abs {
                            *x -= pos.0;
                            *y -= pos.1;
                        } else {
                            *x += pos.0;
                            *y += pos.1;
                        }
                        *abs = !*abs;
                        pos.0 = if *abs { *x } else { pos.0 + *x };
                        pos.1 = if *abs { *y } else { pos.1 + *y };
                        // subpath_start = pos;
                    }
                    PathSegment::LineTo { abs, x, y } => {
                        // line to
                        if *abs {
                            *x -= pos.0;
                            *y -= pos.1;
                        } else {
                            *x += pos.0;
                            *y += pos.1;
                        }
                        *abs = !*abs;
                        pos.0 = if *abs { *x } else { pos.0 + *x };
                        pos.1 = if *abs { *y } else { pos.1 + *y };
                    }
                    PathSegment::HorizontalLineTo { abs, x } => {
                        if *abs {
                            *x -= pos.0;
                        } else {
                            *x += pos.0;
                        }
                        *abs = !*abs;
                        pos.0 = if *abs { *x } else { pos.0 + *x };
                    }
                    PathSegment::VerticalLineTo { abs, y } => {
                        if *abs {
                            *y -= pos.1;
                        } else {
                            *y += pos.1;
                        }
                        *abs = !*abs;
                        pos.1 = if *abs { *y } else { pos.1 + *y };
                    }
                    PathSegment::CurveTo {
                        abs,
                        x1,
                        y1,
                        x2,
                        y2,
                        x,
                        y,
                    } => {
                        if *abs {
                            *x1 -= pos.0;
                            *y1 -= pos.1;
                            *x2 -= pos.0;
                            *y2 -= pos.1;
                            *x -= pos.0;
                            *y -= pos.1;
                        } else {
                            *x1 += pos.0;
                            *y1 += pos.1;
                            *x2 += pos.0;
                            *y2 += pos.1;
                            *x += pos.0;
                            *y += pos.1;
                        }
                        *abs = !*abs;
                        pos.0 = if *abs { *x } else { pos.0 + *x };
                        pos.1 = if *abs { *y } else { pos.1 + *y };
                    }
                    PathSegment::SmoothCurveTo { abs, x2, y2, x, y } => {
                        if *abs {
                            *x2 -= pos.0;
                            *y2 -= pos.1;
                            *x -= pos.0;
                            *y -= pos.1;
                        } else {
                            *x2 += pos.0;
                            *y2 += pos.1;
                            *x += pos.0;
                            *y += pos.1;
                        }
                        *abs = !*abs;
                        pos.0 = if *abs { *x } else { pos.0 + *x };
                        pos.1 = if *abs { *y } else { pos.1 + *y };
                    }
                    PathSegment::Quadratic { abs, x1, y1, x, y } => {
                        if *abs {
                            *x1 -= pos.0;
                            *y1 -= pos.1;
                            *x -= pos.0;
                            *y -= pos.1;
                        } else {
                            *x1 += pos.0;
                            *y1 += pos.1;
                            *x += pos.0;
                            *y += pos.1;
                        }
                        *abs = !*abs;
                        pos.0 = if *abs { *x } else { pos.0 + *x };
                        pos.1 = if *abs { *y } else { pos.1 + *y };
                    }
                    PathSegment::SmoothQuadratic { abs, x, y } => {
                        // smooth quadratic
                        if *abs {
                            *x -= pos.0;
                            *y -= pos.1;
                        } else {
                            *x += pos.0;
                            *y += pos.1;
                        }
                        *abs = !*abs;
                        pos.0 = if *abs { *x } else { pos.0 + *x };
                        pos.1 = if *abs { *y } else { pos.1 + *y };
                    }
                    PathSegment::EllipticalArc { abs, x, y, .. } => {
                        if *abs {
                            *x -= pos.0;
                            *y -= pos.1;
                        } else {
                            *x += pos.0;
                            *y += pos.1;
                        }
                        *abs = !*abs;
                        pos.0 = if *abs { *x } else { pos.0 + *x };
                        pos.1 = if *abs { *y } else { pos.1 + *y };
                    }
                    PathSegment::ClosePath { abs } => {
                        *abs = !*abs;
                        // pos = subpath_start;
                    }
                }
                break;
            }
            // Update the position so we know the current point at index i
            match &item.inner {
                PathSegment::MoveTo { abs, x, y } => {
                    if *abs {
                        pos = (*x, *y);
                    } else {
                        pos.0 += *x;
                        pos.1 += *y;
                    }
                    subpath_start = pos;
                }
                PathSegment::LineTo { abs, x, y }
                | PathSegment::SmoothQuadratic { abs, x, y }
                | PathSegment::Quadratic {
                    abs,
                    x1: _,
                    y1: _,
                    x,
                    y,
                }
                | PathSegment::SmoothCurveTo {
                    abs,
                    x2: _,
                    y2: _,
                    x,
                    y,
                }
                | PathSegment::CurveTo {
                    abs,
                    x1: _,
                    y1: _,
                    x2: _,
                    y2: _,
                    x,
                    y,
                }
                | PathSegment::EllipticalArc { abs, x, y, .. } => {
                    if *abs {
                        pos = (*x, *y);
                    } else {
                        pos.0 += *x;
                        pos.1 += *y;
                    }
                }
                PathSegment::HorizontalLineTo { abs, x } => {
                    pos.0 = if *abs { *x } else { pos.0 + *x };
                }
                PathSegment::VerticalLineTo { abs, y } => {
                    pos.1 = if *abs { *y } else { pos.1 + *y };
                }
                PathSegment::ClosePath { .. } => {
                    pos = subpath_start;
                }
            }
        }
    }

    /// Try replace element
    /// # Errors
    /// Error if it's invalid
    pub fn try_replace_element_at(&mut self, idx: usize, val: &str) -> Result<String, String> {
        if idx < self.items.len() {
            let mut result = String::new();
            for (index, one_item) in self.items.iter().enumerate() {
                let segment_str = if index == idx {
                    val
                } else {
                    &one_item.to_string()
                };
                result.push_str(segment_str);
            }
            match SvgPath::parse(&result) {
                Ok(path) => {
                    return Ok(path.to_string());
                }

                Err(e) => {
                    return Err(e);
                }
            }
        }
        Err("Invalid index".to_string())
    }
}

impl fmt::Display for SvgPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::new();
        for item in &self.items {
            let segment_str = item.to_string();
            if !segment_str.is_empty() {
                result.push_str(&segment_str);
            }
        }
        write!(f, "{result}")
    }
}

/// Convert a polyline string to an SVG path data string
/// # Errors
/// Fails if fails to write to `path_data`
pub fn polyline_to_path(points: &str) -> Result<String, std::fmt::Error> {
    let mut path_data = String::new();
    let mut first = true;
    let all_points = points.split_whitespace().collect::<Vec<&str>>();
    for idx in (0..all_points.len()).step_by(2) {
        let coords1 = all_points[idx];
        let coords2 = all_points[idx + 1];
        if first {
            write!(path_data, "M {coords1} {coords2}")?;
            first = false;
        } else {
            write!(path_data, " L {coords1} {coords2}")?;
        }
    }
    Ok(path_data)
}

/// Convert a line string to an SVG path data string
#[must_use]
pub fn line_to_path(x1: &str, y1: &str, x2: &str, y2: &str) -> String {
    format!("M {x1} {y1} L {x2} {y2}")
}

/// Convert a polygon string to an SVG path data string
/// # Errors
/// Fails if fails to write to `path_data`
pub fn polygon_to_path(points: &str) -> Result<String, std::fmt::Error> {
    let mut path_data = String::new();
    let mut first = true;
    let all_points = points.split_whitespace().collect::<Vec<&str>>();
    for idx in (0..all_points.len()).step_by(2) {
        let coords1 = all_points[idx];
        let coords2 = all_points[idx + 1];
        if first {
            write!(path_data, "M {coords1} {coords2}")?;
            first = false;
        } else {
            write!(path_data, " L {coords1} {coords2}")?;
        }
    }
    path_data.push('Z'); // Close the polygon
    Ok(path_data)
}

/// Convert a svg rect to a path data string
#[must_use]
pub fn rect_to_path(x: &str, y: &str, width: &str, height: &str) -> String {
    format!("M {x} {y} h {width} v {height} h -{width} Z")
}

/// Convert a circle to a path data string
/// # Errors
/// Return error if fail to parse
pub fn circle_to_path(cx: &str, cy: &str, r: &str) -> Result<String, String> {
    let r = r
        .parse::<f32>()
        .map_err(|e| format!("Error formatting r : {e}"))?;
    let double_r = r * 2.0;
    let formatted =
        format!("M {cx} {cy} m {r}, 0a {r},{r} 0 1,0 -{double_r},0 a {r},{r} 0 1,0 {double_r},0");
    Ok(formatted)
}

/// Convert an ellipse to a path data string
/// # Errors
/// Return error if fail to parse
pub fn ellipse_to_path(cx: &str, cy: &str, rx: &str, ry: &str) -> Result<String, String> {
    let rx = rx
        .parse::<f32>()
        .map_err(|e| format!("Error formatting rx : {e}"))?;
    let cx = cx
        .parse::<f32>()
        .map_err(|e| format!("Error formatting cx : {e}"))?;
    let double_rx = rx * 2.0;
    let start_x = cx - rx;
    let formatted =
        format!("M{start_x} {cy} a{rx} {ry} 0 1,0 {double_rx} 0 a{rx} {ry} 0 1,0 -{double_rx} 0 Z");
    Ok(formatted)
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;

    #[test]
    fn test_path_parsed() {
        let mut parsed = SvgPath::parse("M174 270C113 271 84 237 80 207 78 189 79 172 76 172 46 154 34 142 21 109-4 49 45 62 55 66 71 72 79 83 90 94 103 104 160 104 174 104ZM110 150C104 153 98 160 95 166S92 184 95 190C103 208 126 216 142 205 149 200 153 195 155 188 161 172 150 153 133 147 128 145 115 147 110 150ZM174 270C236 271 265 237 269 207 271 189 270 172 273 172 303 154 315 142 328 109 353 49 304 62 294 66 278 72 270 83 259 94 246 104 189 104 174 104ZM239 150C245 153 251 160 254 166S257 184 254 190C246 208 223 216 207 205 200 200 196 195 194 188 188 172 199 153 216 147 221 145 234 147 239 150Z").unwrap();
        assert_eq!(
            parsed.to_string(),
            "M174 270C113 271 84 237 80 207C78 189 79 172 76 172C46 154 34 142 21 109C-4 49 45 62 55 66C71 72 79 83 90 94C103 104 160 104 174 104ZM110 150C104 153 98 160 95 166S92 184 95 190C103 208 126 216 142 205C149 200 153 195 155 188C161 172 150 153 133 147C128 145 115 147 110 150ZM174 270C236 271 265 237 269 207C271 189 270 172 273 172C303 154 315 142 328 109C353 49 304 62 294 66C278 72 270 83 259 94C246 104 189 104 174 104ZM239 150C245 153 251 160 254 166S257 184 254 190C246 208 223 216 207 205C200 200 196 195 194 188C188 172 199 153 216 147C221 145 234 147 239 150Z"
        );
        parsed.absolute();
        assert_eq!(
            parsed.to_string(),
            "M174 270C113 271 84 237 80 207C78 189 79 172 76 172C46 154 34 142 21 109C-4 49 45 62 55 66C71 72 79 83 90 94C103 104 160 104 174 104ZM110 150C104 153 98 160 95 166S92 184 95 190C103 208 126 216 142 205C149 200 153 195 155 188C161 172 150 153 133 147C128 145 115 147 110 150ZM174 270C236 271 265 237 269 207C271 189 270 172 273 172C303 154 315 142 328 109C353 49 304 62 294 66C278 72 270 83 259 94C246 104 189 104 174 104ZM239 150C245 153 251 160 254 166S257 184 254 190C246 208 223 216 207 205C200 200 196 195 194 188C188 172 199 153 216 147C221 145 234 147 239 150Z"
        );

        parsed.relative();
        assert_eq!(
            parsed.to_string(),
            "m174 270c-61 1 -90 -33 -94 -63c-2 -18 -1 -35 -4 -35c-30 -18 -42 -30 -55 -63c-25 -60 24 -47 34 -43c16 6 24 17 35 28c13 10 70 10 84 10zm-64 -120c-6 3 -12 10 -15 16s-3 18 0 24c8 18 31 26 47 15c7 -5 11 -10 13 -17c6 -16 -5 -35 -22 -41c-5 -2 -18 0 -23 3zm64 120c62 1 91 -33 95 -63c2 -18 1 -35 4 -35c30 -18 42 -30 55 -63c25 -60 -24 -47 -34 -43c-16 6 -24 17 -35 28c-13 10 -70 10 -85 10zm65 -120c6 3 12 10 15 16s3 18 0 24c-8 18 -31 26 -47 15c-7 -5 -11 -10 -13 -17c-6 -16 5 -35 22 -41c5 -2 18 0 23 3z"
        );

        let mut parsed_relative = SvgPath::parse("m174 270c-61 1 -90 -33 -94 -63c-2 -18 -1 -35 -4 -35c-30 -18 -42 -30 -55 -63c-25 -60 24 -47 34 -43c16 6 24 17 35 28c13 10 70 10 84 10zm-64 -120c-6 3 -12 10 -15 16s-3 18 0 24c8 18 31 26 47 15c7 -5 11 -10 13 -17c6 -16 -5 -35 -22 -41c-5 -2 -18 0 -23 3zm64 120c62 1 91 -33 95 -63c2 -18 1 -35 4 -35c30 -18 42 -30 55 -63c25 -60 -24 -47 -34 -43c-16 6 -24 17 -35 28c-13 10 -70 10 -85 10zm65 -120c6 3 12 10 15 16s3 18 0 24c-8 18 -31 26 -47 15c-7 -5 -11 -10 -13 -17c-6 -16 5 -35 22 -41c5 -2 18 0 23 3z").unwrap();

        assert_eq!(
            parsed_relative.to_string(),
            "m174 270c-61 1 -90 -33 -94 -63c-2 -18 -1 -35 -4 -35c-30 -18 -42 -30 -55 -63c-25 -60 24 -47 34 -43c16 6 24 17 35 28c13 10 70 10 84 10zm-64 -120c-6 3 -12 10 -15 16s-3 18 0 24c8 18 31 26 47 15c7 -5 11 -10 13 -17c6 -16 -5 -35 -22 -41c-5 -2 -18 0 -23 3zm64 120c62 1 91 -33 95 -63c2 -18 1 -35 4 -35c30 -18 42 -30 55 -63c25 -60 -24 -47 -34 -43c-16 6 -24 17 -35 28c-13 10 -70 10 -85 10zm65 -120c6 3 12 10 15 16s3 18 0 24c-8 18 -31 26 -47 15c-7 -5 -11 -10 -13 -17c-6 -16 5 -35 22 -41c5 -2 18 0 23 3z"
        );
        parsed_relative.absolute();
        assert_eq!(
            parsed_relative.to_string(),
            "M174 270C113 271 84 237 80 207C78 189 79 172 76 172C46 154 34 142 21 109C-4 49 45 62 55 66C71 72 79 83 90 94C103 104 160 104 174 104ZM110 150C104 153 98 160 95 166S92 184 95 190C103 208 126 216 142 205C149 200 153 195 155 188C161 172 150 153 133 147C128 145 115 147 110 150ZM174 270C236 271 265 237 269 207C271 189 270 172 273 172C303 154 315 142 328 109C353 49 304 62 294 66C278 72 270 83 259 94C246 104 189 104 174 104ZM239 150C245 153 251 160 254 166S257 184 254 190C246 208 223 216 207 205C200 200 196 195 194 188C188 172 199 153 216 147C221 145 234 147 239 150Z"
        );
    }

    #[test]
    fn test_toggle_coord_type_at() {
        let mut parsed = SvgPath::parse("M174 270C113 271 84 237 80 207 78 189 79 172 76 172 46 154 34 142 21 109-4 49 45 62 55 66 71 72 79 83 90 94 103 104 160 104 174 104Z").unwrap();
        let len = parsed.items.len();
        assert_eq!(len, 8);
        for i in 0..len {
            parsed.toggle_coord_type_at(i);
        }
        assert_eq!(
            parsed.to_string(),
            "m174 270c-61 1 -90 -33 -94 -63c-2 -18 -1 -35 -4 -35c-30 -18 -42 -30 -55 -63c-25 -60 24 -47 34 -43c16 6 24 17 35 28c13 10 70 10 84 10z"
        );
    }

    #[test]
    fn test_polyline_to_path() {
        // <polyline points="0,100 50,25 50,75 100,0" />
        let path_data = polyline_to_path("0,100 50,25 50,75 100,0").unwrap();
        assert_eq!(path_data, "M 0,100 50,25 L 50,75 100,0");
    }

    #[test]
    fn test_line_to_path() {
        // <line x1="0" y1="80" x2="100" y2="20" />
        let path_data = line_to_path("0", "0", "80", "20");
        assert_eq!(path_data, "M 0 0 L 80 20");
    }

    #[test]
    fn test_polygon_to_path() {
        // <polygon points="0,100 50,25 50,75 100,0" />
        let path_data = polygon_to_path("0,100 50,25 50,75 100,0").unwrap();
        assert_eq!(path_data, "M 0,100 50,25 L 50,75 100,0Z");
    }

    #[test]
    fn test_circle_to_path() {
        // <circle cx="50" cy="50" r="50" />
        let path_data = circle_to_path("50", "50", "50").unwrap();
        assert_eq!(
            path_data,
            "M 50 50 m 50, 0a 50,50 0 1,0 -100,0 a 50,50 0 1,0 100,0"
        );
    }

    #[test]
    fn test_ellipse_to_path() {
        // <ellipse cx="100" cy="50" rx="100" ry="50" />
        let path_data = ellipse_to_path("100", "50", "100", "50").unwrap();
        assert_eq!(
            path_data,
            "M0 50 a100 50 0 1,0 200 0 a100 50 0 1,0 -200 0 Z"
        );
    }
}