Fixed VML, added Set.splice method
[raphael] / raphael.vml.js
1 // ┌─────────────────────────────────────────────────────────────────────┐ \\
2 // │ Raphaël 2 - JavaScript Vector Library                               │ \\
3 // ├─────────────────────────────────────────────────────────────────────┤ \\
4 // │ VML Module                                                          │ \\
5 // ├─────────────────────────────────────────────────────────────────────┤ \\
6 // │ Copyright (c) 2008-2011 Dmitry Baranovskiy (http://raphaeljs.com)   │ \\
7 // │ Copyright (c) 2008-2011 Sencha Labs (http://sencha.com)             │ \\
8 // │ Licensed under the MIT (http://raphaeljs.com/license.html) license. │ \\
9 // └─────────────────────────────────────────────────────────────────────┘ \\
10 window.Raphael.vml && function (R) {
11     var has = "hasOwnProperty",
12         Str = String,
13         toFloat = parseFloat,
14         math = Math,
15         round = math.round,
16         mmax = math.max,
17         mmin = math.min,
18         abs = math.abs,
19         fillString = "fill",
20         separator = /[, ]+/,
21         eve = R.eve,
22         S = " ",
23         E = "";
24     // VML
25     var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
26         bites = /([clmz]),?([^clmz]*)/gi,
27         blurregexp = / progid:\S+Blur\([^\)]+\)/g,
28         val = /-?[^,\s-]+/g,
29         cssDot = "position:absolute;left:0;top:0;width:1px;height:1px",
30         zoom = 21600,
31         pathTypes = {path: 1, rect: 1},
32         ovalTypes = {circle: 1, ellipse: 1},
33         path2vml = function (path) {
34             var total =  /[ahqstv]/ig,
35                 command = R._pathToAbsolute;
36             Str(path).match(total) && (command = R._path2curve);
37             total = /[clmz]/g;
38             if (command == R._pathToAbsolute && !Str(path).match(total)) {
39                 var res = Str(path).replace(bites, function (all, command, args) {
40                     var vals = [],
41                         isMove = command.toLowerCase() == "m",
42                         res = map[command];
43                     args.replace(val, function (value) {
44                         if (isMove && vals.length == 2) {
45                             res += vals + map[command == "m" ? "l" : "L"];
46                             vals = [];
47                         }
48                         vals.push(round(value * zoom));
49                     });
50                     return res + vals;
51                 });
52                 return res;
53             }
54             var pa = command(path), p, r;
55             res = [];
56             for (var i = 0, ii = pa.length; i < ii; i++) {
57                 p = pa[i];
58                 r = pa[i][0].toLowerCase();
59                 r == "z" && (r = "x");
60                 for (var j = 1, jj = p.length; j < jj; j++) {
61                     r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
62                 }
63                 res.push(r);
64             }
65             return res.join(S);
66         },
67         compensation = function (deg, dx, dy) {
68             var m = R.matrix();
69             m.rotate(-deg, .5, .5);
70             return {
71                 dx: m.x(dx, dy),
72                 dy: m.y(dx, dy)
73             };
74         },
75         setCoords = function (p, sx, sy, dx, dy, deg) {
76             var _ = p._,
77                 m = p.matrix,
78                 fillpos = _.fillpos,
79                 o = p.node,
80                 s = o.style,
81                 y = 1,
82                 flip = "",
83                 dxdy,
84                 kx = zoom / sx,
85                 ky = zoom / sy;
86             s.visibility = "hidden";
87             if (!sx || !sy) {
88                 return;
89             }
90             o.coordsize = abs(kx) + S + abs(ky);
91             s.rotation = deg * (sx * sy < 0 ? -1 : 1);
92             if (deg) {
93                 var c = compensation(deg, dx, dy);
94                 dx = c.dx;
95                 dy = c.dy;
96             }
97             sx < 0 && (flip += "x");
98             sy < 0 && (flip += " y") && (y = -1);
99             s.flip = flip;
100             o.coordorigin = (dx * -kx) + S + (dy * -ky);
101             if (fillpos || _.fillsize) {
102                 var fill = o.getElementsByTagName(fillString);
103                 fill = fill && fill[0];
104                 o.removeChild(fill);
105                 if (fillpos) {
106                     c = compensation(deg, m.x(fillpos[0], fillpos[1]), m.y(fillpos[0], fillpos[1]));
107                     fill.position = c.dx * y + S + c.dy * y;
108                 }
109                 if (_.fillsize) {
110                     fill.size = _.fillsize[0] * abs(sx) + S + _.fillsize[1] * abs(sy);
111                 }
112                 o.appendChild(fill);
113             }
114             s.visibility = "visible";
115         };
116     R.toString = function () {
117         return  "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
118     };
119     addArrow = function (o, value, isEnd) {
120         var values = Str(value).toLowerCase().split("-"),
121             se = isEnd ? "end" : "start",
122             i = values.length,
123             type = "classic",
124             w = "medium",
125             h = "medium";
126         while (i--) {
127             switch (values[i]) {
128                 case "block":
129                 case "classic":
130                 case "oval":
131                 case "diamond":
132                 case "open":
133                 case "none":
134                     type = values[i];
135                     break;
136                 case "wide":
137                 case "narrow": h = values[i]; break;
138                 case "long":
139                 case "short": w = values[i]; break;
140             }
141         }
142         var stroke = o.node.getElementsByTagName("stroke")[0];
143         stroke[se + "arrow"] = type;
144         stroke[se + "arrowlength"] = w;
145         stroke[se + "arrowwidth"] = h;
146     };
147     setFillAndStroke = function (o, params) {
148         // o.paper.canvas.style.display = "none";
149         o.attrs = o.attrs || {};
150         var node = o.node,
151             a = o.attrs,
152             s = node.style,
153             xy,
154             newpath = pathTypes[o.type] && (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.cx != a.cx || params.cy != a.cy || params.rx != a.rx || params.ry != a.ry || params.r != a.r),
155             isOval = ovalTypes[o.type] && (a.cx != params.cx || a.cy != params.cy || a.r != params.r || a.rx != params.rx || a.ry != params.ry),
156             res = o;
157
158
159         for (var par in params) if (params[has](par)) {
160             a[par] = params[par];
161         }
162         if (newpath) {
163             a.path = R._getPath[o.type](o);
164             o._.dirty = 1;
165         }
166         params.href && (node.href = params.href);
167         params.title && (node.title = params.title);
168         params.target && (node.target = params.target);
169         params.cursor && (s.cursor = params.cursor);
170         "blur" in params && o.blur(params.blur);
171         "transform" in params && o.transform(params.transform);
172         if (params.path && o.type == "path" || newpath) {
173             node.path = path2vml(a.path);
174         }
175         if (isOval) {
176             var cx = +a.cx,
177                 cy = +a.cy,
178                 rx = +a.rx || +a.r || 0,
179                 ry = +a.ry || +a.r || 0;
180             node.path = R.format("ar{0},{1},{2},{3},{4},{1},{4},{1}x", round((cx - rx) * zoom), round((cy - ry) * zoom), round((cx + rx) * zoom), round((cy + ry) * zoom), round(cx * zoom));
181         }
182         if ("clip-rect" in params) {
183             var rect = Str(params["clip-rect"]).split(separator);
184             if (rect.length == 4) {
185                 rect[2] = +rect[2] + (+rect[0]);
186                 rect[3] = +rect[3] + (+rect[1]);
187                 var div = node.clipRect || R._g.doc.createElement("div"),
188                     dstyle = div.style,
189                     group = node.parentNode;
190                 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
191                 if (!node.clipRect) {
192                     dstyle.position = "absolute";
193                     dstyle.top = 0;
194                     dstyle.left = 0;
195                     dstyle.width = o.paper.width + "px";
196                     dstyle.height = o.paper.height + "px";
197                     group.parentNode.insertBefore(div, group);
198                     div.appendChild(group);
199                     node.clipRect = div;
200                 }
201             }
202             if (!params["clip-rect"]) {
203                 node.clipRect && (node.clipRect.style.clip = E);
204             }
205         }
206         if (o.textpath) {
207             var textpathStyle = o.textpath.style;
208             params.font && (textpathStyle.font = params.font);
209             params["font-family"] && (textpathStyle.fontFamily = '"' + params["font-family"].split(",")[0].replace(/^['"]+|['"]+$/g, E) + '"');
210             params["font-size"] && (textpathStyle.fontSize = params["font-size"]);
211             params["font-weight"] && (textpathStyle.fontWeight = params["font-weight"]);
212             params["font-style"] && (textpathStyle.fontStyle = params["font-style"]);
213         }
214         if ("arrow-start" in params) {
215             addArrow(res, params["arrow-start"]);
216         }
217         if ("arrow-end" in params) {
218             addArrow(res, params["arrow-end"], 1);
219         }
220         if (params.opacity != null || 
221             params["stroke-width"] != null ||
222             params.fill != null ||
223             params.src != null ||
224             params.stroke != null ||
225             params["stroke-width"] != null ||
226             params["stroke-opacity"] != null ||
227             params["fill-opacity"] != null ||
228             params["stroke-dasharray"] != null ||
229             params["stroke-miterlimit"] != null ||
230             params["stroke-linejoin"] != null ||
231             params["stroke-linecap"] != null) {
232             var fill = node.getElementsByTagName(fillString),
233                 newfill = false;
234             fill = fill && fill[0];
235             !fill && (newfill = fill = createNode(fillString));
236             if (o.type == "image" && params.src) {
237                 fill.src = params.src;
238             }
239             params.fill && (fill.on = true);
240             if (fill.on == null || params.fill == "none" || params.fill === null) {
241                 fill.on = false;
242             }
243             if (fill.on && params.fill) {
244                 var isURL = Str(params.fill).match(R._ISURL);
245                 if (isURL) {
246                     fill.parentNode == node && node.removeChild(fill);
247                     fill.rotate = true;
248                     fill.src = isURL[1];
249                     fill.type = "tile";
250                     var bbox = o.getBBox(1);
251                     fill.position = bbox.x + S + bbox.y;
252                     o._.fillpos = [bbox.x, bbox.y];
253
254                     R._preload(isURL[1], function () {
255                         o._.fillsize = [this.offsetWidth, this.offsetHeight];
256                     });
257                 } else {
258                     fill.color = R.getRGB(params.fill).hex;
259                     fill.src = E;
260                     fill.type = "solid";
261                     if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill, fill)) {
262                         a.fill = "none";
263                         a.gradient = params.fill;
264                         fill.rotate = false;
265                     }
266                 }
267             }
268             if ("fill-opacity" in params || "opacity" in params) {
269                 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
270                 opacity = mmin(mmax(opacity, 0), 1);
271                 fill.opacity = opacity;
272                 if (fill.src) {
273                     fill.color = "none";
274                 }
275             }
276             node.appendChild(fill);
277             var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
278             newstroke = false;
279             !stroke && (newstroke = stroke = createNode("stroke"));
280             if ((params.stroke && params.stroke != "none") ||
281                 params["stroke-width"] ||
282                 params["stroke-opacity"] != null ||
283                 params["stroke-dasharray"] ||
284                 params["stroke-miterlimit"] ||
285                 params["stroke-linejoin"] ||
286                 params["stroke-linecap"]) {
287                 stroke.on = true;
288             }
289             (params.stroke == "none" || params.stroke === null || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
290             var strokeColor = R.getRGB(params.stroke);
291             stroke.on && params.stroke && (stroke.color = strokeColor.hex);
292             opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
293             var width = (toFloat(params["stroke-width"]) || 1) * .75;
294             opacity = mmin(mmax(opacity, 0), 1);
295             params["stroke-width"] == null && (width = a["stroke-width"]);
296             params["stroke-width"] && (stroke.weight = width);
297             width && width < 1 && (opacity *= width) && (stroke.weight = 1);
298             stroke.opacity = opacity;
299         
300             params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
301             stroke.miterlimit = params["stroke-miterlimit"] || 8;
302             params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
303             if (params["stroke-dasharray"]) {
304                 var dasharray = {
305                     "-": "shortdash",
306                     ".": "shortdot",
307                     "-.": "shortdashdot",
308                     "-..": "shortdashdotdot",
309                     ". ": "dot",
310                     "- ": "dash",
311                     "--": "longdash",
312                     "- .": "dashdot",
313                     "--.": "longdashdot",
314                     "--..": "longdashdotdot"
315                 };
316                 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
317             }
318             newstroke && node.appendChild(stroke);
319         }
320         if (res.type == "text") {
321             res.paper.canvas.style.display = E;
322             var span = res.paper.span,
323                 m = 100,
324                 fontSize = a.font && a.font.match(/\d+(?:\.\d*)?(?=px)/);
325             s = span.style;
326             a.font && (s.font = a.font);
327             a["font-family"] && (s.fontFamily = a["font-family"]);
328             a["font-weight"] && (s.fontWeight = a["font-weight"]);
329             a["font-style"] && (s.fontStyle = a["font-style"]);
330             fontSize = toFloat(fontSize ? fontSize[0] : a["font-size"]);
331             s.fontSize = fontSize * m + "px";
332             res.textpath.string && (span.innerHTML = Str(res.textpath.string).replace(/</g, "&#60;").replace(/&/g, "&#38;").replace(/\n/g, "<br>"));
333             var brect = span.getBoundingClientRect();
334             res.W = a.w = (brect.right - brect.left) / m;
335             res.H = a.h = (brect.bottom - brect.top) / m;
336             // res.paper.canvas.style.display = "none";
337             res.X = a.x;
338             res.Y = a.y + res.H / 2;
339
340             ("x" in params || "y" in params) && (res.path.v = R.format("m{0},{1}l{2},{1}", round(a.x * zoom), round(a.y * zoom), round(a.x * zoom) + 1));
341             var dirtyattrs = ["x", "y", "text", "font", "font-family", "font-weight", "font-style", "font-size"];
342             for (var d = 0, dd = dirtyattrs.length; d < dd; d++) if (dirtyattrs[d] in params) {
343                 res._.dirty = 1;
344                 break;
345             }
346         
347             // text-anchor emulation
348             switch (a["text-anchor"]) {
349                 case "start":
350                     res.textpath.style["v-text-align"] = "left";
351                     res.bbx = res.W / 2;
352                 break;
353                 case "end":
354                     res.textpath.style["v-text-align"] = "right";
355                     res.bbx = -res.W / 2;
356                 break;
357                 default:
358                     res.textpath.style["v-text-align"] = "center";
359                     res.bbx = 0;
360                 break;
361             }
362             res.textpath.style["v-text-kern"] = true;
363         }
364         // res.paper.canvas.style.display = E;
365     };
366     addGradientFill = function (o, gradient, fill) {
367         o.attrs = o.attrs || {};
368         var attrs = o.attrs,
369             opacity,
370             oindex,
371             type = "linear",
372             fxfy = ".5 .5";
373         o.attrs.gradient = gradient;
374         gradient = Str(gradient).replace(R._radial_gradient, function (all, fx, fy) {
375             type = "radial";
376             if (fx && fy) {
377                 fx = toFloat(fx);
378                 fy = toFloat(fy);
379                 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
380                 fxfy = fx + S + fy;
381             }
382             return E;
383         });
384         gradient = gradient.split(/\s*\-\s*/);
385         if (type == "linear") {
386             var angle = gradient.shift();
387             angle = -toFloat(angle);
388             if (isNaN(angle)) {
389                 return null;
390             }
391         }
392         var dots = R._parseDots(gradient);
393         if (!dots) {
394             return null;
395         }
396         o = o.shape || o.node;
397         if (dots.length) {
398             o.removeChild(fill);
399             fill.on = true;
400             fill.method = "none";
401             fill.color = dots[0].color;
402             fill.color2 = dots[dots.length - 1].color;
403             var clrs = [];
404             for (var i = 0, ii = dots.length; i < ii; i++) {
405                 dots[i].offset && clrs.push(dots[i].offset + S + dots[i].color);
406             }
407             fill.colors = clrs.length ? clrs.join() : "0% " + fill.color;
408             if (type == "radial") {
409                 fill.type = "gradientTitle";
410                 fill.focus = "100%";
411                 fill.focussize = "0 0";
412                 fill.focusposition = fxfy;
413                 fill.angle = 0;
414             } else {
415                 // fill.rotate= true;
416                 fill.type = "gradient";
417                 fill.angle = (270 - angle) % 360;
418             }
419             o.appendChild(fill);
420         }
421         return 1;
422     };
423     Element = function (node, vml) {
424         this[0] = this.node = node;
425         node.raphael = true;
426         this.id = R._oid++;
427         node.raphaelid = this.id;
428         this.X = 0;
429         this.Y = 0;
430         this.attrs = {};
431         this.paper = vml;
432         this.matrix = R.matrix();
433         this._ = {
434             transform: [],
435             sx: 1,
436             sy: 1,
437             dx: 0,
438             dy: 0,
439             deg: 0,
440             dirty: 1,
441             dirtyT: 1
442         };
443         !vml.bottom && (vml.bottom = this);
444         this.prev = vml.top;
445         vml.top && (vml.top.next = this);
446         vml.top = this;
447         this.next = null;
448     };
449     var elproto = R.el;
450
451     Element.prototype = elproto;
452     elproto.constructor = Element;
453     elproto.transform = function (tstr) {
454         if (tstr == null) {
455             return this._.transform;
456         }
457         var vbs = this.paper._viewBoxShift,
458             vbt = vbs ? "s" + [vbs.scale, vbs.scale] + "-1-1t" + [vbs.dx, vbs.dy] : E,
459             oldt;
460         if (vbs) {
461             oldt = tstr = Str(tstr).replace(/\.{3}|\u2026/g, this._.transform || E);
462         }
463         R._extractTransform(this, vbt + tstr);
464         var matrix = this.matrix.clone(),
465             skew = this.skew,
466             o = this.node,
467             split,
468             isGrad = ~Str(this.attrs.fill).indexOf("-"),
469             isPatt = !Str(this.attrs.fill).indexOf("url(");
470         matrix.translate(-.5, -.5);
471         if (isPatt || isGrad || this.type == "image") {
472             skew.matrix = "1 0 0 1";
473             skew.offset = "0 0";
474             split = matrix.split();
475             if ((isGrad && split.noRotation) || !split.isSimple) {
476                 o.style.filter = matrix.toFilter();
477                 var bb = this.getBBox(),
478                     bbt = this.getBBox(1),
479                     dx = bb.x - bbt.x,
480                     dy = bb.y - bbt.y;
481                 o.coordorigin = (dx * -zoom) + S + (dy * -zoom);
482                 setCoords(this, 1, 1, dx, dy, 0);
483             } else {
484                 o.style.filter = E;
485                 setCoords(this, split.scalex, split.scaley, split.dx, split.dy, split.rotate);
486             }
487         } else {
488             o.style.filter = E;
489             skew.matrix = Str(matrix);
490             skew.offset = matrix.offset();
491         }
492         oldt && (this._.transform = oldt);
493         return this;
494     };
495     elproto.rotate = function (deg, cx, cy) {
496         if (this.removed) {
497             return this;
498         }
499         if (deg == null) {
500             return;
501         }
502         deg = Str(deg).split(separator);
503         if (deg.length - 1) {
504             cx = toFloat(deg[1]);
505             cy = toFloat(deg[2]);
506         }
507         deg = toFloat(deg[0]);
508         (cy == null) && (cx = cy);
509         if (cx == null || cy == null) {
510             var bbox = this.getBBox(1);
511             cx = bbox.x + bbox.width / 2;
512             cy = bbox.y + bbox.height / 2;
513         }
514         this._.dirtyT = 1;
515         this.transform(this._.transform.concat([["r", deg, cx, cy]]));
516         return this;
517     };
518     elproto.translate = function (dx, dy) {
519         if (this.removed) {
520             return this;
521         }
522         dx = Str(dx).split(separator);
523         if (dx.length - 1) {
524             dy = toFloat(dx[1]);
525         }
526         dx = toFloat(dx[0]) || 0;
527         dy = +dy || 0;
528         if (this._.bbox) {
529             this._.bbox.x += dx;
530             this._.bbox.y += dy;
531         }
532         this.transform(this._.transform.concat([["t", dx, dy]]));
533         return this;
534     };
535     elproto.scale = function (sx, sy, cx, cy) {
536         if (this.removed) {
537             return this;
538         }
539         sx = Str(sx).split(separator);
540         if (sx.length - 1) {
541             sy = toFloat(sx[1]);
542             cx = toFloat(sx[2]);
543             cy = toFloat(sx[3]);
544             isNaN(cx) && (cx = null);
545             isNaN(cy) && (cy = null);
546         }
547         sx = toFloat(sx[0]);
548         (sy == null) && (sy = sx);
549         (cy == null) && (cx = cy);
550         if (cx == null || cy == null) {
551             var bbox = this.getBBox(1);
552         }
553         cx = cx == null ? bbox.x + bbox.width / 2 : cx;
554         cy = cy == null ? bbox.y + bbox.height / 2 : cy;
555     
556         this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
557         this._.dirtyT = 1;
558         return this;
559     };
560     elproto.hide = function () {
561         !this.removed && (this.node.style.display = "none");
562         return this;
563     };
564     elproto.show = function () {
565         !this.removed && (this.node.style.display = E);
566         return this;
567     };
568     elproto._getBBox = function () {
569         if (this.removed) {
570             return {};
571         }
572         if (this.type == "text") {
573             return {
574                 x: this.X + (this.bbx || 0) - this.W / 2,
575                 y: this.Y - this.H,
576                 width: this.W,
577                 height: this.H
578             };
579         } else {
580             return pathDimensions(this.attrs.path);
581         }
582     };
583     elproto.remove = function () {
584         if (this.removed) {
585             return;
586         }
587         R.eve.unbind("*.*." + this.id);
588         R._tear(this, this.paper);
589         this.node.parentNode.removeChild(this.node);
590         this.shape && this.shape.parentNode.removeChild(this.shape);
591         for (var i in this) {
592             delete this[i];
593         }
594         this.removed = true;
595     };
596     elproto.attr = function (name, value) {
597         if (this.removed) {
598             return this;
599         }
600         if (name == null) {
601             var res = {};
602             for (var a in this.attrs) if (this.attrs[has](a)) {
603                 res[a] = this.attrs[a];
604             }
605             res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
606             res.transform = this._.transform;
607             return res;
608         }
609         if (value == null && R.is(name, "string")) {
610             if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
611                 return this.attrs.gradient;
612             }
613             var names = name.split(separator),
614                 out = {};
615             for (var i = 0, ii = names.length; i < ii; i++) {
616                 name = names[i];
617                 if (name in this.attrs) {
618                     out[name] = this.attrs[name];
619                 } else if (R.is(this.paper.customAttributes[name], "function")) {
620                     out[name] = this.paper.customAttributes[name].def;
621                 } else {
622                     out[name] = R._availableAttrs[name];
623                 }
624             }
625             return ii - 1 ? out : out[names[0]];
626         }
627         if (this.attrs && value == null && R.is(name, "array")) {
628             out = {};
629             for (i = 0, ii = name.length; i < ii; i++) {
630                 out[name[i]] = this.attr(name[i]);
631             }
632             return out;
633         }
634         var params;
635         if (value != null) {
636             params = {};
637             params[name] = value;
638         }
639         value == null && R.is(name, "object") && (params = name);
640         for (var key in params) {
641             R.eve("attr." + key + "." + this.id, this, params[key]);
642         }
643         if (params) {
644             for (key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
645                 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
646                 this.attrs[key] = params[key];
647                 for (var subkey in par) if (par[has](subkey)) {
648                     params[subkey] = par[subkey];
649                 }
650             }
651             // this.paper.canvas.style.display = "none";
652             if (params.text && this.type == "text") {
653                 this.textpath.string = params.text;
654             }
655             setFillAndStroke(this, params);
656             // this.paper.canvas.style.display = E;
657         }
658         return this;
659     };
660     elproto.toFront = function () {
661         !this.removed && this.node.parentNode.appendChild(this.node);
662         this.paper && this.paper.top != this && R._tofront(this, this.paper);
663         return this;
664     };
665     elproto.toBack = function () {
666         if (this.removed) {
667             return this;
668         }
669         if (this.node.parentNode.firstChild != this.node) {
670             this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
671             R._toback(this, this.paper);
672         }
673         return this;
674     };
675     elproto.insertAfter = function (element) {
676         if (this.removed) {
677             return this;
678         }
679         if (element.constructor == R.st.constructor) {
680             element = element[element.length - 1];
681         }
682         if (element.node.nextSibling) {
683             element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
684         } else {
685             element.node.parentNode.appendChild(this.node);
686         }
687         R._insertafter(this, element, this.paper);
688         return this;
689     };
690     elproto.insertBefore = function (element) {
691         if (this.removed) {
692             return this;
693         }
694         if (element.constructor == R.st.constructor) {
695             element = element[0];
696         }
697         element.node.parentNode.insertBefore(this.node, element.node);
698         R._insertbefore(this, element, this.paper);
699         return this;
700     };
701     elproto.blur = function (size) {
702         var s = this.node.runtimeStyle,
703             f = s.filter;
704         f = f.replace(blurregexp, E);
705         if (+size !== 0) {
706             this.attrs.blur = size;
707             s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
708             s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
709         } else {
710             s.filter = f;
711             s.margin = 0;
712             delete this.attrs.blur;
713         }
714     };
715
716     R._engine.path = function (pathString, vml) {
717         var el = createNode("shape");
718         el.style.cssText = cssDot;
719         el.coordsize = zoom + S + zoom;
720         el.coordorigin = vml.coordorigin;
721         var p = new Element(el, vml),
722             attr = {fill: "none", stroke: "#000"};
723         pathString && (attr.path = pathString);
724         p.type = "path";
725         p.path = [];
726         p.Path = E;
727         setFillAndStroke(p, attr);
728         vml.canvas.appendChild(el);
729         var skew = createNode("skew");
730         skew.on = true;
731         el.appendChild(skew);
732         p.skew = skew;
733         p.transform(E);
734         return p;
735     };
736     R._engine.rect = function (vml, x, y, w, h, r) {
737         var path = R._rectPath(x, y, w, h, r),
738             res = vml.path(path),
739             a = res.attrs;
740         res.X = a.x = x;
741         res.Y = a.y = y;
742         res.W = a.width = w;
743         res.H = a.height = h;
744         a.r = r;
745         a.path = path;
746         res.type = "rect";
747         return res;
748     };
749     R._engine.ellipse = function (vml, x, y, rx, ry) {
750         var res = vml.path(),
751             a = res.attrs;
752         res.X = x - rx;
753         res.Y = y - ry;
754         res.W = rx * 2;
755         res.H = ry * 2;
756         res.type = "ellipse";
757         setFillAndStroke(res, {
758             cx: x,
759             cy: y,
760             rx: rx,
761             ry: ry
762         });
763         return res;
764     };
765     R._engine.circle = function (vml, x, y, r) {
766         var res = vml.path(),
767             a = res.attrs;
768         res.X = x - r;
769         res.Y = y - r;
770         res.W = res.H = r * 2;
771         res.type = "circle";
772         setFillAndStroke(res, {
773             cx: x,
774             cy: y,
775             r: r
776         });
777         return res;
778     };
779     R._engine.image = function (vml, src, x, y, w, h) {
780         var path = R._rectPath(x, y, w, h),
781             res = vml.path(path).attr({stroke: "none"}),
782             a = res.attrs,
783             node = res.node,
784             fill = node.getElementsByTagName(fillString)[0];
785         a.src = src;
786         res.X = a.x = x;
787         res.Y = a.y = y;
788         res.W = a.width = w;
789         res.H = a.height = h;
790         a.path = path;
791         res.type = "image";
792         fill.parentNode == node && node.removeChild(fill);
793         fill.rotate = true;
794         fill.src = src;
795         fill.type = "tile";
796         res._.fillpos = [x, y];
797         res._.fillsize = [w, h];
798         node.appendChild(fill);
799         setCoords(res, 1, 1, 0, 0, 0);
800         return res;
801     };
802     R._engine.text = function (vml, x, y, text) {
803         var el = createNode("shape"),
804             path = createNode("path"),
805             o = createNode("textpath");
806         x = x || 0;
807         y = y || 0;
808         text = text || "";
809         path.v = R.format("m{0},{1}l{2},{1}", round(x * zoom), round(y * zoom), round(x * zoom) + 1);
810         path.textpathok = true;
811         o.string = Str(text);
812         o.on = true;
813         el.style.cssText = "position:absolute;left:0;top:0;width:1px;height:1px";
814         el.coordsize = zoom + S + zoom;
815         el.coordorigin = "0 0";
816         var p = new Element(el, vml),
817             attr = {
818                 fill: "#000",
819                 stroke: "none",
820                 font: R._availableAttrs.font,
821                 text: text
822             };
823         p.shape = el;
824         p.path = path;
825         p.textpath = o;
826         p.type = "text";
827         p.attrs.text = Str(text);
828         p.attrs.x = x;
829         p.attrs.y = y;
830         p.attrs.w = 1;
831         p.attrs.h = 1;
832         setFillAndStroke(p, attr);
833         el.appendChild(o);
834         el.appendChild(path);
835         vml.canvas.appendChild(el);
836         var skew = createNode("skew");
837         skew.on = true;
838         el.appendChild(skew);
839         p.skew = skew;
840         p.transform(E);
841         return p;
842     };
843     R._engine.setSize = function (width, height) {
844         var cs = this.canvas.style;
845         this.width = width;
846         this.height = height;
847         width == +width && (width += "px");
848         height == +height && (height += "px");
849         cs.width = width;
850         cs.height = height;
851         cs.clip = "rect(0 " + width + " " + height + " 0)";
852         if (this._viewBox) {
853             setViewBox.apply(this, this._viewBox);
854         }
855         return this;
856     };
857     R._engine.setViewBox = function (x, y, w, h, fit) {
858         R.eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
859         var width = this.width,
860             height = this.height,
861             size = 1 / mmax(w / width, h / height),
862             H, W;
863         if (fit) {
864             H = height / h;
865             W = width / w;
866             if (w * H < width) {
867                 x -= (width - w * H) / 2 / H;
868             }
869             if (h * W < height) {
870                 y -= (height - h * W) / 2 / W;
871             }
872         }
873         this._viewBox = [x, y, w, h, !!fit];
874         this._viewBoxShift = {
875             dx: -x,
876             dy: -y,
877             scale: size
878         };
879         this.forEach(function (el) {
880             el.transform("...");
881         });
882         return this;
883     };
884     var createNode,
885         initWin = function (win) {
886             var doc = win.document;
887             doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
888             try {
889                 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
890                 createNode = function (tagName) {
891                     return doc.createElement('<rvml:' + tagName + ' class="rvml">');
892                 };
893             } catch (e) {
894                 createNode = function (tagName) {
895                     return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
896                 };
897             }
898         };
899     initWin(R._g.win);
900     R._engine.create = function () {
901         var con = R._getContainer.apply(0, arguments),
902             container = con.container,
903             height = con.height,
904             s,
905             width = con.width,
906             x = con.x,
907             y = con.y;
908         if (!container) {
909             throw new Error("VML container not found.");
910         }
911         var res = new R._Paper,
912             c = res.canvas = R._g.doc.createElement("div"),
913             cs = c.style;
914         x = x || 0;
915         y = y || 0;
916         width = width || 512;
917         height = height || 342;
918         res.width = width;
919         res.height = height;
920         width == +width && (width += "px");
921         height == +height && (height += "px");
922         res.coordsize = zoom * 1e3 + S + zoom * 1e3;
923         res.coordorigin = "0 0";
924         res.span = R._g.doc.createElement("span");
925         res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;";
926         c.appendChild(res.span);
927         cs.cssText = R.format("top:0;left:0;width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
928         if (container == 1) {
929             R._g.doc.body.appendChild(c);
930             cs.left = x + "px";
931             cs.top = y + "px";
932             cs.position = "absolute";
933         } else {
934             if (container.firstChild) {
935                 container.insertBefore(c, container.firstChild);
936             } else {
937                 container.appendChild(c);
938             }
939         }
940         // plugins.call(res, res, R.fn);
941         res.renderfix = function () {};
942         return res;
943     };
944     R.prototype.clear = function () {
945         R.eve("clear", this);
946         this.canvas.innerHTML = E;
947         this.span = R._g.doc.createElement("span");
948         this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
949         this.canvas.appendChild(this.span);
950         this.bottom = this.top = null;
951     };
952     R.prototype.remove = function () {
953         R.eve("remove", this);
954         this.canvas.parentNode.removeChild(this.canvas);
955         for (var i in this) {
956             this[i] = removed(i);
957         }
958         return true;
959     };
960 }(window.Raphael);