Compare commits

...

6 Commits

Author SHA1 Message Date
molanp
42842f6a4d
Merge d32a6fbdd4 into a63f26c3b6 2025-08-22 16:35:20 +08:00
HibiKier
a63f26c3b6
增强插件商店功能,支持添加插件时指定源类型。 (#2028)
Some checks failed
检查bot是否运行正常 / bot check (push) Has been cancelled
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Has been cancelled
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, python) (push) Has been cancelled
Sequential Lint and Type Check / ruff-call (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Force Sync to Aliyun / sync (push) Has been cancelled
Update Version / update-version (push) Has been cancelled
Sequential Lint and Type Check / pyright-call (push) Has been cancelled
2025-08-21 11:08:34 +08:00
molanp
d32a6fbdd4
回滚orm版本因为包含破坏性更新 2025-08-08 12:34:52 +08:00
pre-commit-ci[bot]
e954009439 🚨 auto fix by pre-commit hooks 2025-08-07 17:20:39 +00:00
molanp
59507711e8 refactor(zhenxun/utils): 优化代码类型注释和参数类型
- 在 text_size 方法中,将 text 参数类型从 str 改为任意类型,以适应更多输入
- 在 resize 方法中,将 width 和 height 参数类型从 int 改为 float,增加灵活性
- 优化部分代码的类型注释,提高代码可读性和维护性
2025-08-08 01:20:10 +08:00
molanp
86c1165c12 build(deps): 更新项目依赖版本
- 将多个依赖的版本从具体的版本号改为使用 >= 操作符,以允许安装更高版本来获得安全更新修复
- 调整 bilireq 的版本从 0.2.3post0 到 >=0.2.10
- 修改 nonebot-plugin-alconna 的版本从 ^0.54.0 到 >=0.56.0
- 变更 nonebot-plugin-uninfo 的版本从 >0.4.1 到 >=0.7.3
- pillow 的版本从 ^10.0.0 改为 >=10.0.0
- nb-cli 的版本从 ^1.3.0 改为 >=1.3.0
- nonebot2 的版本从 ^2.3.3 改为 >=2.3.3
- ujson 的版本从 ^5.9.0 改为 >=5.9.0
- pypinyin 的版本从 ^0.51.0 改为 >=0.51.0
- tortoise-orm 的版本从 ^0.20.0 改为 >=0.20.0
- nonebot-plugin-session 的版本从 ^0.2.3 到 ^0.3.2
- 对 `_build_image` 文件进行兼容性适配
2025-08-08 00:59:32 +08:00
8 changed files with 1154 additions and 940 deletions

1673
poetry.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -14,21 +14,21 @@ priority = "primary"
[tool.poetry.dependencies]
python = "^3.10"
playwright = "^1.41.1"
nonebot-adapter-onebot = "^2.3.1"
nonebot-adapter-onebot = ">=2.3.1"
nonebot-plugin-apscheduler = "^0.5"
tortoise-orm = "^0.20.0"
cattrs = "^23.2.3"
ruamel-yaml = "^0.18.5"
strenum = "^0.4.15"
nonebot-plugin-session = "^0.2.3"
ujson = "^5.9.0"
nb-cli = "^1.3.0"
nonebot2 = { extras = ["fastapi"], version = "^2.3.3" }
pillow = "^10.0.0"
nonebot-plugin-session = "^0.3.2"
ujson = ">=5.9.0"
nb-cli = ">=1.3.0"
nonebot2 = { extras = ["fastapi"], version = ">=2.3.3" }
pillow = ">=10.0.0"
retrying = "^1.3.4"
aiofiles = "^23.2.1"
nonebot-plugin-htmlrender = ">=0.6.0,<1.0.0"
pypinyin = "^0.51.0"
pypinyin = ">=0.51.0"
beautifulsoup4 = "^4.12.3"
lxml = "^5.1.0"
psutil = "^5.9.8"
@ -36,14 +36,14 @@ feedparser = "^6.0.11"
imagehash = "^4.3.1"
cn2an = "^0.5.22"
dateparser = "^1.2.0"
bilireq = "0.2.3post0"
bilireq = ">=0.2.10"
python-jose = { extras = ["cryptography"], version = "^3.3.0" }
python-multipart = "^0.0.9"
aiocache = {extras = ["redis"], version = "^0.12.3"}
py-cpuinfo = "^9.0.0"
nonebot-plugin-alconna = "^0.54.0"
nonebot-plugin-alconna = ">=0.56.0"
tenacity = "^9.0.0"
nonebot-plugin-uninfo = ">0.4.1"
nonebot-plugin-uninfo = ">=0.7.3"
nonebot-plugin-waiter = "^0.8.1"
multidict = ">=6.0.0,!=6.3.2"

View File

@ -1,6 +1,6 @@
from nonebot.permission import SUPERUSER
from nonebot.plugin import PluginMetadata
from nonebot_plugin_alconna import Alconna, Args, Subcommand, on_alconna
from nonebot_plugin_alconna import Alconna, Args, Match, Option, Subcommand, on_alconna
from nonebot_plugin_session import EventSession
from zhenxun.configs.utils import PluginExtraData
@ -16,11 +16,16 @@ __plugin_meta__ = PluginMetadata(
description="插件商店",
usage="""
插件商店 : 查看当前的插件商店
添加插件 id or module : 添加插件
移除插件 id or module : 移除插件
搜索插件 name or author : 搜索插件
更新插件 id or module : 更新插件
添加插件 id或module或插件名称 ?[-s [git, ali]]: 添加插件
使用-s时指定源git为githubali为阿里云
移除插件 id或module: 移除插件
搜索插件 name或author: 搜索插件
更新插件 id或module: 更新插件
更新全部插件 : 更新全部插件
示例
添加插件 pix
添加插件 真寻日报 -s git
""".strip(),
extra=PluginExtraData(
author="HibiKier",
@ -32,7 +37,11 @@ __plugin_meta__ = PluginMetadata(
_matcher = on_alconna(
Alconna(
"插件商店",
Subcommand("add", Args["plugin_id", str]),
Subcommand(
"add",
Args["plugin_id", str],
Option("-s", Args["source", str]),
),
Subcommand("remove", Args["plugin_id", str]),
Subcommand("search", Args["plugin_name_or_author", str]),
Subcommand("update", Args["plugin_id", str]),
@ -84,7 +93,6 @@ async def _(session: EventSession):
try:
result = await StoreManager.get_plugins_info()
logger.info("查看插件列表", "插件商店", session=session)
await MessageUtils.build_message([*result]).send()
except Exception as e:
logger.error(f"查看插件列表失败 e: {e}", "插件商店", session=session, e=e)
@ -92,13 +100,18 @@ async def _(session: EventSession):
@_matcher.assign("add")
async def _(session: EventSession, plugin_id: str):
async def _(session: EventSession, plugin_id: str, source: Match[str]):
if is_number(plugin_id):
await MessageUtils.build_message(f"正在添加插件 Id: {plugin_id}").send()
else:
await MessageUtils.build_message(f"正在添加插件 Module: {plugin_id}").send()
source_str = source.result if source.available else None
if source_str and source_str not in ["ali", "git"]:
await MessageUtils.build_message(
f"源类型错误: {source_str} 请使用 ali 或 git"
).finish()
try:
if is_number(plugin_id):
await MessageUtils.build_message(f"正在添加插件 Id: {plugin_id}").send()
else:
await MessageUtils.build_message(f"正在添加插件 Module: {plugin_id}").send()
result = await StoreManager.add_plugin(plugin_id)
result = await StoreManager.add_plugin(plugin_id, source_str)
except Exception as e:
logger.error(f"添加插件 Id: {plugin_id}失败", "插件商店", session=session, e=e)
await MessageUtils.build_message(

View File

@ -5,14 +5,12 @@ import shutil
from aiocache import cached
import ujson as json
from zhenxun import ui
from zhenxun.builtin_plugins.plugin_store.models import StorePluginInfo
from zhenxun.configs.path_config import TEMP_PATH
from zhenxun.models.plugin_info import PluginInfo
from zhenxun.services.log import logger
from zhenxun.services.plugin_init import PluginInitManager
from zhenxun.ui.builders import TableBuilder
from zhenxun.ui.models import StatusBadgeCell, TextCell
from zhenxun.utils.image_utils import BuildImage, ImageTemplate, RowStyle
from zhenxun.utils.manager.virtual_env_package_manager import VirtualEnvPackageManager
from zhenxun.utils.repo_utils import RepoFileManager
from zhenxun.utils.repo_utils.models import RepoFileInfo, RepoType
@ -27,6 +25,22 @@ from .config import (
from .exceptions import PluginStoreException
def row_style(column: str, text: str) -> RowStyle:
"""被动技能文本风格
参数:
column: 表头
text: 文本内容
返回:
RowStyle: RowStyle
"""
style = RowStyle()
if column == "-" and text == "已安装":
style.font_color = "#67C23A"
return style
class StoreManager:
@classmethod
@cached(60)
@ -91,123 +105,61 @@ class StoreManager:
return await PluginInfo.filter(load_status=True).values_list(*args)
@classmethod
async def get_plugins_info(cls) -> list[bytes] | str:
async def get_plugins_info(cls) -> list[BuildImage] | str:
"""插件列表
返回:
bytes | str: 返回消息
BuildImage | str: 返回消息
"""
plugin_list, extra_plugin_list = await cls.get_data()
column_name = ["-", "ID", "名称", "简介", "作者", "版本", "类型"]
db_plugin_list = await cls.get_loaded_plugins("module", "version")
suc_plugin = {p[0]: (p[1] or "0.1") for p in db_plugin_list}
HIGHLIGHT_COLOR = "#E6A23C"
structured_native_list = []
structured_extra_list = []
index = 0
data_list = []
extra_data_list = []
for plugin_info in plugin_list:
is_new = cls.check_version_is_new(plugin_info, suc_plugin)
structured_native_list.append(
{
"is_installed": plugin_info.module in suc_plugin,
"id": index,
"name": plugin_info.name,
"description": plugin_info.description,
"author": plugin_info.author,
"version_str": cls.version_check(plugin_info, suc_plugin),
"type_name": plugin_info.plugin_type_name,
"has_update": not is_new and plugin_info.module in suc_plugin,
}
data_list.append(
[
"已安装" if plugin_info.module in suc_plugin else "",
index,
plugin_info.name,
plugin_info.description,
plugin_info.author,
cls.version_check(plugin_info, suc_plugin),
plugin_info.plugin_type_name,
]
)
index += 1
for plugin_info in extra_plugin_list:
is_new = cls.check_version_is_new(plugin_info, suc_plugin)
structured_extra_list.append(
{
"is_installed": plugin_info.module in suc_plugin,
"id": index,
"name": plugin_info.name,
"description": plugin_info.description,
"author": plugin_info.author,
"version_str": cls.version_check(plugin_info, suc_plugin),
"type_name": plugin_info.plugin_type_name,
"has_update": not is_new and plugin_info.module in suc_plugin,
}
extra_data_list.append(
[
"已安装" if plugin_info.module in suc_plugin else "",
index,
plugin_info.name,
plugin_info.description,
plugin_info.author,
cls.version_check(plugin_info, suc_plugin),
plugin_info.plugin_type_name,
]
)
index += 1
native_table_builder = TableBuilder(
title="原生插件列表", tip="通过添加/移除插件 ID 来管理插件"
).set_headers(column_name)
native_rows_data = []
for row_data in structured_native_list:
row_color = HIGHLIGHT_COLOR if row_data["has_update"] else None
status_cell = (
StatusBadgeCell(text="已安装", status_type="ok")
if row_data["is_installed"]
else TextCell(content="")
)
native_rows_data.append(
[
status_cell,
TextCell(content=str(row_data["id"]), color=row_color),
TextCell(content=row_data["name"], color=row_color),
TextCell(content=row_data["description"], color=row_color),
TextCell(content=row_data["author"], color=row_color),
TextCell(
content=row_data["version_str"],
color=row_color,
bold=bool(row_color),
),
TextCell(content=row_data["type_name"], color=row_color),
]
)
native_table_builder.add_rows(native_rows_data)
native_table_bytes = await ui.render(
native_table_builder.build(),
viewport={"width": 1400, "height": 10},
device_scale_factor=2,
)
extra_table_builder = TableBuilder(
title="第三方插件列表", tip="通过添加/移除插件 ID 来管理插件"
).set_headers(column_name)
extra_rows_data = []
for row_data in structured_extra_list:
row_color = HIGHLIGHT_COLOR if row_data["has_update"] else None
status_cell = (
StatusBadgeCell(text="已安装", status_type="ok")
if row_data["is_installed"]
else TextCell(content="")
)
extra_rows_data.append(
[
status_cell,
TextCell(content=str(row_data["id"]), color=row_color),
TextCell(content=row_data["name"], color=row_color),
TextCell(content=row_data["description"], color=row_color),
TextCell(content=row_data["author"], color=row_color),
TextCell(
content=row_data["version_str"],
color=row_color,
bold=bool(row_color),
),
TextCell(content=row_data["type_name"], color=row_color),
]
)
extra_table_builder.add_rows(extra_rows_data)
extra_table_bytes = await ui.render(
extra_table_builder.build(),
viewport={"width": 1400, "height": 10},
device_scale_factor=2,
)
return [native_table_bytes, extra_table_bytes]
return [
await ImageTemplate.table_page(
"原生插件列表",
"通过添加/移除插件 ID 来管理插件",
column_name,
data_list,
text_style=row_style,
),
await ImageTemplate.table_page(
"第三方插件列表",
"通过添加/移除插件 ID 来管理插件",
column_name,
extra_data_list,
text_style=row_style,
),
]
@classmethod
async def get_plugin_by_value(
@ -267,7 +219,7 @@ class StoreManager:
return plugin_info, is_external
@classmethod
async def add_plugin(cls, index_or_module: str) -> str:
async def add_plugin(cls, index_or_module: str, source: str | None) -> str:
"""添加插件
参数:
@ -289,6 +241,7 @@ class StoreManager:
plugin_info.module_path,
plugin_info.is_dir,
is_external,
source,
)
return f"插件 {plugin_info.name} 安装成功! 重启后生效"
@ -299,6 +252,7 @@ class StoreManager:
module_path: str,
is_dir: bool,
is_external: bool = False,
source: str | None = None,
):
"""安装插件
@ -309,6 +263,10 @@ class StoreManager:
is_external: 是否是外部仓库
"""
repo_type = RepoType.GITHUB if is_external else None
if source == "ali":
repo_type = RepoType.ALIYUN
elif source == "git":
repo_type = RepoType.GITHUB
replace_module_path = module_path.replace(".", "/")
if is_dir:
files = await RepoFileManager.list_directory_files(
@ -338,6 +296,7 @@ class StoreManager:
await VirtualEnvPackageManager.install_requirement(requirement_file)
if not is_install_req:
# 从仓库根目录查找文件
rand = random.randint(1, 10000)
requirement_path = TEMP_PATH / f"plugin_store_{rand}_req.txt"
requirements_path = TEMP_PATH / f"plugin_store_{rand}_reqs.txt"
@ -392,20 +351,19 @@ class StoreManager:
return f"插件 {plugin_info.name} 移除成功! 重启后生效"
@classmethod
async def search_plugin(cls, plugin_name_or_author: str) -> bytes | str:
async def search_plugin(cls, plugin_name_or_author: str) -> BuildImage | str:
"""搜索插件
参数:
plugin_name_or_author: 插件名称或作者
返回:
bytes | str: 返回消息
BuildImage | str: 返回消息
"""
plugin_list, extra_plugin_list = await cls.get_data()
all_plugin_list = plugin_list + extra_plugin_list
db_plugin_list = await cls.get_loaded_plugins("module", "version")
suc_plugin = {p[0]: (p[1] or "Unknown") for p in db_plugin_list}
filtered_data = [
(id, plugin_info)
for id, plugin_info in enumerate(all_plugin_list)
@ -413,50 +371,28 @@ class StoreManager:
or plugin_name_or_author.lower() in plugin_info.author.lower()
]
if not filtered_data:
data_list = [
[
"已安装" if plugin_info.module in suc_plugin else "",
id,
plugin_info.name,
plugin_info.description,
plugin_info.author,
cls.version_check(plugin_info, suc_plugin),
plugin_info.plugin_type_name,
]
for id, plugin_info in filtered_data
]
if not data_list:
return "未找到相关插件..."
HIGHLIGHT_COLOR = "#E6A23C"
column_name = ["-", "ID", "名称", "简介", "作者", "版本", "类型"]
builder = TableBuilder(
title=f"插件搜索结果: '{plugin_name_or_author}'",
tip="通过添加/移除插件 ID 来管理插件",
return await ImageTemplate.table_page(
"商店插件列表",
"通过添加/移除插件 ID 来管理插件",
column_name,
data_list,
text_style=row_style,
)
builder.set_headers(column_name)
rows_to_add = []
for id, plugin_info in filtered_data:
is_new = cls.check_version_is_new(plugin_info, suc_plugin)
has_update = not is_new and plugin_info.module in suc_plugin
row_color = HIGHLIGHT_COLOR if has_update else None
status_cell = (
StatusBadgeCell(text="已安装", status_type="ok")
if plugin_info.module in suc_plugin
else TextCell(content="")
)
rows_to_add.append(
[
status_cell,
TextCell(content=str(id), color=row_color),
TextCell(content=plugin_info.name, color=row_color),
TextCell(content=plugin_info.description, color=row_color),
TextCell(content=plugin_info.author, color=row_color),
TextCell(
content=cls.version_check(plugin_info, suc_plugin),
color=row_color,
bold=has_update,
),
TextCell(content=plugin_info.plugin_type_name, color=row_color),
]
)
builder.add_rows(rows_to_add)
render_viewport = {"width": 1400, "height": 10}
return await ui.render(builder.build(), viewport=render_viewport)
@classmethod
async def update_plugin(cls, index_or_module: str) -> str:

View File

@ -42,8 +42,8 @@ class BuildImage:
def __init__(
self,
width: int = 0,
height: int = 0,
width: float = 0,
height: float = 0,
color: ColorAlias = (255, 255, 255),
mode: ModeType = "RGBA",
font: str | Path | FreeTypeFont = "HYWenHei-85W.ttf",
@ -63,12 +63,14 @@ class BuildImage:
else:
self.markImg = Image.open(background)
if width and height:
self.markImg = self.markImg.resize((width, height), Resampling.LANCZOS)
self.markImg = self.markImg.resize(
(int(width), int(height)), Resampling.LANCZOS
)
else:
self.width = self.markImg.width
self.height = self.markImg.height
elif width and height:
self.markImg = Image.new(mode, (width, height), color) # type: ignore
self.markImg = Image.new(mode, (int(width), int(height)), color)
else:
raise ValueError("长度和宽度不能为空...")
self.draw = ImageDraw.Draw(self.markImg)
@ -222,7 +224,7 @@ class BuildImage:
text: str,
font: str | FreeTypeFont | None = "HYWenHei-85W.ttf",
font_size: int = 10,
) -> tuple[int, int]: # sourcery skip: remove-unnecessary-cast
) -> tuple[float, float]:
"""获取该字体下文本需要的长宽
参数:
@ -231,20 +233,20 @@ class BuildImage:
font_size: 字体大小
返回:
tuple[int, int]: 长宽
tuple[float, float]: 长宽
"""
_font = font
if font and type(font) is str:
_font = cls.load_font(font, font_size)
temp_image = Image.new("RGB", (1, 1), (255, 255, 255))
draw = ImageDraw.Draw(temp_image)
text_box = draw.textbbox((0, 0), str(text), font=_font) # type: ignore
text_box = draw.textbbox((0, 0), text, font=_font) # pyright: ignore[reportArgumentType]
text_width = text_box[2] - text_box[0]
text_height = text_box[3] - text_box[1]
return text_width, text_height + 10
# return _font.getsize(str(text)) # type: ignore
def getsize(self, msg: str) -> tuple[int, int]:
def getsize(self, msg: str) -> tuple[float, float]:
# sourcery skip: remove-unnecessary-cast
"""
获取文字在该图片 font_size 下所需要的空间
@ -253,7 +255,7 @@ class BuildImage:
msg: 文本
返回:
tuple[int, int]: 长宽
tuple[float, float]: 长宽
"""
temp_image = Image.new("RGB", (1, 1), (255, 255, 255))
draw = ImageDraw.Draw(temp_image)
@ -265,9 +267,9 @@ class BuildImage:
def __center_xy(
self,
pos: tuple[int, int],
width: int,
height: int,
pos: tuple[float, float],
width: float,
height: float,
center_type: CenterType | None,
) -> tuple[int, int]:
"""
@ -284,21 +286,21 @@ class BuildImage:
# _width, _height = pos
if self.width and self.height:
if center_type == "center":
width = int((self.width - width) / 2)
height = int((self.height - height) / 2)
width = (self.width - width) / 2
height = (self.height - height) / 2
elif center_type == "width":
width = int((self.width - width) / 2)
width = (self.width - width) / 2
height = pos[1]
elif center_type == "height":
width = pos[0]
height = int((self.height - height) / 2)
return width, height
height = (self.height - height) / 2
return int(width), int(height)
@run_sync
def paste(
self,
image: Self | tImage,
pos: tuple[int, int] = (0, 0),
pos: tuple[float, float] = (0, 0),
center_type: CenterType | None = None,
) -> Self:
"""贴图
@ -370,7 +372,7 @@ class BuildImage:
@run_sync
def text(
self,
pos: tuple[int, int],
pos: tuple[float, float],
text: str,
fill: str | tuple[int, int, int] = (0, 0, 0),
center_type: CenterType | None = None,
@ -430,7 +432,7 @@ class BuildImage:
self.markImg.show()
@run_sync
def resize(self, ratio: float = 0, width: int = 0, height: int = 0) -> Self:
def resize(self, ratio: float = 0, width: float = 0, height: float = 0) -> Self:
"""
压缩图片
@ -451,13 +453,13 @@ class BuildImage:
if not width and not height:
width = int(self.width * ratio)
height = int(self.height * ratio)
self.markImg = self.markImg.resize((width, height), Image.LANCZOS) # type: ignore
self.markImg = self.markImg.resize((int(width), int(height)), Image.LANCZOS) # type: ignore
self.width, self.height = self.markImg.size
self.draw = ImageDraw.Draw(self.markImg)
return self
@run_sync
def crop(self, box: tuple[int, int, int, int]) -> Self:
def crop(self, box: tuple[float, float, float, float]) -> Self:
"""
裁剪图片
@ -580,7 +582,7 @@ class BuildImage:
@run_sync
def line(
self,
xy: tuple[int, int, int, int],
xy: tuple[float, float, float, float],
fill: tuple[int, int, int] | str = "#D8DEE4",
width: int = 1,
) -> Self:

View File

@ -321,8 +321,7 @@ class BuildMat:
if not self.build_data.y_index:
"""没有指定y_index时使用data自动生成"""
max_num = max(self.build_data.data)
if max_num < 5:
max_num = 5
max_num = max(max_num, 5)
s = int(max_num / 5)
_y_index = [max_num]
for _n in range(4):
@ -334,23 +333,20 @@ class BuildMat:
# _tmp = ["_" for _ in range(len(_y_index) - 1)]
# _tmp.append(str(_y_index[0]))
# _y_index = _tmp
self.build_data.y_index = _y_index # type: ignore
self.build_data.y_index = _y_index
for item in self.build_data.y_index:
text_size = BuildImage.get_text_size(str(item), font)
if text_size[0] > padding_width:
padding_width = text_size[0]
y_height_list.append(text_size)
if self.build_data.mat_type == MatType.BARH:
_tmp = x_width_list
x_width_list = y_height_list
y_height_list = _tmp
x_width_list, y_height_list = y_height_list, x_width_list
old_space = self.build_data.space
width = padding_width * 2 + self.build_data.space[0] * 2 + 20
height = (
sum([h[1] + self.build_data.space[1] for h in y_height_list])
sum(h[1] + self.build_data.space[1] for h in y_height_list)
+ self.build_data.space[1] * 2
+ 30
)
) + 30
_x_index = self.build_data.x_index
_y_index = self.build_data.y_index
_barh_max_text_width = 0
@ -376,7 +372,7 @@ class BuildMat:
width += self.build_data.space[0] * (len(_x_index) - 1)
else:
"""非横向柱状图时加字体宽度"""
width += sum([w[0] + self.build_data.space[0] for w in x_width_list])
width += sum(w[0] + self.build_data.space[0] for w in x_width_list)
A = BuildImage(
width + 5,

View File

@ -229,8 +229,8 @@ class ImageTemplate:
async def __build_text_image(
cls,
text: str,
width: int,
height: int,
width: float,
height: float,
font: FreeTypeFont,
font_color: str | tuple[int, int, int] = (0, 0, 0),
color: str | tuple[int, int, int] = (255, 255, 255),

View File

@ -65,13 +65,13 @@ async def text2image(
top_padding = padding[0]
left_padding = padding[1]
_font = BuildImage.load_font(font, font_size)
image_list = []
if auto_parse and re.search(r"<f(.*)>(.*)</f>", text):
_data = []
new_text = ""
placeholder_index = 0
for s in text.split("</f>"):
r = re.search(r"<f(.*)>(.*)", s)
if r:
if r := re.search(r"<f(.*)>(.*)", s):
start, end = r.span()
if start != 0 and (t := s[:start]):
new_text += t
@ -79,14 +79,13 @@ async def text2image(
[
(start, end),
f"[placeholder_{placeholder_index}]",
r.group(1).strip(),
r.group(2),
r[1].strip(),
r[2],
]
)
new_text += f"[placeholder_{placeholder_index}]"
placeholder_index += 1
new_text += text.split("</f>")[-1]
image_list = []
current_placeholder_index = 0
# 切分换行,每行为单张图片
for s in new_text.split("\n"):
@ -97,12 +96,9 @@ async def text2image(
for _ in range(s.count("[placeholder_")):
placeholder = _data[_tmp_index]
if "font_size" in placeholder[2]:
r = re.search(r"font_size=['\"]?(\d+)", placeholder[2])
if r:
w, h = BuildImage.get_text_size(
placeholder[3], font, int(r.group(1))
)
img_height = img_height if img_height > h else h
if r := re.search(r"font_size=['\"]?(\d+)", placeholder[2]):
w, h = BuildImage.get_text_size(placeholder[3], font, int(r[1]))
img_height = max(img_height, h)
img_width += w
else:
img_width += BuildImage.get_text_size(placeholder[3], _font)[0]
@ -135,10 +131,8 @@ async def text2image(
_font = e.split("=")[-1]
if e.startswith("font_size=") or e.startswith("fs="):
_font_size = int(e.split("=")[-1])
if _font_size > 1000:
_font_size = 1000
if _font_size < 1:
_font_size = 1
_font_size = min(_font_size, 1000)
_font_size = max(_font_size, 1)
if e.startswith("font_color") or e.startswith("fc="):
_font_color = e.split("=")[-1]
text_img = await BuildImage.build_text_image(
@ -167,7 +161,7 @@ async def text2image(
width = 0
for img in image_list:
height += img.h
width = width if width > img.w else img.w
width = max(width, img.w)
width += pw
height += ph
A = BuildImage(width + left_padding, height + top_padding, color=color)
@ -179,12 +173,11 @@ async def text2image(
width = 0
height = 0
_, h = BuildImage.get_text_size("", _font)
line_height = int(font_size / 3)
image_list = []
line_height = font_size // 3
for s in text.split("\n"):
w, _ = BuildImage.get_text_size(s.strip() or "", _font)
height += h + line_height
width = width if width > w else w
width = max(width, w)
image_list.append(
await BuildImage.build_text_image(
s.strip(), font, font_size, font_color
@ -205,7 +198,7 @@ async def text2image(
return A
def group_image(image_list: list[BuildImage]) -> tuple[list[list[BuildImage]], int]:
def group_image(image_list: list[BuildImage]) -> tuple[list[list[BuildImage]], float]:
"""
说明:
根据图片大小进行分组
@ -240,7 +233,7 @@ def group_image(image_list: list[BuildImage]) -> tuple[list[list[BuildImage]], i
break
else:
break
total_w += max([x.width for x in group]) + 15
total_w += max(x.width for x in group) + 15
image_group.append(group)
while surplus_list:
surplus_list = [x for x in surplus_list if x.uid not in is_use]
@ -252,7 +245,7 @@ def group_image(image_list: list[BuildImage]) -> tuple[list[list[BuildImage]], i
_w = 0
index = -1
for i, ig in enumerate(image_group):
if s := sum([x.height for x in ig]) > _w:
if s := sum(x.height for x in ig) > _w:
_w = s
index = i
if index != -1:
@ -262,29 +255,29 @@ def group_image(image_list: list[BuildImage]) -> tuple[list[list[BuildImage]], i
max_h = 0
max_w = 0
for ig in image_group:
if (_h := sum([x.height + 15 for x in ig])) > max_h:
if (_h := sum(x.height + 15 for x in ig)) > max_h:
max_h = _h
max_w += max([x.width for x in ig]) + 30
max_w += max(x.width for x in ig) + 30
is_use.clear()
while abs(max_h - max_w) > 200 and len(image_group) - 1 >= len(image_group[-1]):
for img in image_group[-1]:
_min_h = 999999
_min_index = -1
for i, ig in enumerate(image_group):
if (_h := sum([x.height for x in ig]) + img.height) < _min_h:
if (_h := sum(x.height for x in ig) + img.height) < _min_h:
_min_h = _h
_min_index = i
is_use.append(_min_index)
image_group[_min_index].append(img)
max_w -= max([x.width for x in image_group[-1]]) - 30
max_w -= max(x.width for x in image_group[-1]) - 30
image_group.pop(-1)
max_h = max([sum([x.height + 15 for x in ig]) for ig in image_group])
max_h = max(sum(x.height + 15 for x in ig) for ig in image_group)
return image_group, max(max_h + 250, max_w + 70)
async def build_sort_image(
image_group: list[list[BuildImage]],
h: int | None = None,
h: float | None = None,
padding_top: int = 200,
color: ColorAlias = (
255,
@ -307,16 +300,15 @@ async def build_sort_image(
"""
bk_file = None
if background_path:
random_bk = os.listdir(background_path)
if random_bk:
if random_bk := os.listdir(background_path):
bk_file = random.choice(random_bk)
image_w = 0
image_h = 0
if not h:
for ig in image_group:
_w = max([x.width + 30 for x in ig])
_w = max(x.width + 30 for x in ig)
image_w += _w + 30
_h = sum([x.height + 10 for x in ig])
_h = sum(x.height + 10 for x in ig)
if _h > image_h:
image_h = _h
image_h += padding_top
@ -342,7 +334,7 @@ async def build_sort_image(
for img in ig:
await A.paste(img, (curr_w, curr_h))
curr_h += img.height + 10
curr_w += max([x.width for x in ig]) + 30
curr_w += max(x.width for x in ig) + 30
return A