Upgrade Eve, Fixed sets in VML and clone() 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         ms = " progid:DXImageTransform.Microsoft",
23         S = " ",
24         E = "",
25         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, image: 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         if (params.path && o.type == "path" || newpath) {
172             node.path = path2vml(~Str(a.path).toLowerCase().indexOf("r") ? R._pathToAbsolute(a.path) : a.path);
173             if (o.type == "image") {
174                 o._.fillpos = [a.x, a.y];
175                 o._.fillsize = [a.width, a.height];
176                 setCoords(o, 1, 1, 0, 0, 0);
177             }
178         }
179         "transform" in params && o.transform(params.transform);
180         if (isOval) {
181             var cx = +a.cx,
182                 cy = +a.cy,
183                 rx = +a.rx || +a.r || 0,
184                 ry = +a.ry || +a.r || 0;
185             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));
186         }
187         if ("clip-rect" in params) {
188             var rect = Str(params["clip-rect"]).split(separator);
189             if (rect.length == 4) {
190                 rect[2] = +rect[2] + (+rect[0]);
191                 rect[3] = +rect[3] + (+rect[1]);
192                 var div = node.clipRect || R._g.doc.createElement("div"),
193                     dstyle = div.style;
194                 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
195                 if (!node.clipRect) {
196                     dstyle.position = "absolute";
197                     dstyle.top = 0;
198                     dstyle.left = 0;
199                     dstyle.width = o.paper.width + "px";
200                     dstyle.height = o.paper.height + "px";
201                     node.parentNode.insertBefore(div, node);
202                     div.appendChild(node);
203                     node.clipRect = div;
204                 }
205             }
206             if (!params["clip-rect"]) {
207                 node.clipRect && (node.clipRect.style.clip = E);
208             }
209         }
210         if (o.textpath) {
211             var textpathStyle = o.textpath.style;
212             params.font && (textpathStyle.font = params.font);
213             params["font-family"] && (textpathStyle.fontFamily = '"' + params["font-family"].split(",")[0].replace(/^['"]+|['"]+$/g, E) + '"');
214             params["font-size"] && (textpathStyle.fontSize = params["font-size"]);
215             params["font-weight"] && (textpathStyle.fontWeight = params["font-weight"]);
216             params["font-style"] && (textpathStyle.fontStyle = params["font-style"]);
217         }
218         if ("arrow-start" in params) {
219             addArrow(res, params["arrow-start"]);
220         }
221         if ("arrow-end" in params) {
222             addArrow(res, params["arrow-end"], 1);
223         }
224         if (params.opacity != null || 
225             params["stroke-width"] != null ||
226             params.fill != null ||
227             params.src != null ||
228             params.stroke != null ||
229             params["stroke-width"] != null ||
230             params["stroke-opacity"] != null ||
231             params["fill-opacity"] != null ||
232             params["stroke-dasharray"] != null ||
233             params["stroke-miterlimit"] != null ||
234             params["stroke-linejoin"] != null ||
235             params["stroke-linecap"] != null) {
236             var fill = node.getElementsByTagName(fillString),
237                 newfill = false;
238             fill = fill && fill[0];
239             !fill && (newfill = fill = createNode(fillString));
240             if (o.type == "image" && params.src) {
241                 fill.src = params.src;
242             }
243             params.fill && (fill.on = true);
244             if (fill.on == null || params.fill == "none" || params.fill === null) {
245                 fill.on = false;
246             }
247             if (fill.on && params.fill) {
248                 var isURL = Str(params.fill).match(R._ISURL);
249                 if (isURL) {
250                     fill.parentNode == node && node.removeChild(fill);
251                     fill.rotate = true;
252                     fill.src = isURL[1];
253                     fill.type = "tile";
254                     var bbox = o.getBBox(1);
255                     fill.position = bbox.x + S + bbox.y;
256                     o._.fillpos = [bbox.x, bbox.y];
257
258                     R._preload(isURL[1], function () {
259                         o._.fillsize = [this.offsetWidth, this.offsetHeight];
260                     });
261                 } else {
262                     fill.color = R.getRGB(params.fill).hex;
263                     fill.src = E;
264                     fill.type = "solid";
265                     if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill, fill)) {
266                         a.fill = "none";
267                         a.gradient = params.fill;
268                         fill.rotate = false;
269                     }
270                 }
271             }
272             if ("fill-opacity" in params || "opacity" in params) {
273                 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
274                 opacity = mmin(mmax(opacity, 0), 1);
275                 fill.opacity = opacity;
276                 if (fill.src) {
277                     fill.color = "none";
278                 }
279             }
280             node.appendChild(fill);
281             var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
282             newstroke = false;
283             !stroke && (newstroke = stroke = createNode("stroke"));
284             if ((params.stroke && params.stroke != "none") ||
285                 params["stroke-width"] ||
286                 params["stroke-opacity"] != null ||
287                 params["stroke-dasharray"] ||
288                 params["stroke-miterlimit"] ||
289                 params["stroke-linejoin"] ||
290                 params["stroke-linecap"]) {
291                 stroke.on = true;
292             }
293             (params.stroke == "none" || params.stroke === null || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
294             var strokeColor = R.getRGB(params.stroke);
295             stroke.on && params.stroke && (stroke.color = strokeColor.hex);
296             opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
297             var width = (toFloat(params["stroke-width"]) || 1) * .75;
298             opacity = mmin(mmax(opacity, 0), 1);
299             params["stroke-width"] == null && (width = a["stroke-width"]);
300             params["stroke-width"] && (stroke.weight = width);
301             width && width < 1 && (opacity *= width) && (stroke.weight = 1);
302             stroke.opacity = opacity;
303         
304             params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
305             stroke.miterlimit = params["stroke-miterlimit"] || 8;
306             params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
307             if (params["stroke-dasharray"]) {
308                 var dasharray = {
309                     "-": "shortdash",
310                     ".": "shortdot",
311                     "-.": "shortdashdot",
312                     "-..": "shortdashdotdot",
313                     ". ": "dot",
314                     "- ": "dash",
315                     "--": "longdash",
316                     "- .": "dashdot",
317                     "--.": "longdashdot",
318                     "--..": "longdashdotdot"
319                 };
320                 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
321             }
322             newstroke && node.appendChild(stroke);
323         }
324         if (res.type == "text") {
325             res.paper.canvas.style.display = E;
326             var span = res.paper.span,
327                 m = 100,
328                 fontSize = a.font && a.font.match(/\d+(?:\.\d*)?(?=px)/);
329             s = span.style;
330             a.font && (s.font = a.font);
331             a["font-family"] && (s.fontFamily = a["font-family"]);
332             a["font-weight"] && (s.fontWeight = a["font-weight"]);
333             a["font-style"] && (s.fontStyle = a["font-style"]);
334             fontSize = toFloat(fontSize ? fontSize[0] : a["font-size"]);
335             s.fontSize = fontSize * m + "px";
336             res.textpath.string && (span.innerHTML = Str(res.textpath.string).replace(/</g, "&#60;").replace(/&/g, "&#38;").replace(/\n/g, "<br>"));
337             var brect = span.getBoundingClientRect();
338             res.W = a.w = (brect.right - brect.left) / m;
339             res.H = a.h = (brect.bottom - brect.top) / m;
340             // res.paper.canvas.style.display = "none";
341             res.X = a.x;
342             res.Y = a.y + res.H / 2;
343
344             ("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));
345             var dirtyattrs = ["x", "y", "text", "font", "font-family", "font-weight", "font-style", "font-size"];
346             for (var d = 0, dd = dirtyattrs.length; d < dd; d++) if (dirtyattrs[d] in params) {
347                 res._.dirty = 1;
348                 break;
349             }
350         
351             // text-anchor emulation
352             switch (a["text-anchor"]) {
353                 case "start":
354                     res.textpath.style["v-text-align"] = "left";
355                     res.bbx = res.W / 2;
356                 break;
357                 case "end":
358                     res.textpath.style["v-text-align"] = "right";
359                     res.bbx = -res.W / 2;
360                 break;
361                 default:
362                     res.textpath.style["v-text-align"] = "center";
363                     res.bbx = 0;
364                 break;
365             }
366             res.textpath.style["v-text-kern"] = true;
367         }
368         // res.paper.canvas.style.display = E;
369     };
370     addGradientFill = function (o, gradient, fill) {
371         o.attrs = o.attrs || {};
372         var attrs = o.attrs,
373             opacity,
374             oindex,
375             type = "linear",
376             fxfy = ".5 .5";
377         o.attrs.gradient = gradient;
378         gradient = Str(gradient).replace(R._radial_gradient, function (all, fx, fy) {
379             type = "radial";
380             if (fx && fy) {
381                 fx = toFloat(fx);
382                 fy = toFloat(fy);
383                 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
384                 fxfy = fx + S + fy;
385             }
386             return E;
387         });
388         gradient = gradient.split(/\s*\-\s*/);
389         if (type == "linear") {
390             var angle = gradient.shift();
391             angle = -toFloat(angle);
392             if (isNaN(angle)) {
393                 return null;
394             }
395         }
396         var dots = R._parseDots(gradient);
397         if (!dots) {
398             return null;
399         }
400         o = o.shape || o.node;
401         if (dots.length) {
402             o.removeChild(fill);
403             fill.on = true;
404             fill.method = "none";
405             fill.color = dots[0].color;
406             fill.color2 = dots[dots.length - 1].color;
407             var clrs = [];
408             for (var i = 0, ii = dots.length; i < ii; i++) {
409                 dots[i].offset && clrs.push(dots[i].offset + S + dots[i].color);
410             }
411             fill.colors = clrs.length ? clrs.join() : "0% " + fill.color;
412             if (type == "radial") {
413                 fill.type = "gradientTitle";
414                 fill.focus = "100%";
415                 fill.focussize = "0 0";
416                 fill.focusposition = fxfy;
417                 fill.angle = 0;
418             } else {
419                 // fill.rotate= true;
420                 fill.type = "gradient";
421                 fill.angle = (270 - angle) % 360;
422             }
423             o.appendChild(fill);
424         }
425         return 1;
426     };
427     Element = function (node, vml) {
428         this[0] = this.node = node;
429         node.raphael = true;
430         this.id = R._oid++;
431         node.raphaelid = this.id;
432         this.X = 0;
433         this.Y = 0;
434         this.attrs = {};
435         this.paper = vml;
436         this.matrix = R.matrix();
437         this._ = {
438             transform: [],
439             sx: 1,
440             sy: 1,
441             dx: 0,
442             dy: 0,
443             deg: 0,
444             dirty: 1,
445             dirtyT: 1
446         };
447         !vml.bottom && (vml.bottom = this);
448         this.prev = vml.top;
449         vml.top && (vml.top.next = this);
450         vml.top = this;
451         this.next = null;
452     };
453     var elproto = R.el;
454
455     Element.prototype = elproto;
456     elproto.constructor = Element;
457     elproto.transform = function (tstr) {
458         if (tstr == null) {
459             return this._.transform;
460         }
461         var vbs = this.paper._viewBoxShift,
462             vbt = vbs ? "s" + [vbs.scale, vbs.scale] + "-1-1t" + [vbs.dx, vbs.dy] : E,
463             oldt;
464         if (vbs) {
465             oldt = tstr = Str(tstr).replace(/\.{3}|\u2026/g, this._.transform || E);
466         }
467         R._extractTransform(this, vbt + tstr);
468         var matrix = this.matrix.clone(),
469             skew = this.skew,
470             o = this.node,
471             split,
472             isGrad = ~Str(this.attrs.fill).indexOf("-"),
473             isPatt = !Str(this.attrs.fill).indexOf("url(");
474         matrix.translate(-.5, -.5);
475         if (isPatt || isGrad || this.type == "image") {
476             skew.matrix = "1 0 0 1";
477             skew.offset = "0 0";
478             split = matrix.split();
479             if ((isGrad && split.noRotation) || !split.isSimple) {
480                 o.style.filter = matrix.toFilter();
481                 var bb = this.getBBox(),
482                     bbt = this.getBBox(1),
483                     dx = bb.x - bbt.x,
484                     dy = bb.y - bbt.y;
485                 o.coordorigin = (dx * -zoom) + S + (dy * -zoom);
486                 setCoords(this, 1, 1, dx, dy, 0);
487             } else {
488                 o.style.filter = E;
489                 setCoords(this, split.scalex, split.scaley, split.dx, split.dy, split.rotate);
490             }
491         } else {
492             o.style.filter = E;
493             skew.matrix = Str(matrix);
494             skew.offset = matrix.offset();
495         }
496         oldt && (this._.transform = oldt);
497         return this;
498     };
499     elproto.rotate = function (deg, cx, cy) {
500         if (this.removed) {
501             return this;
502         }
503         if (deg == null) {
504             return;
505         }
506         deg = Str(deg).split(separator);
507         if (deg.length - 1) {
508             cx = toFloat(deg[1]);
509             cy = toFloat(deg[2]);
510         }
511         deg = toFloat(deg[0]);
512         (cy == null) && (cx = cy);
513         if (cx == null || cy == null) {
514             var bbox = this.getBBox(1);
515             cx = bbox.x + bbox.width / 2;
516             cy = bbox.y + bbox.height / 2;
517         }
518         this._.dirtyT = 1;
519         this.transform(this._.transform.concat([["r", deg, cx, cy]]));
520         return this;
521     };
522     elproto.translate = function (dx, dy) {
523         if (this.removed) {
524             return this;
525         }
526         dx = Str(dx).split(separator);
527         if (dx.length - 1) {
528             dy = toFloat(dx[1]);
529         }
530         dx = toFloat(dx[0]) || 0;
531         dy = +dy || 0;
532         if (this._.bbox) {
533             this._.bbox.x += dx;
534             this._.bbox.y += dy;
535         }
536         this.transform(this._.transform.concat([["t", dx, dy]]));
537         return this;
538     };
539     elproto.scale = function (sx, sy, cx, cy) {
540         if (this.removed) {
541             return this;
542         }
543         sx = Str(sx).split(separator);
544         if (sx.length - 1) {
545             sy = toFloat(sx[1]);
546             cx = toFloat(sx[2]);
547             cy = toFloat(sx[3]);
548             isNaN(cx) && (cx = null);
549             isNaN(cy) && (cy = null);
550         }
551         sx = toFloat(sx[0]);
552         (sy == null) && (sy = sx);
553         (cy == null) && (cx = cy);
554         if (cx == null || cy == null) {
555             var bbox = this.getBBox(1);
556         }
557         cx = cx == null ? bbox.x + bbox.width / 2 : cx;
558         cy = cy == null ? bbox.y + bbox.height / 2 : cy;
559     
560         this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
561         this._.dirtyT = 1;
562         return this;
563     };
564     elproto.hide = function () {
565         !this.removed && (this.node.style.display = "none");
566         return this;
567     };
568     elproto.show = function () {
569         !this.removed && (this.node.style.display = E);
570         return this;
571     };
572     elproto._getBBox = function () {
573         if (this.removed) {
574             return {};
575         }
576         if (this.type == "text") {
577             return {
578                 x: this.X + (this.bbx || 0) - this.W / 2,
579                 y: this.Y - this.H,
580                 width: this.W,
581                 height: this.H
582             };
583         } else {
584             return pathDimensions(this.attrs.path);
585         }
586     };
587     elproto.remove = function () {
588         if (this.removed) {
589             return;
590         }
591         R.eve.unbind("*.*." + this.id);
592         R._tear(this, this.paper);
593         this.node.parentNode.removeChild(this.node);
594         this.shape && this.shape.parentNode.removeChild(this.shape);
595         for (var i in this) {
596             delete this[i];
597         }
598         this.removed = true;
599     };
600     elproto.attr = function (name, value) {
601         if (this.removed) {
602             return this;
603         }
604         if (name == null) {
605             var res = {};
606             for (var a in this.attrs) if (this.attrs[has](a)) {
607                 res[a] = this.attrs[a];
608             }
609             res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
610             res.transform = this._.transform;
611             return res;
612         }
613         if (value == null && R.is(name, "string")) {
614             if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
615                 return this.attrs.gradient;
616             }
617             var names = name.split(separator),
618                 out = {};
619             for (var i = 0, ii = names.length; i < ii; i++) {
620                 name = names[i];
621                 if (name in this.attrs) {
622                     out[name] = this.attrs[name];
623                 } else if (R.is(this.paper.customAttributes[name], "function")) {
624                     out[name] = this.paper.customAttributes[name].def;
625                 } else {
626                     out[name] = R._availableAttrs[name];
627                 }
628             }
629             return ii - 1 ? out : out[names[0]];
630         }
631         if (this.attrs && value == null && R.is(name, "array")) {
632             out = {};
633             for (i = 0, ii = name.length; i < ii; i++) {
634                 out[name[i]] = this.attr(name[i]);
635             }
636             return out;
637         }
638         var params;
639         if (value != null) {
640             params = {};
641             params[name] = value;
642         }
643         value == null && R.is(name, "object") && (params = name);
644         for (var key in params) {
645             R.eve("attr." + key + "." + this.id, this, params[key]);
646         }
647         if (params) {
648             for (key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
649                 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
650                 this.attrs[key] = params[key];
651                 for (var subkey in par) if (par[has](subkey)) {
652                     params[subkey] = par[subkey];
653                 }
654             }
655             // this.paper.canvas.style.display = "none";
656             if (params.text && this.type == "text") {
657                 this.textpath.string = params.text;
658             }
659             setFillAndStroke(this, params);
660             // this.paper.canvas.style.display = E;
661         }
662         return this;
663     };
664     elproto.toFront = function () {
665         !this.removed && this.node.parentNode.appendChild(this.node);
666         this.paper && this.paper.top != this && R._tofront(this, this.paper);
667         return this;
668     };
669     elproto.toBack = function () {
670         if (this.removed) {
671             return this;
672         }
673         if (this.node.parentNode.firstChild != this.node) {
674             this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
675             R._toback(this, this.paper);
676         }
677         return this;
678     };
679     elproto.insertAfter = function (element) {
680         if (this.removed) {
681             return this;
682         }
683         if (element.constructor == R.st.constructor) {
684             element = element[element.length - 1];
685         }
686         if (element.node.nextSibling) {
687             element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
688         } else {
689             element.node.parentNode.appendChild(this.node);
690         }
691         R._insertafter(this, element, this.paper);
692         return this;
693     };
694     elproto.insertBefore = function (element) {
695         if (this.removed) {
696             return this;
697         }
698         if (element.constructor == R.st.constructor) {
699             element = element[0];
700         }
701         element.node.parentNode.insertBefore(this.node, element.node);
702         R._insertbefore(this, element, this.paper);
703         return this;
704     };
705     elproto.blur = function (size) {
706         var s = this.node.runtimeStyle,
707             f = s.filter;
708         f = f.replace(blurregexp, E);
709         if (+size !== 0) {
710             this.attrs.blur = size;
711             s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
712             s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
713         } else {
714             s.filter = f;
715             s.margin = 0;
716             delete this.attrs.blur;
717         }
718     };
719
720     R._engine.path = function (pathString, vml) {
721         var el = createNode("shape");
722         el.style.cssText = cssDot;
723         el.coordsize = zoom + S + zoom;
724         el.coordorigin = vml.coordorigin;
725         var p = new Element(el, vml),
726             attr = {fill: "none", stroke: "#000"};
727         pathString && (attr.path = pathString);
728         p.type = "path";
729         p.path = [];
730         p.Path = E;
731         setFillAndStroke(p, attr);
732         vml.canvas.appendChild(el);
733         var skew = createNode("skew");
734         skew.on = true;
735         el.appendChild(skew);
736         p.skew = skew;
737         p.transform(E);
738         return p;
739     };
740     R._engine.rect = function (vml, x, y, w, h, r) {
741         var path = R._rectPath(x, y, w, h, r),
742             res = vml.path(path),
743             a = res.attrs;
744         res.X = a.x = x;
745         res.Y = a.y = y;
746         res.W = a.width = w;
747         res.H = a.height = h;
748         a.r = r;
749         a.path = path;
750         res.type = "rect";
751         return res;
752     };
753     R._engine.ellipse = function (vml, x, y, rx, ry) {
754         var res = vml.path(),
755             a = res.attrs;
756         res.X = x - rx;
757         res.Y = y - ry;
758         res.W = rx * 2;
759         res.H = ry * 2;
760         res.type = "ellipse";
761         setFillAndStroke(res, {
762             cx: x,
763             cy: y,
764             rx: rx,
765             ry: ry
766         });
767         return res;
768     };
769     R._engine.circle = function (vml, x, y, r) {
770         var res = vml.path(),
771             a = res.attrs;
772         res.X = x - r;
773         res.Y = y - r;
774         res.W = res.H = r * 2;
775         res.type = "circle";
776         setFillAndStroke(res, {
777             cx: x,
778             cy: y,
779             r: r
780         });
781         return res;
782     };
783     R._engine.image = function (vml, src, x, y, w, h) {
784         var path = R._rectPath(x, y, w, h),
785             res = vml.path(path).attr({stroke: "none"}),
786             a = res.attrs,
787             node = res.node,
788             fill = node.getElementsByTagName(fillString)[0];
789         a.src = src;
790         res.X = a.x = x;
791         res.Y = a.y = y;
792         res.W = a.width = w;
793         res.H = a.height = h;
794         a.path = path;
795         res.type = "image";
796         fill.parentNode == node && node.removeChild(fill);
797         fill.rotate = true;
798         fill.src = src;
799         fill.type = "tile";
800         res._.fillpos = [x, y];
801         res._.fillsize = [w, h];
802         node.appendChild(fill);
803         setCoords(res, 1, 1, 0, 0, 0);
804         return res;
805     };
806     R._engine.text = function (vml, x, y, text) {
807         var el = createNode("shape"),
808             path = createNode("path"),
809             o = createNode("textpath");
810         x = x || 0;
811         y = y || 0;
812         text = text || "";
813         path.v = R.format("m{0},{1}l{2},{1}", round(x * zoom), round(y * zoom), round(x * zoom) + 1);
814         path.textpathok = true;
815         o.string = Str(text);
816         o.on = true;
817         el.style.cssText = cssDot;
818         el.coordsize = zoom + S + zoom;
819         el.coordorigin = "0 0";
820         var p = new Element(el, vml),
821             attr = {
822                 fill: "#000",
823                 stroke: "none",
824                 font: R._availableAttrs.font,
825                 text: text
826             };
827         p.shape = el;
828         p.path = path;
829         p.textpath = o;
830         p.type = "text";
831         p.attrs.text = Str(text);
832         p.attrs.x = x;
833         p.attrs.y = y;
834         p.attrs.w = 1;
835         p.attrs.h = 1;
836         setFillAndStroke(p, attr);
837         el.appendChild(o);
838         el.appendChild(path);
839         vml.canvas.appendChild(el);
840         var skew = createNode("skew");
841         skew.on = true;
842         el.appendChild(skew);
843         p.skew = skew;
844         p.transform(E);
845         return p;
846     };
847     R._engine.setSize = function (width, height) {
848         var cs = this.canvas.style;
849         this.width = width;
850         this.height = height;
851         width == +width && (width += "px");
852         height == +height && (height += "px");
853         cs.width = width;
854         cs.height = height;
855         cs.clip = "rect(0 " + width + " " + height + " 0)";
856         if (this._viewBox) {
857             setViewBox.apply(this, this._viewBox);
858         }
859         return this;
860     };
861     R._engine.setViewBox = function (x, y, w, h, fit) {
862         R.eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
863         var width = this.width,
864             height = this.height,
865             size = 1 / mmax(w / width, h / height),
866             H, W;
867         if (fit) {
868             H = height / h;
869             W = width / w;
870             if (w * H < width) {
871                 x -= (width - w * H) / 2 / H;
872             }
873             if (h * W < height) {
874                 y -= (height - h * W) / 2 / W;
875             }
876         }
877         this._viewBox = [x, y, w, h, !!fit];
878         this._viewBoxShift = {
879             dx: -x,
880             dy: -y,
881             scale: size
882         };
883         this.forEach(function (el) {
884             el.transform("...");
885         });
886         return this;
887     };
888     var createNode,
889         initWin = function (win) {
890             var doc = win.document;
891             doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
892             try {
893                 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
894                 createNode = function (tagName) {
895                     return doc.createElement('<rvml:' + tagName + ' class="rvml">');
896                 };
897             } catch (e) {
898                 createNode = function (tagName) {
899                     return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
900                 };
901             }
902         };
903     initWin(R._g.win);
904     R._engine.create = function () {
905         var con = R._getContainer.apply(0, arguments),
906             container = con.container,
907             height = con.height,
908             s,
909             width = con.width,
910             x = con.x,
911             y = con.y;
912         if (!container) {
913             throw new Error("VML container not found.");
914         }
915         var res = new R._Paper,
916             c = res.canvas = R._g.doc.createElement("div"),
917             cs = c.style;
918         x = x || 0;
919         y = y || 0;
920         width = width || 512;
921         height = height || 342;
922         res.width = width;
923         res.height = height;
924         width == +width && (width += "px");
925         height == +height && (height += "px");
926         res.coordsize = zoom * 1e3 + S + zoom * 1e3;
927         res.coordorigin = "0 0";
928         res.span = R._g.doc.createElement("span");
929         res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;";
930         c.appendChild(res.span);
931         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);
932         if (container == 1) {
933             R._g.doc.body.appendChild(c);
934             cs.left = x + "px";
935             cs.top = y + "px";
936             cs.position = "absolute";
937         } else {
938             if (container.firstChild) {
939                 container.insertBefore(c, container.firstChild);
940             } else {
941                 container.appendChild(c);
942             }
943         }
944         // plugins.call(res, res, R.fn);
945         res.renderfix = function () {};
946         return res;
947     };
948     R.prototype.clear = function () {
949         R.eve("clear", this);
950         this.canvas.innerHTML = E;
951         this.span = R._g.doc.createElement("span");
952         this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
953         this.canvas.appendChild(this.span);
954         this.bottom = this.top = null;
955     };
956     R.prototype.remove = function () {
957         R.eve("remove", this);
958         this.canvas.parentNode.removeChild(this.canvas);
959         for (var i in this) {
960             this[i] = removed(i);
961         }
962         return true;
963     };
964
965     var setproto = R.st;
966     for (var method in elproto) if (elproto[has](method) && !setproto[has](method)) {
967         setproto[method] = (function (methodname) {
968             return function () {
969                 var arg = arguments;
970                 return this.forEach(function (el) {
971                     el[methodname].apply(el, arg);
972                 });
973             };
974         })(method);
975     }
976 }(window.Raphael);