Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions video_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
def install_google_font(font_name):
"""
Downloads a font from Google Fonts, extracts it, and saves to assets/fonts directory.
Uses multiple fallback methods to download the font.
Automatically detects the font variant (Regular, Bold, Italic, Light) from the URL
to ensure the filename matches the actual font style.

Args:
font_name: Name of the font (e.g., "Heebo", "Roboto")
Expand All @@ -66,8 +67,10 @@ def install_google_font(font_name):
os.makedirs(save_dir, exist_ok=True)

# Check if font already exists (avoid unnecessary downloads)
font_path = os.path.join(save_dir, f"{font_name}-Regular.ttf")
if os.path.exists(font_path):
# Look for any variant of the font (Regular, Bold, Italic, etc.)
existing_fonts = [f for f in os.listdir(save_dir) if f.startswith(f"{font_name}-") and f.endswith('.ttf')]
if existing_fonts:
font_path = os.path.join(save_dir, existing_fonts[0])
print(f"✓ Font already installed: {font_path}")
return font_path

Expand All @@ -87,9 +90,35 @@ def install_google_font(font_name):

if ttf_urls:
# Download the first TTF file
ttf_response = requests.get(ttf_urls[0], timeout=30)
ttf_url = ttf_urls[0]
ttf_response = requests.get(ttf_url, timeout=30)
if ttf_response.status_code == 200:
font_path = os.path.join(save_dir, f"{font_name}-Regular.ttf")
# Extract font variant from URL to avoid mismatch
# Google Fonts URLs often contain the variant in the filename
# Example: https://fonts.gstatic.com/...FontName-Bold.ttf
font_filename = os.path.basename(ttf_url.split('?')[0]) # Remove query params

# If the filename doesn't contain the font name, use our naming convention
# Normalize both strings for comparison (remove spaces, lowercase)
normalized_font_name = font_name.replace(' ', '').lower()
normalized_filename = font_filename.replace(' ', '').lower()

if normalized_font_name not in normalized_filename:
# Try to detect variant from URL - check compound variants first
ttf_url_lower = ttf_url.lower()
if 'bolditalic' in ttf_url_lower or 'italicbold' in ttf_url_lower:
variant = 'BoldItalic'
elif 'bold' in ttf_url_lower:
variant = 'Bold'
elif 'italic' in ttf_url_lower:
variant = 'Italic'
elif 'light' in ttf_url_lower:
variant = 'Light'
else:
variant = 'Regular'
font_filename = f"{font_name}-{variant}.ttf"

font_path = os.path.join(save_dir, font_filename)
with open(font_path, 'wb') as f:
f.write(ttf_response.content)
print(f"✅ Font installed: {font_path}")
Expand Down