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

@ -31,37 +31,40 @@ font_dirs = [
"/data/fonts", "/data/fonts",
] ]
font_names = [ font_names = [
"NotoSansMono", # 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.
"CascadiaMono", # 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
"CascadiaCode", "Cascadia Code",
"DejaVuSansMono", "DejaVu Sans Mono",
"LiberationMono", "Liberation Mono",
"UbuntuMono", "Ubuntu Mono",
"Hack", "Hack",
"FiraMono", "Fira Mono",
"Inconsolata", "Inconsolata",
"SourceCodePro", "Source Code Pro",
"DroidSansMono", "Droid Sans Mono",
"Consolas", "Consolas",
"Consola", "Consola",
"CourierNew", "Courier New",
"LucidaConsole", "Lucida Console",
"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: