-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add command line script #73
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,3 +75,66 @@ def extractUFO( | |
raise ExtractorError( | ||
"There was an error reading the %s file." % format | ||
) | ||
|
||
|
||
def cmdline(): | ||
""" | ||
Extract one ore more fonts to UFO. Installed as command line script | ||
`extractufo`. | ||
|
||
Usage: extractufo font [font ...] | ||
""" | ||
import os | ||
from sys import exit | ||
from argparse import ArgumentParser | ||
|
||
parser = ArgumentParser( | ||
description="Extract data from font binaries and build UFO objects from them.", | ||
epilog="Each resulting UFO will be saved as FONT_FILE.ufo(z) in the same directory as the original FONT_FILE. " | ||
"If destination file or directory already exists, conversion for that source file will be skipped and the application exit code will indicate an error.", | ||
) | ||
parser.add_argument('FONT_FILE', help='Input font path', nargs="+") | ||
parser.add_argument('-m', '--ufo-module', choices=['ufoLib2', 'defcon'], | ||
help='Select the default library for writing UFOs (default: autodetect, prefer ufoLib2)') | ||
parser.add_argument('-z', '--zip', action="store_true", help="Output UFO ZIP") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, "UFO ZIP" is actually how the specification calls it. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. I can't remember, and I am used to thinking UFOZ, but fine with UFO ZIP There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From https://unifiedfontobject.org/versions/ufo3/#storage-formats:
I'll leave this as-is then. |
||
|
||
args = parser.parse_args() | ||
if args.ufo_module is None: | ||
try: | ||
from ufoLib2 import Font | ||
print("Will use ufoLib2 for UFO output.") | ||
except ImportError: | ||
try: | ||
from defcon import Font | ||
print("Will use defcon for UFO output.") | ||
except ImportError: | ||
print("Either ufoLib2 or, alternatively, defcon library is required to run this command.\nPlease install one of them.") | ||
exit(1) | ||
elif args.ufo_module == 'ufoLib2': | ||
try: | ||
from ufoLib2 import Font | ||
except ImportError: | ||
print("Can't find ufoLib2 installed. Please install it or specify a different UFO library.") | ||
exit(1) | ||
else: | ||
try: | ||
from defcon import Font | ||
except ImportError: | ||
print("Can't find defcon installed. Please install it or specify a different UFO library.") | ||
exit(1) | ||
|
||
structure = "zip" if args.zip else "package" | ||
had_write_errors = False | ||
for font_path in args.FONT_FILE: | ||
ufo_path = f"{font_path}.ufo" if not args.zip else f"{font_path}.ufoz" | ||
print(f"Extracting {ufo_path}... ", end="") | ||
if os.path.exists(ufo_path): | ||
print("path already exists, skipping.") | ||
had_write_errors = True | ||
continue | ||
ufo = Font() | ||
extractUFO(font_path, ufo) | ||
ufo.save(ufo_path, structure=structure) | ||
print("done.") | ||
|
||
exit(had_write_errors) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, not sure if it makes sense to keep this comment now that we're using the argument parser.