-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.js
337 lines (305 loc) · 11.6 KB
/
converter.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
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
const Converter = (function () {
const COLOR_REGEX = /^#(?:[0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})$/;
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
function convert(vdIn) {
let parsed = _parseVd(vdIn);
console.debug("parsed", parsed);
return _createSvg(parsed);
}
function _parseVd(vdIn) {
let parser = new DOMParser();
let xmlVd = parser.parseFromString(vdIn, "text/xml");
let parsed = {
elements: []
};
// Get vector node
let xmlVector = xmlVd.documentElement;
console.debug(xmlVector);
if (xmlVector.nodeName !== "vector") {
throw "Root element must be a vector!";
}
// Parse vector attributes
if ('android:width' in xmlVector.attributes) {
let w = xmlVector.attributes['android:width'].value;
parsed.width = Number(w.replace(/[^\d.]/gi, ''));
} else {
throw "Missing android:width!";
}
if ('android:height' in xmlVector.attributes) {
let h = xmlVector.attributes['android:height'].value;
parsed.height = Number(h.replace(/[^\d.]/gi, ''));
} else {
throw "Missing android:height!";
}
if ('android:viewportWidth' in xmlVector.attributes) {
let vw = xmlVector.attributes['android:viewportWidth'].value;
parsed.viewportWidth = Number(vw.replace(/[^\d.]/gi, ''));
} else {
parsed.viewportWidth = parsed.width;
}
if ('android:viewportHeight' in xmlVector.attributes) {
let vh = xmlVector.attributes['android:viewportHeight'].value;
parsed.viewportHeight = Number(vh.replace(/[^\d.]/gi, ''));
} else {
parsed.viewportHeight = parsed.height;
}
if ('android:tint' in xmlVector.attributes) {
// TODO
console.log("'android:tint' not implemented");
}
if ('android:tintMode' in xmlVector.attributes) {
// TODO
console.log("'android:tintMode' not implemented");
}
if ('android:autoMirrored' in xmlVector.attributes) {
console.warn("Auto mirroring ('android:autoMirrored') not supported by SVG");
}
if ('android:alpha' in xmlVector.attributes) {
let a = xmlVector.attributes['android:alpha'].value;
parsed.alpha = Number(a.replace(/[^\d.]/gi, ''));
}
// Parse children
let child;
for (child of xmlVector.children) {
let parsedChild = _parseVdChild(child);
if (parsedChild) {
parsed.elements.push(parsedChild);
}
}
return parsed;
}
function _parseVdChild(child) {
if (child.nodeName === 'group') {
return _parseVdGroup(child);
} else if (child.nodeName === 'path') {
return _parseVdPath(child);
} else if (child.nodeName === 'clip-path') {
return _parseVdClipPath(child);
} else {
console.warn("Unknown child node '" + child.nodeName + "'");
return false;
}
}
function _parseVdGroup(vdGroup) {
let group = {
type: 'group',
elements: []
};
if ('android:name' in vdGroup.attributes) {
group.name = vdGroup.attributes['android:name'].value;
}
if ('android:rotation' in vdGroup.attributes) {
let r = vdGroup.attributes['android:rotation'].value;
group.rotation = Number(r.replace(/[^\d.]/gi, ''));
}
if ('android:pivotX' in vdGroup.attributes) {
let px = vdGroup.attributes['android:pivotX'].value;
group.pivotX = Number(px.replace(/[^\d.]/gi, ''));
}
if ('android:pivotY' in vdGroup.attributes) {
let py = vdGroup.attributes['android:pivotY'].value;
group.pivotY = Number(py.replace(/[^\d.]/gi, ''));
}
if ('android:scaleX' in vdGroup.attributes) {
let sx = vdGroup.attributes['android:scaleX'].value;
group.scaleX = Number(sx.replace(/[^\d.]/gi, ''));
}
if ('android:scaleY' in vdGroup.attributes) {
let sy = vdGroup.attributes['android:scaleY'].value;
group.scaleY = Number(sy.replace(/[^\d.]/gi, ''));
}
if ('android:translateX' in vdGroup.attributes) {
let tx = vdGroup.attributes['android:translateX'].value;
group.translateX = Number(tx.replace(/[^\d.]/gi, ''));
}
if ('android:translateY' in vdGroup.attributes) {
let ty = vdGroup.attributes['android:translateY'].value;
group.translateY = Number(ty.replace(/[^\d.]/gi, ''));
}
// Parse children
let child;
for (child of vdGroup.children) {
let parsedChild = _parseVdChild(child);
if (parsedChild) {
group.elements.push(parsedChild);
}
}
return group;
}
function _parseVdPath(vdPath) {
let path = {
type: 'path'
};
if ('android:pathData' in vdPath.attributes) {
path.data = vdPath.attributes['android:pathData'].value;
} else {
throw "Missing 'android:pathData'!";
}
if ('android:name' in vdPath.attributes) {
path.name = vdPath.attributes['android:name'].value;
}
if ('android:fillColor' in vdPath.attributes) {
let color = vdPath.attributes['android:fillColor'].value;
if (color.match(COLOR_REGEX)) {
path.fillColor = color;
} else {
console.warn("Unknown path fill color value '" + color + "'");
}
}
if ('android:strokeColor' in vdPath.attributes) {
let color = vdPath.attributes['android:strokeColor'].value;
if (color.match(COLOR_REGEX)) {
path.strokeColor = color;
} else {
console.warn("Unknown path stroke color value '" + color + "'");
}
}
if ('android:strokeWidth' in vdPath.attributes) {
let w = vdPath.attributes['android:strokeWidth'].value;
path.strokeWidth = Number(w.replace(/[^\d.]/gi, ''));
}
if ('android:strokeAlpha' in vdPath.attributes) {
let a = vdPath.attributes['android:strokeAlpha'].value;
path.strokeAlpha = Number(a.replace(/[^\d.]/gi, ''));
}
if ('android:fillAlpha' in vdPath.attributes) {
let a = vdPath.attributes['android:fillAlpha'].value;
path.fillAlpha = Number(a.replace(/[^\d.]/gi, ''));
}
if ('android:trimPathStart' in vdPath.attributes) {
console.log("'android:trimPathStart' not yet implemented");
}
if ('android:trimPathEnd' in vdPath.attributes) {
console.log("'android:trimPathEnd' not yet implemented");
}
if ('android:trimPathOffset' in vdPath.attributes) {
console.log("'android:trimPathOffset' not yet implemented");
}
if ('android:strokeLineCap' in vdPath.attributes) {
console.log("'android:strokeLineCap' not yet implemented");
}
if ('android:strokeLineJoin' in vdPath.attributes) {
console.log("'android:strokeLineJoin' not yet implemented");
}
if ('android:strokeMiterLimit' in vdPath.attributes) {
console.log("'android:strokeMiterLimit' not yet implemented");
}
if ('android:fillType' in vdPath.attributes) {
console.log("'android:fillType' not yet implemented");
}
return path;
}
function _parseVdClipPath(vdClipPath) {
let clipPath = {
type: 'clip-path'
};
if ('android:pathData' in vdClipPath.attributes) {
clipPath.data = vdClipPath.attributes['android:pathData'].value;
} else {
throw "Missing 'android:pathData'!";
}
if ('android:name' in vdClipPath.attributes) {
clipPath.name = vdClipPath.attributes['android:name'].value;
}
return clipPath;
}
function _createSvg(vectorData) {
let svgDoc = document.implementation.createDocument(SVG_NAMESPACE, "svg", null);
// Add attributes
let svgElem = svgDoc.documentElement;
svgElem.setAttribute("width", vectorData.width);
svgElem.setAttribute("height", vectorData.height);
svgElem.setAttribute("viewBox", "0 0 " + vectorData.viewportWidth + " " + vectorData.viewportHeight);
// TODO tint and alpha
let elem;
for (elem of vectorData.elements) {
let svgChild = _createSvgElement(elem);
if (svgChild) {
svgElem.appendChild(svgChild);
}
}
return svgDoc;
}
function _createSvgElement(elementData) {
if (elementData.type === 'group') {
return _createSvgGroup(elementData);
} else if (elementData.type === 'path') {
return _createSvgPath(elementData);
} else if (elementData.type === 'clip-path') {
return _createSvgClipPath(elementData);
} else {
console.error("Unknown element type '" + elementData.type + "'");
return false;
}
}
function _createSvgGroup(groupData) {
let svgGroup = document.createElementNS(SVG_NAMESPACE, "g");
if ('name' in groupData) {
svgGroup.setAttribute('id', groupData.name);
}
let transform = "";
if ('translateX' in groupData || 'translateY' in groupData) {
transform += "translate(" +
(groupData.translateX || 0) +
" " +
(groupData.translateY || 0) +
") ";
}
if ('scaleX' in groupData || 'scaleY' in groupData) {
transform += "scale(" +
(groupData.scaleX || 1) +
" " +
(groupData.scaleY || 1) +
") ";
}
if ('rotation' in groupData) {
transform += "rotate(" + groupData.rotation;
if ('pivotX' in groupData || 'pivotY' in groupData) {
transform += " " +
(groupData.pivotX || 0) +
" " +
(groupData.pivotY || 0);
}
transform += ") ";
}
if (transform) {
svgGroup.setAttribute('transform', transform);
}
let elem;
for (elem of groupData.elements) {
let svgChild = _createSvgElement(elem);
if (svgChild) {
svgGroup.appendChild(svgChild);
}
}
return svgGroup;
}
function _createSvgPath(pathData) {
let svgPath = document.createElementNS(SVG_NAMESPACE, "path");
svgPath.setAttribute('d', pathData.data);
if ('name' in pathData) {
svgPath.setAttribute('id', pathData.name);
}
if ('fillColor' in pathData) {
svgPath.setAttribute('fill', pathData.fillColor);
}
if ('strokeColor' in pathData) {
svgPath.setAttribute('stroke', pathData.strokeColor);
}
if ('strokeWidth' in pathData) {
svgPath.setAttribute('stroke-width', pathData.strokeWidth);
}
if ('fillAlpha' in pathData) {
svgPath.setAttribute('opacity', pathData.fillAlpha);
}
return svgPath;
}
function _createSvgClipPath(clipPathData) {
let svgClipPath = document.createElementNS(SVG_NAMESPACE, "clipPath");
// TODO
return svgClipPath;
}
return {
convertVD2SVG: convert
};
})();