@@ -85,27 +85,57 @@ def cmdline():
85
85
Usage: extractufo font [font ...]
86
86
"""
87
87
from sys import argv , exit
88
- try :
89
- from ufoLib2 import Font
90
- library = "ufoLib2"
91
- except ImportError :
88
+ from argparse import ArgumentParser
89
+ from extractor import extractUFO
90
+
91
+ parser = ArgumentParser (
92
+ description = "Extract data from font binaries and build UFO objects from them." ,
93
+ epilog = "Each resulting UFO will be saved as FONT_FILE.ufo(z) in the same directory as the original FONT_FILE." ,
94
+ )
95
+ parser .add_argument ('FONT_FILE' , help = 'Input font path' , nargs = "+" )
96
+ parser .add_argument ('-m' , '--ufo-module' , choices = ['ufoLib2' , 'defcon' ], help = 'Select the default library for writing UFOs (default: autodetect, prefer ufoLib2)' )
97
+ parser .add_argument ('-z' , '--zip' , action = "store_true" , help = "Output UFO ZIP" )
98
+ parser .add_argument ('-o' , '--overwrite' , action = "store_true" , help = "Overwrite output location if exists" )
99
+
100
+ args = parser .parse_args ()
101
+ if args .ufo_module is None :
102
+ try :
103
+ from ufoLib2 import Font
104
+ print ("Will use ufoLib2 for UFO output." )
105
+ except ImportError :
106
+ try :
107
+ from defcon import Font
108
+ print ("Will use defcon for UFO output." )
109
+ except ImportError :
110
+ print ("Either ufoLib2 or, alternatively, defcon library is required to run this command.\n Please install one of them." )
111
+ exit (1 )
112
+ elif args .ufo_module == 'ufoLib2' :
113
+ try :
114
+ from ufoLib2 import Font
115
+ except ImportError :
116
+ print ("Can't find ufoLib2 installed. Please install it or specify a different UFO library." )
117
+ exit (1 )
118
+ else :
92
119
try :
93
120
from defcon import Font
94
- library = "defcon"
95
121
except ImportError :
96
- print ("Either ufoLib2 or defcon library is required for this command to work. \n Please install one of them ." )
122
+ print ("Can't find defcon installed. Please install it or specify a different UFO library ." )
97
123
exit (1 )
98
124
99
- if len (argv ) <= 1 :
100
- print ("No font path supplied.\n Usage: extractufo font [font ...]" )
101
- exit (1 )
125
+ structure = "zip" if args .zip else "package"
126
+ had_write_errors = False
102
127
103
- print (f"Will use { library } library for UFO output." )
104
-
105
- for font_path in argv [1 :]:
106
- ufo_path = f"{ font_path } .ufo"
128
+ for font_path in args .FONT_FILE :
129
+ ufo_path = f"{ font_path } .ufo" if not args .zip else f"{ font_path } .ufoz"
107
130
print (f"Extracting { ufo_path } ... " , end = "" )
108
131
ufo = Font ()
109
132
extractUFO (font_path , ufo )
110
- ufo .save (ufo_path , overwrite = True )
133
+ try :
134
+ ufo .save (ufo_path , overwrite = args .overwrite , structure = structure )
135
+ except FileExistsError :
136
+ print ("path already exists, skipping." )
137
+ had_write_errors = True
138
+ continue
111
139
print ("done." )
140
+
141
+ exit (had_write_errors )
0 commit comments