From 781b31580874d61e53b7362544e60a44dbf51590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 30 Apr 2011 23:22:29 +0100 Subject: [PATCH] Add getter/setter for Path#closed. --- src/path/CompoundPath.js | 2 +- src/path/Curve.js | 4 ++-- src/path/Path.Constructors.js | 10 +++++----- src/path/Path.js | 33 ++++++++++++++++++++------------- src/path/Segment.js | 6 +++--- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index 656e8311..2d87ed58 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -89,7 +89,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend({ }, closePath: function() { - getCurrentPath(this).closed = true; + getCurrentPath(this).setClosed(true); } }; diff --git a/src/path/Curve.js b/src/path/Curve.js index ae1dea5a..c9349696 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -132,13 +132,13 @@ var Curve = this.Curve = Base.extend({ // TODO: No need to call getCurves() here? var curves = this._path && this._path._curves; return curves && (curves[this._index1 + 1] - || this._path.closed && curves[0]) || null; + || this._path._closed && curves[0]) || null; }, getPrevious: function() { var curves = this._path && this._path._curves; return curves && (curves[this._index1 - 1] - || this._path.closed && curves[curves.length - 1]) || null; + || this._path._closed && curves[curves.length - 1]) || null; }, setSelected: function(selected) { diff --git a/src/path/Path.Constructors.js b/src/path/Path.Constructors.js index 7e9979ee..23616842 100644 --- a/src/path/Path.Constructors.js +++ b/src/path/Path.Constructors.js @@ -41,7 +41,7 @@ Path.inject({ statics: new function() { for (var i = 0; i < 4; i++) { path.add(rect[corners[i]]()); } - path.closed = true; + path.setClosed(true); return path; }, @@ -74,7 +74,7 @@ Path.inject({ statics: new function() { path.add(br.subtract(0, size.height), null, [0, uSize.height]); path.add(br.subtract(size.width, 0), [uSize.width, 0], null); - path.closed = true; + path.setClosed(true); return path; }, @@ -91,7 +91,7 @@ Path.inject({ statics: new function() { segment._handleOut.multiply(size) )); } - path.closed = true; + path.setClosed(true); return path; }, @@ -123,7 +123,7 @@ Path.inject({ statics: new function() { var angle = (360 / numSides) * (i + offset); path.add(center.add(vector.rotate(angle))); } - path.closed = true; + path.setClosed(true); return path; }, @@ -138,7 +138,7 @@ Path.inject({ statics: new function() { length: i % 2 ? radius2 : radius1 })); } - path.closed = true; + path.setClosed(true); return path; } }; diff --git a/src/path/Path.js b/src/path/Path.js index f8a73a06..ecb030da 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -19,7 +19,7 @@ var Path = this.Path = PathItem.extend({ initialize: function(segments) { this.base(); - this.closed = false; + this._closed = false; this._selectedSegmentCount = 0; // Support both passing of segments as array or arguments // If it is an array, it can also be a description of a point, so @@ -34,7 +34,7 @@ var Path = this.Path = PathItem.extend({ getCurves: function() { var length = this._segments.length; // Reduce length by one if it's an open path: - if (!this.closed && length > 0) + if (!this._closed && length > 0) length--; var curves = this._curves = this._curves || new Array(length); curves.length = length; @@ -50,6 +50,14 @@ var Path = this.Path = PathItem.extend({ return curves; }, + getClosed: function() { + return this._closed; + }, + + setClosed: function(closed) { + this._closed = closed; + }, + getFirstSegment: function() { return this._segments[0]; }, @@ -198,7 +206,7 @@ var Path = this.Path = PathItem.extend({ if (last1._point.equals(first1._point)) { first1.setHandleIn(last1._handleIn); last1.remove(); - this.closed = true; + this.setClosed(true); } return true; } @@ -359,7 +367,7 @@ var Path = this.Path = PathItem.extend({ outX = handleOut._x + x; outY = handleOut._y + y; } - if (this.closed && length > 1) { + if (this._closed && length > 1) { var segment = segments[0], point = segment._point, x = point._x, @@ -495,7 +503,7 @@ var Path = this.Path = PathItem.extend({ if (size <= 2) return; - if (this.closed) { + if (this._closed) { // Overlap up to 4 points since averaging beziers affect the 4 // neighboring points overlap = Math.min(size, 4); @@ -506,7 +514,7 @@ var Path = this.Path = PathItem.extend({ var knots = []; for (var i = 0; i < size; i++) knots[i + overlap] = segments[i]._point; - if (this.closed) { + if (this._closed) { // If we're averaging, add the 4 last points again at the // beginning, and the 4 first ones at the end. for (var i = 0; i < overlap; i++) { @@ -536,7 +544,7 @@ var Path = this.Path = PathItem.extend({ // Get first control points Y-values var y = getFirstControlPoints(rhs); - if (this.closed) { + if (this._closed) { // Do the actual averaging simply by linearly fading between the // overlapping values. for (var i = 0, j = size; i < overlap; i++, j++) { @@ -571,7 +579,7 @@ var Path = this.Path = PathItem.extend({ (knots[n]._y + y[n - 1]) / 2); } } - if (this.closed && handleIn) { + if (this._closed && handleIn) { var segment = this._segments[0]; segment.setHandleIn(handleIn.subtract(segment._point)); } @@ -781,7 +789,7 @@ var Path = this.Path = PathItem.extend({ }, closePath: function() { - this.closed = true; + this.setClosed(true); } }; }, new function() { // A dedicated scope for the tricky bounds calculations @@ -878,7 +886,7 @@ var Path = this.Path = PathItem.extend({ } for (var i = 1, l = segments.length; i < l; i++) processSegment(segments[i]); - if (that.closed) + if (that._closed) processSegment(first); return Rectangle.create(min[0], min[1], max[0] - min[0], max[1] - min[1]); @@ -954,7 +962,6 @@ var Path = this.Path = PathItem.extend({ miter = this.getMiterLimit() * width / 2, segments = this._segments, length = segments.length, - closed= this.closed, // It seems to be compatible with Ai we need to pass pen padding // untransformed to getBounds() bounds = getBounds(this, matrix, getPenPadding(radius)); @@ -1034,10 +1041,10 @@ var Path = this.Path = PathItem.extend({ } } - for (var i = 1, l = length - (closed ? 0 : 1); i < l; i++) { + for (var i = 1, l = length - (this._closed ? 0 : 1); i < l; i++) { addJoin(segments[i], join); } - if (closed) { + if (this._closed) { addJoin(segments[0], join); } else { addCap(segments[0], cap, 0); diff --git a/src/path/Segment.js b/src/path/Segment.js index 7de08259..6207a12f 100644 --- a/src/path/Segment.js +++ b/src/path/Segment.js @@ -110,7 +110,7 @@ var Segment = this.Segment = Base.extend({ if (this._path != null) { var index = this.getIndex(); // The last segment of an open path belongs to the last curve - if (!this._path.closed && index == this._path._segments.length - 1) + if (!this._path._closed && index == this._path._segments.length - 1) index--; return this._path.getCurves()[index] || null; } @@ -120,13 +120,13 @@ var Segment = this.Segment = Base.extend({ getNext: function() { var segments = this._path && this._path._segments; return segments && (segments[this.getIndex() + 1] - || this._path.closed && segments[0]) || null; + || this._path._closed && segments[0]) || null; }, getPrevious: function() { var segments = this._path && this._path._segments; return segments && (segments[this.getIndex() - 1] - || this._path.closed && segments[segments.length - 1]) || null; + || this._path._closed && segments[segments.length - 1]) || null; }, _isSelected: function(point) {