New methods getTotalLength and getPointAtLength were added
[raphael] / raphael.js
1 /*!
2  * Raphael 1.3.0dev - JavaScript Vector Library
3  *
4  * Copyright (c) 2008 - 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6  */
7
8
9 window.Raphael = (function () {
10     var separator = /[, ]+/,
11         elements = /^(circle|rect|path|ellipse|text|image)$/,
12         doc = document,
13         win = window,
14         oldRaphael = {
15             was: "Raphael" in win,
16             is: win.Raphael
17         },
18         R = function () {
19             if (R.is(arguments[0], "array")) {
20                 var a = arguments[0],
21                     cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
22                     res = cnv.set();
23                 for (var i = 0, ii = a[length]; i < ii; i++) {
24                     var j = a[i] || {};
25                     elements.test(j.type) && res[push](cnv[j.type]().attr(j));
26                 }
27                 return res;
28             }
29             return create[apply](R, arguments);
30         },
31         Paper = function () {},
32         appendChild = "appendChild",
33         apply = "apply",
34         concat = "concat",
35         E = "",
36         S = " ",
37         split = "split",
38         events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S),
39         has = "hasOwnProperty",
40         join = "join",
41         length = "length",
42         proto = "prototype",
43         lowerCase = String[proto].toLowerCase,
44         math = Math,
45         mmax = math.max,
46         mmin = math.min,
47         nu = "number",
48         toString = "toString",
49         objectToString = Object[proto][toString],
50         paper = {},
51         pow = math.pow,
52         push = "push",
53         rg = /^(?=[\da-f]$)/,
54         ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
55         colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hs[bl]\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hs[bl]\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
56         round = math.round,
57         setAttribute = "setAttribute",
58         toFloat = parseFloat,
59         toInt = parseInt,
60         upperCase = String[proto].toUpperCase,
61         availableAttrs = {"clip-rect": "0 0 10e9 10e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
62         availableAnimAttrs = {"clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
63         rp = "replace";
64     R.version = "1.3.0dev";
65     R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
66     if (R.type == "VML") {
67         var d = document.createElement("div");
68         d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
69         if (d.childNodes[length] != 2) {
70             return null;
71         }
72     }
73     R.svg = !(R.vml = R.type == "VML");
74     Paper[proto] = R[proto];
75     R._id = 0;
76     R._oid = 0;
77     R.fn = {};
78     R.is = function (o, type) {
79         type = lowerCase.call(type);
80         return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
81     };
82     R.setWindow = function (newwin) {
83         win = newwin;
84         doc = win.document;
85     };
86     // colour utilities
87     var toHex = function (color) {
88         if (R.vml) {
89             // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
90             var trim = /^\s+|\s+$/g;
91             toHex = cacher(function (color) {
92                 var bod;
93                 color = (color + E)[rp](trim, E);
94                 try {
95                     var docum = new ActiveXObject("htmlfile");
96                     docum.write("<body>");
97                     docum.close();
98                     bod = docum.body;
99                 } catch(e) {
100                     bod = createPopup().document.body;
101                 }
102                 var range = bod.createTextRange();
103                 try {
104                     bod.style.color = color;
105                     var value = range.queryCommandValue("ForeColor");
106                     value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
107                     return "#" + ("000000" + value[toString](16)).slice(-6);
108                 } catch(e) {
109                     return "none";
110                 }
111             });
112         } else {
113             var i = doc.createElement("i");
114             i.title = "Rapha\xebl Colour Picker";
115             i.style.display = "none";
116             doc.body[appendChild](i);
117             toHex = cacher(function (color) {
118                 i.style.color = color;
119                 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
120             });
121         }
122         return toHex(color);
123     };
124     R.hsb2rgb = cacher(function (hue, saturation, brightness) {
125         if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
126             brightness = hue.b;
127             saturation = hue.s;
128             hue = hue.h;
129         }
130         var red,
131             green,
132             blue;
133         if (brightness == 0) {
134             return {r: 0, g: 0, b: 0, hex: "#000"};
135         }
136         if (hue > 1 || saturation > 1 || brightness > 1) {
137             hue /= 255;
138             saturation /= 255;
139             brightness /= 255;
140         }
141         var i = ~~(hue * 6),
142             f = (hue * 6) - i,
143             p = brightness * (1 - saturation),
144             q = brightness * (1 - (saturation * f)),
145             t = brightness * (1 - (saturation * (1 - f)));
146         red = [brightness, q, p, p, t, brightness, brightness][i];
147         green = [t, brightness, brightness, q, p, p, t][i];
148         blue = [p, p, t, brightness, brightness, q, p][i];
149         red *= 255;
150         green *= 255;
151         blue *= 255;
152         var rgb = {r: red, g: green, b: blue},
153             r = (~~red)[toString](16),
154             g = (~~green)[toString](16),
155             b = (~~blue)[toString](16);
156         r = r[rp](rg, "0");
157         g = g[rp](rg, "0");
158         b = b[rp](rg, "0");
159         rgb.hex = "#" + r + g + b;
160         return rgb;
161     }, R);
162     R.rgb2hsb = cacher(function (red, green, blue) {
163         if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
164             blue = red.b;
165             green = red.g;
166             red = red.r;
167         }
168         if (R.is(red, "string")) {
169             var clr = R.getRGB(red);
170             red = clr.r;
171             green = clr.g;
172             blue = clr.b;
173         }
174         if (red > 1 || green > 1 || blue > 1) {
175             red /= 255;
176             green /= 255;
177             blue /= 255;
178         }
179         var max = mmax(red, green, blue),
180             min = mmin(red, green, blue),
181             hue,
182             saturation,
183             brightness = max;
184         if (min == max) {
185             return {h: 0, s: 0, b: max};
186         } else {
187             var delta = (max - min);
188             saturation = delta / max;
189             if (red == max) {
190                 hue = (green - blue) / delta;
191             } else if (green == max) {
192                 hue = 2 + ((blue - red) / delta);
193             } else {
194                 hue = 4 + ((red - green) / delta);
195             }
196             hue /= 6;
197             hue < 0 && hue++;
198             hue > 1 && hue--;
199         }
200         return {h: hue, s: saturation, b: brightness};
201     }, R);
202     var p2s = /,?([achlmqrstvxz]),?/gi;
203     R._path2string = function () {
204         return this.join(",")[rp](p2s, "$1");
205     };
206     function cacher(f, scope, postprocessor) {
207         function newf() {
208             var arg = Array[proto].slice.call(arguments, 0),
209                 args = arg[join]("\u25ba"),
210                 cache = newf.cache = newf.cache || {},
211                 count = newf.count = newf.count || [];
212             if (cache[has](args)) {
213                 return postprocessor ? postprocessor(cache[args]) : cache[args];
214             }
215             count[length] >= 1e3 && delete cache[count.shift()];
216             count[push](args);
217             cache[args] = f[apply](scope, arg);
218             return postprocessor ? postprocessor(cache[args]) : cache[args];
219         }
220         return newf;
221     }
222
223     R.getRGB = cacher(function (colour) {
224         if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
225             return {r: -1, g: -1, b: -1, hex: "none", error: 1};
226         }
227         if (colour == "none") {
228             return {r: -1, g: -1, b: -1, hex: "none"};
229         }
230         !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
231         var res,
232             red,
233             green,
234             blue,
235             t,
236             rgb = colour.match(colourRegExp);
237         if (rgb) {
238             if (rgb[2]) {
239                 blue = toInt(rgb[2].substring(5), 16);
240                 green = toInt(rgb[2].substring(3, 5), 16);
241                 red = toInt(rgb[2].substring(1, 3), 16);
242             }
243             if (rgb[3]) {
244                 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
245                 green = toInt((t = rgb[3].charAt(2)) + t, 16);
246                 red = toInt((t = rgb[3].charAt(1)) + t, 16);
247             }
248             if (rgb[4]) {
249                 rgb = rgb[4][split](/\s*,\s*/);
250                 red = toFloat(rgb[0]);
251                 green = toFloat(rgb[1]);
252                 blue = toFloat(rgb[2]);
253             }
254             if (rgb[5]) {
255                 rgb = rgb[5][split](/\s*,\s*/);
256                 red = toFloat(rgb[0]) * 2.55;
257                 green = toFloat(rgb[1]) * 2.55;
258                 blue = toFloat(rgb[2]) * 2.55;
259             }
260             if (rgb[6]) {
261                 rgb = rgb[6][split](/\s*,\s*/);
262                 red = toFloat(rgb[0]);
263                 green = toFloat(rgb[1]);
264                 blue = toFloat(rgb[2]);
265                 return R.hsb2rgb(red, green, blue);
266             }
267             if (rgb[7]) {
268                 rgb = rgb[7][split](/\s*,\s*/);
269                 red = toFloat(rgb[0]) * 2.55;
270                 green = toFloat(rgb[1]) * 2.55;
271                 blue = toFloat(rgb[2]) * 2.55;
272                 return R.hsb2rgb(red, green, blue);
273             }
274             rgb = {r: red, g: green, b: blue};
275             var r = (~~red)[toString](16),
276                 g = (~~green)[toString](16),
277                 b = (~~blue)[toString](16);
278             r = r[rp](rg, "0");
279             g = g[rp](rg, "0");
280             b = b[rp](rg, "0");
281             rgb.hex = "#" + r + g + b;
282             return rgb;
283         }
284         return {r: -1, g: -1, b: -1, hex: "none", error: 1};
285     }, R);
286     R.getColor = function (value) {
287         var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
288             rgb = this.hsb2rgb(start.h, start.s, start.b);
289         start.h += .075;
290         if (start.h > 1) {
291             start.h = 0;
292             start.s -= .2;
293             start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
294         }
295         return rgb.hex;
296     };
297     R.getColor.reset = function () {
298         delete this.start;
299     };
300     // path utilities
301     R.parsePathString = cacher(function (pathString) {
302         if (!pathString) {
303             return null;
304         }
305         var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
306             data = [];
307         if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
308             data = pathClone(pathString);
309         }
310         if (!data[length]) {
311             (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
312                 var params = [],
313                     name = lowerCase.call(b);
314                 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
315                     b && params[push](+b);
316                 });
317                 while (params[length] >= paramCounts[name]) {
318                     data[push]([b][concat](params.splice(0, paramCounts[name])));
319                     if (!paramCounts[name]) {
320                         break;
321                     };
322                 }
323             });
324         }
325         data[toString] = R._path2string;
326         return data;
327     });
328     R.findDotsAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
329         var t1 = 1 - t,
330             x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
331             y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
332             mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
333             my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
334             nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
335             ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
336             ax = (1 - t) * p1x + t * c1x,
337             ay = (1 - t) * p1y + t * c1y,
338             cx = (1 - t) * c2x + t * p2x,
339             cy = (1 - t) * c2y + t * p2y;
340         return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}};
341     });
342     var pathDimensions = cacher(function (path) {
343         if (!path) {
344             return {x: 0, y: 0, width: 0, height: 0};
345         }
346         path = path2curve(path);
347         var x = 0, 
348             y = 0,
349             X = [],
350             Y = [],
351             p;
352         for (var i = 0, ii = path[length]; i < ii; i++) {
353             p = path[i];
354             if (p[0] == "M") {
355                 x = p[1];
356                 y = p[2];
357                 X[push](x);
358                 Y[push](y);
359             } else {
360                 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
361                 X = X[concat](dim.min.x, dim.max.x);
362                 Y = Y[concat](dim.min.y, dim.max.y);
363                 x = p[5];
364                 y = p[6];
365             }
366         }
367         var xmin = mmin[apply](0, X),
368             ymin = mmin[apply](0, Y);
369         return {
370             x: xmin,
371             y: ymin,
372             width: mmax[apply](0, X) - xmin,
373             height: mmax[apply](0, Y) - ymin
374         };
375     }),
376         pathClone = function (pathArray) {
377             var res = [];
378             if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
379                 pathArray = R.parsePathString(pathArray);
380             }
381             for (var i = 0, ii = pathArray[length]; i < ii; i++) {
382                 res[i] = [];
383                 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
384                     res[i][j] = pathArray[i][j];
385                 }
386             }
387             res[toString] = R._path2string;
388             return res;
389         },
390         pathToRelative = cacher(function (pathArray) {
391             if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
392                 pathArray = R.parsePathString(pathArray);
393             }
394             var res = [],
395                 x = 0,
396                 y = 0,
397                 mx = 0,
398                 my = 0,
399                 start = 0;
400             if (pathArray[0][0] == "M") {
401                 x = pathArray[0][1];
402                 y = pathArray[0][2];
403                 mx = x;
404                 my = y;
405                 start++;
406                 res[push](["M", x, y]);
407             }
408             for (var i = start, ii = pathArray[length]; i < ii; i++) {
409                 var r = res[i] = [],
410                     pa = pathArray[i];
411                 if (pa[0] != lowerCase.call(pa[0])) {
412                     r[0] = lowerCase.call(pa[0]);
413                     switch (r[0]) {
414                         case "a":
415                             r[1] = pa[1];
416                             r[2] = pa[2];
417                             r[3] = pa[3];
418                             r[4] = pa[4];
419                             r[5] = pa[5];
420                             r[6] = +(pa[6] - x).toFixed(3);
421                             r[7] = +(pa[7] - y).toFixed(3);
422                             break;
423                         case "v":
424                             r[1] = +(pa[1] - y).toFixed(3);
425                             break;
426                         case "m":
427                             mx = pa[1];
428                             my = pa[2];
429                         default:
430                             for (var j = 1, jj = pa[length]; j < jj; j++) {
431                                 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
432                             }
433                     }
434                 } else {
435                     r = res[i] = [];
436                     if (pa[0] == "m") {
437                         mx = pa[1] + x;
438                         my = pa[2] + y;
439                     }
440                     for (var k = 0, kk = pa[length]; k < kk; k++) {
441                         res[i][k] = pa[k];
442                     }
443                 }
444                 var len = res[i][length];
445                 switch (res[i][0]) {
446                     case "z":
447                         x = mx;
448                         y = my;
449                         break;
450                     case "h":
451                         x += +res[i][len - 1];
452                         break;
453                     case "v":
454                         y += +res[i][len - 1];
455                         break;
456                     default:
457                         x += +res[i][len - 2];
458                         y += +res[i][len - 1];
459                 }
460             }
461             res[toString] = R._path2string;
462             return res;
463         }, 0, pathClone),
464         pathToAbsolute = cacher(function (pathArray) {
465             if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
466                 pathArray = R.parsePathString(pathArray);
467             }
468             var res = [],
469                 x = 0,
470                 y = 0,
471                 mx = 0,
472                 my = 0,
473                 start = 0;
474             if (pathArray[0][0] == "M") {
475                 x = +pathArray[0][1];
476                 y = +pathArray[0][2];
477                 mx = x;
478                 my = y;
479                 start++;
480                 res[0] = ["M", x, y];
481             }
482             for (var i = start, ii = pathArray[length]; i < ii; i++) {
483                 var r = res[i] = [],
484                     pa = pathArray[i];
485                 if (pa[0] != upperCase.call(pa[0])) {
486                     r[0] = upperCase.call(pa[0]);
487                     switch (r[0]) {
488                         case "A":
489                             r[1] = pa[1];
490                             r[2] = pa[2];
491                             r[3] = pa[3];
492                             r[4] = pa[4];
493                             r[5] = pa[5];
494                             r[6] = +(pa[6] + x);
495                             r[7] = +(pa[7] + y);
496                             break;
497                         case "V":
498                             r[1] = +pa[1] + y;
499                             break;
500                         case "H":
501                             r[1] = +pa[1] + x;
502                             break;
503                         case "M":
504                             mx = +pa[1] + x;
505                             my = +pa[2] + y;
506                         default:
507                             for (var j = 1, jj = pa[length]; j < jj; j++) {
508                                 r[j] = +pa[j] + ((j % 2) ? x : y);
509                             }
510                     }
511                 } else {
512                     for (var k = 0, kk = pa[length]; k < kk; k++) {
513                         res[i][k] = pa[k];
514                     }
515                 }
516                 switch (r[0]) {
517                     case "Z":
518                         x = mx;
519                         y = my;
520                         break;
521                     case "H":
522                         x = r[1];
523                         break;
524                     case "V":
525                         y = r[1];
526                         break;
527                     default:
528                         x = res[i][res[i][length] - 2];
529                         y = res[i][res[i][length] - 1];
530                 }
531             }
532             res[toString] = R._path2string;
533             return res;
534         }, null, pathClone),
535         l2c = function (x1, y1, x2, y2) {
536             return [x1, y1, x2, y2, x2, y2];
537         },
538         q2c = function (x1, y1, ax, ay, x2, y2) {
539             var _13 = 1 / 3,
540                 _23 = 2 / 3;
541             return [
542                     _13 * x1 + _23 * ax,
543                     _13 * y1 + _23 * ay,
544                     _13 * x2 + _23 * ax,
545                     _13 * y2 + _23 * ay,
546                     x2,
547                     y2
548                 ];
549         },
550         a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
551             // for more information of where this math came from visit:
552             // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
553             var PI = math.PI,
554                 _120 = PI * 120 / 180,
555                 rad = PI / 180 * (+angle || 0),
556                 res = [],
557                 xy,
558                 rotate = cacher(function (x, y, rad) {
559                     var X = x * math.cos(rad) - y * math.sin(rad),
560                         Y = x * math.sin(rad) + y * math.cos(rad);
561                     return {x: X, y: Y};
562                 });
563             if (!recursive) {
564                 xy = rotate(x1, y1, -rad);
565                 x1 = xy.x;
566                 y1 = xy.y;
567                 xy = rotate(x2, y2, -rad);
568                 x2 = xy.x;
569                 y2 = xy.y;
570                 var cos = math.cos(PI / 180 * angle),
571                     sin = math.sin(PI / 180 * angle),
572                     x = (x1 - x2) / 2,
573                     y = (y1 - y2) / 2;
574                 rx = mmax(rx, math.abs(x));
575                 ry = mmax(ry, math.abs(y));
576                 var rx2 = rx * rx,
577                     ry2 = ry * ry,
578                     k = (large_arc_flag == sweep_flag ? -1 : 1) *
579                         math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
580                     cx = k * rx * y / ry + (x1 + x2) / 2,
581                     cy = k * -ry * x / rx + (y1 + y2) / 2,
582                     f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
583                     f2 = math.asin(((y2 - cy) / ry).toFixed(7));
584
585                 f1 = x1 < cx ? PI - f1 : f1;
586                 f2 = x2 < cx ? PI - f2 : f2;
587                 f1 < 0 && (f1 = PI * 2 + f1);
588                 f2 < 0 && (f2 = PI * 2 + f2);
589                 if (sweep_flag && f1 > f2) {
590                     f1 = f1 - PI * 2;
591                 }
592                 if (!sweep_flag && f2 > f1) {
593                     f2 = f2 - PI * 2;
594                 }
595             } else {
596                 f1 = recursive[0];
597                 f2 = recursive[1];
598                 cx = recursive[2];
599                 cy = recursive[3];
600             }
601             var df = f2 - f1;
602             if (math.abs(df) > _120) {
603                 var f2old = f2,
604                     x2old = x2,
605                     y2old = y2;
606                 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
607                 x2 = cx + rx * math.cos(f2);
608                 y2 = cy + ry * math.sin(f2);
609                 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
610             }
611             df = f2 - f1;
612             var c1 = math.cos(f1),
613                 s1 = math.sin(f1),
614                 c2 = math.cos(f2),
615                 s2 = math.sin(f2),
616                 t = math.tan(df / 4),
617                 hx = 4 / 3 * rx * t,
618                 hy = 4 / 3 * ry * t,
619                 m1 = [x1, y1],
620                 m2 = [x1 + hx * s1, y1 - hy * c1],
621                 m3 = [x2 + hx * s2, y2 - hy * c2],
622                 m4 = [x2, y2];
623             m2[0] = 2 * m1[0] - m2[0];
624             m2[1] = 2 * m1[1] - m2[1];
625             if (recursive) {
626                 return [m2, m3, m4][concat](res);
627             } else {
628                 res = [m2, m3, m4][concat](res)[join]()[split](",");
629                 var newres = [];
630                 for (var i = 0, ii = res[length]; i < ii; i++) {
631                     newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
632                 }
633                 return newres;
634             }
635         },
636         findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
637             var t1 = 1 - t;
638             return {
639                 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
640                 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
641             };
642         },
643         curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
644             var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
645                 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
646                 c = p1x - c1x,
647                 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
648                 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
649                 y = [p1y, p2y],
650                 x = [p1x, p2x],
651                 dot;
652             math.abs(t1) > 1e12 && (t1 = .5);
653             math.abs(t2) > 1e12 && (t2 = .5);
654             if (t1 > 0 && t1 < 1) {
655                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
656                 x[push](dot.x);
657                 y[push](dot.y);
658             }
659             if (t2 > 0 && t2 < 1) {
660                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
661                 x[push](dot.x);
662                 y[push](dot.y);
663             }
664             a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
665             b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
666             c = p1y - c1y;
667             t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
668             t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
669             math.abs(t1) > 1e12 && (t1 = .5);
670             math.abs(t2) > 1e12 && (t2 = .5);
671             if (t1 > 0 && t1 < 1) {
672                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
673                 x[push](dot.x);
674                 y[push](dot.y);
675             }
676             if (t2 > 0 && t2 < 1) {
677                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
678                 x[push](dot.x);
679                 y[push](dot.y);
680             }
681             return {
682                 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
683                 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
684             };
685         }),
686         path2curve = cacher(function (path, path2) {
687             var p = pathToAbsolute(path),
688                 p2 = path2 && pathToAbsolute(path2),
689                 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
690                 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
691                 processPath = function (path, d) {
692                     var nx, ny;
693                     if (!path) {
694                         return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
695                     }
696                     !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
697                     switch (path[0]) {
698                         case "M":
699                             d.X = path[1];
700                             d.Y = path[2];
701                             break;
702                         case "A":
703                             path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
704                             break;
705                         case "S":
706                             nx = d.x + (d.x - (d.bx || d.x));
707                             ny = d.y + (d.y - (d.by || d.y));
708                             path = ["C", nx, ny][concat](path.slice(1));
709                             break;
710                         case "T":
711                             d.qx = d.x + (d.x - (d.qx || d.x));
712                             d.qy = d.y + (d.y - (d.qy || d.y));
713                             path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
714                             break;
715                         case "Q":
716                             d.qx = path[1];
717                             d.qy = path[2];
718                             path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
719                             break;
720                         case "L":
721                             path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
722                             break;
723                         case "H":
724                             path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
725                             break;
726                         case "V":
727                             path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
728                             break;
729                         case "Z":
730                             path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
731                             break;
732                     }
733                     return path;
734                 },
735                 fixArc = function (pp, i) {
736                     if (pp[i][length] > 7) {
737                         pp[i].shift();
738                         var pi = pp[i];
739                         while (pi[length]) {
740                             pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
741                         }
742                         pp.splice(i, 1);
743                         ii = mmax(p[length], p2 && p2[length] || 0);
744                     }
745                 },
746                 fixM = function (path1, path2, a1, a2, i) {
747                     if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
748                         path2.splice(i, 0, ["M", a2.x, a2.y]);
749                         a1.bx = 0;
750                         a1.by = 0;
751                         a1.x = path1[i][1];
752                         a1.y = path1[i][2];
753                         ii = mmax(p[length], p2 && p2[length] || 0);
754                     }
755                 };
756             for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
757                 p[i] = processPath(p[i], attrs);
758                 fixArc(p, i);
759                 p2 && (p2[i] = processPath(p2[i], attrs2));
760                 p2 && fixArc(p2, i);
761                 fixM(p, p2, attrs, attrs2, i);
762                 fixM(p2, p, attrs2, attrs, i);
763                 var seg = p[i],
764                     seg2 = p2 && p2[i],
765                     seglen = seg[length],
766                     seg2len = p2 && seg2[length];
767                 attrs.x = seg[seglen - 2];
768                 attrs.y = seg[seglen - 1];
769                 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
770                 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
771                 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
772                 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
773                 attrs2.x = p2 && seg2[seg2len - 2];
774                 attrs2.y = p2 && seg2[seg2len - 1];
775             }
776             return p2 ? [p, p2] : p;
777         }, null, pathClone),
778         parseDots = cacher(function (gradient) {
779             var dots = [];
780             for (var i = 0, ii = gradient[length]; i < ii; i++) {
781                 var dot = {},
782                     par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
783                 dot.color = R.getRGB(par[1]);
784                 if (dot.color.error) {
785                     return null;
786                 }
787                 dot.color = dot.color.hex;
788                 par[2] && (dot.offset = par[2] + "%");
789                 dots[push](dot);
790             }
791             for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
792                 if (!dots[i].offset) {
793                     var start = toFloat(dots[i - 1].offset || 0),
794                         end = 0;
795                     for (var j = i + 1; j < ii; j++) {
796                         if (dots[j].offset) {
797                             end = dots[j].offset;
798                             break;
799                         }
800                     }
801                     if (!end) {
802                         end = 100;
803                         j = ii;
804                     }
805                     end = toFloat(end);
806                     var d = (end - start) / (j - i + 1);
807                     for (; i < j; i++) {
808                         start += d;
809                         dots[i].offset = start + "%";
810                     }
811                 }
812             }
813             return dots;
814         }),
815         getContainer = function () {
816             var container,
817                 x,
818                 y,
819                 width,
820                 height;
821             if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
822                 if (R.is(arguments[0], "string")) {
823                     container = doc.getElementById(arguments[0]);
824                 } else {
825                     container = arguments[0];
826                 }
827                 if (container.tagName) {
828                     if (arguments[1] == null) {
829                         return {
830                             container: container,
831                             width: container.style.pixelWidth || container.offsetWidth,
832                             height: container.style.pixelHeight || container.offsetHeight
833                         };
834                     } else {
835                         return {container: container, width: arguments[1], height: arguments[2]};
836                     }
837                 }
838             } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
839                 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
840             }
841         },
842         plugins = function (con, add) {
843             var that = this;
844             for (var prop in add) if (add[has](prop) && !(prop in con)) {
845                 switch (typeof add[prop]) {
846                     case "function":
847                         (function (f) {
848                             con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
849                         })(add[prop]);
850                     break;
851                     case "object":
852                         con[prop] = con[prop] || {};
853                         plugins.call(this, con[prop], add[prop]);
854                     break;
855                     default:
856                         con[prop] = add[prop];
857                     break;
858                 }
859             }
860         },
861         tear = function (el, paper) {
862             el == paper.top && (paper.top = el.prev);
863             el == paper.bottom && (paper.bottom = el.next);
864             el.next && (el.next.prev = el.prev);
865             el.prev && (el.prev.next = el.next);
866         },
867         tofront = function (el, paper) {
868             if (paper.top === el) {
869                 return;
870             }
871             tear(el, paper);
872             el.next = null;
873             el.prev = paper.top;
874             paper.top.next = el;
875             paper.top = el;
876         },
877         toback = function (el, paper) {
878             if (paper.bottom === el) {
879                 return;
880             }
881             tear(el, paper);
882             el.next = paper.bottom;
883             el.prev = null;
884             paper.bottom.prev = el;
885             paper.bottom = el;
886         },
887         insertafter = function (el, el2, paper) {
888             tear(el, paper);
889             el2 == paper.top && (paper.top = el);
890             el2.next && (el2.next.prev = el);
891             el.next = el2.next;
892             el.prev = el2;
893             el2.next = el;
894         },
895         insertbefore = function (el, el2, paper) {
896             tear(el, paper);
897             el2 == paper.bottom && (paper.bottom = el);
898             el2.prev && (el2.prev.next = el);
899             el.prev = el2.prev;
900             el2.prev = el;
901             el.next = el2;
902         },
903         removed = function (methodname) {
904             return function () {
905                 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
906             };
907         },
908         radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
909
910     // SVG
911     if (R.svg) {
912         Paper[proto].svgns = "http://www.w3.org/2000/svg";
913         Paper[proto].xlink = "http://www.w3.org/1999/xlink";
914         var round = function (num) {
915             return +num + (~~num === num) * .5;
916         },
917             roundPath = function (path) {
918                 for (var i = 0, ii = path[length]; i < ii; i++) {
919                     if (lowerCase.call(path[i][0]) != "a") {
920                         for (var j = 1, jj = path[i][length]; j < jj; j++) {
921                             path[i][j] = round(path[i][j]);
922                         }
923                     } else {
924                         path[i][6] = round(path[i][6]);
925                         path[i][7] = round(path[i][7]);
926                     }
927                 }
928                 return path;
929             },
930             $ = function (el, attr) {
931                 if (attr) {
932                     for (var key in attr) if (attr[has](key)) {
933                         el[setAttribute](key, attr[key]);
934                     }
935                 } else {
936                     return doc.createElementNS(Paper[proto].svgns, el);
937                 }
938             };
939         R[toString] = function () {
940             return  "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
941         };
942         var thePath = function (pathString, SVG) {
943             var el = $("path");
944             SVG.canvas && SVG.canvas[appendChild](el);
945             var p = new Element(el, SVG);
946             p.type = "path";
947             setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
948             return p;
949         };
950         var addGradientFill = function (o, gradient, SVG) {
951             var type = "linear",
952                 fx = .5, fy = .5,
953                 s = o.style;
954             gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
955                 type = "radial";
956                 if (_fx && _fy) {
957                     fx = toFloat(_fx);
958                     fy = toFloat(_fy);
959                     var dir = ((fy > .5) * 2 - 1);
960                     pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
961                         (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
962                         fy != .5 &&
963                         (fy = fy.toFixed(5) - 1e-5 * dir);
964                 }
965                 return E;
966             });
967             gradient = gradient[split](/\s*\-\s*/);
968             if (type == "linear") {
969                 var angle = gradient.shift();
970                 angle = -toFloat(angle);
971                 if (isNaN(angle)) {
972                     return null;
973                 }
974                 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
975                     max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
976                 vector[2] *= max;
977                 vector[3] *= max;
978                 if (vector[2] < 0) {
979                     vector[0] = -vector[2];
980                     vector[2] = 0;
981                 }
982                 if (vector[3] < 0) {
983                     vector[1] = -vector[3];
984                     vector[3] = 0;
985                 }
986             }
987             var dots = parseDots(gradient);
988             if (!dots) {
989                 return null;
990             }
991             var el = $(type + "Gradient");
992             el.id = "r" + (R._id++)[toString](36);
993             $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
994             SVG.defs[appendChild](el);
995             for (var i = 0, ii = dots[length]; i < ii; i++) {
996                 var stop = $("stop");
997                 $(stop, {
998                     offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
999                     "stop-color": dots[i].color || "#fff"
1000                 });
1001                 el[appendChild](stop);
1002             };
1003             $(o, {
1004                 fill: "url(#" + el.id + ")",
1005                 opacity: 1,
1006                 "fill-opacity": 1
1007             });
1008             s.fill = E;
1009             s.opacity = 1;
1010             s.fillOpacity = 1;
1011             return 1;
1012         };
1013         var updatePosition = function (o) {
1014             var bbox = o.getBBox();
1015             $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1016         };
1017         var setFillAndStroke = function (o, params) {
1018             var dasharray = {
1019                     "": [0],
1020                     "none": [0],
1021                     "-": [3, 1],
1022                     ".": [1, 1],
1023                     "-.": [3, 1, 1, 1],
1024                     "-..": [3, 1, 1, 1, 1, 1],
1025                     ". ": [1, 3],
1026                     "- ": [4, 3],
1027                     "--": [8, 3],
1028                     "- .": [4, 3, 1, 3],
1029                     "--.": [8, 3, 1, 3],
1030                     "--..": [8, 3, 1, 3, 1, 3]
1031                 },
1032                 node = o.node,
1033                 attrs = o.attrs,
1034                 rot = o.rotate(),
1035                 addDashes = function (o, value) {
1036                     value = dasharray[lowerCase.call(value)];
1037                     if (value) {
1038                         var width = o.attrs["stroke-width"] || "1",
1039                             butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1040                             dashes = [];
1041                         var i = value[length];
1042                         while (i--) {
1043                             dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1044                         }
1045                         $(node, {"stroke-dasharray": dashes[join](",")});
1046                     }
1047                 };
1048             params[has]("rotation") && (rot = params.rotation);
1049             var rotxy = (rot + E)[split](separator);
1050             if (!(rotxy.length - 1)) {
1051                 rotxy = null;
1052             } else {
1053                 rotxy[1] = +rotxy[1];
1054                 rotxy[2] = +rotxy[2];
1055             }
1056             toFloat(rot) && o.rotate(0, true);
1057             for (var att in params) if (params[has](att)) {
1058                 if (!availableAttrs[has](att)) {
1059                     continue;
1060                 }
1061                 var value = params[att];
1062                 attrs[att] = value;
1063                 switch (att) {
1064                     case "rotation":
1065                         o.rotate(value, true);
1066                         break;
1067                     // Hyperlink
1068                     case "href":
1069                     case "title":
1070                     case "target":
1071                         var pn = node.parentNode;
1072                         if (lowerCase.call(pn.tagName) != "a") {
1073                             var hl = $("a");
1074                             pn.insertBefore(hl, node);
1075                             hl[appendChild](node);
1076                             pn = hl;
1077                         }
1078                         pn.setAttributeNS(o.Paper[proto].xlink, att, value);
1079                         break;
1080                     case "cursor":
1081                         node.style.cursor = value;
1082                         break;
1083                     case "clip-rect":
1084                         var rect = (value + E)[split](separator);
1085                         if (rect[length] == 4) {
1086                             o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1087                             var el = $("clipPath"),
1088                                 rc = $("rect");
1089                             el.id = "r" + (R._id++)[toString](36);
1090                             $(rc, {
1091                                 x: rect[0],
1092                                 y: rect[1],
1093                                 width: rect[2],
1094                                 height: rect[3]
1095                             });
1096                             el[appendChild](rc);
1097                             o.paper.defs[appendChild](el);
1098                             $(node, {"clip-path": "url(#" + el.id + ")"});
1099                             o.clip = rc;
1100                         }
1101                         if (!value) {
1102                             var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1103                             clip && clip.parentNode.removeChild(clip);
1104                             $(node, {"clip-path": E});
1105                             delete o.clip;
1106                         }
1107                     break;
1108                     case "path":
1109                         if (value && o.type == "path") {
1110                             attrs.path = roundPath(pathToAbsolute(value));
1111                             $(node, {d: attrs.path});
1112                         }
1113                         break;
1114                     case "width":
1115                         node[setAttribute](att, value);
1116                         if (attrs.fx) {
1117                             att = "x";
1118                             value = attrs.x;
1119                         } else {
1120                             break;
1121                         }
1122                     case "x":
1123                         if (attrs.fx) {
1124                             value = -attrs.x - (attrs.width || 0);
1125                         }
1126                     case "rx":
1127                         if (att == "rx" && o.type == "rect") {
1128                             break;
1129                         }
1130                     case "cx":
1131                         rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1132                         node[setAttribute](att, round(value));
1133                         o.pattern && updatePosition(o);
1134                         break;
1135                     case "height":
1136                         node[setAttribute](att, value);
1137                         if (attrs.fy) {
1138                             att = "y";
1139                             value = attrs.y;
1140                         } else {
1141                             break;
1142                         }
1143                     case "y":
1144                         if (attrs.fy) {
1145                             value = -attrs.y - (attrs.height || 0);
1146                         }
1147                     case "ry":
1148                         if (att == "ry" && o.type == "rect") {
1149                             break;
1150                         }
1151                     case "cy":
1152                         rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1153                         node[setAttribute](att, round(value));
1154                         o.pattern && updatePosition(o);
1155                         break;
1156                     case "r":
1157                         if (o.type == "rect") {
1158                             $(node, {rx: value, ry: value});
1159                         } else {
1160                             node[setAttribute](att, value);
1161                         }
1162                         break;
1163                     case "src":
1164                         if (o.type == "image") {
1165                             node.setAttributeNS(o.paper.xlink, "href", value);
1166                         }
1167                         break;
1168                     case "stroke-width":
1169                         node.style.strokeWidth = value;
1170                         // Need following line for Firefox
1171                         node[setAttribute](att, value);
1172                         if (attrs["stroke-dasharray"]) {
1173                             addDashes(o, attrs["stroke-dasharray"]);
1174                         }
1175                         break;
1176                     case "stroke-dasharray":
1177                         addDashes(o, value);
1178                         break;
1179                     case "translation":
1180                         var xy = (value + E)[split](separator);
1181                         xy[0] = +xy[0] || 0;
1182                         xy[1] = +xy[1] || 0;
1183                         if (rotxy) {
1184                             rotxy[1] += xy[0];
1185                             rotxy[2] += xy[1];
1186                         }
1187                         translate.call(o, xy[0], xy[1]);
1188                         break;
1189                     case "scale":
1190                         var xy = (value + E)[split](separator);
1191                         o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1192                         break;
1193                     case "fill":
1194                         var isURL = (value + E).match(ISURL);
1195                         if (isURL) {
1196                             var el = $("pattern"),
1197                                 ig = $("image");
1198                             el.id = "r" + (R._id++)[toString](36);
1199                             $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1200                             $(ig, {x: 0, y: 0});
1201                             ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1202                             el[appendChild](ig);
1203
1204                             var img = doc.createElement("img");
1205                             img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1206                             img.onload = function () {
1207                                 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1208                                 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1209                                 doc.body.removeChild(this);
1210                                 o.paper.safari();
1211                             };
1212                             doc.body[appendChild](img);
1213                             img.src = isURL[1];
1214                             o.paper.defs[appendChild](el);
1215                             node.style.fill = "url(#" + el.id + ")";
1216                             $(node, {fill: "url(#" + el.id + ")"});
1217                             o.pattern = el;
1218                             o.pattern && updatePosition(o);
1219                             break;
1220                         }
1221                         if (!R.getRGB(value).error) {
1222                             delete params.gradient;
1223                             delete attrs.gradient;
1224                             !R.is(attrs.opacity, "undefined") &&
1225                                 R.is(params.opacity, "undefined") &&
1226                                 $(node, {opacity: attrs.opacity});
1227                             !R.is(attrs["fill-opacity"], "undefined") &&
1228                                 R.is(params["fill-opacity"], "undefined") &&
1229                                 $(node, {"fill-opacity": attrs["fill-opacity"]});
1230                         } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1231                             attrs.gradient = value;
1232                             attrs.fill = "none";
1233                             break;
1234                         }
1235                     case "stroke":
1236                         node[setAttribute](att, R.getRGB(value).hex);
1237                         break;
1238                     case "gradient":
1239                         (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1240                         break;
1241                     case "opacity":
1242                     case "fill-opacity":
1243                         if (attrs.gradient) {
1244                             var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1245                             if (gradient) {
1246                                 var stops = gradient.getElementsByTagName("stop");
1247                                 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1248                             }
1249                             break;
1250                         }
1251                     default:
1252                         att == "font-size" && (value = toInt(value, 10) + "px");
1253                         var cssrule = att[rp](/(\-.)/g, function (w) {
1254                             return upperCase.call(w.substring(1));
1255                         });
1256                         node.style[cssrule] = value;
1257                         // Need following line for Firefox
1258                         node[setAttribute](att, value);
1259                         break;
1260                 }
1261             }
1262             
1263             tuneText(o, params);
1264             if (rotxy) {
1265                 o.rotate(rotxy.join(S));
1266             } else {
1267                 toFloat(rot) && o.rotate(rot, true);
1268             }
1269         };
1270         var leading = 1.2;
1271         var tuneText = function (el, params) {
1272             if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1273                 return;
1274             }
1275             var a = el.attrs,
1276                 node = el.node,
1277                 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1278
1279             if (params[has]("text")) {
1280                 a.text = params.text;
1281                 while (node.firstChild) {
1282                     node.removeChild(node.firstChild);
1283                 }
1284                 var texts = (params.text + E)[split]("\n");
1285                 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1286                     var tspan = $("tspan");
1287                     i && $(tspan, {dy: fontSize * leading, x: a.x});
1288                     tspan[appendChild](doc.createTextNode(texts[i]));
1289                     node[appendChild](tspan);
1290                 }
1291             } else {
1292                 var texts = node.getElementsByTagName("tspan");
1293                 for (var i = 0, ii = texts[length]; i < ii; i++) {
1294                     i && $(texts[i], {dy: fontSize * leading, x: a.x});
1295                 }
1296             }
1297             $(node, {y: a.y});
1298             var bb = el.getBBox(),
1299                 dif = a.y - (bb.y + bb.height / 2);
1300             dif && isFinite(dif) && $(node, {y: a.y + dif});
1301         };
1302         var Element = function (node, svg) {
1303             var X = 0,
1304                 Y = 0;
1305             this[0] = node;
1306             this.id = R._oid++;
1307             this.node = node;
1308             node.raphael = this;
1309             this.paper = svg;
1310             this.attrs = this.attrs || {};
1311             this.transformations = []; // rotate, translate, scale
1312             this._ = {
1313                 tx: 0,
1314                 ty: 0,
1315                 rt: {deg: 0, cx: 0, cy: 0},
1316                 sx: 1,
1317                 sy: 1
1318             };
1319             !svg.bottom && (svg.bottom = this);
1320             this.prev = svg.top;
1321             svg.top && (svg.top.next = this);
1322             svg.top = this;
1323             this.next = null;
1324         };
1325         Element[proto].rotate = function (deg, cx, cy) {
1326             if (this.removed) {
1327                 return this;
1328             }
1329             if (deg == null) {
1330                 if (this._.rt.cx) {
1331                     return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1332                 }
1333                 return this._.rt.deg;
1334             }
1335             var bbox = this.getBBox();
1336             deg = (deg + E)[split](separator);
1337             if (deg[length] - 1) {
1338                 cx = toFloat(deg[1]);
1339                 cy = toFloat(deg[2]);
1340             }
1341             deg = toFloat(deg[0]);
1342             if (cx != null) {
1343                 this._.rt.deg = deg;
1344             } else {
1345                 this._.rt.deg += deg;
1346             }
1347             (cy == null) && (cx = null);
1348             this._.rt.cx = cx;
1349             this._.rt.cy = cy;
1350             cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1351             cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1352             if (this._.rt.deg) {
1353                 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1354                 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1355             } else {
1356                 this.transformations[0] = E;
1357                 this.clip && $(this.clip, {transform: E});
1358             }
1359             $(this.node, {transform: this.transformations[join](S)});
1360             return this;
1361         };
1362         Element[proto].hide = function () {
1363             !this.removed && (this.node.style.display = "none");
1364             return this;
1365         };
1366         Element[proto].show = function () {
1367             !this.removed && (this.node.style.display = "");
1368             return this;
1369         };
1370         Element[proto].remove = function () {
1371             if (this.removed) {
1372                 return;
1373             }
1374             tear(this, this.paper);
1375             this.node.parentNode.removeChild(this.node);
1376             for (var i in this) {
1377                 delete this[i];
1378             }
1379             this.removed = true;
1380         };
1381         Element[proto].getBBox = function () {
1382             if (this.removed) {
1383                 return this;
1384             }
1385             if (this.type == "path") {
1386                 return pathDimensions(this.attrs.path);
1387             }
1388             if (this.node.style.display == "none") {
1389                 this.show();
1390                 var hide = true;
1391             }
1392             var bbox = {};
1393             try {
1394                 bbox = this.node.getBBox();
1395             } catch(e) {
1396                 // Firefox 3.0.x plays badly here
1397             } finally {
1398                 bbox = bbox || {};
1399             }
1400             if (this.type == "text") {
1401                 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1402                 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1403                     var bb = this.node.getExtentOfChar(i);
1404                     (bb.y < bbox.y) && (bbox.y = bb.y);
1405                     (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1406                     (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1407                 }
1408             }
1409             hide && this.hide();
1410             return bbox;
1411         };
1412         Element[proto].attr = function () {
1413             if (this.removed) {
1414                 return this;
1415             }
1416             if (arguments[length] == 0) {
1417                 var res = {};
1418                 for (var i in this.attrs) if (this.attrs[has](i)) {
1419                     res[i] = this.attrs[i];
1420                 }
1421                 this._.rt.deg && (res.rotation = this.rotate());
1422                 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1423                 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1424                 return res;
1425             }
1426             if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1427                 if (arguments[0] == "translation") {
1428                     return translate.call(this);
1429                 }
1430                 if (arguments[0] == "rotation") {
1431                     return this.rotate();
1432                 }
1433                 if (arguments[0] == "scale") {
1434                     return this.scale();
1435                 }
1436                 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1437                     return this.attrs.gradient;
1438                 }
1439                 return this.attrs[arguments[0]];
1440             }
1441             if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1442                 var values = {};
1443                 for (var j in arguments[0]) if (arguments[0][has](j)) {
1444                     values[arguments[0][j]] = this.attrs[arguments[0][j]];
1445                 }
1446                 return values;
1447             }
1448             if (arguments[length] == 2) {
1449                 var params = {};
1450                 params[arguments[0]] = arguments[1];
1451                 setFillAndStroke(this, params);
1452             } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1453                 setFillAndStroke(this, arguments[0]);
1454             }
1455             return this;
1456         };
1457         Element[proto].toFront = function () {
1458             if (this.removed) {
1459                 return this;
1460             }
1461             this.node.parentNode[appendChild](this.node);
1462             var svg = this.paper;
1463             svg.top != this && tofront(this, svg);
1464             return this;
1465         };
1466         Element[proto].toBack = function () {
1467             if (this.removed) {
1468                 return this;
1469             }
1470             if (this.node.parentNode.firstChild != this.node) {
1471                 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1472                 toback(this, this.paper);
1473                 var svg = this.paper;
1474             }
1475             return this;
1476         };
1477         Element[proto].insertAfter = function (element) {
1478             if (this.removed) {
1479                 return this;
1480             }
1481             var svg = this.paper,
1482                 node = element.node;
1483             if (node.nextSibling) {
1484                 node.parentNode.insertBefore(this.node, node.nextSibling);
1485             } else {
1486                 node.parentNode[appendChild](this.node);
1487             }
1488             insertafter(this, element, this.paper);
1489             return this;
1490         };
1491         Element[proto].insertBefore = function (element) {
1492             if (this.removed) {
1493                 return this;
1494             }
1495             var node = element.node;
1496             node.parentNode.insertBefore(this.node, node);
1497             insertbefore(this, element, this.paper);
1498             return this;
1499         };
1500         Element[proto].getTotalLength = function () {
1501             if (this.type != "path") {
1502                 return -1;
1503             }
1504             return this.node.getTotalLength();
1505         };
1506         Element[proto].getPointAtLength = function (length) {
1507             if (this.type != "path") {
1508                 return -1;
1509             }
1510             var point = this.node.getPointAtLength(length);
1511             return {x: point.x, y: point.y};
1512         };
1513         var theCircle = function (svg, x, y, r) {
1514             x = round(x);
1515             y = round(y);
1516             var el = $("circle");
1517             svg.canvas && svg.canvas[appendChild](el);
1518             var res = new Element(el, svg);
1519             res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1520             res.type = "circle";
1521             $(el, res.attrs);
1522             return res;
1523         };
1524         var theRect = function (svg, x, y, w, h, r) {
1525             x = round(x);
1526             y = round(y);
1527             var el = $("rect");
1528             svg.canvas && svg.canvas[appendChild](el);
1529             var res = new Element(el, svg);
1530             res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1531             res.type = "rect";
1532             $(el, res.attrs);
1533             return res;
1534         };
1535         var theEllipse = function (svg, x, y, rx, ry) {
1536             x = round(x);
1537             y = round(y);
1538             var el = $("ellipse");
1539             svg.canvas && svg.canvas[appendChild](el);
1540             var res = new Element(el, svg);
1541             res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1542             res.type = "ellipse";
1543             $(el, res.attrs);
1544             return res;
1545         };
1546         var theImage = function (svg, src, x, y, w, h) {
1547             var el = $("image");
1548             $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1549             el.setAttributeNS(svg.xlink, "href", src);
1550             svg.canvas && svg.canvas[appendChild](el);
1551             var res = new Element(el, svg);
1552             res.attrs = {x: x, y: y, width: w, height: h, src: src};
1553             res.type = "image";
1554             return res;
1555         };
1556         var theText = function (svg, x, y, text) {
1557             var el = $("text");
1558             $(el, {x: x, y: y, "text-anchor": "middle"});
1559             svg.canvas && svg.canvas[appendChild](el);
1560             var res = new Element(el, svg);
1561             res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1562             res.type = "text";
1563             setFillAndStroke(res, res.attrs);
1564             return res;
1565         };
1566         var setSize = function (width, height) {
1567             this.width = width || this.width;
1568             this.height = height || this.height;
1569             this.canvas[setAttribute]("width", this.width);
1570             this.canvas[setAttribute]("height", this.height);
1571             return this;
1572         };
1573         var create = function () {
1574             var con = getContainer[apply](null, arguments),
1575                 container = con && con.container,
1576                 x = con.x,
1577                 y = con.y,
1578                 width = con.width,
1579                 height = con.height;
1580             if (!container) {
1581                 throw new Error("SVG container not found.");
1582             }
1583             var cnvs = $("svg");
1584             width = width || 512;
1585             height = height || 342;
1586             $(cnvs, {
1587                 xmlns: "http://www.w3.org/2000/svg",
1588                 version: 1.1,
1589                 width: width,
1590                 height: height
1591             });
1592             if (container == 1) {
1593                 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1594                 doc.body[appendChild](cnvs);
1595             } else {
1596                 if (container.firstChild) {
1597                     container.insertBefore(cnvs, container.firstChild);
1598                 } else {
1599                     container[appendChild](cnvs);
1600                 }
1601             }
1602             container = new Paper;
1603             container.width = width;
1604             container.height = height;
1605             container.canvas = cnvs;
1606             plugins.call(container, container, R.fn);
1607             container.clear();
1608             return container;
1609         };
1610         Paper[proto].clear = function () {
1611             var c = this.canvas;
1612             while (c.firstChild) {
1613                 c.removeChild(c.firstChild);
1614             }
1615             this.bottom = this.top = null;
1616             (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1617             c[appendChild](this.desc);
1618             c[appendChild](this.defs = $("defs"));
1619         };
1620         Paper[proto].remove = function () {
1621             this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1622             for (var i in this) {
1623                 this[i] = removed(i);
1624             }
1625         };
1626     }
1627
1628     // VML
1629     if (R.vml) {
1630         var path2vml = function (path) {
1631             var total =  /[ahqtv]/ig,
1632                 command = pathToAbsolute;
1633             (path + E).match(total) && (command = path2curve);
1634             total =  /[clmz]/g;
1635             if (command == pathToAbsolute && !(path + E).match(total)) {
1636                 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1637                     bites = /([clmz]),?([^clmz]*)/gi,
1638                     val = /-?[^,\s-]+/g;
1639                 var res = (path + E)[rp](bites, function (all, command, args) {
1640                     var vals = [];
1641                     args[rp](val, function (value) {
1642                         vals[push](round(value));
1643                     });
1644                     return map[command] + vals;
1645                 });
1646                 return res;
1647             }
1648             var pa = command(path), p, res = [], r;
1649             for (var i = 0, ii = pa[length]; i < ii; i++) {
1650                 p = pa[i];
1651                 r = lowerCase.call(pa[i][0]);
1652                 r == "z" && (r = "x");
1653                 for (var j = 1, jj = p[length]; j < jj; j++) {
1654                     r += round(p[j]) + (j != jj - 1 ? "," : E);
1655                 }
1656                 res[push](r);
1657             }
1658             return res[join](S);
1659         },
1660         segmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
1661             var old = {x: 0, y: 0},
1662                 len = 0;
1663             for (var i = 0; i < 1.01; i+=.01) {
1664                 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
1665                 i && (len += math.sqrt(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2)));
1666                 old = dot;
1667             }
1668             return len;
1669         };
1670         
1671         R[toString] = function () {
1672             return  "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1673         };
1674         var thePath = function (pathString, VML) {
1675             var g = createNode("group");
1676             g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1677             g.coordsize = VML.coordsize;
1678             g.coordorigin = VML.coordorigin;
1679             var el = createNode("shape"), ol = el.style;
1680             ol.width = VML.width + "px";
1681             ol.height = VML.height + "px";
1682             el.coordsize = this.coordsize;
1683             el.coordorigin = this.coordorigin;
1684             g[appendChild](el);
1685             var p = new Element(el, g, VML);
1686             p.isAbsolute = true;
1687             p.type = "path";
1688             p.path = [];
1689             p.Path = E;
1690             pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1691             VML.canvas[appendChild](g);
1692             return p;
1693         };
1694         var setFillAndStroke = function (o, params) {
1695             o.attrs = o.attrs || {};
1696             var node = o.node,
1697                 a = o.attrs,
1698                 s = node.style,
1699                 xy,
1700                 res = o;
1701             for (var par in params) if (params[has](par)) {
1702                 a[par] = params[par];
1703             }
1704             params.href && (node.href = params.href);
1705             params.title && (node.title = params.title);
1706             params.target && (node.target = params.target);
1707             params.cursor && (s.cursor = params.cursor);
1708             if (params.path && o.type == "path") {
1709                 a.path = params.path;
1710                 node.path = path2vml(a.path);
1711             }
1712             if (params.rotation != null) {
1713                 o.rotate(params.rotation, true);
1714             }
1715             if (params.translation) {
1716                 xy = (params.translation + E)[split](separator);
1717                 translate.call(o, xy[0], xy[1]);
1718                 if (o._.rt.cx != null) {
1719                     o._.rt.cx +=+ xy[0];
1720                     o._.rt.cy +=+ xy[1];
1721                     o.setBox(o.attrs, xy[0], xy[1]);
1722                 }
1723             }
1724             if (params.scale) {
1725                 xy = (params.scale + E)[split](separator);
1726                 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1727             }
1728             if ("clip-rect" in params) {
1729                 var rect = (params["clip-rect"] + E)[split](separator);
1730                 if (rect[length] == 4) {
1731                     rect[2] = +rect[2] + (+rect[0]);
1732                     rect[3] = +rect[3] + (+rect[1]);
1733                     var div = node.clipRect || doc.createElement("div"),
1734                         dstyle = div.style,
1735                         group = node.parentNode;
1736                     dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1737                     if (!node.clipRect) {
1738                         dstyle.position = "absolute";
1739                         dstyle.top = 0;
1740                         dstyle.left = 0;
1741                         dstyle.width = o.paper.width + "px";
1742                         dstyle.height = o.paper.height + "px";
1743                         group.parentNode.insertBefore(div, group);
1744                         div[appendChild](group);
1745                         node.clipRect = div;
1746                     }
1747                 }
1748                 if (!params["clip-rect"]) {
1749                     node.clipRect && (node.clipRect.style.clip = E);
1750                 }
1751             }
1752             if (o.type == "image" && params.src) {
1753                 node.src = params.src;
1754             }
1755             if (o.type == "image" && params.opacity) {
1756                 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1757                 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1758             }
1759             params.font && (s.font = params.font);
1760             params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1761             params["font-size"] && (s.fontSize = params["font-size"]);
1762             params["font-weight"] && (s.fontWeight = params["font-weight"]);
1763             params["font-style"] && (s.fontStyle = params["font-style"]);
1764             if (params.opacity != null || 
1765                 params["stroke-width"] != null ||
1766                 params.fill != null ||
1767                 params.stroke != null ||
1768                 params["stroke-width"] != null ||
1769                 params["stroke-opacity"] != null ||
1770                 params["fill-opacity"] != null ||
1771                 params["stroke-dasharray"] != null ||
1772                 params["stroke-miterlimit"] != null ||
1773                 params["stroke-linejoin"] != null ||
1774                 params["stroke-linecap"] != null) {
1775                 node = o.shape || node;
1776                 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1777                     newfill = false;
1778                 !fill && (newfill = fill = createNode("fill"));
1779                 if ("fill-opacity" in params || "opacity" in params) {
1780                     var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1781                     opacity < 0 && (opacity = 0);
1782                     opacity > 1 && (opacity = 1);
1783                     fill.opacity = opacity;
1784                 }
1785                 params.fill && (fill.on = true);
1786                 if (fill.on == null || params.fill == "none") {
1787                     fill.on = false;
1788                 }
1789                 if (fill.on && params.fill) {
1790                     var isURL = params.fill.match(ISURL);
1791                     if (isURL) {
1792                         fill.src = isURL[1];
1793                         fill.type = "tile";
1794                     } else {
1795                         fill.color = R.getRGB(params.fill).hex;
1796                         fill.src = E;
1797                         fill.type = "solid";
1798                         if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1799                             a.fill = "none";
1800                             a.gradient = params.fill;
1801                         }
1802                     }
1803                 }
1804                 newfill && node[appendChild](fill);
1805                 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1806                 newstroke = false;
1807                 !stroke && (newstroke = stroke = createNode("stroke"));
1808                 if ((params.stroke && params.stroke != "none") ||
1809                     params["stroke-width"] ||
1810                     params["stroke-opacity"] != null ||
1811                     params["stroke-dasharray"] ||
1812                     params["stroke-miterlimit"] ||
1813                     params["stroke-linejoin"] ||
1814                     params["stroke-linecap"]) {
1815                     stroke.on = true;
1816                 }
1817                 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1818                 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1819                 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1820                     width = (toFloat(params["stroke-width"]) || 1) * .75;
1821                 opacity < 0 && (opacity = 0);
1822                 opacity > 1 && (opacity = 1);
1823                 params["stroke-width"] == null && (width = a["stroke-width"]);
1824                 params["stroke-width"] && (stroke.weight = width);
1825                 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1826                 stroke.opacity = opacity;
1827                 
1828                 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1829                 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1830                 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1831                 if (params["stroke-dasharray"]) {
1832                     var dasharray = {
1833                         "-": "shortdash",
1834                         ".": "shortdot",
1835                         "-.": "shortdashdot",
1836                         "-..": "shortdashdotdot",
1837                         ". ": "dot",
1838                         "- ": "dash",
1839                         "--": "longdash",
1840                         "- .": "dashdot",
1841                         "--.": "longdashdot",
1842                         "--..": "longdashdotdot"
1843                     };
1844                     stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1845                 }
1846                 newstroke && node[appendChild](stroke);
1847             }
1848             if (res.type == "text") {
1849                 var s = res.paper.span.style;
1850                 a.font && (s.font = a.font);
1851                 a["font-family"] && (s.fontFamily = a["font-family"]);
1852                 a["font-size"] && (s.fontSize = a["font-size"]);
1853                 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1854                 a["font-style"] && (s.fontStyle = a["font-style"]);
1855                 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "&#60;")[rp](/&/g, "&#38;")[rp](/\n/g, "<br>"));
1856                 res.W = a.w = res.paper.span.offsetWidth;
1857                 res.H = a.h = res.paper.span.offsetHeight;
1858                 res.X = a.x;
1859                 res.Y = a.y + round(res.H / 2);
1860
1861                 // text-anchor emulationm
1862                 switch (a["text-anchor"]) {
1863                     case "start":
1864                         res.node.style["v-text-align"] = "left";
1865                         res.bbx = round(res.W / 2);
1866                     break;
1867                     case "end":
1868                         res.node.style["v-text-align"] = "right";
1869                         res.bbx = -round(res.W / 2);
1870                     break;
1871                     default:
1872                         res.node.style["v-text-align"] = "center";
1873                     break;
1874                 }
1875             }
1876         };
1877         var addGradientFill = function (o, gradient) {
1878             o.attrs = o.attrs || {};
1879             var attrs = o.attrs,
1880                 fill = o.node.getElementsByTagName("fill"),
1881                 type = "linear",
1882                 fxfy = ".5 .5";
1883             o.attrs.gradient = gradient;
1884             gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1885                 type = "radial";
1886                 if (fx && fy) {
1887                     fx = toFloat(fx);
1888                     fy = toFloat(fy);
1889                     pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1890                     fxfy = fx + S + fy;
1891                 }
1892                 return E;
1893             });
1894             gradient = gradient[split](/\s*\-\s*/);
1895             if (type == "linear") {
1896                 var angle = gradient.shift();
1897                 angle = -toFloat(angle);
1898                 if (isNaN(angle)) {
1899                     return null;
1900                 }
1901             }
1902             var dots = parseDots(gradient);
1903             if (!dots) {
1904                 return null;
1905             }
1906             o = o.shape || o.node;
1907             fill = fill[0] || createNode("fill");
1908             if (dots[length]) {
1909                 fill.on = true;
1910                 fill.method = "none";
1911                 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1912                 fill.color = dots[0].color;
1913                 fill.color2 = dots[dots[length] - 1].color;
1914                 var clrs = [];
1915                 for (var i = 0, ii = dots[length]; i < ii; i++) {
1916                     dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1917                 }
1918                 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1919                 if (type == "radial") {
1920                     fill.focus = "100%";
1921                     fill.focussize = fxfy;
1922                     fill.focusposition = fxfy;
1923                 } else {
1924                     fill.angle = (270 - angle) % 360;
1925                 }
1926             }
1927             return 1;
1928         };
1929         var Element = function (node, group, vml) {
1930             var Rotation = 0,
1931                 RotX = 0,
1932                 RotY = 0,
1933                 Scale = 1;
1934             this[0] = node;
1935             this.id = R._oid++;
1936             this.node = node;
1937             node.raphael = this;
1938             this.X = 0;
1939             this.Y = 0;
1940             this.attrs = {};
1941             this.Group = group;
1942             this.paper = vml;
1943             this._ = {
1944                 tx: 0,
1945                 ty: 0,
1946                 rt: {deg:0},
1947                 sx: 1,
1948                 sy: 1
1949             };
1950             !vml.bottom && (vml.bottom = this);
1951             this.prev = vml.top;
1952             vml.top && (vml.top.next = this);
1953             vml.top = this;
1954             this.next = null;
1955         };
1956         Element[proto].rotate = function (deg, cx, cy) {
1957             if (this.removed) {
1958                 return this;
1959             }
1960             if (deg == null) {
1961                 if (this._.rt.cx) {
1962                     return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1963                 }
1964                 return this._.rt.deg;
1965             }
1966             deg = (deg + E)[split](separator);
1967             if (deg[length] - 1) {
1968                 cx = toFloat(deg[1]);
1969                 cy = toFloat(deg[2]);
1970             }
1971             deg = toFloat(deg[0]);
1972             if (cx != null) {
1973                 this._.rt.deg = deg;
1974             } else {
1975                 this._.rt.deg += deg;
1976             }
1977             cy == null && (cx = null);
1978             this._.rt.cx = cx;
1979             this._.rt.cy = cy;
1980             this.setBox(this.attrs, cx, cy);
1981             this.Group.style.rotation = this._.rt.deg;
1982             // gradient fix for rotation. TODO
1983             // var fill = (this.shape || this.node).getElementsByTagName("fill");
1984             // fill = fill[0] || {};
1985             // var b = ((360 - this._.rt.deg) - 270) % 360;
1986             // !R.is(fill.angle, "undefined") && (fill.angle = b);
1987             return this;
1988         };
1989         Element[proto].setBox = function (params, cx, cy) {
1990             if (this.removed) {
1991                 return this;
1992             }
1993             var gs = this.Group.style,
1994                 os = (this.shape && this.shape.style) || this.node.style;
1995             params = params || {};
1996             for (var i in params) if (params[has](i)) {
1997                 this.attrs[i] = params[i];
1998             }
1999             cx = cx || this._.rt.cx;
2000             cy = cy || this._.rt.cy;
2001             var attr = this.attrs,
2002                 x,
2003                 y,
2004                 w,
2005                 h;
2006             switch (this.type) {
2007                 case "circle":
2008                     x = attr.cx - attr.r;
2009                     y = attr.cy - attr.r;
2010                     w = h = attr.r * 2;
2011                     break;
2012                 case "ellipse":
2013                     x = attr.cx - attr.rx;
2014                     y = attr.cy - attr.ry;
2015                     w = attr.rx * 2;
2016                     h = attr.ry * 2;
2017                     break;
2018                 case "rect":
2019                 case "image":
2020                     x = +attr.x;
2021                     y = +attr.y;
2022                     w = attr.width || 0;
2023                     h = attr.height || 0;
2024                     break;
2025                 case "text":
2026                     this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2027                     x = attr.x - round(this.W / 2);
2028                     y = attr.y - this.H / 2;
2029                     w = this.W;
2030                     h = this.H;
2031                     break;
2032                 case "path":
2033                     if (!this.attrs.path) {
2034                         x = 0;
2035                         y = 0;
2036                         w = this.paper.width;
2037                         h = this.paper.height;
2038                     } else {
2039                         var dim = pathDimensions(this.attrs.path);
2040                         x = dim.x;
2041                         y = dim.y;
2042                         w = dim.width;
2043                         h = dim.height;
2044                     }
2045                     break;
2046                 default:
2047                     x = 0;
2048                     y = 0;
2049                     w = this.paper.width;
2050                     h = this.paper.height;
2051                     break;
2052             }
2053             cx = (cx == null) ? x + w / 2 : cx;
2054             cy = (cy == null) ? y + h / 2 : cy;
2055             var left = cx - this.paper.width / 2,
2056                 top = cy - this.paper.height / 2;
2057             if (this.type == "path" || this.type == "text") {
2058                 (gs.left != left + "px") && (gs.left = left + "px");
2059                 (gs.top != top + "px") && (gs.top = top + "px");
2060                 this.X = this.type == "text" ? x : -left;
2061                 this.Y = this.type == "text" ? y : -top;
2062                 this.W = w;
2063                 this.H = h;
2064                 (os.left != -left + "px") && (os.left = -left + "px");
2065                 (os.top != -top + "px") && (os.top = -top + "px");
2066             } else {
2067                 (gs.left != left + "px") && (gs.left = left + "px");
2068                 (gs.top != top + "px") && (gs.top = top + "px");
2069                 this.X = x;
2070                 this.Y = y;
2071                 this.W = w;
2072                 this.H = h;
2073                 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2074                 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2075                 (os.left != x - left + "px") && (os.left = x - left + "px");
2076                 (os.top != y - top + "px") && (os.top = y - top + "px");
2077                 (os.width != w + "px") && (os.width = w + "px");
2078                 (os.height != h + "px") && (os.height = h + "px");
2079                 var arcsize = (+params.r || 0) / mmin(w, h);
2080                 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2081                     // We should replace element with the new one
2082                     var o = createNode("roundrect"),
2083                         a = {},
2084                         i = 0,
2085                         ii = this.events && this.events[length];
2086                     o.arcsize = arcsize;
2087                     o.raphael = this;
2088                     this.Group[appendChild](o);
2089                     this.Group.removeChild(this.node);
2090                     this[0] = this.node = o;
2091                     this.arcsize = arcsize;
2092                     for (var i in attr) {
2093                         a[i] = attr[i];
2094                     }
2095                     delete a.scale;
2096                     this.attr(a);
2097                     if (this.events) for (; i < ii; i++) {
2098                         this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2099                     }
2100                 }
2101             }
2102         };
2103         Element[proto].hide = function () {
2104             !this.removed && (this.Group.style.display = "none");
2105             return this;
2106         };
2107         Element[proto].show = function () {
2108             !this.removed && (this.Group.style.display = "block");
2109             return this;
2110         };
2111         Element[proto].getBBox = function () {
2112             if (this.removed) {
2113                 return this;
2114             }
2115             if (this.type == "path") {
2116                 return pathDimensions(this.attrs.path);
2117             }
2118             return {
2119                 x: this.X + (this.bbx || 0),
2120                 y: this.Y,
2121                 width: this.W,
2122                 height: this.H
2123             };
2124         };
2125         Element[proto].remove = function () {
2126             if (this.removed) {
2127                 return;
2128             }
2129             tear(this, this.paper);
2130             this.node.parentNode.removeChild(this.node);
2131             this.Group.parentNode.removeChild(this.Group);
2132             this.shape && this.shape.parentNode.removeChild(this.shape);
2133             for (var i in this) {
2134                 delete this[i];
2135             }
2136             this.removed = true;
2137         };
2138         Element[proto].attr = function () {
2139             if (this.removed) {
2140                 return this;
2141             }
2142             if (arguments[length] == 0) {
2143                 var res = {};
2144                 for (var i in this.attrs) if (this.attrs[has](i)) {
2145                     res[i] = this.attrs[i];
2146                 }
2147                 this._.rt.deg && (res.rotation = this.rotate());
2148                 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2149                 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2150                 return res;
2151             }
2152             if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2153                 if (arguments[0] == "translation") {
2154                     return translate.call(this);
2155                 }
2156                 if (arguments[0] == "rotation") {
2157                     return this.rotate();
2158                 }
2159                 if (arguments[0] == "scale") {
2160                     return this.scale();
2161                 }
2162                 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2163                     return this.attrs.gradient;
2164                 }
2165                 return this.attrs[arguments[0]];
2166             }
2167             if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2168                 var values = {};
2169                 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2170                     values[arguments[0][i]] = this.attrs[arguments[0][i]];
2171                 };
2172                 return values;
2173             }
2174             var params;
2175             if (arguments[length] == 2) {
2176                 params = {};
2177                 params[arguments[0]] = arguments[1];
2178             }
2179             arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2180             if (params) {
2181                 if (params.text && this.type == "text") {
2182                     this.node.string = params.text;
2183                 }
2184                 setFillAndStroke(this, params);
2185                 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2186                     addGradientFill(this, params.gradient);
2187                 }
2188                 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2189             }
2190             return this;
2191         };
2192         Element[proto].toFront = function () {
2193             !this.removed && this.Group.parentNode[appendChild](this.Group);
2194             this.paper.top != this && tofront(this, this.paper);
2195             return this;
2196         };
2197         Element[proto].toBack = function () {
2198             if (this.removed) {
2199                 return this;
2200             }
2201             if (this.Group.parentNode.firstChild != this.Group) {
2202                 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2203                 toback(this, this.paper);
2204             }
2205             return this;
2206         };
2207         Element[proto].insertAfter = function (element) {
2208             if (this.removed) {
2209                 return this;
2210             }
2211             if (element.Group.nextSibling) {
2212                 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2213             } else {
2214                 element.Group.parentNode[appendChild](this.Group);
2215             }
2216             insertafter(this, element, this.paper);
2217             return this;
2218         };
2219         Element[proto].insertBefore = function (element) {
2220             if (this.removed) {
2221                 return this;
2222             }
2223             element.Group.parentNode.insertBefore(this.Group, element.Group);
2224             insertbefore(this, element, this.paper);
2225             return this;
2226         };
2227         Element[proto].getTotalLength = function () {
2228             if (this.type != "path") {
2229                 return -1;
2230             }
2231             var path = path2curve(this.attrs.path), x, y, p,
2232                 len = 0;
2233             for (var i = 0, ii = path.length; i < ii; i++) {
2234                 p = path[i];
2235                 if (p[0] == "M") {
2236                     x = +p[1];
2237                     y = +p[2];
2238                 } else {
2239                     len += segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2240                     x = +p[5];
2241                     y = +p[6];
2242                 }
2243             }
2244             return len;
2245         };
2246         Element[proto].getPointAtLength = function (length) {
2247             if (this.type != "path") {
2248                 return -1;
2249             }
2250             var path = path2curve(this.attrs.path), x, y, p, l,
2251                 len = 0;
2252             for (var i = 0, ii = path.length; i < ii; i++) {
2253                 p = path[i];
2254                 if (p[0] == "M") {
2255                     x = +p[1];
2256                     y = +p[2];
2257                 } else {
2258                     l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2259                     if (len + l > length) {
2260                         return findDotAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2261                     }
2262                     len += l;
2263                     x = +p[5];
2264                     y = +p[6];
2265                 }
2266             }
2267             return {x: x, y: y};
2268         };
2269
2270         var theCircle = function (vml, x, y, r) {
2271             var g = createNode("group"),
2272                 o = createNode("oval"),
2273                 ol = o.style;
2274             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2275             g.coordsize = vml.coordsize;
2276             g.coordorigin = vml.coordorigin;
2277             g[appendChild](o);
2278             var res = new Element(o, g, vml);
2279             res.type = "circle";
2280             setFillAndStroke(res, {stroke: "#000", fill: "none"});
2281             res.attrs.cx = x;
2282             res.attrs.cy = y;
2283             res.attrs.r = r;
2284             res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2285             vml.canvas[appendChild](g);
2286             return res;
2287         },
2288         theRect = function (vml, x, y, w, h, r) {
2289             var g = createNode("group"),
2290                 o = createNode("roundrect"),
2291                 arcsize = (+r || 0) / (mmin(w, h));
2292             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2293             g.coordsize = vml.coordsize;
2294             g.coordorigin = vml.coordorigin;
2295             g[appendChild](o);
2296             o.arcsize = arcsize;
2297             var res = new Element(o, g, vml);
2298             res.type = "rect";
2299             setFillAndStroke(res, {stroke: "#000"});
2300             res.arcsize = arcsize;
2301             res.setBox({x: x, y: y, width: w, height: h, r: r});
2302             vml.canvas[appendChild](g);
2303             return res;
2304         },
2305         theEllipse = function (vml, x, y, rx, ry) {
2306             var g = createNode("group"),
2307                 o = createNode("oval"),
2308                 ol = o.style;
2309             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2310             g.coordsize = vml.coordsize;
2311             g.coordorigin = vml.coordorigin;
2312             g[appendChild](o);
2313             var res = new Element(o, g, vml);
2314             res.type = "ellipse";
2315             setFillAndStroke(res, {stroke: "#000"});
2316             res.attrs.cx = x;
2317             res.attrs.cy = y;
2318             res.attrs.rx = rx;
2319             res.attrs.ry = ry;
2320             res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2321             vml.canvas[appendChild](g);
2322             return res;
2323         },
2324         theImage = function (vml, src, x, y, w, h) {
2325             var g = createNode("group"),
2326                 o = createNode("image"),
2327                 ol = o.style;
2328             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2329             g.coordsize = vml.coordsize;
2330             g.coordorigin = vml.coordorigin;
2331             o.src = src;
2332             g[appendChild](o);
2333             var res = new Element(o, g, vml);
2334             res.type = "image";
2335             res.attrs.src = src;
2336             res.attrs.x = x;
2337             res.attrs.y = y;
2338             res.attrs.w = w;
2339             res.attrs.h = h;
2340             res.setBox({x: x, y: y, width: w, height: h});
2341             vml.canvas[appendChild](g);
2342             return res;
2343         },
2344         theText = function (vml, x, y, text) {
2345             var g = createNode("group"),
2346                 el = createNode("shape"),
2347                 ol = el.style,
2348                 path = createNode("path"),
2349                 ps = path.style,
2350                 o = createNode("textpath");
2351             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2352             g.coordsize = vml.coordsize;
2353             g.coordorigin = vml.coordorigin;
2354             path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2355             path.textpathok = true;
2356             ol.width = vml.width;
2357             ol.height = vml.height;
2358             o.string = text + E;
2359             o.on = true;
2360             el[appendChild](o);
2361             el[appendChild](path);
2362             g[appendChild](el);
2363             var res = new Element(o, g, vml);
2364             res.shape = el;
2365             res.textpath = path;
2366             res.type = "text";
2367             res.attrs.text = text;
2368             res.attrs.x = x;
2369             res.attrs.y = y;
2370             res.attrs.w = 1;
2371             res.attrs.h = 1;
2372             setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2373             res.setBox();
2374             vml.canvas[appendChild](g);
2375             return res;
2376         },
2377         setSize = function (width, height) {
2378             var cs = this.canvas.style;
2379             width == +width && (width += "px");
2380             height == +height && (height += "px");
2381             cs.width = width;
2382             cs.height = height;
2383             cs.clip = "rect(0 " + width + " " + height + " 0)";
2384             return this;
2385         },
2386         createNode;
2387         doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2388         try {
2389             !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2390             createNode = function (tagName) {
2391                 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2392             };
2393         } catch (e) {
2394             createNode = function (tagName) {
2395                 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2396             };
2397         }
2398         var create = function () {
2399             var con = getContainer[apply](null, arguments),
2400                 container = con.container,
2401                 height = con.height,
2402                 s,
2403                 width = con.width,
2404                 x = con.x,
2405                 y = con.y;
2406             if (!container) {
2407                 throw new Error("VML container not found.");
2408             }
2409             var res = new Paper,
2410                 c = res.canvas = doc.createElement("div"),
2411                 cs = c.style;
2412             width = width || 512;
2413             height = height || 342;
2414             width == +width && (width += "px");
2415             height == +height && (height += "px");
2416             res.width = 1e3;
2417             res.height = 1e3;
2418             res.coordsize = "1000 1000";
2419             res.coordorigin = "0 0";
2420             res.span = doc.createElement("span");
2421             res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2422             c[appendChild](res.span);
2423             cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2424             if (container == 1) {
2425                 doc.body[appendChild](c);
2426                 cs.left = x + "px";
2427                 cs.top = y + "px";
2428             } else {
2429                 container.style.width = width;
2430                 container.style.height = height;
2431                 if (container.firstChild) {
2432                     container.insertBefore(c, container.firstChild);
2433                 } else {
2434                     container[appendChild](c);
2435                 }
2436             }
2437             plugins.call(res, res, R.fn);
2438             return res;
2439         };
2440         Paper[proto].clear = function () {
2441             this.canvas.innerHTML = E;
2442             this.bottom = this.top = null;
2443         };
2444         Paper[proto].remove = function () {
2445             this.canvas.parentNode.removeChild(this.canvas);
2446             for (var i in this) {
2447                 this[i] = removed(i);
2448             }
2449         };
2450     }
2451
2452     // rest
2453     // Safari or Chrome (WebKit) rendering bug workaround method
2454     if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2455         Paper[proto].safari = function () {
2456             var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2457             setTimeout(function () {rect.remove();});
2458         };
2459     } else {
2460         Paper[proto].safari = function () {};
2461     }
2462
2463     // Events
2464     var addEvent = (function () {
2465         if (doc.addEventListener) {
2466             return function (obj, type, fn, element) {
2467                 var f = function (e) {
2468                     return fn.call(element, e);
2469                 };
2470                 obj.addEventListener(type, f, false);
2471                 return function () {
2472                     obj.removeEventListener(type, f, false);
2473                     return true;
2474                 };
2475             };
2476         } else if (doc.attachEvent) {
2477             return function (obj, type, fn, element) {
2478                 var f = function (e) {
2479                     return fn.call(element, e || win.event);
2480                 };
2481                 obj.attachEvent("on" + type, f);
2482                 var detacher = function () {
2483                     obj.detachEvent("on" + type, f);
2484                     return true;
2485                 };
2486                 return detacher;
2487             };
2488         }
2489     })();
2490     for (var i = events[length]; i--;) {
2491         (function (eventName) {
2492             Element[proto][eventName] = function (fn) {
2493                 if (R.is(fn, "function")) {
2494                     this.events = this.events || [];
2495                     this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2496                 }
2497                 return this;
2498             };
2499             Element[proto]["un" + eventName] = function (fn) {
2500                 var events = this.events,
2501                     l = events[length];
2502                 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2503                     events[l].unbind();
2504                     events.splice(l, 1);
2505                     !events.length && delete this.events;
2506                     return this;
2507                 }
2508                 return this;
2509             };
2510         })(events[i]);
2511     }
2512     Element[proto].hover = function (f_in, f_out) {
2513         return this.mouseover(f_in).mouseout(f_out);
2514     };
2515     Element[proto].unhover = function (f_in, f_out) {
2516         return this.unmouseover(f_in).unmouseout(f_out);
2517     };
2518     Paper[proto].circle = function (x, y, r) {
2519         return theCircle(this, x || 0, y || 0, r || 0);
2520     };
2521     Paper[proto].rect = function (x, y, w, h, r) {
2522         return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2523     };
2524     Paper[proto].ellipse = function (x, y, rx, ry) {
2525         return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2526     };
2527     Paper[proto].path = function (pathString) {
2528         pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2529         return thePath(R.format[apply](R, arguments), this);
2530     };
2531     Paper[proto].image = function (src, x, y, w, h) {
2532         return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2533     };
2534     Paper[proto].text = function (x, y, text) {
2535         return theText(this, x || 0, y || 0, text || E);
2536     };
2537     Paper[proto].set = function (itemsArray) {
2538         arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2539         return new Set(itemsArray);
2540     };
2541     Paper[proto].setSize = setSize;
2542     Paper[proto].top = Paper[proto].bottom = null;
2543     Paper[proto].raphael = R;
2544     function x_y() {
2545         return this.x + S + this.y;
2546     };
2547     Element[proto].scale = function (x, y, cx, cy) {
2548         if (x == null && y == null) {
2549             return {
2550                 x: this._.sx,
2551                 y: this._.sy,
2552                 toString: x_y
2553             };
2554         }
2555         y = y || x;
2556         !+y && (y = x);
2557         var dx,
2558             dy,
2559             dcx,
2560             dcy,
2561             a = this.attrs;
2562         if (x != 0) {
2563             var bb = this.getBBox(),
2564                 rcx = bb.x + bb.width / 2,
2565                 rcy = bb.y + bb.height / 2,
2566                 kx = x / this._.sx,
2567                 ky = y / this._.sy;
2568             cx = (+cx || cx == 0) ? cx : rcx;
2569             cy = (+cy || cy == 0) ? cy : rcy;
2570             var dirx = ~~(x / math.abs(x)),
2571                 diry = ~~(y / math.abs(y)),
2572                 s = this.node.style,
2573                 ncx = cx + (rcx - cx) * kx,
2574                 ncy = cy + (rcy - cy) * ky;
2575             switch (this.type) {
2576                 case "rect":
2577                 case "image":
2578                     var neww = a.width * dirx * kx,
2579                         hewh = a.height * diry * ky;
2580                     this.attr({
2581                         height: newh,
2582                         r: a.r * mmin(dirx * kx, diry * ky),
2583                         width: neww,
2584                         x: ncx - neww / 2,
2585                         y: ncy - newh / 2
2586                     });
2587                     break;
2588                 case "circle":
2589                 case "ellipse":
2590                     this.attr({
2591                         rx: a.rx * dirx * kx,
2592                         ry: a.ry * diry * ky,
2593                         r: a.r * mmin(dirx * kx, diry * ky),
2594                         cx: ncx,
2595                         cy: ncy
2596                     });
2597                     break;
2598                 case "path":
2599                     var path = pathToRelative(a.path),
2600                         skip = true;
2601                     for (var i = 0, ii = path[length]; i < ii; i++) {
2602                         var p = path[i],
2603                             j,
2604                             P0 = upperCase.call(p[0]);
2605                         if (P0 == "M" && skip) {
2606                             continue;
2607                         } else {
2608                             skip = false;
2609                         }
2610                         if (P0 == "A") {
2611                             p[path[i][length] - 2] *= kx;
2612                             p[path[i][length] - 1] *= ky;
2613                             p[1] *= dirx * kx;
2614                             p[2] *= diry * ky;
2615                             p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2616                         } else if (P0 == "H") {
2617                             for (j = 1, jj = p[length]; j < jj; j++) {
2618                                 p[j] *= kx;
2619                             }
2620                         } else if (P0 == "V") {
2621                             for (j = 1, jj = p[length]; j < jj; j++) {
2622                                 p[j] *= ky;
2623                             }
2624                          } else {
2625                             for (j = 1, jj = p[length]; j < jj; j++) {
2626                                 p[j] *= (j % 2) ? kx : ky;
2627                             }
2628                         }
2629                     }
2630                     var dim2 = pathDimensions(path),
2631                         dx = ncx - dim2.x - dim2.width / 2,
2632                         dy = ncy - dim2.y - dim2.height / 2;
2633                     path[0][1] += dx;
2634                     path[0][2] += dy;
2635                     this.attr({path: path});
2636                 break;
2637             }
2638             if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2639                 if (this.transformations) {
2640                     this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2641                     this.node[setAttribute]("transform", this.transformations[join](S));
2642                     dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2643                     dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2644                     this.attr({x: dx, y: dy});
2645                     a.fx = dirx - 1;
2646                     a.fy = diry - 1;
2647                 } else {
2648                     this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2649                         ", M12=0, M21=0, M22=", diry,
2650                         ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2651                     s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2652                 }
2653             } else {
2654                 if (this.transformations) {
2655                     this.transformations[2] = E;
2656                     this.node[setAttribute]("transform", this.transformations[join](S));
2657                     a.fx = 0;
2658                     a.fy = 0;
2659                 } else {
2660                     this.node.filterMatrix = E;
2661                     s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2662                 }
2663             }
2664             a.scale = [x, y, cx, cy][join](S);
2665             this._.sx = x;
2666             this._.sy = y;
2667         }
2668         return this;
2669     };
2670     Element[proto].clone = function () {
2671         var attr = this.attr();
2672         delete attr.scale;
2673         delete attr.translation;
2674         return this.paper[this.type]().attr(attr);
2675     };
2676
2677     // animation easing formulas
2678     R.easing_formulas = {
2679         linear: function (n) {
2680             return n;
2681         },
2682         "<": function (n) {
2683             return pow(n, 3);
2684         },
2685         ">": function (n) {
2686             return pow(n - 1, 3) + 1;
2687         },
2688         "<>": function (n) {
2689             n = n * 2;
2690             if (n < 1) {
2691                 return pow(n, 3) / 2;
2692             }
2693             n -= 2;
2694             return (pow(n, 3) + 2) / 2;
2695         },
2696         backIn: function (n) {
2697             var s = 1.70158;
2698             return n * n * ((s + 1) * n - s);
2699         },
2700         backOut: function (n) {
2701             n = n - 1;
2702             var s = 1.70158;
2703             return n * n * ((s + 1) * n + s) + 1;
2704         },
2705         elastic: function (n) {
2706             if (n == 0 || n == 1) {
2707                 return n;
2708             }
2709             var p = .3,
2710                 s = p / 4;
2711             return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2712         },
2713         bounce: function (n) {
2714             var s = 7.5625,
2715                 p = 2.75,
2716                 l;
2717             if (n < (1 / p)) {
2718                 l = s * n * n;
2719             } else {
2720                 if (n < (2 / p)) {
2721                     n -= (1.5 / p);
2722                     l = s * n * n + .75;
2723                 } else {
2724                     if (n < (2.5 / p)) {
2725                         n -= (2.25 / p);
2726                         l = s * n * n + .9375;
2727                     } else {
2728                         n -= (2.625 / p);
2729                         l = s * n * n + .984375;
2730                     }
2731                 }
2732             }
2733             return l;
2734         }
2735     };
2736
2737     var animationElements = {length : 0},
2738         animation = function () {
2739             var Now = +new Date;
2740             for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2741                 var e = animationElements[l];
2742                 if (e.stop) {
2743                     delete animationElements[l];
2744                     animationElements[length]--;
2745                     continue;
2746                 }
2747                 var time = Now - e.start,
2748                     ms = e.ms,
2749                     easing = e.easing,
2750                     from = e.from,
2751                     diff = e.diff,
2752                     to = e.to,
2753                     t = e.t,
2754                     prev = e.prev || 0,
2755                     that = e.el,
2756                     callback = e.callback,
2757                     set = {},
2758                     now;
2759                 if (time < ms) {
2760                     var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2761                     for (var attr in from) if (from[has](attr)) {
2762                         switch (availableAnimAttrs[attr]) {
2763                             case "number":
2764                                 now = +from[attr] + pos * ms * diff[attr];
2765                                 break;
2766                             case "colour":
2767                                 now = "rgb(" + [
2768                                     upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2769                                     upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2770                                     upto255(round(from[attr].b + pos * ms * diff[attr].b))
2771                                 ][join](",") + ")";
2772                                 break;
2773                             case "path":
2774                                 now = [];
2775                                 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2776                                     now[i] = [from[attr][i][0]];
2777                                     for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2778                                         now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2779                                     }
2780                                     now[i] = now[i][join](S);
2781                                 }
2782                                 now = now[join](S);
2783                                 break;
2784                             case "csv":
2785                                 switch (attr) {
2786                                     case "translation":
2787                                         var x = diff[attr][0] * (time - prev),
2788                                             y = diff[attr][1] * (time - prev);
2789                                         t.x += x;
2790                                         t.y += y;
2791                                         now = x + S + y;
2792                                     break;
2793                                     case "rotation":
2794                                         now = +from[attr][0] + pos * ms * diff[attr][0];
2795                                         from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2796                                     break;
2797                                     case "scale":
2798                                         now = [+from[attr][0] + pos * ms * diff[attr][0], +from[attr][1] + pos * ms * diff[attr][1], (2 in to[attr] ? to[attr][2] : E), (3 in to[attr] ? to[attr][3] : E)][join](S);
2799                                     break;
2800                                     case "clip-rect":
2801                                         now = [];
2802                                         var i = 4;
2803                                         while (i--) {
2804                                             now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2805                                         }
2806                                     break;
2807                                 }
2808                                 break;
2809                         }
2810                         set[attr] = now;
2811                     }
2812                     that.attr(set);
2813                     that._run && that._run.call(that);
2814                 } else {
2815                     (t.x || t.y) && that.translate(-t.x, -t.y);
2816                     to.scale && (to.scale = to.scale + E);
2817                     that.attr(to);
2818                     delete animationElements[l];
2819                     animationElements[length]--;
2820                     that.in_animation = null;
2821                     R.is(callback, "function") && callback.call(that);
2822                 }
2823                 e.prev = time;
2824             }
2825             R.svg && that.paper.safari();
2826             animationElements[length] && setTimeout(animation);
2827         },
2828         upto255 = function (color) {
2829             return color > 255 ? 255 : (color < 0 ? 0 : color);
2830         },
2831         translate = function (x, y) {
2832             if (x == null) {
2833                 return {x: this._.tx, y: this._.ty, toString: x_y};
2834             }
2835             this._.tx += +x;
2836             this._.ty += +y;
2837             switch (this.type) {
2838                 case "circle":
2839                 case "ellipse":
2840                     this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2841                     break;
2842                 case "rect":
2843                 case "image":
2844                 case "text":
2845                     this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2846                     break;
2847                 case "path":
2848                     var path = pathToRelative(this.attrs.path);
2849                     path[0][1] += +x;
2850                     path[0][2] += +y;
2851                     this.attr({path: path});
2852                 break;
2853             }
2854             return this;
2855         };
2856     Element[proto].animateWith = function (element, params, ms, easing, callback) {
2857         animationElements[element.id] && (params.start = animationElements[element.id].start);
2858         return this.animate(params, ms, easing, callback);
2859     };
2860     Element[proto].onAnimation = function (f) {
2861         this._run = f || null;
2862         return this;
2863     };
2864     Element[proto].animate = function (params, ms, easing, callback) {
2865         if (R.is(easing, "function") || !easing) {
2866             callback = easing || null;
2867         }
2868         var from = {},
2869             to = {},
2870             diff = {};
2871         for (var attr in params) if (params[has](attr)) {
2872             if (availableAnimAttrs[has](attr)) {
2873                 from[attr] = this.attr(attr);
2874                 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2875                 to[attr] = params[attr];
2876                 switch (availableAnimAttrs[attr]) {
2877                     case "number":
2878                         diff[attr] = (to[attr] - from[attr]) / ms;
2879                         break;
2880                     case "colour":
2881                         from[attr] = R.getRGB(from[attr]);
2882                         var toColour = R.getRGB(to[attr]);
2883                         diff[attr] = {
2884                             r: (toColour.r - from[attr].r) / ms,
2885                             g: (toColour.g - from[attr].g) / ms,
2886                             b: (toColour.b - from[attr].b) / ms
2887                         };
2888                         break;
2889                     case "path":
2890                         var pathes = path2curve(from[attr], to[attr]);
2891                         from[attr] = pathes[0];
2892                         var toPath = pathes[1];
2893                         diff[attr] = [];
2894                         for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2895                             diff[attr][i] = [0];
2896                             for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2897                                 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
2898                             }
2899                         }
2900                         break;
2901                     case "csv":
2902                         var values = (params[attr] + E)[split](separator),
2903                             from2 = (from[attr] + E)[split](separator);
2904                         switch (attr) {
2905                             case "translation":
2906                                 from[attr] = [0, 0];
2907                                 diff[attr] = [values[0] / ms, values[1] / ms];
2908                             break;
2909                             case "rotation":
2910                                 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2911                                 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2912                             break;
2913                             case "scale":
2914                                 params[attr] = values;
2915                                 from[attr] = (from[attr] + E)[split](separator);
2916                                 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2917                             break;
2918                             case "clip-rect":
2919                                 from[attr] = (from[attr] + E)[split](separator);
2920                                 diff[attr] = [];
2921                                 var i = 4;
2922                                 while (i--) {
2923                                     diff[attr][i] = (values[i] - from[attr][i]) / ms;
2924                                 }
2925                             break;
2926                         }
2927                         to[attr] = values;
2928                 }
2929             }
2930         }
2931         this.stop();
2932         this.in_animation = 1;
2933         animationElements[this.id] = {
2934             start: params.start || +new Date,
2935             ms: ms,
2936             easing: easing,
2937             from: from,
2938             diff: diff,
2939             to: to,
2940             el: this,
2941             callback: callback,
2942             t: {x: 0, y: 0}
2943         };
2944         ++animationElements[length] == 1 && animation();
2945         return this;
2946     };
2947     Element[proto].stop = function () {
2948         animationElements[this.id] && animationElements[length]--;
2949         delete animationElements[this.id];
2950         return this;
2951     };
2952     Element[proto].translate = function (x, y) {
2953         return this.attr({translation: x + " " + y});
2954     };
2955     Element[proto][toString] = function () {
2956         return "Rapha\xebl\u2019s object";
2957     };
2958     R.ae = animationElements;
2959
2960     // Set
2961     var Set = function (items) {
2962         this.items = [];
2963         this[length] = 0;
2964         if (items) {
2965             for (var i = 0, ii = items[length]; i < ii; i++) {
2966                 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2967                     this[this.items[length]] = this.items[this.items[length]] = items[i];
2968                     this[length]++;
2969                 }
2970             }
2971         }
2972     };
2973     Set[proto][push] = function () {
2974         var item,
2975             len;
2976         for (var i = 0, ii = arguments[length]; i < ii; i++) {
2977             item = arguments[i];
2978             if (item && (item.constructor == Element || item.constructor == Set)) {
2979                 len = this.items[length];
2980                 this[len] = this.items[len] = item;
2981                 this[length]++;
2982             }
2983         }
2984         return this;
2985     };
2986     Set[proto].pop = function () {
2987         delete this[this[length]--];
2988         return this.items.pop();
2989     };
2990     for (var method in Element[proto]) if (Element[proto][has](method)) {
2991         Set[proto][method] = (function (methodname) {
2992             return function () {
2993                 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2994                     this.items[i][methodname][apply](this.items[i], arguments);
2995                 }
2996                 return this;
2997             };
2998         })(method);
2999     }
3000     Set[proto].attr = function (name, value) {
3001         if (name && R.is(name, "array") && R.is(name[0], "object")) {
3002             for (var j = 0, jj = name[length]; j < jj; j++) {
3003                 this.items[j].attr(name[j]);
3004             }
3005         } else {
3006             for (var i = 0, ii = this.items[length]; i < ii; i++) {
3007                 this.items[i].attr[apply](this.items[i], arguments);
3008             }
3009         }
3010         return this;
3011     };
3012     Set[proto].animate = function (params, ms, easing, callback) {
3013         (R.is(easing, "function") || !easing) && (callback = easing || null);
3014         var len = this.items[length],
3015             i = len,
3016             set = this,
3017             collector;
3018         callback && (collector = function () {
3019             !--len && callback.call(set);
3020         });
3021         this.items[--i].animate(params, ms, easing || collector, collector);
3022         while (i--) {
3023             this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3024         }
3025         return this;
3026     };
3027     Set[proto].insertAfter = function (el) {
3028         var i = this.items[length];
3029         while (i--) {
3030             this.items[i].insertAfter(el);
3031         }
3032         return this;
3033     };
3034     Set[proto].getBBox = function () {
3035         var x = [],
3036             y = [],
3037             w = [],
3038             h = [];
3039         for (var i = this.items[length]; i--;) {
3040             var box = this.items[i].getBBox();
3041             x[push](box.x);
3042             y[push](box.y);
3043             w[push](box.x + box.width);
3044             h[push](box.y + box.height);
3045         }
3046         x = mmin[apply](0, x);
3047         y = mmin[apply](0, y);
3048         return {
3049             x: x,
3050             y: y,
3051             width: mmax[apply](0, w) - x,
3052             height: mmax[apply](0, h) - y
3053         };
3054     };
3055
3056     R.registerFont = function (font) {
3057         if (!font.face) {
3058             return font;
3059         }
3060         this.fonts = this.fonts || {};
3061         var fontcopy = {
3062                 w: font.w,
3063                 face: {},
3064                 glyphs: {}
3065             },
3066             family = font.face["font-family"];
3067         for (var prop in font.face) if (font.face[has](prop)) {
3068             fontcopy.face[prop] = font.face[prop];
3069         }
3070         if (this.fonts[family]) {
3071             this.fonts[family][push](fontcopy);
3072         } else {
3073             this.fonts[family] = [fontcopy];
3074         }
3075         if (!font.svg) {
3076             fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3077             for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3078                 var path = font.glyphs[glyph];
3079                 fontcopy.glyphs[glyph] = {
3080                     w: path.w,
3081                     k: {},
3082                     d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3083                             return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3084                         }) + "z"
3085                 };
3086                 if (path.k) {
3087                     for (var k in path.k) if (path[has](k)) {
3088                         fontcopy.glyphs[glyph].k[k] = path.k[k];
3089                     }
3090                 }
3091             }
3092         }
3093         return font;
3094     };
3095     Paper[proto].getFont = function (family, weight, style, stretch) {
3096         stretch = stretch || "normal";
3097         style = style || "normal";
3098         weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3099         var font = R.fonts[family];
3100         if (!font) {
3101             var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3102             for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3103                 if (name.test(fontName)) {
3104                     font = R.fonts[fontName];
3105                     break;
3106                 }
3107             }
3108         }
3109         var thefont;
3110         if (font) {
3111             for (var i = 0, ii = font[length]; i < ii; i++) {
3112                 thefont = font[i];
3113                 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3114                     break;
3115                 }
3116             }
3117         }
3118         return thefont;
3119     };
3120     Paper[proto].print = function (x, y, string, font, size, origin) {
3121         origin = origin || "middle"; // baseline|middle
3122         var out = this.set(),
3123             letters = (string + E)[split](E),
3124             shift = 0,
3125             path = E,
3126             scale;
3127         R.is(font, "string") && (font = this.getFont(font));
3128         if (font) {
3129             scale = (size || 16) / font.face["units-per-em"];
3130             var bb = font.face.bbox.split(separator),
3131                 top = +bb[0],
3132                 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3133             for (var i = 0, ii = letters[length]; i < ii; i++) {
3134                 var prev = i && font.glyphs[letters[i - 1]] || {},
3135                     curr = font.glyphs[letters[i]];
3136                 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3137                 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3138             }
3139             out.scale(scale, scale, top, height).translate(x - top, y - height);
3140         }
3141         return out;
3142     };
3143
3144     R.format = function (token) {
3145         var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3146             rg = /\{(\d+)\}/g;
3147         token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3148             return args[++i] == null ? E : args[i];
3149         }));
3150         return token || E;
3151     };
3152     R.ninja = function () {
3153         var r = win.Raphael, u;
3154         if (oldRaphael.was) {
3155             win.Raphael = oldRaphael.is;
3156         } else {
3157             try {
3158                 delete win.Raphael;
3159             } catch (e) {
3160                 win.Raphael = u;
3161             }
3162         }
3163         return r;
3164     };
3165     R.el = Element[proto];
3166     return R;
3167 })();