forked from srwiley/oksvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icon_cursor.go
391 lines (376 loc) · 9.35 KB
/
icon_cursor.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
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
// Copyright 2017 The oksvg Authors. All rights reserved.
// created: 2/12/2017 by S.R.Wiley
//
// utils.go implements translation of an SVG2.0 path into a rasterx Path.
package oksvg
import (
"encoding/xml"
"errors"
"fmt"
"image/color"
"log"
"math"
"strings"
"github.com/srwiley/rasterx"
)
// IconCursor is used while parsing SVG files.
type IconCursor struct {
PathCursor
icon *SvgIcon
StyleStack []PathStyle
grad *rasterx.Gradient
inTitleText, inDescText, inGrad, inDefs, inDefsStyle bool
currentDef []definition
}
// ReadGradURL reads an SVG format gradient url
// Since the context of the gradient can affect the colors
// the current fill or line color is passed in and used in
// the case of a nil stopClor value
func (c *IconCursor) ReadGradURL(v string, defaultColor interface{}) (grad rasterx.Gradient, ok bool) {
if strings.HasPrefix(v, "url(") && strings.HasSuffix(v, ")") {
urlStr := strings.TrimSpace(v[4 : len(v)-1])
if strings.HasPrefix(urlStr, "#") {
var g *rasterx.Gradient
g, ok = c.icon.Grads[urlStr[1:]]
if ok {
grad = localizeGradIfStopClrNil(g, defaultColor)
}
}
}
return
}
// ReadGradAttr reads an SVG gradient attribute
func (c *IconCursor) ReadGradAttr(attr xml.Attr) (err error) {
switch attr.Name.Local {
case "gradientTransform":
c.grad.Matrix, err = c.parseTransform(attr.Value)
case "gradientUnits":
switch strings.TrimSpace(attr.Value) {
case "userSpaceOnUse":
c.grad.Units = rasterx.UserSpaceOnUse
case "objectBoundingBox":
c.grad.Units = rasterx.ObjectBoundingBox
}
case "spreadMethod":
switch strings.TrimSpace(attr.Value) {
case "pad":
c.grad.Spread = rasterx.PadSpread
case "reflect":
c.grad.Spread = rasterx.ReflectSpread
case "repeat":
c.grad.Spread = rasterx.RepeatSpread
}
}
return
}
// PushStyle parses the style element, and push it on the style stack. Only color and opacity are supported
// for fill. Note that this parses both the contents of a style attribute plus
// direct fill and opacity attributes.
func (c *IconCursor) PushStyle(attrs []xml.Attr) error {
var pairs []string
className := ""
for _, attr := range attrs {
switch strings.ToLower(attr.Name.Local) {
case "style":
pairs = append(pairs, strings.Split(attr.Value, ";")...)
case "class":
className = attr.Value
default:
pairs = append(pairs, attr.Name.Local+":"+attr.Value)
}
}
// Make a copy of the top style
curStyle := c.StyleStack[len(c.StyleStack)-1]
for _, pair := range pairs {
kv := strings.Split(pair, ":")
if len(kv) >= 2 {
k := strings.ToLower(kv[0])
k = strings.TrimSpace(k)
v := strings.TrimSpace(kv[1])
err := c.readStyleAttr(&curStyle, k, v)
if err != nil {
return err
}
}
}
c.adaptClasses(&curStyle, className)
c.StyleStack = append(c.StyleStack, curStyle) // Push style onto stack
return nil
}
func (c *IconCursor) readTransformAttr(m1 rasterx.Matrix2D, k string) (rasterx.Matrix2D, error) {
ln := len(c.points)
switch k {
case "rotate":
if ln == 1 {
m1 = m1.Rotate(c.points[0] * math.Pi / 180)
} else if ln == 3 {
m1 = m1.Translate(c.points[1], c.points[2]).
Rotate(c.points[0]*math.Pi/180).
Translate(-c.points[1], -c.points[2])
} else {
return m1, errParamMismatch
}
case "translate":
if ln == 1 {
m1 = m1.Translate(c.points[0], 0)
} else if ln == 2 {
m1 = m1.Translate(c.points[0], c.points[1])
} else {
return m1, errParamMismatch
}
case "skewx":
if ln == 1 {
m1 = m1.SkewX(c.points[0] * math.Pi / 180)
} else {
return m1, errParamMismatch
}
case "skewy":
if ln == 1 {
m1 = m1.SkewY(c.points[0] * math.Pi / 180)
} else {
return m1, errParamMismatch
}
case "scale":
if ln == 1 {
m1 = m1.Scale(c.points[0], 0)
} else if ln == 2 {
m1 = m1.Scale(c.points[0], c.points[1])
} else {
return m1, errParamMismatch
}
case "matrix":
if ln == 6 {
m1 = m1.Mult(rasterx.Matrix2D{
A: c.points[0],
B: c.points[1],
C: c.points[2],
D: c.points[3],
E: c.points[4],
F: c.points[5]})
} else {
return m1, errParamMismatch
}
default:
return m1, errParamMismatch
}
return m1, nil
}
func (c *IconCursor) parseTransform(v string) (rasterx.Matrix2D, error) {
ts := strings.Split(v, ")")
m1 := c.StyleStack[len(c.StyleStack)-1].mAdder.M
for _, t := range ts {
t = strings.TrimSpace(t)
if len(t) == 0 {
continue
}
d := strings.Split(t, "(")
if len(d) != 2 || len(d[1]) < 1 {
return m1, errParamMismatch // badly formed transformation
}
err := c.GetPoints(d[1])
if err != nil {
return m1, err
}
m1, err = c.readTransformAttr(m1, strings.ToLower(strings.TrimSpace(d[0])))
if err != nil {
return m1, err
}
}
return m1, nil
}
func (c *IconCursor) readStyleAttr(curStyle *PathStyle, k, v string) error {
switch k {
case "fill":
gradient, ok := c.ReadGradURL(v, curStyle.fillerColor)
if ok {
curStyle.fillerColor = gradient
break
}
var err error
curStyle.fillerColor, err = ParseSVGColor(v)
return err
case "stroke":
gradient, ok := c.ReadGradURL(v, curStyle.linerColor)
if ok {
curStyle.linerColor = gradient
break
}
col, errc := ParseSVGColor(v)
if errc != nil {
return errc
}
if col != nil {
curStyle.linerColor = col.(color.NRGBA)
} else {
curStyle.linerColor = nil
}
case "stroke-linegap":
switch v {
case "flat":
curStyle.LineGap = rasterx.FlatGap
case "round":
curStyle.LineGap = rasterx.RoundGap
case "cubic":
curStyle.LineGap = rasterx.CubicGap
case "quadratic":
curStyle.LineGap = rasterx.QuadraticGap
}
case "stroke-leadlinecap":
switch v {
case "butt":
curStyle.LeadLineCap = rasterx.ButtCap
case "round":
curStyle.LeadLineCap = rasterx.RoundCap
case "square":
curStyle.LeadLineCap = rasterx.SquareCap
case "cubic":
curStyle.LeadLineCap = rasterx.CubicCap
case "quadratic":
curStyle.LeadLineCap = rasterx.QuadraticCap
}
case "stroke-linecap":
switch v {
case "butt":
curStyle.LineCap = rasterx.ButtCap
case "round":
curStyle.LineCap = rasterx.RoundCap
case "square":
curStyle.LineCap = rasterx.SquareCap
case "cubic":
curStyle.LineCap = rasterx.CubicCap
case "quadratic":
curStyle.LineCap = rasterx.QuadraticCap
}
case "stroke-linejoin":
switch v {
case "miter":
curStyle.LineJoin = rasterx.Miter
case "miter-clip":
curStyle.LineJoin = rasterx.MiterClip
case "arc-clip":
curStyle.LineJoin = rasterx.ArcClip
case "round":
curStyle.LineJoin = rasterx.Round
case "arc":
curStyle.LineJoin = rasterx.Arc
case "bevel":
curStyle.LineJoin = rasterx.Bevel
}
case "stroke-miterlimit":
mLimit, err := parseFloat(v, 64)
if err != nil {
return err
}
curStyle.MiterLimit = mLimit
case "stroke-width":
width, err := parseFloat(v, 64)
if err != nil {
return err
}
curStyle.LineWidth = width
case "stroke-dashoffset":
dashOffset, err := parseFloat(v, 64)
if err != nil {
return err
}
curStyle.DashOffset = dashOffset
case "stroke-dasharray":
if v != "none" {
dashes := splitOnCommaOrSpace(v)
dList := make([]float64, len(dashes))
for i, dstr := range dashes {
d, err := parseFloat(strings.TrimSpace(dstr), 64)
if err != nil {
return err
}
dList[i] = d
}
curStyle.Dash = dList
break
}
case "opacity", "stroke-opacity", "fill-opacity":
op, err := parseFloat(v, 64)
if err != nil {
return err
}
if k != "stroke-opacity" {
curStyle.FillOpacity *= op
}
if k != "fill-opacity" {
curStyle.LineOpacity *= op
}
case "transform":
m, err := c.parseTransform(v)
if err != nil {
return err
}
curStyle.mAdder.M = m
}
return nil
}
func (c *IconCursor) readStartElement(se xml.StartElement) (err error) {
var skipDef bool
if se.Name.Local == "radialGradient" || se.Name.Local == "linearGradient" || c.inGrad {
skipDef = true
}
if c.inDefs && !skipDef {
ID := ""
for _, attr := range se.Attr {
if attr.Name.Local == "id" {
ID = attr.Value
}
}
if ID != "" && len(c.currentDef) > 0 {
c.icon.Defs[c.currentDef[0].ID] = c.currentDef
c.currentDef = make([]definition, 0)
}
c.currentDef = append(c.currentDef, definition{
ID: ID,
Tag: se.Name.Local,
Attrs: se.Attr,
})
return nil
}
df, ok := drawFuncs[se.Name.Local]
if !ok {
errStr := "Cannot process svg element " + se.Name.Local
if c.returnError(errStr) {
return errors.New(errStr)
}
return nil
}
err = df(c, se.Attr)
if err != nil {
e := fmt.Sprintf("error during processing svg element %s: %s", se.Name.Local, err.Error())
if c.returnError(e) {
err = errors.New(e)
}
err = nil
}
if len(c.Path) > 0 {
//The cursor parsed a path from the xml element
pathCopy := make(rasterx.Path, len(c.Path))
copy(pathCopy, c.Path)
c.icon.SVGPaths = append(c.icon.SVGPaths,
SvgPath{c.StyleStack[len(c.StyleStack)-1], pathCopy})
c.Path = c.Path[:0]
}
return
}
func (c *IconCursor) adaptClasses(pathStyle *PathStyle, className string) {
if className == "" || len(c.icon.classes) == 0 {
return
}
for k, v := range c.icon.classes[className] {
c.readStyleAttr(pathStyle, k, v)
}
}
func (c *IconCursor) returnError(errMsg string) bool {
if c.ErrorMode == StrictErrorMode {
return true
}
if c.ErrorMode == WarnErrorMode {
log.Println(errMsg)
}
return false
}