Add plug-ins functionality.
[raphael] / raphael.js
1 /*
2  * Raphael 0.7 - 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 function Raphael() {
8     return Raphael._create.apply(Raphael, arguments);
9 };
10
11
12 (function (R) {
13     R.version = "0.7";
14     R.idGenerator = 0;
15     R._ = {
16         paper: {},
17         element: {}
18     };
19     R.fn = {};
20     var availableAttrs = {cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '16px "Arial"', "font-family": '"Arial"', "font-size": "16", gradient: 0, height: 0, opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, translation: "0 0", width: 0, x: 0, y: 0},
21         availableAnimAttrs = {cx: "number", cy: "number", fill: "colour", "fill-opacity": "number", "font-size": "number", height: "number", opacity: "number", path: "path", r: "number", rotation: "number", rx: "number", ry: "number", scale: "csv", stroke: "colour", "stroke-opacity": "number", "stroke-width": "number", translation: "csv", width: "number", x: "number", y: "number"};
22     R._.paper.circle = function (x, y, r) {
23         return R._.theCircle(this, x, y, r);
24     };
25     R._.paper.rect = function (x, y, w, h, r) {
26         return R._.theRect(this, x, y, w, h, r);
27     };
28     R._.paper.ellipse = function (x, y, rx, ry) {
29         return R._.theEllipse(this, x, y, rx, ry);
30     };
31     R._.paper.path = function (params, pathString) {
32         return R._.thePath(params, pathString, this);
33     };
34     R._.paper.image = function (src, x, y, w, h) {
35         return R._.theImage(this, src, x, y, w, h);
36     };
37     R._.paper.text = function (x, y, text) {
38         return R._.theText(this, x, y, text);
39     };
40     R._.paper.group = function () {
41         return this;
42     };
43     R._.paper.drawGrid = function (x, y, w, h, wv, hv, color) {
44         color = color || "#000";
45         var p = this.path({stroke: color, "stroke-width": 1})
46                 .moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).lineTo(x, y),
47             rowHeight = h / hv,
48             columnWidth = w / wv;
49         for (var i = 1; i < hv; i++) {
50             p.moveTo(x, y + i * rowHeight).lineTo(x + w, y + i * rowHeight);
51         }
52         for (var i = 1; i < wv; i++) {
53             p.moveTo(x + i * columnWidth, y).lineTo(x + i * columnWidth, y + h);
54         }
55         return p;
56     };
57     R._.element.stop = function () {
58         clearTimeout(this.animation_in_progress);
59     };
60     R._.element.scale = function (x, y) {
61         if (x == undefined && y == undefined) {
62             return {x: this._.sx, y: this._.sy};
63         }
64         y = y || x;
65         var dx, dy, cx, cy;
66         if (x != 0 && !(x == 1 && y == 1)) {
67             var dirx = Math.round(x / Math.abs(x)),
68                 diry = Math.round(y / Math.abs(y)),
69                 s = this.node.style;
70             dx = this.attr("x");
71             dy = this.attr("y");
72             cx = this.attr("cx");
73             cy = this.attr("cy");
74             if (dirx != 1 || diry != 1) {
75                 if (this.transformations) {
76                     this.transformations[2] = "scale(" + [dirx, diry] + ")";
77                     this.node.setAttribute("transform", this.transformations.join(" "));
78                     dx = (dirx < 0) ? -this.attr("x") - this.attrs.width * x * dirx / this._.sx : this.attr("x");
79                     dy = (diry < 0) ? -this.attr("y") - this.attrs.height * y * diry / this._.sy : this.attr("y");
80                     cx = this.attr("cx") * dirx;
81                     cy = this.attr("cy") * diry;
82                 } else {
83                     this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=" + dirx +
84                         ", M12=0, M21=0, M22=" + diry +
85                         ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')";
86                     s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
87                 }
88             } else {
89                 if (this.transformations) {
90                     this.transformations[2] = "";
91                     this.node.setAttribute("transform", this.transformations.join(" "));
92                 } else {
93                     this.node.filterMatrix = "";
94                     s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
95                 }
96             }
97             switch (this.type) {
98                 case "rect":
99                 case "image":
100                     this.attr({
101                         width: this.attrs.width * x * dirx / this._.sx,
102                         height: this.attrs.height * y * diry / this._.sy,
103                         x: dx,
104                         y: dy
105                     });
106                     break;
107                 case "circle":
108                 case "ellipse":
109                     this.attr({
110                         rx: this.attrs.rx * x * dirx / this._.sx,
111                         ry: this.attrs.ry * y * diry / this._.sy,
112                         r: this.attrs.r * x * diry / this._.sx,
113                         cx: cx,
114                         cy: cy
115                     });
116                     break;
117                 case "path":
118                     var path = Raphael.pathToRelative(Raphael.parsePathString(this.attr("path"))), 
119                         skip = true,
120                         dim = Raphael.pathDimensions(this.attrs.path),
121                         dx = -dim.width * (x - 1) / 2,
122                         dy = -dim.height * (y - 1) / 2;
123                     for (var i = 0, ii = path.length; i < ii; i++) {
124                         if (path[i][0].toUpperCase() == "M" && skip) {
125                             continue;
126                         } else {
127                             skip = false;
128                         }
129                         if (path[i][0].toUpperCase() == "A") {
130                             path[i][path[i].length - 2] *= x * dirx;
131                             path[i][path[i].length - 1] *= y * diry;
132                         } else {
133                             for (var j = 1, jj = path[i].length; j < jj; j++) {
134                                 path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
135                             }
136                         }
137                     }
138                     var dim2 = Raphael.pathDimensions(path),
139                         dx = dim.x + dim.width / 2 - dim2.x - dim2.width / 2,
140                         dy = dim.y + dim.height / 2 - dim2.y - dim2.height / 2;
141                     path = Raphael.pathToRelative(path);
142                     path[0][1] += dx;
143                     path[0][2] += dy;
144                     
145                     this.attr({path: path.join(" ")});
146             }
147         }
148         this._.sx = x;
149         this._.sy = y;
150         return this;
151     };
152     R._.element.animate = function (params, ms, callback) {
153         clearTimeout(this.animation_in_progress);
154         var from = {}, to = {}, diff = {}, t = {x: 0, y: 0};
155         for (var attr in params) {
156             if (attr in availableAnimAttrs) {
157                 from[attr] = this.attr(attr);
158                 if (typeof from[attr] == "undefined") {
159                     from[attr] = availableAttrs[attr];
160                 }
161                 to[attr] = params[attr];
162                 switch (availableAnimAttrs[attr]) {
163                     case "number":
164                         diff[attr] = (to[attr] - from[attr]) / ms;
165                         break;
166                     case "colour":
167                         from[attr] = Raphael.getRGB(from[attr]);
168                         var toColour = Raphael.getRGB(to[attr]);
169                         diff[attr] = {
170                             r: (toColour.r - from[attr].r) / ms,
171                             g: (toColour.g - from[attr].g) / ms,
172                             b: (toColour.b - from[attr].b) / ms
173                         };
174                         break;
175                     case "path":
176                         var pathes = Raphael.pathEqualiser(from[attr], to[attr]);
177                         from[attr] = pathes[0];
178                         to[attr] = pathes[1];
179                         diff[attr] = [];
180                         for (var i = 0, ii = from[attr].length; i < ii; i++) {
181                             diff[attr][i] = [0];
182                             for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
183                                 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
184                             }
185                         }
186                         break;
187                     case "csv":
188                         var values = params[attr].split(/[, ]+/);
189                         if (attr == "translation") {
190                             from[attr] = [0, 0];
191                             diff[attr] = [values[0] / ms, values[1] / ms];
192                         } else {
193                             from[attr] = from[attr].split(/[, ]+/);
194                             diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
195                         }
196                         to[attr] = values;
197                 }
198             }
199         }
200         var start = new Date(),
201             prev = 0,
202             that = this;
203         (function () {
204             var time = (new Date()).getTime() - start.getTime(),
205                 set = {},
206                 now;
207             if (time < ms) {
208                 for (var attr in from) {
209                     switch (availableAnimAttrs[attr]) {
210                         case "number":
211                             now = +from[attr] + time * diff[attr];
212                             break;
213                         case "colour":
214                             now = "rgb(" + [
215                                 Math.round(from[attr].r + time * diff[attr].r),
216                                 Math.round(from[attr].g + time * diff[attr].g),
217                                 Math.round(from[attr].b + time * diff[attr].b)
218                             ].join(",") + ")";
219                             break;
220                         case "path":
221                             now = [];
222                             for (var i = 0, ii = from[attr].length; i < ii; i++) {
223                                 now[i] = [from[attr][i][0]];
224                                 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
225                                     now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
226                                 }
227                                 now[i] = now[i].join(" ");
228                             }
229                             now = now.join(" ");
230                             break;
231                         case "csv":
232                             if (attr == "translation") {
233                                 var x = diff[attr][0] * (time - prev),
234                                     y = diff[attr][1] * (time - prev);
235                                 t.x += x;
236                                 t.y += y;
237                                 now = [x, y].join(" ");
238                             } else {
239                                 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
240                             }
241                             break;
242                     }
243                     if (attr == "font-size") {
244                         set[attr] = now + "px";
245                     } else {
246                         set[attr] = now;
247                     }
248                 }
249                 that.attr(set);
250                 that.animation_in_progress = setTimeout(arguments.callee, 0);
251                 R._.paper.safari();
252             } else {
253                 if (t.x || t.y) {
254                     that.translate(-t.x, -t.y);
255                 }
256                 that.attr(params);
257                 clearTimeout(that.animation_in_progress);
258                 R._.paper.safari();
259                 (typeof callback == "function") && callback.call(that);
260             }
261             prev = time;
262         })();
263         return this;
264     };
265     R._.paper.pathfinder = function (p, path) {
266         var commands = {
267             M: function (x, y) {
268                 this.moveTo(x, y);
269             },
270             C: function (x1, y1, x2, y2, x3, y3) {
271                 this.curveTo(x1, y1, x2, y2, x3, y3);
272             },
273             Q: function (x1, y1, x2, y2) {
274                 this.qcurveTo(x1, y1, x2, y2);
275             },
276             T: function (x, y) {
277                 this.qcurveTo(x, y);
278             },
279             S: function (x1, y1, x2, y2) {
280                 p.curveTo(x1, y1, x2, y2);
281             },
282             L: function (x, y) {
283                 p.lineTo(x, y);
284             },
285             H: function (x) {
286                 this.lineTo(x, this.last.y);
287             },
288             V: function (y) {
289                 this.lineTo(this.last.x, y);
290             },
291             A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
292                 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
293             },
294             Z: function () {
295                 this.andClose();
296             }
297         };
298
299         path = Raphael.pathToAbsolute(path);
300         for (var i = 0, ii = path.length; i < ii; i++) {
301             var b = path[i].shift();
302             commands[b].apply(p, path[i]);
303         }
304     };
305
306     R.toString = function () {
307         return  "Your browser " + (this.vml ? "doesn't ": "") + "support" + (this.svg ? "s": "") +
308                 " SVG.\nYou are running " + unescape("Rapha%EBl%20") + this.version;
309     };
310     // generic utilities
311     R.hsb2rgb = function (hue, saturation, brightness) {
312         if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
313             brightness = hue.b;
314             saturation = hue.s;
315             hue = hue.h;
316         }
317         var red,
318             green,
319             blue;
320         if (brightness == 0) {
321             return {r: 0, g: 0, b: 0, hex: "#000"};
322         } else {
323             var i = Math.floor(hue * 6),
324                 f = (hue * 6) - i,
325                 p = brightness * (1 - saturation),
326                 q = brightness * (1 - (saturation * f)),
327                 t = brightness * (1 - (saturation * (1 - f)));
328             [
329                 function () {red = brightness; green = t; blue = p;},
330                 function () {red = q; green = brightness; blue = p;},
331                 function () {red = p; green = brightness; blue = t;},
332                 function () {red = p; green = q; blue = brightness;},
333                 function () {red = t; green = p; blue = brightness;},
334                 function () {red = brightness; green = p; blue = q;},
335                 function () {red = brightness; green = t; blue = p;}
336             ][i]();
337         }
338         var rgb = {r: red, g: green, b: blue};
339         red *= 255;
340         green *= 255;
341         blue *= 255;
342         var r = Math.round(red).toString(16);
343         if (r.length == 1) {
344             r = "0" + r;
345         }
346         var g = Math.round(green).toString(16);
347         if (g.length == 1) {
348             g = "0" + g;
349         }
350         var b = Math.round(blue).toString(16);
351         if (b.length == 1) {
352             b = "0" + b;
353         }
354         rgb.hex = "#" + r + g + b;
355         return rgb;
356     };
357     R.rgb2hsb = function (red, green, blue) {
358         if (typeof red == "object" && "r" in red && "g" in red && "b" in red) {
359             blue = red.b;
360             green = red.g;
361             red = red.r;
362         }
363         if (typeof red == "string" && red.charAt(0) == "#") {
364             if (red.length == 4) {
365                 blue = parseInt(red.substring(3), 16);
366                 green = parseInt(red.substring(2, 3), 16);
367                 red = parseInt(red.substring(1, 2), 16);
368             } else {
369                 blue = parseInt(red.substring(5), 16);
370                 green = parseInt(red.substring(3, 5), 16);
371                 red = parseInt(red.substring(1, 3), 16);
372             }
373         }
374         if (red > 1 || green > 1 || blue > 1) {
375             red /= 255;
376             green /= 255;
377             blue /= 255;
378         }
379         var max = Math.max(red, green, blue),
380             min = Math.min(red, green, blue),
381             hue,
382             saturation,
383             brightness = max;
384         if (min == max) {
385             return {h: 0, s: 0, b: max};
386         } else {
387             var delta = (max - min);
388             saturation = delta / max;
389             if (red == max) {
390                 hue = (green - blue) / delta;
391             } else if (green == max) {
392                 hue = 2 + ((blue - red) / delta);
393             } else {
394                 hue = 4 + ((red - green) / delta);
395             }
396             hue /= 6;
397             if (hue < 0) {
398                 hue += 1;
399             }
400             if (hue > 1) {
401                 hue -= 1;
402             }
403         }
404         return {h: hue, s: saturation, b: brightness};
405     };
406     R.getRGB = function (colour) {
407         var red, green, blue,
408             rgb = colour.match(/^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*(\d+,\s*\d+,\s*\d+)\s*\)|rgb\(\s*(\d+%,\s*\d+%,\s*\d+%)\s*\)|hsb\(\s*(\d+,\s*\d+,\s*\d+)\s*\)|hsb\(\s*(\d+%,\s*\d+%,\s*\d+%)\s*\))\s*$/i);
409         if (rgb) {
410             if (rgb[2]) {
411                 blue = parseInt(rgb[2].substring(5), 16);
412                 green = parseInt(rgb[2].substring(3, 5), 16);
413                 red = parseInt(rgb[2].substring(1, 3), 16);
414             }
415             if (rgb[3]) {
416                 blue = parseInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
417                 green = parseInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
418                 red = parseInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
419             }
420             if (rgb[4]) {
421                 rgb = rgb[4].split(/\s*,\s*/);
422                 red = parseInt(rgb[0], 10);
423                 green = parseInt(rgb[1], 10);
424                 blue = parseInt(rgb[2], 10);
425             }
426             if (rgb[5]) {
427                 rgb = rgb[5].split(/\s*,\s*/);
428                 red = parseInt(rgb[0], 10) * 2.55;
429                 green = parseInt(rgb[1], 10) * 2.55;
430                 blue = parseInt(rgb[2], 10) * 2.55;
431             }
432             if (rgb[6]) {
433                 rgb = rgb[6].split(/\s*,\s*/);
434                 red = parseInt(rgb[0], 10);
435                 green = parseInt(rgb[1], 10);
436                 blue = parseInt(rgb[2], 10);
437                 return this.hsb2rgb(red, green, blue);
438             }
439             if (rgb[7]) {
440                 rgb = rgb[7].split(/\s*,\s*/);
441                 red = parseInt(rgb[0], 10) * 2.55;
442                 green = parseInt(rgb[1], 10) * 2.55;
443                 blue = parseInt(rgb[2], 10) * 2.55;
444                 return this.hsb2rgb(red, green, blue);
445             }
446             var rgb = {r: red, g: green, b: blue};
447             var r = Math.round(red).toString(16);
448             (r.length == 1) && (r = "0" + r);
449             var g = Math.round(green).toString(16);
450             (g.length == 1) && (g = "0" + g);
451             var b = Math.round(blue).toString(16);
452             (b.length == 1) && (b = "0" + b);
453             rgb.hex = "#" + r + g + b;
454             return rgb;
455         }
456     };
457     R.getColor = function (value) {
458         var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
459         var rgb = this.hsb2rgb(start.h, start.s, start.b);
460         start.h += .075;
461         if (start.h > 1) {
462             start.h = 0;
463             start.s -= .2;
464             if (start.s <= 0) {
465                 arguments.callee.start = {h: 0, s: 1, b: start.b};
466             }
467         }
468         return rgb.hex;
469     };
470     R.getColor.reset = function () {
471         this.start = undefined;
472     };
473     R.parsePathString = function (pathString) {
474         var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
475             data = [],
476             toString = function () {
477                 var res = "";
478                 for (var i = 0, ii = this.length; i < ii; i++) {
479                     res += this[i][0] + this[i].join(",").substring(2);
480                 }
481                 return res;
482             };
483         if (pathString.toString.toString() == toString.toString()) {
484             return pathString;
485         }
486         pathString.replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*\s*,?\s*)+)/ig, function (a, b, c) {
487             var params = [], name = b.toLowerCase();
488             c.replace(/(-?\d*\.?\d*)\s*,?\s*/ig, function (a, b) {
489                 b && params.push(+b);
490             });
491             while (params.length >= paramCounts[name]) {
492                 data.push([b].concat(params.splice(0, paramCounts[name])));
493                 if (!paramCounts[name]) {
494                     break;
495                 };
496             }
497         });
498         data.toString = toString;
499         return data;
500     };
501     R.pathDimensions = function (path) {
502         var pathArray = path;
503         if (typeof path == "string") {
504             pathArray = this.parsePathString(path);
505         }
506         pathArray = this.pathToAbsolute(pathArray);
507         var x = [], y = [], length = 0;
508         for (var i = 0, ii = pathArray.length; i < ii; i++) {
509             switch (pathArray[i][0]) {
510                 case "Z":
511                     break;
512                 case "A":
513                     x.push(pathArray[i][pathArray[i].length - 2]);
514                     y.push(pathArray[i][pathArray[i].length - 1]);
515                     break;
516                 default:
517                     for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
518                         if (j % 2) {
519                             x.push(pathArray[i][j]);
520                         } else {
521                             y.push(pathArray[i][j]);
522                         }
523                     }
524             }
525         }
526         var minx = Math.min.apply(Math, x),
527             miny = Math.min.apply(Math, y);
528         return {
529             x: minx,
530             y: miny,
531             width: Math.max.apply(Math, x) - minx,
532             height: Math.max.apply(Math, y) - miny,
533             X: x,
534             Y: y
535         };
536     };
537     R.pathToRelative = function (pathArray) {
538         var res = [];
539         if (typeof pathArray == "string") {
540             pathArray = this.parsePathString(pathArray);
541         }
542         var x = 0, y = 0, start = 0;
543         if (pathArray[0][0] == "M") {
544             x = pathArray[0][1];
545             y = pathArray[0][2];
546             start++;
547             res.push(pathArray[0]);
548         }
549         for (var i = start, ii = pathArray.length; i < ii; i++) {
550             res[i] = [];
551             if (pathArray[i][0] != pathArray[i][0].toLowerCase()) {
552                 res[i][0] = pathArray[i][0].toLowerCase();
553                 switch (res[i][0]) {
554                     case "a":
555                         res[i][1] = pathArray[i][1];
556                         res[i][2] = pathArray[i][2];
557                         res[i][3] = 0;
558                         res[i][4] = pathArray[i][4];
559                         res[i][5] = pathArray[i][5];
560                         res[i][6] = +(pathArray[i][6] - x).toFixed(3);
561                         res[i][7] = +(pathArray[i][7] - y).toFixed(3);
562                         break;
563                     case "v":
564                         res[i][1] = +(pathArray[i][1] - y).toFixed(3);
565                         break;
566                     default:
567                         for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
568                             res[i][j] = +(pathArray[i][j] - ((j % 2) ? x : y)).toFixed(3);
569                         }
570                 }
571             } else {
572                 res[i] = pathArray[i];
573             }
574             switch (res[i][0]) {
575                 case "z":
576                     break;
577                 case "h": 
578                     x += res[i][res[i].length - 1];
579                     break;
580                 case "v":
581                     y += res[i][res[i].length - 1];
582                     break;
583                 default:
584                     x += res[i][res[i].length - 2];
585                     y += res[i][res[i].length - 1];
586             }
587         }
588         res.toString = pathArray.toString;
589         return res;
590     };
591     R.pathToAbsolute = function (pathArray) {
592         var res = [];
593         if (typeof pathArray == "string") {
594             pathArray = this.parsePathString(pathArray);
595         }
596         var x = 0, y = 0, start = 0;
597         if (pathArray[0][0] == "M") {
598             x = +pathArray[0][1];
599             y = +pathArray[0][2];
600             start++;
601             res[0] = pathArray[0];
602         }
603         for (var i = start, ii = pathArray.length; i < ii; i++) {
604             res[i] = [];
605             if (pathArray[i][0] != pathArray[i][0].toUpperCase()) {
606                 res[i][0] = pathArray[i][0].toUpperCase();
607                 switch (res[i][0]) {
608                     case "A":
609                         res[i][1] = pathArray[i][1];
610                         res[i][2] = pathArray[i][2];
611                         res[i][3] = 0;
612                         res[i][4] = pathArray[i][4];
613                         res[i][5] = pathArray[i][5];
614                         res[i][6] = +(pathArray[i][6] + x).toFixed(3);
615                         res[i][7] = +(pathArray[i][7] + y).toFixed(3);
616                         break;
617                     case "V":
618                         res[i][1] = +pathArray[i][1] + y;
619                         break;
620                     default:
621                         for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
622                             res[i][j] = +pathArray[i][j] + ((j % 2) ? x : y);
623                         }
624                 }
625             } else {
626                 res[i] = pathArray[i];
627             }
628             switch (res[i][0]) {
629                 case "Z":
630                     break;
631                 case "H": 
632                     x = res[i][1];
633                     break;
634                 case "V":
635                     y = res[i][1];
636                     break;
637                 default:
638                     x = res[i][res[i].length - 2];
639                     y = res[i][res[i].length - 1];
640             }
641         }
642         res.toString = pathArray.toString;
643         return res;
644     };
645     R.pathEqualiser = function (path1, path2) {
646         var data = [this.pathToAbsolute(this.parsePathString(path1)), this.pathToAbsolute(this.parsePathString(path2))],
647             attrs = [{x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0}, {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0}],
648             processPath = function (path, d) {
649                 if (!path) {
650                     return ["U"];
651                 }
652                 switch (path[0]) {
653                     case "M":
654                         d.X = path[1];
655                         d.Y = path[2];
656                         break;
657                     case "S":
658                         var nx = d.x + (d.x - (d.bx || d.x));
659                         var ny = d.y + (d.y - (d.by || d.y));
660                         path = ["C", nx, ny, path[1], path[2], path[3], path[4]];
661                         break;
662                     case "T":
663                         var nx = d.x + (d.x - (d.bx || d.x));
664                         var ny = d.y + (d.y - (d.by || d.y));
665                         path = ["Q", nx, ny, path[1], path[2]];
666                         break;
667                     case "H":
668                         path = ["L", path[1], d.y];
669                         break;
670                     case "V":
671                         path = ["L", d.x, path[1]];
672                         break;
673                     case "Z":
674                         path = ["L", d.X, d.Y];
675                         break;
676                 }
677                 return path;
678             },
679             edgeCases = function (a, b, i) {
680                 if (data[a][i][0] == "M" && data[b][i][0] != "M") {
681                     data[b].splice(i, 0, ["M", attrs[b].x, attrs[b].y]);
682                     attrs[a].bx = data[a][i][data[a][i].length - 4] || 0;
683                     attrs[a].by = data[a][i][data[a][i].length - 3] || 0;
684                     attrs[a].x = data[a][i][data[a][i].length - 2];
685                     attrs[a].y = data[a][i][data[a][i].length - 1];
686                     return true;
687                 } else if (data[a][i][0] == "L" && data[b][i][0] == "C") {
688                     data[a][i] = ["C", attrs[a].x, attrs[a].y, data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
689                 } else if (data[a][i][0] == "L" && data[b][i][0] == "Q") {
690                     data[a][i] = ["Q", data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
691                 } else if (data[a][i][0] == "Q" && data[b][i][0] == "C") {
692                     var x = data[b][i][data[b][i].length - 2];
693                     var y = data[b][i][data[b][i].length - 1];
694                     data[b].splice(i + 1, 0, ["Q", x, y, x, y]);
695                     data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
696                     i++;
697                     attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
698                     attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
699                     attrs[b].x = data[b][i][data[b][i].length - 2];
700                     attrs[b].y = data[b][i][data[b][i].length - 1];
701                     return true;
702                 } else if (data[a][i][0] == "A" && data[b][i][0] == "C") {
703                     var x = data[b][i][data[b][i].length - 2];
704                     var y = data[b][i][data[b][i].length - 1];
705                     data[b].splice(i + 1, 0, ["A", 0, 0, data[a][i][3], data[a][i][4], data[a][i][5], x, y]);
706                     data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
707                     i++;
708                     attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
709                     attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
710                     attrs[b].x = data[b][i][data[b][i].length - 2];
711                     attrs[b].y = data[b][i][data[b][i].length - 1];
712                     return true;
713                 } else if (data[a][i][0] == "U") {
714                     data[a][i][0] = data[b][i][0];
715                     for (var j = 1, jj = data[b][i].length; j < jj; j++) {
716                         data[a][i][j] = (j % 2) ? attrs[a].x : attrs[a].y;
717                     }
718                 }
719                 return false;
720             };
721         for (var i = 0; i < Math.max(data[0].length, data[1].length); i++) {
722             data[0][i] = processPath(data[0][i], attrs[0]);
723             data[1][i] = processPath(data[1][i], attrs[1]);
724             if (data[0][i][0] != data[1][i][0] && (edgeCases(0, 1, i) || edgeCases(1, 0, i))) {
725                 continue;
726             }
727             attrs[0].bx = data[0][i][data[0][i].length - 4] || 0;
728             attrs[0].by = data[0][i][data[0][i].length - 3] || 0;
729             attrs[0].x = data[0][i][data[0][i].length - 2];
730             attrs[0].y = data[0][i][data[0][i].length - 1];
731             attrs[1].bx = data[1][i][data[1][i].length - 4] || 0;
732             attrs[1].by = data[1][i][data[1][i].length - 3] || 0;
733             attrs[1].x = data[1][i][data[1][i].length - 2];
734             attrs[1].y = data[1][i][data[1][i].length - 1];
735         }
736         return data;
737     };
738
739     var script = document.getElementsByTagName("script"),
740         newscript = document.createElement("script");
741     script = script[script.length - 1];
742     var path = script.src.match(/.*\//);
743     path = path ? path[0] : "";
744     newscript.type = "text/javascript";
745     newscript.src = path + (window.SVGAngle ? "raphael-svg.js" : "raphael-vml.js");
746     script.parentNode.appendChild(newscript);
747 })(Raphael);