What is parse-bmfont-xml?
The parse-bmfont-xml npm package is used to parse BMFont XML files, which are commonly used in bitmap font rendering. This package helps in extracting font data such as character information, kerning, and other font metrics from XML files.
What are parse-bmfont-xml's main functionalities?
Parse BMFont XML
This feature allows you to parse a BMFont XML file and extract font data. The code sample reads an XML file and uses the parse function to extract and log the font information.
const parse = require('parse-bmfont-xml');
const fs = require('fs');
fs.readFile('path/to/font.xml', 'utf8', (err, data) => {
if (err) throw err;
parse(data, (err, font) => {
if (err) throw err;
console.log(font);
});
});
Other packages similar to parse-bmfont-xml
parse-bmfont-ascii
The parse-bmfont-ascii package is used to parse BMFont ASCII files. While parse-bmfont-xml focuses on XML format, parse-bmfont-ascii handles the ASCII format, providing similar functionality for different file types.
fontkit
Fontkit is a comprehensive font library that supports various font formats including TrueType, OpenType, WOFF, and more. It offers more extensive functionality compared to parse-bmfont-xml, which is specialized for BMFont XML files.
opentype.js
opentype.js is a JavaScript parser and writer for OpenType and TrueType fonts. It provides a broader range of font manipulation capabilities compared to parse-bmfont-xml, which is specifically for parsing BMFont XML files.
parse-bmfont-xml
Parses XML BMFont files.
Takes a string or Buffer:
var fs = require('fs')
var parse = require('parse-bmfont-xml')
fs.readFileSync(__dirname+'/Arial.fnt', function(err, data) {
var result = parse(data)
console.log(result.info.face)
console.log(result.pages)
console.log(result.chars)
console.log(result.kernings)
})
Also works in the browser, for example using XHR:
var parse = require('parse-bmfont-xml')
var xhr = require('xhr')
xhr({ uri: 'fonts/NexaLight32.xml' }, function(err, res, body) {
if (err)
throw err
var result = parse(body)
console.log(result.info.face)
})
The spec for the returned JSON object is here. The input XML should match the spec with a <font>
root element, see [test/Nexa Light-32.fnt](test/Nexa Light-32.fnt) for an example.
Related modules:
Usage
result = parse(data)
Parses data
, a string or Buffer that represents XML data of an AngelCode BMFont file. The returned result
object looks like this:
{
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 the data is malformed, an error will be thrown.
The browser implementation relies on xml-parse-from-string, which may not work in environments without valid DOM APIs (like CocoonJS).
License
MIT, see LICENSE.md for details.