73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
生成字体清单 JSON 文件
|
|
扫描 font/ 目录下的所有字体文件,生成一个 JSON 文件供前端使用
|
|
"""
|
|
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
|
|
def scan_fonts(font_dir='font'):
|
|
"""扫描字体目录,返回字体信息列表"""
|
|
fonts = []
|
|
font_dir_path = Path(font_dir)
|
|
|
|
if not font_dir_path.exists():
|
|
print(f"字体目录不存在: {font_dir}")
|
|
return fonts
|
|
|
|
# 遍历所有子目录
|
|
for category_dir in sorted(font_dir_path.iterdir()):
|
|
if not category_dir.is_dir():
|
|
continue
|
|
|
|
category_name = category_dir.name
|
|
|
|
# 遍历类别下的所有字体文件
|
|
for font_file in sorted(category_dir.iterdir()):
|
|
if font_file.suffix.lower() not in ['.ttf', '.otf']:
|
|
continue
|
|
|
|
# 生成字体信息
|
|
font_info = {
|
|
'id': f"{category_name}/{font_file.stem}",
|
|
'name': font_file.stem,
|
|
'filename': font_file.name,
|
|
'category': category_name,
|
|
'path': f"/fonts/{category_name}/{font_file.name}",
|
|
}
|
|
|
|
fonts.append(font_info)
|
|
|
|
return fonts
|
|
|
|
def main():
|
|
"""主函数"""
|
|
# 扫描字体
|
|
fonts = scan_fonts()
|
|
|
|
print(f"找到 {len(fonts)} 个字体文件")
|
|
|
|
# 保存到 JSON 文件
|
|
output_file = 'frontend/public/fonts.json'
|
|
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
json.dump(fonts, f, ensure_ascii=False, indent=2)
|
|
|
|
print(f"字体清单已保存到: {output_file}")
|
|
|
|
# 统计信息
|
|
categories = {}
|
|
for font in fonts:
|
|
category = font['category']
|
|
categories[category] = categories.get(category, 0) + 1
|
|
|
|
print("\n按类别统计:")
|
|
for category, count in sorted(categories.items()):
|
|
print(f" {category}: {count} 个字体")
|
|
|
|
if __name__ == '__main__':
|
|
main()
|