forked from wout/raphael-svg-import
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathraphael-svg-import.js
More file actions
275 lines (240 loc) · 9.01 KB
/
raphael-svg-import.js
File metadata and controls
275 lines (240 loc) · 9.01 KB
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
/*
* Raphael SVG Import 0.0.1 - Extension to Raphael JS
*
* Copyright (c) 2009 Wout Fierens
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*
*
* 2011-12-08 modifications by Jonas Olmstead
* - added support for radial and linear gradients
* - added support for paths
* - removed prototype.js dependencies (I can't read that stuff)
* - changed input parameter to svg xml file
* - added support for text elements
* - added support for nested groups
* - added support for transforms and scaling applied to groups
* - svg elements returned as a set
*
*/
Raphael.fn.importSVG = function (svgXML) {
try {
// create a set to return
var m_myNewSet = this.set();
var strSupportedShapes = "|rect|circle|ellipse|path|image|text|polygon|";
// collect all gradient colors
var linGrads = svgXML.getElementsByTagName("linearGradient");
var radGrads = svgXML.getElementsByTagName("radialGradient");
this.doFill = function(strNode,attr,mNodeName,mNodeValue) {
// check if linear gradient
if (mNodeValue.indexOf("url") == 0) {
var opacity;
var gradID = mNodeValue.substring("url(#".length,mNodeValue.length - 1);
for (var l=0;l<radGrads.length;l++)
if (radGrads.item(l).getAttribute("id") == gradID) {
// get stops
var stop1, stop2;
for (var st=0;st<radGrads.item(l).childNodes.length;st++)
if (radGrads.item(l).childNodes.item(st).nodeName == "stop") {
if (stop1)
stop2 = radGrads.item(l).childNodes.item(st);
else
stop1 = radGrads.item(l).childNodes.item(st);
}
if (!stop1)
return; // could not parse stops
// TODO: implement radial offset
// radial gradients not supported for paths, so do linear
if (strNode == "path")
attr[mNodeName] = 90 + "-" + stop1.getAttribute("stop-color")
+ "-" + stop2.getAttribute("stop-color")
+ ":50-" + stop1.getAttribute("stop-color");
else
attr[mNodeName] = "r(" + radGrads.item(l).getAttribute("fx") + ","
+ radGrads.item(l).getAttribute("fx") + ")" + stop1.getAttribute("stop-color")
+ "-" + stop2.getAttribute("stop-color");
if (stop1.getAttribute("stop-opacity"))
opacity = stop1.getAttribute("stop-opacity")
}
for (var l=0;l<linGrads.length;l++)
if (linGrads.item(l).getAttribute("id") == gradID) {
// get angle
var b = parseFloat(linGrads.item(l).getAttribute("y2")) - parseFloat(linGrads.item(l).getAttribute("y1"));
var c = parseFloat(linGrads.item(l).getAttribute("x2")) - parseFloat(linGrads.item(l).getAttribute("x1"));
var angle = Math.atan(b/c);
if (c < 0)
angle = angle - Math.PI;
angle = parseInt(Raphael.deg(angle) + 360) % 360;
// get stops
var stop1, stop2;
for (var st=0;st<linGrads.item(l).childNodes.length;st++)
if (linGrads.item(l).childNodes.item(st).nodeName == "stop") {
if (stop1)
stop2 = linGrads.item(l).childNodes.item(st);
else
stop1 = linGrads.item(l).childNodes.item(st);
}
if (!stop1)
return; // could not parse stops
// TODO: hardcoded offset value of 50
attr[mNodeName] = angle + "-" + stop1.getAttribute("stop-color")
+ "-" + stop2.getAttribute("stop-color")
+ ":50-" + stop1.getAttribute("stop-color");
if (stop1.getAttribute("stop-opacity"))
opacity = stop1.getAttribute("stop-opacity")
}
if (opacity)
attr["opacity"] = opacity;
} else {
attr[mNodeName] = mNodeValue;
}
};
this.parseElement = function(elShape, myNewSet) {
var node = elShape.nodeName;
if (node == "g") {
// this is a group, parse the children and add to set
var groupSet = this.set();
for (var o=0;o<elShape.childNodes.length;o++)
this.parseElement(elShape.childNodes.item(o), groupSet);
// now apply transforms and attributes to set
for (var k=0;k<elShape.attributes.length;k++) {
var m = elShape.attributes[k];
if (m.nodeName == "transform" && groupSet.transform) {
var actions = m.nodeValue.split(')');
for (var a=0;a<actions.length;a++) {
if (actions[a].indexOf("matrix") == 0)
groupSet.transform("m" + actions[a].substring(actions[a].indexOf("(")+1));
else if (actions[a])
eval("groupSet." + actions[a] + ")");
}
}
else
groupSet.attr(m.nodeName,m.nodeValue);
}
myNewSet.push(groupSet);
return;
}
if (node && strSupportedShapes.indexOf("|" + node + "|") >= 0) {
var attr = { "stroke-width": 0, "fill":"#fff" };
var shape;
var style;
// find the id
var nodeID = elShape.getAttribute("id");
m_font = "";
for (var k=0;k<elShape.attributes.length;k++) {
var m = elShape.attributes[k];
switch(m.nodeName) {
case "stroke-dasharray":
attr[m.nodeName] = "- ";
break;
case "style":
// TODO: handle gradient fills within a style
style = m.nodeValue.split(";");
for (var l=0;l<style.length;l++)
if (style[l].split(":")[0] == "fill")
this.doFill(node,attr,style[l].split(":")[0],style[l].split(":")[1]);
else
attr[style[l].split(":")[0]] = style[l].split(":")[1];
break;
case "fill":
this.doFill(node,attr,m.nodeName,m.nodeValue);
break;
case "font-size":
m_font = m.nodeValue + "px " + m_font;
attr[m.nodeName] = m.nodeValue;
break;
case "font-family":
m_font = m_font + "\"" + m.nodeValue + "\"";
break;
case "x":
case "y":
case "cx":
case "cy":
case "rx":
case "ry":
// use numbers for location coords
attr[m.nodeName] = parseFloat(m.nodeValue);
break;
case "text-anchor":
// skip these due to bug in text scaling
break;
default:
attr[m.nodeName] = m.nodeValue;
break;
}
}
switch(node) {
case "rect":
if (attr["rx"])
shape = this.rect(attr["x"],attr["y"],elShape.getAttribute("width")
,elShape.getAttribute("height"),attr["rx"]);
else
shape = this.rect();
break;
case "circle":
// changed to ellipse, we are not doing circles today
shape = this.ellipse();
attr["rx"] = attr["r"];
attr["ry"] = attr["r"];
break;
case "ellipse":
shape = this.ellipse();
break;
case "path":
shape = this.path(attr["d"]);
break;
case "polygon":
// convert polygon to a path
var point_string = attr["points"].trim();
var aryPoints = point_string.split(" ");
var strNewPoints = "M";
for (var i in aryPoints) {
if (i > 0)
strNewPoints += "L";
strNewPoints += aryPoints[i];
}
strNewPoints += "Z";
shape = this.path(strNewPoints);
break;
case "image":
shape = this.image();
break;
case "text":
shape = this.text(attr["x"],attr["y"],elShape.text || elShape.textContent);
shape.attr("font",m_font);
shape.attr("stroke","none");
shape.origFontPt = parseInt(attr["font-size"]);
break;
}
// put shape into set
myNewSet.push(shape);
// apply attributes
shape.attr(attr);
// apply transforms
for (var k=0;k<elShape.attributes.length;k++) {
var m = elShape.attributes[k];
if (m.nodeName == "transform" && shape.transform) {
var actions = m.nodeValue.split(')');
for (var a=0;a<actions.length;a++) {
if (actions[a].indexOf("matrix") == 0)
shape.transform("m" + actions[a].substring(actions[a].indexOf("(")+1));
else if (actions[a])
eval("shape." + actions[a] + ")");
}
}
}
}
};
var elShape;
var m_font;
var elSVG = svgXML.getElementsByTagName("svg")[0];
elSVG.normalize();
for (var i=0;i<elSVG.childNodes.length;i++) {
elShape = elSVG.childNodes.item(i);
this.parseElement(elShape, m_myNewSet);
}
} catch (error) {
alert("The SVG data you entered was invalid! (" + error + ")");
}
// return our new set
return m_myNewSet;
};