font-patcher: Handle argument checks with arguments

[why]
The extension handling is a bit out-of-place and could be handled by the
arguments handling, which simplifies the code.
Somes goes for other argument validity checks.

[how]
Put argument checks into setup_arguments().

Dropping self.extensions in favour of self.args.extensions.

No functional change.

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
This commit is contained in:
Fini Jastrow 2022-02-09 15:18:22 +01:00
parent ec4cb0a8c5
commit 2432a5700a

View file

@ -163,16 +163,8 @@ class font_patcher:
self.patch_set = None # class 'list' self.patch_set = None # class 'list'
self.font_dim = None # class 'dict' self.font_dim = None # class 'dict'
self.onlybitmaps = 0 self.onlybitmaps = 0
self.extension = ""
self.essential = set() self.essential = set()
self.config = configparser.ConfigParser(empty_lines_in_values=False, allow_no_value=True) self.config = configparser.ConfigParser(empty_lines_in_values=False, allow_no_value=True)
if not os.path.isfile(self.args.font):
sys.exit("{}: Font file does not exist: {}".format(projectName, self.args.font))
if not os.access(self.args.font, os.R_OK):
sys.exit("{}: Can not open font file for reading: {}".format(projectName, self.args.font))
if len(fontforge.fontsInFile(self.args.font)) > 1:
sys.exit("{}: Font file contains {} fonts, can only handle single font files".format(projectName,
len(fontforge.fontsInFile(self.args.font))))
try: try:
self.sourceFont = fontforge.open(self.args.font, 1) # 1 = ("fstypepermitted",)) self.sourceFont = fontforge.open(self.args.font, 1) # 1 = ("fstypepermitted",))
except Exception: except Exception:
@ -181,19 +173,12 @@ class font_patcher:
self.get_essential_references() self.get_essential_references()
self.setup_name_backup() self.setup_name_backup()
self.remove_ligatures() self.remove_ligatures()
make_sure_path_exists(self.args.outputdir)
self.check_position_conflicts() self.check_position_conflicts()
self.setup_patch_set() self.setup_patch_set()
self.setup_line_dimensions() self.setup_line_dimensions()
self.get_sourcefont_dimensions() self.get_sourcefont_dimensions()
self.sourceFont.encoding = 'UnicodeFull' # Update the font encoding to ensure that the Unicode glyphs are available self.sourceFont.encoding = 'UnicodeFull' # Update the font encoding to ensure that the Unicode glyphs are available
self.onlybitmaps = self.sourceFont.onlybitmaps # Fetch this property before adding outlines. NOTE self.onlybitmaps initialized and never used self.onlybitmaps = self.sourceFont.onlybitmaps # Fetch this property before adding outlines. NOTE self.onlybitmaps initialized and never used
if self.args.extension == "":
self.extension = os.path.splitext(self.args.font)[1]
else:
self.extension = '.' + self.args.extension
if re.match("\.ttc$", self.extension, re.IGNORECASE):
sys.exit(projectName + ": Can not create True Type Collections")
def patch(self): def patch(self):
@ -261,11 +246,11 @@ class font_patcher:
def generate(self): def generate(self):
# the `PfEd-comments` flag is required for Fontforge to save '.comment' and '.fontlog'. # the `PfEd-comments` flag is required for Fontforge to save '.comment' and '.fontlog'.
if self.sourceFont.fullname != None: if self.sourceFont.fullname != None:
outfile = self.args.outputdir + "/" + self.sourceFont.fullname + self.extension outfile = self.args.outputdir + "/" + self.sourceFont.fullname + self.args.extension
self.sourceFont.generate(outfile, flags=(str('opentype'), str('PfEd-comments'))) self.sourceFont.generate(outfile, flags=(str('opentype'), str('PfEd-comments')))
message = "\nGenerated: {} in '{}'".format(self.sourceFont.fullname, outfile) message = "\nGenerated: {} in '{}'".format(self.sourceFont.fullname, outfile)
else: else:
outfile = self.args.outputdir + "/" + self.sourceFont.cidfontname + self.extension outfile = self.args.outputdir + "/" + self.sourceFont.cidfontname + self.args.extension
self.sourceFont.generate(outfile, flags=(str('opentype'), str('PfEd-comments'))) self.sourceFont.generate(outfile, flags=(str('opentype'), str('PfEd-comments')))
message = "\nGenerated: {} in '{}'".format(self.sourceFont.fontname, outfile) message = "\nGenerated: {} in '{}'".format(self.sourceFont.fontname, outfile)
@ -1270,6 +1255,22 @@ def setup_arguments():
print("Warniung: Specified contradicting --variable-width-glyphs and --use-single-width-glyph. Ignoring --variable-width-glyphs.") print("Warniung: Specified contradicting --variable-width-glyphs and --use-single-width-glyph. Ignoring --variable-width-glyphs.")
args.nonmono = False args.nonmono = False
make_sure_path_exists(args.outputdir)
if not os.path.isfile(args.font):
sys.exit("{}: Font file does not exist: {}".format(projectName, args.font))
if not os.access(args.font, os.R_OK):
sys.exit("{}: Can not open font file for reading: {}".format(projectName, args.font))
if len(fontforge.fontsInFile(args.font)) > 1:
sys.exit("{}: Font file contains {} fonts, can only handle single font files".format(projectName,
len(fontforge.fontsInFile(args.font))))
if args.extension == "":
args.extension = os.path.splitext(args.font)[1]
else:
args.extension = '.' + args.extension
if re.match("\.ttc$", args.extension, re.IGNORECASE):
sys.exit(projectName + ": Can not create True Type Collections")
return args return args