Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions svg-path-to-polygons.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ function svgPathToPolygons(svgPathString, opts={}) {

// http://antigrain.com/research/adaptive_bezier/
function sampleCubicBézier(x0, y0, x1, y1, x2, y2, x3, y3) {
// ignore degenerate curves
if (x0 === x1 && x0 === x2 && x0 === x3 && y0 === y1 && y0 === y2 && y0 === y3) {
return;
}

// Calculate all the mid-points of the line segments
const x01 = (x0 + x1) / 2,
y01 = (y0 + y1) / 2,
Expand All @@ -76,8 +81,8 @@ function svgPathToPolygons(svgPathString, opts={}) {

if (((d1+d2)*(d1+d2)) < (tolerance2 * (dx*dx + dy*dy))) add(x0123,y0123);
else { // Continue subdivision
sampleCubicBézier(x0, y0, x01, y01, x012, y012, x0123, y0123);
sampleCubicBézier(x0123, y0123, x123, y123, x23, y23, x3, y3);
sampleCubicBézier(x0, y0, x01, y01, x012, y012, x0123, y0123);
sampleCubicBézier(x0123, y0123, x123, y123, x23, y23, x3, y3);
}
}

Expand Down Expand Up @@ -112,4 +117,4 @@ ${polys.map(poly => ` <${poly.closed ? 'polygon' : 'polyline'} points="${poly.j
</g>
</svg>
`.trim());
};
};
8 changes: 7 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const expectations = [
closed: [false],
decimals:1
},
{
m:'degenerate curve',
d:'M0,0 c0,0 0,0 0,0',
o:[[[0,0],[0,0]]],
closed: [false]
},

];

Expand All @@ -55,4 +61,4 @@ expectations.forEach(ex => {
assert.deepEqual(o.map( poly=>!!poly.closed), ex.closed, ex.m+', properly closed' );
});

// TODO: handle parse errors
// TODO: handle parse errors