Last active
November 30, 2016 16:36
-
-
Save codeman38/b2aaf94fb18d688483e7bf93e436cfab to your computer and use it in GitHub Desktop.
Python script which uses fonttools to output a table grouping glyphs in a font by their advance width.
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
import argparse | |
import collections | |
from fontTools.ttLib import TTFont | |
__author__="Cody 'codeman38' Boisclair" | |
def main(): | |
parser = argparse.ArgumentParser( | |
description="Group glyphs in a font by their advance width.") | |
parser.add_argument('font', help='font file to parse') | |
args = parser.parse_args() | |
font = TTFont(args.font) | |
width_dict = collections.defaultdict(list) | |
metrics = font['hmtx'].metrics | |
for name, metric in metrics.items(): | |
width = metric[0] | |
width_dict[width].append(name) | |
sorted_widths = width_dict.items() | |
sorted_widths.sort(key=lambda x: (-len(x[1]), x[0])) | |
print('Width\tCount\tGlyphs') | |
for width, glyphs in sorted_widths: | |
print('{0}\t{1}\t{2}'.format( | |
width, len(glyphs), ' '.join(sorted(glyphs)))) | |
print('TOTAL\t{0}'.format(len(metrics))) | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment