Skip to content

Instantly share code, notes, and snippets.

@codeman38
Last active November 30, 2016 16:36
Show Gist options
  • Save codeman38/b2aaf94fb18d688483e7bf93e436cfab to your computer and use it in GitHub Desktop.
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.
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