/
parsepath.go
146 lines (123 loc) · 2.93 KB
/
parsepath.go
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
package surrender
import (
"errors"
"image"
"image/color"
"log"
"strconv"
"strings"
"unicode"
)
// ParsePath can parse TinySVG 1.2 path attributes
func ParsePath(d string) (SvgPath, error) {
var commands []PathCommand
var currentCmd PathCommand
// Function to process current command before starting a new one
processCurrentCmd := func() {
if currentCmd.Type != "" {
commands = append(commands, currentCmd)
}
currentCmd = PathCommand{}
}
var currentCoords []string
for _, c := range d {
if unicode.IsLetter(c) {
processCurrentCmd()
currentCmd.Type = string(c)
continue
}
switch c {
case ' ', ',':
if len(currentCoords) > 0 {
coords := strings.Join(currentCoords, "")
x, y, err := ParseCoordinates(coords)
if err != nil {
log.Printf("Parsing error: %v", err)
return SvgPath{}, err
}
currentCmd.Points = append(currentCmd.Points, image.Point{X: x, Y: y})
currentCoords = nil
}
case '-':
// Handle "-" as a negative sign
if len(currentCoords) > 0 {
currentCoords = append(currentCoords, string(c))
} else {
currentCoords = []string{string(c)}
}
default:
currentCoords = append(currentCoords, string(c))
}
}
if len(currentCoords) > 0 {
coords := strings.Join(currentCoords, "")
x, y, err := ParseCoordinates(coords)
if err != nil {
log.Printf("Parsing error: %v", err)
return SvgPath{}, err
}
currentCmd.Points = append(currentCmd.Points, image.Point{X: x, Y: y})
}
// Make sure to process the last command
processCurrentCmd()
if cmd := currentCmd; cmd.Type != "" {
commands = append(commands, cmd)
}
return SvgPath{Commands: commands, Fill: color.RGBA{R: 0, G: 0, B: 0, A: 255}}, nil
}
// ParseCoordinates tries to parse a TinySVG 1.2 path attribute coordinate
func ParseCoordinates(coords string) (int, int, error) {
parts := splitCoordinates(coords)
if len(parts) < 1 || len(parts) > 2 {
return 0, 0, errors.New("invalid coordinate format")
}
x, err := parseCoordinate(parts[0])
if err != nil {
return 0, 0, err
}
y := 0
if len(parts) > 1 {
y, err = parseCoordinate(parts[1])
if err != nil {
return 0, 0, err
}
}
return x, y, nil
}
// splitCoordinates splits the coordinate string based on separators (" " and "-")
func splitCoordinates(coords string) []string {
var parts []string
var current string
for _, c := range coords {
if unicode.IsSpace(c) || c == '-' {
if current != "" {
parts = append(parts, current)
current = ""
}
}
current += string(c)
}
if current != "" {
parts = append(parts, current)
}
return parts
}
// parseCoordinate parses a single coordinate value
func parseCoordinate(coord string) (int, error) {
if coord == "" {
return 0, errors.New("empty coordinate")
}
negative := false
if coord[0] == '-' {
negative = true
coord = coord[1:]
}
value, err := strconv.Atoi(coord)
if err != nil {
return 0, err
}
if negative {
value = -value
}
return value, nil
}