Skip to content

Instantly share code, notes, and snippets.

@simoncozens
Created July 10, 2020 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simoncozens/5210ac8e913beb8b6389d1a4c46e3ae1 to your computer and use it in GitHub Desktop.
Save simoncozens/5210ac8e913beb8b6389d1a4c46e3ae1 to your computer and use it in GitHub Desktop.
Survey CMAP tables of installed fonts
from fontTools.ttLib import TTFont
import glob
from tabulate import tabulate
import os
import random
paths = [
os.path.expanduser("~/Library/Fonts"),
"/Library/Fonts",
"/System/Library/Fonts/",
"/Library/Application Support/Apple/Fonts/Language Support"
]
countoftables = {}
formats = {}
platencs = {}
examples = {}
fonts = 0
for path in paths:
for fontfile in glob.glob(path+"/*.?tf"):
font = TTFont(fontfile)
if not "cmap" in font: continue
fonts = fonts + 1
c = font["cmap"].tables
countoftables[len(c)] = 1 + countoftables.get(len(c),0)
for table in font["cmap"].tables:
formats[table.format] = 1 + formats.get(table.format,0)
platEnc = "platformID=%2i encodingID=%2i" % (table.platformID, table.platEncID)
platencs[platEnc] = 1 + platencs.get(platEnc,0)
if not platEnc in examples or random.random() > 0.6:
examples[platEnc] = os.path.basename(fontfile)
total = sum(countoftables.values())
keys = sorted(countoftables.keys())
print("Surveyed %i fonts" % fonts)
values = [ "%6.2f%%" % (countoftables[k]/total*100) for k in keys]
print(tabulate([ ["Number of cmap tables"] + keys, ["% of fonts"] + values]))
print()
total = sum(formats.values())
keys = sorted(formats.keys())
values = [ "%6.2f%%" % (formats[k]/total*100) for k in keys]
print(tabulate([ ["cmap table format"] + keys, ["% of tables"] + values]))
print()
table = []
for p in sorted(platencs.keys()):
table.append([p, int(platencs[p]/total*10000)/100, examples[p]])
print(tabulate(table))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment