Do not complain about open paths if they would not contain an area when closed.

This commit is contained in:
Jürg Lehni 2016-06-13 12:08:04 +02:00
parent 7241edd98a
commit 94853669f6
2 changed files with 26 additions and 14 deletions

View file

@ -291,7 +291,8 @@ var Path = PathItem.extend(/** @lends Path# */{
} }
} }
}, /** @lends Path# */{ }, /** @lends Path# */{
// Enforce bean creation for getPathData(), as it has hidden parameters. // Enforce bean creation for getPathData() and getArea(), as they have
// hidden parameters.
beans: true, beans: true,
getPathData: function(_matrix, _precision) { getPathData: function(_matrix, _precision) {
@ -348,8 +349,7 @@ var Path = PathItem.extend(/** @lends Path# */{
parts.push('z'); parts.push('z');
} }
return parts.join(''); return parts.join('');
} },
}, /** @lends Path# */{
// TODO: Consider adding getSubPath(a, b), returning a part of the current // TODO: Consider adding getSubPath(a, b), returning a part of the current
// path, with the added benefit that b can be < a, and closed looping is // path, with the added benefit that b can be < a, and closed looping is
@ -820,19 +820,25 @@ var Path = PathItem.extend(/** @lends Path# */{
* @bean * @bean
* @type Number * @type Number
*/ */
getArea: function() { getArea: function(_closed) {
if (this._area == null) { // If the call overrides the 'closed' state, do not cache the result.
// This is used in tracePaths().
var cached = _closed === undefined,
area = this._area;
if (!cached || area == null) {
var segments = this._segments, var segments = this._segments,
count = segments.length, count = segments.length,
last = count - 1, closed = cached ? this._closed : _closed;
last = count - 1;
area = 0; area = 0;
for (var i = 0, l = this._closed ? count : last; i < l; i++) { for (var i = 0, l = closed ? count : last; i < l; i++) {
area += Curve.getArea(Curve.getValues( area += Curve.getArea(Curve.getValues(
segments[i], segments[i < last ? i + 1 : 0])); segments[i], segments[i < last ? i + 1 : 0]));
} }
if (cached)
this._area = area; this._area = area;
} }
return this._area; return area;
}, },
/** /**

View file

@ -661,14 +661,20 @@ PathItem.inject(new function() {
path.firstSegment.setHandleIn(handleIn); path.firstSegment.setHandleIn(handleIn);
path.setClosed(true); path.setClosed(true);
} else if (path) { } else if (path) {
var length = path.getLength(); // Only complain about open paths if they would actually contain
// Only complain about open paths if they are long enough. // an area when closed. Such open paths can occur due to
if (length >= /*#=*/Numerical.GEOMETRIC_EPSILON) { // epsilons, e.g. when two segments are so close to each other
// that they are considered the same, but the winding
// calculation still produces a valid winding due to their
// slight differences.
var area = path.getArea(true);
if (Math.abs(area) >= /*#=*/Numerical.GEOMETRIC_EPSILON) {
// This path wasn't finished and is hence invalid. // This path wasn't finished and is hence invalid.
// Report the error to the console for the time being. // Report the error to the console for the time being.
console.error('Boolean operation resulted in open path', console.error('Boolean operation resulted in open path',
'segments =', path._segments.length, 'segments =', path._segments.length,
'length =', length); 'length =', path.getLength(),
'area=', area);
} }
path = null; path = null;
} }