Implement Curve#remove() and Path#reduce().

Also have Item#reduce() recursively call #reduce() on reduced children.
This commit is contained in:
Jürg Lehni 2014-02-20 02:50:39 +01:00
parent 4453f68cfa
commit 86e4d2a6b9
3 changed files with 47 additions and 13 deletions

View file

@ -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();

View file

@ -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.
*

View file

@ -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