Skip to content

Instantly share code, notes, and snippets.

@FlyTechVideos
Created March 25, 2026 23:39
Show Gist options
  • Select an option

  • Save FlyTechVideos/5afdc6e8023893d0ecfc913a4a999717 to your computer and use it in GitHub Desktop.

Select an option

Save FlyTechVideos/5afdc6e8023893d0ecfc913a4a999717 to your computer and use it in GitHub Desktop.
Script which modifies all fonts inside a given directory according to your desires. I can't think of any reasonable use cases for this.
#!/usr/bin/env python3
# The only requirement: fontTools (create venv, then pip install fontTools)
import os
from fontTools.ttLib import TTFont
from fontTools.pens.ttGlyphPen import TTGlyphPen
SOURCE_DIR = './fonts'
TARGET_DIR = './fonts_modified'
# Uncomment the mode you wish to use
MODE = 'NONE'
#MODE = 'ROT'
#MODE = 'BLANK'
#MODE = 'BLACK'
#MODE = 'SINGLE_CHAR'
ROTATIONS = 1 # only useful if mode is ROT
SINGLE_CHAR = '*'
def rotate_char(c):
digit_map = {
'0': '2', '1': '9', '2': '3', '3': '8', '4': '4',
'5': '7', '6': '5', '7': '6', '8': '1', '9': '0'
}
if 'A' <= c <= 'Z':
return chr((ord(c) - ord('A') + ROTATIONS) % 26 + ord('A'))
if 'a' <= c <= 'z':
return chr((ord(c) - ord('a') + ROTATIONS) % 26 + ord('a'))
if c in digit_map:
return digit_map[c]
return c
def rotate_all(input_dir, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for filename in os.listdir(input_dir):
if filename.endswith((".ttf")):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
try:
font = TTFont(input_path)
if MODE == 'BLACK' or MODE == 'BLANK':
for table in ['gvar', 'fvar', 'STAT', 'HVAR', 'VVAR', 'kern', 'GPOS']:
# we have to remove these tables otherwise some fonts will not accept the new drawing
if table in font:
del font[table]
glyph_set = font.getGlyphSet()
glyf_table = font['glyf']
hmtx_table = font['hmtx']
box_width = 1000
box_height = 1200
for glyph_name in font.getGlyphOrder():
pen = TTGlyphPen(glyph_set)
if MODE == 'BLACK':
pen.moveTo((0, -200))
pen.lineTo((box_width, -200))
pen.lineTo((box_width, box_height))
pen.lineTo((0, box_height))
pen.closePath()
glyf_table[glyph_name] = pen.glyph()
hmtx_table[glyph_name] = (box_width, 0)
elif MODE == 'ROT' or MODE == 'SINGLE_CHAR':
for table in font['cmap'].tables:
if table.platformID == 3: # Windows
new_cmap = {}
SPACE = 0x20
for code, glyph_name in table.cmap.items():
try:
target_char = rotate_char(chr(code)) if MODE == 'ROT' or code == SPACE else SINGLE_CHAR
target_code = ord(target_char)
if target_code in table.cmap:
new_cmap[code] = table.cmap[target_code]
else:
new_cmap[code] = glyph_name
except ValueError:
new_cmap[code] = glyph_name
table.cmap = new_cmap
else:
raise f'Invalid MODE selected ({mode}). Please set a MODE at the top of the file.'
font.save(output_path)
print(f"Processed: {filename}")
except Exception as e:
print(f"Failed to process {filename}: {e}")
rotate_all(SOURCE_DIR, TARGET_DIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment