@tracespace/plotter
Version:
Plot @tracespace/parser ASTs into image trees.
3 lines (2 loc) • 12.6 kB
JavaScript
(function(O,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@tracespace/parser")):typeof define=="function"&&define.amd?define(["exports","@tracespace/parser"],r):(O=typeof globalThis<"u"?globalThis:O||self,r(O.TracespacePlotter={},O.TracespaceParser))})(this,function(O,r){"use strict";const X="image",N="imageShape",z="imagePath",Q="imageRegion",x="line",C="arc",I="circle",G="rectangle",P="polygon",v="outline",H="layeredShape",{PI:S}=Math,R=S/2,J=3*R,g=2*S;function B(t){return t>=0&&t<=g?t:t<0?t+g:t>g?t-g:B(t)}function K(t){return t>=R?t-R:t+J}function Z(t){return t*Math.PI/180}function T(t,e,n=0){const s=Z(n),[i,c]=[Math.sin(s),Math.cos(s)],[o,u]=t,a=o*c-u*i+e[0],y=o*i+u*c+e[1];return[a,y]}function W(t,e){return t[0]===e[0]&&t[1]===e[1]}function V(t){return t.length===0}function tt(){return[]}function et(t,e){return V(t)?e:V(e)?t:[Math.min(t[0],e[0]),Math.min(t[1],e[1]),Math.max(t[2],e[2]),Math.max(t[3],e[3])]}function D(t){return t.reduce(et,tt())}function nt(t){return D(t.map(st))}function st(t){return t.type===N?q(t.shape):U(t.segments,t.type===z?t.width:void 0)}function q(t){switch(t.type){case I:{const{cx:e,cy:n,r:s}=t;return Y([e,n],s)}case G:{const{x:e,y:n,xSize:s,ySize:i}=t;return[e,n,e+s,n+i]}case P:return D(t.points.map(e=>Y(e)));case v:return U(t.segments);case H:return D(t.shapes.filter(({erase:e})=>!e).map(q))}}function U(t,e=0){const n=e/2,s=[];for(const i of t)if(s.push(i.start,i.end),i.type===C){const{start:c,end:o,center:u,radius:a}=i,y=Math.abs(o[2]-c[2]);let[h,f]=o[2]>c[2]?[c[2],o[2]]:[o[2],c[2]];h=B(h),f=B(f);const d=[[u[0]+a,u[1]],[u[0],u[1]+a],[u[0]-a,u[1]],[u[0],u[1]-a]];for(const p of d)(h>f||y===g)&&s.push(p),h=K(h),f=K(f)}return D(s.map(i=>Y(i,n)))}function Y(t,e=0){return[t[0]-e,t[1]-e,t[0]+e,t[1]+e]}const yt=Object.freeze(Object.defineProperty({__proto__:null,add:et,empty:tt,fromGraphic:st,fromGraphics:nt,fromPath:U,fromShape:q,isEmpty:V,sum:D},Symbol.toStringTag,{value:"Module"})),ht=/FORMAT={?(\d):(\d)/;function pt(t){const{children:e}=t;let n=null,s=null,i=null,c=0;for(;c<e.length&&(n===null||s===null||i===null);){const o=e[c];switch(o.type){case r.UNITS:{n=o.units;break}case r.COORDINATE_FORMAT:{s=o.format,i=o.zeroSuppression;break}case r.GRAPHIC:{const{coordinates:u}=o;for(const a of Object.values(u)){if(i!==null)break;a.endsWith("0")||a.includes(".")?i=r.LEADING:a.startsWith("0")&&(i=r.TRAILING)}break}case r.COMMENT:{const{comment:u}=o,a=ht.exec(u);/suppress trailing/i.test(u)?i=r.TRAILING:/(suppress leading|keep zeros)/i.test(u)&&(i=r.LEADING),a&&(s=[Number(a[1]),Number(a[2])]);break}}c+=1}return{units:n??r.IN,coordinateFormat:s??[2,4],zeroSuppression:i??r.LEADING}}const w="simpleTool",ct="macroTool";function lt(){return Object.create(dt)}const dt={_currentToolCode:void 0,_toolsByCode:{},_macrosByName:{},use(t){if(t.type===r.TOOL_MACRO&&(this._macrosByName[t.name]=t.children),t.type===r.TOOL_DEFINITION){const{shape:e,hole:n}=t,s=e.type===r.MACRO_SHAPE?{type:ct,macro:this._macrosByName[e.name]??[],variableValues:e.variableValues}:{type:w,shape:e,...n&&{hole:n}};this._toolsByCode[t.code]=s}return(t.type===r.TOOL_DEFINITION||t.type===r.TOOL_CHANGE)&&(this._currentToolCode=t.code),typeof this._currentToolCode=="string"?this._toolsByCode[this._currentToolCode]:void 0}};function _t(){return Object.create(Et)}const Et={_DEFAULT_ARC_OFFSETS:{i:0,j:0,a:0},_previousPoint:{x:0,y:0},use(t,e){let n=this._DEFAULT_ARC_OFFSETS,s=this._previousPoint,i=s;if(t.type===r.GRAPHIC){const{coordinates:c}=t,o=L(c.x0,s.x,e),u=L(c.y0,s.y,e),a=L(c.x,o,e),y=L(c.y,u,e),h=L(c.i,0,e),f=L(c.j,0,e),d=L(c.a,0,e);(s.x!==o||s.y!==u)&&(s={x:o,y:u}),(i.x!==a||i.y!==y)&&(i={x:a,y}),(h!==0||f!==0||d!==0)&&(n={i:h,j:f,a:d})}return this._previousPoint=i,{startPoint:s,endPoint:i,arcOffsets:n}}};function L(t,e,n){if(typeof t!="string")return e;if(t.includes(".")||t==="0")return Number(t);const{coordinateFormat:s,zeroSuppression:i}=n,[c,o]=s,[u,a]=t.startsWith("+")||t.startsWith("-")?[t[0],t.slice(1)]:["+",t],y=c+o,h=i===r.TRAILING?a.padEnd(y,"0"):a.padStart(y,"0"),f=h.slice(0,c),d=h.slice(c);return Number(`${u}${f}.${d}`)}function it(t,e){const{x:n,y:s}=e;switch(t.type){case r.CIRCLE:{const{diameter:i}=t;return{type:I,cx:n,cy:s,r:i/2}}case r.RECTANGLE:case r.OBROUND:{const{xSize:i,ySize:c}=t,o=i/2,u=c/2,a={type:G,x:n-o,y:s-u,xSize:i,ySize:c};return t.type===r.OBROUND&&(a.r=Math.min(o,u)),a}case r.POLYGON:{const{diameter:i,rotation:c,vertices:o}=t,u=i/2,a=Z(c??0),y=g/o,h=Array.from({length:o}).map((f,d)=>{const p=y*d+a,l=n+u*Math.cos(p),E=s+u*Math.sin(p);return[l,E]});return{type:P,points:h}}}}function k(t){if(t.type===I){const{cx:e,cy:n,r:s}=t;return[{type:C,start:[e+s,n,0],end:[e+s,n,g],center:[e,n],radius:s}]}if(t.type===G){const{x:e,y:n,xSize:s,ySize:i,r:c}=t;return c===s/2?[{type:x,start:[e+s,n+c],end:[e+s,n+i-c]},{type:C,start:[e+s,n+i-c,0],end:[e,n+i-c,S],center:[e+c,n+i-c],radius:c},{type:x,start:[e,n+i-c],end:[e,n+c]},{type:C,start:[e,n+c,S],end:[e+s,n+c,g],center:[e+c,n+c],radius:c}]:c===i/2?[{type:x,start:[e+c,n],end:[e+s-c,n]},{type:C,start:[e+s-c,n,-R],end:[e+s-c,n+i,R],center:[e+s-c,n+c],radius:c},{type:x,start:[e+s-c,n+i],end:[e+c,n+i]},{type:C,start:[e+c,n+i,R],end:[e+c,n,J],center:[e+c,n+c],radius:c}]:[{type:x,start:[e,n],end:[e+s,n]},{type:x,start:[e+s,n],end:[e+s,n+i]},{type:x,start:[e+s,n+i],end:[e,n+i]},{type:x,start:[e,n+i],end:[e,n]}]}return t.type===P?t.points.map((e,n)=>{const s=n<t.points.length-1?n+1:0;return{type:x,start:e,end:t.points[s]}}):t.segments}function Ot(t,e){const{shape:n,hole:s}=t,i=it(n,e.endPoint),c=s?it(s,e.endPoint):void 0;return c===void 0?i:{type:v,segments:[...k(i),...k(c)]}}function At(t,e){const n=t.filter(s=>s.type===x).map(s=>Mt(s,e));return{type:N,shape:{type:H,shapes:n}}}function Mt(t,e){const{start:n,end:s}=t,[i,c]=n,[o,u]=s,[a,y]=[e.xSize/2,e.ySize/2],h=Math.atan2(u-c,o-u),[f,d]=[i-a,i+a],[p,l]=[c-y,c+y],[E,m]=[o-a,o+a],[M,A]=[u-y,u+y];let _=[];return W(n,s)?_=[[f,p],[d,p],[m,M],[m,A],[E,A],[f,l]]:h>=0&&h<R?_=[[f,p],[d,p],[m,M],[m,A],[E,A],[f,l]]:h>=R&&h<=S?_=[[d,p],[d,l],[m,A],[E,A],[E,M],[f,p]]:h>=-S&&h<-R?_=[[d,l],[f,l],[E,A],[E,M],[m,M],[d,p]]:_=[[f,l],[f,p],[E,M],[m,M],[m,A],[d,l]],{type:P,points:_}}const ot="cw",$="ccw";function rt(t,e,n){return e===void 0?at(t):mt(t,e,n)}function ut(t,e,n=!1){if(t.length>0){if(n)return{type:Q,segments:t};if((e==null?void 0:e.type)===w&&e.shape.type===I)return{type:z,width:e.shape.diameter,segments:t};if((e==null?void 0:e.type)===w&&e.shape.type===G)return At(t,e.shape)}}function at(t){return{type:x,start:[t.startPoint.x,t.startPoint.y],end:[t.endPoint.x,t.endPoint.y]}}function mt(t,e,n=!1){const{startPoint:s,endPoint:i,arcOffsets:c}=t,o=c.a>0?c.a:(c.i**2+c.j**2)**.5;if(n||c.a>0){if(s.x===i.x&&s.y===i.y)return at(t);const[f,d,p]=xt(t,o).map(l=>F(s,i,l,e)).sort(([l,E],[m,M])=>{const A=Math.abs(E[2]-l[2]),_=Math.abs(M[2]-m[2]);return A-_})[0];return{type:C,start:f,end:d,center:p,radius:o}}const u={x:s.x+c.i,y:s.y+c.j},[a,y,h]=F(s,i,u,e);return{type:C,start:a,end:y,center:h,radius:o}}function F(t,e,n,s){let i=Math.atan2(t.y-n.y,t.x-n.x),c=Math.atan2(e.y-n.y,e.x-n.x);return s===$?c=c>i?c:c+g:i=i>c?i:i+g,[[t.x,t.y,i],[e.x,e.y,c],[n.x,n.y]]}function xt(t,e){const{x:n,y:s}=t.startPoint,{x:i,y:c}=t.endPoint,[o,u]=[i-n,c-s],[a,y]=[i+n,c+s],h=Math.sqrt(o**2+u**2);if(e<=h/2)return[{x:n+o/2,y:s+u/2}];const f=Math.sqrt(4*e**2/h**2-1),[d,p]=[a/2,y/2],[l,E]=[u*f/2,o*f/2];return[{x:d+l,y:p-E},{x:d-l,y:p+E}]}function Pt(t,e){const n=[],s=Object.fromEntries(t.variableValues.map((i,c)=>[`$${c+1}`,i]));for(const i of t.macro)if(i.type===r.MACRO_VARIABLE&&(s[i.name]=j(i.value,s)),i.type===r.MACRO_PRIMITIVE){const c=[e.endPoint.x,e.endPoint.y],o=i.parameters.map(u=>j(u,s));n.push(...Ct(i.code,c,o))}return{type:H,shapes:n}}function j(t,e){if(typeof t=="number")return t;if(typeof t=="string")return e[t];const n=j(t.left,e),s=j(t.right,e);switch(t.operator){case"+":return n+s;case"-":return n-s;case"x":return n*s;case"/":return n/s}}function Ct(t,e,n){switch(t){case r.MACRO_CIRCLE:return[gt(e,n)];case r.MACRO_VECTOR_LINE:case r.MACRO_VECTOR_LINE_DEPRECATED:return[Tt(e,n)];case r.MACRO_CENTER_LINE:return[Rt(e,n)];case r.MACRO_LOWER_LEFT_LINE_DEPRECATED:return[It(e,n)];case r.MACRO_OUTLINE:return[St(e,n)];case r.MACRO_POLYGON:return[Lt(e,n)];case r.MACRO_MOIRE_DEPRECATED:return Nt(e,n);case r.MACRO_THERMAL:return[Gt(e,n)]}return[]}function gt(t,e){const[n,s,i,c,o]=e,u=s/2,[a,y]=T([i,c],t,o);return{type:I,erase:n===0,cx:a,cy:y,r:u}}function Tt(t,e){const[n,s,i,c,o,u,a]=e,[y,h]=[u-c,o-i],f=s/2,d=Math.sqrt(y**2+h**2),[p,l]=[f*h/d,f*y/d];return{type:P,erase:n===0,points:[[i+p,c-l],[o+p,u-l],[o-p,u+l],[i-p,c+l]].map(E=>T(E,t,a))}}function Rt(t,e){const[n,s,i,c,o,u]=e,[a,y]=[s/2,i/2];return{type:P,erase:n===0,points:[[c-a,o-y],[c+a,o-y],[c+a,o+y],[c-a,o+y]].map(h=>T(h,t,u))}}function It(t,e){const[n,s,i,c,o,u]=e;return{type:P,erase:n===0,points:[[c,o],[c+s,o],[c+s,o+i],[c,o+i]].map(a=>T(a,t,u))}}function St(t,e){const[n,,...s]=e.slice(0,-1),i=e[e.length-1];return{type:P,erase:n===0,points:s.flatMap((c,o)=>o%2===1?[[s[o-1],c]]:[]).map(c=>T(c,t,i))}}function Lt(t,e){const[n,s,i,c,o,u]=e,a=o/2,y=2*S/s,h=[];let f;for(f=0;f<s;f++){const d=y*f,p=i+a*Math.cos(d),l=c+a*Math.sin(d);h.push(T([p,l],t,u))}return{type:P,erase:n===0,points:h}}function Nt(t,e){const n=A=>T(A,t,e[8]),[s,i,c,o,u,a,y,h]=e,[f,d]=n([s,i]),p=y/2,l=h/2,E=[];let m=0,M=c;for(;M>=0&&m<a;){const A=M/2,_=A-o;E.push(A),_>0&&E.push(_),m+=1,M=2*(_-u)}return[{type:v,segments:E.flatMap(A=>k({type:I,cx:f,cy:d,r:A}))},{type:P,points:[[s-p,i-l],[s+p,i-l],[s+p,i+l],[s-p,i+l]].map(n)},{type:P,points:[[s-l,i-p],[s+l,i-p],[s+l,i+p],[s-l,i+p]].map(n)}]}function Gt(t,e){const[n,s,i,c,o,u]=e,a=T([n,s],t,u),[y,h]=[i/2,c/2],f=o/2,d=y**2-f**2,p=h**2-f**2,l=Math.sqrt(d),E=p>=0?Math.sqrt(p):f,m=[0,90,180,270],M=[];for(const A of m){const _=[[E,f],[l,f],[f,l],[f,E]].map(b=>T(b,[n,s],A)).map(b=>T(b,t,u)),[wt,Ft,jt]=F({x:_[1][0],y:_[1][1]},{x:_[2][0],y:_[2][1]},{x:a[0],y:a[1]},$);if(M.push({type:x,start:_[0],end:_[1]},{type:C,start:wt,end:Ft,center:jt,radius:y},{type:x,start:_[2],end:_[3]}),!W(_[0],_[3])){const[b,zt,Bt]=F({x:_[3][0],y:_[3][1]},{x:_[0][0],y:_[0][1]},{x:a[0],y:a[1]},ot);M.push({type:C,start:b,end:zt,center:Bt,radius:h})}}return{type:v,segments:M}}function vt(t){const e=Object.create(Dt);return t===r.DRILL?Object.assign(e,bt):e}const Dt={_currentPath:void 0,_arcDirection:void 0,_ambiguousArcCenter:!1,_regionMode:!1,_defaultGraphic:void 0,plot(t,e,n){const s=[],i=this._setGraphicState(t),c=this._plotCurrentPath(t,e,i);if(c&&s.push(c),i===r.SHAPE&&(e==null?void 0:e.type)===w&&s.push({type:N,shape:Ot(e,n)}),i===r.SHAPE&&(e==null?void 0:e.type)===ct&&s.push({type:N,shape:Pt(e,n)}),i===r.SEGMENT&&(this._currentPath=this._currentPath??{segments:[],region:this._regionMode,tool:e},this._currentPath.segments.push(rt(n,this._arcDirection,this._ambiguousArcCenter))),i===r.SLOT){const o=ut([rt(n)],e);o&&s.push(o)}return s},_setGraphicState(t){if(t.type===r.INTERPOLATE_MODE&&(this._arcDirection=ft(t.mode)),t.type===r.QUADRANT_MODE&&(this._ambiguousArcCenter=t.quadrant===r.SINGLE),t.type===r.REGION_MODE&&(this._regionMode=t.region),t.type===r.GRAPHIC)return t.graphic===r.SEGMENT?this._defaultGraphic=r.SEGMENT:t.graphic!==null&&(this._defaultGraphic=void 0),t.graphic??this._defaultGraphic},_plotCurrentPath(t,e,n){if(this._currentPath!==void 0&&(e!==this._currentPath.tool||t.type===r.REGION_MODE||t.type===r.DONE||n===r.MOVE&&this._currentPath.region||n===r.SHAPE&&this._currentPath!==void 0)){const s=ut(this._currentPath.segments,this._currentPath.tool,this._currentPath.region);return this._currentPath=void 0,s}}},bt={_defaultGraphic:r.SHAPE,_ambiguousArcCenter:!0,_setGraphicState(t){if(t.type===r.INTERPOLATE_MODE){const{mode:e}=t;this._arcDirection=ft(e),e===r.CW_ARC||e===r.CCW_ARC||e===r.LINE?this._defaultGraphic=r.SEGMENT:e===r.MOVE?this._defaultGraphic=r.MOVE:this._defaultGraphic=r.SHAPE}if(t.type===r.GRAPHIC)return t.graphic??this._defaultGraphic}};function ft(t){if(t===r.CCW_ARC)return $;if(t===r.CW_ARC)return ot}function Ht(t){const e=pt(t),n=lt(),s=_t(),i=vt(t.filetype),c=[];for(const o of t.children){const u=n.use(o),a=s.use(o,e),y=i.plot(o,u,a);c.push(...y)}return{type:X,units:e.units,size:nt(c),children:c}}O.ARC=C,O.BoundingBox=yt,O.CIRCLE=I,O.IMAGE=X,O.IMAGE_PATH=z,O.IMAGE_REGION=Q,O.IMAGE_SHAPE=N,O.LAYERED_SHAPE=H,O.LINE=x,O.OUTLINE=v,O.POLYGON=P,O.RECTANGLE=G,O.TWO_PI=g,O.plot=Ht,O.positionsEqual=W,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=tracespace-plotter.umd.cjs.map