mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Move more styles to SVGStyles and make them work on export too.
SVGExport now supports text justification.
This commit is contained in:
parent
2770a80a00
commit
f48ef4d1a0
3 changed files with 33 additions and 28 deletions
|
@ -429,15 +429,18 @@ new function() {
|
|||
}
|
||||
attrs[entry.attribute] = value == null
|
||||
? 'none'
|
||||
: entry.type === 'color'
|
||||
? value.gradient
|
||||
? exportGradient(value, item)
|
||||
: value.toCSS(true) // false for noAlpha, see above
|
||||
: entry.type === 'array'
|
||||
? value.join(',')
|
||||
: entry.type === 'number'
|
||||
? formatter.number(value)
|
||||
: value;
|
||||
: entry.type === 'number'
|
||||
? formatter.number(value)
|
||||
: entry.type === 'color'
|
||||
? value.gradient
|
||||
? exportGradient(value, item)
|
||||
// true for noAlpha, see above
|
||||
: value.toCSS(true)
|
||||
: entry.type === 'array'
|
||||
? value.join(',')
|
||||
: entry.type === 'lookup'
|
||||
? entry.toSVG[value]
|
||||
: value;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -55,15 +55,18 @@ new function() {
|
|||
}
|
||||
|
||||
// Converts a string attribute value to the specified type
|
||||
function convertValue(value, type) {
|
||||
function convertValue(value, type, lookup) {
|
||||
return value === 'none'
|
||||
? null
|
||||
: type === 'number'
|
||||
? parseFloat(value)
|
||||
: type === 'array'
|
||||
? value ? value.split(/[\s,]+/g).map(parseFloat) : []
|
||||
: type === 'color' && getDefinition(value)
|
||||
|| value;
|
||||
: type === 'color'
|
||||
? getDefinition(value) || value
|
||||
: type === 'lookup'
|
||||
? lookup[value]
|
||||
: value;
|
||||
}
|
||||
|
||||
// Importer functions for various SVG node types
|
||||
|
@ -336,7 +339,8 @@ new function() {
|
|||
// can affect gradient fills.
|
||||
var attributes = Base.merge(Base.each(SVGStyles, function(entry) {
|
||||
this[entry.attribute] = function(item, value, name, node) {
|
||||
item._style[entry.set](convertValue(value, entry.type));
|
||||
item._style[entry.set](
|
||||
convertValue(value, entry.type, entry.fromSVG));
|
||||
};
|
||||
}, {}), {
|
||||
id: function(item, value) {
|
||||
|
@ -375,19 +379,6 @@ new function() {
|
|||
item.setFont(value.split(',')[0].replace(/^\s+|\s+$/g, ''));
|
||||
},
|
||||
|
||||
'font-size': function(item, value) {
|
||||
item.setFontSize(parseFloat(value));
|
||||
},
|
||||
|
||||
'text-anchor': function(item, value) {
|
||||
// http://www.w3.org/TR/SVG/text.html#TextAnchorProperty
|
||||
item.setJustification({
|
||||
start: 'left',
|
||||
middle: 'center',
|
||||
end: 'right'
|
||||
}[value]);
|
||||
},
|
||||
|
||||
visibility: function(item, value) {
|
||||
item.setVisible(value === 'visible');
|
||||
},
|
||||
|
|
|
@ -18,13 +18,24 @@ var SVGStyles = Base.each({
|
|||
strokeJoin: ['stroke-linejoin', 'string'],
|
||||
miterLimit: ['stroke-miterlimit', 'number'],
|
||||
dashArray: ['stroke-dasharray', 'array'],
|
||||
dashOffset: ['stroke-dashoffset', 'number']
|
||||
dashOffset: ['stroke-dashoffset', 'number'],
|
||||
justification: ['text-anchor', 'lookup', {
|
||||
left: 'start',
|
||||
center: 'middle',
|
||||
right: 'end'
|
||||
}],
|
||||
fontSize: ['font-size', 'number']
|
||||
}, function(entry, key) {
|
||||
var part = Base.capitalize(key);
|
||||
var part = Base.capitalize(key),
|
||||
lookup = entry[2];
|
||||
this[key] = {
|
||||
type: entry[1],
|
||||
property: key,
|
||||
attribute: entry[0],
|
||||
toSVG: lookup,
|
||||
fromSVG: lookup && Base.each(lookup, function(value, name) {
|
||||
this[value] = name;
|
||||
}, {}),
|
||||
get: 'get' + part,
|
||||
set: 'set' + part
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue