-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
63 lines (55 loc) · 1.62 KB
/
index.js
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
var TXT = 'txt',
XML = 'xml';
var parseAscii = require('parse-bmfont-ascii');
var parseXML = require('parse-bmfont-xml');
/**
* Parses a string (or Node Buffer) that is either XML data (with a root <font> element),
* or TXT data (following Bitmap Font spec: http://www.angelcode.com/products/bmfont/doc/file_format.html).
*
* The output looks something like the following JSON. It tries to stay true to the BMFont spec.
*
* ```json
* {
* pages: [
* "sheet_0.png",
* "sheet_1.png"
* ],
* chars: [
* { chnl, height, id, page, width, x, y, xoffset, yoffset, xadvance },
* ...
* ],
* info: { ... },
* common: { ... },
* kernings: [
* { first, second, amount }
* ]
* }
* ```
*
* If no format is provided, it will be guessed based on the starting characters of
* the data.
*
* @param {String|Buffer} data the input data of the TXT/XML file
* @param {String} format an explicit format, either 'xml' or 'txt', otherwise guesses format
*/
function parse(data, format) {
if (!data)
throw "no data provided";
data = data.toString().trim();
if (!format) {
if (data.substring(0,4) === 'info')
format = TXT;
else if (data.charAt(0) === '<')
format = XML;
else
throw "malformed XML/TXT bitmap font file";
}
if (format !== XML && format !== TXT)
throw "only xml and txt formats are currently supported";
if (format === TXT) {
return parseAscii(data);
} else {
return parseXML(data);
}
}
module.exports = parse;