Added support for “O”, “U” & “R” path commands

master
Dmitry Baranovskiy 2013-07-22 12:51:29 +10:00
parent 8766ae2c79
commit f57bb75a1b
1 changed files with 95 additions and 79 deletions

44
svg.js
View File

@ -928,7 +928,7 @@ Savage.parsePathString = function (pathString) {
return pathClone(pth.arr);
}
var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, r: 4, q: 4, s: 4, t: 2, v: 1, z: 0},
var paramCounts = {a: 7, c: 6, o: 2, h: 1, l: 2, m: 2, r: 4, q: 4, s: 4, t: 2, v: 1, u: 3, z: 0},
data = [];
if (is(pathString, "array") && is(pathString[0], "array")) { // rough assumption
data = pathClone(pathString);
@ -945,6 +945,9 @@ Savage.parsePathString = function (pathString) {
name = "l";
b = b == "m" ? "l" : "L";
}
if (name == "o" && params.length == 1) {
data.push([b, params[0]]);
}
if (name == "r") {
data.push([b].concat(params));
} else while (params.length >= paramCounts[name]) {
@ -1442,7 +1445,10 @@ var pathDimensions = function (path) {
start++;
res[0] = ["M", x, y];
}
var crz = pathArray.length == 3 && pathArray[0][0] == "M" && pathArray[1][0].toUpperCase() == "R" && pathArray[2][0].toUpperCase() == "Z";
var crz = pathArray.length == 3 &&
pathArray[0][0] == "M" &&
pathArray[1][0].toUpperCase() == "R" &&
pathArray[2][0].toUpperCase() == "Z";
for (var r, pa, i = start, ii = pathArray.length; i < ii; i++) {
res.push(r = []);
pa = pathArray[i];
@ -1476,11 +1482,14 @@ var pathDimensions = function (path) {
break;
case "O":
res.pop();
res = res.concat(ellipsePath(x, y, pa[1], pa[2]));
dots = ellipsePath(x, y, pa[1], pa[2]);
dots.push(dots[0]);
res = res.concat(dots);
break;
case "U":
res.pop();
res = res.concat(ellipsePath(x, y, pa[1], pa[2], pa[3]));
r = ["U"].concat(res[res.length - 1].slice(-2));
break;
case "M":
mx = +pa[1] + x;
@ -1497,16 +1506,20 @@ var pathDimensions = function (path) {
r = ["R"].concat(pa.slice(-2));
} else if (pa0 == "O") {
res.pop();
res = res.concat(ellipsePath(x, y, pa[1], pa[2]));
dots = ellipsePath(x, y, pa[1], pa[2]);
dots.push(dots[0]);
res = res.concat(dots);
} else if (pa0 == "U") {
res.pop();
res = res.concat(ellipsePath(x, y, pa[1], pa[2], pa[3]));
r = ["U"].concat(res[res.length - 1].slice(-2));
} else {
for (var k = 0, kk = pa.length; k < kk; k++) {
r[k] = pa[k];
}
}
if (pa0 != "O" && pa0 != "U") {
pa0 = pa0.toUpperCase();
if (pa0 != "O") {
switch (r[0]) {
case "Z":
x = mx;
@ -2165,6 +2178,10 @@ function wrap(dom) {
return new Element(dom);
}
(function (proto) {
proto.el = function (name) {
var el = make(name, this.node);
return el;
};
proto.rect = function (x, y, w, h, rx, ry) {
var el = make("rect", this.node);
if (ry == null) {
@ -2557,6 +2574,7 @@ eve.on("savage.util.grad.parse", function parseGrad(string) {
});
eve.on("savage.util.attr.d", function (value) {
eve.stop();
if (is(value, "array") && is(value[0], "array")) {
value = path2string.call(value);
}
@ -2565,12 +2583,11 @@ eve.on("savage.util.attr.d", function (value) {
value = pathToAbsolute(value);
}
$(this.node, {d: value});
eve.stop();
});
})(-1);
eve.on("savage.util.attr.path", function (value) {
this.attr({d: value});
eve.stop();
});
this.attr({d: value});
})(-1);
eve.on("savage.util.attr.viewBox", function (value) {
var vb;
if (is(value, "object") && "x" in value) {
@ -2584,11 +2601,11 @@ eve.on("savage.util.attr.viewBox", function (value) {
viewBox: vb
});
eve.stop();
});
})(-1);
eve.on("savage.util.attr.transform", function (value) {
this.transform(value);
eve.stop();
});
})(-1);
eve.on("savage.util.attr.r", function (value) {
if (this.type == "rect") {
eve.stop();
@ -2597,7 +2614,7 @@ eve.on("savage.util.attr.r", function (value) {
ry: value
});
}
});
})(-1);
eve.on("savage.util.attr.text", function (value) {
if (this.type == "text") {
var i = 0,
@ -2620,7 +2637,7 @@ eve.on("savage.util.attr.text", function (value) {
node.appendChild(tuner(value));
}
eve.stop();
});
})(-1);
// default
var availableAttributes = {
rect: {x: 0, y: 0, width: 0, height: 0, rx: 0, ry: 0, "class": 0},
@ -2648,7 +2665,6 @@ eve.on("savage.util.attr", function (value) {
var style = att.replace(/-(\w)/gi, function (all, letter) {
return letter.toUpperCase();
});
// var state = document.defaultView.getComputedStyle(this.node, null).getPropertyValue(style);
if (availableAttributes[has](this.type) && availableAttributes[this.type][has](att)) {
value == null ? this.node.removeAttribute(att) : this.node.setAttribute(att, value);
} else {