forked from adobe-type-tools/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateAllKernFiles.py
More file actions
executable file
·126 lines (97 loc) · 3.34 KB
/
generateAllKernFiles.py
File metadata and controls
executable file
·126 lines (97 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python
import os
import sys
import time
############################################
# THE VALUES BELOW CAN BE EDITED AS NEEDED #
############################################
minKern = 3
# This value is inclusive; this means that pairs which EQUAL this ABSOLUTE
# value will NOT be ignored/trimmed. Anything kerning pair that falls below
# will be ignored.
writeTrimmed = False
# If 'False', trimmed pairs will not be processed and therefore will not be
# written to the kerning feature file.
# If 'True', trimmed pairs will be written, but commented out.
writeSubtables = True
# Sometimes the kerning feature file needs to have explicit subtable breaks,
# otherwise the OTF won't compile due to a subtable overflow.
############################################
__doc__ = """
This script takes a path to a folder as input, finds all
the UFOs inside that folder and its subdirectories, and outputs each
font's kerning in feature file syntax. If a path is not provided, the
script uses the current path as the top-most directory. The name of
the resulting kerning FEA file is managed by the WriteFeaturesKernFDK
module.
"""
# ----------------------------------------------
libraryNotFound = False
try:
from defcon import Font
except:
print "ERROR: This script requires defcon. It can be downloaded from https://github.com/typesupply/defcon"
libraryNotFound = True
try:
import kernFeatureWriter
except:
print "ERROR: This script requires kernFeatureWriter.py. It can be downloaded from https://github.com/adobe-type-tools/python-modules"
libraryNotFound = True
if libraryNotFound:
sys.exit()
fontsList = []
def getFontPaths(path, startpath):
files = os.listdir(path)
for file in files:
if file[-4:].lower() in [".ufo"]:
fontsList.append(os.path.join(path, file))
else:
if os.path.isdir(os.path.join(path, file)):
getFontPaths(os.path.join(path, file), startpath)
def doTask(fonts, startpath):
totalFonts = len(fonts)
print "%d fonts found\n" % totalFonts
i = 0
for font in fonts:
i += 1
folderPath, fontFileName = os.path.split(font)
# path to the folder where the font is contained and the font's file name
styleName = os.path.basename(folderPath)
folderPath = os.path.abspath(folderPath)
# name of the folder where the font is contained
# Change current directory to the folder where the font is contained
os.chdir(folderPath)
exportMessage = 'Exporting kern files for %s...(%d/%d)' % (
styleName, i, totalFonts)
print '*' * len(exportMessage)
print exportMessage
ufoFont = Font(fontFileName)
kernFeatureWriter.run(ufoFont, folderPath, minKern, writeSubtables)
os.chdir(startpath)
def run():
# if a path is provided
if len(sys.argv[1:]):
baseFolderPath = os.path.normpath(sys.argv[1])
# make sure the path is valid
if not os.path.isdir(baseFolderPath):
print 'Invalid directory.'
return
# if a path is not provided, use the current directory
else:
baseFolderPath = os.getcwd()
t1 = time.time()
getFontPaths(baseFolderPath, baseFolderPath)
startpath = os.path.abspath(os.path.curdir)
if len(fontsList):
doTask(fontsList, startpath)
else:
print "No fonts found"
return
t2 = time.time()
elapsedSeconds = t2-t1
if (elapsedSeconds/60) < 1:
print 'Completed in %.1f seconds.' % elapsedSeconds
else:
print 'Completed in %.1f minutes.' % (elapsedSeconds/60)
if __name__=='__main__':
run()