From 29deb5d699e26dfc3b830c739c2f793b0b6b88f2 Mon Sep 17 00:00:00 2001 From: David Lu Date: Tue, 28 Aug 2018 22:56:24 -0400 Subject: [PATCH 1/2] add test for degenerate curve --- test/test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 From 6159e86cb426974e069ba6ecbd7cc8c0d657943c Mon Sep 17 00:00:00 2001 From: David Lu Date: Tue, 28 Aug 2018 22:56:38 -0400 Subject: [PATCH 2/2] add fix for handling degenerate curve --- svg-path-to-polygons.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 +};