Skip to content

Instantly share code, notes, and snippets.

@rjmoggach
Created May 8, 2013 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rjmoggach/5540736 to your computer and use it in GitHub Desktop.
Save rjmoggach/5540736 to your computer and use it in GitHub Desktop.
Python script to change the font family of individual font files. It's useful if you have multiple fonts that should be the same family but are not grouped together in GUI software.
#!/usr/bin/python
import os, sys, string, re, MacOS
from string import capwords
import fontforge as ff
from optparse import OptionParser
SKIP_FILES = ['.DS_Store', '.AppleDB', 'convert_font.log', 'Icon?']
class DirectoryWalker:
# a forward iterator that traverses a directory tree
def __init__(self, directory):
self.stack = [directory]
self.files = []
self.index = 0
def __getitem__(self, index):
while 1:
try:
file = self.files[self.index]
self.index = self.index + 1
except IndexError:
# pop next directory from stack
self.directory = self.stack.pop()
self.files = os.listdir(self.directory)
self.index = 0
else:
# got a filename
fullname = os.path.join(self.directory, file)
if os.path.isdir(fullname) and not os.path.islink(fullname):
self.stack.append(fullname)
return fullname
SKIPPED_FILES = []
def main():
DEFAULT_FONT_DIR = os.path.join(os.path.expanduser('~'), 'FontsConverted')
parser=OptionParser()
usage = "\n %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-f", dest="FAMILY", default=None, help="set the family name to change to")
parser.add_option("-t", dest="TEST", action='store_true', default=False, help="test the family conversion without changing any files")
(options, args) = parser.parse_args()
TEST = options.TEST
FAMILY = options.FAMILY.strip()
if not '-f' in sys.argv or not FAMILY:
print "\nYou must specify a family name to change to for this utility to do it's magic.\n"
parser.print_help()
if TEST:
print '\n------ TESTING ONLY --------\n'
for file in DirectoryWalker('.'):
file = file.decode("utf-8")
head,tail=os.path.split(file)
if not tail in SKIP_FILES:
if os.path.isfile(file):
try:
macfiletype = MacOS.GetCreatorAndType(file)
print 'DEBUG MAC CREATOR:', macfiletype
except: pass
if list(macfiletype)[0] in ['2T2L', '2t2l','VOMD', 'vomd', 'RVOM', 'rvom','\xbfSCC'] and list(macfiletype)[1] in ['LIFF','liff']: continue
try:
print '\nOPEN: %s\n--------------------------------------------------------------------------' % file
f=ff.open(file)
font_family = f.familyname
if f.is_cid: font_family = f.cidfamilyname
f.close()
print " TEST: Family '%s' will become '%s'." % (font_family, FAMILY)
except EnvironmentError:
print "Error:", file
print '\n------ TESTING COMPLETE --------\n'
else:
print '\n------ STARTING CONVERSION --------\n'
for file in DirectoryWalker('.'):
file = file.decode("utf-8")
head,tail=os.path.split(file)
if not tail in SKIP_FILES:
if os.path.isfile(file):
try:
macfiletype = MacOS.GetCreatorAndType(file)
print 'DEBUG MAC CREATOR:', macfiletype
except: pass
# skip suitcases that crash fontforge
if list(macfiletype)[0] in ['2T2L', '2t2l','VOMD', 'vomd', 'RVOM', 'rvom','\xbfSCC'] and list(macfiletype)[1] in ['LIFF','liff']: continue
try:
print '\nOPEN: %s\n--------------------------------------------------------------------------' % file
f=ff.open(file)
if f.is_cid:
font_family = f.cidfamilyname
f.cidfamilyname = FAMILY
f.familyname = FAMILY
else:
font_family = f.familyname
f.familyname = FAMILY
f.generate(file)
f.close()
print>>sys.stdout, '\tFAMILY: "%s" --> %s' % (font_family, FAMILY)
except EnvironmentError, UnicodeDecodeError:
SKIPPED_FILES.append(file)
else:
print "Skipping Directory: ", file
else:
pass
print '\n------ FINISHED CONVERSION --------\n'
for entry in SKIPPED_FILES:
print>>sys.stdout, "SKIPPED:", entry
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment