Skip to content

Commit 2cdd885

Browse files
committed
Update checkAll to Python3
1 parent 671d6c7 commit 2cdd885

1 file changed

Lines changed: 104 additions & 102 deletions

File tree

checkAll.py

Lines changed: 104 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/env python
22

33
import os
44
import sys
@@ -7,132 +7,134 @@
77

88

99
__doc__ = """
10-
checkAll v1.1 - Aug 04 2013
11-
12-
This script takes a path to a folder as input, finds all the Type 1 fonts
13-
(.pfa files) or UFO fonts inside that folder and its subdirectories, and
14-
removes their overlaps using the FDK's checkOutlines tool. If a path is not
15-
provided, the script will use the current path as the top-most directory.
16-
The script ignores MM PFA fonts, usually named 'mmfont.pfa'.
17-
The Type 1 fonts can also be in plain text format (.txt) where the Private
18-
and CharStrings dictionaries are not encrypted. These files can be obtained
10+
checkAll v1.2 - Dec 01 2019
11+
12+
This script takes a path to a folder as input, finds all UFO files (or Type 1
13+
fonts - .pfa files) inside that folder and its subdirectories, and removes
14+
their overlaps using the FDK's checkoutlinesufo tool.
15+
If a path is not provided, the script will use the current path as the topmost
16+
directory. The script ignores MM PFA fonts, usually named 'mmfont.pfa'.
17+
Type 1 fonts can also be in plain text format (.txt) where the Private
18+
and CharStrings dictionaries are not encrypted. These files can be created
1919
by using the FDK's detype1 tool.
2020
2121
==================================================
2222
Versions:
2323
v1.0 - Jul 09 2013 - Initial release
2424
v1.1 - Aug 04 2013 - Added support for UFO files
25+
v1.2 - Dec 01 2019 - Convert to Python 3, checkoutlinesufo
2526
"""
2627

2728
kFontTXT = "font.txt"
2829
kTempPFA = "font_TEMP_.pfa"
2930

3031

3132
def getFontPaths(path):
32-
fontsList = []
33-
for r, folders, files in os.walk(path):
34-
fileAndFolderList = folders[:]
35-
fileAndFolderList.extend(files)
36-
37-
for item in fileAndFolderList:
38-
fileName, extension = os.path.splitext(item)
39-
extension = extension.lower()
40-
if extension == ".pfa" and not fileName == "mmfont":
41-
fontsList.append(os.path.join(r, item))
42-
elif extension == ".txt" and fileName == "font":
43-
fontsList.append(os.path.join(r, item))
44-
elif extension == ".ufo":
45-
fontsList.append(os.path.join(r, item))
46-
else:
47-
continue
48-
49-
return fontsList
33+
fontsList = []
34+
for r, folders, files in os.walk(path):
35+
fileAndFolderList = folders[:]
36+
fileAndFolderList.extend(files)
37+
38+
for item in fileAndFolderList:
39+
fileName, extension = os.path.splitext(item)
40+
extension = extension.lower()
41+
if extension == ".pfa" and not fileName == "mmfont":
42+
fontsList.append(os.path.join(r, item))
43+
elif extension == ".txt" and fileName == "font":
44+
fontsList.append(os.path.join(r, item))
45+
elif extension == ".ufo":
46+
fontsList.append(os.path.join(r, item))
47+
else:
48+
continue
49+
50+
return fontsList
5051

5152

