-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
185 lines (180 loc) · 3.99 KB
/
unmarshal.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
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
package svg
import (
"encoding/xml"
"fmt"
"io"
"strconv"
"strings"
"github.com/cdillond/gdf"
)
func unmarshalXML(n *node, r io.Reader, defs map[string]*node) {
if n == nil {
return
}
dec := xml.NewDecoder(r)
tok, err := dec.Token()
for {
// loop until the outermost svg element is found; fragments are not accepted.
if v, ok := tok.(xml.StartElement); ok {
if v.Name.Local == "svg" {
break
}
}
tok, err = dec.Token()
}
for ; err == nil; tok, err = dec.Token() {
switch v := tok.(type) {
case xml.StartElement:
next := new(node)
next.parent = n
err := unmarshalAttrs(next, v.Attr)
if err != nil {
fmt.Println(err.Error())
}
next.transforms = append(next.transforms, next.parent.transforms...)
if next.self.transform != nil {
next.transforms = append(next.transforms, next.self.transform...)
}
next.k = toKind(v.Name.Local)
if next.k == styleKind {
tok, _ = dec.Token()
if styleStr, ok := tok.(xml.CharData); ok {
unmarshalCSS(string(styleStr))
}
}
if next.self.id != nil {
defs[*next.self.id] = next
}
next.inherited = merge(n.inherited, n.self)
next.parent = n
n.children = append(n.children, next)
n = next
case xml.EndElement:
n = n.parent
}
}
}
// Most Attrs can be parsed easily, but "d" and "points" attributes are more complex.
func unmarshalAttrs(n *node, x []xml.Attr) error {
for _, v := range x {
if v.Value == "" {
continue
}
switch v.Name.Local {
case "class":
a, ok := classes[v.Value]
if ok {
n.self = merge(a, n.self)
}
case "clip-rule":
if v.Value == "evenodd" {
cr := gdf.EvenOdd
n.self.clipRule = &cr
} else {
cr := gdf.NonZero
n.self.clipRule = &cr
}
case "color":
c, ok := parseColor(v.Value)
if ok {
n.self.color = &c
}
case "cx":
f64, _ := strconv.ParseFloat(v.Value, 64)
n.self.cx = &f64
case "cy":
f64, _ := strconv.ParseFloat(v.Value, 64)
n.self.cy = &f64
case "d":
rawCmds, err := run([]byte(v.Value), lexSVGPathOp, parseDAttr)
n.tmpCmds = rawCmds
if err != nil {
return err
}
case "dx":
f64, _ := strconv.ParseFloat(v.Value, 64)
n.self.dx = &f64
case "dy":
f64, _ := strconv.ParseFloat(v.Value, 64)
n.self.dy = &f64
case "fill":
c, ok := parseColor(v.Value)
if ok {
if n.k == maskKind {
n.self.maskFill = &c
} else {
n.self.fill = &c
}
}
case "fillRule":
if v.Value == "evenodd" {
fr := gdf.EvenOdd
n.self.fillRule = &fr
} else {
fr := gdf.NonZero
n.self.fillRule = &fr
}
case "height":
f64, _ := parseAbsoluteLength(v.Value)
n.self.height = &f64
case "href":
n.self.href = &v.Value
case "id":
n.self.id = &v.Value
case "mask":
s := parseMaskURL(v.Value)
n.self.mask = &s
case "points":
rawCmds, err := run([]byte(v.Value), lexPolyNum, parsePointsAttr)
if err != nil {
return err
}
n.tmpCmds = rawCmds
case "r":
f64, _ := strconv.ParseFloat(v.Value, 64)
n.self.r = &f64
case "rx":
f64, _ := strconv.ParseFloat(v.Value, 64)
n.self.rx = &f64
case "ry":
f64, _ := strconv.ParseFloat(v.Value, 64)
n.self.ry = &f64
case "stroke":
c, ok := parseColor(v.Value)
if ok {
n.self.stroke = &c
}
case "strokeLineCap":
n.self.strokeLineCap = new(gdf.LineCap)
case "strokeLineJoin":
n.self.strokeLineJoin = new(gdf.LineJoin)
case "strokeWidth":
f64, _ := strconv.ParseFloat(v.Value, 64)
n.self.strokeWidth = &f64
//case "style":
case "transform":
n.self.transform = parseTransform(v.Value)
case "viewBox":
s := strings.Fields(v.Value)
if len(s) != 4 {
continue
}
var nums [4]float64
for i := range s {
f64, _ := strconv.ParseFloat(s[i], 64)
nums[i] = f64
}
n.self.viewBox = &nums
case "width":
f64, _ := parseAbsoluteLength(v.Value)
n.self.width = &f64
case "x":
f64, _ := parseAbsoluteLength(v.Value)
n.self.x = &f64
case "y":
f64, _ := parseAbsoluteLength(v.Value)
n.self.y = &f64
}
}
return nil
}