From 8e7113e1b566d74397632f65092c1285f4999509 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Wed, 19 Jul 2023 01:39:36 -0400 Subject: [PATCH] Match font names case-insensitively and ignoring spaces --- src/textual_paint/rasterize_ansi_art.py | 35 ++++++++++++++----------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/textual_paint/rasterize_ansi_art.py b/src/textual_paint/rasterize_ansi_art.py index ca36c66..feec1f1 100644 --- a/src/textual_paint/rasterize_ansi_art.py +++ b/src/textual_paint/rasterize_ansi_art.py @@ -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: