// Vectorizer. // ----------- // A tiny library for making your live easier when dealing with SVG. // Licensed under the MPL v2 license. // Copyright © 2012 - 2014 client IO (http://client.io) (function(root,factory){if(typeof define==="function"&&define.amd){define([],factory)}else{root.Vectorizer=root.V=factory()}})(this,function(){var SVGsupported=!!(window.SVGAngle||document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure","1.1"));var ns={xmlns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink"};var SVGversion="1.1";var idCounter=0;function uniqueId(){var id=++idCounter+"";return"v-"+id}function createElement(el,attrs,children){if(!el)return undefined;if(typeof el==="object"){return new VElement(el)}attrs=attrs||{};if(el.toLowerCase()==="svg"){attrs.xmlns=ns.xmlns;attrs["xmlns:xlink"]=ns.xlink;attrs.version=SVGversion}else if(el[0]==="<"){var svg=''+el+"";var parser=new DOMParser;parser.async=false;var svgDoc=parser.parseFromString(svg,"text/xml").documentElement;if(svgDoc.childNodes.length>1){var ret=[];for(var i=0,len=svgDoc.childNodes.length;i-1){var combinedKey=name.split(":");el.setAttributeNS(ns[combinedKey[0]],combinedKey[1],value)}else if(name==="id"){el.id=value}else{el.setAttribute(name,value)}}function parseTransformString(transform){var translate,rotate,scale;if(transform){var translateMatch=transform.match(/translate\((.*)\)/);if(translateMatch){translate=translateMatch[1].split(",")}var rotateMatch=transform.match(/rotate\((.*)\)/);if(rotateMatch){rotate=rotateMatch[1].split(",")}var scaleMatch=transform.match(/scale\((.*)\)/);if(scaleMatch){scale=scaleMatch[1].split(",")}}var sx=scale&&scale[0]?parseFloat(scale[0]):1;return{translate:{tx:translate&&translate[0]?parseInt(translate[0],10):0,ty:translate&&translate[1]?parseInt(translate[1],10):0},rotate:{angle:rotate&&rotate[0]?parseInt(rotate[0],10):0,cx:rotate&&rotate[1]?parseInt(rotate[1],10):undefined,cy:rotate&&rotate[2]?parseInt(rotate[2],10):undefined},scale:{sx:sx,sy:scale&&scale[1]?parseFloat(scale[1]):sx}}}function deltaTransformPoint(matrix,point){var dx=point.x*matrix.a+point.y*matrix.c+0;var dy=point.x*matrix.b+point.y*matrix.d+0;return{x:dx,y:dy}}function decomposeMatrix(matrix){var px=deltaTransformPoint(matrix,{x:0,y:1});var py=deltaTransformPoint(matrix,{x:1,y:0});var skewX=180/Math.PI*Math.atan2(px.y,px.x)-90;var skewY=180/Math.PI*Math.atan2(py.y,py.x);return{translateX:matrix.e,translateY:matrix.f,scaleX:Math.sqrt(matrix.a*matrix.a+matrix.b*matrix.b),scaleY:Math.sqrt(matrix.c*matrix.c+matrix.d*matrix.d),skewX:skewX,skewY:skewY,rotation:skewX}}function VElement(el){this.node=el;if(!this.node.id){this.node.id=uniqueId()}}VElement.prototype={translate:function(tx,ty){ty=ty||0;var transformAttr=this.attr("transform")||"",transform=parseTransformString(transformAttr);if(typeof tx==="undefined"){return transform.translate}transformAttr=transformAttr.replace(/translate\([^\)]*\)/g,"").trim();var newTx=transform.translate.tx+tx,newTy=transform.translate.ty+ty;this.attr("transform","translate("+newTx+","+newTy+") "+transformAttr);return this},rotate:function(angle,cx,cy){var transformAttr=this.attr("transform")||"",transform=parseTransformString(transformAttr);if(typeof angle==="undefined"){return transform.rotate}transformAttr=transformAttr.replace(/rotate\([^\)]*\)/g,"").trim();var newAngle=transform.rotate.angle+angle%360,newOrigin=cx!==undefined&&cy!==undefined?","+cx+","+cy:"";this.attr("transform",transformAttr+" rotate("+newAngle+newOrigin+")");return this},scale:function(sx,sy){sy=typeof sy==="undefined"?sx:sy;var transformAttr=this.attr("transform")||"",transform=parseTransformString(transformAttr);if(typeof sx==="undefined"){return transform.scale}transformAttr=transformAttr.replace(/scale\([^\)]*\)/g,"").trim();this.attr("transform",transformAttr+" scale("+sx+","+sy+")");return this},bbox:function(withoutTransformations,target){if(!this.node.ownerSVGElement)return{x:0,y:0,width:0,height:0};var box;try{box=this.node.getBBox();box={x:box.x|0,y:box.y|0,width:box.width|0,height:box.height|0}}catch(e){box={x:this.node.clientLeft,y:this.node.clientTop,width:this.node.clientWidth,height:this.node.clientHeight}}if(withoutTransformations){return box}var matrix=this.node.getTransformToElement(target||this.node.ownerSVGElement);var corners=[];var point=this.node.ownerSVGElement.createSVGPoint();point.x=box.x;point.y=box.y;corners.push(point.matrixTransform(matrix));point.x=box.x+box.width;point.y=box.y;corners.push(point.matrixTransform(matrix));point.x=box.x+box.width;point.y=box.y+box.height;corners.push(point.matrixTransform(matrix));point.x=box.x;point.y=box.y+box.height;corners.push(point.matrixTransform(matrix));var minX=corners[0].x;var maxX=minX;var minY=corners[0].y;var maxY=minY;for(var i=1,len=corners.length;imaxX){maxX=x}if(ymaxY){maxY=y}}return{x:minX,y:minY,width:maxX-minX,height:maxY-minY}},text:function(content){var lines=content.split("\n"),i=0,tspan;this.attr("y","0.8em");this.attr("display",content?null:"none");if(lines.length===1){this.node.textContent=content;return this}this.node.textContent="";for(;i