Files
font2pic/scripts/generate-fonts-json.py
2026-02-09 16:09:44 +08:00

164 lines
4.4 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Font2SVG - fonts.json 生成脚本
扫描 fonts/ 目录,生成字体清单
URL 格式https://fonts.biboer.cn/fonts/{category}/{fontname}.ttf
"""
import os
import json
from pathlib import Path
from urllib.parse import quote
# 配置
BASE_URL = "https://fonts.biboer.cn/fonts"
FONTS_DIR = Path(__file__).parent.parent / "fonts"
OUTPUT_FILE = Path(__file__).parent.parent / "frontend" / "public" / "fonts.json"
def scan_fonts(fonts_dir: Path) -> list:
"""
扫描字体目录,生成字体列表
Args:
fonts_dir: 字体根目录
Returns:
字体信息列表
"""
fonts = []
# 遍历所有分类目录
for category_dir in fonts_dir.iterdir():
if not category_dir.is_dir():
continue
category = category_dir.name
# 跳过隐藏目录
if category.startswith('.'):
continue
# 遍历分类下的所有字体文件
for font_file in category_dir.glob("*.ttf"):
# 获取文件信息
font_name = font_file.stem # 不含扩展名的文件名
file_size = font_file.stat().st_size
# 构建 URL需要编码中文路径
encoded_category = quote(category)
encoded_filename = quote(font_file.name)
url = f"{BASE_URL}/{encoded_category}/{encoded_filename}"
# 创建字体信息对象id 在后续统一按序号回填)
font_info = {
"id": "",
"name": font_name,
"category": category,
"path": url,
"size": file_size
}
fonts.append(font_info)
print(f"{category}/{font_name} ({file_size} bytes)")
return fonts
def sort_fonts(fonts: list) -> list:
"""
对字体列表排序
1. 按分类排序
2. 同分类内按名称排序
"""
sorted_fonts = sorted(fonts, key=lambda x: (x["category"], x["name"]))
for index, font in enumerate(sorted_fonts, start=1):
font["id"] = f"{index:04d}"
return sorted_fonts
def save_fonts_json(fonts: list, output_file: Path):
"""
保存 fonts.json
"""
# 确保输出目录存在
output_file.parent.mkdir(parents=True, exist_ok=True)
# 写入 JSON
with output_file.open("w", encoding="utf-8") as f:
json.dump(fonts, f, ensure_ascii=False, indent=2)
print(f"\n✓ fonts.json 已保存到: {output_file}")
print(f"✓ 共 {len(fonts)} 个字体")
def print_summary(fonts: list):
"""
打印统计信息
"""
# 按分类统计
categories = {}
total_size = 0
for font in fonts:
category = font["category"]
size = font["size"]
if category not in categories:
categories[category] = {"count": 0, "size": 0}
categories[category]["count"] += 1
categories[category]["size"] += size
total_size += size
print("\n" + "="*50)
print("字体统计信息")
print("="*50)
for category, stats in sorted(categories.items()):
size_mb = stats["size"] / 1024 / 1024
print(f"{category:20s} {stats['count']:3d} 个字体 {size_mb:8.2f} MB")
print("-"*50)
total_mb = total_size / 1024 / 1024
print(f"{'总计':20s} {len(fonts):3d} 个字体 {total_mb:8.2f} MB")
print("="*50)
def main():
print("="*50)
print("Font2SVG - fonts.json 生成工具")
print("目标域名: fonts.biboer.cn")
print("="*50)
print()
# 检查字体目录是否存在
if not FONTS_DIR.exists():
print(f"❌ 错误:字体目录不存在: {FONTS_DIR}")
return
print(f"扫描目录: {FONTS_DIR}")
print()
# 扫描字体
fonts = scan_fonts(FONTS_DIR)
if not fonts:
print("❌ 未找到任何字体文件")
return
# 排序
fonts = sort_fonts(fonts)
# 保存
save_fonts_json(fonts, OUTPUT_FILE)
# 统计信息
print_summary(fonts)
print()
print("下一步操作:")
print("1. 检查生成的 fonts.json 内容")
print("2. 运行部署脚本: bash scripts/deploy-fonts.sh")
print("3. 验证访问: curl https://fonts.biboer.cn/fonts.json")
if __name__ == "__main__":
main()