Repository navigation
Expand file tree
/
Copy pathsvg_elements.go
More file actions
498 lines (475 loc) · 11.3 KB
/
Copy pathsvg_elements.go
File metadata and controls
498 lines (475 loc) · 11.3 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package svg
import (
"encoding/xml"
"errors"
"strings"
"golang.org/x/image/math/fixed"
)
func init() {
// avoids cyclical static declaration
// called on package initialization
drawFuncs["use"] = useF
}
type svgFunc func(c *svgCursor, attrs []xml.Attr) error
var drawFuncs = map[string]svgFunc{
"svg": svgF,
"g": gF,
"line": lineF,
"stop": stopF,
"rect": rectF,
"circle": circleF,
"ellipse": circleF, // circleF handles ellipse also
"polyline": polylineF,
"polygon": polygonF,
"path": pathF,
"desc": descF,
"defs": defsF,
"title": titleF,
"linearGradient": linearGradientF,
"radialGradient": radialGradientF,
"mask": maskF,
}
func svgF(c *svgCursor, attrs []xml.Attr) error {
c.svg.ViewBox.X = 0
c.svg.ViewBox.Y = 0
c.svg.ViewBox.W = 0
c.svg.ViewBox.H = 0
var width, height float64
var err error
for _, attr := range attrs {
switch attr.Name.Local {
case "viewBox":
err = c.getPoints(attr.Value)
if len(c.points) != 4 {
return errParamMismatch
}
c.svg.ViewBox.X = c.points[0]
c.svg.ViewBox.Y = c.points[1]
c.svg.ViewBox.W = c.points[2]
c.svg.ViewBox.H = c.points[3]
case "width":
c.svg.Width = attr.Value
width, err = parseBasicFloat(attr.Value)
case "height":
c.svg.Height = attr.Value
height, err = parseBasicFloat(attr.Value)
}
if err != nil {
return err
}
}
if c.svg.ViewBox.W == 0 {
c.svg.ViewBox.W = width
}
if c.svg.ViewBox.H == 0 {
c.svg.ViewBox.H = height
}
return nil
}
func gF(*svgCursor, []xml.Attr) error { return nil } // g does nothing but push the style
func rectF(c *svgCursor, attrs []xml.Attr) error {
var x, y, w, h, rx, ry float64
var err error
for _, attr := range attrs {
switch attr.Name.Local {
case "x":
x, err = c.parseUnit(attr.Value, widthPercentage)
case "y":
y, err = c.parseUnit(attr.Value, heightPercentage)
case "width":
w, err = c.parseUnit(attr.Value, widthPercentage)
case "height":
h, err = c.parseUnit(attr.Value, heightPercentage)
case "rx":
rx, err = c.parseUnit(attr.Value, widthPercentage)
case "ry":
ry, err = c.parseUnit(attr.Value, heightPercentage)
}
if err != nil {
return err
}
}
if w == 0 || h == 0 {
return nil
}
// If only one of rx or ry is specified, the other should be same
if rx != 0 && ry == 0 {
ry = rx
}
if ry != 0 && rx == 0 {
rx = ry
}
c.path.addRoundRect(x+c.curX, y+c.curY, w+x+c.curX, h+y+c.curY, rx, ry, 0)
return nil
}
func maskF(c *svgCursor, attrs []xml.Attr) error {
var x, y, w, h float64
var id string
var err error
for _, attr := range attrs {
switch attr.Name.Local {
case "id":
id = attr.Value
if len(id) == 0 {
return errZeroLengthID
}
case "x":
x, err = c.parseUnit(attr.Value, widthPercentage)
case "y":
y, err = c.parseUnit(attr.Value, heightPercentage)
case "width":
w, err = c.parseUnit(attr.Value, widthPercentage)
case "height":
h, err = c.parseUnit(attr.Value, heightPercentage)
}
if err != nil {
return err
}
}
c.inMask = true
c.mask = &SvgMask{
ID: id,
X: x,
Y: y,
W: w,
H: h,
SvgPaths: make([]SvgPath, 0),
Transform: Identity,
}
return nil
}
func circleF(c *svgCursor, attrs []xml.Attr) error {
var cx, cy, rx, ry float64
var err error
for _, attr := range attrs {
switch attr.Name.Local {
case "cx":
cx, err = c.parseUnit(attr.Value, widthPercentage)
case "cy":
cy, err = c.parseUnit(attr.Value, heightPercentage)
case "r":
rx, err = c.parseUnit(attr.Value, diagPercentage)
ry = rx
case "rx":
rx, err = c.parseUnit(attr.Value, widthPercentage)
case "ry":
ry, err = c.parseUnit(attr.Value, heightPercentage)
}
if err != nil {
return err
}
}
if rx == 0 || ry == 0 { // not drawn, but not an error
return nil
}
c.ellipseAt(cx+c.curX, cy+c.curY, rx, ry)
return nil
}
func lineF(c *svgCursor, attrs []xml.Attr) error {
var x1, x2, y1, y2 float64
var err error
for _, attr := range attrs {
switch attr.Name.Local {
case "x1":
x1, err = c.parseUnit(attr.Value, widthPercentage)
case "x2":
x2, err = c.parseUnit(attr.Value, widthPercentage)
case "y1":
y1, err = c.parseUnit(attr.Value, heightPercentage)
case "y2":
y2, err = c.parseUnit(attr.Value, heightPercentage)
}
if err != nil {
return err
}
}
c.path.Start(fixed.Point26_6{
X: fixed.Int26_6((x1 + c.curX) * 64),
Y: fixed.Int26_6((y1 + c.curY) * 64),
})
c.path.Line(fixed.Point26_6{
X: fixed.Int26_6((x2 + c.curX) * 64),
Y: fixed.Int26_6((y2 + c.curY) * 64),
})
return nil
}
func polylineF(c *svgCursor, attrs []xml.Attr) error {
var err error
for _, attr := range attrs {
switch attr.Name.Local {
case "points":
err = c.getPoints(attr.Value)
if len(c.points)%2 != 0 {
return errors.New("polygon has odd number of points")
}
}
if err != nil {
return err
}
}
if len(c.points) > 4 {
c.path.Start(fixed.Point26_6{
X: fixed.Int26_6((c.points[0] + c.curX) * 64),
Y: fixed.Int26_6((c.points[1] + c.curY) * 64),
})
for i := 2; i < len(c.points)-1; i += 2 {
c.path.Line(fixed.Point26_6{
X: fixed.Int26_6((c.points[i] + c.curX) * 64),
Y: fixed.Int26_6((c.points[i+1] + c.curY) * 64),
})
}
}
return nil
}
func polygonF(c *svgCursor, attrs []xml.Attr) error {
err := polylineF(c, attrs)
if len(c.points) > 4 {
c.path.Stop(true)
}
return err
}
func pathF(c *svgCursor, attrs []xml.Attr) error {
var err error
for _, attr := range attrs {
switch attr.Name.Local {
case "d":
err = c.compilePath(attr.Value)
}
if err != nil {
return err
}
}
return nil
}
func descF(c *svgCursor, attrs []xml.Attr) error {
c.inDescText = true
c.svg.Descriptions = append(c.svg.Descriptions, "")
return nil
}
func titleF(c *svgCursor, attrs []xml.Attr) error {
c.inTitleText = true
c.svg.Titles = append(c.svg.Titles, "")
return nil
}
func defsF(c *svgCursor, attrs []xml.Attr) error {
c.inDefs = true
return nil
}
func linearGradientF(c *svgCursor, attrs []xml.Attr) error {
var err error
c.inGrad = true
// interpretation of percentage in direction depends
// on gradientUnits: we first store the string values
// and resolve them in a second pass
directionStrings := [4]string{"0%", "0%", "100%", "0"} // default value
c.grad = &Gradient{Bounds: c.svg.ViewBox, Matrix: Identity}
for _, attr := range attrs {
switch attr.Name.Local {
case "id":
id := attr.Value
if len(id) >= 0 {
c.svg.grads[id] = c.grad
} else {
return errZeroLengthID
}
case "x1":
directionStrings[0] = attr.Value
case "y1":
directionStrings[1] = attr.Value
case "x2":
directionStrings[2] = attr.Value
case "y2":
directionStrings[3] = attr.Value
default:
err = c.readGradAttr(attr)
}
if err != nil {
return err
}
}
// now we can resolve percentages
bbox := Bounds{W: 1, H: 1} // default is ObjectBoundingBox
if c.grad.Units == UserSpaceOnUse {
bbox = c.grad.Bounds
}
var direction Linear
direction[0], err = bbox.resolveUnit(directionStrings[0], widthPercentage)
if err != nil {
return err
}
direction[1], err = bbox.resolveUnit(directionStrings[1], heightPercentage)
if err != nil {
return err
}
direction[2], err = bbox.resolveUnit(directionStrings[2], widthPercentage)
if err != nil {
return err
}
direction[3], err = bbox.resolveUnit(directionStrings[3], heightPercentage)
if err != nil {
return err
}
c.grad.Direction = direction
return nil
}
func radialGradientF(c *svgCursor, attrs []xml.Attr) error {
c.inGrad = true
c.grad = &Gradient{Bounds: c.svg.ViewBox, Matrix: Identity}
var setFx, setFy bool
var err error
directionStrings := [6]string{"50%", "50%", "50%", "50%", "50%", "50%"} // default values
for _, attr := range attrs {
switch attr.Name.Local {
case "id":
id := attr.Value
if len(id) >= 0 {
c.svg.grads[id] = c.grad
} else {
return errZeroLengthID
}
case "cx":
directionStrings[0] = attr.Value
case "cy":
directionStrings[1] = attr.Value
case "fx":
setFx = true
directionStrings[2] = attr.Value
case "fy":
setFy = true
directionStrings[3] = attr.Value
case "r":
directionStrings[4] = attr.Value
case "fr":
directionStrings[5] = attr.Value
default:
err = c.readGradAttr(attr)
}
if err != nil {
return err
}
}
if !setFx { // set fx to cx by default
directionStrings[2] = directionStrings[0]
}
if !setFy { // set fy to cy by default
directionStrings[3] = directionStrings[1]
}
// now we can resolve percentages
bbox := Bounds{W: 1, H: 1} // default is ObjectBoundingBox
if c.grad.Units == UserSpaceOnUse {
bbox = c.grad.Bounds
}
var direction Radial
direction[0], err = bbox.resolveUnit(directionStrings[0], widthPercentage)
if err != nil {
return err
}
direction[1], err = bbox.resolveUnit(directionStrings[1], heightPercentage)
if err != nil {
return err
}
direction[2], err = bbox.resolveUnit(directionStrings[2], widthPercentage)
if err != nil {
return err
}
direction[3], err = bbox.resolveUnit(directionStrings[3], heightPercentage)
if err != nil {
return err
}
direction[4], err = bbox.resolveUnit(directionStrings[4], diagPercentage)
if err != nil {
return err
}
direction[5], err = bbox.resolveUnit(directionStrings[5], diagPercentage)
if err != nil {
return err
}
c.grad.Direction = direction
return nil
}
func stopF(c *svgCursor, attrs []xml.Attr) error {
var err error
if c.inGrad {
stop := GradStop{Opacity: 1.0}
// parse style and push into attrs
attrs, err = appendStyleAttrs(attrs, "stop-color", "stop-opacity")
if err != nil {
return err
}
for _, attr := range attrs {
switch attr.Name.Local {
case "offset":
stop.Offset, err = readFraction(attr.Value)
case "stop-color":
// todo: add current color inherit
var optColor optionnalColor
optColor, err = parseSVGColor(attr.Value)
stop.StopColor = optColor.asColor()
case "stop-opacity":
stop.Opacity, err = parseBasicFloat(attr.Value)
}
if err != nil {
return err
}
}
c.grad.Stops = append(c.grad.Stops, stop)
}
return nil
}
func useF(c *svgCursor, attrs []xml.Attr) error {
var (
href string
x, y float64
err error
)
for _, attr := range attrs {
switch attr.Name.Local {
case "href":
href = attr.Value
case "x":
x, err = c.parseUnit(attr.Value, widthPercentage)
case "y":
y, err = c.parseUnit(attr.Value, heightPercentage)
}
if err != nil {
return err
}
}
c.curX, c.curY = x, y
defer func() {
c.curX, c.curY = 0, 0
}()
if href == "" {
return errors.New("only use tags with href is supported")
}
if !strings.HasPrefix(href, "#") {
return errors.New("only the ID CSS selector is supported")
}
defs, ok := c.svg.defs[href[1:]]
if !ok {
return errors.New("href ID in use statement was not found in saved defs")
}
for _, def := range defs {
if def.Tag == "endg" {
// pop style
c.styleStack = c.styleStack[:len(c.styleStack)-1]
continue
}
if err = c.pushStyle(def.Attrs); err != nil {
return err
}
df, ok := drawFuncs[def.Tag]
if !ok {
errStr := "Cannot process svg element " + def.Tag
return c.handleError(errStr)
}
if err := df(c, def.Attrs); err != nil {
return err
}
if def.Tag != "g" {
// pop style
c.styleStack = c.styleStack[:len(c.styleStack)-1]
}
}
return nil
}