Bug fixes & docs update
[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         R._extractTransform(this, tstr);
458         var matrix = this.matrix.clone(),
459             vbs = this.paper._viewBoxShift,
460             skew = this.skew,
461             o = this.node,
462             split,
463             isGrad = Str(this.attrs.fill).indexOf("-") + 1;
464         matrix.translate(-.5, -.5);
465         if (vbs) {
466             matrix.scale(vbs.scale, vbs.scale, -1, -1);
467             matrix.translate(vbs.dx, vbs.dy);
468         }
469         if (isGrad || this.type == "image") {
470             skew.matrix = "1 0 0 1";
471             skew.offset = "0 0";
472             split = matrix.split();
473             if ((isGrad && split.noRotation) || !split.isSimple) {
474                 o.style.filter = matrix.toFilter();
475                 var bb = this.getBBox(),
476                     bbt = this.getBBox(1),
477                     dx = bb.x - bbt.x,
478                     dy = bb.y - bbt.y;
479                 o.coordorigin = (dx * -zoom) + S + (dy * -zoom);
480                 setCoords(this, 1, 1, dx, dy, 0);
481             } else {
482                 o.style.filter = E;
483                 setCoords(this, split.scalex, split.scaley, split.dx, split.dy, split.rotate);
484             }
485         } else {
486             o.style.filter = E;
487             skew.matrix = Str(matrix);
488             skew.offset = matrix.offset();
489         }
490         return this;
491     };
492     elproto.rotate = function (deg, cx, cy) {
493         if (this.removed) {
494             return this;
495         }
496         if (deg == null) {
497             return;
498         }
499         deg = Str(deg).split(separator);
500         if (deg.length - 1) {
501             cx = toFloat(deg[1]);
502             cy = toFloat(deg[2]);
503         }
504         deg = toFloat(deg[0]);
505         (cy == null) && (cx = cy);
506         if (cx == null || cy == null) {
507             var bbox = this.getBBox(1);
508             cx = bbox.x + bbox.width / 2;
509             cy = bbox.y + bbox.height / 2;
510         }
511         this._.dirtyT = 1;
512         this.transform(this._.transform.concat([["r", deg, cx, cy]]));
513         return this;
514     };
515     elproto.translate = function (dx, dy) {
516         if (this.removed) {
517             return this;
518         }
519         dx = Str(dx).split(separator);
520         if (dx.length - 1) {
521             dy = toFloat(dx[1]);
522         }
523         dx = toFloat(dx[0]) || 0;
524         dy = +dy || 0;
525         if (this._.bbox) {
526             this._.bbox.x += dx;
527             this._.bbox.y += dy;
528         }
529         this.transform(this._.transform.concat([["t", dx, dy]]));
530         return this;
531     };
532     elproto.scale = function (sx, sy, cx, cy) {
533         if (this.removed) {
534             return this;
535         }
536         sx = Str(sx).split(separator);
537         if (sx.length - 1) {
538             sy = toFloat(sx[1]);
539             cx = toFloat(sx[2]);
540             cy = toFloat(sx[3]);
541             isNaN(cx) && (cx = null);
542             isNaN(cy) && (cy = null);
543         }
544         sx = toFloat(sx[0]);
545         (sy == null) && (sy = sx);
546         (cy == null) && (cx = cy);
547         if (cx == null || cy == null) {
548             var bbox = this.getBBox(1);
549         }
550         cx = cx == null ? bbox.x + bbox.width / 2 : cx;
551         cy = cy == null ? bbox.y + bbox.height / 2 : cy;
552     
553         this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
554         this._.dirtyT = 1;
555         return this;
556     };
557     elproto.hide = function () {
558         !this.removed && (this.node.style.display = "none");
559         return this;
560     };
561     elproto.show = function () {
562         !this.removed && (this.node.style.display = E);
563         return this;
564     };
565     elproto._getBBox = function () {
566         if (this.removed) {
567             return {};
568         }
569         if (this.type == "text") {
570             return {
571                 x: this.X + (this.bbx || 0) - this.W / 2,
572                 y: this.Y - this.H,
573                 width: this.W,
574                 height: this.H
575             };
576         } else {
577             return pathDimensions(this.attrs.path);
578         }
579     };
580     elproto.remove = function () {
581         if (this.removed) {
582             return;
583         }
584         R.eve.unbind("*.*." + this.id);
585         R._tear(this, this.paper);
586         this.node.parentNode.removeChild(this.node);
587         this.shape && this.shape.parentNode.removeChild(this.shape);
588         for (var i in this) {
589             delete this[i];
590         }
591         this.removed = true;
592     };
593     elproto.attr = function (name, value) {
594         if (this.removed) {
595             return this;
596         }
597         if (name == null) {
598             var res = {};
599             for (var a in this.attrs) if (this.attrs[has](a)) {
600                 res[a] = this.attrs[a];
601             }
602             res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
603             res.transform = this._.transform;
604             return res;
605         }
606         if (value == null && R.is(name, "string")) {
607             if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
608                 return this.attrs.gradient;
609             }
610             var names = name.split(separator),
611                 out = {};
612             for (var i = 0, ii = names.length; i < ii; i++) {
613                 name = names[i];
614                 if (name in this.attrs) {
615                     out[name] = this.attrs[name];
616                 } else if (R.is(this.paper.customAttributes[name], "function")) {
617                     out[name] = this.paper.customAttributes[name].def;
618                 } else {
619                     out[name] = R._availableAttrs[name];
620                 }
621             }
622             return ii - 1 ? out : out[names[0]];
623         }
624         if (this.attrs && value == null && R.is(name, "array")) {
625             out = {};
626             for (i = 0, ii = name.length; i < ii; i++) {
627                 out[name[i]] = this.attr(name[i]);
628             }
629             return out;
630         }
631         var params;
632         if (value != null) {
633             params = {};
634             params[name] = value;
635         }
636         value == null && R.is(name, "object") && (params = name);
637         for (var key in params) {
638             R.eve("attr." + key + "." + this.id, this, params[key]);
639         }
640         if (params) {
641             for (key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
642                 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
643                 this.attrs[key] = params[key];
644                 for (var subkey in par) if (par[has](subkey)) {
645                     params[subkey] = par[subkey];
646                 }
647             }
648             // this.paper.canvas.style.display = "none";
649             if (params.text && this.type == "text") {
650                 this.textpath.string = params.text;
651             }
652             setFillAndStroke(this, params);
653             // this.paper.canvas.style.display = E;
654         }
655         return this;
656     };
657     elproto.toFront = function () {
658         !this.removed && this.node.parentNode.appendChild(this.node);
659         this.paper && this.paper.top != this && tofront(this, this.paper);
660         return this;
661     };
662     elproto.toBack = function () {
663         if (this.removed) {
664             return this;
665         }
666         if (this.node.parentNode.firstChild != this.node) {
667             this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
668             toback(this, this.paper);
669         }
670         return this;
671     };
672     elproto.insertAfter = function (element) {
673         if (this.removed) {
674             return this;
675         }
676         if (element.constructor == Set) {
677             element = element[element.length - 1];
678         }
679         if (element.node.nextSibling) {
680             element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
681         } else {
682             element.node.parentNode.appendChild(this.node);
683         }
684         R._insertafter(this, element, this.paper);
685         return this;
686     };
687     elproto.insertBefore = function (element) {
688         if (this.removed) {
689             return this;
690         }
691         if (element.constructor == Set) {
692             element = element[0];
693         }
694         element.node.parentNode.insertBefore(this.node, element.node);
695         R._insertbefore(this, element, this.paper);
696         return this;
697     };
698     elproto.blur = function (size) {
699         var s = this.node.runtimeStyle,
700             f = s.filter;
701         f = f.replace(blurregexp, E);
702         if (+size !== 0) {
703             this.attrs.blur = size;
704             s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
705             s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
706         } else {
707             s.filter = f;
708             s.margin = 0;
709             delete this.attrs.blur;
710         }
711     };
712
713     R._engine.path = function (pathString, vml) {
714         var el = createNode("shape");
715         el.style.cssText = cssDot;
716         el.coordsize = zoom + S + zoom;
717         el.coordorigin = vml.coordorigin;
718         var p = new Element(el, vml),
719             attr = {fill: "none", stroke: "#000"};
720         pathString && (attr.path = pathString);
721         p.type = "path";
722         p.path = [];
723         p.Path = E;
724         setFillAndStroke(p, attr);
725         vml.canvas.appendChild(el);
726         var skew = createNode("skew");
727         skew.on = true;
728         el.appendChild(skew);
729         p.skew = skew;
730         p.transform(E);
731         return p;
732     };
733     R._engine.rect = function (vml, x, y, w, h, r) {
734         var path = R._rectPath(x, y, w, h, r),
735             res = vml.path(path),
736             a = res.attrs;
737         res.X = a.x = x;
738         res.Y = a.y = y;
739         res.W = a.width = w;
740         res.H = a.height = h;
741         a.r = r;
742         a.path = path;
743         res.type = "rect";
744         return res;
745     };
746     R._engine.ellipse = function (vml, x, y, rx, ry) {
747         var res = vml.path(),
748             a = res.attrs;
749         res.X = x - rx;
750         res.Y = y - ry;
751         res.W = rx * 2;
752         res.H = ry * 2;
753         res.type = "ellipse";
754         setFillAndStroke(res, {
755             cx: x,
756             cy: y,
757             rx: rx,
758             ry: ry
759         });
760         return res;
761     };
762     R._engine.circle = function (vml, x, y, r) {
763         var res = vml.path(),
764             a = res.attrs;
765         res.X = x - r;
766         res.Y = y - r;
767         res.W = res.H = r * 2;
768         res.type = "circle";
769         setFillAndStroke(res, {
770             cx: x,
771             cy: y,
772             r: r
773         });
774         return res;
775     };
776     R._engine.image = function (vml, src, x, y, w, h) {
777         var path = R._rectPath(x, y, w, h),
778             res = vml.path(path).attr({stroke: "none"}),
779             a = res.attrs,
780             node = res.node,
781             fill = node.getElementsByTagName(fillString)[0];
782         a.src = src;
783         res.X = a.x = x;
784         res.Y = a.y = y;
785         res.W = a.width = w;
786         res.H = a.height = h;
787         a.path = path;
788         res.type = "image";
789         fill.parentNode == node && node.removeChild(fill);
790         fill.rotate = true;
791         fill.src = src;
792         fill.type = "tile";
793         res._.fillpos = [x, y];
794         res._.fillsize = [w, h];
795         node.appendChild(fill);
796         setCoords(res, 1, 1, 0, 0, 0);
797         return res;
798     };
799     R._engine.text = function (vml, x, y, text) {
800         var el = createNode("shape"),
801             path = createNode("path"),
802             o = createNode("textpath");
803         x = x || 0;
804         y = y || 0;
805         text = text || "";
806         path.v = R.format("m{0},{1}l{2},{1}", round(x * zoom), round(y * zoom), round(x * zoom) + 1);
807         path.textpathok = true;
808         o.string = Str(text);
809         o.on = true;
810         el.style.cssText = "position:absolute;left:0;top:0;width:1px;height:1px";
811         el.coordsize = zoom + S + zoom;
812         el.coordorigin = "0 0";
813         var p = new Element(el, vml),
814             attr = {
815                 fill: "#000",
816                 stroke: "none",
817                 font: R._availableAttrs.font,
818                 text: text
819             };
820         p.shape = el;
821         p.path = path;
822         p.textpath = o;
823         p.type = "text";
824         p.attrs.text = Str(text);
825         p.attrs.x = x;
826         p.attrs.y = y;
827         p.attrs.w = 1;
828         p.attrs.h = 1;
829         setFillAndStroke(p, attr);
830         el.appendChild(o);
831         el.appendChild(path);
832         vml.canvas.appendChild(el);
833         var skew = createNode("skew");
834         skew.on = true;
835         el.appendChild(skew);
836         p.skew = skew;
837         p.transform(E);
838         return p;
839     };
840     R._engine.setSize = function (width, height) {
841         var cs = this.canvas.style;
842         this.width = width;
843         this.height = height;
844         width == +width && (width += "px");
845         height == +height && (height += "px");
846         cs.width = width;
847         cs.height = height;
848         cs.clip = "rect(0 " + width + " " + height + " 0)";
849         if (this._viewBox) {
850             setViewBox.apply(this, this._viewBox);
851         }
852         return this;
853     };
854     R._engine.setViewBox = function (x, y, w, h, fit) {
855         R.eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
856         var width = this.width,
857             height = this.height,
858             size = 1 / mmax(w / width, h / height),
859             H, W;
860         if (fit) {
861             H = height / h;
862             W = width / w;
863             if (w * H < width) {
864                 x -= (width - w * H) / 2 / H;
865             }
866             if (h * W < height) {
867                 y -= (height - h * W) / 2 / W;
868             }
869         }
870         this._viewBox = [x, y, w, h, !!fit];
871         this._viewBoxShift = {
872             dx: -x,
873             dy: -y,
874             scale: size
875         };
876         this.forEach(function (el) {
877             el.transform("...");
878         });
879         return this;
880     };
881     var createNode,
882         initWin = function (win) {
883             var doc = win.document;
884             doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
885             try {
886                 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
887                 createNode = function (tagName) {
888                     return doc.createElement('<rvml:' + tagName + ' class="rvml">');
889                 };
890             } catch (e) {
891                 createNode = function (tagName) {
892                     return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
893                 };
894             }
895         };
896     initWin(R._g.win);
897     R._engine.create = function () {
898         var con = R._getContainer.apply(0, arguments),
899             container = con.container,
900             height = con.height,
901             s,
902             width = con.width,
903             x = con.x,
904             y = con.y;
905         if (!container) {
906             throw new Error("VML container not found.");
907         }
908         var res = new R._Paper,
909             c = res.canvas = R._g.doc.createElement("div"),
910             cs = c.style;
911         x = x || 0;
912         y = y || 0;
913         width = width || 512;
914         height = height || 342;
915         res.width = width;
916         res.height = height;
917         width == +width && (width += "px");
918         height == +height && (height += "px");
919         res.coordsize = zoom * 1e3 + S + zoom * 1e3;
920         res.coordorigin = "0 0";
921         res.span = R._g.doc.createElement("span");
922         res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;";
923         c.appendChild(res.span);
924         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);
925         if (container == 1) {
926             R._g.doc.body.appendChild(c);
927             cs.left = x + "px";
928             cs.top = y + "px";
929             cs.position = "absolute";
930         } else {
931             if (container.firstChild) {
932                 container.insertBefore(c, container.firstChild);
933             } else {
934                 container.appendChild(c);
935             }
936         }
937         // plugins.call(res, res, R.fn);
938         res.renderfix = function () {};
939         return res;
940     };
941     R.prototype.clear = function () {
942         R.eve("clear", this);
943         this.canvas.innerHTML = E;
944         this.span = R._g.doc.createElement("span");
945         this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
946         this.canvas.appendChild(this.span);
947         this.bottom = this.top = null;
948     };
949     R.prototype.remove = function () {
950         R.eve("remove", this);
951         this.canvas.parentNode.removeChild(this.canvas);
952         for (var i in this) {
953             this[i] = removed(i);
954         }
955         return true;
956     };
957 }(window.Raphael);