Update images
This commit is contained in:
@@ -1,22 +1,31 @@
|
||||
"""Generate a markdown font preview page and images for SD font families."""
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import yaml
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent / 'assets' / 'sd-fonts'
|
||||
OUTPUT_DIR = Path(__file__).resolve().parent.parent / 'docs' / 'images' / 'sd-font-previews'
|
||||
MD_PATH = Path(__file__).resolve().parent.parent / 'docs' / 'sd-fonts-preview.md'
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
ROOT = REPO_ROOT / 'assets' / 'sd-fonts'
|
||||
OUTPUT_DIR = REPO_ROOT / 'docs' / 'images' / 'sd-font-previews'
|
||||
MD_PATH = REPO_ROOT / 'docs' / 'sd-fonts-preview.md'
|
||||
YAML_PATH = REPO_ROOT / 'lib' / 'EpdFont' / 'scripts' / 'sd-fonts.yaml'
|
||||
CACHE_DIR = Path(__file__).resolve().parent / 'font-cache'
|
||||
SAMPLE_TEXT = 'Everyone has the right to freedom of thought...'
|
||||
DEFAULT_FONT_SIZE = 28
|
||||
IMAGE_SIZE = (1080, 220)
|
||||
|
||||
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
||||
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
try:
|
||||
font = ImageFont.truetype('arial.ttf', DEFAULT_FONT_SIZE)
|
||||
except Exception:
|
||||
font = ImageFont.load_default()
|
||||
with YAML_PATH.open('r', encoding='utf-8') as yaml_file:
|
||||
manifest = yaml.safe_load(yaml_file)
|
||||
|
||||
family_map = {family['name']: family for family in manifest['families']}
|
||||
|
||||
families = [p.name for p in sorted(ROOT.iterdir()) if p.is_dir()]
|
||||
|
||||
lines = [
|
||||
'# SD Font Preview',
|
||||
'',
|
||||
@@ -26,11 +35,65 @@ lines = [
|
||||
'|---|---|',
|
||||
]
|
||||
|
||||
|
||||
def download_font(url: str, cache_dir: Path) -> Path:
|
||||
parsed = urllib.parse.urlparse(url)
|
||||
filename = Path(parsed.path).name
|
||||
output_path = cache_dir / filename
|
||||
if not output_path.exists():
|
||||
print(f'Downloading {url}...')
|
||||
urllib.request.urlretrieve(url, output_path)
|
||||
return output_path
|
||||
|
||||
|
||||
def resolve_instanced_font_path(family_name: str, style_name: str) -> Optional[Path]:
|
||||
instanced_dir = REPO_ROOT / 'lib' / 'EpdFont' / 'scripts' / 'instanced_fonts' / family_name
|
||||
if not instanced_dir.exists():
|
||||
return None
|
||||
matches = sorted(instanced_dir.glob(f'{style_name}*.ttf'))
|
||||
return matches[0] if matches else None
|
||||
|
||||
|
||||
def resolve_font_path(family_name: str) -> Optional[Path]:
|
||||
family = family_map.get(family_name)
|
||||
if family is None:
|
||||
return None
|
||||
|
||||
styles = family.get('styles', {})
|
||||
regular = styles.get('regular') or next(iter(styles.values()), None)
|
||||
if regular is None:
|
||||
return None
|
||||
|
||||
if 'path' in regular:
|
||||
local_path = REPO_ROOT / 'lib' / 'EpdFont' / regular['path']
|
||||
if local_path.exists():
|
||||
return local_path
|
||||
|
||||
instanced = resolve_instanced_font_path(family_name, 'regular')
|
||||
if instanced is not None:
|
||||
return instanced
|
||||
|
||||
if 'url' in regular:
|
||||
return download_font(regular['url'], CACHE_DIR)
|
||||
return None
|
||||
|
||||
|
||||
for family in families:
|
||||
font_path = resolve_font_path(family)
|
||||
try:
|
||||
if font_path is None:
|
||||
raise FileNotFoundError('No source font found')
|
||||
font = ImageFont.truetype(str(font_path), DEFAULT_FONT_SIZE)
|
||||
except Exception as exc:
|
||||
print(f'Warning: unable to load font for {family}: {exc}. Falling back to default font.')
|
||||
font = ImageFont.load_default()
|
||||
|
||||
text = f'{family} - {SAMPLE_TEXT}'
|
||||
image = Image.new('RGB', IMAGE_SIZE, color=(255, 255, 255))
|
||||
draw = ImageDraw.Draw(image)
|
||||
text_width, text_height = draw.textsize(text, font=font)
|
||||
bbox = draw.textbbox((0, 0), text, font=font)
|
||||
text_width = bbox[2] - bbox[0]
|
||||
text_height = bbox[3] - bbox[1]
|
||||
x = 20
|
||||
y = (IMAGE_SIZE[1] - text_height) // 2
|
||||
draw.text((x, y), text, fill='black', font=font)
|
||||
|
||||
Reference in New Issue
Block a user