forked from srwiley/oksvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.go
392 lines (385 loc) · 9.68 KB
/
draw.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
392
// 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"
"log"
"strings"
"github.com/srwiley/rasterx"
"golang.org/x/image/math/fixed"
)
// svgFunc defines function interface to use as drawing implementation.
type svgFunc func(c *IconCursor, 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,
"style": styleF,
"title": titleF,
"linearGradient": linearGradientF,
"radialGradient": radialGradientF,
}
svgF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
c.icon.ViewBox.X = 0
c.icon.ViewBox.Y = 0
c.icon.ViewBox.W = 0
c.icon.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.icon.ViewBox.X = c.points[0]
c.icon.ViewBox.Y = c.points[1]
c.icon.ViewBox.W = c.points[2]
c.icon.ViewBox.H = c.points[3]
case "width":
width, err = parseFloat(attr.Value, 64)
case "height":
height, err = parseFloat(attr.Value, 64)
}
if err != nil {
return err
}
}
if c.icon.ViewBox.W == 0 {
c.icon.ViewBox.W = width
}
if c.icon.ViewBox.H == 0 {
c.icon.ViewBox.H = height
}
return nil
}
gF svgFunc = func(*IconCursor, []xml.Attr) error { return nil } // g does nothing but push the style
rectF svgFunc = func(c *IconCursor, 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 = parseFloat(attr.Value, 64)
case "y":
y, err = parseFloat(attr.Value, 64)
case "width":
w, err = parseFloat(attr.Value, 64)
case "height":
h, err = parseFloat(attr.Value, 64)
case "rx":
rx, err = parseFloat(attr.Value, 64)
case "ry":
ry, err = parseFloat(attr.Value, 64)
}
if err != nil {
return err
}
}
if w == 0 || h == 0 {
return nil
}
rasterx.AddRoundRect(x, y, w+x, h+y, rx, ry, 0, rasterx.RoundGap, &c.Path)
return nil
}
circleF svgFunc = func(c *IconCursor, 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 = parseFloat(attr.Value, 64)
case "cy":
cy, err = parseFloat(attr.Value, 64)
case "r":
rx, err = parseFloat(attr.Value, 64)
ry = rx
case "rx":
rx, err = parseFloat(attr.Value, 64)
case "ry":
ry, err = parseFloat(attr.Value, 64)
}
if err != nil {
return err
}
}
if rx == 0 || ry == 0 { // not drawn, but not an error
return nil
}
c.EllipseAt(cx, cy, rx, ry)
return nil
}
lineF svgFunc = func(c *IconCursor, 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 = parseFloat(attr.Value, 64)
case "x2":
x2, err = parseFloat(attr.Value, 64)
case "y1":
y1, err = parseFloat(attr.Value, 64)
case "y2":
y2, err = parseFloat(attr.Value, 64)
}
if err != nil {
return err
}
}
c.Path.Start(fixed.Point26_6{
X: fixed.Int26_6((x1) * 64),
Y: fixed.Int26_6((y1) * 64)})
c.Path.Line(fixed.Point26_6{
X: fixed.Int26_6((x2) * 64),
Y: fixed.Int26_6((y2) * 64)})
return nil
}
polylineF svgFunc = func(c *IconCursor, 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]) * 64),
Y: fixed.Int26_6((c.points[1]) * 64)})
for i := 2; i < len(c.points)-1; i += 2 {
c.Path.Line(fixed.Point26_6{
X: fixed.Int26_6((c.points[i]) * 64),
Y: fixed.Int26_6((c.points[i+1]) * 64)})
}
}
return nil
}
polygonF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
err := polylineF(c, attrs)
if len(c.points) > 4 {
c.Path.Stop(true)
}
return err
}
pathF svgFunc = func(c *IconCursor, 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
}
descF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
c.inDescText = true
c.icon.Descriptions = append(c.icon.Descriptions, "")
return nil
}
titleF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
c.inTitleText = true
c.icon.Titles = append(c.icon.Titles, "")
return nil
}
defsF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
c.inDefs = true
return nil
}
styleF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
c.inDefsStyle = true
return nil
}
linearGradientF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
var err error
c.inGrad = true
c.grad = &rasterx.Gradient{Points: [5]float64{0, 0, 1, 0, 0},
IsRadial: false, Bounds: c.icon.ViewBox, Matrix: rasterx.Identity}
for _, attr := range attrs {
switch attr.Name.Local {
case "id":
id := attr.Value
if len(id) >= 0 {
c.icon.Grads[id] = c.grad
} else {
return errZeroLengthID
}
case "x1":
c.grad.Points[0], err = readFraction(attr.Value)
case "y1":
c.grad.Points[1], err = readFraction(attr.Value)
case "x2":
c.grad.Points[2], err = readFraction(attr.Value)
case "y2":
c.grad.Points[3], err = readFraction(attr.Value)
default:
err = c.ReadGradAttr(attr)
}
if err != nil {
return err
}
}
return nil
}
radialGradientF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
c.inGrad = true
c.grad = &rasterx.Gradient{Points: [5]float64{0.5, 0.5, 0.5, 0.5, 0.5},
IsRadial: true, Bounds: c.icon.ViewBox, Matrix: rasterx.Identity}
var setFx, setFy bool
var err error
for _, attr := range attrs {
switch attr.Name.Local {
case "id":
id := attr.Value
if len(id) >= 0 {
c.icon.Grads[id] = c.grad
} else {
return errZeroLengthID
}
case "r":
c.grad.Points[4], err = readFraction(attr.Value)
case "cx":
c.grad.Points[0], err = readFraction(attr.Value)
case "cy":
c.grad.Points[1], err = readFraction(attr.Value)
case "fx":
setFx = true
c.grad.Points[2], err = readFraction(attr.Value)
case "fy":
setFy = true
c.grad.Points[3], err = readFraction(attr.Value)
default:
err = c.ReadGradAttr(attr)
}
if err != nil {
return err
}
}
if !setFx { // set fx to cx by default
c.grad.Points[2] = c.grad.Points[0]
}
if !setFy { // set fy to cy by default
c.grad.Points[3] = c.grad.Points[1]
}
return nil
}
stopF svgFunc = func(c *IconCursor, attrs []xml.Attr) error {
var err error
if c.inGrad {
stop := rasterx.GradStop{Opacity: 1.0}
for _, attr := range attrs {
switch attr.Name.Local {
case "offset":
stop.Offset, err = readFraction(attr.Value)
case "stop-color":
//todo: add current color inherit
stop.StopColor, err = ParseSVGColor(attr.Value)
case "stop-opacity":
stop.Opacity, err = parseFloat(attr.Value, 64)
}
if err != nil {
return err
}
}
c.grad.Stops = append(c.grad.Stops, stop)
}
return nil
}
useF svgFunc = func(c *IconCursor, 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 = parseFloat(attr.Value, 64)
case "y":
y, err = parseFloat(attr.Value, 64)
}
if err != nil {
return err
}
}
// Translate the Style adder matrix by use's x and y
c.StyleStack[len(c.StyleStack)-1].mAdder.M =
c.StyleStack[len(c.StyleStack)-1].mAdder.M.Translate(x, y)
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.icon.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
if c.ErrorMode == StrictErrorMode {
return errors.New(errStr)
} else if c.ErrorMode == WarnErrorMode {
log.Println(errStr)
}
return nil
}
if err := df(c, def.Attrs); err != nil {
return err
}
//Did c.Path get added to during the drawFunction call iteration?
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]
}
if def.Tag != "g" {
// pop style
c.StyleStack = c.StyleStack[:len(c.StyleStack)-1]
}
}
return nil
}
)
func init() {
// avoids cyclical static declaration
// called on package initialization
drawFuncs["use"] = useF
}