-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg.go
261 lines (241 loc) · 5.98 KB
/
svg.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
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
package svg
import (
"bytes"
"strconv"
"strings"
"github.com/cdillond/gdf"
)
const px float64 = gdf.In / 96.0
var adjY = gdf.Rotate(180 * gdf.Deg)
type kind uint
const (
svgKind kind = iota
aKind
circleKind
ellipseKind
defsKind
gKind
lineKind
pathKind
polygonKind
rectKind
useKind
styleKind
maskKind
badKind
)
var kinds = [...]string{"svg", "a", "circle", "ellipse", "defs", "g", "line", "path", "polygon", "rect", "use", "style", "mask"}
func isValid(k kind) bool {
return k < badKind
}
func (k kind) String() string {
if isValid(k) {
return kinds[k]
}
return "invalid"
}
func toKind(s string) kind {
for i := range kinds {
if kinds[i] == s {
return kind(i)
}
}
return badKind
}
type node struct {
k kind
parent *node
children []*node
self attributes // attributes defined for the node
inherited attributes // attributes inherited from ancestor nodes
transforms []gdf.Matrix // transforms are applied for each node
tmpCmds []svgCmd
}
// Pointers or pointer-types are used for all fields here in order to differentiate between
// fields with zero values and fields that have not been set.
type attributes struct {
color *gdf.RGBColor // fill and stroke color if neither are set
clipRule *gdf.FillRule
cx, cy *float64 // center point
d []pdfPathCmd // path
dx, dy *float64 // x and y offset
fill *gdf.RGBColor // fill color
maskFill *gdf.RGBColor
fillRule *gdf.FillRule
height *float64
href *string
id *string
mask *string
points *string
r *float64 // radius
rx, ry *float64
stroke *gdf.RGBColor // stroke color
strokeLineCap *gdf.LineCap
strokeLineJoin *gdf.LineJoin
strokeWidth *float64
transform []gdf.Matrix
viewBox *[4]float64
width *float64
x *float64
y *float64
}
var classes = map[string]attributes{}
func unmarshalCSS(s string) {
buf := newBuffer([]byte(s))
// get class name
//start:
for _, ok := buf.peek(); ok; _, ok = buf.peek() {
buf.skipWSP()
var a attributes
_, ok = buf.peek()
if !ok {
return
}
buf.skip()
cname := buf.upTo('{')
buf.skip()
attrs := bytes.Split(buf.upTo('}'), []byte(";"))
for _, attr := range attrs {
before, after, ok := strings.Cut(string(attr), ":")
if !ok {
continue
}
before = strings.TrimSpace(before)
after = strings.TrimSpace(after)
switch before {
case "color":
c, ok := parseColor(after)
if ok {
a.color = &c
}
case "clipRule":
if after == "evenodd" {
cr := gdf.EvenOdd
a.clipRule = &cr
} else {
cr := gdf.NonZero
a.clipRule = &cr
}
case "cx":
f64, _ := strconv.ParseFloat(after, 64)
a.cx = &f64
case "cy":
f64, _ := strconv.ParseFloat(after, 64)
a.cy = &f64
case "dx":
f64, _ := strconv.ParseFloat(after, 64)
a.dx = &f64
case "dy":
f64, _ := strconv.ParseFloat(after, 64)
a.dy = &f64
case "fill": // fill color
c, ok := parseColor(string(after))
if ok {
a.fill = &c
}
case "fillRule":
if after == "evenodd" {
fr := gdf.EvenOdd
a.fillRule = &fr
} else {
fr := gdf.NonZero
a.fillRule = &fr
}
case "height":
f64, _ := parseAbsoluteLength(after)
a.height = &f64
case "mask":
s := parseMaskURL(after)
a.mask = &s
case "r": // radius
f64, _ := strconv.ParseFloat(after, 64)
a.r = &f64
case "rx": // radius
f64, _ := strconv.ParseFloat(after, 64)
a.rx = &f64
case "ry": // radius
f64, _ := strconv.ParseFloat(after, 64)
a.ry = &f64
case "stroke":
c, ok := parseColor(string(after))
if ok {
a.stroke = &c
}
//case "strokeLineCap":
//case "strokeLineJoin":
case "strokeWidth":
f64, _ := strconv.ParseFloat(after, 64)
a.strokeWidth = &f64
case "transform":
a.transform = parseTransform(after)
case "viewBox":
s := strings.Fields(after)
if len(s) != 4 {
continue
}
var nums [4]float64
for i := range s {
f64, _ := strconv.ParseFloat(s[i], 64)
nums[i] = f64
}
a.viewBox = &nums
case "width":
f64, _ := parseAbsoluteLength(after)
a.width = &f64
case "x":
f64, _ := parseAbsoluteLength(after)
a.x = &f64
case "y":
f64, _ := parseAbsoluteLength(after)
a.y = &f64
}
}
classes[string(cname)] = a
buf.skipWSP()
buf.skip()
}
//if _, ok = buf.peek(); ok {
// goto start
//}
}
func merge(parent, child attributes) attributes {
return attributes{
color: inherit(parent.color, child.color),
clipRule: inherit(parent.clipRule, child.clipRule),
cx: inherit(parent.cx, child.cx),
cy: inherit(parent.cy, child.cy),
d: child.d,
dx: inherit(parent.dx, child.dx),
dy: inherit(parent.dy, child.dy),
fill: inherit(parent.fill, child.fill),
fillRule: inherit(parent.fillRule, child.fillRule),
height: inherit(parent.height, child.height),
href: child.href,
//mask: inherit(parent.mask, child.mask),
points: child.points,
r: inherit(parent.r, child.r),
rx: inherit(parent.rx, child.rx),
ry: inherit(parent.ry, child.ry),
stroke: inherit(parent.stroke, child.stroke),
strokeLineCap: inherit(parent.strokeLineCap, child.strokeLineCap),
strokeLineJoin: inherit(parent.strokeLineJoin, child.strokeLineJoin),
strokeWidth: inherit(parent.strokeWidth, child.strokeWidth),
transform: inheritSlice(parent.transform, child.transform),
viewBox: inherit(parent.viewBox, child.viewBox),
x: inherit(parent.x, child.x),
y: inherit(parent.y, child.y),
}
}
// returns b if b is not nil; returns a otherwise
func inherit[T any](parent, child *T) *T {
if child != nil {
return child
}
return parent
}
func inheritSlice(parent, child []gdf.Matrix) []gdf.Matrix {
if child != nil {
return child
}
return parent
}