Match font names case-insensitively and ignoring spaces

This commit is contained in:
Isaiah Odhner 2023-07-19 01:39:36 -04:00
parent ec7986f9dd
commit 8e7113e1b5

View File

@ -32,7 +32,7 @@ font_dirs = [
] ]
font_names = [ font_names = [
"Noto Sans Mono", # first because of broad Unicode coverage ("Noto" stands for "no tofu", i.e. replacement characters that look like blocks of tofu) "Noto Sans Mono", # first because of broad Unicode coverage ("Noto" stands for "no tofu", i.e. replacement characters that look like blocks of tofu)
# The rest of this list is not very deliberately ordered. # The rest of this list is not very deliberately ordered (or curated) yet.
"Cascadia Mono", # Cascadia Code without ligatures; drawing cell by cell, ligatures won't apply anyways "Cascadia Mono", # Cascadia Code without ligatures; drawing cell by cell, ligatures won't apply anyways
"Cascadia Code", "Cascadia Code",
"DejaVu Sans Mono", "DejaVu Sans Mono",
@ -50,18 +50,21 @@ font_names = [
"Monaco", "Monaco",
"Menlo", "Menlo",
"Andale Mono", "Andale Mono",
"Courier New",
"Cour", "Cour",
] ]
def normalize_font_name(name: str) -> str:
return name.lower().replace(" ", "").replace("-", "").replace("_", "")
font_names = [normalize_font_name(name) for name in font_names]
font = None font = None
for font_dir in font_dirs: for font_dir in font_dirs:
path = Path(os.path.expandvars(os.path.expanduser(font_dir))) path = Path(os.path.expandvars(os.path.expanduser(font_dir)))
files = path.glob("**/*.ttf") files = path.glob("**/*.ttf")
# files = list(files) # printing consumes the generator without this! files = list(files) # printing consumes the generator without this!
# print("path", path, "files", "\n".join(map(str, files))) # print("path", path, "files", "\n".join(map(str, files)))
for file in files: for file in files:
# print(f"stem {file.stem!r}", file.stem in font_names) # print(f"stem {file.stem!r}", normalize_font_name(file.stem) in font_names)
if file.stem in font_names: if normalize_font_name(file.stem) in font_names:
font = ImageFont.truetype(str(file), size=16, layout_engine=ImageFont.LAYOUT_BASIC) font = ImageFont.truetype(str(file), size=16, layout_engine=ImageFont.LAYOUT_BASIC)
break break
if font: if font: