File size: 3,878 Bytes
623e14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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()