Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
fontforge script to generate subsets of fonts
#!/usr/bin/env fontforge -lang=py -script
# -*- coding: utf-8 -*-
import fontforge
import sys
import os
def _select_codepoint(codepoint, selection, more=True):
more_str = 'more' if more else 'less'
if isinstance(codepoint, tuple) and len(codepoint) == 2:
first, last = codepoint
if isinstance(first, int) and isinstance(last, int):, 'ranges', 'unicode'), first, last)
elif isinstance(first, unicode) and isinstance(last, unicode):, 'ranges', 'unicode'), ord(first), ord(last))
message = 'invalid codepoint type: ' + str(codepoint)
raise Exception(message)
elif isinstance(codepoint, int):, 'unicode'), codepoint)
elif isinstance(codepoint, unicode):
for c in codepoint:, 'unicode'), ord(c))
message = 'invalid codepoint type: ' + str(codepoint)
raise Exception(message)
def _clear_unneeded_glyphs(font, codepoints):
for codepoint in codepoints:
_select_codepoint(codepoint, font.selection, more=False)
def generate_webfonts(input, outdir, codepoints):
if not os.path.exists(outdir):
if not os.path.isdir(outdir):
message = 'last argument is not directory: ' + outdir
raise Exception(message)
font =
_clear_unneeded_glyphs(font, codepoints)
filename = os.path.split(input)[-1]
basename, ext = os.path.splitext(filename)
for newext in [ext, '.woff', '.eot']:
output = os.path.join(outdir, basename + newext)
if __name__ == '__main__':
codepoints = [(0x0000, 0x03FF), (0x2200, 0x22FF)]
if len(sys.argv) > 2:
for input in sys.argv[1:-1]:
generate_webfonts(input, sys.argv[-1], codepoints)
print('usage: ' + sys.argv[0] + ' inputs... outdir')

This comment has been minimized.

Copy link

saqib-ahmed commented Apr 29, 2020

Wow. Just what I was looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.