fix:macos fonts runtime erros

This commit is contained in:
gold3bear
2023-12-12 21:16:51 +08:00
parent 8f60c81ef3
commit 1188b9d3bc
+10 -11
View File
@@ -2,32 +2,31 @@ import os
# FONT_PATH= os.path.abspath(os.path.join(os.path.dirname(__file__),'../assets/王汉宗颜楷体繁.ttf'))
import matplotlib.font_manager as fm
def get_font_files(directory):
font_files = {}
# 从指定目录加载字体
for file in os.listdir(directory):
if file.endswith('.ttf') or file.endswith('.otf'):
font_name = os.path.splitext(file)[0]
font_path = os.path.join(directory, file)
font_files[font_name] = os.path.abspath(font_path)
# 尝试获取系统字体
try:
import sys
if sys.platform != "darwin":
import matplotlib.font_manager as fm
font_paths = fm.findSystemFonts()
for path in font_paths:
font_paths = fm.findSystemFonts()
for path in font_paths:
try:
font_prop = fm.FontProperties(fname=path)
font_name = font_prop.get_name()
font_files[font_name] = path
except ValueError:
print("findSystemFonts error")
except Exception as e:
print(f"Error processing font {path}: {e}")
except Exception as e:
print(f"Error finding system fonts: {e}")
return font_files
r_directory = os.path.join(os.path.dirname(__file__), '../assets/')