/
parse.go
378 lines (364 loc) · 9.11 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
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
package svg
import (
"encoding/xml"
"errors"
"fmt"
"log"
"math"
"strings"
"golang.org/x/image/math/fixed"
)
type (
// svgCursor is used while parsing SVG files
svgCursor struct {
pathCursor
svg *Svg
styleStack []PathStyle
grad *Gradient
inTitleText, inDescText, inGrad, inDefs, inMask bool
currentDef []definition
mask *SvgMask
}
// definition is used to store what's given in a def tag
definition struct {
ID, Tag string
Attrs []xml.Attr
}
)
func fToFixed(f float64) fixed.Int26_6 {
return fixed.Int26_6(f * 64)
}
// treat the error according to the errorMode
func (c *svgCursor) handleError(originFmt string, args ...interface{}) error {
formatted := fmt.Sprintf(originFmt, args...)
if c.errorMode == StrictErrorMode {
return errors.New(formatted)
} else if c.errorMode == WarnErrorMode {
log.Println(formatted) // then return nil
}
return nil
}
func (c *svgCursor) readTransformAttr(m1 Matrix2D, k string) (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(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 *svgCursor) parseTransform(v string) (Matrix2D, error) {
ts := strings.Split(v, ")")
m1 := c.styleStack[len(c.styleStack)-1].Transform
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 *svgCursor) parseSelector(v string) (string, error) {
if len(v) == 0 || v == "none" {
return "", nil
}
if strings.HasPrefix(v, "url(") {
i := strings.Index(v, ")")
if i < 0 {
return "", errParamMismatch
}
v = v[4:i]
if !strings.HasPrefix(v, "#") {
return "", fmt.Errorf("unsupported url selector: %s", v)
}
return v[1:], nil
}
return "", fmt.Errorf("unsupported selector: %s", v)
}
func (c *svgCursor) readStyleAttr(curStyle *PathStyle, k, v string) error {
switch k {
case "fill":
gradient, ok := c.readGradURL(v, curStyle.FillerColor)
if ok {
curStyle.FillerColor = gradient
break
}
optCol, err := parseSVGColor(v)
curStyle.FillerColor = optCol.asPattern()
return err
case "stroke":
gradient, ok := c.readGradURL(v, curStyle.LinerColor)
if ok {
curStyle.LinerColor = gradient
break
}
optCol, errc := parseSVGColor(v)
if errc != nil {
return errc
}
curStyle.LinerColor = optCol.asPattern()
case "stroke-linegap":
switch v {
case "flat":
curStyle.Join.LineGap = FlatGap
case "round":
curStyle.Join.LineGap = RoundGap
case "cubic":
curStyle.Join.LineGap = CubicGap
case "quadratic":
curStyle.Join.LineGap = QuadraticGap
default:
return c.handleError("unsupported value '%s' for <stroke-linegap>", v)
}
case "stroke-leadlinecap":
switch v {
case "butt":
curStyle.Join.LeadLineCap = ButtCap
case "round":
curStyle.Join.LeadLineCap = RoundCap
case "square":
curStyle.Join.LeadLineCap = SquareCap
case "cubic":
curStyle.Join.LeadLineCap = CubicCap
case "quadratic":
curStyle.Join.LeadLineCap = QuadraticCap
default:
return c.handleError("unsupported value '%s' for <stroke-leadlinecap>", v)
}
case "stroke-linecap":
switch v {
case "butt":
curStyle.Join.TrailLineCap = ButtCap
case "round":
curStyle.Join.TrailLineCap = RoundCap
case "square":
curStyle.Join.TrailLineCap = SquareCap
case "cubic":
curStyle.Join.TrailLineCap = CubicCap
case "quadratic":
curStyle.Join.TrailLineCap = QuadraticCap
default:
return c.handleError("unsupported value '%s' for <stroke-linecap>", v)
}
case "stroke-linejoin":
switch v {
case "miter":
curStyle.Join.LineJoin = Miter
case "miter-clip":
curStyle.Join.LineJoin = MiterClip
case "arc-clip":
curStyle.Join.LineJoin = ArcClip
case "round":
curStyle.Join.LineJoin = Round
case "arc":
curStyle.Join.LineJoin = Arc
case "bevel":
curStyle.Join.LineJoin = Bevel
default:
return c.handleError("unsupported value '%s' for <stroke-linejoin>", v)
}
case "stroke-miterlimit":
mLimit, err := parseBasicFloat(v)
if err != nil {
return err
}
curStyle.Join.MiterLimit = fToFixed(mLimit)
case "stroke-width":
width, err := c.parseUnit(v, widthPercentage)
if err != nil {
return err
}
curStyle.LineWidth = width
case "stroke-dashoffset":
dashOffset, err := c.parseUnit(v, diagPercentage)
if err != nil {
return err
}
curStyle.Dash.DashOffset = dashOffset
case "stroke-dasharray":
if v != "none" {
dashes := splitOnCommaOrSpace(v)
dList := make([]float64, len(dashes))
for i, dstr := range dashes {
d, err := c.parseUnit(strings.TrimSpace(dstr), diagPercentage)
if err != nil {
return err
}
dList[i] = d
}
curStyle.Dash.Dash = dList
break
}
case "opacity", "stroke-opacity", "fill-opacity":
op, err := parseBasicFloat(v)
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.Transform = m
case "mask":
id, err := c.parseSelector(v)
if err != nil {
return err
}
curStyle.Masks = append(curStyle.Masks, id)
}
return nil
}
// 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 *svgCursor) pushStyle(attrs []xml.Attr) error {
var pairs []string
for _, attr := range attrs {
switch strings.ToLower(attr.Name.Local) {
case "style":
pairs = append(pairs, strings.Split(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.styleStack = append(c.styleStack, curStyle) // Push style onto stack
return nil
}
// splitOnCommaOrSpace returns a list of strings after splitting the input on comma and space delimiters
func splitOnCommaOrSpace(s string) []string {
return strings.FieldsFunc(s,
func(r rune) bool {
return r == ',' || r == ' '
})
}
func (c *svgCursor) 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.svg.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.errorMode == StrictErrorMode {
return errors.New(errStr)
} else if c.errorMode == WarnErrorMode {
log.Println(errStr)
}
return nil
}
err = df(c, se.Attr)
if len(c.path) > 0 {
// The svgCursor parsed a path from the xml element
pathCopy := append(Path{}, c.path...)
if c.inMask && c.mask != nil {
c.mask.SvgPaths = append(c.mask.SvgPaths,
SvgPath{Path: pathCopy, Style: c.styleStack[len(c.styleStack)-1]})
} else if !c.inMask {
c.svg.SvgPaths = append(c.svg.SvgPaths,
SvgPath{Path: pathCopy, Style: c.styleStack[len(c.styleStack)-1]})
}
c.path = c.path[:0]
}
return
}