forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseXMLBitmapFont.js
More file actions
135 lines (112 loc) · 3.52 KB
/
Copy pathParseXMLBitmapFont.js
File metadata and controls
135 lines (112 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Read an integer value from an XML Node.
*
* @function getValue
* @since 3.0.0
* @private
*
* @param {Node} node - The XML Node.
* @param {string} attribute - The attribute to read.
*
* @return {integer} The parsed value.
*/
function getValue (node, attribute)
{
return parseInt(node.getAttribute(attribute), 10);
}
/**
* Parse an XML font to Bitmap Font data for the Bitmap Font cache.
*
* @function ParseXMLBitmapFont
* @since 3.0.0
* @private
*
* @param {XMLDocument} xml - The XML Document to parse the font from.
* @param {integer} [xSpacing=0] - The x-axis spacing to add between each letter.
* @param {integer} [ySpacing=0] - The y-axis spacing to add to the line height.
* @param {Phaser.Textures.Frame} [frame] - The texture frame to take into account while parsing.
*
* @return {Phaser.GameObjects.BitmapText.Types.BitmapFontData} The parsed Bitmap Font data.
*/
var ParseXMLBitmapFont = function (xml, xSpacing, ySpacing, frame)
{
if (xSpacing === undefined) { xSpacing = 0; }
if (ySpacing === undefined) { ySpacing = 0; }
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;
data.chars = {};
var letters = xml.getElementsByTagName('char');
var adjustForTrim = (frame !== undefined && frame.trimmed);
if (adjustForTrim)
{
var top = frame.height;
var left = frame.width;
}
for (var i = 0; i < letters.length; i++)
{
var node = letters[i];
var charCode = getValue(node, 'id');
var gx = getValue(node, 'x');
var gy = getValue(node, 'y');
var gw = getValue(node, 'width');
var gh = getValue(node, 'height');
// Handle frame trim issues
if (adjustForTrim)
{
if (gx < left)
{
left = gx;
}
if (gy < top)
{
top = gy;
}
}
data.chars[charCode] =
{
x: gx,
y: gy,
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: {},
kerning: {}
};
}
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;
}
}
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');
data.chars[second].kerning[first] = amount;
}
return data;
};
module.exports = ParseXMLBitmapFont;