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",
]
font_names = [
"NotoSansMono", # 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.
"CascadiaMono", # Cascadia Code without ligatures; drawing cell by cell, ligatures won't apply anyways
"CascadiaCode",
"DejaVuSansMono",
"LiberationMono",
"UbuntuMono",
"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 (or curated) yet.
"Cascadia Mono", # Cascadia Code without ligatures; drawing cell by cell, ligatures won't apply anyways
"Cascadia Code",
"DejaVu Sans Mono",
"Liberation Mono",
"Ubuntu Mono",
"Hack",
"FiraMono",
"Fira Mono",
"Inconsolata",
"SourceCodePro",
"DroidSansMono",
"Source Code Pro",
"Droid Sans Mono",
"Consolas",
"Consola",
"CourierNew",
"LucidaConsole",
"Courier New",
"Lucida Console",
"Monaco",
"Menlo",
"Andale Mono",
"Courier New",
"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
for font_dir in font_dirs:
path = Path(os.path.expandvars(os.path.expanduser(font_dir)))
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)))
for file in files:
# print(f"stem {file.stem!r}", file.stem in font_names)
if file.stem in font_names:
# print(f"stem {file.stem!r}", normalize_font_name(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)
break
if font: