wout/raphael-svg-import — Issue #14: support transform attribute
Author: karak
Created: 2012-01-17 01:35:25+00:00
State: open

Hi, this is a great script.
I need some in order to parse transform attribute for my use, however.

Here's my patch.

====

@@ -5,8 +5,27 @@
* - Load order fix by Georgi Momchilov
* - Prototype dependency removed by Matt Cook
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
+*
+* Patch:
+* - parse transform attribute which start with "matrix"
+* - getBBox result is invalid?(undone)
*/
Raphael.fn.importSVG = function (rawSVG, set) {
+ function parseTransformAttribute(t) {
+ var m;
+ if (t !== undefined) {
+ if (t.substr(0, 6) != 'matrix') {
+ throw 'transform must start with "matrix"';
+ }
+ m = t.substring(7, t.length - 1).split(',').map(function (e) {
+ return parseFloat(e);
+ });
+ return (Raphael.matrix(m[0], m[1], m[2], m[3], m[4], m[5]));
+ } else {
+ return undefined;
+ }
+ };
+
try {
if (typeof rawSVG === 'undefined')
throw 'No data was provided.';
@@ -70,8 +89,18 @@
//-F break;
}

+ var transformMatrix = parseTransformAttribute(attr['transform']);
+
+ if (transformMatrix) {
+ delete attr['transform'];
+ }
+
shape.attr(attr);

+ if (transformMatrix) {
+ shape.transform(transformMatrix.toTransformString());
+ }
+
if (typeof set !== 'undefined')
set.push(shape);
};
@@ -98,4 +127,4 @@
poly.push('Z');

return this.path(poly);
-};
+};
\ No newline at end of file