2 * Raphael 1.5.0 - JavaScript Vector Library
4 * Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
8 Raphael = (function () {
10 if (R.is(arguments[0], array)) {
12 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
14 for (var i = 0, ii = a[length]; i < ii; i++) {
16 elements.test(j.type) && res[push](cnv[j.type]().attr(j));
20 return create[apply](R, arguments);
23 var separator = /[, ]+/,
24 elements = /^(circle|rect|path|ellipse|text|image)$/,
26 has = "hasOwnProperty",
30 was: Object[proto][has].call(win, "Raphael"),
34 this.customAttributes = {};
36 appendChild = "appendChild",
39 supportsTouch = "createTouch" in doc,
44 events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
46 mousedown: "touchstart",
47 mousemove: "touchmove",
52 lowerCase = Str[proto].toLowerCase,
60 toString = "toString",
62 objectToString = Object[proto][toString],
66 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
67 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+(?:\s*,\s*[\d\.]+)?)\s*\)|rgba?\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%(?:\s*,\s*[\d\.]+%)?)\s*\)|hsb\(\s*([\d\.]+(?:deg|\xb0)?\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hsb\(\s*([\d\.]+(?:deg|\xb0|%)\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hsl\(\s*([\d\.]+(?:deg|\xb0)?\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hsl\(\s*([\d\.]+(?:deg|\xb0|%)\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
68 isnan = /^(NaN|-?Infinity)$/,
70 setAttribute = "setAttribute",
73 ms = " progid:DXImageTransform.Microsoft",
74 upperCase = Str[proto].toUpperCase,
75 availableAttrs = {blur: 0, "clip-rect": "0 0 1e9 1e9", 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},
76 availableAnimAttrs = {along: "along", blur: nu, "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},
78 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
79 if (R.type == "VML") {
80 var d = doc.createElement("div"),
82 d.innerHTML = '<v:shape adj="1"/>';
84 b.style.behavior = "url(#default#VML)";
85 if (!(b && typeof b.adj == "object")) {
90 R.svg = !(R.vml = R.type == "VML");
91 Paper[proto] = R[proto];
95 R.is = function (o, type) {
96 type = lowerCase.call(type);
97 if (type == "finite") {
98 return !isnan.test(+o);
100 return (type == "null" && o === null) ||
101 (type == typeof o) ||
102 (type == "object" && o === Object(o)) ||
103 (type == "array" && Array.isArray && Array.isArray(o)) ||
104 objectToString.call(o).slice(8, -1).toLowerCase() == type;
106 R.angle = function (x1, y1, x2, y2, x3, y3) {
113 return ((x < 0) * 180 + math.atan(-y / -x) * 180 / math.PI + 360) % 360;
115 return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
118 R.snapTo = function (values, value, tolerance) {
119 tolerance = tolerance || 10;
120 values = [][concat](values);
121 var i = values.length;
122 while (i--) if (math.abs(values[i] - value) <= tolerance) {
128 R.setWindow = function (newwin) {
133 var toHex = function (color) {
135 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
136 var trim = /^\s+|\s+$/g;
139 var docum = new win.ActiveXObject("htmlfile");
140 docum.write("<body>");
144 bod = win.createPopup().document.body;
146 var range = bod.createTextRange();
147 toHex = cacher(function (color) {
149 bod.style.color = Str(color)[rp](trim, E);
150 var value = range.queryCommandValue("ForeColor");
151 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
152 return "#" + ("000000" + value[toString](16)).slice(-6);
158 var i = doc.createElement("i");
159 i.title = "Rapha\xebl Colour Picker";
160 i.style.display = "none";
161 doc.body[appendChild](i);
162 toHex = cacher(function (color) {
163 i.style.color = color;
164 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
169 hsbtoString = function () {
170 return "hsb(" + [this.h, this.s, this.b] + ")";
172 hsltoString = function () {
173 return "hsl(" + [this.h, this.s, this.l] + ")";
175 rgbtoString = function () {
178 R.hsb2rgb = function (h, s, b) {
179 if (R.is(h, "object") && "h" in h && "s" in h && "b" in h) {
184 return R.hsl2rgb(h, s, b / 2);
186 R.hsl2rgb = function (h, s, l) {
187 if (R.is(h, "object") && "h" in h && "s" in h && "l" in h) {
192 if (h > 1 || s > 1 || l > 1) {
198 channels = ["r", "g", "b"],
213 for (var i = 0, ii = channels.length; i < ii; i++) {
214 t3 = h + 1 / 3 * -(i - 1);
218 rgb[channels[i]] = t1 + (t2 - t1) * 6 * t3;
219 } else if (t3 * 2 < 1) {
220 rgb[channels[i]] = t2;
221 } else if (t3 * 3 < 2) {
222 rgb[channels[i]] = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
224 rgb[channels[i]] = t1;
231 r = (~~rgb.r)[toString](16);
232 g = (~~rgb.g)[toString](16);
233 b = (~~rgb.b)[toString](16);
237 rgb.hex = "#" + r + g + b;
238 rgb.toString = rgbtoString;
241 R.rgb2hsb = function (red, green, blue) {
242 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
247 if (green == null && R.is(red, string)) {
248 var clr = R.getRGB(red);
253 if (red > 1 || green > 1 || blue > 1) {
258 var max = mmax(red, green, blue),
259 min = mmin(red, green, blue),
264 return {h: 0, s: 0, b: max, toString: hsbtoString};
266 var delta = (max - min);
267 saturation = delta / max;
269 hue = (green - blue) / delta;
270 } else if (green == max) {
271 hue = 2 + ((blue - red) / delta);
273 hue = 4 + ((red - green) / delta);
279 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
281 R.rgb2hsl = function (red, green, blue) {
282 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
287 if (green == null && R.is(red, string)) {
288 var clr = R.getRGB(red);
293 if (red > 1 || green > 1 || blue > 1) {
298 var max = mmax(red, green, blue),
299 min = mmin(red, green, blue),
305 hsl = {h: 0, s: 0, l: l};
307 var delta = max - min;
308 s = l < .5 ? delta / (max + min) : delta / (2 - max - min);
310 h = (green - blue) / delta;
311 } else if (green == max) {
312 h = 2 + (blue - red) / delta;
314 h = 4 + (red - green) / delta;
319 hsl = {h: h, s: s, l: l};
321 hsl.toString = hsltoString;
324 var p2s = /,?([achlmqrstvxz]),?/gi,
325 commaSpaces = /\s*,\s*/,
326 hsrg = {hs: 1, rg: 1};
327 R._path2string = function () {
328 return this.join(",")[rp](p2s, "$1");
330 function cacher(f, scope, postprocessor) {
332 var arg = Array[proto].slice.call(arguments, 0),
333 args = arg[join]("\u25ba"),
334 cache = newf.cache = newf.cache || {},
335 count = newf.count = newf.count || [];
336 if (cache[has](args)) {
337 return postprocessor ? postprocessor(cache[args]) : cache[args];
339 count[length] >= 1e3 && delete cache[count.shift()];
341 cache[args] = f[apply](scope, arg);
342 return postprocessor ? postprocessor(cache[args]) : cache[args];
347 R.getRGB = cacher(function (colour) {
348 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
349 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
351 if (colour == "none") {
352 return {r: -1, g: -1, b: -1, hex: "none"};
354 !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
361 rgb = colour.match(colourRegExp);
364 blue = toInt(rgb[2].substring(5), 16);
365 green = toInt(rgb[2].substring(3, 5), 16);
366 red = toInt(rgb[2].substring(1, 3), 16);
369 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
370 green = toInt((t = rgb[3].charAt(2)) + t, 16);
371 red = toInt((t = rgb[3].charAt(1)) + t, 16);
374 rgb = rgb[4][split](commaSpaces);
375 red = toFloat(rgb[0]);
376 green = toFloat(rgb[1]);
377 blue = toFloat(rgb[2]);
378 opacity = toFloat(rgb[3]);
381 rgb = rgb[5][split](commaSpaces);
382 red = toFloat(rgb[0]) * 2.55;
383 green = toFloat(rgb[1]) * 2.55;
384 blue = toFloat(rgb[2]) * 2.55;
385 opacity = toFloat(rgb[3]);
388 rgb = rgb[6][split](commaSpaces);
389 red = toFloat(rgb[0]);
390 green = toFloat(rgb[1]);
391 blue = toFloat(rgb[2]);
392 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360);
393 return R.hsb2rgb(red, green, blue);
396 rgb = rgb[7][split](commaSpaces);
397 red = toFloat(rgb[0]) * 2.55;
398 green = toFloat(rgb[1]) * 2.55;
399 blue = toFloat(rgb[2]) * 2.55;
400 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360 * 2.55);
401 return R.hsb2rgb(red, green, blue);
404 rgb = rgb[8][split](commaSpaces);
405 red = toFloat(rgb[0]);
406 green = toFloat(rgb[1]);
407 blue = toFloat(rgb[2]);
408 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360);
409 return R.hsl2rgb(red, green, blue);
412 rgb = rgb[9][split](commaSpaces);
413 red = toFloat(rgb[0]) * 2.55;
414 green = toFloat(rgb[1]) * 2.55;
415 blue = toFloat(rgb[2]) * 2.55;
416 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360 * 2.55);
417 return R.hsl2rgb(red, green, blue);
419 rgb = {r: red, g: green, b: blue};
420 var r = (~~red)[toString](16),
421 g = (~~green)[toString](16),
422 b = (~~blue)[toString](16);
426 rgb.hex = "#" + r + g + b;
427 isFinite(toFloat(opacity)) && (rgb.o = opacity);
430 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
432 R.getColor = function (value) {
433 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
434 rgb = this.hsb2rgb(start.h, start.s, start.b);
439 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
443 R.getColor.reset = function () {
447 var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
448 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
449 R.parsePathString = cacher(function (pathString) {
453 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
455 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
456 data = pathClone(pathString);
459 Str(pathString)[rp](pathCommand, function (a, b, c) {
461 name = lowerCase.call(b);
462 c[rp](pathValues, function (a, b) {
463 b && params[push](+b);
465 if (name == "m" && params[length] > 2) {
466 data[push]([b][concat](params.splice(0, 2)));
468 b = b == "m" ? "l" : "L";
470 while (params[length] >= paramCounts[name]) {
471 data[push]([b][concat](params.splice(0, paramCounts[name])));
472 if (!paramCounts[name]) {
478 data[toString] = R._path2string;
481 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
483 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
484 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
485 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
486 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
487 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
488 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
489 ax = (1 - t) * p1x + t * c1x,
490 ay = (1 - t) * p1y + t * c1y,
491 cx = (1 - t) * c2x + t * p2x,
492 cy = (1 - t) * c2y + t * p2y,
493 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
494 (mx > nx || my < ny) && (alpha += 180);
495 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};
497 var pathDimensions = cacher(function (path) {
499 return {x: 0, y: 0, width: 0, height: 0};
501 path = path2curve(path);
507 for (var i = 0, ii = path[length]; i < ii; i++) {
515 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
516 X = X[concat](dim.min.x, dim.max.x);
517 Y = Y[concat](dim.min.y, dim.max.y);
522 var xmin = mmin[apply](0, X),
523 ymin = mmin[apply](0, Y);
527 width: mmax[apply](0, X) - xmin,
528 height: mmax[apply](0, Y) - ymin
531 pathClone = function (pathArray) {
533 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
534 pathArray = R.parsePathString(pathArray);
536 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
538 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
539 res[i][j] = pathArray[i][j];
542 res[toString] = R._path2string;
545 pathToRelative = cacher(function (pathArray) {
546 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
547 pathArray = R.parsePathString(pathArray);
555 if (pathArray[0][0] == "M") {
561 res[push](["M", x, y]);
563 for (var i = start, ii = pathArray[length]; i < ii; i++) {
566 if (pa[0] != lowerCase.call(pa[0])) {
567 r[0] = lowerCase.call(pa[0]);
575 r[6] = +(pa[6] - x).toFixed(3);
576 r[7] = +(pa[7] - y).toFixed(3);
579 r[1] = +(pa[1] - y).toFixed(3);
585 for (var j = 1, jj = pa[length]; j < jj; j++) {
586 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
595 for (var k = 0, kk = pa[length]; k < kk; k++) {
599 var len = res[i][length];
606 x += +res[i][len - 1];
609 y += +res[i][len - 1];
612 x += +res[i][len - 2];
613 y += +res[i][len - 1];
616 res[toString] = R._path2string;
619 pathToAbsolute = cacher(function (pathArray) {
620 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
621 pathArray = R.parsePathString(pathArray);
629 if (pathArray[0][0] == "M") {
630 x = +pathArray[0][1];
631 y = +pathArray[0][2];
635 res[0] = ["M", x, y];
637 for (var i = start, ii = pathArray[length]; i < ii; i++) {
640 if (pa[0] != upperCase.call(pa[0])) {
641 r[0] = upperCase.call(pa[0]);
662 for (var j = 1, jj = pa[length]; j < jj; j++) {
663 r[j] = +pa[j] + ((j % 2) ? x : y);
667 for (var k = 0, kk = pa[length]; k < kk; k++) {
683 mx = res[i][res[i][length] - 2];
684 my = res[i][res[i][length] - 1];
686 x = res[i][res[i][length] - 2];
687 y = res[i][res[i][length] - 1];
690 res[toString] = R._path2string;
693 l2c = function (x1, y1, x2, y2) {
694 return [x1, y1, x2, y2, x2, y2];
696 q2c = function (x1, y1, ax, ay, x2, y2) {
708 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
709 // for more information of where this math came from visit:
710 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
712 _120 = PI * 120 / 180,
713 rad = PI / 180 * (+angle || 0),
716 rotate = cacher(function (x, y, rad) {
717 var X = x * math.cos(rad) - y * math.sin(rad),
718 Y = x * math.sin(rad) + y * math.cos(rad);
722 xy = rotate(x1, y1, -rad);
725 xy = rotate(x2, y2, -rad);
728 var cos = math.cos(PI / 180 * angle),
729 sin = math.sin(PI / 180 * angle),
732 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
740 k = (large_arc_flag == sweep_flag ? -1 : 1) *
741 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
742 cx = k * rx * y / ry + (x1 + x2) / 2,
743 cy = k * -ry * x / rx + (y1 + y2) / 2,
744 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
745 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
747 f1 = x1 < cx ? PI - f1 : f1;
748 f2 = x2 < cx ? PI - f2 : f2;
749 f1 < 0 && (f1 = PI * 2 + f1);
750 f2 < 0 && (f2 = PI * 2 + f2);
751 if (sweep_flag && f1 > f2) {
754 if (!sweep_flag && f2 > f1) {
764 if (math.abs(df) > _120) {
768 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
769 x2 = cx + rx * math.cos(f2);
770 y2 = cy + ry * math.sin(f2);
771 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
774 var c1 = math.cos(f1),
778 t = math.tan(df / 4),
782 m2 = [x1 + hx * s1, y1 - hy * c1],
783 m3 = [x2 + hx * s2, y2 - hy * c2],
785 m2[0] = 2 * m1[0] - m2[0];
786 m2[1] = 2 * m1[1] - m2[1];
788 return [m2, m3, m4][concat](res);
790 res = [m2, m3, m4][concat](res)[join]()[split](",");
792 for (var i = 0, ii = res[length]; i < ii; i++) {
793 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
798 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
801 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
802 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
805 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
806 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
807 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
809 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
810 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
814 math.abs(t1) > "1e12" && (t1 = .5);
815 math.abs(t2) > "1e12" && (t2 = .5);
816 if (t1 > 0 && t1 < 1) {
817 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
821 if (t2 > 0 && t2 < 1) {
822 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
826 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
827 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
829 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
830 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
831 math.abs(t1) > "1e12" && (t1 = .5);
832 math.abs(t2) > "1e12" && (t2 = .5);
833 if (t1 > 0 && t1 < 1) {
834 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
838 if (t2 > 0 && t2 < 1) {
839 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
844 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
845 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
848 path2curve = cacher(function (path, path2) {
849 var p = pathToAbsolute(path),
850 p2 = path2 && pathToAbsolute(path2),
851 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
852 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
853 processPath = function (path, d) {
856 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
858 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
865 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
868 nx = d.x + (d.x - (d.bx || d.x));
869 ny = d.y + (d.y - (d.by || d.y));
870 path = ["C", nx, ny][concat](path.slice(1));
873 d.qx = d.x + (d.x - (d.qx || d.x));
874 d.qy = d.y + (d.y - (d.qy || d.y));
875 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
880 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
883 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
886 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
889 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
892 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
897 fixArc = function (pp, i) {
898 if (pp[i][length] > 7) {
902 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
905 ii = mmax(p[length], p2 && p2[length] || 0);
908 fixM = function (path1, path2, a1, a2, i) {
909 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
910 path2.splice(i, 0, ["M", a2.x, a2.y]);
915 ii = mmax(p[length], p2 && p2[length] || 0);
918 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
919 p[i] = processPath(p[i], attrs);
921 p2 && (p2[i] = processPath(p2[i], attrs2));
923 fixM(p, p2, attrs, attrs2, i);
924 fixM(p2, p, attrs2, attrs, i);
927 seglen = seg[length],
928 seg2len = p2 && seg2[length];
929 attrs.x = seg[seglen - 2];
930 attrs.y = seg[seglen - 1];
931 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
932 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
933 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
934 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
935 attrs2.x = p2 && seg2[seg2len - 2];
936 attrs2.y = p2 && seg2[seg2len - 1];
938 return p2 ? [p, p2] : p;
940 parseDots = cacher(function (gradient) {
942 for (var i = 0, ii = gradient[length]; i < ii; i++) {
944 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
945 dot.color = R.getRGB(par[1]);
946 if (dot.color.error) {
949 dot.color = dot.color.hex;
950 par[2] && (dot.offset = par[2] + "%");
953 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
954 if (!dots[i].offset) {
955 var start = toFloat(dots[i - 1].offset || 0),
957 for (var j = i + 1; j < ii; j++) {
958 if (dots[j].offset) {
959 end = dots[j].offset;
968 var d = (end - start) / (j - i + 1);
971 dots[i].offset = start + "%";
977 getContainer = function (x, y, w, h) {
979 if (R.is(x, string) || R.is(x, "object")) {
980 container = R.is(x, string) ? doc.getElementById(x) : x;
981 if (container.tagName) {
984 container: container,
985 width: container.style.pixelWidth || container.offsetWidth,
986 height: container.style.pixelHeight || container.offsetHeight
989 return {container: container, width: y, height: w};
993 return {container: 1, x: x, y: y, width: w, height: h};
996 plugins = function (con, add) {
998 for (var prop in add) {
999 if (add[has](prop) && !(prop in con)) {
1000 switch (typeof add[prop]) {
1003 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
1007 con[prop] = con[prop] || {};
1008 plugins.call(this, con[prop], add[prop]);
1011 con[prop] = add[prop];
1017 tear = function (el, paper) {
1018 el == paper.top && (paper.top = el.prev);
1019 el == paper.bottom && (paper.bottom = el.next);
1020 el.next && (el.next.prev = el.prev);
1021 el.prev && (el.prev.next = el.next);
1023 tofront = function (el, paper) {
1024 if (paper.top === el) {
1029 el.prev = paper.top;
1030 paper.top.next = el;
1033 toback = function (el, paper) {
1034 if (paper.bottom === el) {
1038 el.next = paper.bottom;
1040 paper.bottom.prev = el;
1043 insertafter = function (el, el2, paper) {
1045 el2 == paper.top && (paper.top = el);
1046 el2.next && (el2.next.prev = el);
1051 insertbefore = function (el, el2, paper) {
1053 el2 == paper.bottom && (paper.bottom = el);
1054 el2.prev && (el2.prev.next = el);
1059 removed = function (methodname) {
1060 return function () {
1061 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
1064 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
1065 R.pathToRelative = pathToRelative;
1068 Paper[proto].svgns = "http://www.w3.org/2000/svg";
1069 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
1070 round = function (num) {
1071 return +num + (~~num === num) * .5;
1073 var $ = function (el, attr) {
1075 for (var key in attr) {
1076 if (attr[has](key)) {
1077 el[setAttribute](key, Str(attr[key]));
1081 el = doc.createElementNS(Paper[proto].svgns, el);
1082 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
1086 R[toString] = function () {
1087 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
1089 var thePath = function (pathString, SVG) {
1091 SVG.canvas && SVG.canvas[appendChild](el);
1092 var p = new Element(el, SVG);
1094 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1097 var addGradientFill = function (o, gradient, SVG) {
1098 var type = "linear",
1101 gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
1106 var dir = ((fy > .5) * 2 - 1);
1107 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
1108 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
1110 (fy = fy.toFixed(5) - 1e-5 * dir);
1114 gradient = gradient[split](/\s*\-\s*/);
1115 if (type == "linear") {
1116 var angle = gradient.shift();
1117 angle = -toFloat(angle);
1121 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
1122 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
1125 if (vector[2] < 0) {
1126 vector[0] = -vector[2];
1129 if (vector[3] < 0) {
1130 vector[1] = -vector[3];
1134 var dots = parseDots(gradient);
1138 var id = o.getAttribute(fillString);
1139 id = id.match(/^url\(#(.*)\)$/);
1140 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1142 var el = $(type + "Gradient");
1143 el.id = "r" + (R._id++)[toString](36);
1144 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1145 SVG.defs[appendChild](el);
1146 for (var i = 0, ii = dots[length]; i < ii; i++) {
1147 var stop = $("stop");
1149 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1150 "stop-color": dots[i].color || "#fff"
1152 el[appendChild](stop);
1155 fill: "url(#" + el.id + ")",
1164 var updatePosition = function (o) {
1165 var bbox = o.getBBox();
1166 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1168 var setFillAndStroke = function (o, params) {
1175 "-..": [3, 1, 1, 1, 1, 1],
1179 "- .": [4, 3, 1, 3],
1180 "--.": [8, 3, 1, 3],
1181 "--..": [8, 3, 1, 3, 1, 3]
1186 addDashes = function (o, value) {
1187 value = dasharray[lowerCase.call(value)];
1189 var width = o.attrs["stroke-width"] || "1",
1190 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1192 var i = value[length];
1194 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1196 $(node, {"stroke-dasharray": dashes[join](",")});
1199 params[has]("rotation") && (rot = params.rotation);
1200 var rotxy = Str(rot)[split](separator);
1201 if (!(rotxy.length - 1)) {
1204 rotxy[1] = +rotxy[1];
1205 rotxy[2] = +rotxy[2];
1207 toFloat(rot) && o.rotate(0, true);
1208 for (var att in params) {
1209 if (params[has](att)) {
1210 if (!availableAttrs[has](att)) {
1213 var value = params[att];
1220 o.rotate(value, true);
1225 var pn = node.parentNode;
1226 if (lowerCase.call(pn.tagName) != "a") {
1228 pn.insertBefore(hl, node);
1229 hl[appendChild](node);
1232 pn.setAttributeNS(o.paper.xlink, att, value);
1235 node.style.cursor = value;
1238 var rect = Str(value)[split](separator);
1239 if (rect[length] == 4) {
1240 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1241 var el = $("clipPath"),
1243 el.id = "r" + (R._id++)[toString](36);
1250 el[appendChild](rc);
1251 o.paper.defs[appendChild](el);
1252 $(node, {"clip-path": "url(#" + el.id + ")"});
1256 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1257 clip && clip.parentNode.removeChild(clip);
1258 $(node, {"clip-path": E});
1263 if (o.type == "path") {
1264 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1268 node[setAttribute](att, value);
1277 value = -attrs.x - (attrs.width || 0);
1280 if (att == "rx" && o.type == "rect") {
1284 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1285 node[setAttribute](att, value);
1286 o.pattern && updatePosition(o);
1289 node[setAttribute](att, value);
1298 value = -attrs.y - (attrs.height || 0);
1301 if (att == "ry" && o.type == "rect") {
1305 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1306 node[setAttribute](att, value);
1307 o.pattern && updatePosition(o);
1310 if (o.type == "rect") {
1311 $(node, {rx: value, ry: value});
1313 node[setAttribute](att, value);
1317 if (o.type == "image") {
1318 node.setAttributeNS(o.paper.xlink, "href", value);
1321 case "stroke-width":
1322 node.style.strokeWidth = value;
1323 // Need following line for Firefox
1324 node[setAttribute](att, value);
1325 if (attrs["stroke-dasharray"]) {
1326 addDashes(o, attrs["stroke-dasharray"]);
1329 case "stroke-dasharray":
1330 addDashes(o, value);
1333 var xy = Str(value)[split](separator);
1334 xy[0] = +xy[0] || 0;
1335 xy[1] = +xy[1] || 0;
1340 translate.call(o, xy[0], xy[1]);
1343 xy = Str(value)[split](separator);
1344 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1347 var isURL = Str(value).match(ISURL);
1350 var ig = $("image");
1351 el.id = "r" + (R._id++)[toString](36);
1352 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1353 $(ig, {x: 0, y: 0});
1354 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1355 el[appendChild](ig);
1357 var img = doc.createElement("img");
1358 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1359 img.onload = function () {
1360 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1361 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1362 doc.body.removeChild(this);
1365 doc.body[appendChild](img);
1367 o.paper.defs[appendChild](el);
1368 node.style.fill = "url(#" + el.id + ")";
1369 $(node, {fill: "url(#" + el.id + ")"});
1371 o.pattern && updatePosition(o);
1374 var clr = R.getRGB(value);
1376 delete params.gradient;
1377 delete attrs.gradient;
1378 !R.is(attrs.opacity, "undefined") &&
1379 R.is(params.opacity, "undefined") &&
1380 $(node, {opacity: attrs.opacity});
1381 !R.is(attrs["fill-opacity"], "undefined") &&
1382 R.is(params["fill-opacity"], "undefined") &&
1383 $(node, {"fill-opacity": attrs["fill-opacity"]});
1384 } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1385 attrs.gradient = value;
1386 attrs.fill = "none";
1389 clr[has]("o") && $(node, {"fill-opacity": clr.o > 1 ? clr.o / 100 : clr.o});
1391 clr = R.getRGB(value);
1392 node[setAttribute](att, clr.hex);
1393 att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o > 1 ? clr.o / 100 : clr.o});
1396 (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1399 case "fill-opacity":
1400 if (attrs.gradient) {
1401 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1403 var stops = gradient.getElementsByTagName("stop");
1404 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1409 att == "font-size" && (value = toInt(value, 10) + "px");
1410 var cssrule = att[rp](/(\-.)/g, function (w) {
1411 return upperCase.call(w.substring(1));
1413 node.style[cssrule] = value;
1414 // Need following line for Firefox
1415 node[setAttribute](att, value);
1421 tuneText(o, params);
1423 o.rotate(rotxy.join(S));
1425 toFloat(rot) && o.rotate(rot, true);
1429 tuneText = function (el, params) {
1430 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1435 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1437 if (params[has]("text")) {
1438 a.text = params.text;
1439 while (node.firstChild) {
1440 node.removeChild(node.firstChild);
1442 var texts = Str(params.text)[split]("\n");
1443 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1444 var tspan = $("tspan");
1445 i && $(tspan, {dy: fontSize * leading, x: a.x});
1446 tspan[appendChild](doc.createTextNode(texts[i]));
1447 node[appendChild](tspan);
1450 texts = node.getElementsByTagName("tspan");
1451 for (i = 0, ii = texts[length]; i < ii; i++) {
1452 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1456 var bb = el.getBBox(),
1457 dif = a.y - (bb.y + bb.height / 2);
1458 dif && isFinite(dif) && $(node, {y: a.y + dif});
1460 Element = function (node, svg) {
1466 node.raphael = this;
1468 this.attrs = this.attrs || {};
1469 this.transformations = []; // rotate, translate, scale
1473 rt: {deg: 0, cx: 0, cy: 0},
1477 !svg.bottom && (svg.bottom = this);
1478 this.prev = svg.top;
1479 svg.top && (svg.top.next = this);
1483 Element[proto].rotate = function (deg, cx, cy) {
1489 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1491 return this._.rt.deg;
1493 var bbox = this.getBBox();
1494 deg = Str(deg)[split](separator);
1495 if (deg[length] - 1) {
1496 cx = toFloat(deg[1]);
1497 cy = toFloat(deg[2]);
1499 deg = toFloat(deg[0]);
1500 if (cx != null && cx !== false) {
1501 this._.rt.deg = deg;
1503 this._.rt.deg += deg;
1505 (cy == null) && (cx = null);
1508 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1509 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1510 if (this._.rt.deg) {
1511 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1512 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1514 this.transformations[0] = E;
1515 this.clip && $(this.clip, {transform: E});
1517 $(this.node, {transform: this.transformations[join](S)});
1520 Element[proto].hide = function () {
1521 !this.removed && (this.node.style.display = "none");
1524 Element[proto].show = function () {
1525 !this.removed && (this.node.style.display = "");
1528 Element[proto].remove = function () {
1532 tear(this, this.paper);
1533 this.node.parentNode.removeChild(this.node);
1534 for (var i in this) {
1537 this.removed = true;
1539 Element[proto].getBBox = function () {
1543 if (this.type == "path") {
1544 return pathDimensions(this.attrs.path);
1546 if (this.node.style.display == "none") {
1552 bbox = this.node.getBBox();
1554 // Firefox 3.0.x plays badly here
1558 if (this.type == "text") {
1559 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1560 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1561 var bb = this.node.getExtentOfChar(i);
1562 (bb.y < bbox.y) && (bbox.y = bb.y);
1563 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1564 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1567 hide && this.hide();
1570 Element[proto].attr = function (name, value) {
1576 for (var i in this.attrs) if (this.attrs[has](i)) {
1577 res[i] = this.attrs[i];
1579 this._.rt.deg && (res.rotation = this.rotate());
1580 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1581 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1584 if (value == null && R.is(name, string)) {
1585 if (name == "translation") {
1586 return translate.call(this);
1588 if (name == "rotation") {
1589 return this.rotate();
1591 if (name == "scale") {
1592 return this.scale();
1594 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1595 return this.attrs.gradient;
1597 return this.attrs[name];
1599 if (value == null && R.is(name, array)) {
1601 for (var j = 0, jj = name.length; j < jj; j++) {
1602 values[name[j]] = this.attr(name[j]);
1606 if (value != null) {
1608 params[name] = value;
1609 } else if (name != null && R.is(name, "object")) {
1612 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1613 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
1614 this.attrs[key] = params[key];
1615 for (var subkey in par) if (par[has](subkey)) {
1616 params[subkey] = par[subkey];
1619 setFillAndStroke(this, params);
1622 Element[proto].toFront = function () {
1626 this.node.parentNode[appendChild](this.node);
1627 var svg = this.paper;
1628 svg.top != this && tofront(this, svg);
1631 Element[proto].toBack = function () {
1635 if (this.node.parentNode.firstChild != this.node) {
1636 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1637 toback(this, this.paper);
1638 var svg = this.paper;
1642 Element[proto].insertAfter = function (element) {
1646 var node = element.node || element[element.length - 1].node;
1647 if (node.nextSibling) {
1648 node.parentNode.insertBefore(this.node, node.nextSibling);
1650 node.parentNode[appendChild](this.node);
1652 insertafter(this, element, this.paper);
1655 Element[proto].insertBefore = function (element) {
1659 var node = element.node || element[0].node;
1660 node.parentNode.insertBefore(this.node, node);
1661 insertbefore(this, element, this.paper);
1664 Element[proto].blur = function (size) {
1665 // Experimental. No Safari support. Use it on your own risk.
1668 var fltr = $("filter"),
1669 blur = $("feGaussianBlur");
1670 t.attrs.blur = size;
1671 fltr.id = "r" + (R._id++)[toString](36);
1672 $(blur, {stdDeviation: +size || 1.5});
1673 fltr.appendChild(blur);
1674 t.paper.defs.appendChild(fltr);
1676 $(t.node, {filter: "url(#" + fltr.id + ")"});
1679 t._blur.parentNode.removeChild(t._blur);
1681 delete t.attrs.blur;
1683 t.node.removeAttribute("filter");
1686 var theCircle = function (svg, x, y, r) {
1687 var el = $("circle");
1688 svg.canvas && svg.canvas[appendChild](el);
1689 var res = new Element(el, svg);
1690 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1691 res.type = "circle";
1695 var theRect = function (svg, x, y, w, h, r) {
1697 svg.canvas && svg.canvas[appendChild](el);
1698 var res = new Element(el, svg);
1699 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1704 var theEllipse = function (svg, x, y, rx, ry) {
1705 var el = $("ellipse");
1706 svg.canvas && svg.canvas[appendChild](el);
1707 var res = new Element(el, svg);
1708 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1709 res.type = "ellipse";
1713 var theImage = function (svg, src, x, y, w, h) {
1714 var el = $("image");
1715 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1716 el.setAttributeNS(svg.xlink, "href", src);
1717 svg.canvas && svg.canvas[appendChild](el);
1718 var res = new Element(el, svg);
1719 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1723 var theText = function (svg, x, y, text) {
1725 $(el, {x: x, y: y, "text-anchor": "middle"});
1726 svg.canvas && svg.canvas[appendChild](el);
1727 var res = new Element(el, svg);
1728 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1730 setFillAndStroke(res, res.attrs);
1733 var setSize = function (width, height) {
1734 this.width = width || this.width;
1735 this.height = height || this.height;
1736 this.canvas[setAttribute]("width", this.width);
1737 this.canvas[setAttribute]("height", this.height);
1740 var create = function () {
1741 var con = getContainer[apply](0, arguments),
1742 container = con && con.container,
1746 height = con.height;
1748 throw new Error("SVG container not found.");
1750 var cnvs = $("svg");
1753 width = width || 512;
1754 height = height || 342;
1756 xmlns: "http://www.w3.org/2000/svg",
1761 if (container == 1) {
1762 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1763 doc.body[appendChild](cnvs);
1765 if (container.firstChild) {
1766 container.insertBefore(cnvs, container.firstChild);
1768 container[appendChild](cnvs);
1771 container = new Paper;
1772 container.width = width;
1773 container.height = height;
1774 container.canvas = cnvs;
1775 plugins.call(container, container, R.fn);
1779 Paper[proto].clear = function () {
1780 var c = this.canvas;
1781 while (c.firstChild) {
1782 c.removeChild(c.firstChild);
1784 this.bottom = this.top = null;
1785 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1786 c[appendChild](this.desc);
1787 c[appendChild](this.defs = $("defs"));
1789 Paper[proto].remove = function () {
1790 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1791 for (var i in this) {
1792 this[i] = removed(i);
1799 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1800 bites = /([clmz]),?([^clmz]*)/gi,
1801 val = /-?[^,\s-]+/g,
1802 coordsize = 1e3 + S + 1e3,
1804 pathlike = {path: 1, rect: 1},
1805 path2vml = function (path) {
1806 var total = /[ahqstv]/ig,
1807 command = pathToAbsolute;
1808 Str(path).match(total) && (command = path2curve);
1810 if (command == pathToAbsolute && !Str(path).match(total)) {
1811 var res = Str(path)[rp](bites, function (all, command, args) {
1813 isMove = lowerCase.call(command) == "m",
1815 args[rp](val, function (value) {
1816 if (isMove && vals[length] == 2) {
1817 res += vals + map[command == "m" ? "l" : "L"];
1820 vals[push](round(value * zoom));
1826 var pa = command(path), p, r;
1828 for (var i = 0, ii = pa[length]; i < ii; i++) {
1830 r = lowerCase.call(pa[i][0]);
1831 r == "z" && (r = "x");
1832 for (var j = 1, jj = p[length]; j < jj; j++) {
1833 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1837 return res[join](S);
1840 R[toString] = function () {
1841 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1843 thePath = function (pathString, vml) {
1844 var g = createNode("group");
1845 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1846 g.coordsize = vml.coordsize;
1847 g.coordorigin = vml.coordorigin;
1848 var el = createNode("shape"), ol = el.style;
1849 ol.width = vml.width + "px";
1850 ol.height = vml.height + "px";
1851 el.coordsize = coordsize;
1852 el.coordorigin = vml.coordorigin;
1854 var p = new Element(el, g, vml),
1855 attr = {fill: "none", stroke: "#000"};
1856 pathString && (attr.path = pathString);
1860 setFillAndStroke(p, attr);
1861 vml.canvas[appendChild](g);
1864 setFillAndStroke = function (o, params) {
1865 o.attrs = o.attrs || {};
1870 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1873 for (var par in params) if (params[has](par)) {
1874 a[par] = params[par];
1877 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1883 params.href && (node.href = params.href);
1884 params.title && (node.title = params.title);
1885 params.target && (node.target = params.target);
1886 params.cursor && (s.cursor = params.cursor);
1887 "blur" in params && o.blur(params.blur);
1888 if (params.path && o.type == "path" || newpath) {
1889 node.path = path2vml(a.path);
1891 if (params.rotation != null) {
1892 o.rotate(params.rotation, true);
1894 if (params.translation) {
1895 xy = Str(params.translation)[split](separator);
1896 translate.call(o, xy[0], xy[1]);
1897 if (o._.rt.cx != null) {
1898 o._.rt.cx +=+ xy[0];
1899 o._.rt.cy +=+ xy[1];
1900 o.setBox(o.attrs, xy[0], xy[1]);
1904 xy = Str(params.scale)[split](separator);
1905 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1907 if ("clip-rect" in params) {
1908 var rect = Str(params["clip-rect"])[split](separator);
1909 if (rect[length] == 4) {
1910 rect[2] = +rect[2] + (+rect[0]);
1911 rect[3] = +rect[3] + (+rect[1]);
1912 var div = node.clipRect || doc.createElement("div"),
1914 group = node.parentNode;
1915 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1916 if (!node.clipRect) {
1917 dstyle.position = "absolute";
1920 dstyle.width = o.paper.width + "px";
1921 dstyle.height = o.paper.height + "px";
1922 group.parentNode.insertBefore(div, group);
1923 div[appendChild](group);
1924 node.clipRect = div;
1927 if (!params["clip-rect"]) {
1928 node.clipRect && (node.clipRect.style.clip = E);
1931 if (o.type == "image" && params.src) {
1932 node.src = params.src;
1934 if (o.type == "image" && params.opacity) {
1935 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1936 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1938 params.font && (s.font = params.font);
1939 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1940 params["font-size"] && (s.fontSize = params["font-size"]);
1941 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1942 params["font-style"] && (s.fontStyle = params["font-style"]);
1943 if (params.opacity != null ||
1944 params["stroke-width"] != null ||
1945 params.fill != null ||
1946 params.stroke != null ||
1947 params["stroke-width"] != null ||
1948 params["stroke-opacity"] != null ||
1949 params["fill-opacity"] != null ||
1950 params["stroke-dasharray"] != null ||
1951 params["stroke-miterlimit"] != null ||
1952 params["stroke-linejoin"] != null ||
1953 params["stroke-linecap"] != null) {
1954 node = o.shape || node;
1955 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1957 !fill && (newfill = fill = createNode(fillString));
1958 if ("fill-opacity" in params || "opacity" in params) {
1959 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1960 opacity < 0 && (opacity = 0);
1961 opacity > 1 && (opacity = 1);
1962 fill.opacity = opacity;
1964 params.fill && (fill.on = true);
1965 if (fill.on == null || params.fill == "none") {
1968 if (fill.on && params.fill) {
1969 var isURL = params.fill.match(ISURL);
1971 fill.src = isURL[1];
1974 fill.color = R.getRGB(params.fill).hex;
1976 fill.type = "solid";
1977 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
1979 a.gradient = params.fill;
1983 newfill && node[appendChild](fill);
1984 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1986 !stroke && (newstroke = stroke = createNode("stroke"));
1987 if ((params.stroke && params.stroke != "none") ||
1988 params["stroke-width"] ||
1989 params["stroke-opacity"] != null ||
1990 params["stroke-dasharray"] ||
1991 params["stroke-miterlimit"] ||
1992 params["stroke-linejoin"] ||
1993 params["stroke-linecap"]) {
1996 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1997 var strokeColor = R.getRGB(params.stroke);
1998 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
1999 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
2000 var width = (toFloat(params["stroke-width"]) || 1) * .75;
2001 opacity < 0 && (opacity = 0);
2002 opacity > 1 && (opacity = 1);
2003 params["stroke-width"] == null && (width = a["stroke-width"]);
2004 params["stroke-width"] && (stroke.weight = width);
2005 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
2006 stroke.opacity = opacity;
2008 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
2009 stroke.miterlimit = params["stroke-miterlimit"] || 8;
2010 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
2011 if (params["stroke-dasharray"]) {
2015 "-.": "shortdashdot",
2016 "-..": "shortdashdotdot",
2021 "--.": "longdashdot",
2022 "--..": "longdashdotdot"
2024 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
2026 newstroke && node[appendChild](stroke);
2028 if (res.type == "text") {
2029 s = res.paper.span.style;
2030 a.font && (s.font = a.font);
2031 a["font-family"] && (s.fontFamily = a["font-family"]);
2032 a["font-size"] && (s.fontSize = a["font-size"]);
2033 a["font-weight"] && (s.fontWeight = a["font-weight"]);
2034 a["font-style"] && (s.fontStyle = a["font-style"]);
2035 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
2036 res.W = a.w = res.paper.span.offsetWidth;
2037 res.H = a.h = res.paper.span.offsetHeight;
2039 res.Y = a.y + round(res.H / 2);
2041 // text-anchor emulationm
2042 switch (a["text-anchor"]) {
2044 res.node.style["v-text-align"] = "left";
2045 res.bbx = round(res.W / 2);
2048 res.node.style["v-text-align"] = "right";
2049 res.bbx = -round(res.W / 2);
2052 res.node.style["v-text-align"] = "center";
2057 addGradientFill = function (o, gradient) {
2058 o.attrs = o.attrs || {};
2059 var attrs = o.attrs,
2063 o.attrs.gradient = gradient;
2064 gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
2069 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
2074 gradient = gradient[split](/\s*\-\s*/);
2075 if (type == "linear") {
2076 var angle = gradient.shift();
2077 angle = -toFloat(angle);
2082 var dots = parseDots(gradient);
2086 o = o.shape || o.node;
2087 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
2088 !fill.parentNode && o.appendChild(fill);
2091 fill.method = "none";
2092 fill.color = dots[0].color;
2093 fill.color2 = dots[dots[length] - 1].color;
2095 for (var i = 0, ii = dots[length]; i < ii; i++) {
2096 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
2098 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
2099 if (type == "radial") {
2100 fill.type = "gradientradial";
2101 fill.focus = "100%";
2102 fill.focussize = fxfy;
2103 fill.focusposition = fxfy;
2105 fill.type = "gradient";
2106 fill.angle = (270 - angle) % 360;
2111 Element = function (node, group, vml) {
2119 node.raphael = this;
2132 !vml.bottom && (vml.bottom = this);
2133 this.prev = vml.top;
2134 vml.top && (vml.top.next = this);
2138 Element[proto].rotate = function (deg, cx, cy) {
2144 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2146 return this._.rt.deg;
2148 deg = Str(deg)[split](separator);
2149 if (deg[length] - 1) {
2150 cx = toFloat(deg[1]);
2151 cy = toFloat(deg[2]);
2153 deg = toFloat(deg[0]);
2155 this._.rt.deg = deg;
2157 this._.rt.deg += deg;
2159 cy == null && (cx = null);
2162 this.setBox(this.attrs, cx, cy);
2163 this.Group.style.rotation = this._.rt.deg;
2164 // gradient fix for rotation. TODO
2165 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2166 // fill = fill[0] || {};
2167 // var b = ((360 - this._.rt.deg) - 270) % 360;
2168 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2171 Element[proto].setBox = function (params, cx, cy) {
2175 var gs = this.Group.style,
2176 os = (this.shape && this.shape.style) || this.node.style;
2177 params = params || {};
2178 for (var i in params) if (params[has](i)) {
2179 this.attrs[i] = params[i];
2181 cx = cx || this._.rt.cx;
2182 cy = cy || this._.rt.cy;
2183 var attr = this.attrs,
2188 switch (this.type) {
2190 x = attr.cx - attr.r;
2191 y = attr.cy - attr.r;
2195 x = attr.cx - attr.rx;
2196 y = attr.cy - attr.ry;
2203 w = attr.width || 0;
2204 h = attr.height || 0;
2207 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2208 x = attr.x - round(this.W / 2);
2209 y = attr.y - this.H / 2;
2215 if (!this.attrs.path) {
2218 w = this.paper.width;
2219 h = this.paper.height;
2221 var dim = pathDimensions(this.attrs.path);
2231 w = this.paper.width;
2232 h = this.paper.height;
2235 cx = (cx == null) ? x + w / 2 : cx;
2236 cy = (cy == null) ? y + h / 2 : cy;
2237 var left = cx - this.paper.width / 2,
2238 top = cy - this.paper.height / 2, t;
2239 gs.left != (t = left + "px") && (gs.left = t);
2240 gs.top != (t = top + "px") && (gs.top = t);
2241 this.X = pathlike[has](this.type) ? -left : x;
2242 this.Y = pathlike[has](this.type) ? -top : y;
2245 if (pathlike[has](this.type)) {
2246 os.left != (t = -left * zoom + "px") && (os.left = t);
2247 os.top != (t = -top * zoom + "px") && (os.top = t);
2248 } else if (this.type == "text") {
2249 os.left != (t = -left + "px") && (os.left = t);
2250 os.top != (t = -top + "px") && (os.top = t);
2252 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2253 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2254 os.left != (t = x - left + "px") && (os.left = t);
2255 os.top != (t = y - top + "px") && (os.top = t);
2256 os.width != (t = w + "px") && (os.width = t);
2257 os.height != (t = h + "px") && (os.height = t);
2260 Element[proto].hide = function () {
2261 !this.removed && (this.Group.style.display = "none");
2264 Element[proto].show = function () {
2265 !this.removed && (this.Group.style.display = "block");
2268 Element[proto].getBBox = function () {
2272 if (pathlike[has](this.type)) {
2273 return pathDimensions(this.attrs.path);
2276 x: this.X + (this.bbx || 0),
2282 Element[proto].remove = function () {
2286 tear(this, this.paper);
2287 this.node.parentNode.removeChild(this.node);
2288 this.Group.parentNode.removeChild(this.Group);
2289 this.shape && this.shape.parentNode.removeChild(this.shape);
2290 for (var i in this) {
2293 this.removed = true;
2295 Element[proto].attr = function (name, value) {
2301 for (var i in this.attrs) if (this.attrs[has](i)) {
2302 res[i] = this.attrs[i];
2304 this._.rt.deg && (res.rotation = this.rotate());
2305 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2306 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2309 if (value == null && R.is(name, "string")) {
2310 if (name == "translation") {
2311 return translate.call(this);
2313 if (name == "rotation") {
2314 return this.rotate();
2316 if (name == "scale") {
2317 return this.scale();
2319 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2320 return this.attrs.gradient;
2322 return this.attrs[name];
2324 if (this.attrs && value == null && R.is(name, array)) {
2325 var ii, values = {};
2326 for (i = 0, ii = name[length]; i < ii; i++) {
2327 values[name[i]] = this.attr(name[i]);
2332 if (value != null) {
2334 params[name] = value;
2336 value == null && R.is(name, "object") && (params = name);
2338 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
2339 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
2340 this.attrs[key] = params[key];
2341 for (var subkey in par) if (par[has](subkey)) {
2342 params[subkey] = par[subkey];
2345 if (params.text && this.type == "text") {
2346 this.node.string = params.text;
2348 setFillAndStroke(this, params);
2349 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2350 addGradientFill(this, params.gradient);
2352 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2356 Element[proto].toFront = function () {
2357 !this.removed && this.Group.parentNode[appendChild](this.Group);
2358 this.paper.top != this && tofront(this, this.paper);
2361 Element[proto].toBack = function () {
2365 if (this.Group.parentNode.firstChild != this.Group) {
2366 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2367 toback(this, this.paper);
2371 Element[proto].insertAfter = function (element) {
2375 if (element.constructor == Set) {
2376 element = element[element.length];
2378 if (element.Group.nextSibling) {
2379 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2381 element.Group.parentNode[appendChild](this.Group);
2383 insertafter(this, element, this.paper);
2386 Element[proto].insertBefore = function (element) {
2390 if (element.constructor == Set) {
2391 element = element[0];
2393 element.Group.parentNode.insertBefore(this.Group, element.Group);
2394 insertbefore(this, element, this.paper);
2397 var blurregexp = / progid:\S+Blur\([^\)]+\)/g;
2398 Element[proto].blur = function (size) {
2399 var s = this.node.runtimeStyle,
2401 f = f.replace(blurregexp, E);
2403 this.attrs.blur = size;
2404 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2405 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
2409 delete this.attrs.blur;
2413 theCircle = function (vml, x, y, r) {
2414 var g = createNode("group"),
2415 o = createNode("oval"),
2417 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2418 g.coordsize = coordsize;
2419 g.coordorigin = vml.coordorigin;
2421 var res = new Element(o, g, vml);
2422 res.type = "circle";
2423 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2427 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2428 vml.canvas[appendChild](g);
2431 function rectPath(x, y, w, h, r) {
2433 return R.format("M{0},{1}l{2},0a{3},{3},0,0,1,{3},{3}l0,{5}a{3},{3},0,0,1,{4},{3}l{6},0a{3},{3},0,0,1,{4},{4}l0,{7}a{3},{3},0,0,1,{3},{4}z", x + r, y, w - r * 2, r, -r, h - r * 2, r * 2 - w, r * 2 - h);
2435 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2438 theRect = function (vml, x, y, w, h, r) {
2439 var path = rectPath(x, y, w, h, r),
2440 res = vml.path(path),
2444 res.W = a.width = w;
2445 res.H = a.height = h;
2451 theEllipse = function (vml, x, y, rx, ry) {
2452 var g = createNode("group"),
2453 o = createNode("oval"),
2455 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2456 g.coordsize = coordsize;
2457 g.coordorigin = vml.coordorigin;
2459 var res = new Element(o, g, vml);
2460 res.type = "ellipse";
2461 setFillAndStroke(res, {stroke: "#000"});
2466 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2467 vml.canvas[appendChild](g);
2470 theImage = function (vml, src, x, y, w, h) {
2471 var g = createNode("group"),
2472 o = createNode("image");
2473 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2474 g.coordsize = coordsize;
2475 g.coordorigin = vml.coordorigin;
2478 var res = new Element(o, g, vml);
2480 res.attrs.src = src;
2485 res.setBox({x: x, y: y, width: w, height: h});
2486 vml.canvas[appendChild](g);
2489 theText = function (vml, x, y, text) {
2490 var g = createNode("group"),
2491 el = createNode("shape"),
2493 path = createNode("path"),
2495 o = createNode("textpath");
2496 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2497 g.coordsize = coordsize;
2498 g.coordorigin = vml.coordorigin;
2499 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2500 path.textpathok = true;
2501 ol.width = vml.width;
2502 ol.height = vml.height;
2503 o.string = Str(text);
2506 el[appendChild](path);
2508 var res = new Element(o, g, vml);
2510 res.textpath = path;
2512 res.attrs.text = text;
2517 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2519 vml.canvas[appendChild](g);
2522 setSize = function (width, height) {
2523 var cs = this.canvas.style;
2524 width == +width && (width += "px");
2525 height == +height && (height += "px");
2528 cs.clip = "rect(0 " + width + " " + height + " 0)";
2532 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2534 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2535 createNode = function (tagName) {
2536 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2539 createNode = function (tagName) {
2540 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2543 create = function () {
2544 var con = getContainer[apply](0, arguments),
2545 container = con.container,
2546 height = con.height,
2552 throw new Error("VML container not found.");
2554 var res = new Paper,
2555 c = res.canvas = doc.createElement("div"),
2559 width = width || 512;
2560 height = height || 342;
2561 width == +width && (width += "px");
2562 height == +height && (height += "px");
2565 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2566 res.coordorigin = "0 0";
2567 res.span = doc.createElement("span");
2568 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2569 c[appendChild](res.span);
2570 cs.cssText = R.format("width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2571 if (container == 1) {
2572 doc.body[appendChild](c);
2575 cs.position = "absolute";
2577 if (container.firstChild) {
2578 container.insertBefore(c, container.firstChild);
2580 container[appendChild](c);
2583 plugins.call(res, res, R.fn);
2586 Paper[proto].clear = function () {
2587 this.canvas.innerHTML = E;
2588 this.span = doc.createElement("span");
2589 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2590 this.canvas[appendChild](this.span);
2591 this.bottom = this.top = null;
2593 Paper[proto].remove = function () {
2594 this.canvas.parentNode.removeChild(this.canvas);
2595 for (var i in this) {
2596 this[i] = removed(i);
2603 // WebKit rendering bug workaround method
2604 var version = navigator.userAgent.match(/Version\/(.*?)\s/);
2605 if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP")) {
2606 Paper[proto].safari = function () {
2607 var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
2608 win.setTimeout(function () {rect.remove();});
2611 Paper[proto].safari = function () {};
2615 var preventDefault = function () {
2616 this.returnValue = false;
2618 preventTouch = function () {
2619 return this.originalEvent.preventDefault();
2621 stopPropagation = function () {
2622 this.cancelBubble = true;
2624 stopTouch = function () {
2625 return this.originalEvent.stopPropagation();
2627 addEvent = (function () {
2628 if (doc.addEventListener) {
2629 return function (obj, type, fn, element) {
2630 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2631 var f = function (e) {
2632 if (supportsTouch && touchMap[has](type)) {
2633 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2634 if (e.targetTouches[i].target == obj) {
2636 e = e.targetTouches[i];
2637 e.originalEvent = olde;
2638 e.preventDefault = preventTouch;
2639 e.stopPropagation = stopTouch;
2644 return fn.call(element, e);
2646 obj.addEventListener(realName, f, false);
2647 return function () {
2648 obj.removeEventListener(realName, f, false);
2652 } else if (doc.attachEvent) {
2653 return function (obj, type, fn, element) {
2654 var f = function (e) {
2656 e.preventDefault = e.preventDefault || preventDefault;
2657 e.stopPropagation = e.stopPropagation || stopPropagation;
2658 return fn.call(element, e);
2660 obj.attachEvent("on" + type, f);
2661 var detacher = function () {
2662 obj.detachEvent("on" + type, f);
2670 dragMove = function (e) {
2673 scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2674 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft,
2679 if (supportsTouch) {
2680 var i = e.touches.length,
2683 touch = e.touches[i];
2684 if (touch.identifier == dragi.el._drag.id) {
2687 (e.originalEvent ? e.originalEvent : e).preventDefault();
2696 dragi.move && dragi.move.call(dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y);
2699 dragUp = function () {
2700 R.unmousemove(dragMove).unmouseup(dragUp);
2701 var i = drag.length,
2705 dragi.el._drag = {};
2706 dragi.end && dragi.end.call(dragi.el);
2710 for (var i = events[length]; i--;) {
2711 (function (eventName) {
2712 R[eventName] = Element[proto][eventName] = function (fn) {
2713 if (R.is(fn, "function")) {
2714 this.events = this.events || [];
2715 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, this)});
2719 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2720 var events = this.events,
2722 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2724 events.splice(l, 1);
2725 !events.length && delete this.events;
2732 Element[proto].hover = function (f_in, f_out) {
2733 return this.mouseover(f_in).mouseout(f_out);
2735 Element[proto].unhover = function (f_in, f_out) {
2736 return this.unmouseover(f_in).unmouseout(f_out);
2738 Element[proto].drag = function (onmove, onstart, onend) {
2740 this.mousedown(function (e) {
2741 (e.originalEvent || e).preventDefault();
2742 var scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2743 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft;
2744 this._drag.x = e.clientX + scrollX;
2745 this._drag.y = e.clientY + scrollY;
2746 this._drag.id = e.identifier;
2747 onstart && onstart.call(this, e.clientX + scrollX, e.clientY + scrollY);
2748 !drag.length && R.mousemove(dragMove).mouseup(dragUp);
2749 drag.push({el: this, move: onmove, end: onend});
2753 Element[proto].undrag = function (onmove, onstart, onend) {
2754 var i = drag.length;
2756 drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i, 1);
2757 !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
2760 Paper[proto].circle = function (x, y, r) {
2761 return theCircle(this, x || 0, y || 0, r || 0);
2763 Paper[proto].rect = function (x, y, w, h, r) {
2764 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2766 Paper[proto].ellipse = function (x, y, rx, ry) {
2767 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2769 Paper[proto].path = function (pathString) {
2770 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2771 return thePath(R.format[apply](R, arguments), this);
2773 Paper[proto].image = function (src, x, y, w, h) {
2774 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2776 Paper[proto].text = function (x, y, text) {
2777 return theText(this, x || 0, y || 0, Str(text));
2779 Paper[proto].set = function (itemsArray) {
2780 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2781 return new Set(itemsArray);
2783 Paper[proto].setSize = setSize;
2784 Paper[proto].top = Paper[proto].bottom = null;
2785 Paper[proto].raphael = R;
2787 return this.x + S + this.y;
2789 Element[proto].resetScale = function () {
2795 this.attrs.scale = "1 1";
2797 Element[proto].scale = function (x, y, cx, cy) {
2801 if (x == null && y == null) {
2816 var bb = this.getBBox(),
2817 rcx = bb.x + bb.width / 2,
2818 rcy = bb.y + bb.height / 2,
2821 cx = (+cx || cx == 0) ? cx : rcx;
2822 cy = (+cy || cy == 0) ? cy : rcy;
2823 var dirx = ~~(x / math.abs(x)),
2824 diry = ~~(y / math.abs(y)),
2825 s = this.node.style,
2826 ncx = cx + (rcx - cx) * kx,
2827 ncy = cy + (rcy - cy) * ky;
2828 switch (this.type) {
2831 var neww = a.width * dirx * kx,
2832 newh = a.height * diry * ky;
2835 r: a.r * mmin(dirx * kx, diry * ky),
2844 rx: a.rx * dirx * kx,
2845 ry: a.ry * diry * ky,
2846 r: a.r * mmin(dirx * kx, diry * ky),
2858 var path = pathToRelative(a.path),
2860 for (var i = 0, ii = path[length]; i < ii; i++) {
2862 P0 = upperCase.call(p[0]);
2863 if (P0 == "M" && skip) {
2869 p[path[i][length] - 2] *= kx;
2870 p[path[i][length] - 1] *= ky;
2873 p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
2874 } else if (P0 == "H") {
2875 for (var j = 1, jj = p[length]; j < jj; j++) {
2878 } else if (P0 == "V") {
2879 for (j = 1, jj = p[length]; j < jj; j++) {
2883 for (j = 1, jj = p[length]; j < jj; j++) {
2884 p[j] *= (j % 2) ? kx : ky;
2888 var dim2 = pathDimensions(path);
2889 dx = ncx - dim2.x - dim2.width / 2;
2890 dy = ncy - dim2.y - dim2.height / 2;
2893 this.attr({path: path});
2896 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2897 if (this.transformations) {
2898 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2899 this.node[setAttribute]("transform", this.transformations[join](S));
2900 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2901 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2902 this.attr({x: dx, y: dy});
2906 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2907 ", M12=0, M21=0, M22=", diry,
2908 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2909 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2912 if (this.transformations) {
2913 this.transformations[2] = E;
2914 this.node[setAttribute]("transform", this.transformations[join](S));
2918 this.node.filterMatrix = E;
2919 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2922 a.scale = [x, y, cx, cy][join](S);
2928 Element[proto].clone = function () {
2932 var attr = this.attr();
2934 delete attr.translation;
2935 return this.paper[this.type]().attr(attr);
2937 var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2940 for (var i = 0; i < 1.01; i+=.01) {
2941 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2942 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2943 if (len >= length) {
2949 getLengthFactory = function (istotal, subpath) {
2950 return function (path, length, onlystart) {
2951 path = path2curve(path);
2952 var x, y, p, l, sp = "", subpaths = {}, point,
2954 for (var i = 0, ii = path.length; i < ii; i++) {
2960 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2961 if (len + l > length) {
2962 if (subpath && !subpaths.start) {
2963 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2964 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2965 if (onlystart) {return sp;}
2966 subpaths.start = sp;
2967 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2973 if (!istotal && !subpath) {
2974 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2975 return {x: point.x, y: point.y, alpha: point.alpha};
2985 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2986 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2990 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2991 var old = {x: 0, y: 0},
2993 for (var i = 0; i < 1.01; i+=.01) {
2994 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2995 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
3000 var getTotalLength = getLengthFactory(1),
3001 getPointAtLength = getLengthFactory(),
3002 getSubpathsAtLength = getLengthFactory(0, 1);
3003 Element[proto].getTotalLength = function () {
3004 if (this.type != "path") {return;}
3005 if (this.node.getTotalLength) {
3006 return this.node.getTotalLength();
3008 return getTotalLength(this.attrs.path);
3010 Element[proto].getPointAtLength = function (length) {
3011 if (this.type != "path") {return;}
3012 if (this.node.getPointAtLength) {
3013 return this.node.getPointAtLength(length);
3015 return getPointAtLength(this.attrs.path, length);
3017 Element[proto].getSubpath = function (from, to) {
3018 if (this.type != "path") {return;}
3019 if (math.abs(this.getTotalLength() - to) < 1e-6) {
3020 return getSubpathsAtLength(this.attrs.path, from).end;
3022 var a = getSubpathsAtLength(this.attrs.path, to, 1);
3023 return from ? getSubpathsAtLength(a, from).end : a;
3026 // animation easing formulas
3027 R.easing_formulas = {
3028 linear: function (n) {
3035 return pow(n - 1, 3) + 1;
3037 "<>": function (n) {
3040 return pow(n, 3) / 2;
3043 return (pow(n, 3) + 2) / 2;
3045 backIn: function (n) {
3047 return n * n * ((s + 1) * n - s);
3049 backOut: function (n) {
3052 return n * n * ((s + 1) * n + s) + 1;
3054 elastic: function (n) {
3055 if (n == 0 || n == 1) {
3060 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
3062 bounce: function (n) {
3071 l = s * n * n + .75;
3073 if (n < (2.5 / p)) {
3075 l = s * n * n + .9375;
3078 l = s * n * n + .984375;
3086 var animationElements = {length : 0},
3087 animation = function () {
3088 var Now = +new Date;
3089 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
3090 var e = animationElements[l];
3091 if (e.stop || e.el.removed) {
3092 delete animationElements[l];
3093 animationElements[length]--;
3096 var time = Now - e.start,
3104 callback = e.callback,
3108 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
3109 for (var attr in from) if (from[has](attr)) {
3110 switch (availableAnimAttrs[attr]) {
3112 now = pos * ms * diff[attr];
3113 to.back && (now = to.len - now);
3114 var point = getPointAtLength(to[attr], now);
3115 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
3118 that.translate(point.x - diff.sx, point.y - diff.sy);
3119 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3122 now = +from[attr] + pos * ms * diff[attr];
3126 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
3127 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
3128 upto255(round(from[attr].b + pos * ms * diff[attr].b))
3133 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3134 now[i] = [from[attr][i][0]];
3135 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3136 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
3138 now[i] = now[i][join](S);
3145 var x = pos * ms * diff[attr][0] - t.x,
3146 y = pos * ms * diff[attr][1] - t.y;
3152 now = +from[attr][0] + pos * ms * diff[attr][0];
3153 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
3156 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);
3162 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3168 var from2 = [].concat(from[attr]);
3170 i = that.paper.customAttributes[attr].length;
3172 now[i] = +from2[i] + pos * ms * diff[attr][i];
3179 that._run && that._run.call(that);
3182 point = getPointAtLength(to.along, to.len * !to.back);
3183 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3184 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3186 (t.x || t.y) && that.translate(-t.x, -t.y);
3187 to.scale && (to.scale += E);
3189 delete animationElements[l];
3190 animationElements[length]--;
3191 that.in_animation = null;
3192 R.is(callback, "function") && setTimeout(function () {
3193 callback.call(that);
3197 R.svg && that && that.paper && that.paper.safari();
3198 animationElements[length] && setTimeout(animation);
3200 upto255 = function (color) {
3201 return mmax(mmin(color, 255), 0);
3203 translate = function (x, y) {
3205 return {x: this._.tx, y: this._.ty, toString: x_y};
3209 switch (this.type) {
3212 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3217 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3220 var path = pathToRelative(this.attrs.path);
3223 this.attr({path: path});
3228 Element[proto].animateWith = function (element, params, ms, easing, callback) {
3229 animationElements[element.id] && (params.start = animationElements[element.id].start);
3230 return this.animate(params, ms, easing, callback);
3232 Element[proto].animateAlong = along();
3233 Element[proto].animateAlongBack = along(1);
3234 function along(isBack) {
3235 return function (path, ms, rotate, callback) {
3236 var params = {back: isBack};
3237 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3238 path && path.constructor == Element && (path = path.attrs.path);
3239 path && (params.along = path);
3240 return this.animate(params, ms, callback);
3243 Element[proto].onAnimation = function (f) {
3247 Element[proto].animate = function (params, ms, easing, callback) {
3248 if (R.is(easing, "function") || !easing) {
3249 callback = easing || null;
3252 callback && callback.call(this);
3258 for (var attr in params) if (params[has](attr)) {
3259 if (availableAnimAttrs[has](attr) || this.paper.customAttributes[has](attr)) {
3260 from[attr] = this.attr(attr);
3261 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3262 to[attr] = params[attr];
3263 switch (availableAnimAttrs[attr]) {
3265 var len = getTotalLength(params[attr]);
3266 var point = getPointAtLength(params[attr], len * !!params.back);
3267 var bb = this.getBBox();
3268 diff[attr] = len / ms;
3273 to.rot = params.rot;
3274 to.back = params.back;
3276 params.rot && (diff.r = toFloat(this.rotate()) || 0);
3279 diff[attr] = (to[attr] - from[attr]) / ms;
3282 from[attr] = R.getRGB(from[attr]);
3283 var toColour = R.getRGB(to[attr]);
3285 r: (toColour.r - from[attr].r) / ms,
3286 g: (toColour.g - from[attr].g) / ms,
3287 b: (toColour.b - from[attr].b) / ms
3291 var pathes = path2curve(from[attr], to[attr]);
3292 from[attr] = pathes[0];
3293 var toPath = pathes[1];
3295 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3296 diff[attr][i] = [0];
3297 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3298 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3303 var values = Str(params[attr])[split](separator),
3304 from2 = Str(from[attr])[split](separator);
3307 from[attr] = [0, 0];
3308 diff[attr] = [values[0] / ms, values[1] / ms];
3311 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3312 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3315 params[attr] = values;
3316 from[attr] = Str(from[attr])[split](separator);
3317 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3320 from[attr] = Str(from[attr])[split](separator);
3324 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3331 values = [].concat(params[attr]);
3332 from2 = [].concat(from[attr]);
3334 i = this.paper.customAttributes[attr].length;
3336 diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
3343 this.in_animation = 1;
3344 animationElements[this.id] = {
3345 start: params.start || +new Date,
3355 ++animationElements[length] == 1 && setTimeout(animation);
3358 Element[proto].stop = function () {
3359 animationElements[this.id] && animationElements[length]--;
3360 delete animationElements[this.id];
3363 Element[proto].translate = function (x, y) {
3364 return this.attr({translation: x + " " + y});
3366 Element[proto][toString] = function () {
3367 return "Rapha\xebl\u2019s object";
3369 R.ae = animationElements;
3372 var Set = function (items) {
3377 for (var i = 0, ii = items[length]; i < ii; i++) {
3378 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3379 this[this.items[length]] = this.items[this.items[length]] = items[i];
3385 Set[proto][push] = function () {
3388 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3389 item = arguments[i];
3390 if (item && (item.constructor == Element || item.constructor == Set)) {
3391 len = this.items[length];
3392 this[len] = this.items[len] = item;
3398 Set[proto].pop = function () {
3399 delete this[this[length]--];
3400 return this.items.pop();
3402 for (var method in Element[proto]) if (Element[proto][has](method)) {
3403 Set[proto][method] = (function (methodname) {
3404 return function () {
3405 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3406 this.items[i][methodname][apply](this.items[i], arguments);
3412 Set[proto].attr = function (name, value) {
3413 if (name && R.is(name, array) && R.is(name[0], "object")) {
3414 for (var j = 0, jj = name[length]; j < jj; j++) {
3415 this.items[j].attr(name[j]);
3418 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3419 this.items[i].attr(name, value);
3424 Set[proto].animate = function (params, ms, easing, callback) {
3425 (R.is(easing, "function") || !easing) && (callback = easing || null);
3426 var len = this.items[length],
3431 callback && (collector = function () {
3432 !--len && callback.call(set);
3434 easing = R.is(easing, string) ? easing : collector;
3435 item = this.items[--i].animate(params, ms, easing, collector);
3437 this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, params, ms, easing, collector);
3441 Set[proto].insertAfter = function (el) {
3442 var i = this.items[length];
3444 this.items[i].insertAfter(el);
3448 Set[proto].getBBox = function () {
3453 for (var i = this.items[length]; i--;) {
3454 var box = this.items[i].getBBox();
3457 w[push](box.x + box.width);
3458 h[push](box.y + box.height);
3460 x = mmin[apply](0, x);
3461 y = mmin[apply](0, y);
3465 width: mmax[apply](0, w) - x,
3466 height: mmax[apply](0, h) - y
3469 Set[proto].clone = function (s) {
3471 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3472 s[push](this.items[i].clone());
3477 R.registerFont = function (font) {
3481 this.fonts = this.fonts || {};
3487 family = font.face["font-family"];
3488 for (var prop in font.face) if (font.face[has](prop)) {
3489 fontcopy.face[prop] = font.face[prop];
3491 if (this.fonts[family]) {
3492 this.fonts[family][push](fontcopy);
3494 this.fonts[family] = [fontcopy];
3497 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3498 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3499 var path = font.glyphs[glyph];
3500 fontcopy.glyphs[glyph] = {
3503 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3504 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3508 for (var k in path.k) if (path[has](k)) {
3509 fontcopy.glyphs[glyph].k[k] = path.k[k];
3516 Paper[proto].getFont = function (family, weight, style, stretch) {
3517 stretch = stretch || "normal";
3518 style = style || "normal";
3519 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3523 var font = R.fonts[family];
3525 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3526 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3527 if (name.test(fontName)) {
3528 font = R.fonts[fontName];
3535 for (var i = 0, ii = font[length]; i < ii; i++) {
3537 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3544 Paper[proto].print = function (x, y, string, font, size, origin) {
3545 origin = origin || "middle"; // baseline|middle
3546 var out = this.set(),
3547 letters = Str(string)[split](E),
3551 R.is(font, string) && (font = this.getFont(font));
3553 scale = (size || 16) / font.face["units-per-em"];
3554 var bb = font.face.bbox.split(separator),
3556 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3557 for (var i = 0, ii = letters[length]; i < ii; i++) {
3558 var prev = i && font.glyphs[letters[i - 1]] || {},
3559 curr = font.glyphs[letters[i]];
3560 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3561 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3563 out.scale(scale, scale, top, height).translate(x - top, y - height);
3568 var formatrg = /\{(\d+)\}/g;
3569 R.format = function (token, params) {
3570 var args = R.is(params, array) ? [0][concat](params) : arguments;
3571 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3572 return args[++i] == null ? E : args[i];
3576 R.ninja = function () {
3577 oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
3580 R.el = Element[proto];