Skip to content

Commit

Permalink
don't drop empty arcs in evaluate + use relative lines in unarc, clos…
Browse files Browse the repository at this point in the history
…ing #20
  • Loading branch information
Vitaly Puzrin committed Jan 3, 2016
1 parent 9f5b841 commit db6bba3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
16 changes: 12 additions & 4 deletions lib/svgpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,25 @@ 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 (end point === start point)
if ((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 [];
}
}*/

// Transform rx, ry and the x-axis-rotation
var e = ellipse(s[1], s[2], s[3]).transform(m.toArray());

// Transform end point as usual (without translation for relative notation)
p = m.calc(s[6], s[7], s[0] === 'a');

// Empty arcs can be ignored by renderer, but should not be dropped
// to avoid collisions with `S A S` and so on. Replace with empty line.
if ((s[0] === 'A' && s[6] === x && s[7] === y) ||
(s[0] === 'a' && s[6] === 0 && s[7] === 0)) {
result = [ s[0] === 'a' ? 'l' : 'L', p[0], p[1] ];
break;
}

// if the resulting ellipse is (almost) a segment ...
if (e.isDegenerate()) {
// replace the arc by a line
Expand Down Expand Up @@ -499,9 +507,9 @@ SvgPath.prototype.unarc = function () {
new_segments = a2c(x, y, nextX, nextY, s[4], s[5], s[1], s[2], s[3]);

// Degenerated arcs can be ignored by renderer, but should not be dropped
// to avoid collisions wish `S A S` and so on. Replace with empty line.
// to avoid collisions with `S A S` and so on. Replace with empty line.
if (new_segments.length === 0) {
return [ [ 'L', nextX, nextY ] ];
return [ [ s[0] === 'a' ? 'l' : 'L', s[6], s[7] ] ];
}

new_segments.forEach(function (s) {
Expand Down
9 changes: 7 additions & 2 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,11 @@ describe('API', function () {
svgpath('M100 100A123 456 90 0 1 100 100').unarc().round(0).toString(),
'M100 100L100 100'
);

assert.equal(
svgpath('M100 100a123 456 90 0 1 0 0').unarc().round(0).toString(),
'M100 100l0 0'
);
});

it('radii are zero', function () {
Expand Down Expand Up @@ -521,12 +526,12 @@ describe('API', function () {
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'
'M80 60l0 0'
);

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

Expand Down

0 comments on commit db6bba3

Please sign in to comment.