feat(client): 字体改本地打包(拉丁全量 + Noto Sans SC GB2312 子集)
生产 PangolinFonts.useBundled=true,离线可用,不再运行时拉 google_fonts; 中文经 fontFamilyFallback=[Noto Sans SC] 兜底,子集外字符再兜系统 CJK 字体。 - 拉丁:Sora/Manrope/JetBrains Mono 静态权重(复用 test/fonts)。 - 中文:Noto Sans SC 变量字体 instance 到 Regular + subset 到 GB2312 6763 字 (单权重,粗体引擎合成),client/fonts 合计约 3.4MB。 - tools/fonts/make-cjk-subset.sh 可复现生成(GB2312 字表无需联网)。 - 新增 test/unit/fonts_test.dart 锁定接线;golden 因 notdef 占位字形微调已更新。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@@ -154,12 +154,14 @@ class PangolinFonts {
|
||||
static const cjk = 'Noto Sans SC';
|
||||
static const mono = 'JetBrains Mono';
|
||||
|
||||
/// 测试钩子:true 时用 bundled family(test/fonts/ 经 FontLoader 注册),不走 google_fonts。
|
||||
static bool useBundled = false;
|
||||
/// 生产默认 true:用本地打包 ttf(pubspec fonts: 段),离线可用,不走 google_fonts
|
||||
/// 运行时拉取。测试态由 flutter_test_config.dart 经 FontLoader 注册 test/fonts/。
|
||||
/// 中文经 [cjk](Noto Sans SC 子集)走 fontFamilyFallback;子集外字符再兜系统 CJK 字体。
|
||||
static bool useBundled = true;
|
||||
|
||||
static TextStyle sora({double? fontSize, FontWeight? fontWeight, double? height, double? letterSpacing}) =>
|
||||
useBundled
|
||||
? TextStyle(fontFamily: display, fontSize: fontSize, fontWeight: fontWeight, height: height, letterSpacing: letterSpacing)
|
||||
? TextStyle(fontFamily: display, fontFamilyFallback: const [cjk], fontSize: fontSize, fontWeight: fontWeight, height: height, letterSpacing: letterSpacing)
|
||||
: GoogleFonts.sora(fontSize: fontSize, fontWeight: fontWeight, height: height, letterSpacing: letterSpacing);
|
||||
|
||||
static TextStyle manrope(
|
||||
@@ -167,6 +169,7 @@ class PangolinFonts {
|
||||
useBundled
|
||||
? TextStyle(
|
||||
fontFamily: sans,
|
||||
fontFamilyFallback: const [cjk],
|
||||
fontSize: fontSize,
|
||||
fontWeight: fontWeight,
|
||||
height: height,
|
||||
@@ -177,11 +180,11 @@ class PangolinFonts {
|
||||
|
||||
static TextStyle jetBrainsMono({double? fontSize, FontWeight? fontWeight, List<FontFeature>? fontFeatures}) =>
|
||||
useBundled
|
||||
? TextStyle(fontFamily: mono, fontSize: fontSize, fontWeight: fontWeight, fontFeatures: fontFeatures)
|
||||
? TextStyle(fontFamily: mono, fontFamilyFallback: const [cjk], fontSize: fontSize, fontWeight: fontWeight, fontFeatures: fontFeatures)
|
||||
: GoogleFonts.jetBrainsMono(fontSize: fontSize, fontWeight: fontWeight, fontFeatures: fontFeatures);
|
||||
|
||||
static TextTheme manropeTextTheme(TextTheme base) =>
|
||||
useBundled ? base.apply(fontFamily: sans) : GoogleFonts.manropeTextTheme(base);
|
||||
useBundled ? base.apply(fontFamily: sans, fontFamilyFallback: const [cjk]) : GoogleFonts.manropeTextTheme(base);
|
||||
}
|
||||
|
||||
/// ── Type scale ──────────────────────────────────────────────────────
|
||||
|
||||
@@ -46,5 +46,30 @@ flutter:
|
||||
- assets/app-icon.svg
|
||||
- assets/tray_icon.png
|
||||
|
||||
# ── 字体(由 google_fonts 运行时加载,无需本地 ttf)──
|
||||
# 若需离线/打包字体,下载 ttf 放 fonts/,并按 pubspec 模板恢复 fonts: 段。
|
||||
# ── 字体(本地打包,全平台离线)──
|
||||
# 拉丁全量(Sora/Manrope/JetBrains Mono)+ Noto Sans SC 子集(GB2312 6763 字,单 Regular
|
||||
# 权重;CJK 粗体由引擎合成)。子集由 tools/fonts/make-cjk-subset.sh 可复现生成。
|
||||
# 生产经 PangolinFonts.useBundled=true 走这些 family;中文经 fontFamilyFallback 兜底。
|
||||
fonts:
|
||||
- family: Sora
|
||||
fonts:
|
||||
- asset: fonts/Sora-Regular.ttf
|
||||
- asset: fonts/Sora-SemiBold.ttf
|
||||
weight: 600
|
||||
- asset: fonts/Sora-Bold.ttf
|
||||
weight: 700
|
||||
- family: Manrope
|
||||
fonts:
|
||||
- asset: fonts/Manrope-Regular.ttf
|
||||
- asset: fonts/Manrope-Medium.ttf
|
||||
weight: 500
|
||||
- asset: fonts/Manrope-SemiBold.ttf
|
||||
weight: 600
|
||||
- asset: fonts/Manrope-Bold.ttf
|
||||
weight: 700
|
||||
- family: JetBrains Mono
|
||||
fonts:
|
||||
- asset: fonts/JetBrainsMono-Regular.ttf
|
||||
- family: Noto Sans SC
|
||||
fonts:
|
||||
- asset: fonts/NotoSansSC-Regular-subset.otf
|
||||
|
||||
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
|
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 40 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 25 KiB |
@@ -0,0 +1,54 @@
|
||||
// fonts_test.dart — 本地打包字体接线回归。
|
||||
//
|
||||
// 锁定 Task 3 的字体改造:生产走 bundled family(非 google_fonts 运行时拉取),
|
||||
// 且每个字体函数都把中文兜到 Noto Sans SC(fontFamilyFallback: [cjk])。
|
||||
// flutter_test_config.dart 在测试启动已置 PangolinFonts.useBundled = true。
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pangolin_vpn/pangolin_theme.dart';
|
||||
|
||||
void main() {
|
||||
group('PangolinFonts 本地打包接线', () {
|
||||
test('测试态(useBundled=true)由 flutter_test_config 设置', () {
|
||||
// 该 hook 由 test/flutter_test_config.dart 在所有测试前置 true;
|
||||
// 生产代码默认值也是 true(见 pangolin_theme.dart)。
|
||||
expect(PangolinFonts.useBundled, isTrue);
|
||||
});
|
||||
|
||||
test('cjk family 为 Noto Sans SC(与 pubspec fonts: 段一致)', () {
|
||||
expect(PangolinFonts.cjk, 'Noto Sans SC');
|
||||
});
|
||||
|
||||
test('sora() 用 bundled display family 且中文兜底到 cjk', () {
|
||||
final s = PangolinFonts.sora(fontSize: 16);
|
||||
expect(s.fontFamily, PangolinFonts.display);
|
||||
expect(s.fontFamilyFallback, contains(PangolinFonts.cjk));
|
||||
});
|
||||
|
||||
test('manrope() 用 bundled sans family 且中文兜底到 cjk', () {
|
||||
final s = PangolinFonts.manrope(fontSize: 16);
|
||||
expect(s.fontFamily, PangolinFonts.sans);
|
||||
expect(s.fontFamilyFallback, contains(PangolinFonts.cjk));
|
||||
});
|
||||
|
||||
test('jetBrainsMono() 用 bundled mono family 且中文兜底到 cjk', () {
|
||||
final s = PangolinFonts.jetBrainsMono(fontSize: 14);
|
||||
expect(s.fontFamily, PangolinFonts.mono);
|
||||
expect(s.fontFamilyFallback, contains(PangolinFonts.cjk));
|
||||
});
|
||||
|
||||
test('manropeTextTheme 应用 sans family 且中文兜底到 cjk', () {
|
||||
const base = TextTheme(bodyMedium: TextStyle(fontSize: 16));
|
||||
final t = PangolinFonts.manropeTextTheme(base);
|
||||
expect(t.bodyMedium?.fontFamily, PangolinFonts.sans);
|
||||
expect(t.bodyMedium?.fontFamilyFallback, contains(PangolinFonts.cjk));
|
||||
});
|
||||
|
||||
test('PangolinText 标度经由 bundled 接线(中文兜底存在)', () {
|
||||
// 抽查几个常用标度,确保未绕过 PangolinFonts 封装。
|
||||
expect(PangolinText.h1.fontFamilyFallback, contains(PangolinFonts.cjk));
|
||||
expect(PangolinText.body.fontFamilyFallback, contains(PangolinFonts.cjk));
|
||||
expect(PangolinText.mono.fontFamilyFallback, contains(PangolinFonts.cjk));
|
||||
});
|
||||
});
|
||||
}
|
||||