md2pdf 中文字体修复:install.sh 只认 TrueType(glyf) 轮廓,Noto(CFF) 移出候选并魔数校验自愈重建 fonts/cjk.ttf;img-utils 水印字型优先文泉驿(Noto ttc 默认字型是 JP),仅作兜底

This commit is contained in:
2026-09-16 10:49:25 +08:00
parent f9095f4588
commit 6969c5d949
3 changed files with 98 additions and 63 deletions
+36 -16
View File
@@ -1,25 +1,45 @@
#!/bin/bash
set -e
# 确保存在可用的中文字体(watermark 中文水印需要)。
# 确保存在可用的中文字体(watermark 中文水印需要),Pillow 读字体无 reportlab 的 CFF 限制,
# 但 NotoSansCJK ttc 的默认字型(索引 0)是 JP,中文水印会出日式字形,
# 因此就绪判定只认文泉驿(系统 wqy 或同仓 md2pdf 提取的 fonts/cjk.ttf),Noto 仅作兜底。
# 命中即幂等退出;依赖缓存由 Skill Manager 依据本脚本内容哈希管理。
for f in \
/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc \
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc \
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc; do
if [ -f "$f" ]; then
echo "中文字体已存在:$f"
exit 0
fi
done
SKILL_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MD2PDF_FONT="$SKILL_DIR/../md2pdf/fonts/cjk.ttf"
has_wqy() {
for f in /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc \
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc; do
[ -f "$f" ] && return 0
done
return 1
}
if has_wqy; then
echo "文泉驿字体已存在"
exit 0
fi
if [ -s "$MD2PDF_FONT" ]; then
echo "使用同仓 md2pdf 准备的中文字体:$MD2PDF_FONT"
exit 0
fi
if [ "$(id -u)" -eq 0 ]; then
echo "未找到中文字体,安装 fonts-wqy-zenhei..."
apt-get update -y -qq
apt-get install -y -qq fonts-wqy-zenhei
else
echo "错误:非 root 用户且系统无中文字体,无法自动安装" >&2
exit 1
# best-effort:apt 失败不直接报错,交给下面的就绪判定兜底
apt-get update -y -qq || true
apt-get install -y -qq fonts-wqy-zenhei || true
fi
echo "中文字体就绪"
if has_wqy || [ -s "$MD2PDF_FONT" ]; then
echo "中文字体就绪"
elif [ -f /usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc ]; then
# Noto 兜底:中文能显示,只是默认字型偏日式;不算失败
echo "警告:仅有 Noto CJK 字体(中文水印将使用其默认 JP 字型),建议安装文泉驿"
else
echo "错误:无中文字体可用(非 root 且系统未装文泉驿/Noto)" >&2
exit 1
fi
+5 -2
View File
@@ -36,8 +36,10 @@ NAMED_COLORS = {
CJK_FONT_CANDIDATES = [
'/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc',
'/usr/share/fonts/truetype/wqy/wqy-microhei.ttc',
'/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc',
]
# Noto 只作最后兜底:Pillow(FreeType) 能读它的 CFF 轮廓,但 ttc 默认字型(索引 0)是 JP 字形,
# 中文水印会出日式写法;同仓 md2pdf 的 cjk.ttf(文泉驿)排在它前面。
NOTO_CJK_FALLBACK = '/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc'
def to_int(v, default=0):
@@ -221,7 +223,7 @@ def action_compress(img, fmt, args):
def find_font(explicit):
"""字体查找:显式 font > 系统 CJK 候选 > 同仓 md2pdf 技能准备的字体。"""
"""字体查找:显式 font > 系统 wqy > 同仓 md2pdf 准备的字体 > Noto 兜底。"""
if explicit:
p = Path(explicit)
if not p.exists():
@@ -230,6 +232,7 @@ def find_font(explicit):
skill_root = Path(__file__).resolve().parent.parent
candidates = list(CJK_FONT_CANDIDATES)
candidates.append(str(skill_root / 'md2pdf' / 'fonts' / 'cjk.ttf'))
candidates.append(NOTO_CJK_FALLBACK)
for c in candidates:
if Path(c).exists():
return c