2017-03-03 01:41:33 +00:00
|
|
|
function getValue (node, attribute)
|
|
|
|
{
|
|
|
|
return parseInt(node.getAttribute(attribute), 10);
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
var x = (frame) ? frame.x : 0;
|
|
|
|
var y = (frame) ? frame.y : 0;
|
|
|
|
|
|
|
|
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');
|
|
|
|
var gw = getValue(node, 'width');
|
|
|
|
var gh = getValue(node, 'height');
|
2017-02-23 20:52:52 +00:00
|
|
|
|
2017-03-01 00:16:35 +00:00
|
|
|
data.chars[charCode] =
|
|
|
|
{
|
2017-03-03 01:41:33 +00:00
|
|
|
x: x + getValue(node, 'x'),
|
|
|
|
y: y + getValue(node, 'y'),
|
|
|
|
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: {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|