Download font_utils.py from digidau/qur: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/digidau/qur/resolve/main/font_utils.py
- Command line
-
hf download hf://spaces/digidau/qur/font_utils.py
-
curl -L -o font_utils.py https://huggingface.co/spaces/digidau/qur/resolve/main/font_utils.py
1.11 kB
| 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 |