Skip to content

Commit f48ef4d

Browse files
committed
Move more styles to SVGStyles and make them work on export too.
SVGExport now supports text justification.
1 parent 2770a80 commit f48ef4d

3 files changed

Lines changed: 33 additions & 28 deletions

File tree

src/svg/SVGExport.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -429,15 +429,18 @@ new function() {
429429
}
430430
attrs[entry.attribute] = value == null
431431
? 'none'
432-
: entry.type === 'color'
433-
? value.gradient
434-
? exportGradient(value, item)
435-
: value.toCSS(true) // false for noAlpha, see above
436-
: entry.type === 'array'
437-
? value.join(',')
438-
: entry.type === 'number'
439-
? formatter.number(value)
440-
: value;
432+
: entry.type === 'number'
433+
? formatter.number(value)
434+
: entry.type === 'color'
435+
? value.gradient
436+
? exportGradient(value, item)
437+
// true for noAlpha, see above
438+
: value.toCSS(true)
439+
: entry.type === 'array'
440+
? value.join(',')
441+
: entry.type === 'lookup'
442+
? entry.toSVG[value]
443+
: value;
441444
}
442445
});
443446

src/svg/SVGImport.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,18 @@ new function() {
5555
}
5656

5757
// Converts a string attribute value to the specified type
58-
function convertValue(value, type) {
58+
function convertValue(value, type, lookup) {
5959
return value === 'none'
6060
? null
6161
: type === 'number'
6262
? parseFloat(value)
6363
: type === 'array'
6464
? value ? value.split(/[\s,]+/g).map(parseFloat) : []
65-
: type === 'color' && getDefinition(value)
66-
|| value;
65+
: type === 'color'
66+
? getDefinition(value) || value
67+
: type === 'lookup'
68+
? lookup[value]
69+
: value;
6770
}
6871

6972
// Importer functions for various SVG node types
@@ -336,7 +339,8 @@ new function() {
336339
// can affect gradient fills.
337340
var attributes = Base.merge(Base.each(SVGStyles, function(entry) {
338341
this[entry.attribute] = function(item, value, name, node) {
339-
item._style[entry.set](convertValue(value, entry.type));
342+
item._style[entry.set](
343+
convertValue(value, entry.type, entry.fromSVG));
340344
};
341345
}, {}), {
342346
id: function(item, value) {
@@ -375,19 +379,6 @@ new function() {
375379
item.setFont(value.split(',')[0].replace(/^\s+|\s+$/g, ''));
376380
},
377381

378-
'font-size': function(item, value) {
379-
item.setFontSize(parseFloat(value));
380-
},
381-
382-
'text-anchor': function(item, value) {
383-
// http://www.w3.org/TR/SVG/text.html#TextAnchorProperty
384-
item.setJustification({
385-
start: 'left',
386-
middle: 'center',
387-
end: 'right'
388-
}[value]);
389-
},
390-
391382
visibility: function(item, value) {
392383
item.setVisible(value === 'visible');
393384
},

src/svg/SVGStyles.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@ var SVGStyles = Base.each({
1818
strokeJoin: ['stroke-linejoin', 'string'],
1919
miterLimit: ['stroke-miterlimit', 'number'],
2020
dashArray: ['stroke-dasharray', 'array'],
21-
dashOffset: ['stroke-dashoffset', 'number']
21+
dashOffset: ['stroke-dashoffset', 'number'],
22+
justification: ['text-anchor', 'lookup', {
23+
left: 'start',
24+
center: 'middle',
25+
right: 'end'
26+
}],
27+
fontSize: ['font-size', 'number']
2228
}, function(entry, key) {
23-
var part = Base.capitalize(key);
29+
var part = Base.capitalize(key),
30+
lookup = entry[2];
2431
this[key] = {
2532
type: entry[1],
2633
property: key,
2734
attribute: entry[0],
35+
toSVG: lookup,
36+
fromSVG: lookup && Base.each(lookup, function(value, name) {
37+
this[value] = name;
38+
}, {}),
2839
get: 'get' + part,
2940
set: 'set' + part
3041
};

0 commit comments

Comments
 (0)