Spaces:
Sleeping
Sleeping
Upload install_fonts.py
Browse files- install_fonts.py +62 -0
install_fonts.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import subprocess
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
def install_fonts_from_repository():
|
7 |
+
"""
|
8 |
+
将仓库中的 fonts 目录下的所有字体文件复制到 ~/.fonts 目录,并刷新字体缓存。
|
9 |
+
"""
|
10 |
+
|
11 |
+
# 获取当前用户的主目录路径
|
12 |
+
home_dir = Path.home()
|
13 |
+
|
14 |
+
# 定义目标字体目录路径:~/.fonts
|
15 |
+
fonts_dir = home_dir / ".fonts"
|
16 |
+
|
17 |
+
# 如果 ~/.fonts 目录不存在,则创建该目录
|
18 |
+
if not fonts_dir.exists():
|
19 |
+
fonts_dir.mkdir(parents=True)
|
20 |
+
print(f"已创建字体目录:{fonts_dir}")
|
21 |
+
else:
|
22 |
+
print(f"字体目录已存在:{fonts_dir}")
|
23 |
+
|
24 |
+
# 定义仓库中的 fonts 目录路径
|
25 |
+
repo_fonts_dir = Path(__file__).parent / "fonts"
|
26 |
+
|
27 |
+
# 检查仓库中的 fonts 目录是否存在
|
28 |
+
if not repo_fonts_dir.exists():
|
29 |
+
print(f"错误:仓库中的 fonts 目录不存在:{repo_fonts_dir}")
|
30 |
+
return
|
31 |
+
|
32 |
+
# 支持的字体文件扩展名
|
33 |
+
font_extensions = [".ttf", ".otf"]
|
34 |
+
|
35 |
+
# 统计已复制的字体文件数量
|
36 |
+
copied_count = 0
|
37 |
+
|
38 |
+
# 遍历 fonts 目录中的所有文件
|
39 |
+
for font_file in repo_fonts_dir.iterdir():
|
40 |
+
if font_file.suffix.lower() in font_extensions:
|
41 |
+
destination = fonts_dir / font_file.name
|
42 |
+
try:
|
43 |
+
shutil.copy(font_file, destination)
|
44 |
+
print(f"已复制字体文件:{font_file.name} 到 {destination}")
|
45 |
+
copied_count += 1
|
46 |
+
except Exception as e:
|
47 |
+
print(f"复制字体文件时出错:{font_file.name},错误信息:{e}")
|
48 |
+
|
49 |
+
if copied_count == 0:
|
50 |
+
print("未找到任何可复制的字体文件。")
|
51 |
+
return
|
52 |
+
|
53 |
+
# 刷新字体缓存
|
54 |
+
try:
|
55 |
+
print("正在刷新字体缓存...")
|
56 |
+
subprocess.run(["fc-cache", "-f", "-v"], check=True)
|
57 |
+
print("字体缓存刷新完成。")
|
58 |
+
except subprocess.CalledProcessError as e:
|
59 |
+
print(f"刷新字体缓存时出错:{e}")
|
60 |
+
|
61 |
+
if __name__ == "__main__":
|
62 |
+
install_fonts_from_repository()
|