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 22e46a7
Showing 1 changed file with 70 additions and 69 deletions.
139 changes: 70 additions & 69 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 Expand Up @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit 22e46a7

Please sign in to comment.