mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-28 00:14:21 +00:00
perf: 优化字体加载过程
This commit is contained in:
+21
-12
@@ -4,23 +4,32 @@ from PIL import Image, ImageDraw, ImageFont
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import config
|
import config
|
||||||
|
import traceback
|
||||||
|
|
||||||
use_font = config.font_path if hasattr(config, "font_path") else ""
|
if hasattr(config, "blob_message_strategy") and config.blob_message_strategy == "image": # 仅在启用了image时才加载字体
|
||||||
text_render_font: ImageFont = None
|
try:
|
||||||
|
use_font = config.font_path if hasattr(config, "font_path") else ""
|
||||||
|
text_render_font: ImageFont = None
|
||||||
|
|
||||||
# 检查是否存在
|
# 检查是否存在
|
||||||
if not os.path.exists(use_font):
|
|
||||||
# 若是windows系统,使用微软雅黑
|
|
||||||
if os.name == "nt":
|
|
||||||
use_font = "C:/Windows/Fonts/msyh.ttc"
|
|
||||||
if not os.path.exists(use_font):
|
if not os.path.exists(use_font):
|
||||||
logging.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。")
|
# 若是windows系统,使用微软雅黑
|
||||||
config.blob_message_strategy = "forward"
|
if os.name == "nt":
|
||||||
|
use_font = "C:/Windows/Fonts/msyh.ttc"
|
||||||
|
if not os.path.exists(use_font):
|
||||||
|
logging.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。")
|
||||||
|
config.blob_message_strategy = "forward"
|
||||||
|
else:
|
||||||
|
logging.info("使用Windows自带字体:" + use_font)
|
||||||
|
text_render_font = ImageFont.truetype(use_font, 32, encoding="utf-8")
|
||||||
|
else:
|
||||||
|
logging.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。")
|
||||||
|
config.blob_message_strategy = "forward"
|
||||||
else:
|
else:
|
||||||
logging.info("使用Windows自带字体:" + use_font)
|
|
||||||
text_render_font = ImageFont.truetype(use_font, 32, encoding="utf-8")
|
text_render_font = ImageFont.truetype(use_font, 32, encoding="utf-8")
|
||||||
else:
|
except:
|
||||||
logging.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。")
|
traceback.print_exc()
|
||||||
|
logging.error("加载字体文件失败({}),更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。".format(use_font))
|
||||||
config.blob_message_strategy = "forward"
|
config.blob_message_strategy = "forward"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user