Fixes SVG capability detection bug in Safari 3.0.4 (http://github.com/DmitryBaranovsk...
[raphael] / raphael.js
index 6e56e0f..a7222bb 100644 (file)
@@ -1,28 +1,28 @@
 /*
- * Raphael 0.7.2 - JavaScript Vector Library
+ * Raphael 0.7.4 - JavaScript Vector Library
  *
- * Copyright (c) 2008  2009 Dmitry Baranovskiy (http://raphaeljs.com)
+ * Copyright (c) 2008 - 2009 Dmitry Baranovskiy (http://raphaeljs.com)
  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
  */
 
 
-var Raphael = (function () {
+window.Raphael = (function () {
     var separator = /[, ]+/,
-        create,
         doc = document,
         win = window,
         R = function () {
             return create.apply(R, arguments);
-        };
-    R.version = "0.7.2";
-    R.type = (win.SVGAngle ? "SVG" : "VML");
+        },
+        paper = {},
+        availableAttrs = {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},
+        availableAnimAttrs = {cx: "number", cy: "number", fill: "colour", "fill-opacity": "number", "font-size": "number", height: "number", opacity: "number", path: "path", r: "number", rotation: "csv", rx: "number", ry: "number", scale: "csv", stroke: "colour", "stroke-opacity": "number", "stroke-width": "number", translation: "csv", width: "number", x: "number", y: "number"},
+        events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"];
+    R.version = "0.7.4";
+    R.type = (window.SVGAngle || document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1")
+ ? "SVG" : "VML");
     R.svg = !(R.vml = R.type == "VML");
     R.idGenerator = 0;
-    var paper = {};
     R.fn = {};
-    var availableAttrs = {cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", gradient: 0, height: 0, 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, translation: "0 0", width: 0, x: 0, y: 0},
-        availableAnimAttrs = {cx: "number", cy: "number", fill: "colour", "fill-opacity": "number", "font-size": "number", height: "number", opacity: "number", path: "path", r: "number", rotation: "csv", rx: "number", ry: "number", scale: "csv", stroke: "colour", "stroke-opacity": "number", "stroke-width": "number", translation: "csv", width: "number", x: "number", y: "number"},
-        events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"];
     R.toString = function () {
         return  "Your browser " + (this.vml ? "doesn't ": "") + "support" + (this.svg ? "s": "") +
                 " SVG.\nYou are running " + unescape("Rapha%EBl%20") + this.version;
@@ -161,14 +161,14 @@ var Raphael = (function () {
                 red = parseFloat(rgb[0], 10);
                 green = parseFloat(rgb[1], 10);
                 blue = parseFloat(rgb[2], 10);
-                return Raphael.hsb2rgb(red, green, blue);
+                return R.hsb2rgb(red, green, blue);
             }
             if (rgb[7]) {
                 rgb = rgb[7].split(/\s*,\s*/);
                 red = parseFloat(rgb[0], 10) * 2.55;
                 green = parseFloat(rgb[1], 10) * 2.55;
                 blue = parseFloat(rgb[2], 10) * 2.55;
-                return Raphael.hsb2rgb(red, green, blue);
+                return R.hsb2rgb(red, green, blue);
             }
             var rgb = {r: red, g: green, b: blue};
             var r = Math.round(red).toString(16);
@@ -185,7 +185,7 @@ var Raphael = (function () {
     };
     R.getColor = function (value) {
         var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
-        var rgb = Raphael.hsb2rgb(start.h, start.s, start.b);
+        var rgb = this.hsb2rgb(start.h, start.s, start.b);
         start.h += .075;
         if (start.h > 1) {
             start.h = 0;
@@ -197,7 +197,7 @@ var Raphael = (function () {
         return rgb.hex;
     };
     R.getColor.reset = function () {
-        this.start = undefined;
+        delete this.start;
     };
     // path utilities
     R.parsePathString = function (pathString) {
@@ -213,9 +213,9 @@ var Raphael = (function () {
         if (pathString.toString.toString() == toString.toString()) {
             return pathString;
         }
-        pathString.replace(/([achlmqstvz])[\s,]*((-?\d*(?:e-?\d+|\.?\d*)\s*,?\s*)+)/ig, function (a, b, c) {
+        pathString.replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
             var params = [], name = b.toLowerCase();
-            c.replace(/(-?\d*(?:e-?\d+|\.?\d*))\s*,?\s*/ig, function (a, b) {
+            c.replace(/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
                 b && params.push(+b);
             });
             while (params.length >= paramCounts[name]) {
@@ -231,7 +231,7 @@ var Raphael = (function () {
     var pathDimensions = function (path) {
         var pathArray = path;
         if (typeof path == "string") {
-            pathArray = Raphael.parsePathString(path);
+            pathArray = R.parsePathString(path);
         }
         pathArray = pathToAbsolute(pathArray);
         var x = [], y = [], length = 0;
@@ -375,7 +375,7 @@ var Raphael = (function () {
         return res;
     };
     var pathEqualiser = function (path1, path2) {
-        var data = [pathToAbsolute(Raphael.parsePathString(path1)), pathToAbsolute(Raphael.parsePathString(path2))],
+        var data = [pathToAbsolute(R.parsePathString(path1)), pathToAbsolute(R.parsePathString(path2))],
             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}],
             processPath = function (path, d) {
                 if (!path) {
@@ -529,6 +529,45 @@ var Raphael = (function () {
             return gradient;
         }
     };
+    var getContainer = function () {
+        var container, x, y, width, height;
+        if (typeof arguments[0] == "string" || typeof arguments[0] == "object") {
+            if (typeof arguments[0] == "string") {
+                container = doc.getElementById(arguments[0]);
+            } else {
+                container = arguments[0];
+            }
+            if (container.tagName) {
+                if (arguments[1] == null) {
+                    return {
+                        container: container,
+                        width: container.style.pixelWidth || container.offsetWidth,
+                        height: container.style.pixelHeight || container.offsetHeight
+                    };
+                } else {
+                    return {container: container, width: arguments[1], height: arguments[2]};
+                }
+            }
+        } else if (typeof arguments[0] == "number" && arguments.length > 3) {
+            return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
+        }
+    };
+    var plugins = function (con, scope, add) {
+        for (var prop in add) if (!(prop in con)) {
+            switch (typeof add[prop]) {
+                case "function":
+                    con[prop] = con === scope ? add[prop] : function () { add[prop].apply(scope, arguments); };
+                break;
+                case "object":
+                    con[prop] = {};
+                    plugins(con[prop], con, add[prop]);
+                break;
+                default:
+                    con[prop] = add[prop];
+                break;
+            }
+        }
+    };
 
     // SVG
     if (R.svg) {
@@ -721,7 +760,7 @@ var Raphael = (function () {
         var addGrdientFill = function (o, gradient, SVG) {
             gradient = toGradient(gradient);
             var el = doc.createElementNS(SVG.svgns, (gradient.type || "linear") + "Gradient");
-            el.id = "raphael-gradient-" + Raphael.idGenerator++;
+            el.id = "raphael-gradient-" + R.idGenerator++;
             if (gradient.vector && gradient.vector.length) {
                 el.setAttribute("x1", gradient.vector[0]);
                 el.setAttribute("y1", gradient.vector[1]);
@@ -751,7 +790,7 @@ var Raphael = (function () {
         };
         var updatePosition = function (o) {
             if (o.pattern) {
-                var bbox = o.node.getBBox();
+                var bbox = o.getBBox();
                 o.pattern.setAttribute("patternTransform", "translate(" + [bbox.x, bbox.y].join(",") + ")");
             }
         };
@@ -782,9 +821,25 @@ var Raphael = (function () {
                 }
             };
             for (var att in params) {
+                if (!(att in availableAttrs)) {
+                    continue;
+                }
                 var value = params[att];
                 o.attrs[att] = value;
                 switch (att) {
+                    // Hyperlink
+                    case "href":
+                    case "title":
+                    case "target":
+                        var pn = o.node.parentNode;
+                        if (pn.tagName.toLowerCase() != "a") {
+                            var hl = doc.createElementNS(o.svg.svgns, "a");
+                            pn.insertBefore(hl, o.node);
+                            hl.appendChild(o.node);
+                            pn = hl;
+                        }
+                        pn.setAttributeNS(o.svg.xlink, att, value);
+                      break;
                     case "path":
                         if (o.type == "path") {
                             o.node.setAttribute("d", "M0,0");
@@ -810,7 +865,7 @@ var Raphael = (function () {
                         break;
                     case "src":
                         if (o.type == "image") {
-                            o.node.setAttributeNS(svg.xlink, "href", value);
+                            o.node.setAttributeNS(o.svg.xlink, "href", value);
                         }
                         break;
                     case "stroke-width":
@@ -840,7 +895,7 @@ var Raphael = (function () {
                         if (isURL) {
                             var el = doc.createElementNS(o.svg.svgns, "pattern");
                             var ig = doc.createElementNS(o.svg.svgns, "image");
-                            el.id = "raphael-pattern-" + Raphael.idGenerator++;
+                            el.id = "raphael-pattern-" + R.idGenerator++;
                             el.setAttribute("x", 0);
                             el.setAttribute("y", 0);
                             el.setAttribute("patternUnits", "userSpaceOnUse");
@@ -946,8 +1001,6 @@ var Raphael = (function () {
             if (dif) {
                 element.node.setAttribute("y", element.attrs.y - dif);
             }
-            setTimeout(function () {
-            });
         };
         var Element = function (node, svg) {
             var X = 0,
@@ -1006,7 +1059,20 @@ var Raphael = (function () {
             this.node.parentNode.removeChild(this.node);
         };
         Element.prototype.getBBox = function () {
-            return this.node.getBBox();
+            var bbox = this.node.getBBox();
+            if (this.type == "text") {
+                var chr0 = this.node.getExtentOfChar(0);
+                if (chr0.height > bbox.height) {
+                    var chrl = this.node.getExtentOfChar(this.node.getNumberOfChars() - 1);
+                    return {
+                        x: chr0.x,
+                        y: chr0.y,
+                        width: chrl.x - chr0.x + chrl.width,
+                        height: chr0.height
+                    };
+                }
+            }
+            return bbox;
         };
         Element.prototype.attr = function () {
             if (arguments.length == 1 && typeof arguments[0] == "string") {
@@ -1185,25 +1251,12 @@ var Raphael = (function () {
             return this;
         };
         var create = function () {
-            // container, width, height
-            // x, y, width, height
-            if (typeof arguments[0] == "string") {
-                var container = doc.getElementById(arguments[0]);
-                var width = arguments[1];
-                var height = arguments[2];
-            }
-            if (typeof arguments[0] == "object") {
-                var container = arguments[0];
-                var width = arguments[1];
-                var height = arguments[2];
-            }
-            if (typeof arguments[0] == "number") {
-                var container = 1,
-                    x = arguments[0],
-                    y = arguments[1],
-                    width = arguments[2],
-                    height = arguments[3];
-            }
+            var con = getContainer.apply(null, arguments);
+            var container = con.container,
+                x = con.x,
+                y = con.y,
+                width = con.width,
+                height = con.height;
             if (!container) {
                 throw new Error("SVG container not found.");
             }
@@ -1239,11 +1292,7 @@ var Raphael = (function () {
                     container[prop] = paper[prop];
                 }
             }
-            for (var prop in R.fn) {
-                if (!container[prop]) {
-                    container[prop] = R.fn[prop];
-                }
-            }
+            plugins(container, container, R.fn);
             container.clear();
             return container;
         };
@@ -1500,6 +1549,9 @@ var Raphael = (function () {
             for (var par in params) {
                 o.attrs[par] = params[par];
             }
+            params.href && (o.node.href = params.href);
+            params.title && (o.node.title = params.title);
+            params.target && (o.node.target = params.target);
             if (params.path && o.type == "path") {
                 o.Path = "";
                 o.path = [];
@@ -1528,7 +1580,7 @@ var Raphael = (function () {
             params["font-size"] && (s.fontSize = params["font-size"]);
             params["font-weight"] && (s.fontWeight = params["font-weight"]);
             params["font-style"] && (s.fontStyle = params["font-style"]);
-            if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined" || params["stroke-width"] || params["stroke-opacity"] || params["stroke-dasharray"] || params["stroke-miterlimit"] || params["stroke-linejoin"] || params["stroke-linecap"]) {
+            if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined" || params["stroke-width"] || params["stroke-opacity"] || params["fill-opacity"] || params["stroke-dasharray"] || params["stroke-miterlimit"] || params["stroke-linejoin"] || params["stroke-linecap"]) {
                 o = o.shape || o.node;
                 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || createNode("fill");
                 if ("fill-opacity" in params || "opacity" in params) {
@@ -1587,6 +1639,10 @@ var Raphael = (function () {
             if (res.type == "text") {
                 var span = doc.createElement("span"),
                     s = span.style;
+                s.padding = 0;
+                s.margin = 0;
+                s.lineHeight = 1;
+                s.display = "inline";
                 res.attrs.font && (s.font = res.attrs.font);
                 res.attrs["font-family"] && (s.fontFamily = res.attrs["font-family"]);
                 res.attrs["font-size"] && (s.fontSize = res.attrs["font-size"]);
@@ -1596,9 +1652,24 @@ var Raphael = (function () {
                 span.innerText = res.node.string;
                 res.W = res.attrs.w = span.offsetWidth;
                 res.H = res.attrs.h = span.offsetHeight;
-                res.X = res.attrs.x - Math.round(res.W / 2);
-                res.Y = res.attrs.y - Math.round(res.H / 2);
+                res.X = res.attrs.x;
+                res.Y = res.attrs.y + Math.round(res.H / 2);
                 res.node.parentNode.removeChild(span);
+
+                // text-anchor emulation
+                switch (res.attrs["text-anchor"]) {
+                    case "start":
+                        res.node.style["v-text-align"] = "left";
+                        res.bbx = Math.round(res.W / 2);
+                    break;
+                    case "end":
+                        res.node.style["v-text-align"] = "right";
+                        res.bbx = -Math.round(res.W / 2);
+                    break;
+                    default:
+                        res.node.style["v-text-align"] = "center";
+                    break;
+                }
             }
         };
         var getAngle = function (a, b, c, d) {
@@ -1689,14 +1760,14 @@ var Raphael = (function () {
                 cy = parseFloat(deg[2], 10);
             }
             deg = parseFloat(deg[0], 10);
-            if (cy == null) {
-                cx = null;
-            }
             if (cx != null) {
                 this._.rt.deg = deg;
             } else {
                 this._.rt.deg += deg;
             }
+            if (cy == null) {
+                cx = null;
+            }
             this._.rt.cx = cx;
             this._.rt.cy = cy;
             this.setBox(null, cx, cy);
@@ -1797,7 +1868,7 @@ var Raphael = (function () {
         };
         Element.prototype.getBBox = function () {
             return {
-                x: this.X,
+                x: this.X + (this.bbx || 0),
                 y: this.Y,
                 width: this.W,
                 height: this.H
@@ -1961,6 +2032,7 @@ var Raphael = (function () {
             res.attrs.w = 1;
             res.attrs.h = 1;
             setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
+            res.setBox();
             return res;
         };
         var setSize = function (width, height) {
@@ -1986,26 +2058,12 @@ var Raphael = (function () {
             };
         }
         var create = function () {
-            // container, width, height
-            // x, y, width, height
-            var container, width, height;
-            if (typeof arguments[0] == "string") {
-                container = doc.getElementById(arguments[0]);
-                width = arguments[1];
-                height = arguments[2];
-            }
-            if (typeof arguments[0] == "object") {
-                container = arguments[0];
-                width = arguments[1];
-                height = arguments[2];
-            }
-            if (typeof arguments[0] == "number") {
-                container = 1;
-                x = arguments[0];
-                y = arguments[1];
-                width = arguments[2];
-                height = arguments[3];
-            }
+            var con = getContainer.apply(null, arguments);
+            var container = con.container,
+                x = con.x,
+                y = con.y,
+                width = con.width,
+                height = con.height;
             if (!container) {
                 throw new Error("VML container not found.");
             }
@@ -2062,11 +2120,7 @@ var Raphael = (function () {
             for (var prop in paper) {
                 container[prop] = paper[prop];
             }
-            for (var prop in R.fn) {
-                if (!container[prop]) {
-                    container[prop] = R.fn[prop];
-                }
-            }
+            plugins(container, container, R.fn);
             container.clear = function () {
                 var todel = [];
                 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
@@ -2233,7 +2287,7 @@ var Raphael = (function () {
         clearTimeout(this.animation_in_progress);
     };
     Element.prototype.scale = function (x, y) {
-        if (x == undefined && y == undefined) {
+        if (x == null && y == null) {
             return {x: this._.sx, y: this._.sy};
         }
         y = y || x;
@@ -2292,20 +2346,20 @@ var Raphael = (function () {
                     });
                     break;
                 case "path":
-                    var path = pathToRelative(Raphael.parsePathString(this.attr("path"))),
+                    var path = pathToRelative(R.parsePathString(this.attr("path"))),
                         skip = true,
-                        dim = pathDimensions(this.attrs.path),
-                        dx = -dim.width * (x - 1) / 2,
-                        dy = -dim.height * (y - 1) / 2;
+                        dim = pathDimensions(this.attrs.path);
                     for (var i = 0, ii = path.length; i < ii; i++) {
                         if (path[i][0].toUpperCase() == "M" && skip) {
                             continue;
                         } else {
                             skip = false;
                         }
-                        if (path[i][0].toUpperCase() == "A") {
+                        if (this.svg && path[i][0].toUpperCase() == "A") {
                             path[i][path[i].length - 2] *= x * dirx;
                             path[i][path[i].length - 1] *= y * diry;
+                            path[i][1] *= x;
+                            path[i][2] *= y;
                         } else {
                             for (var j = 1, jj = path[i].length; j < jj; j++) {
                                 path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
@@ -2532,5 +2586,10 @@ var Raphael = (function () {
         };
     };
 
+    R.noConflict = function () {
+        var r = window.Raphael;
+        delete window.Raphael;
+        return r;
+    };
     return R;
 })();
\ No newline at end of file