/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
const t=1e20;class e{constructor(t){this.sdfSizePx=t.sdfSizePx,this.sdfBufferPx=t.sdfBufferPx,this.radius=t.radius,this.cutoff=t.cutoff;const e=t.sdfSizePx+8*t.sdfBufferPx;this.canvasSize=e;const s=this._createCanvas(e);this.context=s.getContext("2d",{willReadFrequently:!0}),this.context.imageSmoothingEnabled=!1,this.context.fillStyle="black",this.context.strokeStyle="black",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:r,gridOuter:a,sdfSizePx:n,sdfBufferPx:o,canvasSize:l,cutoff:c,radius:h}=this;this.context.fillStyle="black",this.context.strokeStyle="black",this.context.clearRect(0,0,l,l);const f=e.paintCurves(s,i,n,o);if(!f)return null;const d=Math.ceil(f.width),u=Math.ceil(f.height),x=f.top,g=f.left;f.free();const w=d+2*o,y=u+2*o,S=new Uint8ClampedArray(Math.max(w*y,0)),b={buffer:S,width:d,height:u,top:x,left:g};if(0===d||0===u)return b;const p=i.getImageData(o,o,d,u);a.fill(t,0,S.length),r.fill(0,0,S.length);for(let P=0;P<u;P++)for(let e=0;e<d;e++){const s=p.data[4*(P*d+e)+3]/255;if(0===s)continue;const i=(P+o)*w+e+o;if(1===s)a[i]=0,r[i]=t;else{const t=.5-s;a[i]=t>0?t*t:0,r[i]=t<0?t*t:0}}this._edt(a,0,0,w,y,w),this._edt(r,0,0,w,y,w);for(let t=0;t<S.length;t++){const e=Math.sqrt(a[t])-Math.sqrt(r[t]);S[t]=Math.round(255-255*(e/h+c))}return b}_createCanvas(t){const e=document.createElement("canvas");return e.width=e.height=t,e}_edt(t,e,s,i,r,a){for(let n=e;n<e+i;n++)this._edt1d(t,s*a+n,a,r);for(let n=s;n<s+r;n++)this._edt1d(t,n*a+e,1,i)}_edt1d(e,s,i,r){const a=this.parabolaIndices,n=this.parabolaBoundaries,o=this.gridValues;a[0]=0,n[0]=-t,n[1]=t,o[0]=e[s];for(let l=1,c=0,h=0;l<r;l++){o[l]=e[s+l*i];const r=l*l;do{const t=a[c];h=(o[l]-o[t]+r-t*t)/(l-t)/2}while(h<=n[c]&&--c>-1);c++,a[c]=l,n[c]=h,n[c+1]=t}for(let t=0,l=0;t<r;t++){for(;n[l+1]<t;)l++;const r=a[l],c=t-r;e[s+t*i]=o[r]+c*c}}}export{e as GlyphSDFCreator};
