Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downsizing an .OTF font by removing glyphs

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.

like image 872
shennan Avatar asked Jan 28 '13 08:01

shennan


People also ask

How do I change glyphs of a font?

Ctrl -click in the Font Window and select the Edit Glyphs command to open selected glyphs. Select several glyphs in the Font Window and click on any of the editing tools in the Toolbar. While in the Glyph window, press T . This will switch the window to the Text mode allowing you to type any glyph or text.

How do I remove a glyph from Fontforge?

In the main menu hit "Edit -> Select -> Invert Selection" to have all the unused glyphs selected. now we can run in main menu "Encoding -> Detach and Remove glyphs" to remove all the selected (unused) glyphs.


1 Answers

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.

like image 138
SadaleNet Avatar answered Sep 17 '22 16:09

SadaleNet