diff --git a/lib/svgpath.js b/lib/svgpath.js index 0b45a2f..61a8858 100644 --- a/lib/svgpath.js +++ b/lib/svgpath.js @@ -171,28 +171,25 @@ SvgPath.prototype.__evaluateStack = function () { // Convert processed SVG Path back to string // SvgPath.prototype.toString = function () { - var elements = []; + var elements = [], skipCmd; this.__evaluateStack(); for (var i = 0; i < this.segments.length; i++) { // remove repeating commands names - if (i > 0 && (this.segments[i][0] === this.segments[i - 1][0])) { - elements.push(this.segments[i].slice(1)); - continue; - } - elements.push(this.segments[i]); + skipCmd = i > 0 && (this.segments[i][0] === this.segments[i - 1][0]); + elements = elements.concat(skipCmd ? this.segments[i].slice(1) : this.segments[i]); } - return [].concat.apply([], elements).join(' ') - // Optimizations: remove spaces around commands & before `-` - // - // We could also remove leading zeros for `0.5`-like values, - // but their count is too small to spend time for. - .replace(/ ?([achlmqrstvz]) ?/gi, '$1') - .replace(/ \-/g, '-') - // workaround for FontForge SVG importing bug - .replace(/zm/g, 'z m'); + return elements.join(' ') + // Optimizations: remove spaces around commands & before `-` + // + // We could also remove leading zeros for `0.5`-like values, + // but their count is too small to spend time for. + .replace(/ ?([achlmqrstvz]) ?/gi, '$1') + .replace(/ \-/g, '-') + // workaround for FontForge SVG importing bug + .replace(/zm/g, 'z m'); }; @@ -298,37 +295,39 @@ SvgPath.prototype.iterate = function (iterator, keepLazyStack) { needReplace = true; } - // all relative commands except Z - isRelative = 'achlmqrstv'.indexOf(s[0]) >= 0; - var nameLC = s[0].toLowerCase(); + var isRelative = (s[0] === s[0].toLowerCase()); // calculate absolute X and Y - if (nameLC === 'm') { - lastX = s[1] + (isRelative ? lastX : 0); - lastY = s[2] + (isRelative ? lastY : 0); - countourStartX = lastX; - countourStartY = lastY; - return; - } + switch (s[0]) { + case 'm': + case 'M': + lastX = s[1] + (isRelative ? lastX : 0); + lastY = s[2] + (isRelative ? lastY : 0); + countourStartX = lastX; + countourStartY = lastY; + return; - if (nameLC === 'h') { - lastX = s[1] + (isRelative ? lastX : 0); - return; - } + case 'h': + case 'H': + lastX = s[1] + (isRelative ? lastX : 0); + return; - if (nameLC === 'v') { - lastY = s[1] + (isRelative ? lastY : 0); - return; - } + case 'v': + case 'V': + lastY = s[1] + (isRelative ? lastY : 0); + return; - if (nameLC === 'z') { - lastX = countourStartX; - lastY = countourStartY; - return; - } + case 'z': + case 'Z': + // That make sence for multiple contours + lastX = countourStartX; + lastY = countourStartY; + return; - lastX = s[s.length - 2] + (isRelative ? lastX : 0); - lastY = s[s.length - 1] + (isRelative ? lastY : 0); + default: + lastX = s[s.length - 2] + (isRelative ? lastX : 0); + lastY = s[s.length - 1] + (isRelative ? lastY : 0); + } }); // Replace segments if iterator return results @@ -367,22 +366,23 @@ SvgPath.prototype.abs = function () { s[0] = nameUC; - // V is the only command, with shifted coords parity - if (name === 'v') { - s[1] += y; - return; - } + switch (name) { + case 'v': + // v has shifted coords parity + s[1] += y; + return; - // ARC is: ['A', rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y] - // touch x, y only - if (name === 'a') { - s[6] += x; - s[7] += y; - return; - } + case 'a': + // ARC is: ['A', rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y] + // touch x, y only + s[6] += x; + s[7] += y; + return; - for (i = 1; i < s.length; i++) { - s[i] += i % 2 ? x : y; // odd values are X, even - Y + default: + for (i = 1; i < s.length; i++) { + s[i] += i % 2 ? x : y; // odd values are X, even - Y + } } }, true); @@ -402,24 +402,25 @@ SvgPath.prototype.rel = function () { // Skip relative commands if (name === nameLC) { return; } - s[0] = name.toLowerCase(); + s[0] = nameLC; - // V is the only command, with shifted coords parity - if (name === 'V') { - s[1] -= y; - return; - } + switch (name) { + case 'V': + // V has shifted coords parity + s[1] -= y; + return; - // ARC is: ['A', rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y] - // touch x, y only - if (name === 'A') { - s[6] -= x; - s[7] -= y; - return; - } + case 'A': + // ARC is: ['A', rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y] + // touch x, y only + s[6] -= x; + s[7] -= y; + return; - for (i = 1; i < s.length; i++) { - s[i] -= i % 2 ? x : y; // odd values are X, even - Y + default: + for (i = 1; i < s.length; i++) { + s[i] -= i % 2 ? x : y; // odd values are X, even - Y + } } }, true);