Implement SVGImport options support.

For now only options.expandShapes, which expands 	Shape items to Path items.
This commit is contained in:
Jürg Lehni 2013-10-29 16:47:02 +01:00
parent d649b1bb21
commit c765e26a08

View file

@ -71,7 +71,7 @@ new function() {
// Importer functions for various SVG node types // Importer functions for various SVG node types
function importGroup(node, type) { function importGroup(node, type, options) {
var nodes = node.childNodes, var nodes = node.childNodes,
clip = type === 'clippath', clip = type === 'clippath',
item = new Group(), item = new Group(),
@ -94,7 +94,8 @@ new function() {
for (var i = 0, l = nodes.length; i < l; i++) { for (var i = 0, l = nodes.length; i < l; i++) {
var childNode = nodes[i], var childNode = nodes[i],
child; child;
if (childNode.nodeType === 1 && (child = importSVG(childNode)) if (childNode.nodeType === 1
&& (child = importSVG(childNode, options))
&& !(child instanceof Symbol)) && !(child instanceof Symbol))
children.push(child); children.push(child);
} }
@ -165,8 +166,8 @@ new function() {
// NOTE: All importers are lowercase, since jsdom is using uppercase // NOTE: All importers are lowercase, since jsdom is using uppercase
// nodeNames still. // nodeNames still.
var importers = { var importers = {
'#document': function(node) { '#document': function(node, type, options) {
return importSVG(node.childNodes[0]); return importSVG(node.childNodes[0], options);
}, },
// http://www.w3.org/TR/SVG/struct.html#Groups // http://www.w3.org/TR/SVG/struct.html#Groups
@ -478,15 +479,21 @@ new function() {
return match && definitions[match[1]]; return match && definitions[match[1]];
} }
function importSVG(node, clearDefs) { function importSVG(node, options, clearDefs) {
if (!options)
options = {};
if (typeof node === 'string') if (typeof node === 'string')
node = new DOMParser().parseFromString(node, 'image/svg+xml'); node = new DOMParser().parseFromString(node, 'image/svg+xml');
// jsdom in Node.js uses uppercase values for nodeName... // jsdom in Node.js uses uppercase values for nodeName...
var type = node.nodeName.toLowerCase(), var type = node.nodeName.toLowerCase(),
importer = importers[type], importer = importers[type],
item = importer && importer(node, type), item = importer && importer(node, type, options),
data = type !== '#document' && node.getAttribute('data-paper-data'); data = type !== '#document' && node.getAttribute('data-paper-data');
// See importGroup() for an explanation of this filtering: // See importGroup() for an explanation of this filtering:
if (options.expandShapes && item instanceof Shape) {
item.remove();
item = item.toPath();
}
if (item && !(item instanceof Group)) if (item && !(item instanceof Group))
item = applyAttributes(item, node); item = applyAttributes(item, node);
if (item && data) if (item && data)
@ -498,15 +505,15 @@ new function() {
} }
Item.inject({ Item.inject({
importSVG: function(node) { importSVG: function(node, options) {
return this.addChild(importSVG(node, true)); return this.addChild(importSVG(node, options, true));
} }
}); });
Project.inject({ Project.inject({
importSVG: function(node) { importSVG: function(node, options) {
this.activate(); this.activate();
return importSVG(node, true); return importSVG(node, options, true);
} }
}); });
}; };