-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont_utils.py
117 lines (92 loc) · 3.59 KB
/
font_utils.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# font_utils.py
"""
Utility functions for working with fonts
Usage:
python font_utils.py
"""
from functools import lru_cache
from PIL import ImageFont
import unicodedata
import logging
from log_config import logger
import os
def test_font_character(font_path: str, char: str, size: int = 24) -> bool:
"""
Test if a font supports a specific character.
Args:
font_path: Path to the font file
char: Character to test
size: Font size to use for testing
Returns:
bool: True if the font supports the character, False otherwise
"""
try:
# Load the font
font = ImageFont.truetype(font_path, size)
font_short_name = os.path.basename(font_path)
# Get the character name for logging
char_name = unicodedata.name(char, "UNKNOWN")
# Try to get the glyph metrics
bbox = font.getbbox(char)
# If bbox is None or has zero width/height, the character isn't supported
if not bbox or (bbox[2] - bbox[0] == 0) or (bbox[3] - bbox[1] == 0):
logger.debug(f"Font {font_short_name} does not support '{char}' ({char_name})")
return False
logger.debug(f"Font {font_short_name} supports '{char}' ({char_name})")
return True
except Exception as e:
logger.error(f"Error testing font {font_short_name} for character '{char}': {e}")
return False
# Add to weather.py or where your weather icons are defined
WEATHER_ICONS = {
'Clear': '☀',
'Clouds': '☁',
'Rain': '🌧',
'Snow': '❄',
'Thunderstorm': '⚡',
'Drizzle': '🌦',
'Mist': '🌫',
}
def verify_font_support():
"""Verify that the current font supports all weather icons"""
font_path = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'
unsupported = []
for weather, icon in WEATHER_ICONS.items():
if not test_font_character(font_path, icon):
unsupported.append((weather, icon))
if unsupported:
logger.warning(f"Current font does not support these weather icons: {unsupported}")
logger.warning("Consider installing fonts-noto-color-emoji for better support")
return len(unsupported) == 0
if __name__ == "__main__":
verify_font_support()
# Example usage:
test_chars = ['🕒', '⚡', '☀', '☁', '🌧', '❄', '⚡', '🌦', '🌫']
font_paths = [
'/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
# '/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf',
# Add other font paths to test
]
for font_path in font_paths:
print(f"\nTesting font: {font_path}")
for char in test_chars:
supported = test_font_character(font_path, char)
print(f"Character {char}: {'✓' if supported else '✗'}")
@lru_cache(maxsize=32)
def get_font_paths():
"""Get the appropriate font paths based on the operating system."""
import platform
import os
system = platform.system()
if system == "Darwin": # macOS
return {
'dejavu': '/System/Library/Fonts/Supplemental/DejaVuSans.ttf',
'dejavu_bold': '/System/Library/Fonts/Supplemental/DejaVuSans-Bold.ttf',
'emoji': os.path.expanduser('~/Library/Fonts/NotoEmoji[wght].ttf') # Use user's Noto Emoji font
}
else: # Assume Raspberry Pi/Linux
return {
'dejavu': '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
'dejavu_bold': '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',
'emoji': '/usr/local/share/fonts/noto/NotoEmoji-Regular.ttf'
}