File size: 1,105 Bytes
8d102be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import os
import re
import subprocess
def get_font_family(font_path):
try:
result = subprocess.run(
["fc-scan", font_path],
capture_output=True,
text=True
)
match = re.search(
r'family:\s+"([^"]+)"',
result.stdout
)
if match:
return match.group(1)
except Exception as e:
print(e)
return None
def build_font_map(font_dir="fonts"):
font_map = {}
if not os.path.exists(font_dir):
return font_map
for filename in os.listdir(font_dir):
if not (
filename.endswith(".ttf")
or filename.endswith(".otf")
):
continue
path = os.path.join(
font_dir,
filename
)
family = get_font_family(path)
if family:
key = (
filename
.replace(".ttf", "")
.replace(".otf", "")
)
font_map[key] = family
print("\nFONT MAP:")
print(font_map)
return font_map |