@li-canghai/dsh-paper 0.8.3 → 0.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,6 +32,7 @@
32
32
  | 启动器 | `uvx`(页面图、PDF 元数据、MinerU 解析三条链共用;缺失时详情页自检卡给安装命令)|
33
33
  | 解析语言 | `ch`(中英);扫描件可开 `parseOcr` |
34
34
  | 自动翻译 | 开,范围「前 20 页」,模型跟随宿主默认 |
35
+ | 翻译模型 | 设置页可钉到某个 provider/model;本地专用 MT(如 `tencent/Hy-MT3-7B-GGUF`)请把「输出」选成 `plain` 或 `lines`。三档都能用,但 `plain` 最稳、`lines` 最快;`json` 档需要模型严守 JSON 转义规则,含 LaTeX 的论文容易整批失败 |
35
36
 
36
37
  完整配置项表见 [SPEC.md 的配置项](docs/SPEC.md#配置项)。
37
38
 
@@ -10,46 +10,57 @@ dsh-paper 以 [MIT](LICENSE) 发布。它依赖下列第三方软件,每个项
10
10
 
11
11
  插件在运行时直接 `import` 的包,随插件一起被安装。
12
12
 
13
- | 包 | 版本 | 许可 | 上游 |
14
- | --- | --- | --- | --- |
15
- | `@citation-js/core` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
16
- | `@citation-js/plugin-bibtex` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
17
- | `@citation-js/plugin-csl` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
18
- | `@citation-js/plugin-ris` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
19
- | `@xmldom/xmldom` | 0.9.12 | MIT | [xmldom/xmldom](https://github.com/xmldom/xmldom) |
20
- | `https-proxy-agent` | 9.1.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
21
- | `katex` | 0.16.47 | MIT | [KaTeX/KaTeX](https://github.com/KaTeX/KaTeX) |
22
- | `socks` | 2.8.10 | MIT | [JoshGlazebrook/socks](https://github.com/JoshGlazebrook/socks) |
13
+ | 包 | 版本 | 许可 | 上游 |
14
+ | ---------------------------- | ------- | ---- | ----------------------------------------------------------------------- |
15
+ | `@citation-js/core` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
16
+ | `@citation-js/plugin-bibtex` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
17
+ | `@citation-js/plugin-csl` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
18
+ | `@citation-js/plugin-ris` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
19
+ | `@xmldom/xmldom` | 0.9.12 | MIT | [xmldom/xmldom](https://github.com/xmldom/xmldom) |
20
+ | `https-proxy-agent` | 9.1.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
21
+ | `katex` | 0.16.47 | MIT | [KaTeX/KaTeX](https://github.com/KaTeX/KaTeX) |
22
+ | `socks` | 2.8.10 | MIT | [JoshGlazebrook/socks](https://github.com/JoshGlazebrook/socks) |
23
23
 
24
24
  ## 运行时传递依赖
25
25
 
26
26
  由上面那些包引入,同样随插件一起被安装。
27
27
 
28
- | 包 | 版本 | 许可 | 上游 |
29
- | --- | --- | --- | --- |
30
- | `@citation-js/date` | 0.5.1 | MIT | [citation-js/date](https://github.com/citation-js/date) |
31
- | `@citation-js/name` | 0.4.2 | MIT | [citation-js/name](https://github.com/citation-js/name) |
32
- | `agent-base` | 9.0.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
33
- | `citeproc` | 2.4.63 | CPAL-1.0 OR AGPL-1.0 | [juris-m/citeproc-js](https://github.com/juris-m/citeproc-js) |
34
- | `commander` | 8.3.0 | MIT | [tj/commander.js](https://github.com/tj/commander.js) |
35
- | `debug` | 4.4.3 | MIT | [debug-js/debug](https://github.com/debug-js/debug) |
36
- | `ip-address` | 10.7.5 | MIT | [beaugunderson/ip-address](https://github.com/beaugunderson/ip-address) |
37
- | `moo` | 0.5.3 | BSD-3-Clause | [tjvr/moo](https://github.com/tjvr/moo) |
38
- | `ms` | 2.1.3 | MIT | [vercel/ms](https://github.com/vercel/ms) |
39
- | `proxy-agent-negotiate` | 1.1.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
40
- | `smart-buffer` | 4.2.0 | MIT | [JoshGlazebrook/smart-buffer](https://github.com/JoshGlazebrook/smart-buffer) |
41
- | `sync-fetch-undici` | 0.7.1 | MIT | [larsgw/sync-fetch](https://github.com/larsgw/sync-fetch) |
42
- | `whatwg-mimetype` | 5.0.0 | MIT | [jsdom/whatwg-mimetype](https://github.com/jsdom/whatwg-mimetype) |
28
+ | 包 | 版本 | 许可 | 上游 |
29
+ | ----------------------- | ------ | -------------------- | ----------------------------------------------------------------------------- |
30
+ | `@citation-js/date` | 0.5.1 | MIT | [citation-js/date](https://github.com/citation-js/date) |
31
+ | `@citation-js/name` | 0.4.2 | MIT | [citation-js/name](https://github.com/citation-js/name) |
32
+ | `agent-base` | 9.0.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
33
+ | `citeproc` | 2.4.63 | CPAL-1.0 OR AGPL-1.0 | [juris-m/citeproc-js](https://github.com/juris-m/citeproc-js) |
34
+ | `commander` | 8.3.0 | MIT | [tj/commander.js](https://github.com/tj/commander.js) |
35
+ | `debug` | 4.4.3 | MIT | [debug-js/debug](https://github.com/debug-js/debug) |
36
+ | `ip-address` | 10.7.5 | MIT | [beaugunderson/ip-address](https://github.com/beaugunderson/ip-address) |
37
+ | `moo` | 0.5.3 | BSD-3-Clause | [tjvr/moo](https://github.com/tjvr/moo) |
38
+ | `ms` | 2.1.3 | MIT | [vercel/ms](https://github.com/vercel/ms) |
39
+ | `proxy-agent-negotiate` | 1.1.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
40
+ | `smart-buffer` | 4.2.0 | MIT | [JoshGlazebrook/smart-buffer](https://github.com/JoshGlazebrook/smart-buffer) |
41
+ | `sync-fetch-undici` | 0.7.1 | MIT | [larsgw/sync-fetch](https://github.com/larsgw/sync-fetch) |
42
+ | `whatwg-mimetype` | 5.0.0 | MIT | [jsdom/whatwg-mimetype](https://github.com/jsdom/whatwg-mimetype) |
43
+
44
+ ## 随包分发的字体
45
+
46
+ 插件自带两款中文兜底字体(D207),位于 `lib/fonts/`。它们**不是 npm 依赖**,是从 Google Fonts 取完整字体后自行子集化的产物(GB2312 一级常用字 3755 + ASCII + 常用标点,共 3880 字)。
47
+
48
+ | 字体 | 文件 | 大小 | 许可 | 上游 |
49
+ | ------------- | -------------------------- | ------ | ----------- | ----------------------------------------------------------- |
50
+ | Noto Serif SC | `NotoSerifSC-common.woff2` | 679 KB | SIL OFL 1.1 | [notofonts/noto-cjk](https://github.com/notofonts/noto-cjk) |
51
+ | Noto Sans SC | `NotoSansSC-common.woff2` | 525 KB | SIL OFL 1.1 | [notofonts/noto-cjk](https://github.com/notofonts/noto-cjk) |
52
+
53
+ **OFL 的要求**:保留版权声明与许可原文;若修改了字体,不得使用原作者的保留字体名作为自己的产品名。因此,本插件把子集声明为 `Paper Noto Serif SC` / `Paper Noto Sans SC` 两个自有族名,字体本位图的派生也未被用于宣传。
43
54
 
44
55
  ## 由宿主提供(不随本包安装)
45
56
 
46
57
  这些包由 DSH 宿主在运行时供给,本插件的 `package.json` 只在 `peerDependencies` 里声明版本区间,不把副本装进自己的安装树。
47
58
 
48
- | 包 | 版本区间 | 许可 | 上游 |
49
- | --- | --- | --- | --- |
50
- | `@deepseek-ai/schemastery` | `^3.18.1` | MIT | [deepseek-ai/deepseek-harness](https://github.com/deepseek-ai/deepseek-harness) |
51
- | `@deepseek-ai/cosmokit` | 随 schemastery | MIT | [deepseek-ai/deepseek-harness](https://github.com/deepseek-ai/deepseek-harness) |
52
- | `@standard-schema/spec` | 随 schemastery | MIT | [standard-schema/standard-schema](https://github.com/standard-schema/standard-schema) |
59
+ | 包 | 版本区间 | 许可 | 上游 |
60
+ | -------------------------- | -------------- | ---- | ------------------------------------------------------------------------------------- |
61
+ | `@deepseek-ai/schemastery` | `^3.18.1` | MIT | [deepseek-ai/deepseek-harness](https://github.com/deepseek-ai/deepseek-harness) |
62
+ | `@deepseek-ai/cosmokit` | 随 schemastery | MIT | [deepseek-ai/deepseek-harness](https://github.com/deepseek-ai/deepseek-harness) |
63
+ | `@standard-schema/spec` | 随 schemastery | MIT | [standard-schema/standard-schema](https://github.com/standard-schema/standard-schema) |
53
64
 
54
65
  ## 需要单独说明的许可
55
66
 
@@ -1,5 +1,5 @@
1
1
  import { readFile } from 'node:fs/promises'
2
- import { dirname, extname, isAbsolute, relative, resolve, sep } from 'node:path'
2
+ import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path'
3
3
  import { isPageImageUnavailable } from './page-image.js'
4
4
 
5
5
  /**
@@ -27,6 +27,7 @@ export function createAssetRoutes(deps = {}) {
27
27
  const renderPageImages = typeof deps.renderPageImages === 'function' ? deps.renderPageImages : null
28
28
  const cacheDir = typeof deps.cacheDir === 'string' ? deps.cacheDir.trim() : ''
29
29
  const command = typeof deps.command === 'string' && deps.command.trim() !== '' ? deps.command : undefined
30
+ const fontDir = typeof deps.fontDir === 'string' ? deps.fontDir.trim() : ''
30
31
 
31
32
  /** `GET /analysis-asset?documentId=&path=`:结果目录内的图片。 */
32
33
  async function analysisAsset(request, url) {
@@ -97,7 +98,40 @@ export function createAssetRoutes(deps = {}) {
97
98
  return byteResponse(image.data, PAGE_IMAGE_CONTENT_TYPE)
98
99
  }
99
100
 
100
- return { 'analysis-asset': analysisAsset, 'page-image': pageImage }
101
+ /**
102
+ * `GET /font-file?name=`:随包自带的中文分片字体(D207)。
103
+ *
104
+ * 🔴 文件名走**白名单正则**而不是路径拼接:`..`、分隔符、绝对路径一个都进不来,
105
+ * 也不需要 relative() 那种越界判定 —— 正则本身就把范围钉死在"编号或语种 + 数字"的形状上。
106
+ *
107
+ * 🔴 缓存一年:文件名里带着分片编号,字体内容变了编号就变了,所以长缓存安全。
108
+ * 这一点与图片那两条不同 —— 图片是私有一小时(按 documentId 变),字体是包内静态资产。
109
+ *
110
+ * 缺文件回 404 而不是 500:客户端据此判断"这个分片没有",静默跳过即可 ——
111
+ * 分片缺一片只会让那几个字落到下一档字体,不该在控制台留 500 的红字。
112
+ */
113
+ async function fontFile(request, url) {
114
+ if (fontDir === '') return textResponse('字体目录未接线', 503)
115
+ const params = searchParamsOf(request, url)
116
+ const name = (params.get('name') ?? '').trim()
117
+ if (!FONT_FILE_PATTERN.test(name)) return textResponse('字体文件名非法', 400)
118
+ let data
119
+ try {
120
+ data = await readFile(join(fontDir, name))
121
+ } catch {
122
+ return textResponse('字体分片不存在', 404)
123
+ }
124
+ return new Response(data, {
125
+ status: 200,
126
+ headers: {
127
+ 'Content-Type': 'font/woff2',
128
+ 'Content-Length': String(data.byteLength),
129
+ 'Cache-Control': FONT_CACHE_CONTROL,
130
+ },
131
+ })
132
+ }
133
+
134
+ return { 'analysis-asset': analysisAsset, 'page-image': pageImage, 'font-file': fontFile }
101
135
  }
102
136
 
103
137
  /** 扩展名 → Content-Type。**不猜**:表里没有的一律 application/octet-stream。 */
@@ -111,6 +145,19 @@ const OCTET_STREAM = 'application/octet-stream'
111
145
  const PAGE_IMAGE_CONTENT_TYPE = 'image/webp'
112
146
  /** 结果目录内的图片与内容寻址的页图都属于「私有一小时的静态资源」。 */
113
147
  const CACHE_CONTROL = 'private, max-age=3600'
148
+ /** 字体是包内静态资产:文件名即内容标识,缓存一年。 */
149
+ const FONT_CACHE_CONTROL = 'public, max-age=31536000, immutable'
150
+ /**
151
+ * 字体文件名白名单。只认随包自带的这两款 Noto 的命名形状:
152
+ * `NotoSerifSC-common.woff2` / `NotoSansSC-common.woff2`(当前实际发布的子集),
153
+ * 以及 @fontsource 那套 `noto-<serif|sans>-sc-<编号>-400-normal.woff2` 切片 ——
154
+ * 后者留作将来换回切片方案的余地,两种形态都钉在这条正则里。
155
+ *
156
+ * 🔴 用**闭集正则**而不是"不含 `..`"这类黑名单:黑名单永远漏一种写法,
157
+ * 正则可以反过来把整个命名空间钉死。哪天要加第三款,是改这一行加一条分支,
158
+ * 而不是放宽成通配。
159
+ */
160
+ const FONT_FILE_PATTERN = /^(?:Noto(?:Serif|Sans)SC-common|noto-(?:serif|sans)-sc-(?:\d{1,3}|latin|cyrillic|vietnamese)(?:-ext)?-400-normal)\.woff2$/
114
161
  /** UI 高清变体;翻译侧走 896,两档在缓存键里分开(D49),互不干扰。 */
115
162
  const DEFAULT_PAGE_WIDTH = 1200
116
163
  /** width 直接决定 uvx 渲染出的位图大小,不设上限就是一个内存放大器。 */
@@ -2655,9 +2655,204 @@
2655
2655
  // 写不进去就算了:下次打开退回默认值,不该为此打断阅读。
2656
2656
  }
2657
2657
  }
2658
- /** 排版偏好(字号 / 行距)。 */
2658
+ /**
2659
+ * 字体族清单。**按语言分开两份**(用户裁定:原文是英文,不该被中文字体名接管)。
2660
+ *
2661
+ * 🔴 为什么要分:界面上那三个按钮改的是**当前档位**那一套排版,而双语/并排档里
2662
+ * 原文段与译文段同时在场。共用一份清单时,中文档选的「宋体」会原样落到英文段上 ——
2663
+ * 宋体没有拉丁字形,浏览器只能回退,读者的选择于是看起来「没生效」。
2664
+ *
2665
+ * 🔴 「系统默认」是**栈本身**为空,不是名字叫 'system-ui':空栈表示不写 font-family,
2666
+ * 由浏览器用自己那套默认字体渲染(Chrome 在中文 Windows 上是宋体 / 雅黑)。
2667
+ * 写死 system-ui 反而会剥夺用户改浏览器默认字体的能力,那是另一回事。
2668
+ *
2669
+ * 固定清单,不给自由输入 —— 允许任意字符串等于允许读者把正文变成豆腐块,
2670
+ * 而浏览器又不提供"列出系统字体"的接口(字体枚举要用户授权且各浏览器行为不一)。
2671
+ *
2672
+ * 🔴 **每一档都必须是"换台电脑也还在"的字体**(用户裁定:这些字体要在别的机器上也可用)。
2673
+ * 选名依据是官方清单,不是本机 `C:\Windows\Fonts` 里有什么:
2674
+ *
2675
+ * · Windows 11 核心字体清单(learn.microsoft.com/typography/fonts/windows_11_font_list)
2676
+ * —— 任何语言版 Windows 都带的那批;
2677
+ * · W3C clreq《Recommended System Font Mappings for Generic Chinese Font Families》
2678
+ * —— 逐款标了 Built-in / OS optional / Downloadable,还给了 macOS 与 Linux 的名字。
2679
+ *
2680
+ * 于是栈的排法是**跨平台优先级**而非"本机有什么排前面":每档第一个名字是至少一个主流平台
2681
+ * 一定有的,后面依次是另外两个平台的对应款,末尾永远是 CSS 通用族。换一台没有第一款字体
2682
+ * 的机器,浏览器顺着往下找,最差落到通用族 —— 仍是完整字形,不会是豆腐块。
2683
+ *
2684
+ * 🔴 特意**没有**收进来的(它们在本机上确实存在,但不保证别人也有):
2685
+ * · Noto Sans/Serif SC、HarmonyOS Sans SC —— 微软核心清单里没有,是第三方装上去的;
2686
+ * · 方正系列(FZShuTi / FZYaoTi)、华文系列(STSong / STKaiti / STFangsong / STXingkai /
2687
+ * STLiti / STXihei…)—— Windows 上来自 Office,macOS 上才是系统自带,
2688
+ * 所以它们只作为**栈中间的一环**出现,绝不单独成为某一档的唯一希望;
2689
+ * · 隶书 LiSu、幼圆 YouYuan —— 同上,早期 Office 内置,不是 Windows 自带。
2690
+ * 这些名字留在栈里是有用的(装了 Office 的机器能命中),但**没有任何一档以它们打头**。
2691
+ *
2692
+ * ⚠️ 楷体在 Windows 上是 simkai.ttf,英文族名 `KaiTi`;`STKaiti` 是另一款
2693
+ * (STKAITI.TTF,华文楷体),两者不是同一款字,所以栈里是两个名字、不能说谁代替谁。
2694
+ */
2695
+ const SRC_FONT_FAMILIES = Object.freeze([
2696
+ Object.freeze({ value: 'system', label: '系统默认', stack: '' }),
2697
+ Object.freeze({
2698
+ value: 'serif', label: '衬线', group: '衬线体',
2699
+ stack: "Georgia,'Times New Roman',Times,'Source Serif Pro',serif",
2700
+ }),
2701
+ Object.freeze({
2702
+ value: 'classic', label: '古典', group: '衬线体',
2703
+ stack: "'Iowan Old Style',Charter,Constantia,Cambria,Georgia,serif",
2704
+ }),
2705
+ Object.freeze({
2706
+ value: 'book', label: '书籍', group: '衬线体',
2707
+ stack: "'Palatino Linotype',Palatino,'Book Antiqua',Garamond,Georgia,serif",
2708
+ }),
2709
+ Object.freeze({
2710
+ value: 'sans', label: '无衬线', group: '无衬线体',
2711
+ stack: "'Segoe UI','Helvetica Neue',Helvetica,Arial,'Liberation Sans',sans-serif",
2712
+ }),
2713
+ Object.freeze({
2714
+ value: 'humanist', label: '人文', group: '无衬线体',
2715
+ stack: "Verdana,Tahoma,'Trebuchet MS','Gill Sans MT','Gill Sans',sans-serif",
2716
+ }),
2717
+ Object.freeze({
2718
+ value: 'mono', label: '等宽',
2719
+ stack: "'Cascadia Mono',Consolas,Menlo,'DejaVu Sans Mono','Liberation Mono',monospace",
2720
+ }),
2721
+ Object.freeze({
2722
+ value: 'kai', label: '手写',
2723
+ stack: "'Segoe Script','Segoe Print','Bradley Hand','Comic Sans MS',cursive",
2724
+ }),
2725
+ ])
2726
+ const ZH_FONT_FAMILIES = Object.freeze([
2727
+ Object.freeze({ value: 'system', label: '系统默认', stack: '' }),
2728
+ // 🔴 顺序就是「系统优先、自带兜底」(用户裁定,D207):SimSun / 微软雅黑 排在最前,
2729
+ // 机器上有原版就用原版;没有时落到随包自带的 Paper Noto(D207),任何平台都渲染得出。
2730
+ // 浏览器从栈左往右找第一个**存在**的字体,不需要任何运行时检测。
2731
+ Object.freeze({
2732
+ value: 'serif', label: '宋体', group: '印刷体',
2733
+ stack: "SimSun,'Songti SC',STSong,'Paper Noto Serif SC','Noto Serif CJK SC','Source Han Serif SC',serif",
2734
+ }),
2735
+ Object.freeze({
2736
+ value: 'sans', label: '黑体', group: '印刷体',
2737
+ stack: "'Microsoft YaHei','Heiti SC','PingFang SC','Paper Noto Sans SC','Noto Sans CJK SC','Source Han Sans SC','WenQuanYi Zen Hei',sans-serif",
2738
+ }),
2739
+ Object.freeze({
2740
+ value: 'kai', label: '楷体', group: '印刷体',
2741
+ stack: "KaiTi,'Kaiti SC',STKaiti,'LXGW WenKai',serif",
2742
+ }),
2743
+ Object.freeze({
2744
+ value: 'fangsong', label: '仿宋', group: '印刷体',
2745
+ stack: "FangSong,STFangsong,'Noto Serif CJK SC',serif",
2746
+ }),
2747
+ Object.freeze({
2748
+ value: 'yuan', label: '圆体', group: '印刷体',
2749
+ stack: "'Yuanti SC',YouYuan,'Microsoft YaHei',sans-serif",
2750
+ }),
2751
+ Object.freeze({
2752
+ value: 'xingkai', label: '行楷', group: '书体',
2753
+ stack: "STXingkai,'Xingkai SC',KaiTi,cursive",
2754
+ }),
2755
+ Object.freeze({
2756
+ value: 'lishu', label: '隶书', group: '书体',
2757
+ stack: "LiSu,'Libian SC',STLiti,serif",
2758
+ }),
2759
+ ])
2760
+ /** 某一侧的字体清单。side 是 'src' / 'zh'。 */
2761
+ function fontFamiliesOf(side) {
2762
+ return side === 'src' ? SRC_FONT_FAMILIES : ZH_FONT_FAMILIES
2763
+ }
2764
+ /**
2765
+ * 字体菜单的 items(宿主 Menu 的三态条目:普通项 / 分组标题 / 分隔线)。
2766
+ *
2767
+ * 🔴 分组标题只在**同组两项以上**时插:一项底下顶个标题只是噪声。
2768
+ * 中文那侧「印刷体(五项)/ 书体(两项)」的分法是有意义的 —— 前者拿来读,后者拿来摆;
2769
+ * 英文那侧衬线组三项、无衬线组两项也够格,等宽与手写各一项,于是不出标题。
2770
+ *
2771
+ * ⚠️ 组名**必须与所有档位名错开**:两者撞名时菜单上会出现两个一模一样的词,
2772
+ * 一个点得动一个点不动(第一版英文组名写成「衬线」,与档位「衬线」撞了个正着)。
2773
+ *
2774
+ * 标题的 id 加 group: 前缀,纯粹为了不与某个档位的 value 撞车 —— 宿主拿它当 key。
2775
+ */
2776
+ function fontMenuItemsOf(side) {
2777
+ const list = fontFamiliesOf(side)
2778
+ const counts = new Map()
2779
+ for (const entry of list) {
2780
+ if (entry.group === undefined) continue
2781
+ counts.set(entry.group, (counts.get(entry.group) ?? 0) + 1)
2782
+ }
2783
+ const items = []
2784
+ let current
2785
+ for (const entry of list) {
2786
+ const group = entry.group
2787
+ if (group !== current && group !== undefined && (counts.get(group) ?? 0) > 1) {
2788
+ items.push({ type: 'label', id: 'group:' + group, text: group })
2789
+ }
2790
+ if (group !== undefined) current = group
2791
+ items.push({ id: entry.value, label: entry.label })
2792
+ }
2793
+ return items
2794
+ }
2795
+ /**
2796
+ * 把存下来的 family 解析成某一侧真正要用的栈。**每侧各用各的清单**:
2797
+ * 旧数据里译文存的 'serif' 拿到原文那侧仍然叫 'serif',但栈换成了拉丁字体。
2798
+ * 认不出的值回落该侧的第一项(系统默认)—— 与"缺字段回默认"同一口径。
2799
+ */
2800
+ function fontStackOf(side, value) {
2801
+ const list = fontFamiliesOf(side)
2802
+ const entry = list.find(item => item.value === value)
2803
+ return (entry === undefined ? list[0] : entry).stack
2804
+ }
2805
+ /** 字体族取值校验。认不出的回落该侧的默认档 —— 每侧清单不同,校验也要按侧来。 */
2806
+ function fontOf(side, value) {
2807
+ return fontFamiliesOf(side).some(entry => entry.value === value) ? value : fontFamiliesOf(side)[0].value
2808
+ }
2809
+
2810
+ /**
2811
+ * 排版偏好(字号 / 行距 / 字体族),**原文与译文各一套**。
2812
+ *
2813
+ * 🔴 **全局一份、不按论文分**(D152 原意不变):它答的是"我习惯怎么读",不是"这一篇怎么看"。
2814
+ *
2815
+ * 🔴 **旧格式要能读**。拆包之前存的是 `{ size, height }`(只有一套),读者机器上可能就躺着
2816
+ * 这么一个对象。读成"译文那套",原文整组用默认值 —— 升级不丢读者已经调好的字号。
2817
+ * 反过来说:把旧字段直接丢弃、整块回落默认,等于悄悄把人家调了半天的字号重置掉。
2818
+ *
2819
+ * @returns {{src: {size: number, height: number, family: string}, zh: {size: number, height: number, family: string}}}
2820
+ */
2659
2821
  function readTypePref() { return readPref(TYPE_PREF_KEY) }
2660
- function writeTypePref(size, height) { writePref(TYPE_PREF_KEY, { size, height }) }
2822
+ function typePref() {
2823
+ const raw = readTypePref()
2824
+ const pick = (side, group, fallback) => {
2825
+ const source = group !== null && typeof group === 'object' ? group : null
2826
+ // 旧格式没有 src/zh 分组,size/height 直接躺在顶层。
2827
+ const legacy = source === null && fallback === 'legacy' ? raw : null
2828
+ const sizeOf = value => clampType(value, MIN_FONT_SIZE, MAX_FONT_SIZE, DEFAULT_FONT_SIZE)
2829
+ const heightOf = value => clampType(value, MIN_LINE_HEIGHT, MAX_LINE_HEIGHT, DEFAULT_LINE_HEIGHT)
2830
+ if (legacy !== null) {
2831
+ // 旧数据没有 family 字段。原文那侧回「系统默认」,译文那侧回宋体 ——
2832
+ // 两侧清单不同,同一个字面量在两边指向的不是同一套字体。
2833
+ return { size: sizeOf(legacy.size), height: heightOf(legacy.height), family: side === 'src' ? 'system' : 'serif' }
2834
+ }
2835
+ return {
2836
+ size: sizeOf(source?.size),
2837
+ height: heightOf(source?.height),
2838
+ family: fontOf(side, source?.family),
2839
+ }
2840
+ }
2841
+ return {
2842
+ src: pick('src', raw?.src, 'default'),
2843
+ // 🔴 顶层旧字段只喂给译文那套:它是拆包前唯一存在的那一套。
2844
+ zh: pick('zh', raw?.zh, 'legacy'),
2845
+ }
2846
+ }
2847
+ /**
2848
+ * 写一套排版偏好。
2849
+ *
2850
+ * 🔴 **每次都要写全**,不能只写改动的那一组:`writePref` 是整键覆盖,
2851
+ * 只写 `{zh}` 会把 `src` 从磁盘上抹掉 —— 读者改完译文再刷新,原文那套就没了。
2852
+ *
2853
+ * @param {object} next 完整的三组值。
2854
+ */
2855
+ function writeTypePref(next) { writePref(TYPE_PREF_KEY, next) }
2661
2856
  /**
2662
2857
  * 视图档位的取值校验(D152)。只认下拉里真有的档 —— 手改过的 localStorage 不该让选择框
2663
2858
  * 落在一个不存在的值上(那时正文按 zh 渲染、选择框却显示别的,看着像「设置没生效」)。