################################################################################ # Program: fontmerger.py # # Description: This program merges all the font files in current directory # and merged font file is saved as mergedfont.ttf. Note that # only files with .ttf extention are considered for now. # # Author: Mohsin Shafeeque Hijazee (m_hijazee@yahoo.com) # This program written for Grand Nastaleeq Project at # UrduWeb.Org (http://www.urduweb.org). # The aim of the project to create an everlasting Nastaleeq # font for Urdu utilizing ligature and character based # paradimgs of the Nastaleeq font development. # You can join the development fourms # at http://www.urduweb.org/mehfil # # RevisionLog: 22 June, 2008: (Mohsin) # * Initial testing of idea # # 24 June, 2008: (Mohsin) # * Moved merging of two fonts to mergeFont() # * mergeFont() improved to take either TTFont object, a # File object or a string object containing path as args. # This is done by addding function getFontObject() # * Added fucntion mergeAllFonts() # * Program can be used as reuseable module # # TODO: User should be able to specify output directory and file name # TODO: User should be able to mention the source directory for fonts to be merged. import types import sys import glob try: from fontTools.ttLib import TTFont except ImportError: print 'fontmerger requires fontTools to be installed. Google fontTools for more information.' exit() def getFontObject(file_or_path): """ Given a file object or path, returns TTFont object""" # If its a file object or a path string, return font object # Otherwise chances are that its already a TTFont object if type(file_or_path) == types.StringType or type(file_or_path) == types.FileType: return TTFont(file_or_path) else: return file_or_path def mergeFonts(font1, font2): """ Merges second font into first font. Both arguments can be either * File objects * String mentioning absolute/relative path * TTFont objects. Returns merged TTFont object. """ # Get the font objects firstFont = getFontObject(font1) secondFont = getFontObject(font2) # Get the 'glyf' table of each first_glyf = firstFont['glyf'] second_glyf = secondFont['glyf'] # Get 'hmtx' tables of each to merge horizontal metrics first_hmtx = firstFont['hmtx'] second_hmtx = secondFont['hmtx'] # For each of the glyphs in 'glyf' table of second font for glyphName in second_glyf.keys(): # If first font already has this glyph if first_glyf.glyphs.has_key(glyphName): print "WARNING: font already contains glyph named '%s'" % glyphName continue # 1... Merge glph in 'glyf' table # 2... Record horizontal metrices in 'hmtx' # 3... Record order in 'loca' table first_glyf.glyphs[glyphName] = second_glyf[glyphName] first_hmtx.metrics[glyphName] = second_hmtx.metrics[glyphName] firstFont.glyphOrder.append(glyphName) return firstFont def mergeAllFonts(): """Merges all the fonts in current directory to single font mergedfont.ttf""" # Get all the .ttf files in current director fontFiles = glob.glob('*.ttf') # Pick the first file firstFile = fontFiles[0] # All fonts will be merged into this mergedFont = TTFont(firstFile) for index in range(1, len(fontFiles)): print "Merging '%s' with '%s'" % (fontFiles[index], firstFile) mergeFonts(mergedFont, fontFiles[index]) mergedFont.save('mergedfont.ttf') print "Merged font is saved as 'mergedfont.ttf'" # If not being used as module, merge all fonts in current directory if __name__ == '__main__': mergeAllFonts()