From 86e4d2a6b9dcc2668d16ca4897879a4859904093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 20 Feb 2014 02:50:39 +0100 Subject: [PATCH] Implement Curve#remove() and Path#reduce(). Also have Item#reduce() recursively call #reduce() on reduced children. --- src/item/Item.js | 2 +- src/path/Curve.js | 45 +++++++++++++++++++++++++++++++++------------ src/path/Path.js | 13 +++++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index f3d2f4cd..e02659ed 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -2064,7 +2064,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{ */ reduce: function() { if (this._children && this._children.length === 1) { - var child = this._children[0]; + var child = this._children[0].reduce(); child.insertAbove(this); child.setStyle(this._style); this.remove(); diff --git a/src/path/Curve.js b/src/path/Curve.js index 278bf846..8b42e93a 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -267,8 +267,12 @@ var Curve = Base.extend(/** @lends Curve# */{ * @bean */ getLength: function() { - if (this._length == null) - this._length = Curve.getLength(this.getValues(), 0, 1); + if (this._length == null) { + // Use simple point distance for linear curves + this._length = this.isLinear() + ? this._segment2._point.getDistance(this._segment1._point) + : Curve.getLength(this.getValues(), 0, 1); + } return this._length; }, @@ -303,16 +307,6 @@ var Curve = Base.extend(/** @lends Curve# */{ // TODO: adjustThroughPoint - /** - * Returns a reversed version of the curve, without modifying the curve - * itself. - * - * @return {Curve} a reversed version of the curve - */ - reverse: function() { - return new Curve(this._segment2.reverse(), this._segment1.reverse()); - }, - /** * Private method that handles all types of offset / isParameter pairs and * converts it to a curve parameter. @@ -418,6 +412,33 @@ var Curve = Base.extend(/** @lends Curve# */{ : null; }, + /** + * Returns a reversed version of the curve, without modifying the curve + * itself. + * + * @return {Curve} a reversed version of the curve + */ + reverse: function() { + return new Curve(this._segment2.reverse(), this._segment1.reverse()); + }, + + /** + * Removes the curve from the path that it belongs to, by merging its two + * path segments. + * @return {Boolean} {@true if the curve was removed} + */ + remove: function() { + var removed = false; + if (this._path) { + var segment2 = this._segment2, + handleOut = segment2._handleOut; + removed = segment2.remove(); + if (removed) + this._segment1._handleOut.set(handleOut.x, handleOut.y); + } + return removed; + }, + /** * Returns a copy of the curve. * diff --git a/src/path/Path.js b/src/path/Path.js index 94ad65b5..12db9220 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -900,6 +900,19 @@ var Path = PathItem.extend(/** @lends Path# */{ this.setSegments(segments); }, + /** + * Reduces the path by removing curves that have a lenght of 0. + */ + reduce: function() { + var curves = this.getCurves(); + for (var i = curves.length - 1; i >= 0; i--) { + var curve = curves[i]; + if (curve.isLinear() && curve.getLength() === 0) + curve.remove(); + } + return this; + }, + /** * Smooths a path by simplifying it. The {@link Path#segments} array is * analyzed and replaced by a more optimal set of segments, reducing memory