Skip to content

Commit

Permalink
More clear conditions inside some loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Apr 16, 2015
1 parent 49069ea commit 7f82280
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions lib/svgpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7f82280

Please sign in to comment.