Skip to content

Commit 6f02955

Browse files
committed
Fixes and improvements to generateAll___Files.py
1 parent f151c0e commit 6f02955

2 files changed

Lines changed: 94 additions & 142 deletions

File tree

generateAllKernFiles.py

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,6 @@
1313
import sys
1414
import time
1515

16-
############################################
17-
# THE VALUES BELOW CAN BE EDITED AS NEEDED #
18-
############################################
19-
20-
minKern = 3
21-
# This value is inclusive; this means that pairs which EQUAL this ABSOLUTE
22-
# value will NOT be ignored/trimmed. Anything kerning pair that falls below
23-
# will be ignored.
24-
writeTrimmed = False
25-
# If 'False', trimmed pairs will not be processed and therefore will not be
26-
# written to the kerning feature file.
27-
# If 'True', trimmed pairs will be written, but commented out.
28-
writeSubtables = True
29-
# Sometimes the kerning feature file needs to have explicit subtable breaks,
30-
# otherwise the OTF won't compile due to a subtable overflow.
31-
32-
33-
############################################
34-
3516
libraryNotFound = False
3617

3718
try:
@@ -47,37 +28,24 @@
4728
"downloaded from https://github.com/adobe-type-tools/python-modules")
4829
libraryNotFound = True
4930

50-
if libraryNotFound:
51-
sys.exit()
52-
53-
fontsList = []
54-
5531

56-
def getFontPaths(path, startpath):
57-
files = os.listdir(path)
58-
for file in files:
59-
if file[-4:].lower() in [".ufo"]:
60-
fontsList.append(os.path.join(path, file))
61-
else:
62-
if os.path.isdir(os.path.join(path, file)):
63-
getFontPaths(os.path.join(path, file), startpath)
32+
def getFontPaths(startpath):
33+
font_paths = []
34+
for dir_path, _, _ in os.walk(startpath):
35+
if dir_path.lower().endswith('.ufo'):
36+
font_paths.append(dir_path)
37+
return sorted(font_paths)
6438

6539

6640
def doTask(fonts, startpath):
6741
totalFonts = len(fonts)
6842
print("%d fonts found\n" % totalFonts)
69-
i = 0
7043

71-
for font in fonts:
72-
i += 1
44+
for i, font in enumerate(fonts, 1):
7345
folderPath, fontFileName = os.path.split(font)
74-
# path to the folder where the font is contained
75-
# and the font's file name
7646
styleName = os.path.basename(folderPath)
7747
folderPath = os.path.abspath(folderPath)
78-
# name of the folder where the font is contained
7948

80-
# Change current directory to the folder where the font is contained
8149
os.chdir(folderPath)
8250

