2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2015-02-25 03:36:23 +00:00
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-10-03 01:38:35 +00:00
|
|
|
* Phaser.LoaderParser parses data objects from Phaser.Loader that need more preparation before they can be inserted into the Cache.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @class Phaser.LoaderParser
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-10-03 01:38:35 +00:00
|
|
|
Phaser.LoaderParser = {
|
2014-02-14 04:34:57 +00:00
|
|
|
|
2013-09-10 22:51:35 +00:00
|
|
|
/**
|
2015-07-16 00:17:03 +00:00
|
|
|
* Alias for xmlBitmapFont, for backwards compatibility.
|
2014-07-09 04:39:49 +00:00
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.LoaderParser.bitmapFont
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {object} xml - XML data you want to parse.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
|
2014-07-09 04:39:49 +00:00
|
|
|
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
|
|
|
|
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @return {object} The parsed Bitmap Font data.
|
2013-09-10 22:51:35 +00:00
|
|
|
*/
|
2015-07-16 00:17:03 +00:00
|
|
|
bitmapFont: function (xml, baseTexture, xSpacing, ySpacing) {
|
|
|
|
|
|
|
|
return this.xmlBitmapFont(xml, baseTexture, xSpacing, ySpacing);
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
},
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
/**
|
|
|
|
* Parse a Bitmap Font from an XML file.
|
|
|
|
*
|
|
|
|
* @method Phaser.LoaderParser.xmlBitmapFont
|
|
|
|
* @param {object} xml - XML data you want to parse.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
|
2015-06-06 10:35:08 +00:00
|
|
|
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
|
|
|
|
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @return {object} The parsed Bitmap Font data.
|
2015-06-06 10:35:08 +00:00
|
|
|
*/
|
2015-07-16 00:17:03 +00:00
|
|
|
xmlBitmapFont: function (xml, baseTexture, xSpacing, ySpacing) {
|
|
|
|
|
2013-09-10 22:51:35 +00:00
|
|
|
var data = {};
|
2014-02-14 03:34:35 +00:00
|
|
|
var info = xml.getElementsByTagName('info')[0];
|
|
|
|
var common = xml.getElementsByTagName('common')[0];
|
|
|
|
|
|
|
|
data.font = info.getAttribute('face');
|
|
|
|
data.size = parseInt(info.getAttribute('size'), 10);
|
2014-02-14 04:34:57 +00:00
|
|
|
data.lineHeight = parseInt(common.getAttribute('lineHeight'), 10) + ySpacing;
|
2013-09-10 22:51:35 +00:00
|
|
|
data.chars = {};
|
|
|
|
|
2014-02-14 03:34:35 +00:00
|
|
|
var letters = xml.getElementsByTagName('char');
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < letters.length; i++)
|
|
|
|
{
|
2014-02-14 03:34:35 +00:00
|
|
|
var charCode = parseInt(letters[i].getAttribute('id'), 10);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
data.chars[charCode] = {
|
2015-06-06 10:35:08 +00:00
|
|
|
x: parseInt(letters[i].getAttribute('x'), 10),
|
|
|
|
y: parseInt(letters[i].getAttribute('y'), 10),
|
|
|
|
width: parseInt(letters[i].getAttribute('width'), 10),
|
|
|
|
height: parseInt(letters[i].getAttribute('height'), 10),
|
2014-02-14 03:34:35 +00:00
|
|
|
xOffset: parseInt(letters[i].getAttribute('xoffset'), 10),
|
|
|
|
yOffset: parseInt(letters[i].getAttribute('yoffset'), 10),
|
2014-02-14 04:34:57 +00:00
|
|
|
xAdvance: parseInt(letters[i].getAttribute('xadvance'), 10) + xSpacing,
|
2015-06-06 10:35:08 +00:00
|
|
|
kerning: {}
|
2013-09-10 22:51:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-02-14 03:34:35 +00:00
|
|
|
var kernings = xml.getElementsByTagName('kerning');
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
for (i = 0; i < kernings.length; i++)
|
|
|
|
{
|
2014-02-14 03:34:35 +00:00
|
|
|
var first = parseInt(kernings[i].getAttribute('first'), 10);
|
|
|
|
var second = parseInt(kernings[i].getAttribute('second'), 10);
|
|
|
|
var amount = parseInt(kernings[i].getAttribute('amount'), 10);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
data.chars[second].kerning[first] = amount;
|
|
|
|
}
|
|
|
|
|
2015-07-16 00:17:03 +00:00
|
|
|
return this.finalizeBitmapFont(baseTexture, data);
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
},
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
/**
|
|
|
|
* Parse a Bitmap Font from a JSON file.
|
|
|
|
*
|
|
|
|
* @method Phaser.LoaderParser.jsonBitmapFont
|
|
|
|
* @param {object} json - JSON data you want to parse.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
|
2015-06-06 10:35:08 +00:00
|
|
|
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
|
|
|
|
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @return {object} The parsed Bitmap Font data.
|
2015-06-06 10:35:08 +00:00
|
|
|
*/
|
2015-07-16 00:17:03 +00:00
|
|
|
jsonBitmapFont: function (json, baseTexture, xSpacing, ySpacing) {
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
var data = {
|
2015-07-16 11:30:55 +00:00
|
|
|
font: json.font.info._face,
|
2015-06-06 10:35:08 +00:00
|
|
|
size: parseInt(json.font.info._size, 10),
|
|
|
|
lineHeight: parseInt(json.font.common._lineHeight, 10) + ySpacing,
|
|
|
|
chars: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
json.font.chars["char"].forEach(
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
function parseChar(letter) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
var charCode = parseInt(letter._id, 10);
|
|
|
|
|
|
|
|
data.chars[charCode] = {
|
|
|
|
x: parseInt(letter._x, 10),
|
|
|
|
y: parseInt(letter._y, 10),
|
|
|
|
width: parseInt(letter._width, 10),
|
|
|
|
height: parseInt(letter._height, 10),
|
|
|
|
xOffset: parseInt(letter._xoffset, 10),
|
|
|
|
yOffset: parseInt(letter._yoffset, 10),
|
|
|
|
xAdvance: parseInt(letter._xadvance, 10) + xSpacing,
|
|
|
|
kerning: {}
|
|
|
|
};
|
|
|
|
}
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
);
|
|
|
|
|
2015-07-16 11:30:55 +00:00
|
|
|
if (json.font.kernings && json.font.kernings.kerning) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-07-16 11:30:55 +00:00
|
|
|
json.font.kernings.kerning.forEach(
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-07-16 11:30:55 +00:00
|
|
|
function parseKerning(kerning) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-07-16 11:30:55 +00:00
|
|
|
data.chars[kerning._second].kerning[kerning._first] = parseInt(kerning._amount, 10);
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-07-16 11:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2015-06-06 10:35:08 +00:00
|
|
|
|
2015-07-16 00:17:03 +00:00
|
|
|
return this.finalizeBitmapFont(baseTexture, data);
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalize Bitmap Font parsing.
|
|
|
|
*
|
|
|
|
* @method Phaser.LoaderParser.finalizeBitmapFont
|
|
|
|
* @private
|
2015-07-16 00:17:03 +00:00
|
|
|
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
|
2015-06-06 10:35:08 +00:00
|
|
|
* @param {object} bitmapFontData - Pre-parsed bitmap font data.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @return {object} The parsed Bitmap Font data.
|
|
|
|
*/
|
|
|
|
finalizeBitmapFont: function (baseTexture, bitmapFontData) {
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
Object.keys(bitmapFontData.chars).forEach(
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
function addTexture(charCode) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
var letter = bitmapFontData.chars[charCode];
|
|
|
|
|
2015-07-16 00:17:03 +00:00
|
|
|
letter.texture = new PIXI.Texture(baseTexture, new Phaser.Rectangle(letter.x, letter.y, letter.width, letter.height));
|
|
|
|
|
2015-06-06 10:54:14 +00:00
|
|
|
}
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-07-16 00:17:03 +00:00
|
|
|
return bitmapFontData;
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
}
|
2014-03-10 06:59:32 +00:00
|
|
|
};
|