5253
def doTask(fonts):
53-
totalFonts = len(fonts)
54-
print "%d fonts found\n" % totalFonts
55-
i = 1
56-
57-
for font in fonts:
58-
folderPath, fontFileName = os.path.split(font)
59-
styleName = os.path.basename(folderPath)
60-
61-
# Change current directory to the folder where the font is contained
62-
os.chdir(folderPath)
63-
64-
# If it's a txt font, convert it to pfa temporarily
65-
fontIsTXT = False
66-
if fontFileName == kFontTXT:
67-
fontIsTXT = True
68-
fontFileName = kTempPFA
69-
cmd = 'type1 %s > %s' % (kFontTXT, kTempPFA)
70-
popen = Popen(cmd, shell=True, stdout=PIPE)
71-
popenout, popenerr = popen.communicate()
72-
if popenout:
73-
print popenout
74-
if popenerr:
75-
print popenerr
76-
77-
print '*******************************'
78-
print 'Checking %s...(%d/%d)' % (styleName, i, totalFonts)
79-
i += 1
80-
cmd = 'checkOutlines -e "%s"' % fontFileName
81-
popen = Popen(cmd, shell=True, stdout=PIPE)
82-
popenout, popenerr = popen.communicate()
83-
if popenout:
84-
print popenout
85-
if popenerr:
86-
print popenerr
87-
88-
# Revert font back to pfa, and delete temporary file
89-
if fontIsTXT:
90-
cmd = 'detype1 %s > %s' % (kTempPFA, kFontTXT)
91-
popen = Popen(cmd, shell=True, stdout=PIPE)
92-
popenout, popenerr = popen.communicate()
93-
if popenout:
94-
print popenout
95-
if popenerr:
96-
print popenerr
97-
98-
if os.path.exists(kTempPFA):
99-
os.remove(kTempPFA)
54+
totalFonts = len(fonts)
55+
print("%d fonts found\n" % totalFonts)
56+
i = 1
57+
58+
for font in fonts:
59+
folderPath, fontFileName = os.path.split(font)
60+
styleName = os.path.basename(folderPath)
61+
62+
# Change current directory to the folder where the font is contained
63+
os.chdir(folderPath)
64+
65+
# If it's a txt font, convert it to pfa temporarily
66+
fontIsTXT = False
67+
if fontFileName == kFontTXT:
68+
fontIsTXT = True
69+
fontFileName = kTempPFA
70+
cmd = 'type1 %s > %s' % (kFontTXT, kTempPFA)
71+
popen = Popen(cmd, shell=True, stdout=PIPE)
72+
popenout, popenerr = popen.communicate()
73+
if popenout:
74+
print(popenout.decode('utf-8'))
75+
if popenerr:
76+
print(popenerr.decode('utf-8'))
77+
78+
print('*******************************')
79+
print('Checking %s...(%d/%d)' % (styleName, i, totalFonts))
80+
i += 1
81+
cmd = 'checkoutlinesufo -e "%s"' % fontFileName
82+
popen = Popen(cmd, shell=True, stdout=PIPE)
83+
popenout, popenerr = popen.communicate()
84+
if popenout:
85+
print(popenout.decode('utf-8'))
86+
if popenerr:
87+
print(popenerr.decode('utf-8'))
88+
89+
# Revert font back to pfa, and delete temporary file
90+
if fontIsTXT:
91+
cmd = 'detype1 %s > %s' % (kTempPFA, kFontTXT)
92+
popen = Popen(cmd, shell=True, stdout=PIPE)
93+
popenout, popenerr = popen.communicate()
94+
if popenout:
95+
print(popenout.decode('utf-8'))
96+
if popenerr:
97+
print(popenerr.decode('utf-8'))
98+
99+
if os.path.exists(kTempPFA):
100+
os.remove(kTempPFA)
100101

101102

102103
def run():
103-
# if a path is provided
104-
if len(sys.argv[1:]):
105-
baseFolderPath = sys.argv[1]
104+
# if a path is provided
105+
if len(sys.argv[1:]):
106+
baseFolderPath = sys.argv[1]
106107

107-
if baseFolderPath[-1] == '/': # remove last slash if present
108-
baseFolderPath = baseFolderPath[:-1]
108+
if baseFolderPath[-1] == '/': # remove last slash if present
109+
baseFolderPath = baseFolderPath[:-1]
109110

110-
# make sure the path is valid
111-
if not os.path.isdir(baseFolderPath):
112-
print 'Invalid directory.'
113-
return
111+
# make sure the path is valid
112+
if not os.path.isdir(baseFolderPath):
113+
print('Invalid directory.')
114+
return
114115

115-
# if a path is not provided, use the current directory
116-
else:
117-
baseFolderPath = os.getcwd()
116+
# if a path is not provided, use the current directory
117+
else:
118+
baseFolderPath = os.getcwd()
118119

119-
t1 = time.time()
120-
fontsList = getFontPaths(baseFolderPath)
120+
t1 = time.time()
121+
fontsList = getFontPaths(baseFolderPath)
121122

122-
if len(fontsList):
123-
doTask(fontsList)
124-
else:
125-
print "No fonts found"
126-
return
123+
if len(fontsList):
124+
doTask(fontsList)
125+
else:
126+
print("No fonts found")
127+
return
127128

128-
t2 = time.time()
129-
elapsedSeconds = t2-t1
129+
t2 = time.time()
130+
elapsedSeconds = t2 - t1
131+
elapsedMinutes = elapsedSeconds / 60
130132

131-
if (elapsedSeconds/60) < 1:
132-
print 'Completed in %.1f seconds.' % elapsedSeconds
133-
else:
134-
print 'Completed in %.1f minutes.' % (elapsedSeconds/60)
133+
if elapsedMinutes < 1:
134+
print('Completed in %.1f seconds.' % elapsedSeconds)
135+
else:
136+
print('Completed in %.1f minutes.' % elapsedMinutes)
135137

136138

137-
if __name__=='__main__':
138-
run()
139+
if __name__ == '__main__':
140+
run()

0 commit comments

Comments
 (0)