/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import t from"../../../../request.js";import e from"../../../../core/pbf.js";class s{constructor(t){if(this._metrics=[],!t)return void(this._allBitmaps=null);const e=new Map;let s=0;for(;t.next();)switch(t.tag()){case 1:{const a=t.getMessage();for(;a.next();)switch(a.tag()){case 3:{const t=a.getMessage();let r,n,i,c,o,g,h;for(;t.next();)switch(t.tag()){case 1:r=t.getUInt32();break;case 2:n=t.getBytes();break;case 3:i=t.getUInt32();break;case 4:c=t.getUInt32();break;case 5:o=t.getSInt32();break;case 6:g=t.getSInt32();break;case 7:h=t.getUInt32();break;default:t.skip()}if(t.release(),r){const t=n?.length??0;this._metrics[r]={width:i,height:c,left:o,top:g,advance:h,startOffset:s,length:t},e.set(r,n),s+=t}break}default:a.skip()}a.release();break}default:t.skip()}const a=new Uint8Array(s),r=this._metrics;for(const[n,i]of e){const{startOffset:t,length:e}=r[n];if(i)for(let s=0;s<e;++s)a[t+s]=i[s]}this._allBitmaps=a}getMetrics(t){return this._metrics[t]}getBitmap(t){if(!this._allBitmaps)return;const e=this._metrics[t];if(void 0===e)return;const{startOffset:s,length:a}=e;return 0!==a?new n(this._allBitmaps,s,a):void 0}}class a{constructor(){this._ranges=[]}get ranges(){return this._ranges}getRange(t){return this._ranges[t]}addRange(t,e){this._ranges[t]=e}}class r{constructor(t){this._glyphInfo={},this._baseURL=t}getRange(a,r){const n=this._getFontStack(a);if(n.getRange(r))return Promise.resolve();const i=256*r,c=i+255;if(this._baseURL){const o=this._baseURL.replace("{fontstack}",a).replace("{range}",i+"-"+c);return t(o,{responseType:"array-buffer"}).then(t=>{n.addRange(r,new s(new e(new Uint8Array(t.data),new DataView(t.data))))}).catch(()=>{n.addRange(r,new s)})}return n.addRange(r,new s),Promise.resolve()}getGlyph(t,e){const s=this._getFontStack(t);if(!s)return;const a=Math.floor(e/256),r=s.getRange(a);return r?{metrics:r.getMetrics(e),bitmap:r.getBitmap(e)}:void 0}_getFontStack(t){let e=this._glyphInfo[t];return e||(e=this._glyphInfo[t]=new a),e}}class n{constructor(t,e,s){this._array=t,this._start=e,this.length=s}at(t){return 0<=t&&t<this.length?this._array[this._start+t]:void 0}}export{r as default};
