hostile-takeover/font/fontmap.py
Nathan Fulton e02296b6ac Add utility scripts for font processing
fontmap.py:
Maps glyph images to characters via the file name notation and then outputs a font json

fontutils.py:
Contains functions and variables that are used by both fontmap.py and glyphpos.py

glyphpos.py:
Since the game now draws glyphs directly from their image, a glyph’s placement within its image is very important. glyphpos.py is a helper script to mass (re)position the glyphs within their images.
2017-10-21 20:28:30 -04:00

61 lines
2.1 KiB
Python

import sys
import json
import fontutils
if __name__ == "__main__":
art_dir = sys.argv[1]
out_dir = sys.argv[2]
fonts = fontutils.get_fonts(art_dir)
errors = list()
for font in fonts:
glyph_map = {}
images = fontutils.images_in_dir(font.path)
for image in images:
# image_name that the character will be mapped to
# this is the name the game will use to load the glyph
image_name = "{}/{}".format(font.dir_name, image)
# Magically get the chracter name
char = fontutils.get_character(image, font)
if char == None:
errors.append(image)
continue
# Different characters can be mapped to the same glyph, but you
# can't have the same character mapped to different glyphs in the
# same font
if char in glyph_map:
print ("{}: trying to add \"{}\" with key \"{}\" but"
"\"{}\" already has been mapped to \"{}\""
.format(font.name,image_name, char, char, glyph_map[char]))
# Map it
glyph_map[char] = image_name
# It's important that the default character exists becuase it is drawn
# whenever a character can't be found in the map
if font.default_char not in glyph_map:
# If this is happening, make sure an image with the appropriate
# notation exists in the font's dir
print ("Unable to write {}.json: deafult char \"{}\""
"doesn't exist in map.".format(font.name, font.default_char))
continue
# Construct the object
j = {}
j['default'] = glyph_map[font.default_char]
j['height'] = fontutils.get_font_height(font, False)
j['glyph_overlap'] = font.glyph_overlap
j['line_overlap'] = font.line_overlap
j['glyph_map'] = glyph_map
with open("{}/{}.json".format(out_dir, font.name), 'w') as outfile:
json.dump(j, outfile, sort_keys = True)
if len(errors) > 0:
print "Unprocessed images:"
for fn in errors:
print fn