#!/usr/bin/env python3 """ 生成字体清单 JSON 文件 扫描 frontend/public/fonts/ 目录下的所有字体文件,生成 frontend/public/fonts.json """ import os import json from pathlib import Path def scan_fonts(font_dir='frontend/public/fonts'): """扫描字体目录,返回字体信息列表""" fonts = [] font_dir_path = Path(font_dir) if not font_dir_path.exists(): print(f"字体目录不存在: {font_dir}") return fonts # 递归遍历 fonts 目录(支持多级分类) for font_file in sorted(font_dir_path.rglob('*')): if not font_file.is_file(): continue 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(): """主函数""" # 扫描字体(唯一来源:frontend/public/fonts) fonts = scan_fonts('frontend/public/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()