Skip to content

Commit 9314c5a

Browse files
committed
LoaderParser.bitmapFont, xmlBitmapFont and jsonBitmapFont all now return the font data rather than write it to the now deprecated PIXI global font cache.
1 parent 3c7293a commit 9314c5a

3 files changed

Lines changed: 41 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ For the full list of p2 additions please read [their change log](https://github.
393393
* Canvas.setSmoothingEnabled only applies the value of the property exists, which avoids the Chrome webkit prefix deprecation warnings.
394394
* PIXI._CompileShader can now take an array or a string for the fragment src.
395395
* AnimationParser.spriteSheet can now accept either a string-based key or an HTML Image object as the key argument.
396+
* LoaderParser.bitmapFont, xmlBitmapFont and jsonBitmapFont all now return the font data rather than write it to the now deprecated PIXI global font cache.
396397

397398
### Bug Fixes
398399

src/loader/Cache.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,16 +449,13 @@ Phaser.Cache.prototype = {
449449

450450
if (atlasType === 'json')
451451
{
452-
// TODO: These can return the object rather than add it to the PIXI Cache
453-
Phaser.LoaderParser.jsonBitmapFont(this.game, atlasData, key, xSpacing, ySpacing);
452+
obj.font = Phaser.LoaderParser.jsonBitmapFont(atlasData, obj.base, xSpacing, ySpacing);
454453
}
455454
else
456455
{
457-
Phaser.LoaderParser.xmlBitmapFont(this.game, atlasData, key, xSpacing, ySpacing);
456+
obj.font = Phaser.LoaderParser.xmlBitmapFont(atlasData, obj.base, xSpacing, ySpacing);
458457
}
459458

460-
obj.font = PIXI.BitmapText.fonts[key];;
461-
462459
this._cache.bitmapFont[key] = obj;
463460

464461
this._resolveURL(url, obj);

src/loader/LoaderParser.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,33 @@
1212
Phaser.LoaderParser = {
1313

1414
/**
15-
* Alias for xmlBitmapFont, for backwards compatiblity.
15+
* Alias for xmlBitmapFont, for backwards compatibility.
1616
*
1717
* @method Phaser.LoaderParser.bitmapFont
18-
* @param {Phaser.Game} game - A reference to the current game.
1918
* @param {object} xml - XML data you want to parse.
20-
* @param {string} cacheKey - The key of the texture this font uses in the cache.
19+
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
2120
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
2221
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
22+
* @return {object} The parsed Bitmap Font data.
2323
*/
24-
bitmapFont: function (game, xml, cacheKey, xSpacing, ySpacing) {
25-
this.xmlBitmapFont(game, xml, cacheKey, xSpacing, ySpacing);
24+
bitmapFont: function (xml, baseTexture, xSpacing, ySpacing) {
25+
26+
return this.xmlBitmapFont(xml, baseTexture, xSpacing, ySpacing);
27+
2628
},
2729

2830
/**
2931
* Parse a Bitmap Font from an XML file.
3032
*
3133
* @method Phaser.LoaderParser.xmlBitmapFont
32-
* @param {Phaser.Game} game - A reference to the current game.
3334
* @param {object} xml - XML data you want to parse.
34-
* @param {string} cacheKey - The key of the texture this font uses in the cache.
35+
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
3536
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
3637
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
38+
* @return {object} The parsed Bitmap Font data.
3739
*/
38-
xmlBitmapFont: function (game, xml, cacheKey, xSpacing, ySpacing) {
40+
xmlBitmapFont: function (xml, baseTexture, xSpacing, ySpacing) {
41+
3942
var data = {};
4043
var info = xml.getElementsByTagName('info')[0];
4144
var common = xml.getElementsByTagName('common')[0];
@@ -74,20 +77,22 @@ Phaser.LoaderParser = {
7477
data.chars[second].kerning[first] = amount;
7578
}
7679

77-
this.finalizeBitmapFont(cacheKey, data);
80+
return this.finalizeBitmapFont(baseTexture, data);
81+
7882
},
7983

8084
/**
8185
* Parse a Bitmap Font from a JSON file.
8286
*
8387
* @method Phaser.LoaderParser.jsonBitmapFont
84-
* @param {Phaser.Game} game - A reference to the current game.
8588
* @param {object} json - JSON data you want to parse.
86-
* @param {string} cacheKey - The key of the texture this font uses in the cache.
89+
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
8790
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
8891
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
92+
* @return {object} The parsed Bitmap Font data.
8993
*/
90-
jsonBitmapFont: function (game, json, cacheKey, xSpacing, ySpacing) {
94+
jsonBitmapFont: function (json, baseTexture, xSpacing, ySpacing) {
95+
9196
var data = {
9297
font: json.font.info._font,
9398
size: parseInt(json.font.info._size, 10),
@@ -96,7 +101,9 @@ Phaser.LoaderParser = {
96101
};
97102

98103
json.font.chars["char"].forEach(
104+
99105
function parseChar(letter) {
106+
100107
var charCode = parseInt(letter._id, 10);
101108

102109
data.chars[charCode] = {
@@ -110,38 +117,47 @@ Phaser.LoaderParser = {
110117
kerning: {}
111118
};
112119
}
120+
113121
);
114122

115123
json.font.kernings.kerning.forEach(
124+
116125
function parseKerning(kerning) {
126+
117127
data.chars[kerning._second].kerning[kerning._first] = parseInt(kerning._amount, 10);
128+
118129
}
130+
119131
);
120132

121-
this.finalizeBitmapFont(cacheKey, data);
133+
return this.finalizeBitmapFont(baseTexture, data);
134+
122135
},
123136

124137
/**
125138
* Finalize Bitmap Font parsing.
126139
*
127140
* @method Phaser.LoaderParser.finalizeBitmapFont
128141
* @private
129-
* @param {string} cacheKey - The key of the texture this font uses in the cache.
142+
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
130143
* @param {object} bitmapFontData - Pre-parsed bitmap font data.
131-
*/
132-
finalizeBitmapFont: function (cacheKey, bitmapFontData) {
144+
* @return {object} The parsed Bitmap Font data.
145+
*/
146+
finalizeBitmapFont: function (baseTexture, bitmapFontData) {
147+
133148
Object.keys(bitmapFontData.chars).forEach(
149+
134150
function addTexture(charCode) {
151+
135152
var letter = bitmapFontData.chars[charCode];
136-
var textureRect = new PIXI.Rectangle(
137-
letter.x, letter.y,
138-
letter.width, letter.height
139-
);
140153

141-
letter.texture = new PIXI.Texture(PIXI.BaseTextureCache[cacheKey], textureRect);
154+
letter.texture = new PIXI.Texture(baseTexture, new Phaser.Rectangle(letter.x, letter.y, letter.width, letter.height));
155+
142156
}
157+
143158
);
144159

145-
PIXI.BitmapText.fonts[cacheKey] = bitmapFontData;
160+
return bitmapFontData;
161+
146162
}
147163
};

0 commit comments

Comments
 (0)