UNPKG

cadviewer-angular

Version:

CADViewer - JavaScript CAD Viewer Library

11 lines (10 loc) 4.76 kB
(function(r,k){"object"===typeof exports&&"undefined"!==typeof module?module.exports=k():"function"===typeof define&&define.amd?define(k):r.PathConverter=k()})(this,function(){var r=function(a,b){if(isNaN(a)||isNaN(b))throw Error("NaN encountered: "+a+"/"+b);this.x=a;this.y=b},k=function(a,b){this.main=new r(a,b);this.right=this.left=null;this.isplain=!0};k.prototype.setLeft=function(a,b){this.isplain=!1;this.left=new r(a,b)};k.prototype.setRight=function(a,b){this.isplain=!1;this.right=new r(a,b)}; k.prototype.isPlain=function(){return this.isplain};k.prototype.getPoint=function(){return this.main};k.prototype.getLeft=function(){return this.isplain?this.main:null==this.left?this.main:this.left};k.prototype.getRight=function(){return this.isplain?this.main:null==this.right?this.main:this.right};var q=function(){this.points=[]};q.prototype.current=function(){return this.points.slice(-1)[0]};q.prototype.addPoint=function(a,b){this.points.push(new k(a,b))};q.prototype.setLeft=function(a,b){this.current().setLeft(a, b)};q.prototype.setRight=function(a,b){this.current().setRight(a,b)};q.prototype.toSVG=function(){var a=this.points,b=a[0].getPoint(),g=b.y;b="M"+b.x+(0>g?g:" "+g);for(g=1;g<a.length;g++){var f=a[g-1],d=a[g];if(d.isPlain()&&f.isPlain()){var n=d.getPoint().x;d=d.getPoint().y;b+="L"+n+(0>d?d:" "+d)}else{n=f.getRight().x;f=f.getRight().y;var e=d.getLeft().x,k=d.getLeft().y,c=d.getPoint().x;d=d.getPoint().y;b+="C"+n+(0>f?f:" "+f)+(0>e?e:" "+e)+(0>k?k:" "+k)+(0>c?c:" "+c)+(0>d?d:" "+d)}}return b+"Z"}; var p=function(){this.curveshapes=[];this.current=new q;this.bounds=!1};p.prototype.getShapes=function(){return this.curveshapes};p.prototype.getShape=function(a){return this.curveshapes[a]};p.prototype.toSVG=function(){var a="";this.curveshapes.forEach(function(b){return a+=b.toSVG()});return a};p.prototype.startGroup=function(){this.curveshapes=[]};p.prototype.startShape=function(){this.current=new q};p.prototype.addPoint=function(a,b){this.current.addPoint(a,b);var g=this.bounds;g||(g=this.bounds= [a,b,a,b]);a<g[0]&&(g[0]=a);a>g[2]&&(g[2]=a);b<g[1]&&(g[1]=b);b>g[3]&&(g[3]=b)};p.prototype.setLeftControl=function(a,b){this.current.setLeft(a,b)};p.prototype.setRightControl=function(a,b){this.current.setRight(a,b)};p.prototype.closeShape=function(){this.curveshapes.push(this.current);this.current=null};p.prototype.closeGroup=function(){this.curveshapes.push(this.current);this.current=null};var t=function(a){this.receiver=a};t.prototype.getReceiver=function(){return receiver};t.prototype.parse= function(a,b,g){b=b||0;g=g||0;a=a.replace(/\s*([mlvhqczMLVHQCZ])\s*/g,"\n$1 ").replace(/,/g," ").replace(/-/g," -").replace(/ +/g," ");a=a.split("\n");var f=b,d=g,n=this.receiver;n.startGroup();for(var e={},k=1;k<a.length;e={op:e.op},k++){var c=a[k].trim();e.op=c.substring(0,1);c=1<c.length?c.substring(2).trim().split(" "):[];if("m"===e.op||"M"===e.op)"m"===e.op?(f+=parseFloat(c[0]),d+=parseFloat(c[1])):"M"===e.op&&(f=parseFloat(c[0])+b,d=parseFloat(c[1])+g),k<a.length-1&&(c=a[k+1].trim().substring(0, 1),"m"!==c&&"M"!==c&&"z"!==c&&"Z"!==c&&(1<k&&n.closeShape(),n.startShape(),n.addPoint(f,d)));else if("l"===e.op||"L"===e.op)for(var h=0;h<c.length;h+=2)"l"===e.op?(f+=parseFloat(c[h+0]),d+=parseFloat(c[h+1])):"L"===e.op&&(f=parseFloat(c[h+0])+b,d=parseFloat(c[h+1])+g),n.addPoint(f,d);else if("v"===e.op||"V"===e.op)c.forEach(function(a){return function(b){"v"===a.op?d+=parseFloat(b):"V"===a.op&&(d=parseFloat(b)+g);n.addPoint(f,d)}}(e));else if("h"===e.op||"H"===e.op)c.forEach(function(a){return function(b){"h"=== a.op?f+=parseFloat(b):"H"===a.op&&(f=parseFloat(b)+g);n.addPoint(f,d)}}(e));else if("q"===e.op||"Q"===e.op)for(h=0;h<c.length;h+=4){var l=0,m=0;"q"===e.op?(l=f+parseFloat(c[h]),m=d+parseFloat(c[h+1])):"Q"===e.op&&(l=parseFloat(c[h])+b,m=parseFloat(c[h+1])+g);var p=(f+l+l)/3,q=(d+m+m)/3;f==p&&d==q||n.setRightControl(p,q);"q"===e.op?(f+=parseFloat(c[h+2]),d+=parseFloat(c[h+3])):"Q"===e.op&&(f=parseFloat(c[h+2])+b,d=parseFloat(c[h+3])+g);l=(f+l+l)/3;m=(d+m+m)/3;n.addPoint(f,d);f==l&&d==m||n.setLeftControl(l, m)}else if("c"===e.op||"C"===e.op)for(h=0;h<c.length;h+=6)l=m=0,"c"===e.op?(m=f+parseFloat(c[h]),l=d+parseFloat(c[h+1])):"C"===e.op&&(m=parseFloat(c[h])+b,l=parseFloat(c[h+1])+g),f==m&&d==l||n.setRightControl(m,l),l=m=0,"c"===e.op?(m=f+parseFloat(c[h+2]),l=d+parseFloat(c[h+3])):"C"===e.op&&(m=parseFloat(c[h+2])+b,l=parseFloat(c[h+3])+g),"c"===e.op?(f+=parseFloat(c[h+4]),d+=parseFloat(c[h+5])):"C"===e.op&&(f=parseFloat(c[h+4])+b,d=parseFloat(c[h+5])+g),n.addPoint(f,d),f==m&&d==l||n.setLeftControl(m, l);else"z"!==e.op&&"Z"!==e.op||n.closeGroup()}};var u={SVGParser:t,Outline:p,PointCurveShape:q,parse:function(a){var b=new u.Outline;(new u.SVGParser(b)).parse(a);return b}};return u});