178 lines
6.7 KiB
Bash
Executable File
178 lines
6.7 KiB
Bash
Executable File
#!/bin/bash
|
||
# gen_font.sh —— 生成中文字体子集
|
||
#
|
||
# 为什么要子集:app 分区只有 3MB,完整 CJK 字体要 8MB+。
|
||
# 这里扫描源码里实际用到的字符,只把用到的字打进固件。
|
||
# 新增中文文案后重新跑一次本脚本即可(工具会自动扩容)。
|
||
#
|
||
# 依赖:node/npx(lv_font_conv)、fonttools(从系统 ttc 提取 ttf)
|
||
set -e
|
||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||
SRC="$ROOT/src"
|
||
ASSETS="$SRC/assets"
|
||
FONT_TTF="${FONT_TTF:-/tmp/HeitiSC-Medium.ttf}"
|
||
|
||
mkdir -p "$ASSETS"
|
||
|
||
# 1. 从系统字体提取简体黑体(只需一次)
|
||
if [ ! -f "$FONT_TTF" ]; then
|
||
echo ">> extracting Heiti SC from system ttc..."
|
||
python3 - <<'PY'
|
||
from fontTools.ttLib import TTCollection
|
||
import os
|
||
c = TTCollection('/System/Library/Fonts/STHeiti Medium.ttc')
|
||
for f in c.fonts:
|
||
if 'SC' in (f['name'].getDebugName(4) or ''):
|
||
f.save('/tmp/HeitiSC-Medium.ttf')
|
||
break
|
||
PY
|
||
fi
|
||
|
||
# 2. 收集源码中出现过的所有非 ASCII 字符
|
||
# 只扫字符串字面量:注释里的中文永远不会上屏,收进字体会白占几十 KB。
|
||
# 具体规则见 collect_chars.py(build.sh 也会用 --check 复用它做一致性检查)。
|
||
python3 "$ROOT/tools/collect_chars.py" "$ROOT"
|
||
|
||
SYMBOLS="$(cat "$ROOT/tools/symbols.txt")"
|
||
|
||
# 3. 生成 16px / 24px 两档
|
||
#
|
||
# 为什么除了 0x20-0x7E 还要带三组区间:
|
||
# 源码字面量能收"我们写的字",收不到"别人的字"——歌名/歌手来自 ID3 标签,
|
||
# 城市与运势来自服务端。这些文本里到处都是《》「」·—…,实测歌名
|
||
# 「万能青年旅店《杀死那个石家庄人》」中间的《 就是个空心方块。
|
||
# 把 CJK 标点、通用标点、全角字符整段带上,总共约 270 个字形,
|
||
# 24px 档多 78KB、16px 档多 35KB,比再踩一次"方块"便宜得多。
|
||
for SIZE in 16 24; do
|
||
echo ">> generating lv_font_cjk_${SIZE} ..."
|
||
npx --yes lv_font_conv \
|
||
--font "$FONT_TTF" \
|
||
--range 0x20-0x7E \
|
||
--range 0x2000-0x206F \
|
||
--range 0x3000-0x303F \
|
||
--range 0xFF01-0xFF5E \
|
||
--symbols "$SYMBOLS" \
|
||
--size $SIZE --bpp 4 --format lvgl \
|
||
--lv-include lvgl.h \
|
||
--lv-font-name lv_font_cjk_${SIZE} \
|
||
--no-compress \
|
||
-o "$ASSETS/lv_font_cjk_${SIZE}.c"
|
||
done
|
||
|
||
ls -lh "$ASSETS"/lv_font_cjk_*.c
|
||
|
||
# 3.5 媒体字体:歌名/歌手来自音乐文件的 ID3 标签,字不可控。
|
||
# 把现代汉语常用字表(2500 字,见 common_chars.txt)单独生成一枚
|
||
# 16px 字体,只给音乐等"文件来源文本"用——不进 24px,控制体积。
|
||
echo ">> generating lv_font_media_16 ..."
|
||
MEDIA_SYMBOLS="$(python3 - "$ROOT" <<'PY'
|
||
import sys, os
|
||
root = sys.argv[1]
|
||
ui = open(os.path.join(root, 'tools', 'symbols.txt'), encoding='utf-8').read()
|
||
common = open(os.path.join(root, 'tools', 'common_chars.txt'), encoding='utf-8').read()
|
||
# 服务端下发的中文(城市名、和风天气描述、星座/运势正文)不在源码字面量里,
|
||
# 只靠 collect_chars.py 收不到,缺字就是空心方块。这张补充表由
|
||
# nex_buddy 侧脚本统计生成,随服务端文案一起更新。
|
||
server = open(os.path.join(root, 'tools', 'server_vocab.txt'), encoding='utf-8').read()
|
||
seen = set()
|
||
out = []
|
||
for ch in ui + common + server:
|
||
if ch not in seen:
|
||
seen.add(ch)
|
||
out.append(ch)
|
||
sys.stdout.write(''.join(out))
|
||
PY
|
||
)"
|
||
npx --yes lv_font_conv \
|
||
--font "$FONT_TTF" \
|
||
--range 0x20-0x7E \
|
||
--range 0x2000-0x206F \
|
||
--range 0x3000-0x303F \
|
||
--range 0xFF01-0xFF5E \
|
||
--symbols "$MEDIA_SYMBOLS" \
|
||
--size 16 --bpp 4 --format lvgl \
|
||
--lv-include lvgl.h \
|
||
--lv-font-name lv_font_media_16 \
|
||
--no-compress \
|
||
-o "$ASSETS/lv_font_media_16.c"
|
||
|
||
ls -lh "$ASSETS"/lv_font_media_16.c
|
||
|
||
# 3.6 24px 动态文本档:天气描述、星座名这类"服务端给的大字"。
|
||
# ⚠ 只收 server_vocab 会漏字:server_vocab 是"服务端字表 − 常用字",像"多云"的
|
||
# "多"属于常用字、被减掉后 24px 档就没有它,上屏就是一个空心方块(实测踩过)。
|
||
# 所以这里取 symbols(源码字面量:多云/晴…)∪ server_vocab ∪ vocab24。
|
||
# vocab24.txt 是"会以大字号上屏的对外词汇"(十二星座名、和风描述里可能出现的
|
||
# 字、火土风水这些属性词)——它们全都是常用字,正好被 server_vocab 剔掉了,
|
||
# 不显式补就会掉字,实测「太阳 射手座」显示成「太阳 手座」。
|
||
# 整张常用字表做 24px 会多出约 1MB,放不进 3MB 分区,所以只补需要的。
|
||
echo ">> generating lv_font_media_24 ..."
|
||
VOCAB24="$(python3 - "$ROOT" <<'PY'
|
||
import sys, os
|
||
root = sys.argv[1]
|
||
parts = ['symbols.txt', 'server_vocab.txt', 'vocab24.txt']
|
||
seen = set()
|
||
out = []
|
||
for name in parts:
|
||
p = os.path.join(root, 'tools', name)
|
||
if not os.path.exists(p):
|
||
continue
|
||
for ch in open(p, encoding='utf-8').read():
|
||
if ch not in seen:
|
||
seen.add(ch)
|
||
out.append(ch)
|
||
sys.stderr.write(' media_24 chars = %d\n' % len(out))
|
||
sys.stdout.write(''.join(out))
|
||
PY
|
||
)"
|
||
npx --yes lv_font_conv \
|
||
--font "$FONT_TTF" \
|
||
--range 0x20-0x7E \
|
||
--range 0x2000-0x206F \
|
||
--range 0x3000-0x303F \
|
||
--range 0xFF01-0xFF5E \
|
||
--symbols "$VOCAB24" \
|
||
--size 24 --bpp 4 --format lvgl \
|
||
--lv-include lvgl.h \
|
||
--lv-font-name lv_font_media_24 \
|
||
--no-compress \
|
||
-o "$ASSETS/lv_font_media_24.c"
|
||
|
||
ls -lh "$ASSETS"/lv_font_media_24.c
|
||
|
||
# 4. 时间数字专用字体:只含数字与冒号,做得比正文字体大得多。
|
||
# 等宽很关键——时钟每分钟刷新时数字不能左右跳。
|
||
echo ">> generating lv_font_num_64 ..."
|
||
npx --yes lv_font_conv \
|
||
--font "$FONT_TTF" \
|
||
--symbols "0123456789:.-°% " \
|
||
--size 64 --bpp 4 --format lvgl \
|
||
--lv-include lvgl.h \
|
||
--lv-font-name lv_font_num_64 \
|
||
--no-compress \
|
||
-o "$ASSETS/lv_font_num_64.c"
|
||
|
||
ls -lh "$ASSETS"/lv_font_num_64.c
|
||
|
||
# 5. 黄道十二宫符号(♈♉♊♋♌♍♎♏♐♑♒♓)
|
||
# 中文字体里根本没有这些码位(Heiti 没有,硬排就是空心方块),
|
||
# 所以从系统符号字体单独取 12 个字形,做成一枚小字体给星盘页用。
|
||
# 比往 TF 卡下发 PNG 省事得多:不占同步带宽、缩放不糊、跟着 OTA 走。
|
||
ZODIAC_TTF="${ZODIAC_TTF:-/System/Library/Fonts/Apple Symbols.ttf}"
|
||
if [ -f "$ZODIAC_TTF" ]; then
|
||
echo ">> generating lv_font_zodiac_26 ..."
|
||
npx --yes lv_font_conv \
|
||
--font "$ZODIAC_TTF" \
|
||
--symbols "♈♉♊♋♌♍♎♏♐♑♒♓" \
|
||
--size 26 --bpp 4 --format lvgl \
|
||
--lv-include lvgl.h \
|
||
--lv-font-name lv_font_zodiac_26 \
|
||
--no-compress \
|
||
-o "$ASSETS/lv_font_zodiac_26.c"
|
||
ls -lh "$ASSETS/lv_font_zodiac_26.c"
|
||
else
|
||
echo "!! 缺少符号字体 $ZODIAC_TTF —— 跳过星座字形,星盘页不会有 ♈~♓"
|
||
fi
|
||
|
||
echo ">> font done"
|