phaser/v3/src/gameobjects/bitmaptext/ParseXMLBitmapFont.js

81 lines
2 KiB
JavaScript
Raw Normal View History

function getValue (node, attribute)
{
return parseInt(node.getAttribute(attribute), 10);
}
// "azo-fire":
// {
// "frame": {"x":2,"y":2,"w":1020,"h":376},
// "rotated": false,
// "trimmed": true,
// "spriteSourceSize": {"x":1,"y":1,"w":1020,"h":376},
// "sourceSize": {"w":1024,"h":512},
// "pivot": {"x":0.5,"y":0.5}
// },
var ParseXMLBitmapFont = function (xml, xSpacing, ySpacing, frame)
2017-02-23 20:52:52 +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');
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 = 0;
var y = 0;
if (frame && frame.trim)
{
}
2017-02-23 20:52:52 +00:00
for (var i = 0; i < letters.length; i++)
{
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
data.chars[charCode] =
{
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,
data: {},
2017-02-23 20:52:52 +00:00
kerning: {}
};
}
var kernings = xml.getElementsByTagName('kerning');
for (i = 0; i < kernings.length; i++)
{
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-02-23 18:18:01 +00:00
module.exports = ParseXMLBitmapFont;