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