Skip to content

Instantly share code, notes, and snippets.

@dghost
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save dghost/20ee2f55deb89861230b to your computer and use it in GitHub Desktop.

Select an option

Save dghost/20ee2f55deb89861230b to your computer and use it in GitHub Desktop.
Load BMFont Text format files into a Python dictionary...
# loads a BMFont Text format glyph atlas into a dictionary
# see https://71squared.com/blog/bitmap-font-file-format for more info
def loadGlyphAtlas(filename):
atlas = {}
for line in open(filename):
attributes = line.split(" ")
attributes = [x for x in attributes if x != '' and x != '\n']
dictkey = attributes[0]
if dictkey in atlas:
attribdict = atlas[dictkey]
else:
attribdict = atlas[dictkey] = {}
if dictkey in ['char', 'page']:
c = int(attributes[1].split("=")[1])
entry = {}
for attrib in attributes[2:]:
key, value = attrib.split("=")
try:
entry[key] = float(value)
except:
entry[key] = value.strip('\"\n')
attribdict[c] = entry
else:
for attrib in attributes[1:]:
key, value = attrib.split("=")
try:
attribdict[key] = float(value)
except ValueError:
strval = value.strip('\"\n')
if ',' in strval:
arry = strval.split(',')
try:
arry = map(float,arry)
finally:
attribdict[key] = arry
else:
attribdict[key] = strval
return atlas
loadGlyphAtlas("Arial.fnt")
@dghost
Copy link
Author

dghost commented Feb 11, 2015

Currently doesn't handle kerning entries. Probably could be extended to support that fairly easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment