File size: 2,087 Bytes
6961b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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()