var gdjs;(function(l){const o=new l.Logger("Bitmap text"),a=GlobalPIXIModule.PIXI,m="GDJS-DEFAULT-BITMAP-FONT",u=5,d=(r,t)=>{const e=r.font;return r.font=t,a.BitmapFont.available[t]=r,delete a.BitmapFont.available[e],a.BitmapFont.available[t]};class h{constructor(t,e,i){this._pixiBitmapFontsInUse={};this._pixiBitmapFontsToUninstall=[];this._loadedFontsData={};this._defaultSlugFontName=null;this._resources=t,this._imageManager=i,this._resourcesLoader=e}getDefaultBitmapFont(){if(this._defaultSlugFontName!==null)return a.BitmapFont.available[this._defaultSlugFontName];const t="Arial",e=new a.TextStyle({fontFamily:t,fontSize:20,padding:5,align:"left",fill:"#ffffff",wordWrap:!0,lineHeight:20}),i=d(a.BitmapFont.from(t,e,{chars:[[" ","~"]]}),m);return this._defaultSlugFontName=i.font,i}setResources(t){this._resources=t}_markBitmapFontAsUsed(t){this._pixiBitmapFontsInUse[t]=this._pixiBitmapFontsInUse[t]||{objectsUsingTheFont:0},this._pixiBitmapFontsInUse[t].objectsUsingTheFont++;for(let e=0;e<this._pixiBitmapFontsToUninstall.length;)this._pixiBitmapFontsToUninstall[e]===t?this._pixiBitmapFontsToUninstall.splice(e,1):e++}releaseBitmapFont(t){if(t!==m){if(!this._pixiBitmapFontsInUse[t]){o.warn("BitmapFont with name "+t+" was tried to be released but was never marked as used.");return}if(this._pixiBitmapFontsInUse[t].objectsUsingTheFont--,this._pixiBitmapFontsInUse[t].objectsUsingTheFont===0&&(delete this._pixiBitmapFontsInUse[t],this._pixiBitmapFontsToUninstall.includes(t)||this._pixiBitmapFontsToUninstall.push(t),this._pixiBitmapFontsToUninstall.length>u)){const e=this._pixiBitmapFontsToUninstall.shift();a.BitmapFont.uninstall(e),o.log("Bitmap Text",'Uninstalled BitmapFont "'+e+'" from memory.')}}}obtainBitmapFont(t,e){const i=t+"@"+e;if(a.BitmapFont.available[i])return this._markBitmapFontAsUsed(i),a.BitmapFont.available[i];const n=this._loadedFontsData[t];if(!n)return o.warn('Could not find Bitmap Font for resource named "'+t+'". The default font will be used.'),this.getDefaultBitmapFont();const s=this._imageManager.getPIXITexture(e);try{const p=d(a.BitmapFont.install(n,s),i);return this._markBitmapFontAsUsed(i),p}catch(p){return o.error('Could not load the Bitmap Font for resource named "'+t+'". The default font will be used. Error is: '+p),this.getDefaultBitmapFont()}}loadBitmapFontData(t){const e=this._resources.filter(n=>n.kind==="bitmapFont"&&!n.disablePreload);if(e.length===0)return Promise.resolve([]);let i=0;return Promise.all(e.map(n=>fetch(this._resourcesLoader.getFullUrl(n.file),{credentials:this._resourcesLoader.checkIfCredentialsRequired(n.file)?"include":"same-origin"}).then(s=>s.text()).then(s=>{this._loadedFontsData[n.name]=s}).catch(s=>{o.error("Can't fetch the bitmap font file "+n.file+", error: "+s)}).then(()=>{i++,t(i,e.length)})))}}l.PixiBitmapFontManager=h,l.BitmapFontManager=l.PixiBitmapFontManager})(gdjs||(gdjs={}));
//# sourceMappingURL=pixi-bitmapfont-manager.js.map
