Do not include text-styles in items that don't relate to text.

Closes #934
This commit is contained in:
Jürg Lehni 2016-02-02 21:43:44 +01:00
parent 8fb7c41537
commit 5e53939006
3 changed files with 41 additions and 37 deletions

View file

@ -27,4 +27,3 @@ var window = self ? self.window : require('./node/window'),
// NOTE: We're not modifying the global `self` here. We receive its value passed // NOTE: We're not modifying the global `self` here. We receive its value passed
// to the paper.js function scope, and this is the one that is modified here. // to the paper.js function scope, and this is the one that is modified here.
self = self || window; self = self || window;

View file

@ -87,7 +87,11 @@ var Style = Base.extend(new function() {
shadowBlur: 0, shadowBlur: 0,
shadowOffset: new Point(), shadowOffset: new Point(),
// Selection // Selection
selectedColor: undefined, selectedColor: undefined
},
// For TextItem, override default fillColor and add text-specific properties
textDefaults = new Base(defaults, {
fillColor: new Color(), // black
// Characters // Characters
fontFamily: 'sans-serif', fontFamily: 'sans-serif',
fontWeight: 'normal', fontWeight: 'normal',
@ -95,7 +99,7 @@ var Style = Base.extend(new function() {
leading: null, leading: null,
// Paragraphs // Paragraphs
justification: 'left' justification: 'left'
}, }),
flags = { flags = {
strokeWidth: /*#=*/Change.STROKE, strokeWidth: /*#=*/Change.STROKE,
strokeCap: /*#=*/Change.STROKE, strokeCap: /*#=*/Change.STROKE,
@ -116,15 +120,26 @@ var Style = Base.extend(new function() {
beans: true beans: true
}, },
fields = { fields = {
_class: 'Style',
initialize: function Style(style, owner, project) {
// We keep values in a separate object that we can iterate over.
this._values = {};
this._owner = owner;
this._project = owner && owner._project || project || paper.project;
if (owner instanceof TextItem)
this._defaults = textDefaults;
if (style)
this.set(style);
},
_defaults: defaults, _defaults: defaults,
// Override default fillColor for text items
_textDefaults: new Base(defaults, {
fillColor: new Color() // black
}),
beans: true beans: true
}; };
Base.each(defaults, function(value, key) { // Iterate over textDefaults to inject getters / setters, to cover all
// properties
Base.each(textDefaults, function(value, key) {
var isColor = /Color$/.test(key), var isColor = /Color$/.test(key),
isPoint = key === 'shadowOffset', isPoint = key === 'shadowOffset',
part = Base.capitalize(key), part = Base.capitalize(key),
@ -149,7 +164,7 @@ var Style = Base.extend(new function() {
&& !(owner instanceof CompoundPath)) { && !(owner instanceof CompoundPath)) {
for (var i = 0, l = children.length; i < l; i++) for (var i = 0, l = children.length; i < l; i++)
children[i]._style[set](value); children[i]._style[set](value);
} else { } else if (key in this._defaults) {
var old = this._values[key]; var old = this._values[key];
if (old !== value) { if (old !== value) {
if (isColor) { if (isColor) {
@ -182,8 +197,8 @@ var Style = Base.extend(new function() {
// If the owner has children, walk through all of them and see if // If the owner has children, walk through all of them and see if
// they all have the same style. // they all have the same style.
// If true is passed for _dontMerge, don't merge children styles // If true is passed for _dontMerge, don't merge children styles
if (!children || children.length === 0 || _dontMerge if (key in this._defaults && (!children || children.length === 0
|| owner instanceof CompoundPath) { || _dontMerge || owner instanceof CompoundPath)) {
var value = this._values[key]; var value = this._values[key];
if (value === undefined) { if (value === undefined) {
value = this._defaults[key]; value = this._defaults[key];
@ -200,16 +215,16 @@ var Style = Base.extend(new function() {
value._owner = owner; value._owner = owner;
} }
} }
return value; } else if (children) {
} for (var i = 0, l = children.length; i < l; i++) {
for (var i = 0, l = children.length; i < l; i++) { var childValue = children[i]._style[get]();
var childValue = children[i]._style[get](); if (i === 0) {
if (i === 0) { value = childValue;
value = childValue; } else if (!Base.equals(value, childValue)) {
} else if (!Base.equals(value, childValue)) { // If there is another child with a different
// If there is another child with a different // style, the style is not defined:
// style, the style is not defined: return undefined;
return undefined; }
} }
} }
return value; return value;
@ -242,19 +257,6 @@ var Style = Base.extend(new function() {
Item.inject(item); Item.inject(item);
return fields; return fields;
}, /** @lends Style# */{ }, /** @lends Style# */{
_class: 'Style',
initialize: function Style(style, _owner, _project) {
// We keep values in a separate object that we can iterate over.
this._values = {};
this._owner = _owner;
this._project = _owner && _owner._project || _project || paper.project;
if (_owner instanceof TextItem)
this._defaults = this._textDefaults;
if (style)
this.set(style);
},
set: function(style) { set: function(style) {
// If the passed style object is also a Style, clone its clonable // If the passed style object is also a Style, clone its clonable
// fields rather than simply copying them. // fields rather than simply copying them.

View file

@ -248,10 +248,13 @@ var compareItem = function(actual, expected, message, options, properties) {
if (properties) if (properties)
compareProperties(actual, expected, properties, message, options); compareProperties(actual, expected, properties, message, options);
// Style // Style
compareProperties(actual.style, expected.style, ['fillColor', var styles = ['fillColor',
'strokeColor', 'strokeCap', 'strokeJoin', 'dashArray', 'strokeColor', 'strokeCap', 'strokeJoin', 'dashArray',
'dashOffset', 'miterLimit', 'fontSize', 'font', 'leading', 'dashOffset', 'miterLimit'];
'justification'], message + '.style', options); if (expected instanceof TextItem)
styles.push('fontSize', 'font', 'leading', 'justification');
compareProperties(actual.style, expected.style, styles,
message + '.style', options);
} }
}; };