From 6969c5d9498aee02dddbd5a7b28aca0d0762ea16 Mon Sep 17 00:00:00 2001 From: kicer Date: Wed, 16 Sep 2026 10:49:25 +0800 Subject: [PATCH] =?UTF-8?q?md2pdf=20=E4=B8=AD=E6=96=87=E5=AD=97=E4=BD=93?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9Ainstall.sh=20=E5=8F=AA=E8=AE=A4=20Tr?= =?UTF-8?q?ueType(glyf)=20=E8=BD=AE=E5=BB=93=EF=BC=8CNoto(CFF)=20=E7=A7=BB?= =?UTF-8?q?=E5=87=BA=E5=80=99=E9=80=89=E5=B9=B6=E9=AD=94=E6=95=B0=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E8=87=AA=E6=84=88=E9=87=8D=E5=BB=BA=20fonts/cjk.ttf?= =?UTF-8?q?=EF=BC=9Bimg-utils=20=E6=B0=B4=E5=8D=B0=E5=AD=97=E5=9E=8B?= =?UTF-8?q?=E4=BC=98=E5=85=88=E6=96=87=E6=B3=89=E9=A9=BF=EF=BC=88Noto=20tt?= =?UTF-8?q?c=20=E9=BB=98=E8=AE=A4=E5=AD=97=E5=9E=8B=E6=98=AF=20JP=EF=BC=89?= =?UTF-8?q?=EF=BC=8C=E4=BB=85=E4=BD=9C=E5=85=9C=E5=BA=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skills/img-utils/install.sh | 52 ++++++++++++------ skills/img-utils/run.py | 7 ++- skills/md2pdf/install.sh | 102 ++++++++++++++++++++---------------- 3 files changed, 98 insertions(+), 63 deletions(-) diff --git a/skills/img-utils/install.sh b/skills/img-utils/install.sh index 3081344..43db54c 100644 --- a/skills/img-utils/install.sh +++ b/skills/img-utils/install.sh @@ -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 diff --git a/skills/img-utils/run.py b/skills/img-utils/run.py index aff228c..e645cd8 100644 --- a/skills/img-utils/run.py +++ b/skills/img-utils/run.py @@ -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 diff --git a/skills/md2pdf/install.sh b/skills/md2pdf/install.sh index d752333..40ac51c 100755 --- a/skills/md2pdf/install.sh +++ b/skills/md2pdf/install.sh @@ -1,77 +1,89 @@ #!/bin/bash set -e -# 创建字体目录 +# 准备中文字体 fonts/cjk.ttf。 +# 关键约束:reportlab 的 TTFont 只支持 TrueType(glyf) 轮廓, +# Noto Sans CJK / Source Han Sans 均为 OTTO(CFF) 轮廓,提取/改名后依然注册失败, +# 因此候选只保留文泉驿(wqy,glyf 轮廓),缺失时用 apt 安装。 +FONT_TARGET="fonts/cjk.ttf" + mkdir -p fonts -# 如果已经存在提取好的字体且非空,则跳过(可选) -if [ -s fonts/cjk.ttf ]; then - echo "字体文件已存在,跳过准备" +# sfnt magic 必须是 TrueType(00010000 / true / typ1)才算有效; +# 旧版本曾用 Noto(CFF) 提取出魔数为 OTTO 的假 .ttf,这里检测并自愈重建。 +is_valid_ttf() { + [ -s "$1" ] || return 1 + local magic + magic=$(od -An -N4 -tx1 "$1" | tr -d ' \n') + case "$magic" in + 00010000|74727565|74797031) return 0 ;; + *) return 1 ;; + esac +} + +if is_valid_ttf "$FONT_TARGET"; then + echo "字体文件已存在且为有效 TrueType,跳过准备" exit 0 fi -# 查找系统字体 +if [ -s "$FONT_TARGET" ]; then + echo "现有 $FONT_TARGET 非 TrueType 轮廓(可能是旧版提取的 CFF 字体),删除重建" + rm -f "$FONT_TARGET" +fi + +# 查找系统中的文泉驿字体(仅 glyf 轮廓字体可被 reportlab 使用) FONT_FILE="" -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 \ - /usr/share/fonts/truetype/source-han-sans/SourceHanSansSC-Regular.otf; do +for f in /usr/share/fonts/truetype/wqy/wqy-microhei.ttc \ + /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc; do if [ -f "$f" ]; then FONT_FILE="$f" break fi done -# 如果没有找到,尝试安装文泉驿正黑 +# 没有则安装文泉驿 if [ -z "$FONT_FILE" ]; then - echo "未找到中文字体,尝试安装 fonts-wqy-zenhei..." + echo "未找到文泉驿字体,尝试安装 fonts-wqy-microhei fonts-wqy-zenhei..." if [ "$(id -u)" -eq 0 ]; then apt-get update -y -qq - apt-get install -y -qq fonts-wqy-zenhei - FONT_FILE="/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc" - else - echo "错误:非 root 用户且系统无中文字体,无法自动安装" >&2 + apt-get install -y -qq fonts-wqy-microhei fonts-wqy-zenhei + for f in /usr/share/fonts/truetype/wqy/wqy-microhei.ttc \ + /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc; do + if [ -f "$f" ]; then + FONT_FILE="$f" + break + fi + done + fi + if [ -z "$FONT_FILE" ]; then + echo "错误:无中文字体可用(非 root 且系统未装文泉驿)" >&2 exit 1 fi fi -# 根据字体格式提取/转换为 TTF 到 fonts/cjk.ttf -case "$FONT_FILE" in - *.ttc) - echo "从 TTC 提取 TTF..." - python3 - "$FONT_FILE" <<'PY' +echo "从 TTC 提取第一个字型到 $FONT_TARGET:$FONT_FILE" +python3 - "$FONT_FILE" <<'PY' import sys from fontTools.ttLib import TTCollection + ttc_path = sys.argv[1] out_path = "fonts/cjk.ttf" collection = TTCollection(ttc_path) if not collection.fonts: print("TTC 中无字体", file=sys.stderr) sys.exit(1) -collection.fonts[0].save(out_path) -print(f"提取完成:{out_path}") +font = collection.fonts[0] +if "glyf" not in font: + print(f"字体 {font['name'].getDebugName(4)} 不是 TrueType 轮廓,reportlab 无法使用", file=sys.stderr) + sys.exit(1) +font.save(out_path) +print(f"提取完成:{out_path}({font['name'].getDebugName(4)})") PY - ;; - *.otf) - echo "转换 OTF 为 TTF..." - python3 - "$FONT_FILE" <<'PY' -import sys -from fontTools.ttLib import TTFont -font = TTFont(sys.argv[1]) -# 确保输出为 TrueType 轮廓(去除 CFF) -font.flavor = None -font.save("fonts/cjk.ttf") -print("转换完成") -PY - ;; - *.ttf) - echo "复制 TTF 字体..." - cp "$FONT_FILE" fonts/cjk.ttf - ;; - *) - echo "不支持的字体格式:$FONT_FILE" >&2 - exit 1 - ;; -esac -echo "中文字体准备完成:fonts/cjk.ttf" +# 兜底校验:产出的必须又是有效 TrueType +if ! is_valid_ttf "$FONT_TARGET"; then + echo "错误:提取结果不是有效 TrueType 字体" >&2 + exit 1 +fi + +echo "中文字体准备完成:$FONT_TARGET"