| 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 |