BMFont ASCII File Parser
The `parse-bmfont-ascii` library is designed to parse ASCII-formatted BMFont (Bitmap Font) files into a structured JavaScript object. It specifically handles the human-readable text-based `.fnt` files generated by tools like AngelCode BMFont. This package, currently at version 1.0.6, was last published 11 years ago, indicating it is no longer actively maintained. It accepts either a string or a Node.js `Buffer` containing the BMFont data and returns a JavaScript object detailing font metadata (`info`, `common`), individual character properties (`chars`), kerning pairs (`kernings`), and referenced page textures (`pages`). Its primary function is to provide a lightweight, dedicated parser for this specific text-based bitmap font format, distinct from XML or binary BMFont variants. Given its age, it relies on CommonJS module patterns and does not inherently support ESM or TypeScript type definitions.
Common errors
-
TypeError: parse is not a function
cause Attempting to use `parse` as a function after an incorrect `require` statement, or when a bundler fails to correctly resolve the CommonJS default export in an ESM context.fixEnsure you are using `const parse = require('parse-bmfont-ascii')` and that your build tools correctly handle CommonJS modules, especially if in an ESM project. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported
cause Attempting to `require` a pure ESM module from a CommonJS context, or more commonly, trying to `import` this (CommonJS) package directly in a pure ESM Node.js environment without proper configuration.fixThis package is CJS. In an ESM project, use dynamic import `import('parse-bmfont-ascii').then(mod => mod.default)`. Alternatively, ensure your bundler (e.g., Webpack, Rollup) is configured to handle CJS modules. -
Cannot find name 'parse'.
cause This TypeScript error occurs because `parse-bmfont-ascii` does not provide its own type declarations (`.d.ts` files).fixCreate a declaration file (e.g., `types/parse-bmfont-ascii.d.ts`) that defines the module's exports, or use `// @ts-ignore` above the import statement as a temporary workaround. A basic declaration could be `declare module 'parse-bmfont-ascii' { function parse(data: string | Buffer): any; export = parse; }`.
Warnings
- breaking This package is an old CommonJS module and does not natively support ES Modules (ESM). Attempting to `import` it in a pure ESM project will result in an error without a CJS-to-ESM wrapper or bundler configuration.
- deprecated The package `parse-bmfont-ascii` is abandoned, with its last release over a decade ago. It receives no active maintenance, meaning bug fixes, security updates, or new features are highly unlikely. Consider alternatives for new projects or if robust maintenance is critical.
- gotcha The README's example code incorrectly uses `require('parse-bmfont-xml')` when demonstrating the usage of `parse-bmfont-ascii`. Users should ensure they are importing `parse-bmfont-ascii` if they intend to parse ASCII BMFont files.
- gotcha This package does not ship with TypeScript declaration files (`.d.ts`). Using it in a TypeScript project will result in type errors unless custom declarations are provided or the import is ignored.
Install
-
npm install parse-bmfont-ascii -
yarn add parse-bmfont-ascii -
pnpm add parse-bmfont-ascii
Imports
- parse
import parse from 'parse-bmfont-ascii'
const parse = require('parse-bmfont-ascii')
Quickstart
const parse = require('parse-bmfont-ascii');
// Example BMFont ASCII data (simplified Arial.fnt structure)
const bmfontData = `info face="Arial" size=32 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=1,1,1,1 spacing=0,0 outline=0
common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=1 packed=0 alphaChnl=0 redChnl=0 greenChnl=0 blueChnl=0
page id=0 file="sheet0.png"
chars count=3
char id=65 x=1 y=1 width=20 height=20 xoffset=0 yoffset=4 xadvance=20 page=0 chnl=15
char id=66 x=22 y=1 width=20 height=20 xoffset=0 yoffset=4 xadvance=20 page=0 chnl=15
char id=67 x=43 y=1 width=20 height=20 xoffset=0 yoffset=4 xadvance=20 page=0 chnl=15
kernings count=1
kerning first=65 second=66 amount=-2`;
try {
const result = parse(bmfontData);
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: 3
console.log('First char ID:', result.chars[0].id); // Expected: 65 (A)
console.log('First kerning amount:', result.kernings[0].amount); // Expected: -2
} catch (error) {
console.error('Failed to parse BMFont data:', error);
}