forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoaderParser.js
More file actions
147 lines (128 loc) · 5.69 KB
/
Copy pathLoaderParser.js
File metadata and controls
147 lines (128 loc) · 5.69 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
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2015 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Phaser.LoaderParser parses data objects from Phaser.Loader that need more preparation before they can be inserted into the Cache.
*
* @class Phaser.LoaderParser
*/
Phaser.LoaderParser = {
/**
* Alias for xmlBitmapFont, for backwards compatiblity.
*
* @method Phaser.LoaderParser.bitmapFont
* @param {Phaser.Game} game - A reference to the current game.
* @param {object} xml - XML data you want to parse.
* @param {string} cacheKey - The key of the texture this font uses in the cache.
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
*/
bitmapFont: function (game, xml, cacheKey, xSpacing, ySpacing) {
this.xmlBitmapFont(game, xml, cacheKey, xSpacing, ySpacing);
},
/**
* Parse a Bitmap Font from an XML file.
*
* @method Phaser.LoaderParser.xmlBitmapFont
* @param {Phaser.Game} game - A reference to the current game.
* @param {object} xml - XML data you want to parse.
* @param {string} cacheKey - The key of the texture this font uses in the cache.
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
*/
xmlBitmapFont: function (game, xml, cacheKey, xSpacing, ySpacing) {
var data = {};
var info = xml.getElementsByTagName('info')[0];
var common = xml.getElementsByTagName('common')[0];
data.font = info.getAttribute('face');
data.size = parseInt(info.getAttribute('size'), 10);
data.lineHeight = parseInt(common.getAttribute('lineHeight'), 10) + ySpacing;
data.chars = {};
var letters = xml.getElementsByTagName('char');
for (var i = 0; i < letters.length; i++)
{
var charCode = parseInt(letters[i].getAttribute('id'), 10);
data.chars[charCode] = {
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),
xOffset: parseInt(letters[i].getAttribute('xoffset'), 10),
yOffset: parseInt(letters[i].getAttribute('yoffset'), 10),
xAdvance: parseInt(letters[i].getAttribute('xadvance'), 10) + xSpacing,
kerning: {}
};
}
var kernings = xml.getElementsByTagName('kerning');
for (i = 0; i < kernings.length; i++)
{
var first = parseInt(kernings[i].getAttribute('first'), 10);
var second = parseInt(kernings[i].getAttribute('second'), 10);
var amount = parseInt(kernings[i].getAttribute('amount'), 10);
data.chars[second].kerning[first] = amount;
}
this.finalizeBitmapFont(cacheKey, data);
},
/**
* Parse a Bitmap Font from a JSON file.
*
* @method Phaser.LoaderParser.jsonBitmapFont
* @param {Phaser.Game} game - A reference to the current game.
* @param {object} json - JSON data you want to parse.
* @param {string} cacheKey - The key of the texture this font uses in the cache.
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
*/
jsonBitmapFont: function (game, json, cacheKey, xSpacing, ySpacing) {
var data = {
font: json.font.info._font,
size: parseInt(json.font.info._size, 10),
lineHeight: parseInt(json.font.common._lineHeight, 10) + ySpacing,
chars: {}
};
json.font.chars["char"].forEach(
function parseChar(letter) {
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: {}
};
}
);
json.font.kernings.kerning.forEach(
function parseKerning(kerning) {
data.chars[kerning._second].kerning[kerning._first] = parseInt(kerning._amount, 10);
}
);
this.finalizeBitmapFont(cacheKey, data);
},
/**
* Finalize Bitmap Font parsing.
*
* @method Phaser.LoaderParser.finalizeBitmapFont
* @private
* @param {string} cacheKey - The key of the texture this font uses in the cache.
* @param {object} bitmapFontData - Pre-parsed bitmap font data.
*/
finalizeBitmapFont: function (cacheKey, bitmapFontData) {
Object.keys(bitmapFontData.chars).forEach(
function addTexture(charCode) {
var letter = bitmapFontData.chars[charCode];
var textureRect = new PIXI.Rectangle(
letter.x, letter.y,
letter.width, letter.height
);
letter.texture = new PIXI.Texture(PIXI.BaseTextureCache[cacheKey], textureRect);
}
);
PIXI.BitmapText.fonts[cacheKey] = bitmapFontData;
}
};