-
Notifications
You must be signed in to change notification settings - Fork 16
/
parse.go
64 lines (52 loc) · 1.17 KB
/
parse.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
package svg
import (
"encoding/xml"
"errors"
"strconv"
"strings"
)
type Point struct {
X float32
Y float32
}
type PolyLine struct {
ID string `xml:"id,attr"`
Stroke string `xml:"stroke,attr"`
StrokeWidth int `xml:"stroke-width,attr"`
PointsRaw string `xml:"points,attr"`
Points []Point
}
type SVG struct {
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
Style string `xml:"style,attr"`
ViewBox string `xml:"viewBox,attr"`
PolyLines []PolyLine `xml:"polyline"`
}
func Parse(data []byte) (*SVG, error) {
v := &SVG{}
err := xml.Unmarshal(data, &v)
if err != nil {
return nil, err
}
for i, polyLine := range v.PolyLines {
points := make([]Point, 0)
for _, s := range strings.Split(polyLine.PointsRaw, " ") {
ps := strings.Split(s, ",")
if len(ps) < 2 {
return nil, errors.New("svgの形式が不正です")
}
x, err := strconv.ParseFloat(ps[0], 32)
if err != nil {
return nil, err
}
y, err := strconv.ParseFloat(ps[1], 32)
if err != nil {
return nil, err
}
points = append(points, Point{float32(x), float32(y)})
}
v.PolyLines[i].Points = points
}
return v, nil
}