Skip to content

Commit e195aac

Browse files
committed
ParseXMLBitmapFont will now calculate the WebGL uv data for the glyphs during parsing. This avoids it having to be done during rendering, saving CPU cycles on an operation that never changes.
1 parent 0385d10 commit e195aac

4 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/gameobjects/bitmaptext/ParseFromAtlas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var ParseFromAtlas = function (scene, fontName, textureKey, frameKey, xmlKey, xS
3232

3333
if (frame && xml)
3434
{
35-
var data = ParseXMLBitmapFont(xml, xSpacing, ySpacing, frame);
35+
var data = ParseXMLBitmapFont(xml, frame, xSpacing, ySpacing);
3636

3737
scene.sys.cache.bitmapFont.add(fontName, { data: data, texture: textureKey, frame: frameKey });
3838

src/gameobjects/bitmaptext/ParseXMLBitmapFont.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,22 @@ function getValue (node, attribute)
2929
* @private
3030
*
3131
* @param {XMLDocument} xml - The XML Document to parse the font from.
32+
* @param {Phaser.Textures.Frame} frame - The texture frame to take into account when creating the uv data.
3233
* @param {integer} [xSpacing=0] - The x-axis spacing to add between each letter.
3334
* @param {integer} [ySpacing=0] - The y-axis spacing to add to the line height.
34-
* @param {Phaser.Textures.Frame} [frame] - The texture frame to take into account while parsing.
3535
*
3636
* @return {Phaser.Types.GameObjects.BitmapText.BitmapFontData} The parsed Bitmap Font data.
3737
*/
38-
var ParseXMLBitmapFont = function (xml, xSpacing, ySpacing, frame)
38+
var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
3939
{
4040
if (xSpacing === undefined) { xSpacing = 0; }
4141
if (ySpacing === undefined) { ySpacing = 0; }
4242

43+
var textureX = frame.cutX;
44+
var textureY = frame.cutY;
45+
var textureWidth = frame.source.width;
46+
var textureHeight = frame.source.height;
47+
4348
var data = {};
4449
var info = xml.getElementsByTagName('info')[0];
4550
var common = xml.getElementsByTagName('common')[0];
@@ -84,6 +89,15 @@ var ParseXMLBitmapFont = function (xml, xSpacing, ySpacing, frame)
8489
}
8590
}
8691

92+
if (adjustForTrim && top !== 0 && left !== 0)
93+
{
94+
// Now we know the top and left coordinates of the glyphs in the original data
95+
// so we can work out how much to adjust the glyphs by
96+
97+
gx -= frame.x;
98+
gy -= frame.y;
99+
}
100+
87101
data.chars[charCode] =
88102
{
89103
x: gx,
@@ -96,24 +110,14 @@ var ParseXMLBitmapFont = function (xml, xSpacing, ySpacing, frame)
96110
yOffset: getValue(node, 'yoffset'),
97111
xAdvance: getValue(node, 'xadvance') + xSpacing,
98112
data: {},
99-
kerning: {}
113+
kerning: {},
114+
u0: (textureX + gx) / textureWidth,
115+
v0: (textureY + gy) / textureHeight,
116+
u1: (textureX + gx + gw) / textureWidth,
117+
v1: (textureY + gy + gh) / textureHeight
100118
};
101119
}
102120

103-
if (adjustForTrim && top !== 0 && left !== 0)
104-
{
105-
// Now we know the top and left coordinates of the glyphs in the original data
106-
// so we can work out how much to adjust the glyphs by
107-
108-
for (var code in data.chars)
109-
{
110-
var glyph = data.chars[code];
111-
112-
glyph.x -= frame.x;
113-
glyph.y -= frame.y;
114-
}
115-
}
116-
117121
var kernings = xml.getElementsByTagName('kerning');
118122

119123
for (i = 0; i < kernings.length; i++)

src/gameobjects/bitmaptext/typedefs/BitmapFontCharacterData.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* Describes the character's position, size, offset and kerning.
55
*
6+
* As of version 3.50 it also includes the WebGL texture uv data.
7+
*
68
* @typedef {object} Phaser.Types.GameObjects.BitmapText.BitmapFontCharacterData
79
* @since 3.0.0
810
*
@@ -14,6 +16,10 @@
1416
* @property {number} centerY - The center y position of the character.
1517
* @property {number} xOffset - The x offset of the character.
1618
* @property {number} yOffset - The y offset of the character.
19+
* @property {number} u0 - WebGL texture u0.
20+
* @property {number} v0 - WebGL texture v0.
21+
* @property {number} u1 - WebGL texture u1.
22+
* @property {number} v1 - WebGL texture v1.
1723
* @property {object} data - Extra data for the character.
1824
* @property {Object.<number>} kerning - Kerning values, keyed by character code.
1925
*/

src/loader/filetypes/BitmapFontFile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ var BitmapFontFile = new Class({
9999
image.addToCache();
100100
xml.addToCache();
101101

102-
this.loader.cacheManager.bitmapFont.add(image.key, { data: ParseXMLBitmapFont(xml.data), texture: image.key, frame: null });
102+
var data = ParseXMLBitmapFont(xml.data, image.cache.getFrame(image.key));
103+
104+
this.loader.cacheManager.bitmapFont.add(image.key, { data: data, texture: image.key, frame: null });
103105

104106
this.complete = true;
105107
}

0 commit comments

Comments
 (0)