/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
const t=1e20;class e{constructor(t){this.pxDensity=4,this.sdfSizePx=t.sdfSizePx,this.sdfBufferPx=t.sdfBufferPx,this.rasterSizePx=this.pxDensity*this.sdfSizePx,this.rasterBufferPx=this.pxDensity*this.sdfBufferPx,this.radius=t.radius,this.cutoff=t.cutoff;const e=this.pxDensity*t.sdfSizePx*3;this.canvasSize=e;const s=this._createCanvas(e);this.context=s.getContext("2d",{willReadFrequently:!0}),this.context.imageSmoothingEnabled=!1,this.gridOuter=new Float64Array(e*e),this.gridInner=new Float64Array(e*e),this.gridValues=new Float64Array(e),this.parabolaBoundaries=new Float64Array(e+1),this.parabolaIndices=new Uint16Array(e)}draw(e,s){const{context:i,gridInner:n,gridOuter:r,rasterSizePx:a,rasterBufferPx:h,canvasSize:o,radius:l,cutoff:c}=this,f=a/e.units_per_em();this.context.fillStyle="black",this.context.strokeStyle="black",this.context.resetTransform(),this.context.clearRect(0,0,o,o),this.context.translate(a,a),this.context.scale(f,f);const x=e.paintCurves(s,i);if(!x)return null;const d=x.xMin*f,u=x.xMax*f,y=x.yMin*f,p=x.yMax*f;x.free();const M=Math.ceil(u-d),D=Math.ceil(p-y),g=Math.floor(a+d-h),w=Math.floor(a+y-h),P=M+2*h,S=D+2*h,_=new Float32Array(Math.max(P*S,0)),m=i.getImageData(g,w,P,S);r.fill(t,0,_.length),n.fill(0,0,_.length);for(let I=0;I<S;I++)for(let e=0;e<P;e++){const s=S-1-I,i=m.data[4*(s*P+e)+3]/255;if(0===i)continue;const a=I*P+e;if(1===i)r[a]=0,n[a]=t;else{const t=.5-i;r[a]=t>0?t*t:0,n[a]=t<0?t*t:0}}this._edt(r,0,0,P,S,P),this._edt(n,0,0,P,S,P);for(let t=0;t<_.length;t++)_[t]=Math.sqrt(r[t])-Math.sqrt(n[t]);const b=Math.ceil(P/this.pxDensity),z=Math.ceil(S/this.pxDensity),A=this.pxDensity*this.pxDensity,B=new Uint8ClampedArray(Math.max(b*z,0));for(let t=0;t<z;t++)for(let e=0;e<b;e++){let s=0;for(let n=0;n<this.pxDensity;n++)for(let i=0;i<this.pxDensity;i++){if(e+i>P||t+n>S)continue;s+=_[(this.pxDensity*t+n)*P+(this.pxDensity*e+i)]}const i=s/this.pxDensity/A/l+c;B[t*b+e]=Math.round(255-255*i)}const v=f*e.face_height(),F=Math.round(p-v+2*h),C=Math.round(d-h);return{buffer:B,width:Math.ceil(M/this.pxDensity),height:Math.ceil(D/this.pxDensity),top:Math.round(F/this.pxDensity),left:Math.round(C/this.pxDensity)}}_createCanvas(t){const e=document.createElement("canvas");return e.width=e.height=t,e}_edt(t,e,s,i,n,r){for(let a=e;a<e+i;a++)this._edt1d(t,s*r+a,r,n);for(let a=s;a<s+n;a++)this._edt1d(t,a*r+e,1,i)}_edt1d(e,s,i,n){const r=this.parabolaIndices,a=this.parabolaBoundaries,h=this.gridValues;r[0]=0,a[0]=-t,a[1]=t,h[0]=e[s];let o=0;for(let l=1;l<n;l++){h[l]=e[s+l*i];const n=l*l;let c;do{const t=r[o];c=(h[l]-h[t]+n-t*t)/(l-t)/2}while(c<=a[o]&&--o>-1);o++,r[o]=l,a[o]=c,a[o+1]=t}for(let t=0,l=0;t<n;t++){for(;a[l+1]<t;)l++;const n=r[l],o=t-n;e[s+t*i]=h[n]+o*o}}}export{e as GlyphSDFCreator};
