update at 2026-02-07 13:32:31

This commit is contained in:
douboer
2026-02-07 13:32:31 +08:00
parent b0e89a56e1
commit 951eda9c58
6 changed files with 73 additions and 79 deletions

View File

@@ -1,14 +1,14 @@
#!/usr/bin/env python3
"""
生成字体清单 JSON 文件
扫描 font/ 目录下的所有字体文件,生成一个 JSON 文件供前端使用
扫描 frontend/public/fonts/ 目录下的所有字体文件,生成 frontend/public/fonts.json
"""
import os
import json
from pathlib import Path
def scan_fonts(font_dir='font'):
def scan_fonts(font_dir='frontend/public/fonts'):
"""扫描字体目录,返回字体信息列表"""
fonts = []
font_dir_path = Path(font_dir)
@@ -17,35 +17,37 @@ def scan_fonts(font_dir='font'):
print(f"字体目录不存在: {font_dir}")
return fonts
# 遍历所有子目录
for category_dir in sorted(font_dir_path.iterdir()):
if not category_dir.is_dir():
# 递归遍历 fonts 目录(支持多级分类)
for font_file in sorted(font_dir_path.rglob('*')):
if not font_file.is_file():
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)
if font_file.suffix.lower() not in ['.ttf', '.otf']:
continue
relative_parent = font_file.parent.relative_to(font_dir_path)
category_name = str(relative_parent).replace('\\', '/')
if category_name == '.':
category_name = '未分类'
relative_path = font_file.relative_to(font_dir_path).as_posix()
# 生成字体信息
font_info = {
'id': f"{category_name}/{font_file.stem}",
'name': font_file.stem,
'filename': font_file.name,
'category': category_name,
'path': f"/fonts/{relative_path}",
}
fonts.append(font_info)
return fonts
def main():
"""主函数"""
# 扫描字体
fonts = scan_fonts()
# 扫描字体唯一来源frontend/public/fonts
fonts = scan_fonts('frontend/public/fonts')
print(f"找到 {len(fonts)} 个字体文件")