This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
from PIL import ImageFont
|
||||
from enum import Enum
|
||||
from typing import Tuple, Union, Optional
|
||||
import os
|
||||
|
||||
|
||||
class FontWeight(Enum):
|
||||
@@ -63,8 +64,17 @@ class Font:
|
||||
# Load the font file or use default
|
||||
self._load_font()
|
||||
|
||||
def _get_bundled_font_path(self):
|
||||
"""Get the path to the bundled font"""
|
||||
# Get the directory containing this module
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
# Navigate to the assets/fonts directory
|
||||
assets_dir = os.path.join(os.path.dirname(current_dir), 'assets', 'fonts')
|
||||
bundled_font_path = os.path.join(assets_dir, 'DejaVuSans.ttf')
|
||||
return bundled_font_path if os.path.exists(bundled_font_path) else None
|
||||
|
||||
def _load_font(self):
|
||||
"""Load the font using PIL's ImageFont with better system fonts"""
|
||||
"""Load the font using PIL's ImageFont with consistent bundled font"""
|
||||
try:
|
||||
if self._font_path:
|
||||
self._font = ImageFont.truetype(
|
||||
@@ -72,8 +82,15 @@ class Font:
|
||||
self._font_size
|
||||
)
|
||||
else:
|
||||
# Try to load better system fonts
|
||||
font_candidates = [
|
||||
# Try bundled font first for consistency across environments
|
||||
bundled_font_path = self._get_bundled_font_path()
|
||||
|
||||
font_candidates = []
|
||||
if bundled_font_path:
|
||||
font_candidates.append(bundled_font_path)
|
||||
|
||||
# Fallback to system fonts if bundled font is not available
|
||||
font_candidates.extend([
|
||||
# Linux fonts
|
||||
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
||||
@@ -83,7 +100,7 @@ class Font:
|
||||
"C:/Windows/Fonts/calibri.ttf", # Windows
|
||||
# Fallback to default
|
||||
None
|
||||
]
|
||||
])
|
||||
|
||||
self._font = None
|
||||
for font_path in font_candidates:
|
||||
|
||||
Reference in New Issue
Block a user