feat: optimize cover title font size dynamic adjustment

This commit is contained in:
neo-cloud-ai
2026-01-30 14:35:17 +08:00
parent 55594c6e4f
commit 065a1ad3e2
2 changed files with 28 additions and 12 deletions

View File

@@ -148,11 +148,19 @@ def generate_cover_html(metadata: dict, theme: str, width: int, height: int) ->
title = metadata.get('title', '标题') title = metadata.get('title', '标题')
subtitle = metadata.get('subtitle', '') subtitle = metadata.get('subtitle', '')
# 限制标题和副标题长度
if len(title) > 15: # 动态调整标题字体大小
title = title[:15] title_len = len(title)
if len(subtitle) > 15: if title_len <= 6:
subtitle = subtitle[:15] title_size = int(width * 0.14) # 极大
elif title_len <= 10:
title_size = int(width * 0.12) # 大
elif title_len <= 18:
title_size = int(width * 0.09) # 中
elif title_len <= 30:
title_size = int(width * 0.07) # 小
else:
title_size = int(width * 0.055) # 极小
# 获取主题背景色 # 获取主题背景色
theme_backgrounds = { theme_backgrounds = {
@@ -231,7 +239,7 @@ def generate_cover_html(metadata: dict, theme: str, width: int, height: int) ->
.cover-title {{ .cover-title {{
font-weight: 900; font-weight: 900;
font-size: {int(width * 0.12)}px; font-size: {title_size}px;
line-height: 1.4; line-height: 1.4;
background: {title_bg}; background: {title_bg};
-webkit-background-clip: text; -webkit-background-clip: text;

View File

@@ -295,11 +295,19 @@ def generate_cover_html(metadata: dict, style_key: str = "purple") -> str:
title = metadata.get('title', '标题') title = metadata.get('title', '标题')
subtitle = metadata.get('subtitle', '') subtitle = metadata.get('subtitle', '')
# 限制标题和副标题长度
if len(title) > 15: # 动态调整标题字体大小
title = title[:15] title_len = len(title)
if len(subtitle) > 15: if title_len <= 6:
subtitle = subtitle[:15] title_size = 150 # 极大 (width * 0.14)
elif title_len <= 10:
title_size = 130 # 大 (width * 0.12)
elif title_len <= 18:
title_size = 100 # 中 (width * 0.09)
elif title_len <= 30:
title_size = 80 # 小 (width * 0.07)
else:
title_size = 60 # 极小 (width * 0.055)
# 暗黑模式特殊处理 # 暗黑模式特殊处理
is_dark = style_key == "dark" is_dark = style_key == "dark"
@@ -335,7 +343,7 @@ def generate_cover_html(metadata: dict, style_key: str = "purple") -> str:
}} }}
.cover-emoji {{ font-size: 180px; line-height: 1.2; margin-bottom: 50px; }} .cover-emoji {{ font-size: 180px; line-height: 1.2; margin-bottom: 50px; }}
.cover-title {{ .cover-title {{
font-weight: 900; font-size: 130px; line-height: 1.4; font-weight: 900; font-size: {title_size}px; line-height: 1.4;
background: {title_gradient}; background: {title_gradient};
-webkit-background-clip: text; -webkit-background-clip: text;
-webkit-text-fill-color: transparent; -webkit-text-fill-color: transparent;