-
Notifications
You must be signed in to change notification settings - Fork 173
/
fix-svg-style.go
215 lines (180 loc) · 4.44 KB
/
fix-svg-style.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
// fix-svg-style fixes Inkscape palette to be compatible with Affinity Designer
//
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
func main() {
err := filepath.Walk("vector", func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(path) == ".svg" {
return ProcessSVGFile(path)
}
return nil
})
if err != nil {
panic(err)
}
}
func ProcessSVGFile(file string) error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
input := bytes.NewReader(data)
context := &html.Node{
Type: html.ElementNode,
}
nodes, err := html.ParseFragment(input, context)
if err != nil {
return err
}
changed := ProcessSVG(nodes)
output := bytes.NewBuffer(nil)
for _, node := range nodes {
html.Render(output, node)
}
outdata := output.Bytes()
if changed {
fmt.Println("Wrinting ", file)
err2 := ioutil.WriteFile(file, outdata, 0755)
if err2 != nil {
return err2
}
}
return nil
}
func ProcessSVG(nodes []*html.Node) bool {
changed := false
getElementByID := map[string]*html.Node{}
var process func(node *html.Node)
rxRemoveStyle := regexp.MustCompile(`(visibility:visible)[;$]`)
rxFillStyle := regexp.MustCompile(`fill:url\((#[a-zA-Z0-9\-]+)\)`)
rxStrokeStyle := regexp.MustCompile(`stroke:url\((#[a-zA-Z0-9\-]+)\)`)
rxStopColor := regexp.MustCompile(`stop-color:([^;]*)[;$]`)
resolveColorCache := map[string]string{}
resolveColor := func(id string) string {
if cached, ok := resolveColorCache[id]; ok {
return cached
}
node := getElementByID[id]
for {
xlink := GetAttributeValue(node, "href")
if xlink == "" {
break
}
node = getElementByID[xlink[1:]]
}
stops := GetElementsByTagName(node, "stop")
if len(stops) == 0 {
return "rgba(0,0,0,0)"
}
if len(stops) >= 2 {
return "url(#" + id + ")"
}
stop := stops[0]
style := GetAttributeValue(stop, "style")
match := rxStopColor.FindStringSubmatch(style)
resolveColorCache[id] = match[1]
return match[1]
}
process = func(node *html.Node) {
// build index
id := GetAttributeValue(node, "id")
if id != "" {
getElementByID[id] = node
}
// recurse
for child := node.FirstChild; child != nil; child = child.NextSibling {
process(child)
}
if style := GetAttribute(node, "style"); style != nil {
style.Val = rxRemoveStyle.ReplaceAllString(style.Val, "")
style.Val = rxFillStyle.ReplaceAllStringFunc(style.Val, func(attribute string) string {
// fill:url(#xyz)
colorId := attribute[10 : len(attribute)-1]
replacement := "fill:" + resolveColor(colorId)
changed = changed || (replacement != attribute)
return replacement
})
style.Val = rxStrokeStyle.ReplaceAllStringFunc(style.Val, func(attribute string) string {
// colore:url(#xyz)
colorId := attribute[12 : len(attribute)-1]
replacement := "stroke:" + resolveColor(colorId)
changed = changed || (replacement != attribute)
return replacement
})
}
}
for _, node := range nodes {
process(node)
}
// remove all dead gradients
for _, node := range nodes {
defss := GetElementsByTagName(node, "defs")
for _, defs := range defss {
toRemove := []*html.Node{}
for child := defs.FirstChild; child != nil; child = child.NextSibling {
if child.Data == "linearGradient" {
id := GetAttributeValue(child, "id")
noReplacement := "url(#" + id + ")"
if noReplacement != resolveColor(id) {
toRemove = append(toRemove, child)
}
}
}
for _, child := range toRemove {
changed = true
defs.RemoveChild(child)
}
}
}
return changed
}
func GetAttributeValue(node *html.Node, name string) string {
for i := range node.Attr {
if node.Attr[i].Key == name {
return node.Attr[i].Val
}
}
return ""
}
func GetAttribute(node *html.Node, name string) *html.Attribute {
for i := range node.Attr {
if node.Attr[i].Key == name {
return &node.Attr[i]
}
}
return nil
}
func GetElementsByTagName(node *html.Node, tagname string) []*html.Node {
xs := []*html.Node{}
dataAtom := atom.Lookup([]byte(tagname))
if dataAtom != 0 {
tagname = ""
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
if child.Type != html.ElementNode {
continue
}
if child.DataAtom == dataAtom && child.Data == tagname {
xs = append(xs, child)
continue
}
}
return xs
}
func check(err error) {
if err != nil {
panic(err)
}
}