|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +################################################### |
| 4 | +### THE VALUES BELOW CAN BE EDITED AS NEEDED ###### |
| 5 | +################################################### |
| 6 | + |
| 7 | +minKern = 3 # inclusive; this means that pairs which EQUAL this ABSOLUTE value will NOT be ignored/trimmed. Anything below WILL. |
| 8 | +writeTrimmed = False # If 'False', trimmed pairs will not be processed and, therefore, will not be written to the kerning feature file. |
| 9 | +writeSubtables = False # Sometimes the kerning feature file needs to have explicit subtable breaks, otherwise the OTF won't compile due to a subtable overflow. |
| 10 | + |
| 11 | + |
| 12 | +libraryNotFound = False |
| 13 | + |
| 14 | +import sys, os, time |
| 15 | +try: |
| 16 | + from defcon import Font |
| 17 | +except: |
| 18 | + print "ERROR: This script requires defcon. It can be downloaded from https://github.com/typesupply/defcon" |
| 19 | + libraryNotFound = True |
| 20 | +try: |
| 21 | + import WriteFeaturesKernFDK |
| 22 | +except: |
| 23 | + print "ERROR: This script requires WriteFeaturesKernFDK.py. It can be downloaded from https://github.com/adobe-type-tools/python-modules" |
| 24 | + libraryNotFound = True |
| 25 | + |
| 26 | +if libraryNotFound: |
| 27 | + sys.exit() |
| 28 | + |
| 29 | + |
| 30 | +def generateKernFile(font): |
| 31 | + |
| 32 | + folderPath, fontFileName = os.path.split(font) |
| 33 | + # path to the folder where the font is contained and the font's file name |
| 34 | + os.chdir(folderPath) |
| 35 | + |
| 36 | + ufoFont = Font(fontFileName) |
| 37 | + ufoBaseName = os.path.splitext(fontFileName)[0] |
| 38 | + kernFileName = 'kern_%s.fea' % ufoBaseName |
| 39 | + styleName = ufoFont.info.styleName |
| 40 | + |
| 41 | + print '*******************************' |
| 42 | + print 'Kerning for %s...' % (styleName) |
| 43 | + |
| 44 | + WriteFeaturesKernFDK.KernDataClass(ufoFont, folderPath, minKern, writeTrimmed, writeSubtables, kernFileName) |
| 45 | + |
| 46 | + |
| 47 | +def run(): |
| 48 | + # if a path is provided |
| 49 | + if len(sys.argv[1:]): |
| 50 | + baseFolderPath = sys.argv[1] |
| 51 | + |
| 52 | + if baseFolderPath[-1] == '/': # remove last slash if present |
| 53 | + baseFolderPath = baseFolderPath[:-1] |
| 54 | + |
| 55 | + # make sure the path is valid |
| 56 | + if not os.path.isdir(baseFolderPath): |
| 57 | + print 'Invalid directory.' |
| 58 | + return |
| 59 | + |
| 60 | + # if a path is not provided, use the current directory |
| 61 | + else: |
| 62 | + baseFolderPath = os.getcwd() |
| 63 | + |
| 64 | + t1 = time.time() |
| 65 | + |
| 66 | + fontPath = os.path.abspath(sys.argv[-1]) |
| 67 | + |
| 68 | + print fontPath |
| 69 | + fontsList = [] |
| 70 | + |
| 71 | + if os.path.exists(fontPath) and fontPath.lower().endswith('.ufo'): |
| 72 | + generateKernFile(fontPath) |
| 73 | + |
| 74 | + else: |
| 75 | + print "No fonts found" |
| 76 | + return |
| 77 | + |
| 78 | + t2 = time.time() |
| 79 | + elapsedSeconds = t2-t1 |
| 80 | + |
| 81 | + if (elapsedSeconds/60) < 1: |
| 82 | + print 'Completed in %.1f seconds.' % elapsedSeconds |
| 83 | + else: |
| 84 | + print 'Completed in %.1f minutes.' % (elapsedSeconds/60) |
| 85 | + |
| 86 | + |
| 87 | +if __name__=='__main__': |
| 88 | + run() |
0 commit comments