2011-06-20 08:56:49 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-20 08:56:49 -04:00
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-20 08:56:49 -04:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-06-20 08:56:49 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal base-class for all style objects, e.g. PathStyle, CharacterStyle,
|
|
|
|
* PargraphStyle.
|
|
|
|
*/
|
|
|
|
var Style = Item.extend({
|
2011-06-20 09:10:37 -04:00
|
|
|
initialize: function(style) {
|
2011-06-20 09:58:48 -04:00
|
|
|
// If the passed style object is also a Style, clone its clonable
|
|
|
|
// fields rather than simply copying them.
|
|
|
|
var clone = style instanceof Style;
|
|
|
|
// Note: This relies on bean getters and setters that get implicetly
|
|
|
|
// called when getting from style[key] and setting on this[key].
|
|
|
|
return Base.each(this._defaults, function(value, key) {
|
|
|
|
value = style && style[key] || value;
|
|
|
|
this[key] = value && clone && value.clone
|
|
|
|
? value.clone() : value;
|
2011-06-20 09:10:37 -04:00
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
2011-06-20 08:56:49 -04:00
|
|
|
statics: {
|
|
|
|
create: function(item) {
|
|
|
|
var style = new this(this.dont);
|
|
|
|
style._item = item;
|
|
|
|
return style;
|
2011-06-20 09:58:48 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
extend: function(src) {
|
|
|
|
// Inject style getters and setters into the 'owning' class, which
|
|
|
|
// redirect calls to the linked style objects through their internal
|
|
|
|
// property on the instances of that class, as defined by _style.
|
2011-06-20 10:21:42 -04:00
|
|
|
var styleKey = src._style,
|
|
|
|
flags = src._flags || {};
|
2011-06-20 09:58:48 -04:00
|
|
|
src._owner.inject(Base.each(src._defaults, function(value, key) {
|
2011-06-20 10:21:42 -04:00
|
|
|
var isColor = !!key.match(/Color$/),
|
|
|
|
part = Base.capitalize(key),
|
2011-06-20 09:58:48 -04:00
|
|
|
set = 'set' + part,
|
|
|
|
get = 'get' + part;
|
2011-06-20 10:21:42 -04:00
|
|
|
// Simply extend src with these getters and setters, to be
|
|
|
|
// injected into this class using this.base() further down.
|
|
|
|
src[set] = function(value) {
|
|
|
|
var children = this._item && this._item._children;
|
|
|
|
value = isColor ? Color.read(arguments) : value;
|
|
|
|
if (children) {
|
|
|
|
for (var i = 0, l = children.length; i < l; i++)
|
|
|
|
children[i][styleKey][set](value);
|
|
|
|
} else {
|
|
|
|
var old = this['_' + key];
|
|
|
|
if (old != value && !(old && old.equals
|
|
|
|
&& old.equals(value))) {
|
|
|
|
this['_' + key] = value;
|
|
|
|
if (isColor) {
|
|
|
|
if (old)
|
|
|
|
old._removeOwner(this._item);
|
|
|
|
if (value)
|
|
|
|
value._addOwner(this._item);
|
|
|
|
}
|
|
|
|
if (this._item)
|
|
|
|
this._item._changed(flags[key] || Change.STYLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
src[get] = function() {
|
|
|
|
var children = this._item && this._item._children,
|
|
|
|
style;
|
|
|
|
// If this item has children, walk through all of them and
|
|
|
|
// see if they all have the same style.
|
|
|
|
if (!children)
|
|
|
|
return this['_' + key];
|
|
|
|
for (var i = 0, l = children.length; i < l; i++) {
|
|
|
|
var childStyle = children[i][styleKey][get]();
|
|
|
|
if (!style) {
|
|
|
|
style = childStyle;
|
|
|
|
} else if (style != childStyle && !(style
|
|
|
|
&& style.equals && style.equals(childStyle))) {
|
|
|
|
// If there is another item with a different
|
|
|
|
// style, the style is not defined:
|
|
|
|
// PORT: Change this in Scriptographer
|
|
|
|
// (currently returns null)
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
};
|
|
|
|
// Style-getters and setters for owner class:
|
|
|
|
// 'this' = the Base.each() side-car = the object that is
|
|
|
|
// returned from Base.each and injected into _owner above:
|
2011-06-20 09:58:48 -04:00
|
|
|
this[set] = function(value) {
|
2011-06-20 10:21:42 -04:00
|
|
|
this[styleKey][set](value);
|
2011-06-20 09:58:48 -04:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
this[get] = function() {
|
2011-06-20 10:21:42 -04:00
|
|
|
return this[styleKey][get]();
|
2011-06-20 09:58:48 -04:00
|
|
|
};
|
|
|
|
}, {}));
|
|
|
|
return this.base(src);
|
2011-06-20 08:56:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|