|
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 |
2 | 11 |
|
3 | 12 | import os |
4 | 13 | import sys |
5 | 14 | import time |
6 | 15 |
|
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 | | - |
26 | 16 | libraryNotFound = False |
27 | 17 |
|
28 | 18 | 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 |
33 | 24 | 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 |
41 | 30 |
|
42 | | -fontsList = [] |
43 | 31 |
|
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) |
53 | 38 |
|
54 | 39 |
|
55 | 40 | 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) |
81 | 43 |
|
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) |
86 | 48 |
|
87 | | - # make sure the path is valid |
88 | | - if not os.path.isdir(baseFolderPath): |
89 | | - print 'Invalid directory.' |
90 | | - return |
| 49 | + os.chdir(folderPath) |
91 | 50 |
|
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) |
95 | 55 |
|
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) |
97 | 63 |
|
98 | | - getFontPaths(baseFolderPath, baseFolderPath) |
99 | 64 |
|
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