-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
234 lines (173 loc) · 5.78 KB
/
index.js
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
var fs = require('fs');
var args = require('minimist')(process.argv.slice(2), {
default: {
e: false
},
});
var jsdom = require("jsdom");
var {JSDOM} = jsdom;
const PATH = {
src: "./svg",
data: "./data",
output: "./output"
}
// Loading source SVG
var source = fs.readFileSync(PATH.src + '/' + args.s, 'utf8');
var dom = new JSDOM(source);
var doc = dom.window.document;
var svgName = (args.s).replace('.svg', '');
// Data extraction from SVG to JSON
if (args.e) {
console.log('Extracting data from ' + args.s + ' ...');
var svgData = new Object();
// Extracting gradients
var allGradients = doc.querySelectorAll("linearGradient");
var gradients = new Object();
for (i = 0; i < allGradients.length; i++) {
var stops = new Array();
var j = 1;
var allStops = allGradients[i].querySelectorAll("stop");
while (j < (allStops.length + 1)) {
var gradient = allGradients[i].querySelector("stop:nth-child(" + j + ")").getAttribute("stop-color");
stops.push(gradient);
j++;
}
gradients[allGradients[i].id] = stops;
}
if (Object.keys(gradients).length > 0) {
svgData["gradients"] = gradients;
}
// Extracting colors
var colors = new Object();
var colorsList = new Array();
const colorAttributes = ["fill", "stroke"];
for (k = 0; k < colorAttributes.length; k++) {
var allColors = doc.querySelectorAll("[" + colorAttributes[k] + "]");
for (i = 0; i < allColors.length; i++) {
var currColor = allColors[i].getAttribute(colorAttributes[k]);
if (!colorsList.includes(currColor) && !currColor.includes("url")) {
colorsList.push(currColor);
}
}
}
colorsList.sort();
for (i = 0; i < colorsList.length; i++) {
colors[colorsList[i]] = colorsList[i];
}
if (Object.keys(colors).length > 0) {
svgData["colors"] = colors;
}
// Extracting fonts
var allFonts = doc.querySelectorAll("[font-family]");
var fonts = new Object();
var fontsList = new Array();
for (i = 0; i < allFonts.length; i++) {
var currFont = allFonts[i].getAttribute("font-family");
if (!fontsList.includes(currFont)) {
fontsList.push(currFont);
}
}
fontsList.sort();
for (i = 0; i < fontsList.length; i++) {
fonts[fontsList[i]] = fontsList[i];
}
if (Object.keys(fonts).length > 0) {
svgData["fonts"] = fonts;
}
// Extracting text
var allText = doc.querySelectorAll("text");
var text = new Object();
var bug = 0;
for (i = 0; i < allText.length; i++) {
var lines = new Array();
var j = 1;
var tspan = allText[i].querySelectorAll("tspan");
while (j < (tspan.length + 1)) {
var line = allText[i].querySelector("tspan:nth-child(" + j + ")").textContent;
lines.push(line);
j++;
}
// Fix for missing IDs
if (allText[i].id == "") {
bug++;
allText[i].id = "missing-id-" + bug;
}
text[allText[i].id] = lines;
}
if (Object.keys(text).length > 0) {
svgData["text"] = text;
}
if (!fs.existsSync(PATH.data)){
fs.mkdirSync(PATH.data);
}
if (bug > 0) {
fs.writeFile(PATH.src + "/" + svgName + ".svg", '<?xml version="1.0" encoding="UTF-8"?>' + "\n" + dom.window.document.querySelector("body").innerHTML, function (err) {
if (err) {
return console.log(err);
}
console.log("Source file was replaced, fixed " + bug + " missing IDs.");
});
}
fs.writeFile(PATH.data + '/' + svgName + ".json", JSON.stringify(svgData, null, 4), function (err) {
if (err) {
return console.log(err);
}
console.log("Data extraction was successful!");
});
return;
}
// Replacing with JSON data
console.log('Replacing data of ' + args.s + ' ...');
var file = fs.readFileSync('data/' + svgName + '.json', 'utf8');
var data = JSON.parse(file);
// Replacing colors
var cl = Object.keys(data.colors);
for (i = 0; i < cl.length; i++) {
var fill = 'fill="' + cl[i] + '"';
var stroke = 'stroke="' + cl[i] + '"';
source = source.split(fill).join('fill="' + data.colors[cl[i]] + '"');
source = source.split(stroke).join('stroke="' + data.colors[cl[i]] + '"');
}
// Replacing fonts
var fl = Object.keys(data.fonts);
for (i = 0; i < fl.length; i++) {
var fontOld = 'font-family="' + fl[i] + '"'
var fontNew = 'font-family="' + data.fonts[fl[i]] + '"'
source = source.split(fontOld).join(fontNew);
}
var dom = new JSDOM(source);
var doc = dom.window.document;
// Replacing gradients
var tl = Object.keys(data.gradients);
for (i = 0; i < tl.length; i++) {
var id = tl[i];
var value = data.gradients[id];
var j = 0;
while (j < value.length) {
j++;
var selector = "linearGradient[id='" + id + "'] stop:nth-child(" + j + ")";
doc.querySelector(selector).setAttribute('stop-color', value[j - 1]);
}
}
// Replacing text
var tl = Object.keys(data.text);
for (i = 0; i < tl.length; i++) {
var id = tl[i];
var value = data.text[id];
var j = 0;
while (j < value.length) {
j++;
var selector = "text[id='" + id + "'] tspan:nth-child(" + j + ")";
doc.querySelector(selector).textContent = value[j - 1];
}
}
// Writing to file
if (!fs.existsSync(PATH.output)){
fs.mkdirSync(PATH.output);
}
fs.writeFile(PATH.output + "/" + svgName + "-new.svg", '<?xml version="1.0" encoding="UTF-8"?>' + "\n" + dom.window.document.querySelector("body").innerHTML, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});