diff --git a/svg-path-to-polygons.js b/svg-path-to-polygons.js index 5ae1901..a186087 100644 --- a/svg-path-to-polygons.js +++ b/svg-path-to-polygons.js @@ -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, @@ -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); } } @@ -112,4 +117,4 @@ ${polys.map(poly => ` <${poly.closed ? 'polygon' : 'polyline'} points="${poly.j `.trim()); -}; \ No newline at end of file +}; diff --git a/test/test.js b/test/test.js index 88a42d4..0d73c06 100644 --- a/test/test.js +++ b/test/test.js @@ -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] + }, ]; @@ -55,4 +61,4 @@ expectations.forEach(ex => { assert.deepEqual(o.map( poly=>!!poly.closed), ex.closed, ex.m+', properly closed' ); }); -// TODO: handle parse errors \ No newline at end of file +// TODO: handle parse errors