Improve handling of merged CompoundPath style through #getStyle().

Only access _style directly in core code if you really know what you're doing!
This commit is contained in:
Jürg Lehni 2013-06-12 20:12:08 -07:00
parent 33eb750586
commit 95ecab8a6f
5 changed files with 21 additions and 31 deletions

View file

@ -381,15 +381,17 @@ var Item = Base.extend(Callback, /** @lends Item# */{
}, },
setStyle: function(style) { setStyle: function(style) {
this._style.set(style); // Don't access _style directly so Path#getStyle() can be overriden for
// CompoundPaths.
this.getStyle().set(style);
}, },
hasFill: function() { hasFill: function() {
return !!this._style.getFillColor(); return !!this.getStyle().getFillColor();
}, },
hasStroke: function() { hasStroke: function() {
return !!this._style.getStrokeColor(); return !!this.getStyle().getStrokeColor();
} }
}, Base.each(['locked', 'visible', 'blendMode', 'opacity', 'guide'], }, Base.each(['locked', 'visible', 'blendMode', 'opacity', 'guide'],
// Produce getter/setters for properties. We need setters because we want to // Produce getter/setters for properties. We need setters because we want to
@ -2890,8 +2892,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{
var blending = this._blendMode !== 'normal', var blending = this._blendMode !== 'normal',
parentCtx, itemOffset, prevOffset; parentCtx, itemOffset, prevOffset;
if (blending || this._opacity < 1 && this._type !== 'raster' if (blending || this._opacity < 1 && this._type !== 'raster'
&& (this._type !== 'path' && (this._type !== 'path'
|| this.getFillColor() && this.getStrokeColor())) { || this.hasFill() && this.hasStroke())) {
// Apply the paren't global matrix to the calculation of correct // Apply the paren't global matrix to the calculation of correct
// bounds. // bounds.
var bounds = this.getStrokeBounds(parentMatrix); var bounds = this.getStrokeBounds(parentMatrix);

View file

@ -227,7 +227,7 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
_hitTest: function _hitTest(point, options) { _hitTest: function _hitTest(point, options) {
var res = _hitTest.base.call(this, point, var res = _hitTest.base.call(this, point,
Base.merge(options, { fill: false })); Base.merge(options, { fill: false }));
if (!res && options.fill && this._style.getFillColor()) { if (!res && options.fill && this.hasFill()) {
res = this._contains(point); res = this._contains(point);
res = res ? new HitResult('fill', res[0]) : null; res = res ? new HitResult('fill', res[0]) : null;
} }

View file

@ -729,8 +729,8 @@ statics: {
if (!bounds) { if (!bounds) {
// Calculate the curve bounds by passing a segment list for the // Calculate the curve bounds by passing a segment list for the
// curve to the static Path.get*Boudns methods. // curve to the static Path.get*Boudns methods.
bounds = this._bounds[name] = Path[name]( bounds = this._bounds[name] = Path[name]([this._segment1,
[this._segment1, this._segment2], false, this._path._style); this._segment2], false, this._path.getStyle());
} }
return bounds.clone(); return bounds.clone();
}; };

View file

@ -204,7 +204,6 @@ var Path = PathItem.extend(/** @lends Path# */{
*/ */
getPathData: function(/* precision */) { getPathData: function(/* precision */) {
var segments = this._segments, var segments = this._segments,
style = this._style,
precision = arguments[0], precision = arguments[0],
f = Formatter.instance, f = Formatter.instance,
parts = []; parts = [];
@ -1585,18 +1584,11 @@ var Path = PathItem.extend(/** @lends Path# */{
return this.getNearestLocation(point).getPoint(); return this.getNearestLocation(point).getPoint();
}, },
hasFill: function() { getStyle: function() {
// If this path is part of a CompoundPath, we need to check that // If this path is part of a CompoundPath, use the paren't style instead
// for fillColor...
var parent = this._parent; var parent = this._parent;
return !!(parent && parent._type === 'compound-path' return (parent && parent._type === 'compound-path'
? parent : this)._style.getFillColor(); ? parent : this)._style;
},
hasStroke: function() {
var parent = this._parent;
return !!(parent && parent._type === 'compound-path'
? parent : this)._style.getStrokeColor();
}, },
_contains: function(point) { _contains: function(point) {
@ -1641,9 +1633,7 @@ var Path = PathItem.extend(/** @lends Path# */{
}, },
_hitTest: function(point, options) { _hitTest: function(point, options) {
// See #draw() for an explanation of why we can access _style properties var style = this.getStyle(),
// directly here:
var style = this._style,
tolerance = options.tolerance || 0, tolerance = options.tolerance || 0,
radius = (options.stroke && style.getStrokeColor() radius = (options.stroke && style.getStrokeColor()
? style.getStrokeWidth() / 2 : 0) + tolerance, ? style.getStrokeWidth() / 2 : 0) + tolerance,
@ -1829,10 +1819,7 @@ var Path = PathItem.extend(/** @lends Path# */{
if (!compound) if (!compound)
ctx.beginPath(); ctx.beginPath();
// We can access styles directly on the internal _styles object, var style = this.getStyle(),
// since Path items do not have children, thus do not need style
// accessors for merged styles.
var style = this._style,
fillColor = style.getFillColor(), fillColor = style.getFillColor(),
strokeColor = style.getStrokeColor(), strokeColor = style.getStrokeColor(),
dashArray = style.getDashArray(), dashArray = style.getDashArray(),
@ -2215,7 +2202,8 @@ var Path = PathItem.extend(/** @lends Path# */{
_getBounds: function(getter, matrix) { _getBounds: function(getter, matrix) {
// See #draw() for an explanation of why we can access _style // See #draw() for an explanation of why we can access _style
// properties directly here: // properties directly here:
return Path[getter](this._segments, this._closed, this._style, matrix); return Path[getter](this._segments, this._closed, this.getStyle(),
matrix);
}, },
// Mess with indentation in order to get more line-space below... // Mess with indentation in order to get more line-space below...

View file

@ -199,7 +199,7 @@ new function() {
function exportText(item) { function exportText(item) {
var attrs = getTransform(item, true), var attrs = getTransform(item, true),
style = item._style, style = item.getStyle(),
font = style.getFont(), font = style.getFont(),
fontSize = style.getFontSize(); fontSize = style.getFontSize();
if (font) if (font)
@ -407,9 +407,9 @@ new function() {
function applyStyle(item, node) { function applyStyle(item, node) {
var attrs = {}, var attrs = {},
style = item._style, style = item.getStyle(),
parent = item.getParent(), parent = item.getParent(),
parentStyle = parent && parent._style; parentStyle = parent && parent.getStyle();
if (item._name != null) if (item._name != null)
attrs.id = item._name; attrs.id = item._name;