Downsizing an .OTF font by removing glyphs

shennan picture shennan · Jan 28, 2013 · Viewed 14.7k times · Source

I can't quite believe this question hasn't been asked specifically for OpenType fonts, but does anyone know of a way to remove glyphs from these fonts?

I have an .OTF with a very large file-size (almost 10MB) and I need to make it smaller. The reasons are two fold.

1) I'm trying to prepare it for web embedding, so the smaller the files, the easier for the client.

2) Font Squirrel (used for easy preparation of font files) has a 2MB upload limit - I know there are alternatives, but none so far have been successful. To save wasting peoples time, the ones I've tried that have failed are http://fontface.codeandmore.com/ and http://www.font2web.com/. CodeAndMore.com appears to work, but the fonts it spits back out are completely different to the one I gave it.

Please be aware I'm not a font expert, so go easy on the answer.

Answer

SadaleNet picture SadaleNet · Dec 7, 2015

I've written a Python2 script with fontforge library does the following:

  • Accepts a source font
  • Accepts a file containing all characters to be used. It can be a translation file, string asset file, HTML file, etc.
  • Output a font with characters that aren't shown in the file removed

Here is the code:

#!/usr/bin/python2
import sys
import fontforge

if len(sys.argv) == 4:
    font = fontforge.open(sys.argv[1])

    with open(sys.argv[2], "r") as f:
        for i in f.read().decode("UTF-8"):
            font.selection[ord(i)] = True

    font.selection.invert()

    for i in font.selection.byGlyphs:
        font.removeGlyph(i)

    font.generate(sys.argv[3])
else:
    print "WARNING: Check the license of the source font\nbefore distributing the output font generated by this script.\nI'm not responsible for any legal issue caused by\ninappropriate use of this script!\n"
    print "Usage: {} [source font] [file with glyphs NOT to be removed] [output]".format(sys.argv[0])
    print "Example: {} /path/to/ukai.ttc chineseTranslation.txt ukaiStripped.ttf".format(sys.argv[0])

Please notice that it may not be legal to use this script on certain fonts. Ensure to checkout the license of the source font. I'm not responsible for any legal issue caused by using any font generated by this script.