Skip to content

Commit

Permalink
Fix BitmapFont destroying respects external Textures (#7327)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtimebuddy committed Mar 24, 2021
1 parent da816e6 commit c14cc03
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
25 changes: 19 additions & 6 deletions packages/text-bitmap/src/BitmapFont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,24 @@ export class BitmapFont
public readonly lineHeight: number;
public readonly chars: Dict<IBitmapFontCharacter>;
public readonly pageTextures: Dict<Texture>;
private _ownsTextures: boolean;

/**
* @param {PIXI.BitmapFontData} data
* @param {PIXI.Texture[]|Object.<string, PIXI.Texture>} textures
* @param {boolean} ownsTextures - Setting to `true` will destroy page textures
* when the font is uninstalled.
*/
constructor(data: BitmapFontData, textures: Texture[]|Dict<Texture>)
constructor(data: BitmapFontData, textures: Texture[]|Dict<Texture>, ownsTextures?: boolean)
{
const [info] = data.info;
const [common] = data.common;
const [page] = data.page;
const res = getResolutionOfUrl(page.file);
const pageTextures: Dict<Texture> = {};

this._ownsTextures = ownsTextures;

/**
* The name of the font face.
*
Expand Down Expand Up @@ -234,7 +239,11 @@ export class BitmapFont

for (const id in this.pageTextures)
{
this.pageTextures[id].destroy(true);
if (this._ownsTextures)
{
this.pageTextures[id].destroy(true);
}

this.pageTextures[id] = null;
}

Expand All @@ -251,12 +260,16 @@ export class BitmapFont
* characters map that could be provided as xml or raw string.
* @param {Object.<string, PIXI.Texture>|PIXI.Texture|PIXI.Texture[]}
* textures - List of textures for each page.
* @param managedTexture - Set to `true` to destroy page textures
* when the font is uninstalled. By default fonts created with
* `BitmapFont.from` or from the `BitmapFontLoader` are `true`.
* @return {PIXI.BitmapFont} Result font object with font, size, lineHeight
* and char fields.
*/
public static install(
data: string|XMLDocument|BitmapFontData,
textures: Texture|Texture[]|Dict<Texture>
textures: Texture|Texture[]|Dict<Texture>,
ownsTextures?: boolean
): BitmapFont
{
let fontData;
Expand All @@ -283,7 +296,7 @@ export class BitmapFont
textures = [textures];
}

const font = new BitmapFont(fontData, textures);
const font = new BitmapFont(fontData, textures, ownsTextures);

BitmapFont.available[font.font] = font;

Expand All @@ -294,7 +307,7 @@ export class BitmapFont
* Remove bitmap font by name.
*
* @static
* @param {string} name
* @param name - Name of the font to uninstall.
*/
public static uninstall(name: string): void
{
Expand Down Expand Up @@ -505,7 +518,7 @@ export class BitmapFont
}
}

const font = new BitmapFont(fontData, textures);
const font = new BitmapFont(fontData, textures, true);

// Make it easier to replace a font
if (BitmapFont.available[name] !== undefined)
Expand Down
2 changes: 1 addition & 1 deletion packages/text-bitmap/src/BitmapFontLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class BitmapFontLoader

if (Object.keys(textures).length === data.page.length)
{
resource.bitmapFont = BitmapFont.install(data, textures);
resource.bitmapFont = BitmapFont.install(data, textures, true);
next();
}
};
Expand Down
40 changes: 39 additions & 1 deletion packages/text-bitmap/test/BitmapFontLoader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const path = require('path');
const fs = require('fs');
const { Loader } = require('@pixi/loaders');
const { BaseTextureCache, TextureCache } = require('@pixi/utils');
const { BaseTextureCache, TextureCache, destroyTextureCache } = require('@pixi/utils');
const { Texture, BaseTexture } = require('@pixi/core');
const { Spritesheet } = require('@pixi/spritesheet');
const { BitmapFont, BitmapFontLoader } = require('../');
const { expect } = require('chai');

describe('PIXI.BitmapFontLoader', function ()
{
Expand Down Expand Up @@ -516,6 +517,43 @@ describe('PIXI.BitmapFontLoader', function ()
});
});

it('should load and uninstall font cleanly, remove all textures', function (done)
{
const loader = new Loader();
const fontPath = path.join(this.resources, 'font.fnt');
const textureCount = Object.keys(TextureCache).length;

expect(BitmapFont.available.font).to.be.undefined;

loader.use(BitmapFontLoader.use);
loader.add('font', fontPath);
loader.load(() =>
{
expect(BitmapFont.available.font).to.not.be.undefined;
BitmapFont.uninstall('font');
expect(BitmapFont.available.font).to.be.undefined;
expect(Object.keys(TextureCache).length - textureCount).equals(0);

done();
});
});

it('should load and uninstall font cleanly, preserve textures', function ()
{
const textureCount = Object.keys(TextureCache).length;
const texture = Texture.from(this.fontImage);
const fontText = BitmapFont.install(this.fontText, texture);

expect(BitmapFont.available.fontText).equals(fontText);

BitmapFont.uninstall('fontText');

expect(BitmapFont.available.fontText).to.be.undefined;
expect(Object.keys(TextureCache).length - textureCount).equals(1);

texture.destroy(true);
});

it('should properly register bitmap font based on text format', function (done)
{
const texture = Texture.from(this.fontImage);
Expand Down

0 comments on commit c14cc03

Please sign in to comment.