Further simplify SVG attribute / style handling by merging in SvgStyles.

This commit is contained in:
Jürg Lehni 2013-02-28 15:57:47 -08:00
parent 8585556584
commit 84f3f041e3
3 changed files with 15 additions and 20 deletions

View file

@ -444,7 +444,7 @@ new function() {
if (item._name != null)
attrs.id = item._name;
Base.each(SvgStyles.properties, function(entry) {
Base.each(SvgStyles, function(entry) {
// Get a given style only if it differs from the value on the parent
// (A layer or group which can have style values in SVG).
var value = style[entry.get]();

View file

@ -408,7 +408,12 @@ new function() {
}
}
var attributes = {
// Create apply methos for attributes, and merge in those for SvgStlyes:
var attributes = Base.each(SvgStyles, function(entry) {
this[entry.attribute] = function(item, node, name, value) {
item._style[entry.set](convertValue(value, entry.type));
};
}, {
id: function(item, node, name, value) {
definitions[value] = item;
if (item.setName)
@ -474,7 +479,7 @@ new function() {
// TODO: the viewbox does not always need to be clipped
return createClipGroup(item, new Path.Rectangle(rectangle));
}
};
});
/**
* Converts various SVG styles and attributes into Paper.js styles and
@ -509,18 +514,11 @@ new function() {
* @param value the value of the SVG style
*/
function applyAttribute(item, node, name, value) {
if (value == null)
return item;
var entry = SvgStyles.attributes[name];
if (entry) {
item._style[entry.set](convertValue(value, entry.type));
} else {
var attribute = attributes[name];
if (attribute) {
var res = attribute(item, node, name, value);
if (res !== undefined)
item = res;
}
var attribute;
if (value != null && (attribute = attributes[name])) {
var res = attribute(item, node, name, value);
if (res !== undefined)
item = res;
}
return item;
}

View file

@ -21,14 +21,11 @@ var SvgStyles = Base.each({
dashOffset: ['stroke-dashoffset', 'number']
}, function(entry, key) {
var part = Base.capitalize(key);
this.attributes[entry[0]] = this.properties[key] = {
this[key] = {
type: entry[1],
property: key,
attribute: entry[0],
get: 'get' + part,
set: 'set' + part
};
}, {
properties: {},
attributes: {}
});
}, {});