84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
"""
|
|
System utility functions for the Text Texture Generator addon.
|
|
"""
|
|
|
|
import subprocess
|
|
import sys
|
|
import platform
|
|
import os
|
|
|
|
def install_pillow():
|
|
"""Install Pillow if not available"""
|
|
try:
|
|
import PIL
|
|
except ImportError:
|
|
print("Installing Pillow...")
|
|
subprocess.check_call([sys.executable, "-m", "pip", "install", "pillow>=10.0.0"])
|
|
|
|
def get_system_fonts():
|
|
"""Get detailed list of available system fonts with paths"""
|
|
try:
|
|
from PIL import ImageFont
|
|
import glob
|
|
|
|
fonts = {}
|
|
system = platform.system()
|
|
|
|
if system == "Windows":
|
|
font_dirs = [
|
|
"C:/Windows/Fonts/",
|
|
os.path.expanduser("~/AppData/Local/Microsoft/Windows/Fonts/")
|
|
]
|
|
extensions = ["*.ttf", "*.ttc", "*.otf"]
|
|
elif system == "Darwin": # macOS
|
|
font_dirs = [
|
|
"/System/Library/Fonts/",
|
|
"/Library/Fonts/",
|
|
os.path.expanduser("~/Library/Fonts/")
|
|
]
|
|
extensions = ["*.ttf", "*.ttc", "*.otf", "*.dfont"]
|
|
else: # Linux
|
|
font_dirs = [
|
|
"/usr/share/fonts/",
|
|
"/usr/local/share/fonts/",
|
|
os.path.expanduser("~/.local/share/fonts/"),
|
|
os.path.expanduser("~/.fonts/")
|
|
]
|
|
extensions = ["*.ttf", "*.ttc", "*.otf"]
|
|
|
|
for font_dir in font_dirs:
|
|
if os.path.exists(font_dir):
|
|
for ext in extensions:
|
|
for font_path in glob.glob(os.path.join(font_dir, "**", ext), recursive=True):
|
|
try:
|
|
font_name = os.path.splitext(os.path.basename(font_path))[0]
|
|
font_name = font_name.replace("-", " ").replace("_", " ")
|
|
fonts[font_name] = font_path
|
|
except Exception:
|
|
continue
|
|
|
|
return fonts
|
|
except ImportError:
|
|
return {}
|
|
|
|
def get_font_enum_items(self, context):
|
|
"""Dynamic enum items for font selection"""
|
|
items = [("default", "Default Font", "Use system default font", 0)]
|
|
|
|
if not hasattr(get_font_enum_items, 'cached_fonts'):
|
|
get_font_enum_items.cached_fonts = get_system_fonts()
|
|
|
|
fonts = get_font_enum_items.cached_fonts
|
|
|
|
for i, (font_name, font_path) in enumerate(sorted(fonts.items())[:50]):
|
|
items.append((font_path, font_name, f"Font: {font_name}", i + 1))
|
|
|
|
return items
|
|
|
|
def get_anchor_point_matrix():
|
|
"""Return a 3x3 matrix of anchor point identifiers for UI positioning grid"""
|
|
return [
|
|
['TOP_LEFT', 'TOP_CENTER', 'TOP_RIGHT'],
|
|
['MIDDLE_LEFT', 'MIDDLE_CENTER', 'MIDDLE_RIGHT'],
|
|
['BOTTOM_LEFT', 'BOTTOM_CENTER', 'BOTTOM_RIGHT']
|
|
] |