/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import t from"../../../../config.js";import e from"../../../../request.js";import a from"../../../../core/pbf.js";class s{constructor(t){this._rects=[],this._shapings=[],this._bitmaps=[];const e=new Map;let a=0;for(;t.next();)switch(t.tag()){case 1:{const s=t.getMessage();for(;s.next();)switch(s.tag()){case 3:{const t=s.getMessage();let n,r,i,c,o,f,g;for(;t.next();)switch(t.tag()){case 1:n=t.getUInt32();break;case 2:r=t.getBytes();break;case 3:i=t.getUInt32();break;case 4:c=t.getUInt32();break;case 5:o=t.getSInt32();break;case 6:f=t.getSInt32();break;case 7:g=t.getUInt32();break;default:t.skip()}if(t.release(),n){const t={width:i,height:c,left:o,top:f};this._rects[n]=t;const s={xOffset:0,yOffset:0,xAdvance:g,yAdvance:0};if(this._shapings[n]=s,r){const t=r.length;e.set(n,r),this._bitmaps[n]={start:a,length:t},a+=t}}break}default:s.skip()}s.release();break}default:t.skip()}this._buffer=new Uint8Array(a);for(const[s,n]of e)this._buffer.set(n,this._bitmaps[s].start)}getRect(t){return this._rects.at(t)}getShaping(t){return this._shapings.at(t)}getBitmap(t){const e=this._bitmaps.at(t);if(void 0!==e&&0!==e.length)return this._buffer.subarray(e.start,e.start+e.length)}}class n{constructor(){this._ranges=[]}getRange(t){return this._ranges.at(t)}addRange(t,e){this._ranges[t]=e}}class r{constructor(e){this._glyphInfo={},this._baseURL=e,this._woff2URL=`${t.fontsUrl}/woff2/{fontstack}.woff2`}async fetchRange(t,n,r){const i=this._getFontStack(t);if(i.getRange(n))return;const c=256*n,o=c+255,f=this._baseURL.replace("{fontstack}",t).replace("{range}",c+"-"+o);return e(f,{responseType:"array-buffer",...r}).then(t=>{i.addRange(n,new s(new a(new Uint8Array(t.data),new DataView(t.data))))})}async preloadASCIIRange(t){const n=this._getFontStack(t),r=0,i=255,c=this._baseURL.replace("{fontstack}",t).replace("{range}",r+"-"+i),o=await e(c,{responseType:"array-buffer"}),f=new s(new a(new Uint8Array(o.data),new DataView(o.data)));for(let e=r;e<=i;e++)n.getRange(e)||n.addRange(e,f)}getRange(t,e){return this._getFontStack(t)?.getRange(Math.floor(e/256))}woffURL(t){return this._woff2URL.replace("{fontstack}",t)}_getFontStack(t){let e=this._glyphInfo[t];return e||(e=this._glyphInfo[t]=new n),e}}export{r as default};
