BitmapFontParser

ceramic.BitmapFontParser (Class)

Parser for bitmap font definition files in BMFont format.

BitmapFontParser reads and parses font definition files (.fnt) that describe bitmap font layouts. It supports both plain text and XML formats commonly generated by tools like:

  • BMFont (AngelCode)
  • Hiero
  • msdf-bmfont-xml

The parser extracts:

  • Font metadata (face, size, line height)
  • Character definitions with texture coordinates
  • Texture page references
  • Kerning pairs
  • Distance field information (for MSDF fonts)
// Parse a font definition file
var fontData = BitmapFontParser.parse(fontFileContent);

// Create textures from the page files
var textures = new Map<String,Texture>();
for (page in fontData.pages) {
    textures.set(page.file, loadTexture(page.file));
}

// Create the font
var font = new BitmapFont(fontData, textures);
See: BitmapFontData The data structure produced by parsing, BitmapFont The font class that uses parsed data

Static Members

parse(rawFontData: String): BitmapFontData

Parses a bitmap font definition file into structured data.

Accepts both plain text (.fnt) and XML (.xml) formats. XML format is automatically detected and converted to text format before parsing.

Name Type Description
rawFontData String The raw content of the font definition file
Returns Description
BitmapFontData Parsed font data ready for use with BitmapFont

Private Members

convertXmlFontData(rawFontData: String): String

Converts XML format font data to plain text format.

XML format is commonly used by tools like Hiero and msdf-bmfont-xml. This method transforms the XML structure into the text-based format that the parser expects internally.

Name Type Description
rawFontData String XML font definition content
Returns Description
String Equivalent font data in plain text format

parseLine(line: String, info: BitmapFontData): Void

Parses a single line of font definition data.

Each line starts with a token identifying the data type:

  • info: Font face and size
  • common: Line height and base size
  • page: Texture page definition
  • chars: Character count
  • char: Individual character metrics
  • kerning: Kerning pair data
  • distanceField: MSDF configuration
Name Type Description
line String A single line from the font file
info BitmapFontData The font data structure to populate

extractLineTokens(line: String, map: Map): String

Extracts key-value pairs from a font definition line.

Parses lines in the format: token key1=value1 key2="quoted value" Handles both quoted and unquoted values, with proper escape sequence support.

Name Type Description
line String The line to parse
map Map Output map to populate with key-value pairs
Returns Description
String The first token on the line (e.g., "char", "info")

unquote(s: String): String

Removes surrounding quotes from a string if present.

Used to clean up quoted values from the font file. Only removes quotes if they exist at both start and end.

Name Type Description
s String The string to unquote
Returns Description
String The string without surrounding quotes