-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpublic.go
287 lines (265 loc) · 7.73 KB
/
public.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
// 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 (
"bytes"
"encoding/xml"
"fmt"
"image/color"
"io"
"io/ioutil"
"math"
"os"
"strconv"
"strings"
"github.com/srwiley/rasterx"
"golang.org/x/image/colornames"
"golang.org/x/net/html/charset"
)
// ReadIconStream reads the Icon from the given io.Reader.
// This only supports a sub-set of SVG, but
// is enough to draw many icons. If errMode is provided,
// the first value determines if the icon ignores, errors out, or logs a warning
// if it does not handle an element found in the icon file. Ignore warnings is
// the default if no ErrorMode value is provided.
func ReadIconStream(stream io.Reader, errMode ...ErrorMode) (*SvgIcon, error) {
icon := &SvgIcon{Defs: make(map[string][]definition), Grads: make(map[string]*rasterx.Gradient), Transform: rasterx.Identity}
cursor := &IconCursor{StyleStack: []PathStyle{DefaultStyle}, icon: icon}
if len(errMode) > 0 {
cursor.ErrorMode = errMode[0]
}
classInfo := ""
decoder := xml.NewDecoder(stream)
decoder.CharsetReader = charset.NewReaderLabel
for {
t, err := decoder.Token()
if err != nil {
if err == io.EOF {
break
}
return icon, err
}
// Inspect the type of the XML token
switch se := t.(type) {
case xml.StartElement:
// Reads all recognized style attributes from the start element
// and places it on top of the styleStack
err = cursor.PushStyle(se.Attr)
if err != nil {
return icon, err
}
err = cursor.readStartElement(se)
if err != nil {
return icon, err
}
if se.Name.Local == "style" && cursor.inDefs {
cursor.inDefsStyle = true
}
case xml.EndElement:
// pop style
cursor.StyleStack = cursor.StyleStack[:len(cursor.StyleStack)-1]
switch se.Name.Local {
case "g":
if cursor.inDefs {
cursor.currentDef = append(cursor.currentDef, definition{
Tag: "endg",
})
}
case "title":
cursor.inTitleText = false
case "desc":
cursor.inDescText = false
case "defs":
if len(cursor.currentDef) > 0 {
cursor.icon.Defs[cursor.currentDef[0].ID] = cursor.currentDef
cursor.currentDef = make([]definition, 0)
}
cursor.inDefs = false
case "radialGradient", "linearGradient":
cursor.inGrad = false
case "style":
if cursor.inDefsStyle {
icon.classes, err = parseClasses(classInfo)
if err != nil {
return icon, err
}
cursor.inDefsStyle = false
}
}
case xml.CharData:
if cursor.inTitleText {
icon.Titles[len(icon.Titles)-1] += string(se)
}
if cursor.inDescText {
icon.Descriptions[len(icon.Descriptions)-1] += string(se)
}
if cursor.inDefsStyle {
classInfo = string(se)
}
}
}
return icon, nil
}
// ReadReplacingCurrentColor replaces currentColor value with specified value and loads SvgIcon as ReadIconStream do.
// currentColor value should be valid hex, rgb or named color value.
func ReadReplacingCurrentColor(stream io.Reader, currentColor string, errMode ...ErrorMode) (icon *SvgIcon, err error) {
var (
data []byte
)
if data, err = ioutil.ReadAll(stream); err != nil {
return nil, fmt.Errorf("%w: read data: %v", errParamMismatch, err)
}
if currentColor != "" && strings.Contains(string(data), "currentColor") {
data = []byte(strings.ReplaceAll(string(data), "currentColor", currentColor))
}
if icon, err = ReadIconStream(bytes.NewBuffer(data), errMode...); err != nil {
return nil, fmt.Errorf("%w: load: %v", errParamMismatch, err)
}
return icon, nil
}
// ReadIcon reads the Icon from the named file.
// This only supports a sub-set of SVG, but is enough to draw many icons.
// If errMode is provided, the first value determines if the icon ignores, errors out, or logs a warning
// if it does not handle an element found in the icon file.
// Ignore warnings is the default if no ErrorMode value is provided.
func ReadIcon(iconFile string, errMode ...ErrorMode) (*SvgIcon, error) {
fin, errf := os.Open(iconFile)
if errf != nil {
return nil, errf
}
defer fin.Close()
return ReadIconStream(fin, errMode...)
}
// ParseSVGColorNum reads the SFG color string e.g. #FBD9BD
func ParseSVGColorNum(colorStr string) (r, g, b uint8, err error) {
colorStr = strings.TrimPrefix(colorStr, "#")
var t uint64
if len(colorStr) != 6 {
if len(colorStr) != 3 {
err = fmt.Errorf("color string %s is not length 3 or 6 as required by SVG specification",
colorStr)
return
}
// SVG specs say duplicate characters in case of 3 digit hex number
colorStr = string([]byte{colorStr[0], colorStr[0],
colorStr[1], colorStr[1], colorStr[2], colorStr[2]})
}
for _, v := range []struct {
c *uint8
s string
}{
{&r, colorStr[0:2]},
{&g, colorStr[2:4]},
{&b, colorStr[4:6]}} {
t, err = strconv.ParseUint(v.s, 16, 8)
if err != nil {
return
}
*v.c = uint8(t)
}
return
}
// ParseSVGColor parses an SVG color string in all forms
// including all SVG1.1 names, obtained from the image.colornames package
func ParseSVGColor(colorStr string) (color.Color, error) {
// _, _, _, a := curColor.RGBA()
v := strings.ToLower(colorStr)
if strings.HasPrefix(v, "url") { // We are not handling urls
// and gradients and stuff at this point
return color.NRGBA{0, 0, 0, 255}, nil
}
switch v {
case "none", "":
// nil signals that the function (fill or stroke) is off;
// not the same as black
return nil, nil
default:
cn, ok := colornames.Map[v]
if ok {
r, g, b, a := cn.RGBA()
return color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)}, nil
}
}
cStr := strings.TrimPrefix(colorStr, "rgb(")
if cStr != colorStr {
cStr := strings.TrimSuffix(cStr, ")")
vals := strings.Split(cStr, ",")
if len(vals) != 3 {
return color.NRGBA{}, errParamMismatch
}
var cvals [3]uint8
var err error
for i := range cvals {
cvals[i], err = parseColorValue(vals[i])
if err != nil {
return nil, err
}
}
return color.NRGBA{cvals[0], cvals[1], cvals[2], 0xFF}, nil
}
cStr = strings.TrimPrefix(colorStr, "hsl(")
if cStr != colorStr {
cStr := strings.TrimSuffix(cStr, ")")
vals := strings.Split(cStr, ",")
if len(vals) != 3 {
return color.NRGBA{}, errParamMismatch
}
H, err := strconv.ParseInt(strings.TrimSpace(vals[0]), 10, 64)
if err != nil {
return color.NRGBA{}, fmt.Errorf("invalid hue in hsl: '%s' (%s)", vals[0], err)
}
S, err := strconv.ParseFloat(strings.TrimSpace(vals[1][:len(vals[1])-1]), 64)
if err != nil {
return color.NRGBA{}, fmt.Errorf("invalid saturation in hsl: '%s' (%s)", vals[1], err)
}
S = S / 100
L, err := strconv.ParseFloat(strings.TrimSpace(vals[2][:len(vals[2])-1]), 64)
if err != nil {
return color.NRGBA{}, fmt.Errorf("invalid lightness in hsl: '%s' (%s)", vals[2], err)
}
L = L / 100
C := (1 - math.Abs((2*L)-1)) * S
X := C * (1 - math.Abs(math.Mod((float64(H)/60), 2)-1))
m := L - C/2
var rp, gp, bp float64
if H < 60 {
rp, gp, bp = float64(C), float64(X), float64(0)
} else if H < 120 {
rp, gp, bp = float64(X), float64(C), float64(0)
} else if H < 180 {
rp, gp, bp = float64(0), float64(C), float64(X)
} else if H < 240 {
rp, gp, bp = float64(0), float64(X), float64(C)
} else if H < 300 {
rp, gp, bp = float64(X), float64(0), float64(C)
} else {
rp, gp, bp = float64(C), float64(0), float64(X)
}
r, g, b := math.Round((rp+m)*255), math.Round((gp+m)*255), math.Round((bp+m)*255)
if r > 255 {
r = 255
}
if g > 255 {
g = 255
}
if b > 255 {
b = 255
}
return color.NRGBA{
uint8(r),
uint8(g),
uint8(b),
0xFF,
}, nil
}
if colorStr[0] == '#' {
r, g, b, err := ParseSVGColorNum(colorStr)
if err != nil {
return nil, err
}
return color.NRGBA{r, g, b, 0xFF}, nil
}
return nil, errParamMismatch
}