BMFont XML Parser

1.1.6 · maintenance · verified Sun Apr 19

parse-bmfont-xml is a focused JavaScript library designed to parse XML-formatted BMFont (Bitmap Font) files generated by AngelCode BMFont. It takes a string or Buffer containing the XML data and converts it into a structured JavaScript object, adhering to the bmfont2json specification. The current stable version is 1.1.6, with the last update approximately seven years ago, indicating a mature but largely inactive maintenance status. Its primary differentiator is its singular focus on the BMFont XML format, providing a straightforward API for font data extraction, including character definitions, kerning pairs, and texture page references. It is suitable for both Node.js environments using `fs` and browser environments via XHR, though browser usage depends on internal DOM parsing APIs.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read an XML BMFont file using Node.js's `fs` module and parse its content into a JavaScript object, then access key properties like font face, pages, and character data.

const fs = require('fs');
const parse = require('parse-bmfont-xml');
const path = require('path');

// Create a dummy BMFont XML file for demonstration
const dummyXmlContent = `
<font>
    <info face="Arial" size="32" bold="0" italic="0" charset="" unicode="0" stretchH="100" smooth="1" aa="1" padding="0,0,0,0" spacing="1,1" outline="0"/>
    <common lineHeight="32" base="26" scaleW="256" scaleH="256" pages="1" packed="0" alphaChnl="0" redChnl="0" greenChnl="0" blueChnl="0"/>
    <pages>
        <page id="0" file="sheet0.png"/>
    </pages>
    <chars count="1">
        <char id="65" x="0" y="0" width="20" height="28" xoffset="0" yoffset="4" xadvance="20" page="0" chnl="15"/>
    </chars>
    <kernings count="0"/>
</font>
`;

const filePath = path.join(__dirname, 'dummy.fnt');
fs.writeFileSync(filePath, dummyXmlContent);

// Read and parse the BMFont file
fs.readFile(filePath, (err, data) => {
  if (err) {
    console.error('Error reading file:', err);
    return;
  }
  try {
    const result = parse(data);
    console.log('Font Face:', result.info.face);         // Expected: "Arial"
    console.log('Pages:', result.pages);                 // Expected: [ 'sheet0.png' ]
    console.log('Number of Chars:', result.chars.length); // Expected: 1
    console.log('First Char ID:', result.chars[0].id);   // Expected: 65 (for 'A')
  } catch (parseError) {
    console.error('Error parsing BMFont XML:', parseError);
  }
});

view raw JSON →