var gdjs;(function(s){const n=new s.Logger("Bitmap text"),p="GDJS-DEFAULT-BITMAP-FONT",d=5,m=(o,t)=>{const i=o.font;return o.font=t,PIXI.BitmapFont.available[t]=o,delete PIXI.BitmapFont.available[i],PIXI.BitmapFont.available[t]},F=["bitmapFont"];class h{constructor(t,i){this._pixiBitmapFontsInUse={};this._pixiBitmapFontsToUninstall=[];this._loadedFontsData=new s.ResourceCache;this._defaultSlugFontName=null;this._imageManager=i,this._resourceLoader=t}getResourceKinds(){return F}getDefaultBitmapFont(){if(this._defaultSlugFontName!==null)return PIXI.BitmapFont.available[this._defaultSlugFontName];const t="Arial",i=new PIXI.TextStyle({fontFamily:t,fontSize:20,padding:5,align:"left",fill:"#ffffff",wordWrap:!0,lineHeight:20}),e=m(PIXI.BitmapFont.from(t,i,{chars:[[" ","~"]]}),p);return this._defaultSlugFontName=e.font,e}_markBitmapFontAsUsed(t){this._pixiBitmapFontsInUse[t]=this._pixiBitmapFontsInUse[t]||{objectsUsingTheFont:0},this._pixiBitmapFontsInUse[t].objectsUsingTheFont++;for(let i=0;i<this._pixiBitmapFontsToUninstall.length;)this._pixiBitmapFontsToUninstall[i]===t?this._pixiBitmapFontsToUninstall.splice(i,1):i++}releaseBitmapFont(t){if(t!==p){if(!this._pixiBitmapFontsInUse[t]){n.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>d)){const i=this._pixiBitmapFontsToUninstall.shift();PIXI.BitmapFont.uninstall(i),n.log("Bitmap Text",'Uninstalled BitmapFont "'+i+'" from memory.')}}}obtainBitmapFont(t,i){const e=t+"@"+i;if(PIXI.BitmapFont.available[e])return this._markBitmapFontAsUsed(e),PIXI.BitmapFont.available[e];const r=this._loadedFontsData.getFromName(t);if(!r)return n.warn('Could not find Bitmap Font for resource named "'+t+'". The default font will be used.'),this.getDefaultBitmapFont();const l=this._imageManager.getPIXITexture(i);try{const a=m(PIXI.BitmapFont.install(r,l),e);return this._markBitmapFontAsUsed(e),a}catch(a){return n.error('Could not load the Bitmap Font for resource named "'+t+'". The default font will be used. Error is: '+a),this.getDefaultBitmapFont()}}async processResource(t){}async loadResource(t){const i=this._resourceLoader.getResource(t);if(!i){n.warn('Unable to find bitmap font for resource "'+t+'".');return}if(!this._loadedFontsData.get(i))try{const e=await fetch(this._resourceLoader.getFullUrl(i.file),{credentials:this._resourceLoader.checkIfCredentialsRequired(i.file)?"include":"same-origin"});if(!e.ok)throw new Error(`HTTP error while loading bitmap font. Status is ${e.status}.`);const l=(await e.text()).split(`
`).filter(a=>!a.trim().startsWith("#")).join(`
`);this._loadedFontsData.set(i,l)}catch(e){throw n.error("Can't fetch the bitmap font file "+i.file+", error: "+e),this._loadedFontsData.delete(i),e}}dispose(){for(const t in this._pixiBitmapFontsInUse)PIXI.BitmapFont.uninstall(t);for(const t of this._pixiBitmapFontsToUninstall)PIXI.BitmapFont.uninstall(t);this._pixiBitmapFontsInUse={},this._pixiBitmapFontsToUninstall.length=0,this._loadedFontsData.clear()}unloadResource(t){this._loadedFontsData.delete(t);for(const i in this._pixiBitmapFontsInUse)i.startsWith(t.name+"@")&&(PIXI.BitmapFont.uninstall(i),delete this._pixiBitmapFontsInUse[i]);for(let i=0;i<this._pixiBitmapFontsToUninstall.length;i++){const e=this._pixiBitmapFontsToUninstall[i];e.startsWith(t.name+"@")&&(PIXI.BitmapFont.uninstall(e),this._pixiBitmapFontsToUninstall.splice(i,1),i--)}}}s.PixiBitmapFontManager=h,s.BitmapFontManager=s.PixiBitmapFontManager})(gdjs||(gdjs={}));
//# sourceMappingURL=pixi-bitmapfont-manager.js.map
