import os import shutil import subprocess from pathlib import Path def install_fonts_from_repository(): """ 将仓库中的 fonts 目录下的所有字体文件复制到 ~/.fonts 目录,并刷新字体缓存。 """ # 获取当前用户的主目录路径 home_dir = Path.home() # 定义目标字体目录路径:~/.fonts fonts_dir = home_dir / ".fonts" # 如果 ~/.fonts 目录不存在,则创建该目录 if not fonts_dir.exists(): fonts_dir.mkdir(parents=True) print(f"已创建字体目录:{fonts_dir}") else: print(f"字体目录已存在:{fonts_dir}") # 定义仓库中的 fonts 目录路径 repo_fonts_dir = Path(__file__).parent / "fonts" # 检查仓库中的 fonts 目录是否存在 if not repo_fonts_dir.exists(): print(f"错误:仓库中的 fonts 目录不存在:{repo_fonts_dir}") return # 支持的字体文件扩展名 font_extensions = [".ttf", ".otf"] # 统计已复制的字体文件数量 copied_count = 0 # 遍历 fonts 目录中的所有文件 for font_file in repo_fonts_dir.iterdir(): if font_file.suffix.lower() in font_extensions: destination = fonts_dir / font_file.name try: shutil.copy(font_file, destination) print(f"已复制字体文件:{font_file.name} 到 {destination}") copied_count += 1 except Exception as e: print(f"复制字体文件时出错:{font_file.name},错误信息:{e}") if copied_count == 0: print("未找到任何可复制的字体文件。") return # 刷新字体缓存 try: print("正在刷新字体缓存...") subprocess.run(["fc-cache", "-f", "-v"], check=True) print("字体缓存刷新完成。") except subprocess.CalledProcessError as e: print(f"刷新字体缓存时出错:{e}") if __name__ == "__main__": install_fonts_from_repository()