feat: 不再自带字体文件

This commit is contained in:
Rock Chin 2023-03-05 19:36:09 +08:00
parent 74c018e271
commit 2668ef2b3f
3 changed files with 27 additions and 2 deletions

View File

@ -183,6 +183,12 @@ blob_message_threshold = 256
# - "forward": 将长消息转换为转发消息组件发送
blob_message_strategy = "forward"
# 文字转图片时使用的字体文件路径
# 当策略为"image"时生效
# 若在Windows系统下程序会自动使用Windows自带的微软雅黑字体
# 若未填写或不存在且不是Windows将禁用文字转图片功能改为使用转发消息组件
font_path = ""
# 消息处理超时重试次数
retry_times = 3

View File

@ -1,8 +1,27 @@
import logging
from PIL import Image, ImageDraw, ImageFont
import re
import os
import config
text_render_font = ImageFont.truetype("res/simhei.ttf", 32, encoding="utf-8")
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):
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"
def indexNumber(path=''):
@ -123,7 +142,7 @@ def text_to_image(text_str: str, save_as="temp.png", width=800):
else:
continue
# 准备画布
img = Image.new('RGBA', (width, max(280, len(final_lines) * 35 + 45)), (255, 255, 255, 255))
img = Image.new('RGBA', (width, max(280, len(final_lines) * 35 + 65)), (255, 255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGBA')

Binary file not shown.