/*! JointJS v0.9.7 - JavaScript diagramming library 2016-04-20 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ (function(root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['g'], function(g) { return factory(g); }); } else if (typeof exports === 'object') { // Node. Does not work with strict CommonJS, but // only CommonJS-like environments that support module.exports, // like Node. var g = require('./geometry'); module.exports = factory(g); } else { // Browser globals. var g = root.g; root.Vectorizer = root.V = factory(g); } }(this, function(g) { var V,Vectorizer;V=Vectorizer=function(){"use strict";var a="object"==typeof window&&!(!window.SVGAngle&&!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure","1.1"));if(!a)return function(){throw new Error("SVG is required to use Vectorizer.")};var b={xmlns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink"},c="1.1",d=function(a,c,e){if(!(this instanceof d))return d.apply(Object.create(d.prototype),arguments);if(a){if(d.isV(a)&&(a=a.node),c=c||{},d.isString(a))if("svg"===a.toLowerCase())a=d.createSvgDocument();else if("<"===a[0]){var f=d.createSvgDocument(a);if(f.childNodes.length>1){var g,h,i=[];for(g=0,h=f.childNodes.length;h>g;g++){var j=f.childNodes[g];i.push(new d(document.importNode(j,!0)))}return i}a=document.importNode(f.firstChild,!0)}else a=document.createElementNS(b.xmlns,a);return this.node=a,this.node.id||(this.node.id=d.uniqueId()),this.setAttributes(c),e&&this.append(e),this}};d.prototype.getTransformToElement=function(a){return a.getScreenCTM().inverse().multiply(this.node.getScreenCTM())},d.prototype.transform=function(a){if(d.isUndefined(a))return this.node.parentNode?this.getTransformToElement(this.node.parentNode):this.node.getScreenCTM();var b=d.createSVGTransform(a);return this.node.transform.baseVal.appendItem(b),this},d.prototype.translate=function(a,b,c){c=c||{},b=b||0;var e=this.attr("transform")||"",f=d.parseTransformString(e);if(d.isUndefined(a))return f.translate;e=e.replace(/translate\([^\)]*\)/g,"").trim();var g=c.absolute?a:f.translate.tx+a,h=c.absolute?b:f.translate.ty+b,i="translate("+g+","+h+")";return this.attr("transform",(i+" "+e).trim()),this},d.prototype.rotate=function(a,b,c,e){e=e||{};var f=this.attr("transform")||"",g=d.parseTransformString(f);if(d.isUndefined(a))return g.rotate;f=f.replace(/rotate\([^\)]*\)/g,"").trim(),a%=360;var h=e.absolute?a:g.rotate.angle+a,i=void 0!==b&&void 0!==c?","+b+","+c:"",j="rotate("+h+i+")";return this.attr("transform",(f+" "+j).trim()),this},d.prototype.scale=function(a,b){b=d.isUndefined(b)?a:b;var c=this.attr("transform")||"",e=d.parseTransformString(c);if(d.isUndefined(a))return e.scale;c=c.replace(/scale\([^\)]*\)/g,"").trim();var f="scale("+a+","+b+")";return this.attr("transform",(c+" "+f).trim()),this},d.prototype.bbox=function(a,b){if(!this.node.ownerSVGElement)return{x:0,y:0,width:0,height:0};var c;try{c=this.node.getBBox(),c={x:c.x,y:c.y,width:c.width,height:c.height}}catch(e){c={x:this.node.clientLeft,y:this.node.clientTop,width:this.node.clientWidth,height:this.node.clientHeight}}if(a)return c;var f=this.getTransformToElement(b||this.node.ownerSVGElement);return d.transformRect(c,f)},d.prototype.text=function(a,b){a=d.sanitizeText(a),b=b||{};var c,e=a.split("\n"),f=0,g=this.attr("y");g||this.attr("y","0.8em"),this.attr("display",a?null:"none"),this.node.setAttributeNS("http://www.w3.org/XML/1998/namespace","xml:space","preserve"),this.node.textContent="";var h=this.node;if(b.textPath){var i=this.find("defs");0===i.length&&(i=d("defs"),this.append(i));var j=Object(b.textPath)===b.textPath?b.textPath.d:b.textPath;if(j){var k=d("path",{d:j});i.append(k)}var l=d("textPath");!b.textPath["xlink:href"]&&k&&l.attr("xlink:href","#"+k.node.id),Object(b.textPath)===b.textPath&&l.attr(b.textPath),this.append(l),h=l.node}for(var m=0,f=0;fq&&(q=u),c=d("tspan",t.attrs),b.includeAnnotationIndices&&c.attr("annotations",t.annotations),t.attrs["class"]&&c.addClass(t.attrs["class"]),c.node.textContent=t.t}else c=document.createTextNode(t||" ");p.append(c)}"auto"===b.lineHeight&&q&&0!==f&&p.attr("dy",1.2*q+"px")}else p.node.textContent=n;else p.addClass("v-empty-line"),p.node.style.opacity=0,p.node.textContent="-";d(h).append(p),m+=n.length+1}return this},d.prototype.attr=function(a,b){if(d.isUndefined(a)){for(var c=this.node.attributes,e={},f=0;fb;b++)e=a[b],this.node.appendChild(d.isV(e)?e.node:e.nodeName&&e||e[0]);return this},d.prototype.prepend=function(a){return this.node.insertBefore(d.isV(a)?a.node:a,this.node.firstChild),this},d.prototype.svg=function(){return this.node instanceof window.SVGSVGElement?this:d(this.node.ownerSVGElement)},d.prototype.defs=function(){var a=this.svg().node.getElementsByTagName("defs");return a&&a.length?d(a[0]):void 0},d.prototype.clone=function(){var a=d(this.node.cloneNode(!0));return a.node.id=d.uniqueId(),a},d.prototype.findOne=function(a){var b=this.node.querySelector(a);return b?d(b):void 0},d.prototype.find=function(a){var b=[],c=this.node.querySelectorAll(a);if(c)for(var e=0;ej;j++){var l=i[j],m=new Animator(g,l,j);animators.push(m),g.animators[j]=m,m.register()}}}},d.prototype.hasClass=function(a){return new RegExp("(\\s|^)"+a+"(\\s|$)").test(this.node.getAttribute("class"))},d.prototype.addClass=function(a){if(!this.hasClass(a)){var b=this.node.getAttribute("class")||"";this.node.setAttribute("class",(b+" "+a).trim())}return this},d.prototype.removeClass=function(a){if(this.hasClass(a)){var b=this.node.getAttribute("class").replace(new RegExp("(\\s|^)"+a+"(\\s|$)","g"),"$2");this.node.setAttribute("class",b)}return this},d.prototype.toggleClass=function(a,b){var c=d.isUndefined(b)?this.hasClass(a):!b;return c?this.removeClass(a):this.addClass(a),this},d.prototype.sample=function(a){a=a||1;for(var b,c=this.node,d=c.getTotalLength(),e=[],f=0;d>f;)b=c.getPointAtLength(f),e.push({x:b.x,y:b.y,distance:f}),f+=a;return e},d.prototype.convertToPath=function(){var a=d("path");a.attr(this.attr());var b=this.convertToPathData();return b&&a.attr("d",b),a},d.prototype.convertToPathData=function(){var a=this.node.tagName.toUpperCase();switch(a){case"PATH":return this.attr("d");case"LINE":return d.convertLineToPathData(this.node);case"POLYGON":return d.convertPolygonToPathData(this.node);case"POLYLINE":return d.convertPolylineToPathData(this.node);case"ELLIPSE":return d.convertEllipseToPathData(this.node);case"CIRCLE":return d.convertCircleToPathData(this.node);case"RECT":return d.convertRectToPathData(this.node)}throw new Error(a+" cannot be converted to PATH.")},d.prototype.findIntersection=function(a,b){var c=this.svg().node;b=b||c;var e=g.rect(this.bbox(!1,b)),f=e.center();if(e.intersectionWithLineFromCenterToPoint(a)){var h,i=this.node.localName.toUpperCase();if("RECT"===i){var j=g.rect(parseFloat(this.attr("x")||0),parseFloat(this.attr("y")||0),parseFloat(this.attr("width")),parseFloat(this.attr("height"))),k=this.getTransformToElement(b),l=d.decomposeMatrix(k),m=c.createSVGTransform();m.setRotate(-l.rotation,f.x,f.y);var n=d.transformRect(j,m.matrix.multiply(k));h=g.rect(n).intersectionWithLineFromCenterToPoint(a,l.rotation)}else if("PATH"===i||"POLYGON"===i||"POLYLINE"===i||"CIRCLE"===i||"ELLIPSE"===i){var o,p,q,r,s,t,u="PATH"===i?this:this.convertToPath(),v=u.sample(),w=1/0,x=[];for(o=0;ot?(w=t,x=[{sample:p,refDistance:s}]):w+1>t&&x.push({sample:p,refDistance:s});x.sort(function(a,b){return a.refDistance-b.refDistance}),x[0]&&(h=x[0].sample)}return h}},d.createSvgDocument=function(a){var e=''+(a||"")+"",f=d.parseXML(e,{async:!1});return f.documentElement},d.idCounter=0,d.uniqueId=function(){var a=++d.idCounter+"";return"v-"+a},d.sanitizeText=function(a){return(a||"").replace(/ /g," ")},d.isUndefined=function(a){return"undefined"==typeof a},d.isString=function(a){return"string"==typeof a},d.isObject=function(a){return a&&"object"==typeof a},d.isArray=Array.isArray,d.parseXML=function(a,b){b=b||{};var c;try{var e=new DOMParser;d.isUndefined(b.async)||(e.async=b.async),c=e.parseFromString(a,"text/xml")}catch(f){c=void 0}if(!c||c.getElementsByTagName("parsererror").length)throw new Error("Invalid XML: "+a);return c},d.setAttribute=function(a,c,d){if(c.indexOf(":")>-1){var e=c.split(":");a.setAttributeNS(b[e[0]],e[1],d)}else"id"===c?a.id=d:a.setAttribute(c,d)},d.parseTransformString=function(a){var b,c,d;if(a){var e=/[ ,]+/,f=a.match(/translate\((.*)\)/);f&&(b=f[1].split(e));var g=a.match(/rotate\((.*)\)/);g&&(c=g[1].split(e));var h=a.match(/scale\((.*)\)/);h&&(d=h[1].split(e))}var i=d&&d[0]?parseFloat(d[0]):1;return{translate:{tx:b&&b[0]?parseInt(b[0],10):0,ty:b&&b[1]?parseInt(b[1],10):0},rotate:{angle:c&&c[0]?parseInt(c[0],10):0,cx:c&&c[1]?parseInt(c[1],10):void 0,cy:c&&c[2]?parseInt(c[2],10):void 0},scale:{sx:i,sy:d&&d[1]?parseFloat(d[1]):i}}},d.deltaTransformPoint=function(a,b){var c=b.x*a.a+b.y*a.c+0,d=b.x*a.b+b.y*a.d+0;return{x:c,y:d}},d.decomposeMatrix=function(a){var b=d.deltaTransformPoint(a,{x:0,y:1}),c=d.deltaTransformPoint(a,{x:1,y:0}),e=180/Math.PI*Math.atan2(b.y,b.x)-90,f=180/Math.PI*Math.atan2(c.y,c.x);return{translateX:a.e,translateY:a.f,scaleX:Math.sqrt(a.a*a.a+a.b*a.b),scaleY:Math.sqrt(a.c*a.c+a.d*a.d),skewX:e,skewY:f,rotation:e}},d.isV=function(a){return a instanceof d},d.isVElement=d.isV;var e=d("svg").node;return d.createSVGMatrix=function(a){var b=e.createSVGMatrix();for(var c in a)b[c]=a[c];return b},d.createSVGTransform=function(a){return d.isUndefined(a)?e.createSVGTransform():(a instanceof SVGMatrix||(a=d.createSVGMatrix(a)),e.createSVGTransformFromMatrix(a))},d.createSVGPoint=function(a,b){var c=e.createSVGPoint();return c.x=a,c.y=b,c},d.transformRect=function(a,b){var c=e.createSVGPoint();c.x=a.x,c.y=a.y;var d=c.matrixTransform(b);c.x=a.x+a.width,c.y=a.y;var f=c.matrixTransform(b);c.x=a.x+a.width,c.y=a.y+a.height;var g=c.matrixTransform(b);c.x=a.x,c.y=a.y+a.height;var h=c.matrixTransform(b),i=Math.min(d.x,f.x,g.x,h.x),j=Math.max(d.x,f.x,g.x,h.x),k=Math.min(d.y,f.y,g.y,h.y),l=Math.max(d.y,f.y,g.y,h.y);return{x:i,y:k,width:j-i,height:l-k}},d.transformPoint=function(a,b){return d.createSVGPoint(a.x,a.y).matrixTransform(b)},d.styleToObject=function(a){for(var b={},c=a.split(";"),d=0;di&&(j=h,h=i,i=j),i-h),k=j=e?f?"M0,"+g+"A"+g+","+g+" 0 1,1 0,"+-g+"A"+g+","+g+" 0 1,1 0,"+g+"M0,"+f+"A"+f+","+f+" 0 1,0 0,"+-f+"A"+f+","+f+" 0 1,0 0,"+f+"Z":"M0,"+g+"A"+g+","+g+" 0 1,1 0,"+-g+"A"+g+","+g+" 0 1,1 0,"+g+"Z":f?"M"+g*l+","+g*m+"A"+g+","+g+" 0 "+k+",1 "+g*n+","+g*o+"L"+f*n+","+f*o+"A"+f+","+f+" 0 "+k+",0 "+f*l+","+f*m+"Z":"M"+g*l+","+g*m+"A"+g+","+g+" 0 "+k+",1 "+g*n+","+g*o+"L0,0Z"},d.mergeAttrs=function(a,b){for(var c in b)"class"===c?a[c]=a[c]?a[c]+" "+b[c]:b[c]:"style"===c?d.isObject(a[c])&&d.isObject(b[c])?a[c]=d.mergeAttrs(a[c],b[c]):d.isObject(a[c])?a[c]=d.mergeAttrs(a[c],d.styleToObject(b[c])):d.isObject(b[c])?a[c]=d.mergeAttrs(d.styleToObject(a[c]),b[c]):a[c]=d.mergeAttrs(d.styleToObject(a[c]),d.styleToObject(b[c])):a[c]=b[c];return a},d.annotateString=function(a,b,c){b=b||[],c=c||{};for(var e,f,g,h=c.offset||0,i=[],j=[],k=0;k=n&&o>k&&(d.isObject(f)?f.attrs=d.mergeAttrs(d.mergeAttrs({},f.attrs),m.attrs):f=j[k]={t:a[k],attrs:m.attrs},c.includeAnnotationIndices&&(f.annotations||(f.annotations=[])).push(l))}g=j[k-1],g?d.isObject(f)&&d.isObject(g)?JSON.stringify(f.attrs)===JSON.stringify(g.attrs)?e.t+=f.t:(i.push(e),e=f):d.isObject(f)?(i.push(e),e=f):d.isObject(g)?(i.push(e),e=f):e=(e||"")+f:e=f}return e&&i.push(e),i},d.findAnnotationsAtIndex=function(a,b){var c=[];return a&&a.forEach(function(a){a.start=a.start&&ba.start&&c<=a.end||a.start>=b&&a.end=b?a.end+=c:a.start>=b&&(a.start+=c,a.end+=c)}),a},d.convertLineToPathData=function(a){a=d(a);var b=["M",a.attr("x1"),a.attr("y1"),"L",a.attr("x2"),a.attr("y2")].join(" ");return b},d.convertPolygonToPathData=function(a){a=d(a);var b=d.getPointsFromSvgNode(a.node);return b.length>0?d.svgPointsToPath(b):null},d.convertPolylineToPathData=function(a){var b=d.getPointsFromSvgNode(a.node);return b.length>0?d.svgPointsToPath(b):null},d.svgPointsToPath=function(a){var b;for(b=0;b