From 9e5b47c1ace0283f6f74395b639b5f90dd47bf06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 9 May 2013 02:31:10 -0700 Subject: [PATCH] Implement SVG transform attribute parser, to completely remove dependency on baseVal. --- src/svg/SVGImport.js | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/svg/SVGImport.js b/src/svg/SVGImport.js index fd55ce9f..07124ba2 100644 --- a/src/svg/SVGImport.js +++ b/src/svg/SVGImport.js @@ -273,12 +273,42 @@ new function() { function applyTransform(item, value, name, node) { // http://www.w3.org/TR/SVG/types.html#DataTypeTransformList - var transforms = node[name] && node[name].baseVal, + // Parse SVG transform string. First we split at /)\s*/, to separate + // commands + var transforms = (node.getAttribute(name) || '').split(/\)\s*/g), matrix = new Matrix(); - for (var i = 0, l = transforms && transforms.numberOfItems; i < l; i++) { - var mx = transforms.getItem(i).matrix; - matrix.concatenate( - new Matrix(mx.a, mx.b, mx.c, mx.d, mx.e, mx.f)); + for (var i = 0, l = transforms.length; i < l; i++) { + var transform = transforms[i]; + if (!transform) + break; + // Command come before the '(', values after + var parts = transform.split('('), + command = parts[0], + v = parts[1].split(/[\s,]+/g); + // Convert values to floats + for (var j = 0, m = v.length; j < m; j++) + v[j] = parseFloat(v[j]); + switch (command) { + case 'matrix': + matrix.concatenate( + new Matrix(v[0], v[2], v[1], v[3], v[4], v[5])); + break; + case 'rotate': + matrix.rotate(v[0], v[1], v[2]); + break; + case 'translate': + matrix.translate(v[0], v[1]); + break; + case 'scale': + matrix.scale(v); + break; + case 'skewX': + case 'skewY': + var value = Math.tan(v[0] * Math.PI / 180), + isX = command == 'skewX'; + matrix.shear(isX ? value : 0, isX ? 0 : value); + break; + } } item.transform(matrix); }