/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../chunks/_rollupPluginBabelHelpers","../../../../core/has","../../../../core/promiseUtils","./Rect","./RectangleBinPack","../../../webgl/enums","../../../webgl/Texture"],(function(t,e,i,h,s,n,r){"use strict";const a=256,c=t=>Math.floor(t/256);function o(t){const e=new Set;for(const i of t)e.add(c(i));return e}function u(t,e,h){return t.has(e)||t.set(e,h().then((()=>{t.delete(e)})).catch((h=>{t.delete(e),i.throwIfNotAbortError(h)}))),t.get(e)}const l=t=>({rect:new h(0,0,0,0),page:0,metrics:{left:0,width:0,height:0,advance:0,top:0},code:t,sdf:!0});let g=function(){function i(t,e,i){this.width=0,this.height=0,this._dirties=[],this._glyphData=[],this._currentPage=0,this._glyphCache={},this._textures=[],this._rangePromises=new Map,this.width=t,this.height=e,this._glyphSource=i,this._binPack=new s(t-4,e-4),this._glyphData.push(new Uint8Array(t*e)),this._dirties.push(!0),this._textures.push(null),this._initDecorationGlyph()}var c=i.prototype;return c.dispose=function(){this._binPack=null;for(const t of this._textures)t&&t.dispose();this._textures.length=0,this._glyphData.length=0},c._initDecorationGlyph=function(){const t=[117,149,181,207,207,181,149,117],e=[];for(let h=0;h<t.length;h++){const i=t[h];for(let t=0;t<11;t++)e.push(i)}const i={metrics:{width:5,height:2,left:0,top:0,advance:0},bitmap:new Uint8Array(e)};this._recordGlyph(i)},c.getGlyphItems=function(){var e=t._asyncToGenerator((function*(t,e,i){const h=this._getGlyphCache(t);return yield this._fetchRanges(t,e,i),e.map((e=>this._getMosaicItem(h,t,e)))}));function i(t,i,h){return e.apply(this,arguments)}return i}(),c.bind=function(t,e,i,h){const s=this._getTexture(t,i);s.setSamplingMode(e),this._dirties[i]&&(s.setData(this._glyphData[i]),this._dirties[i]=!1),t.bindTexture(s,h)},c._getGlyphCache=function(t){return this._glyphCache[t]||(this._glyphCache[t]={}),this._glyphCache[t]},c._getTexture=function(t,e){return this._textures[e]||(this._textures[e]=new r.Texture(t,{pixelFormat:n.PixelFormat.ALPHA,dataType:n.PixelType.UNSIGNED_BYTE,width:this.width,height:this.height},new Uint8Array(this.width*this.height))),this._textures[e]},c._invalidate=function(){this._dirties[this._currentPage]=!0},c._fetchRanges=function(){var e=t._asyncToGenerator((function*(t,e,i){const h=o(e),s=[];h.forEach((e=>{s.push(this._fetchRange(t,e,i))})),yield Promise.all(s)}));function i(t,i,h){return e.apply(this,arguments)}return i}(),c._fetchRange=function(){var e=t._asyncToGenerator((function*(t,e,i){if(e>a)return null;const h=t+e;return u(this._rangePromises,h,(()=>this._glyphSource.getRange(t,e,i)))}));function i(t,i,h){return e.apply(this,arguments)}return i}(),c._getMosaicItem=function(t,e,i){if(!t[i]){const h=this._glyphSource.getGlyph(e,i);if(!h||!h.metrics)return l(i);const s=this._recordGlyph(h),n=this._currentPage,r=h.metrics;t[i]={rect:s,page:n,metrics:r,code:i,sdf:!0},this._invalidate()}return t[i]},c._recordGlyph=function(t){const i=t.metrics;let n;if(0===i.width)n=new h(0,0,0,0);else{const h=3,r=i.width+2*h,a=i.height+2*h;n=this._binPack.allocate(r,a),n.isEmpty&&(this._dirties[this._currentPage]||(this._glyphData[this._currentPage]=null),this._currentPage=this._glyphData.length,this._glyphData.push(new Uint8Array(this.width*this.height)),this._dirties.push(!0),this._textures.push(null),this._initDecorationGlyph(),this._binPack=new s(this.width-4,this.height-4),n=this._binPack.allocate(r,a));const c=this._glyphData[this._currentPage],o=t.bitmap;let u,l;if(o)for(let t=0;t<a;t++){u=r*t,l=this.width*(n.y+t)+n.x;for(let t=0;t<r;t++)c[l+t]=o[u+t]}e("esri-glyph-debug")&&this._showDebugPage(c)}return n},c._showDebugPage=function(t){const e=document.createElement("canvas"),i=e.getContext("2d"),h=new ImageData(this.width,this.height),s=h.data;e.width=this.width,e.height=this.height,e.style.border="1px solid black";for(let n=0;n<t.length;++n)s[4*n+0]=t[n],s[4*n+1]=0,s[4*n+2]=0,s[4*n+3]=255;i.putImageData(h,0,0),document.body.appendChild(e)},i}();return g}));
