2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-03-15 17:27:30 +00:00
|
|
|
/**
|
|
|
|
* @function getValue
|
|
|
|
* @since 3.0.0
|
|
|
|
* @private
|
|
|
|
*/
|
2017-03-03 01:41:33 +00:00
|
|
|
function getValue (node, attribute)
|
|
|
|
{
|
|
|
|
return parseInt(node.getAttribute(attribute), 10);
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:27:30 +00:00
|
|
|
/**
|
|
|
|
* @function ParseXMLBitmapFont
|
|
|
|
* @since 3.0.0
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* @param {[type]} xml - [description]
|
|
|
|
* @param {integer} xSpacing - [description]
|
|
|
|
* @param {integer} ySpacing - [description]
|
|
|
|
* @param {[type]} frame - [description]
|
|
|
|
*/
|
2017-03-01 00:16:35 +00:00
|
|
|
var ParseXMLBitmapFont = function (xml, xSpacing, ySpacing, frame)
|
2017-02-23 20:52:52 +00:00
|
|
|
{
|
2017-03-01 00:16:35 +00:00
|
|
|
if (xSpacing === undefined) { xSpacing = 0; }
|
|
|
|
if (ySpacing === undefined) { ySpacing = 0; }
|
|
|
|
|
2017-02-23 20:52:52 +00:00
|
|
|
var data = {};
|
|
|
|
var info = xml.getElementsByTagName('info')[0];
|
|
|
|
var common = xml.getElementsByTagName('common')[0];
|
|
|
|
|
|
|
|
data.font = info.getAttribute('face');
|
2017-03-03 01:41:33 +00:00
|
|
|
data.size = getValue(info, 'size');
|
|
|
|
data.lineHeight = getValue(common, 'lineHeight') + ySpacing;
|
2017-02-23 20:52:52 +00:00
|
|
|
data.chars = {};
|
|
|
|
|
|
|
|
var letters = xml.getElementsByTagName('char');
|
|
|
|
|
2017-07-12 02:40:27 +00:00
|
|
|
var adjustForTrim = (frame !== undefined && frame.trimmed);
|
2017-07-11 23:49:20 +00:00
|
|
|
|
2017-07-12 02:40:27 +00:00
|
|
|
if (adjustForTrim)
|
2017-07-11 23:49:20 +00:00
|
|
|
{
|
2017-07-12 02:40:27 +00:00
|
|
|
var top = frame.height;
|
|
|
|
var left = frame.width;
|
2017-07-11 23:49:20 +00:00
|
|
|
}
|
2017-02-23 20:52:52 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < letters.length; i++)
|
|
|
|
{
|
2017-03-03 01:41:33 +00:00
|
|
|
var node = letters[i];
|
|
|
|
|
|
|
|
var charCode = getValue(node, 'id');
|
2017-07-12 02:40:27 +00:00
|
|
|
var gx = getValue(node, 'x');
|
|
|
|
var gy = getValue(node, 'y');
|
2017-03-03 01:41:33 +00:00
|
|
|
var gw = getValue(node, 'width');
|
|
|
|
var gh = getValue(node, 'height');
|
2017-02-23 20:52:52 +00:00
|
|
|
|
2017-07-12 02:40:27 +00:00
|
|
|
// Handle frame trim issues
|
|
|
|
|
|
|
|
if (adjustForTrim)
|
|
|
|
{
|
|
|
|
if (gx < left)
|
|
|
|
{
|
|
|
|
left = gx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gy < top)
|
|
|
|
{
|
|
|
|
top = gy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 00:16:35 +00:00
|
|
|
data.chars[charCode] =
|
|
|
|
{
|
2017-07-12 02:40:27 +00:00
|
|
|
x: gx,
|
|
|
|
y: gy,
|
2017-03-03 01:41:33 +00:00
|
|
|
width: gw,
|
|
|
|
height: gh,
|
|
|
|
centerX: Math.floor(gw / 2),
|
|
|
|
centerY: Math.floor(gh / 2),
|
|
|
|
xOffset: getValue(node, 'xoffset'),
|
|
|
|
yOffset: getValue(node, 'yoffset'),
|
|
|
|
xAdvance: getValue(node, 'xadvance') + xSpacing,
|
2017-06-08 01:24:50 +00:00
|
|
|
data: {},
|
2017-02-23 20:52:52 +00:00
|
|
|
kerning: {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-12 02:40:27 +00:00
|
|
|
if (adjustForTrim && top !== 0 && left !== 0)
|
|
|
|
{
|
|
|
|
// console.log('top and left', top, left, frame.x, frame.y);
|
|
|
|
|
|
|
|
// Now we know the top and left coordinates of the glyphs in the original data
|
|
|
|
// so we can work out how much to adjust the glyphs by
|
|
|
|
|
|
|
|
for (var code in data.chars)
|
|
|
|
{
|
|
|
|
var glyph = data.chars[code];
|
|
|
|
|
|
|
|
glyph.x -= frame.x;
|
|
|
|
glyph.y -= frame.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 20:52:52 +00:00
|
|
|
var kernings = xml.getElementsByTagName('kerning');
|
|
|
|
|
|
|
|
for (i = 0; i < kernings.length; i++)
|
|
|
|
{
|
2017-03-03 01:41:33 +00:00
|
|
|
var kern = kernings[i];
|
|
|
|
|
|
|
|
var first = getValue(kern, 'first');
|
|
|
|
var second = getValue(kern, 'second');
|
|
|
|
var amount = getValue(kern, 'amount');
|
2017-02-23 20:52:52 +00:00
|
|
|
|
|
|
|
data.chars[second].kerning[first] = amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2017-03-01 00:16:35 +00:00
|
|
|
};
|
2017-02-23 18:18:01 +00:00
|
|
|
|
2017-03-01 00:16:35 +00:00
|
|
|
module.exports = ParseXMLBitmapFont;
|