diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f9d398..55f2ae4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ All notable changes to [path-intersection](https://github.com/bpmn-io/path-inter ___Note:__ Yet to be released changes appear here._ +* `FIX`: parse elliptical arc flags without separators ([#21](https://github.com/bpmn-io/path-intersection/issues/21)) + ## 4.2.0 * `FEAT`: improve intersection performance ([#33](https://github.com/bpmn-io/path-intersection/pull/33)) diff --git a/intersect.js b/intersect.js index 45c5879..111e9ab 100644 --- a/intersect.js +++ b/intersect.js @@ -21,10 +21,120 @@ var p2s = /,?([a-z]),?/gi, pow = math.pow, abs = math.abs, pathCommand = /([a-z])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?[\s]*,?[\s]*)+)/ig, - pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)[\s]*,?[\s]*/ig; + pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)[\s]*,?[\s]*/ig, + pathNumber = /^-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/i; var isArray = Array.isArray || function(o) { return o instanceof Array; }; +function skipCommaWsp(str, i) { + while (i < str.length) { + var code = str.charCodeAt(i); + + // space, tab, CR, LF, comma + if (code === 32 || code === 9 || code === 13 || code === 10 || code === 44) { + i++; + continue; + } + + break; + } + + return i; +} + +function parseNumberAt(str, i) { + var match = pathNumber.exec(str.slice(i)); + + if (!match) { + return null; + } + + return { + value: +match[0], + next: i + match[0].length + }; +} + +function parseFlagAt(str, i) { + var ch = str.charAt(i); + + if (ch === '0' || ch === '1') { + return { + value: +ch, + next: i + 1 + }; + } + + return null; +} + +/** + * Parse elliptical-arc parameters. + * + * Flags are a single `0`/`1` and, per the SVG path BNF, may sit next to + * each other or the following coordinate (`A 5 5 0 10 10 10` ≡ + * `A 5 5 0 1 0 10 10`). + * + * @param {string} values + * + * @return {number[]} + */ +function parseArcParams(values) { + var params = [], + i = 0, + len = values.length, + rx, ry, angle, large, sweep, x, y; + + while ((i = skipCommaWsp(values, i)) < len) { + + rx = parseNumberAt(values, i); + if (!rx) { + break; + } + + i = skipCommaWsp(values, rx.next); + ry = parseNumberAt(values, i); + if (!ry) { + break; + } + + i = skipCommaWsp(values, ry.next); + angle = parseNumberAt(values, i); + if (!angle) { + break; + } + + i = skipCommaWsp(values, angle.next); + large = parseFlagAt(values, i); + if (!large) { + break; + } + + i = skipCommaWsp(values, large.next); + sweep = parseFlagAt(values, i); + if (!sweep) { + break; + } + + i = skipCommaWsp(values, sweep.next); + x = parseNumberAt(values, i); + if (!x) { + break; + } + + i = skipCommaWsp(values, x.next); + y = parseNumberAt(values, i); + if (!y) { + break; + } + + i = y.next; + params.push(rx.value, ry.value, angle.value, large.value, sweep.value, x.value, y.value); + } + + return params; +} + /** * Parse SVG path string and return an array of path components. * @@ -45,9 +155,13 @@ function parsePathString(pathString) { var params = [], name = b.toLowerCase(); - c.replace(pathValues, function(a, b) { - b && params.push(+b); - }); + if (name == 'a') { + params = parseArcParams(c); + } else { + c.replace(pathValues, function(a, b) { + b && params.push(+b); + }); + } if (name == 'm' && params.length > 2) { pathComponents.push([ b, ...params.splice(0, 2) ]); diff --git a/test/intersect.spec.js b/test/intersect.spec.js index 48d64b7..0318bd5 100644 --- a/test/intersect.spec.js +++ b/test/intersect.spec.js @@ -121,6 +121,38 @@ describe('path-intersection', function() { }); + it('should parse elliptical arc flags without separators', function() { + + // given + var spaced = parsePath('M0,0A10,10,0,1,0,20,0'); + var compact = parsePath('M0,0A10,10,0,10,20,0'); + var glued = parsePath('M0,0A10,10,0,1020,0'); + + // then + expect(compact).to.eql(spaced); + expect(glued).to.eql(spaced); + }); + + + it('should parse all adjacent elliptical arc flag pairs', function() { + + // given + var flags = [ '00', '01', '10', '11' ]; + + flags.forEach(function(pair) { + var a = pair.charAt(0); + var b = pair.charAt(1); + + // when + var spaced = parsePath('M0,0A10,10,0,' + a + ',' + b + ',20,0'); + var compact = parsePath('M0,0A10,10,0,' + pair + ',20,0'); + + // then + expect(compact).to.eql(spaced); + }); + }); + + it('should provide performance improvement with parsePath', function() { // given @@ -261,6 +293,15 @@ describe('path-intersection', function() { }); + test('line with circle (compact arc flags)', { + p0: 'M150,150m0,-18a18,18,0,110,36a18,18,0,110,-36z', + p1: 'M100,100L150,150', + expectedIntersections: [ + { x: 137, y: 137, segment1: 5, segment2: 1 } + ] + }); + + test('line with circle (top)', { p0: 'M150,150m0,-18a18,18,0,1,1,0,36a18,18,0,1,1,0,-36z', p1: 'M150,100L150,150',