diff --git a/src/item/Item.js b/src/item/Item.js index e1e63e6c..dc2e42b2 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1250,23 +1250,28 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ return false; } }, Base.each(['bounds', 'strokeBounds', 'handleBounds', 'roughBounds'], function(name) { + // Produce getters for bounds properties. These handle caching, matrices and + // redirect the call to the private _getBounds, which can be overridden by + // subclasses, see below. this['get' + Base.capitalize(name)] = function(/* matrix */) { var matrix = arguments[0]; // If the matrix is an identity transformation, set it to null for // faster processing if (matrix && matrix.isIdentity()) matrix = null; + // Allow subclasses to override _boundsType if they use the same + // calculations for multiple types. The default is name: + var type = this._boundsType; + if (typeof type != 'string') + type = type && type[name] || name; // See if we can cache these bounds. We only cache non-transformed // bounds on items without children, as we do not receive hierarchy // change notifiers from children, and walking up the parents and // merging cache bounds is not expensive. - // Allow subclasses to define _simpleBounds if they want to share the - // cache across all different bound types. - var cache = !this._children && !matrix - && (this._simpleBounds && 'bounds' || name); + var cache = !this._children && !matrix && type; if (cache && this._bounds && this._bounds[cache]) return this._bounds[cache]; - var bounds = this._getBounds(name, matrix); + var bounds = this._getBounds(type, matrix); // If we're returning 'bounds', create a LinkedRectangle that uses // the setBounds() setter to update the Item whenever the bounds are // changed: diff --git a/src/item/PlacedItem.js b/src/item/PlacedItem.js index 7b237e79..79a56c4a 100644 --- a/src/item/PlacedItem.js +++ b/src/item/PlacedItem.js @@ -24,6 +24,8 @@ * @extends Item */ var PlacedItem = this.PlacedItem = Item.extend(/** @lends PlacedItem# */{ + // PlacedItem uses strokeBounds for bounds + _boundsType: { bounds: 'strokeBounds' }, _transform: function(matrix, flags) { // In order to set the right context transformation when drawing the @@ -49,9 +51,6 @@ var PlacedItem = this.PlacedItem = Item.extend(/** @lends PlacedItem# */{ }, _getBounds: function(type, matrix) { - // The bounds of PlacedItems are the same as the strokeBounds - if (type == 'bounds') - type = 'strokeBounds'; // Concatenate the passed matrix with the internal one matrix = matrix ? matrix.clone().concatenate(this._matrix) : this._matrix; diff --git a/src/item/Raster.js b/src/item/Raster.js index 26a61cde..63cb8eb0 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -23,8 +23,8 @@ */ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ // Raster doesn't make the distinction between the different bounds, - // so use the same cache for all of them - _simpleBounds: true, + // so use the same name for all of them + _boundsType: 'bounds', // TODO: Implement url / type, width, height. // TODO: Have PlacedSymbol & Raster inherit from a shared class? diff --git a/src/text/TextItem.js b/src/text/TextItem.js index 75ed611e..3f17ba6c 100644 --- a/src/text/TextItem.js +++ b/src/text/TextItem.js @@ -27,8 +27,8 @@ */ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{ // TextItem doesn't make the distinction between the different bounds, - // so use the same cache for all of them - _simpleBounds: true, + // so use the same name for all of them + _boundsType: 'bounds', initialize: function() { this.base();