Skip to content

Commit

Permalink
Coverage + fixes for empty arcs transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Apr 16, 2015
1 parent c5f3eb4 commit aed6826
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 10 deletions.
7 changes: 3 additions & 4 deletions lib/svgpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ SvgPath.prototype.__matrix = function (m) {
// ARC is: ['A', rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y]

// Drop segment if arc is empty
if (s[1] === 0 || s[2] === 0 ||
(s[0] === 'A' && s[6] === x && s[7] === y) ||
if ((s[0] === 'A' && s[6] === x && s[7] === y) ||
(s[0] === 'a' && s[6] === 0 && s[7] === 0)) {
return [];
}
Expand All @@ -104,8 +103,8 @@ SvgPath.prototype.__matrix = function (m) {
}
}

// If scaleX or scaleY === 0 - replace arc with line
if (arc2line) {
// If scaleX / scaleY / rx / ry === 0 - replace arc with line
if (arc2line || s[1] === 0 || s[2] === 0) {
p = m.calc(s[6], s[7], s[0] === 'a');
result = [ (s[0] === 'a') ? 'l' : 'L', p[0], p[1] ];
break;
Expand Down
7 changes: 7 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
globals:
describe: false
it: false
before: false
beforeEach: false
after: false
afterEach: false
58 changes: 56 additions & 2 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/*global describe, it*/

'use strict';


Expand Down Expand Up @@ -440,4 +438,60 @@ describe('API', function () {
);
});
});


describe('arc transform edge cases', function () {
it('replace arcs rx/ry = 0 with lines', function () {
assert.equal(
svgpath('M40 30a0 40 -45 0 1 20 50Z M40 30A20 0 -45 0 1 20 50Z').scale(2, 2).toString(),
'M80 60l40 100ZM80 60L40 100Z'
);
});

it('drop arcs with end point === start point', function () {
assert.equal(
svgpath('M40 30a20 40 -45 0 1 0 0').scale(2, 2).toString(),
'M80 60'
);

assert.equal(
svgpath('M40 30A20 40 -45 0 1 40 30').scale(2, 2).toString(),
'M80 60'
);
});

it('to line at scale x|y = 0 ', function () {
assert.equal(
svgpath('M40 30a20 40 -45 0 1 20 50').scale(0, 1).toString(),
'M0 30l0 50'
);

assert.equal(
svgpath('M40 30A20 40 -45 0 1 20 50').scale(1, 0).toString(),
'M40 0L20 0'
);
});

it('rotate to +/- 90 degree', function () {
assert.equal(
svgpath('M40 30a20 40 -45 0 1 20 50').rotate(90).round(2).toString(),
'M-30 40a20 40 45 0 1-50 20'
);

assert.equal(
svgpath('M40 30a20 40 -45 0 1 20 50').matrix([ 0, 1, -1, 0, 0, 0 ]).toString(),
'M-30 40a20 40 45 0 1-50 20'
);

assert.equal(
svgpath('M40 30a20 40 -45 0 1 20 50').rotate(-90).round(2).toString(),
'M30-40a20 40-135 0 1 50-20'
);

assert.equal(
svgpath('M40 30a20 40 -45 0 1 20 50').matrix([ 0, -1, 1, 0, 0, 0 ]).toString(),
'M30-40a20 40-135 0 1 50-20'
);
});
});
});
67 changes: 67 additions & 0 deletions test/matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';


var assert = require('assert');
var matrix = require('../lib/matrix');

var m;

describe('Matrix', function () {

it('ignore empty actions', function () {
m = matrix();

m.matrix([ 1, 0, 0, 1, 0, 0 ]);
assert.equal(m.queue.length, 0);

m.translate(0, 0);
assert.equal(m.queue.length, 0);

m.scale(1, 1);
assert.equal(m.queue.length, 0);

m.rotate(0);
assert.equal(m.queue.length, 0);

m.skewX(0);
assert.equal(m.queue.length, 0);

m.skewY(0);
assert.equal(m.queue.length, 0);
});

it('do nothing on empty queue', function () {
assert.deepEqual(matrix().calc(10, 11, false), [ 10, 11 ]);
assert.deepEqual(matrix().toArray(), [ 1, 0, 0, 1, 0, 0 ]);
});

it('compose', function () {
m = matrix()
.translate(10, 10)
.translate(-10, -10)
.rotate(180, 10, 10)
.rotate(180, 10, 10)
.toArray();

// Need to round errors prior to compare
assert.equal(+m[0].toFixed(2), 1);
assert.equal(+m[1].toFixed(2), 0);
assert.equal(+m[2].toFixed(2), 0);
assert.equal(+m[3].toFixed(2), 1);
assert.equal(+m[4].toFixed(2), 0);
assert.equal(+m[5].toFixed(2), 0);
});

it('cache', function () {
m = matrix()
.translate(10, 20)
.scale(2, 3);

assert.strictEqual(m.cache, null);
assert.deepEqual(m.toArray(), [ 2, 0, 0, 3, 10, 20 ]);
assert.deepEqual(m.cache, [ 2, 0, 0, 3, 10, 20 ]);
m.cache = [ 1, 2, 3, 4, 5, 6 ];
assert.deepEqual(m.toArray(), [ 1, 2, 3, 4, 5, 6 ]);
});

});
2 changes: 0 additions & 2 deletions test/path_parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/*global describe, it*/

'use strict';


Expand Down
2 changes: 0 additions & 2 deletions test/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/*global describe, it*/

'use strict';


Expand Down

0 comments on commit aed6826

Please sign in to comment.