var gdjs;(function(o){const a=new o.Logger("Bitmap text"),p="GDJS-DEFAULT-BITMAP-FONT",d=5,u=(n,t)=>{const i=n.pageTextures;return i&&Object.values(i).some(e=>e.baseTexture&&e.baseTexture.destroyed||e.baseTexture!==t.baseTexture)},m=(n,t)=>{const i=n.font;return n.font=t,PIXI.BitmapFont.available[t]=n,delete PIXI.BitmapFont.available[i],PIXI.BitmapFont.available[t]},F=["bitmapFont"];class c{constructor(t,i){this._pixiBitmapFontsInUse={};this._pixiBitmapFontsToUninstall=[];this._loadedFontsData=new o.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]){a.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.available[i]&&PIXI.BitmapFont.uninstall(i),a.log("Bitmap Text",'Uninstalled BitmapFont "'+i+'" from memory.')}}}obtainBitmapFont(t,i){const e=t+"@"+i;if(PIXI.BitmapFont.available[e]&&u(PIXI.BitmapFont.available[e],this._imageManager.getPIXITexture(i))&&PIXI.BitmapFont.uninstall(e),PIXI.BitmapFont.available[e])return this._markBitmapFontAsUsed(e),PIXI.BitmapFont.available[e];const r=this._loadedFontsData.getFromName(t);if(!r)return a.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 s=m(PIXI.BitmapFont.install(r,l),e);return this._markBitmapFontAsUsed(e),s}catch(s){return a.error('Could not load the Bitmap Font for resource named "'+t+'". The default font will be used. Error is: '+s),this.getDefaultBitmapFont()}}async processResource(t){}async loadResource(t){const i=this._resourceLoader.getResource(t);if(!i){a.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(s=>!s.trim().startsWith("#")).join(`
`);this._loadedFontsData.set(i,l)}catch(e){throw a.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(let i=0;i<this._pixiBitmapFontsToUninstall.length;i++){const e=this._pixiBitmapFontsToUninstall[i];e.startsWith(t.name+"@")&&(PIXI.BitmapFont.available[e]&&PIXI.BitmapFont.uninstall(e),this._pixiBitmapFontsToUninstall.splice(i,1),i--)}}}o.PixiBitmapFontManager=c,o.BitmapFontManager=o.PixiBitmapFontManager})(gdjs||(gdjs={}));
//# sourceMappingURL=pixi-bitmapfont-manager.js.map
