forked from fyne-io/fyne
/
svg.go
158 lines (142 loc) · 5.1 KB
/
svg.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
package theme
import (
"encoding/hex"
"encoding/xml"
"fmt"
"image/color"
"io"
"io/ioutil"
)
// svg holds the unmarshaled XML from a Scalable Vector Graphic
type svg struct {
XMLName xml.Name `xml:"svg"`
XMLNS string `xml:"xmlns,attr"`
Width string `xml:"width,attr"`
Height string `xml:"height,attr"`
ViewBox string `xml:"viewBox,attr"`
Paths []*pathObj `xml:"path"`
Rects []*rectObj `xml:"rect"`
Circles []*circleObj `xml:"circle"`
Polygons []*polygonObj `xml:"polygon"`
Groups []*objGroup `xml:"g"`
}
type pathObj struct {
XMLName xml.Name `xml:"path"`
Fill string `xml:"fill,attr,omitempty"`
Stroke string `xml:"stroke,attr,omitempty"`
StrokeWidth string `xml:"stroke-width,attr,omitempty"`
StrokeLineCap string `xml:"stroke-linecap,attr,omitempty"`
StrokeLineJoin string `xml:"stroke-linejoin,attr,omitempty"`
StrokeDashArray string `xml:"stroke-dasharray,attr,omitempty"`
D string `xml:"d,attr"`
}
type rectObj struct {
XMLName xml.Name `xml:"rect"`
Fill string `xml:"fill,attr,omitempty"`
Stroke string `xml:"stroke,attr,omitempty"`
StrokeWidth string `xml:"stroke-width,attr,omitempty"`
StrokeLineCap string `xml:"stroke-linecap,attr,omitempty"`
StrokeLineJoin string `xml:"stroke-linejoin,attr,omitempty"`
StrokeDashArray string `xml:"stroke-dasharray,attr,omitempty"`
X string `xml:"x,attr"`
Y string `xml:"y,attr"`
Width string `xml:"width,attr"`
Height string `xml:"height,attr"`
}
type circleObj struct {
XMLName xml.Name `xml:"circle"`
Fill string `xml:"fill,attr,omitempty"`
Stroke string `xml:"stroke,attr,omitempty"`
StrokeWidth string `xml:"stroke-width,attr,omitempty"`
StrokeLineCap string `xml:"stroke-linecap,attr,omitempty"`
StrokeLineJoin string `xml:"stroke-linejoin,attr,omitempty"`
StrokeDashArray string `xml:"stroke-dasharray,attr,omitempty"`
CX string `xml:"cx,attr"`
CY string `xml:"cy,attr"`
R string `xml:"r,attr"`
}
type polygonObj struct {
XMLName xml.Name `xml:"polygon"`
Fill string `xml:"fill,attr,omitempty"`
Stroke string `xml:"stroke,attr,omitempty"`
StrokeWidth string `xml:"stroke-width,attr,omitempty"`
StrokeLineCap string `xml:"stroke-linecap,attr,omitempty"`
StrokeLineJoin string `xml:"stroke-linejoin,attr,omitempty"`
StrokeDashArray string `xml:"stroke-dasharray,attr,omitempty"`
Points string `xml:"points,attr"`
}
type objGroup struct {
XMLName xml.Name `xml:"g"`
ID string `xml:"id,attr,omitempty"`
Fill string `xml:"fill,attr,omitempty"`
Stroke string `xml:"stroke,attr,omitempty"`
StrokeWidth string `xml:"stroke-width,attr,omitempty"`
StrokeLineCap string `xml:"stroke-linecap,attr,omitempty"`
StrokeLineJoin string `xml:"stroke-linejoin,attr,omitempty"`
StrokeDashArray string `xml:"stroke-dasharray,attr,omitempty"`
Paths []*pathObj `xml:"path"`
Rects []*rectObj `xml:"rect"`
Polygons []*polygonObj `xml:"polygon"`
}
func replacePathsFill(paths []*pathObj, hexColor string) {
for _, path := range paths {
if path.Fill != "none" {
path.Fill = hexColor
}
}
}
func replaceRectsFill(rects []*rectObj, hexColor string) {
for _, rect := range rects {
if rect.Fill != "none" {
rect.Fill = hexColor
}
}
}
func replaceCirclesFill(circles []*circleObj, hexColor string) {
for _, circle := range circles {
if circle.Fill != "none" {
circle.Fill = hexColor
}
}
}
func replacePolygonsFill(polys []*polygonObj, hexColor string) {
for _, poly := range polys {
if poly.Fill != "none" {
poly.Fill = hexColor
}
}
}
func replaceGroupObjectFill(groups []*objGroup, hexColor string) {
for _, grp := range groups {
replacePathsFill(grp.Paths, hexColor)
replaceRectsFill(grp.Rects, hexColor)
replacePolygonsFill(grp.Polygons, hexColor)
}
}
// replaceFillColor alters an svg objects fill color. Note that if an svg with multiple fill
// colors is being operated upon, all fills will be converted to a single color. Mostly used
// to recolor Icons to match the theme's IconColor.
func (s *svg) replaceFillColor(reader io.Reader, color color.Color) error {
replacePathsFill(s.Paths, colorToHexString(color))
replaceRectsFill(s.Rects, colorToHexString(color))
replaceCirclesFill(s.Circles, colorToHexString(color))
replacePolygonsFill(s.Polygons, colorToHexString(color))
replaceGroupObjectFill(s.Groups, colorToHexString(color))
return nil
}
func svgFromXML(reader io.Reader) (*svg, error) {
var s svg
bSlice, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
if err := xml.Unmarshal(bSlice, &s); err != nil {
return nil, err
}
return &s, nil
}
func colorToHexString(color color.Color) string {
r, g, b, _ := color.RGBA()
cBytes := []byte{byte(r), byte(g), byte(b)}
return fmt.Sprintf("#%s", hex.EncodeToString(cBytes))
}