8351
exportMessage = 'Exporting kern files for %s...(%d/%d)' % (
@@ -86,7 +54,7 @@ def doTask(fonts, startpath):
8654
print(exportMessage)
8755

8856
ufoFont = Font(fontFileName)
89-
kernFeatureWriter.run(ufoFont, folderPath, minKern, writeSubtables)
57+
kernFeatureWriter.run(ufoFont, folderPath, writeSubtables=True)
9058

9159
os.chdir(startpath)
9260

@@ -99,20 +67,20 @@ def run():
9967
# make sure the path is valid
10068
if not os.path.isdir(baseFolderPath):
10169
print('Invalid directory.')
102-
return
70+
return 1
10371

10472
# if a path is not provided, use the current directory
10573
else:
10674
baseFolderPath = os.getcwd()
10775

10876
t1 = time.time()
109-
getFontPaths(baseFolderPath, baseFolderPath)
77+
fontsList = getFontPaths(baseFolderPath)
11078
startpath = os.path.abspath(os.path.curdir)
11179
if len(fontsList):
11280
doTask(fontsList, startpath)
11381
else:
11482
print("No fonts found")
115-
return
83+
return 1
11684

11785
t2 = time.time()
11886
elapsedSeconds = t2 - t1
@@ -124,4 +92,6 @@ def run():
12492

12593

12694
if __name__ == '__main__':
127-
run()
95+
if libraryNotFound:
96+
sys.exit(1)
97+
sys.exit(run())

generateAllMarkFiles.py

Lines changed: 80 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,100 @@
1-
#!/usr/bin/python
1+
"""
2+
This script takes a path to a folder as input, finds all
3+
the UFOs inside that folder and its subdirectories, and outputs each
4+
font's anchors in feature file syntax. If a path is not provided, the
5+
script uses the current path as the top-most directory. The name of
6+
the resulting mark FEA files is managed by the markFeatureWriter
7+
module.
8+
"""
9+
10+
from __future__ import print_function
211

312
import os
413
import sys
514
import time
615

7-
############################################
8-
# THE VALUES BELOW CAN BE EDITED AS NEEDED #
9-
############################################
10-
11-
writeClassesFile = True
12-
# TRUE: Writes mark classes to external file.
13-
# FALSE: Writes mark classes as part of mark.fea file.
14-
genMkmkFeature = True
15-
# TRUE: Writes mkmk.fea file.
16-
# FALSE: Ignores mark-to-mark placement.
17-
indianScriptsFormat = True
18-
# TRUE: Writes abvm.fea and blwm.fea files.
19-
# FALSE: Writes simple mark.fea file.
20-
trimCasingTags = True
21-
# TRUE: Trims casing tags so that all marks can be applied to UC/LC.
22-
# FALSE: Leaves casing tags as is.
23-
24-
# ------------------------------------------
25-
2616
libraryNotFound = False
2717

2818
try:
29-
from defcon import Font
30-
except:
31-
print "ERROR: This script requires defcon. It can be downloaded from https://github.com/typesupply/defcon"
32-
libraryNotFound = True
19+
from defcon import Font
20+
except ImportError:
21+
print("ERROR: This script requires defcon. It can be downloaded from "
22+
"https://github.com/typesupply/defcon")
23+
libraryNotFound = True
3324
try:
34-
import WriteFeaturesMarkFDK
35-
except:
36-
print "ERROR: This script requires WriteFeaturesMarkFDK.py. It can be downloaded from https://github.com/adobe-type-tools/python-modules"
37-
libraryNotFound = True
38-
39-
if libraryNotFound:
40-
sys.exit()
25+
import markFeatureWriter
26+
except ImportError:
27+
print("ERROR: This script requires markFeatureWriter.py. It can be "
28+
"downloaded from https://github.com/adobe-type-tools/python-modules")
29+
libraryNotFound = True
4130

42-
fontsList = []
4331

44-
45-
def getFontPaths(path, startpath):
46-
files = os.listdir(path)
47-
for file in files:
48-
if file[-4:].lower() in [".ufo"]:
49-
fontsList.append(os.path.join(path, file))
50-
else:
51-
if os.path.isdir(os.path.join(path, file)):
52-
getFontPaths(os.path.join(path, file), startpath)
32+
def getFontPaths(startpath):
33+
font_paths = []
34+
for dir_path, _, _ in os.walk(startpath):
35+
if dir_path.lower().endswith('.ufo'):
36+
font_paths.append(os.path.abspath(dir_path))
37+
return sorted(font_paths)
5338

5439

5540
def doTask(fonts, startpath):
56-
totalFonts = len(fonts)
57-
print "%d fonts found\n" % totalFonts
58-
i = 0
59-
60-
for font in fonts:
61-
i += 1
62-
folderPath, fontFileName = os.path.split(os.path.realpath(font))
63-
styleName = os.path.basename(folderPath)
64-
65-
# Change current directory to the folder where the font is contained
66-
os.chdir(folderPath)
67-
exportMessage = 'Exporting mark files for %s...(%d/%d)' % (
68-
styleName, i, totalFonts)
69-
print '*' * len(exportMessage)
70-
print exportMessage
71-
72-
ufoFont = Font(fontFileName)
73-
WriteFeaturesMarkFDK.MarkDataClass(
74-
ufoFont, folderPath, trimCasingTags,
75-
genMkmkFeature, writeClassesFile,
76-
indianScriptsFormat
77-
)
78-
# go back to the start
79-
os.chdir(startpath)
80-
41+
totalFonts = len(fonts)
42+
print("%d fonts found\n" % totalFonts)
8143

82-
def run():
83-
# if a path is provided
84-
if len(sys.argv[1:]):
85-
baseFolderPath = os.path.normpath(sys.argv[1])
44+
for i, font in enumerate(fonts, 1):
45+
folderPath, fontFileName = os.path.split(font)
46+
styleName = os.path.basename(folderPath)
47+
folderPath = os.path.abspath(folderPath)
8648

87-
# make sure the path is valid
88-
if not os.path.isdir(baseFolderPath):
89-
print 'Invalid directory.'
90-
return
49+
os.chdir(folderPath)
9150

92-
# if a path is not provided, use the current directory
93-
else:
94-
baseFolderPath = os.getcwd()
51+
exportMessage = 'Exporting mark files for %s...(%d/%d)' % (
52+
styleName, i, totalFonts)
53+
print('*' * len(exportMessage))
54+
print(exportMessage)
9555

96-
t1 = time.time()
56+
markFeatureWriter.run(font,
57+
write_classes=True,
58+
write_mkmk=True,
59+
indic_format=True,
60+
trim_tags=True,
61+
)
62+
os.chdir(startpath)
9763

98-
getFontPaths(baseFolderPath, baseFolderPath)
9964

100-
# the path from which the script is executed
101-
startpath = os.path.abspath(os.path.curdir)
102-
if len(fontsList):
103-
doTask(fontsList, startpath)
104-
else:
105-
print "No fonts found"
106-
return
107-
108-
t2 = time.time()
109-
elapsedSeconds = t2-t1
110-
111-
if (elapsedSeconds/60) < 1:
112-
print 'Completed in %.1f seconds.' % elapsedSeconds
113-
else:
114-
print 'Completed in %.1f minutes.' % (elapsedSeconds/60)
115-
116-
117-
if __name__=='__main__':
118-
run()
65+
def run():
66+
# if a path is provided
67+
if len(sys.argv[1:]):
68+
baseFolderPath = os.path.normpath(sys.argv[1])
69+
70+
# make sure the path is valid
71+
if not os.path.isdir(baseFolderPath):
72+
print('Invalid directory.')
73+
return 1
74+
75+
# if a path is not provided, use the current directory
76+
else:
77+
baseFolderPath = os.getcwd()
78+
79+
t1 = time.time()
80+
fontsList = getFontPaths(baseFolderPath)
81+
startpath = os.path.abspath(os.path.curdir)
82+
if len(fontsList):
83+
doTask(fontsList, startpath)
84+
else:
85+
print("No fonts found")
86+
return 1
87+
88+
t2 = time.time()
89+
elapsedSeconds = t2 - t1
90+
91+
if (elapsedSeconds // 60) < 1:
92+
print('Completed in %.1f seconds.' % elapsedSeconds)
93+
else:
94+
print('Completed in %.1f minutes.' % (elapsedSeconds // 60))
95+
96+
97+
if __name__ == '__main__':
98+
if libraryNotFound:
99+
sys.exit(1)
100+
sys.exit(run())

0 commit comments

Comments
 (0)