From 1188b9d3bc3bb51e1fc564c2430dc39dd102afb8 Mon Sep 17 00:00:00 2001 From: gold3bear Date: Tue, 12 Dec 2023 21:16:51 +0800 Subject: [PATCH] fix:macos fonts runtime erros --- nodes/Utils.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/nodes/Utils.py b/nodes/Utils.py index 23b0470..d34a3e6 100644 --- a/nodes/Utils.py +++ b/nodes/Utils.py @@ -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/')