Created
July 15, 2020 12:47
-
-
Save korniux/b17971e294eafc97892192ca5c847ee8 to your computer and use it in GitHub Desktop.
Extracts TTF to individual glyphs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import os | |
import subprocess | |
from itertools import chain | |
from fontTools.ttLib import TTFont | |
from fontTools.unicode import Unicode | |
def usage(): | |
print("{0} usage: ".format(sys.argv[0])) | |
print("{0} <ttf> [output_folder_name]".format(sys.argv[0])) | |
def main(): | |
argc = len(sys.argv) | |
size = 16 | |
if argc < 2 or argc > 3: | |
usage() | |
sys.exit(1) | |
ttf_file = sys.argv[1] | |
if argc == 3: | |
out_folder = sys.argv[2] | |
else: | |
out_folder = os.path.splitext(ttf_file)[0].split('/')[-1] | |
if not os.path.exists(out_folder): | |
os.mkdir(out_folder) | |
if not os.path.isfile(ttf_file): | |
print("{0} does not exist".format(ttf_file)) | |
usage() | |
sys.exit(1) | |
ttf = TTFont(ttf_file, allowVID=0, ignoreDecompileErrors=True, fontNumber=-1) | |
chars = chain.from_iterable([y + (Unicode[y[0]],) for y in x.cmap.items()] for x in ttf["cmap"].tables) | |
for c in list(chars): | |
out_png = "{0}/{1}.png".format(out_folder, c[2]) | |
char = chr(c[0]) | |
if c[1] not in ['NUL', 'STX', 'HT', 'LF', 'space', 'nonmarkingreturn']: | |
args = ["convert", "-font", ttf_file, "-pointsize", str(size), "-background", "black", "-fill", "white", '''label:{0}'''.format(char), out_png] | |
subprocess.call(args) | |
ttf.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment