Clean up applyAttributeOrStyle().

Do not use style properties directly, but setters instead.
This commit is contained in:
Jürg Lehni 2012-11-02 19:26:15 -07:00
parent 118a02bf05
commit 99a39bebc3

View file

@ -256,86 +256,69 @@ var SvgImporter = this.SvgImporter = new function() {
* @param value the value of the SVG style * @param value the value of the SVG style
*/ */
function applyAttributeOrStyle(svg, item, name, value) { function applyAttributeOrStyle(svg, item, name, value) {
if (!value) { if (value == null)
return; return;
}
switch (name) { switch (name) {
case 'id': case 'id':
item.name = value; item.setName(value);
break; break;
case 'fill': case 'fill':
if (value != 'none') { if (value !== 'none')
item.fillColor = value; item.setFillColor(value);
}
break; break;
case 'stroke': case 'stroke':
if (value != 'none') { if (value !== 'none')
item.strokeColor = value; item.setStrokeColor(value);
}
break; break;
case 'stroke-width': case 'stroke-width':
item.strokeWidth = parseFloat(value, 10); item.setStrokeWidth(parseFloat(value, 10));
break; break;
case 'stroke-linecap': case 'stroke-linecap':
item.strokeCap = value; item.setStrokeCap(value);
break; break;
case 'stroke-linejoin': case 'stroke-linejoin':
item.strokeJoin = value; item.setStrokeJoin(value);
break; break;
case 'stroke-dasharray': case 'stroke-dasharray':
value = value.replace(/px/g, ''); value = value.replace(/px/g, '').replace(/, /g, ',')
value = value.replace(/, /g, ','); .replace(/ /g, ',').split(',');
value = value.replace(/ /g, ','); for (var i = 0, l = value.length; i < l; i++)
value = value.split(',');
for (var i in value) {
value[i] = parseFloat(value[i], 10); value[i] = parseFloat(value[i], 10);
} item.setDashArray(value);
item.dashArray = value;
break; break;
case 'stroke-dashoffset': case 'stroke-dashoffset':
item.dashOffset = parseFloat(value, 10); item.setDashOffset(parseFloat(value, 10));
break; break;
case 'stroke-miterlimit': case 'stroke-miterlimit':
item.miterLimit = parseFloat(value, 10); item.setMiterLimit(parseFloat(value, 10));
break; break;
case 'transform': case 'transform':
applyTransform(svg, item); applyTransform(svg, item);
break; break;
case 'opacity': case 'opacity':
item.opacity = parseFloat(value, 10); item.setOpacity(parseFloat(value, 10));
break; break;
case 'visibility': case 'visibility':
item.visibility = (value == 'visible') ? true : false; item.setVisibility(value === 'visible');
break; break;
case 'font': case 'font':
var text = document.createElement('span');
text.style.font = value;
for (var i = 0; i < text.style.length; i++) {
var n = text.style[i];
applyAttributeOrStyle(svg, item, n, text.style[n]);
}
break;
case 'font-family': case 'font-family':
item.setFont(value.split(',')[0].replace(/^\s+|\s+$/g, ""));
break;
case 'font-size': case 'font-size':
//Implemented in characterStyle below. item.setFontSize(parseFloat(value, 10));
break; break;
default: default:
// Not supported yet. // Not supported yet.
break; break;
} }
if (item.characterStyle) {
switch (name) {
case 'font':
var text = document.createElement('span');
text.style.font = value;
for (var i = 0; i < text.style.length; i++) {
var n = text.style[i];
applyAttributeOrStyle(svg, item, n, text.style[n]);
}
break;
case 'font-family':
var fonts = value.split(',');
fonts[0] = fonts[0].replace(/^\s+|\s+$/g, "");
item.characterStyle.font = fonts[0];
break;
case 'font-size':
item.characterStyle.fontSize = parseFloat(value, 10);
break;
}
}
} }
/** /**