diff --git a/src/svg/SvgExporter.js b/src/svg/SvgExporter.js index 3fd71c1b..c20c3f50 100644 --- a/src/svg/SvgExporter.js +++ b/src/svg/SvgExporter.js @@ -347,11 +347,8 @@ var SvgExporter = this.SvgExporter = new function() { parent = item.getParent(), parentStyle = parent && parent._style; - if (item._id != null) - attrs.id = item._id; - // Same thing as stroke color except with the item name if (item._name != null) - attrs.name = item._name; + attrs.id = item._name; Base.each(SvgStyles.properties, function(entry) { // Get a given style only if it differs from the value on the parent diff --git a/src/svg/SvgImporter.js b/src/svg/SvgImporter.js index c88ee655..e2791994 100644 --- a/src/svg/SvgImporter.js +++ b/src/svg/SvgImporter.js @@ -262,58 +262,44 @@ var SvgImporter = this.SvgImporter = new function() { function applyAttributeOrStyle(svg, item, name, value) { if (value == null) return; - switch (name) { - case 'id': - item.setName(value); - break; - case 'fill': - if (value !== 'none') - item.setFillColor(value); - break; - case 'stroke': - if (value !== 'none') - item.setStrokeColor(value); - break; - case 'stroke-width': - item.setStrokeWidth(parseFloat(value, 10)); - break; - case 'stroke-linecap': - item.setStrokeCap(value); - break; - case 'stroke-linejoin': - item.setStrokeJoin(value); - break; - case 'stroke-dasharray': - value = value.replace(/px/g, '').replace(/, /g, ',') - .replace(/ /g, ',').split(','); - for (var i = 0, l = value.length; i < l; i++) - value[i] = parseFloat(value[i], 10); - item.setDashArray(value); - break; - case 'stroke-dashoffset': - item.setDashOffset(parseFloat(value, 10)); - break; - case 'stroke-miterlimit': - item.setMiterLimit(parseFloat(value, 10)); - break; - case 'transform': - applyTransform(svg, item); - break; - case 'opacity': - item.setOpacity(parseFloat(value, 10)); - break; - case 'visibility': - item.setVisibility(value === 'visible'); - break; - case 'font': - case 'font-family': - case 'font-size': - case 'text-anchor': - applyTextStyle(svg, item, name, value); - break; - default: - // Not supported yet. - break; + if (value === 'none') + value = null; + var entry = SvgStyles.attributes[name]; + if (entry) { + var style = item._style; + if (entry.type === 'number') { + value = parseFloat(value, 10); + } else if (entry.type === 'array') { + value = value.replace(/px/g, '').replace(/, /g, ',') + .replace(/ /g, ',').split(','); + for (var i = 0, l = value.length; i < l; i++) + value[i] = parseFloat(value[i], 10); + } + style[entry.set](value); + } else { + switch (name) { + case 'id': + item.setName(value); + break; + case 'transform': + applyTransform(svg, item); + break; + case 'opacity': + item.setOpacity(parseFloat(value, 10)); + break; + case 'visibility': + item.setVisibility(value === 'visible'); + break; + case 'font': + case 'font-family': + case 'font-size': + case 'text-anchor': + applyTextStyle(svg, item, name, value); + break; + default: + // Not supported yet. + break; + } } } diff --git a/src/svg/SvgStyles.js b/src/svg/SvgStyles.js index 47acf474..76300a8c 100644 --- a/src/svg/SvgStyles.js +++ b/src/svg/SvgStyles.js @@ -15,24 +15,20 @@ */ var SvgStyles = Base.each({ - fillColor: 'fill', - strokeColor: 'stroke', - strokeWidth: 'stroke-width', - strokeCap: 'stroke-linecap', - strokeJoin: 'stroke-linejoin', - miterLimit: 'stroke-miterlimit', - dashArray: 'stroke-dasharray', - dashOffset: 'stroke-dashoffset' -}, function(attr, prop) { - var part = Base.capitalize(prop); - this.attributes[attr] = this.properties[prop] = { - type: /Color$/.test(prop) - ? 'color' - : prop == 'dashArray' - ? 'array' - : 'value', - property: prop, - attribute: attr, + fillColor: ['fill', 'color'], + strokeColor: ['stroke', 'color'], + strokeWidth: ['stroke-width', 'number'], + strokeCap: ['stroke-linecap', 'string'], + strokeJoin: ['stroke-linejoin', 'string'], + miterLimit: ['stroke-miterlimit', 'number'], + dashArray: ['stroke-dasharray', 'array'], + dashOffset: ['stroke-dashoffset', 'number'] +}, function(entry, key) { + var part = Base.capitalize(key); + this.attributes[entry[0]] = this.properties[key] = { + type: entry[1], + property: key, + attribute: entry[0], get: 'get' + part, set: 'set' + part }; diff --git a/src/svg/constants.js b/src/svg/constants.js index 0c92bd47..48ba000e 100644 --- a/src/svg/constants.js +++ b/src/svg/constants.js @@ -17,7 +17,8 @@ /** * To shrink code, we automatically replace the long SVGPathSeg and SVGTransform * constants with their actual numeric values on preprocessing time, using - * *#=* statements. To do so, we need their values defined, which happens here. + * prepro statements. + * To do so, we need their values defined, which happens here. */ // http://dxr.mozilla.org/mozilla-central/dom/interfaces/svg/nsIDOMSVGPathSeg.idl.html