-
Notifications
You must be signed in to change notification settings - Fork 1
/
iesvg.htc
393 lines (327 loc) · 12.8 KB
/
iesvg.htc
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<public:component tagname="svg">
<public:attach event="ondocumentready" onevent="convert()"/>
</public:component>
<script language="JScript">
var re_split = /[ ,]/;
var re_ltrim = /^\s+/g;
var re_rtrim = /\s+$/g;
function ltrim(str)
{
return str.replace(re_ltrim, '');
}
function rtrim(str)
{
return str.replace(re_rtrim, '');
}
function convert()
{
// create a container for the VML
var oVmlRoot = element.ownerDocument.createElement('div');
oVmlRoot.style.cssText = element.style.cssText;
// setup default fill and stroke
var def_fill = {color: 'black', opacity: '1'};
var def_stroke = {color: 'none', opacity: '1', width: '1px', join: 'miter'};
// start the conversion
convertChildren(oVmlRoot, element, def_fill, def_stroke);
// swap the SVG element contents with eh new VML
element.parentNode.replaceChild(oVmlRoot, element);
}
function convertChildren(oVmlRoot, oSvgRoot, default_fill, default_stroke)
{
// loop over the child SVG tags and convert them to VML
for (i=0; i<oSvgRoot.children.length; i++)
{
var oSvg = oSvgRoot.children[i];
var sTagName = oSvg.tagName;
if (sTagName == "rect")
{
var oVml = element.ownerDocument.createElement('v:rect');
if (oSvg.getAttribute('id') != '')
oVml.id = oSvg.id;
oVml.style.position = 'absolute';
oVml.style.left = oSvg.getAttribute('x');
oVml.style.top = oSvg.getAttribute('y');
oVml.style.width = oSvg.getAttribute('width');
oVml.style.height = oSvg.getAttribute('height');
oVml.setAttribute('coordsize', '21600,21600');
setFill(oVml, oSvg, default_fill);
setStroke(oVml, oSvg, default_stroke);
oVmlRoot.appendChild(oVml);
}
else if (sTagName == "ellipse")
{
var oVml = element.ownerDocument.createElement('v:oval');
if (oSvg.getAttribute('id') != '')
oVml.id = oSvg.id;
oVml.style.position = 'absolute';
oVml.style.left = oSvg.getAttribute('cx') - oSvg.getAttribute('rx');
oVml.style.top = oSvg.getAttribute('cy') - oSvg.getAttribute('ry');
oVml.style.width = oSvg.getAttribute('rx') * 2;
oVml.style.height = oSvg.getAttribute('ry') * 2;
oVml.setAttribute('coordsize', '21600,21600');
setFill(oVml, oSvg, default_fill);
setStroke(oVml, oSvg, default_stroke);
oVmlRoot.appendChild(oVml);
}
else if (sTagName == "circle")
{
var oVml = element.ownerDocument.createElement('v:oval');
if (oSvg.getAttribute('id') != '')
oVml.id = oSvg.id;
oVml.style.position = 'absolute';
oVml.style.left = oSvg.getAttribute('cx') - oSvg.getAttribute('r');
oVml.style.top = oSvg.getAttribute('cy') - oSvg.getAttribute('r');
oVml.style.width = oSvg.getAttribute('r') * 2;
oVml.style.height = oSvg.getAttribute('r') * 2;
oVml.setAttribute('coordsize', '21600,21600');
setFill(oVml, oSvg, default_fill);
setStroke(oVml, oSvg, default_stroke);
oVmlRoot.appendChild(oVml);
}
else if (sTagName == "polyline")
{
var oVml = element.ownerDocument.createElement('v:polyline');
if (oSvg.getAttribute('id') != '')
oVml.id = oSvg.id;
oVml.style.position = 'absolute';
oVml.style.left = '0';
oVml.style.top = '0';
oVml.style.width = '21600';
oVml.style.height = '21600';
oVml.setAttribute('coordsize', '21600,21600');
oVml.setAttribute('points', oSvg.getAttribute('points'));
setFill(oVml, oSvg, default_fill);
setStroke(oVml, oSvg, default_stroke);
oVmlRoot.appendChild(oVml);
}
else if (sTagName == "polygon")
{
var oVml = element.ownerDocument.createElement('v:shape');
if (oSvg.getAttribute('id') != '')
oVml.id = oSvg.id;
oVml.style.position = 'absolute';
oVml.style.left = '0';
oVml.style.top = '0';
oVml.style.width = '21600';
oVml.style.height = '21600';
oVml.setAttribute('coordsize', '21600,21600');
// adjust for the way VML does a polygon
var points = ltrim(oSvg.getAttribute('points')).split(re_split);
var firstpointx = points[0];
var firstpointy = points[1];
var path = 'm ' + firstpointx + ',' + firstpointy + ' l ' + points + ' x e';
oVml.setAttribute('path', path);
setFill(oVml, oSvg, default_fill);
setStroke(oVml, oSvg, default_stroke);
oVmlRoot.appendChild(oVml);
}
else if (sTagName == "path")
{
var oVml = element.ownerDocument.createElement('v:shape');
if (oSvg.getAttribute('id') != '')
oVml.id = oSvg.id;
oVml.style.position = 'absolute';
oVml.style.left = '0';
oVml.style.top = '0';
oVml.style.width = '21600';
oVml.style.height = '21600';
oVml.setAttribute('coordsize', '21600,21600');
// adjust some SVG path commands to VML commands
var path = oSvg.getAttribute('d');
path = path.replace(/c/g,'v');
path = path.replace(/z/g,'x');
// round all decimal points to integers
// VML does not appear to parse them correctly
if (path.search(/\./) > -1) {
pathitems = path.split(re_split);
for (var i=0; i<pathitems.length; i++) {
if (isNaN(parseFloat(pathitems[i])) == false)
pathitems[i] = Math.round(pathitems[i]);
}
path = pathitems.join(" ");
}
oVml.setAttribute('path', path);
setFill(oVml, oSvg, default_fill);
setStroke(oVml, oSvg, default_stroke);
oVmlRoot.appendChild(oVml);
}
else if (sTagName == "g")
{
var oVml = element.ownerDocument.createElement('v:group');
if (oSvg.getAttribute('id') != '')
oVml.id = oSvg.id;
oVml.style.position = 'absolute';
oVml.style.left = '0';
oVml.style.top = '0';
oVml.style.width = '21600';
oVml.style.height = '21600';
oVml.setAttribute('coordsize', '21600,21600');
var local_fill = getFill(oSvg, default_fill);
var local_stroke = getStroke(oSvg, default_stroke);
convertChildren(oVml, oSvg, local_fill, local_stroke);
oVmlRoot.appendChild(oVml);
}
}
}
function setFill(oVml, oSvg, defaults)
{
var oFill = element.ownerDocument.createElement('v:fill');
// check for attributes and STYLE
var fill = oSvg.getAttribute('fill');
if (fill == null)
fill = oSvg.style.getAttribute('fill');
// adjust for VML
if (fill == 'none')
oFill.setAttribute('on', 'false');
else if (fill != null && fill.substr(0,5) == 'url(#')
setGradient(oFill, fill.substr(5, fill.length - 6));
else if (fill != null)
oFill.setAttribute('color', fill);
else
oFill.setAttribute('color', defaults.color);
// check for attributes and STYLE
var opacity = oSvg.getAttribute('fill-opacity');
if (opacity == null)
opacity = oSvg.style.getAttribute('fill-opacity');
if (opacity == null)
opacity = defaults.opacity;
oFill.setAttribute('opacity', opacity);
// oVml.insertAdjacentHTML('beforeEnd', oFill.outerHTML + '</v:fill>');
oVml.appendChild(oFill);
}
function getFill(oSvg, defaults)
{
var local = defaults;
// check for attributes and STYLE
var fill = oSvg.getAttribute('fill');
if (fill == null)
fill = oSvg.style.getAttribute('fill');
if (fill != null)
local.color = fill;
// check for attributes and STYLE
var opacity = oSvg.getAttribute('fill-opacity');
if (opacity == null)
opacity = oSvg.style.getAttribute('fill-opacity');
if (opacity != null)
local.opacity = opacity;
return local;
}
function setStroke(oVml, oSvg, defaults)
{
var oStroke = element.ownerDocument.createElement('v:stroke');
// check for attributes and STYLE
var stroke = oSvg.getAttribute('stroke');
if (stroke == null)
stroke = oSvg.style.getAttribute('stroke');
if (stroke == null)
stroke = defaults.color;
if (stroke == 'none')
oStroke.setAttribute('on', 'false');
else
oStroke.setAttribute('color', stroke);
// check for attributes and STYLE
var strokewidth = oSvg.getAttribute('stroke-width');
if (strokewidth == null)
strokewidth = oSvg.style.getAttribute('stroke-width');
if (strokewidth == null)
strokewidth = defaults.width;
else {
// check for an explicit unit. SVG assumes 'px' if none is given, VML assumes 'pt'
if (isNaN(parseInt(strokewidth.charAt(strokewidth.length - 1), 10)) == false)
strokewidth = parseInt(strokewidth, 10).toFixed(1) + "px";
}
oStroke.setAttribute('weight', strokewidth);
// check for attributes and STYLE
var opacity = oSvg.getAttribute('stroke-opacity');
if (opacity == null)
opacity = oSvg.style.getAttribute('stroke-opacity');
if (opacity == null)
opacity = defaults.opacity
oStroke.setAttribute('opacity', opacity);
// check for attributes and STYLE
var join = oSvg.getAttribute('stroke-linejoin');
if (join == null)
join = oSvg.style.getAttribute('stroke-linejoin');
if (join == null)
join = defaults.join;
oStroke.setAttribute('joinstyle', join);
oVml.appendChild(oStroke);
}
function getStroke(oSvg, defaults)
{
var local = defaults;
// check for attributes and STYLE
var stroke = oSvg.getAttribute('stroke');
if (stroke == null)
stroke = oSvg.style.getAttribute('stroke');
if (stroke != null)
local.color = stroke;
// check for attributes and STYLE
var strokewidth = oSvg.getAttribute('stroke-width');
if (strokewidth == null)
strokewidth = oSvg.style.getAttribute('stroke-width');
if (strokewidth == null)
local.width = strokewidth;
// check for attributes and STYLE
var opacity = oSvg.getAttribute('stroke-opacity');
if (opacity == null)
opacity = oSvg.style.getAttribute('stroke-opacity');
if (opacity != null)
local.opacity = opacity;
// check for attributes and STYLE
var join = oSvg.getAttribute('stroke-linejoin');
if (join == null)
join = oSvg.style.getAttribute('stroke-linejoin');
if (join != null)
local.join = join;
return local;
}
function setGradient(oFill, gradientid)
{
var gradient = element.ownerDocument.getElementById(gradientid);
if (gradient) {
if (gradient.tagName == 'linearGradient')
oFill.setAttribute('type', 'gradient');
else
oFill.setAttribute('type', 'gradientradial');
var x1 = gradient.getAttribute('x1');
x1 = x1 != null ? parseFloat(x1) : 0;
var y1 = gradient.getAttribute('y1');
y1 = y1 != null ? parseFloat(y1) : 0;
var x2 = gradient.getAttribute('x2');
x2 = x2 != null ? parseFloat(x2) : 1;
var y2 = gradient.getAttribute('y2');
y2 = y2 != null ? parseFloat(y2) : 0;
var angle = Math.round(Math.atan((y2 - y1) / (x2 - x1)) * 57.29) + 180;
oFill.setAttribute('angle', angle);
var stops = gradient.getElementsByTagName('stop');
for (var i=0; i<stops.length; i++) {
var stop = stops.item(i);
// check for attributes and STYLE
var offset = stop.getAttribute('offset');
if (offset == null)
offset = stop.style.getAttribute('offset');
// check for attributes and STYLE
var color = stop.getAttribute('stop-color');
if (color == null)
color = stop.style.getAttribute('stop-color');
if (color == null)
color = 'black';
// check for attributes and STYLE
var opacity = stop.getAttribute('stop-opacity');
if (opacity == null)
opacity = stop.style.getAttribute('stop-opacity');
if (opacity == null)
opacity = '1';
if (parseFloat(offset) == 0) {
oFill.setAttribute('color', color);
oFill.setAttribute('opacity', opacity);
}
if (parseFloat(offset) == 1 || parseFloat(offset) == 100) {
oFill.setAttribute('color2', color);
oFill.setAttribute('opacity2', opacity);
}
}
}
}
</script>