Skip to content

Instantly share code, notes, and snippets.

@eliheuer
Last active December 30, 2022 06:46
Show Gist options
  • Save eliheuer/728ed62fc550b9a106e6ebcc6cb9470e to your computer and use it in GitHub Desktop.
Save eliheuer/728ed62fc550b9a106e6ebcc6cb9470e to your computer and use it in GitHub Desktop.
This script iterates through the full Google Fonts catalog and checks each font to see how many codepoints from the GF_Latin_African.txt glyphset are included. Can be modified to work with other glyphsets as needed.
# To use this script, place it in a directory with a clone of the
# Google Fonts "fonts" directory and a text file from GF-Glyphssets.
# .
# ├── GF_Latin_African.txt
# ├── fonts
# └── gf-glyphset-check.py
#
# See links below:
#
# https://github.com/google/fonts
# https://github.com/googlefonts/glyphsets/blob/main/GF_glyphsets/Latin/txt/prod-names/GF_Latin_African.txt
#
# This script is hardcoded for the GF_Latin_African.txt glyphset
# but can be modified with a few changes to use any other glyphset.
#
# Run the script from the directory containing the three requiered items:
# $ python3 gf-glyphset-check.py
import glob, os
from fontTools.ttLib import TTFont
# Makes a list from a GF glyphset
# Used to check what codepoints are in a font
with open('GF_Latin_African.txt') as file_in:
gf_latin_african = [x.rstrip() for x in file_in] # remove line breaks
# Finds all .ttf files in the Google Fonts "fonts" directory
# Adds them to a list called font_list
font_list = []
for file in glob.glob("fonts/**/*.ttf", recursive=True):
font_list.append(file)
# Gets a list of unicode codepoints from a font
def get_codepoints_from_font(font_path):
# Open the font using fontTools
font_to_test = TTFont(font_path)
# Initialize an empty list to store the code points
codepoints = []
# Get the Unicode mapping for the font
cmap = font_to_test["cmap"].getBestCmap()
# Iterate through the mapping
for codepoint, glyph_name in cmap.items():
# Format the code point as a string in the desired format
codepoint_str = "uni{:04X}".format(codepoint)
# Add the code point to the list
codepoints.append(codepoint_str)
# Return the list of code points
return codepoints
def compare_codepoints(gf_glyphset, font_codepoints):
# Make a blank list of matching codepoints
matches = []
# Find matching codepoints
for codepoint in gf_glyphset:
if codepoint in font_codepoints:
matches.append(codepoint)
# Return the list of matching codepoints
return matches
# Iterate through all the Google Fonts and print results to standard out
for font in font_list:
print("\nChecking: ", font)
codepoints = get_codepoints_from_font(font)
codepoint_matches = compare_codepoints(gf_latin_african, codepoints)
print("The number of codepoints from GF_Latin_African in this font:", len(codepoint_matches))
print("Codepoints from GF_Latin_African in this font...")
print(codepoint_matches)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment