pdf-4 / setup_fonts.py
fokan's picture
Initial commit with static file serving and inline PDF viewing
623e14e
#!/usr/bin/env python3
"""
Setup script to install Arabic fonts for Hugging Face Spaces
This script downloads and installs Arabic fonts that are not available in Debian repositories
"""
import os
import subprocess
import urllib.request
import zipfile
import tempfile
import shutil
def run_command(cmd):
"""Run a shell command and return the result"""
try:
result = subprocess.run(cmd, shell=True, check=True, capture_output=True, text=True)
return result.stdout
except subprocess.CalledProcessError as e:
print(f"Error running command '{cmd}': {e}")
print(f"Error output: {e.stderr}")
return None
def download_and_extract(url, extract_to):
"""Download a zip file and extract it"""
try:
with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tmp_file:
urllib.request.urlretrieve(url, tmp_file.name)
with zipfile.ZipFile(tmp_file.name, 'r') as zip_ref:
zip_ref.extractall(extract_to)
os.unlink(tmp_file.name)
return True
except Exception as e:
print(f"Error downloading/extracting {url}: {e}")
return False
def setup_arabic_fonts():
"""Setup Arabic fonts for LibreOffice"""
print("🔤 Setting up Arabic fonts for RTL support...")
# Create fonts directory
fonts_dir = "/usr/share/fonts/truetype/arabic-enhanced"
os.makedirs(fonts_dir, exist_ok=True)
# Download and install Amiri font
print("📥 Installing Amiri font...")
with tempfile.TemporaryDirectory() as tmp_dir:
amiri_url = "https://github.com/aliftype/amiri/releases/download/0.117/Amiri-0.117.zip"
if download_and_extract(amiri_url, tmp_dir):
amiri_dir = os.path.join(tmp_dir, "Amiri-0.117")
if os.path.exists(amiri_dir):
for file in os.listdir(amiri_dir):
if file.endswith('.ttf'):
src = os.path.join(amiri_dir, file)
dst = os.path.join(fonts_dir, file)
shutil.copy2(src, dst)
os.chmod(dst, 0o644)
print("✅ Amiri font installed successfully")
else:
print("❌ Amiri font directory not found")
else:
print("❌ Failed to download Amiri font")
# Download and install Scheherazade New font
print("📥 Installing Scheherazade New font...")
with tempfile.TemporaryDirectory() as tmp_dir:
scheherazade_url = "https://github.com/silnrsi/font-scheherazade/releases/download/v3.300/ScheherazadeNew-3.300.zip"
if download_and_extract(scheherazade_url, tmp_dir):
scheherazade_dir = os.path.join(tmp_dir, "ScheherazadeNew-3.300")
if os.path.exists(scheherazade_dir):
for file in os.listdir(scheherazade_dir):
if file.endswith('.ttf'):
src = os.path.join(scheherazade_dir, file)
dst = os.path.join(fonts_dir, file)
shutil.copy2(src, dst)
os.chmod(dst, 0o644)
print("✅ Scheherazade New font installed successfully")
else:
print("❌ Scheherazade New font directory not found")
else:
print("❌ Failed to download Scheherazade New font")
# Update font cache
print("🔄 Updating font cache...")
run_command("fc-cache -fv")
# Verify installation
print("✅ Verifying Arabic fonts installation...")
result = run_command("fc-list | grep -i 'amiri\\|scheherazade\\|noto.*arabic' | head -10")
if result:
print("Available Arabic fonts:")
print(result)
print("🎯 Arabic fonts setup completed!")
if __name__ == "__main__":
setup_arabic_fonts()