@a200888213/gbdocx 0.6.0 → 0.6.2

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.
Files changed (76) hide show
  1. package/package.json +1 -1
  2. package/src/app/file-io.js +122 -122
  3. package/src/app/main.js +9 -9
  4. package/src/app/word/catalog.js +52 -52
  5. package/src/app/word/comments.js +112 -112
  6. package/src/app/word/doc.js +113 -113
  7. package/src/app/word/edit.js +72 -72
  8. package/src/app/word/hf.js +76 -76
  9. package/src/app/word/layout.js +155 -155
  10. package/src/app/word/notes.js +110 -110
  11. package/src/app/word/shared.js +22 -22
  12. package/src/app/word/view.js +3 -0
  13. package/src/app/word-app.js +1 -0
  14. package/src/app/word-sheets.js +101 -101
  15. package/src/kernel/docx/codec/blocks.js +123 -123
  16. package/src/kernel/docx/codec/carry.js +129 -129
  17. package/src/kernel/docx/codec/comments.js +25 -25
  18. package/src/kernel/docx/codec/docx.js +198 -198
  19. package/src/kernel/docx/codec/docxStyles.js +177 -177
  20. package/src/kernel/docx/codec/fonts.js +195 -195
  21. package/src/kernel/docx/codec/helpers.js +161 -161
  22. package/src/kernel/docx/codec/hfHtml.js +152 -152
  23. package/src/kernel/docx/codec/html.js +739 -739
  24. package/src/kernel/docx/codec/losses.js +420 -420
  25. package/src/kernel/docx/codec/markdown.js +170 -170
  26. package/src/kernel/docx/codec/parts.js +249 -249
  27. package/src/kernel/docx/codec/read.js +603 -603
  28. package/src/kernel/docx/codec/render.js +1156 -1156
  29. package/src/kernel/docx/codec/roundtrip.js +236 -236
  30. package/src/kernel/docx/codec/write.js +833 -833
  31. package/src/kernel/docx/comments.js +14 -14
  32. package/src/kernel/docx/pageGeometry.js +123 -123
  33. package/src/kernel/docx/pagination.js +481 -481
  34. package/src/kernel/docx/watermark.js +45 -45
  35. package/src/kernel/editor/constants.js +8 -8
  36. package/src/kernel/editor/engine/blocks.js +594 -594
  37. package/src/kernel/editor/engine/blocksEnter.js +109 -109
  38. package/src/kernel/editor/engine/blocksInsert.js +254 -254
  39. package/src/kernel/editor/engine/blocksStructure.js +226 -226
  40. package/src/kernel/editor/engine/commands.js +181 -181
  41. package/src/kernel/editor/engine/dom.js +646 -646
  42. package/src/kernel/editor/engine/history.js +149 -149
  43. package/src/kernel/editor/engine/inline.js +656 -656
  44. package/src/kernel/editor/engine/input.js +554 -554
  45. package/src/kernel/editor/engine/interaction.js +137 -137
  46. package/src/kernel/editor/engine/pointer.js +39 -39
  47. package/src/kernel/editor/engine/revision.js +138 -138
  48. package/src/kernel/editor/engine/schema.js +417 -417
  49. package/src/kernel/editor/engine/selection.js +119 -119
  50. package/src/kernel/editor/engine/state.js +118 -118
  51. package/src/kernel/editor/engine/tableClip.js +154 -154
  52. package/src/kernel/editor/engine/tableGrid.js +55 -55
  53. package/src/kernel/editor/engine/tableInteraction.js +388 -388
  54. package/src/kernel/editor/engine/tableMenu.js +250 -250
  55. package/src/kernel/editor/engine/tableOps.js +174 -174
  56. package/src/kernel/editor/engine/tables.js +732 -732
  57. package/src/kernel/editor/overlayGeometry.js +20 -20
  58. package/src/kernel/editor/utils.js +48 -48
  59. package/src/kernel/keyboard.js +114 -114
  60. package/src/kernel/textStyles.js +84 -84
  61. package/src/kernel/util/imageFile.js +91 -91
  62. package/src/kernel/util/shortcutKey.js +33 -33
  63. package/src/surface/editor-surface.js +600 -600
  64. package/src/ui/color-panel.js +69 -69
  65. package/src/ui/context-menu.js +133 -133
  66. package/src/ui/find-bar.js +151 -151
  67. package/src/ui/menubar.js +28 -28
  68. package/src/ui/panels.js +115 -115
  69. package/src/ui/prompt.js +89 -89
  70. package/src/ui/ribbon.js +485 -485
  71. package/src/ui/special-chars.js +39 -39
  72. package/src/ui/statusbar.js +81 -81
  73. package/src/ui/toolbar.js +32 -32
  74. package/styles/editor-content.css +440 -440
  75. package/styles/tokens.css +154 -154
  76. package/styles/word-panel.css +1 -1
@@ -1,195 +1,195 @@
1
- // [文件] web·docx·codec·fonts —— rFonts ⇄ CSS font-family:拉丁/东亚两套字形分工、未装字体按同族近似兜底、回读拆分
2
- //
3
- // [设计] Word 一个 run 带 ascii(拉丁)与 eastAsia(中日韩)两个字体名;CSS 是「逐字符沿字体链找第一个含该字形的
4
- // 字体」,所以把 ascii 排在前、eastAsia 紧随其后,正好等价于 Word 的分工,不必再按字符拆标签。
5
- // [坑] 只取 ascii 会让整篇中文落到浏览器回退字体(系统默认无衬线),字宽一变断行与页数跟着全错——
6
- // 这是「打开别人写的 docx,版式全乱」的根因,不是行高或页边距的问题。
7
-
8
- import { sanitizeFontName } from '../../util/safeValue.js';
9
-
10
- /** CSS 通用族:只是兜底关键字,不是字体名 */
11
- const GENERIC = new Set(['serif', 'sans-serif', 'monospace', 'cursive', 'fantasy', 'system-ui']);
12
-
13
- /** 拉丁字体的度量兼容替代(Carlito↔Calibri、Caladea↔Cambria、Gelasio↔Georgia 三对字宽几乎相同) */
14
- const LATIN_STACKS = {
15
- 'times new roman': 'Times,Georgia,serif',
16
- arial: 'Helvetica,Helvetica Neue,Liberation Sans,sans-serif',
17
- calibri: 'Carlito,Segoe UI,sans-serif',
18
- 'calibri light': 'Carlito,Segoe UI,sans-serif',
19
- cambria: 'Caladea,Georgia,serif',
20
- georgia: 'Gelasio,Georgia,serif',
21
- 'courier new': 'Courier,monospace',
22
- consolas: 'Inconsolata,Menlo,monospace',
23
- 'segoe ui': 'Segoe UI,Helvetica,Helvetica Neue,sans-serif',
24
- verdana: 'DejaVu Sans,Verdana,Tahoma,sans-serif',
25
- tahoma: 'DejaVu Sans,Tahoma,Verdana,sans-serif'
26
- };
27
-
28
- const latinFallbackOf = (name) => LATIN_STACKS[name.toLowerCase()] || 'serif';
29
-
30
- /**
31
- * 东亚字体族:同一字形设计在 Windows / macOS / Linux 上的常见名字。
32
- * 链首是英文规范名(各平台都按它认字体),中文名紧随其后(中文版 Office 装的是中文名)。
33
- * @type {Array<{names: string[], stack: string}>}
34
- */
35
- const EA_FAMILIES = [
36
- {
37
- names: ['宋体', 'simsun', '新宋体', 'nsimsun', '宋体-简', 'songti sc', 'stsong', '华文宋体'],
38
- stack: 'SimSun,Songti SC,STSong,Noto Serif CJK SC,Noto Serif SC,Source Han Serif SC,AR PL UMing CN,serif'
39
- },
40
- {
41
- names: ['仿宋', 'fangsong', '仿宋_gb2312', 'fzfangsong-z02s', '方正仿宋简体', 'stfangsong', '华文仿宋', '仿宋-简'],
42
- stack: 'FangSong,STFangsong,仿宋,SimSun,serif'
43
- },
44
- {
45
- names: ['楷体', 'kaiti', '楷体_gb2312', 'kaiti sc', 'stkaiti', '华文楷体', '楷体-简', 'fzkai-z03s', '方正楷体简体'],
46
- stack: 'KaiTi,STKaiti,楷体,SimSun,serif'
47
- },
48
- {
49
- names: ['黑体', 'simhei', 'heiti sc', 'stheiti', '黑体-简', 'fzhei-b01s', '方正黑体简体'],
50
- stack: 'SimHei,Heiti SC,STHeiti,Microsoft YaHei,Noto Sans CJK SC,sans-serif'
51
- },
52
- {
53
- names: ['微软雅黑', 'microsoft yahei', '微软雅黑 light', 'microsoft yahei light'],
54
- stack: 'Microsoft YaHei,PingFang SC,Hiragino Sans GB,Noto Sans CJK SC,Source Han Sans SC,sans-serif'
55
- },
56
- {
57
- names: ['等线', 'dengxian', '等线 light', 'dengxian light'],
58
- stack: 'DengXian,Microsoft YaHei,PingFang SC,Noto Sans CJK SC,sans-serif'
59
- },
60
- {
61
- // 公文红头字体:没装时按小标宋/宋体字形走,字宽最接近
62
- names: ['方正小标宋简体', '方正小标宋_gbk', 'fzxiaobiaosong-b05s', '小标宋', '方正小标宋'],
63
- stack: 'FZXiaoBiaoSong-B05S,STZhongsong,华文中宋,SimSun,serif'
64
- },
65
- {
66
- names: ['华文中宋', 'stzhongsong', '中宋'],
67
- stack: 'STZhongsong,SimSun,serif'
68
- },
69
- {
70
- names: ['隶书', 'lisu'],
71
- stack: 'LiSu,STLiti,SimSun,serif'
72
- },
73
- {
74
- names: ['幼圆', 'youyuan'],
75
- stack: 'YouYuan,STXihei,SimHei,sans-serif'
76
- },
77
- {
78
- names: ['新細明體', 'pmingliu', '細明體', 'mingliu', '標楷體', 'dfkai-sb'],
79
- stack: 'PMingLiU,MingLiU,Songti TC,Noto Serif CJK TC,serif'
80
- },
81
- {
82
- names: ['微軟正黑體', 'microsoft jhenghei', 'jhenghei'],
83
- stack: 'Microsoft JhengHei,PingFang TC,Noto Sans CJK TC,sans-serif'
84
- },
85
- {
86
- names: ['ms 明朝', 'ms mincho', 'ms p明朝', 'ms pmincho', '游明朝', 'yu mincho'],
87
- stack: 'Yu Mincho,YuMincho,MS Mincho,Hiragino Mincho ProN,Noto Serif CJK JP,serif'
88
- },
89
- {
90
- names: ['ms ゴシック', 'ms gothic', 'ms pゴシック', 'ms pgothic', '游ゴシック', 'yu gothic', 'メイリオ', 'meiryo'],
91
- stack: 'Yu Gothic,YuGothic,Meiryo,MS Gothic,Hiragino Sans,Noto Sans CJK JP,sans-serif'
92
- },
93
- {
94
- names: ['맑은 고딕', 'malgun gothic', '돋움', 'dotum', '굴림', 'gulim'],
95
- stack: 'Malgun Gothic,Apple SD Gothic Neo,Noto Sans CJK KR,sans-serif'
96
- },
97
- {
98
- names: ['바탕', 'batang', '궁서', 'gungsuh'],
99
- stack: 'Batang,AppleMyungjo,Noto Serif CJK KR,serif'
100
- }
101
- ];
102
-
103
- /** 字体名(小写)→ 族定义 */
104
- const EA_BY_NAME = new Map();
105
- /** 所有算作东亚字体的名字(族名 + 各自兜底链里的每个名字,含 Noto/思源这类纯 ASCII 名) */
106
- const EA_NAMES = new Set();
107
- for (const family of EA_FAMILIES) {
108
- for (const name of family.names) {
109
- EA_BY_NAME.set(name.toLowerCase(), family);
110
- EA_NAMES.add(name.toLowerCase());
111
- }
112
- for (const name of family.stack.split(',')) {
113
- const key = name.trim().toLowerCase();
114
- if (key !== '' && !GENERIC.has(key)) EA_NAMES.add(key);
115
- }
116
- }
117
-
118
- /** 含中日韩字符或全角形式:这类字体的字宽占满整字宽,直接决定断行 */
119
- const HAS_CJK = /[\u1100-\u11ff\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\ufe30-\ufe4f\uff00-\uffef]/;
120
-
121
- /** 该字体名是否东亚字体(判定只看名字,与本机装没装无关) */
122
- export const isEaFont = (name) => HAS_CJK.test(String(name)) || EA_NAMES.has(String(name).toLowerCase());
123
-
124
- /** 东亚字体的兜底链;未登记的东亚字体按宋体近似 */
125
- const eaStackOf = (name) => (EA_BY_NAME.get(String(name).toLowerCase()) || EA_BY_NAME.get('宋体')).stack;
126
-
127
- /**
128
- * 一个 run 的 CSS 字体链:拉丁名 + 拉丁兜底 + 东亚名 + 东亚兜底
129
- * @param {string} latin w:ascii(缺省取 w:hAnsi)
130
- * @param {string} ea w:eastAsia
131
- * @returns {string} 逗号分隔、不带引号的字体链;两个名都为空时 ''
132
- */
133
- export const fontFamilyOf = (latin, ea) => {
134
- const names = [];
135
- const seen = new Set();
136
- let generic = 'serif';
137
- const push = (list) => {
138
- for (const raw of String(list).split(',')) {
139
- const name = raw.trim();
140
- if (name === '') continue;
141
- const key = name.toLowerCase();
142
- // 通用族只能在链尾出现一次:出现在中间会把后面的真字体全挡掉
143
- if (GENERIC.has(key)) {
144
- generic = key;
145
- continue;
146
- }
147
- if (seen.has(key)) continue;
148
- seen.add(key);
149
- names.push(name);
150
- }
151
- };
152
- const latinName = sanitizeFontName(latin);
153
- const eaName = sanitizeFontName(ea);
154
- if (latinName !== '') {
155
- push(latinName);
156
- push(isEaFont(latinName) ? eaStackOf(latinName) : latinFallbackOf(latinName));
157
- }
158
- if (eaName !== '' && eaName.toLowerCase() !== latinName.toLowerCase()) {
159
- push(eaName);
160
- push(eaStackOf(eaName));
161
- }
162
- if (names.length) names.push(generic);
163
- return names.join(',');
164
- };
165
-
166
- /**
167
- * 反解 HTML 的 font-family 链 → { latin, ea },供写出端还原 w:rFonts
168
- * [口径] 链是「拉丁组 + 东亚组」,每组末尾跟着自己那一族的兜底链,兜底名不参与还原:
169
- * 链首非东亚名 → 它是 ascii,链上第一个东亚名是 eastAsia;
170
- * 链首就是东亚名(Word 里 ascii 也填了中文字体)→ 跳过它自己那一族的兜底链,下一个陌生东亚名才是 eastAsia。
171
- */
172
- export const parseFontFamily = (css) => {
173
- const names = String(css || '')
174
- .split(',')
175
- .map((s) => sanitizeFontName(s))
176
- .filter((s) => s !== '' && !GENERIC.has(s.toLowerCase()));
177
- const first = names[0] || '';
178
- if (first === '') return { latin: '', ea: '' };
179
- if (!isEaFont(first)) return { latin: first, ea: names.find(isEaFont) || first };
180
- const own = new Set(eaStackOf(first).split(',').map((s) => s.trim().toLowerCase()));
181
- const next = names.slice(1).find((n) => !own.has(n.toLowerCase()));
182
- return { latin: first, ea: next && isEaFont(next) ? next : first };
183
- };
184
-
185
- /**
186
- * 落盘用的两个字体名:显式给了东亚名就用它,否则从字体链里反解
187
- * (编辑器只存一条 font-family 链,两种来源必须收敛到同一处判定)
188
- */
189
- export const fontPartsOf = (font, ea) => {
190
- const parsed = parseFontFamily(font);
191
- const east = sanitizeFontName(ea) || parsed.ea;
192
- const west = sanitizeFontName(parsed.latin) || east;
193
- return { latin: west, ea: east || west };
194
- };
195
-
1
+ // [文件] web·docx·codec·fonts —— rFonts ⇄ CSS font-family:拉丁/东亚两套字形分工、未装字体按同族近似兜底、回读拆分
2
+ //
3
+ // [设计] Word 一个 run 带 ascii(拉丁)与 eastAsia(中日韩)两个字体名;CSS 是「逐字符沿字体链找第一个含该字形的
4
+ // 字体」,所以把 ascii 排在前、eastAsia 紧随其后,正好等价于 Word 的分工,不必再按字符拆标签。
5
+ // [坑] 只取 ascii 会让整篇中文落到浏览器回退字体(系统默认无衬线),字宽一变断行与页数跟着全错——
6
+ // 这是「打开别人写的 docx,版式全乱」的根因,不是行高或页边距的问题。
7
+
8
+ import { sanitizeFontName } from '../../util/safeValue.js';
9
+
10
+ /** CSS 通用族:只是兜底关键字,不是字体名 */
11
+ const GENERIC = new Set(['serif', 'sans-serif', 'monospace', 'cursive', 'fantasy', 'system-ui']);
12
+
13
+ /** 拉丁字体的度量兼容替代(Carlito↔Calibri、Caladea↔Cambria、Gelasio↔Georgia 三对字宽几乎相同) */
14
+ const LATIN_STACKS = {
15
+ 'times new roman': 'Times,Georgia,serif',
16
+ arial: 'Helvetica,Helvetica Neue,Liberation Sans,sans-serif',
17
+ calibri: 'Carlito,Segoe UI,sans-serif',
18
+ 'calibri light': 'Carlito,Segoe UI,sans-serif',
19
+ cambria: 'Caladea,Georgia,serif',
20
+ georgia: 'Gelasio,Georgia,serif',
21
+ 'courier new': 'Courier,monospace',
22
+ consolas: 'Inconsolata,Menlo,monospace',
23
+ 'segoe ui': 'Segoe UI,Helvetica,Helvetica Neue,sans-serif',
24
+ verdana: 'DejaVu Sans,Verdana,Tahoma,sans-serif',
25
+ tahoma: 'DejaVu Sans,Tahoma,Verdana,sans-serif'
26
+ };
27
+
28
+ const latinFallbackOf = (name) => LATIN_STACKS[name.toLowerCase()] || 'serif';
29
+
30
+ /**
31
+ * 东亚字体族:同一字形设计在 Windows / macOS / Linux 上的常见名字。
32
+ * 链首是英文规范名(各平台都按它认字体),中文名紧随其后(中文版 Office 装的是中文名)。
33
+ * @type {Array<{names: string[], stack: string}>}
34
+ */
35
+ const EA_FAMILIES = [
36
+ {
37
+ names: ['宋体', 'simsun', '新宋体', 'nsimsun', '宋体-简', 'songti sc', 'stsong', '华文宋体'],
38
+ stack: 'SimSun,Songti SC,STSong,Noto Serif CJK SC,Noto Serif SC,Source Han Serif SC,AR PL UMing CN,serif'
39
+ },
40
+ {
41
+ names: ['仿宋', 'fangsong', '仿宋_gb2312', 'fzfangsong-z02s', '方正仿宋简体', 'stfangsong', '华文仿宋', '仿宋-简'],
42
+ stack: 'FangSong,STFangsong,仿宋,SimSun,serif'
43
+ },
44
+ {
45
+ names: ['楷体', 'kaiti', '楷体_gb2312', 'kaiti sc', 'stkaiti', '华文楷体', '楷体-简', 'fzkai-z03s', '方正楷体简体'],
46
+ stack: 'KaiTi,STKaiti,楷体,SimSun,serif'
47
+ },
48
+ {
49
+ names: ['黑体', 'simhei', 'heiti sc', 'stheiti', '黑体-简', 'fzhei-b01s', '方正黑体简体'],
50
+ stack: 'SimHei,Heiti SC,STHeiti,Microsoft YaHei,Noto Sans CJK SC,sans-serif'
51
+ },
52
+ {
53
+ names: ['微软雅黑', 'microsoft yahei', '微软雅黑 light', 'microsoft yahei light'],
54
+ stack: 'Microsoft YaHei,PingFang SC,Hiragino Sans GB,Noto Sans CJK SC,Source Han Sans SC,sans-serif'
55
+ },
56
+ {
57
+ names: ['等线', 'dengxian', '等线 light', 'dengxian light'],
58
+ stack: 'DengXian,Microsoft YaHei,PingFang SC,Noto Sans CJK SC,sans-serif'
59
+ },
60
+ {
61
+ // 公文红头字体:没装时按小标宋/宋体字形走,字宽最接近
62
+ names: ['方正小标宋简体', '方正小标宋_gbk', 'fzxiaobiaosong-b05s', '小标宋', '方正小标宋'],
63
+ stack: 'FZXiaoBiaoSong-B05S,STZhongsong,华文中宋,SimSun,serif'
64
+ },
65
+ {
66
+ names: ['华文中宋', 'stzhongsong', '中宋'],
67
+ stack: 'STZhongsong,SimSun,serif'
68
+ },
69
+ {
70
+ names: ['隶书', 'lisu'],
71
+ stack: 'LiSu,STLiti,SimSun,serif'
72
+ },
73
+ {
74
+ names: ['幼圆', 'youyuan'],
75
+ stack: 'YouYuan,STXihei,SimHei,sans-serif'
76
+ },
77
+ {
78
+ names: ['新細明體', 'pmingliu', '細明體', 'mingliu', '標楷體', 'dfkai-sb'],
79
+ stack: 'PMingLiU,MingLiU,Songti TC,Noto Serif CJK TC,serif'
80
+ },
81
+ {
82
+ names: ['微軟正黑體', 'microsoft jhenghei', 'jhenghei'],
83
+ stack: 'Microsoft JhengHei,PingFang TC,Noto Sans CJK TC,sans-serif'
84
+ },
85
+ {
86
+ names: ['ms 明朝', 'ms mincho', 'ms p明朝', 'ms pmincho', '游明朝', 'yu mincho'],
87
+ stack: 'Yu Mincho,YuMincho,MS Mincho,Hiragino Mincho ProN,Noto Serif CJK JP,serif'
88
+ },
89
+ {
90
+ names: ['ms ゴシック', 'ms gothic', 'ms pゴシック', 'ms pgothic', '游ゴシック', 'yu gothic', 'メイリオ', 'meiryo'],
91
+ stack: 'Yu Gothic,YuGothic,Meiryo,MS Gothic,Hiragino Sans,Noto Sans CJK JP,sans-serif'
92
+ },
93
+ {
94
+ names: ['맑은 고딕', 'malgun gothic', '돋움', 'dotum', '굴림', 'gulim'],
95
+ stack: 'Malgun Gothic,Apple SD Gothic Neo,Noto Sans CJK KR,sans-serif'
96
+ },
97
+ {
98
+ names: ['바탕', 'batang', '궁서', 'gungsuh'],
99
+ stack: 'Batang,AppleMyungjo,Noto Serif CJK KR,serif'
100
+ }
101
+ ];
102
+
103
+ /** 字体名(小写)→ 族定义 */
104
+ const EA_BY_NAME = new Map();
105
+ /** 所有算作东亚字体的名字(族名 + 各自兜底链里的每个名字,含 Noto/思源这类纯 ASCII 名) */
106
+ const EA_NAMES = new Set();
107
+ for (const family of EA_FAMILIES) {
108
+ for (const name of family.names) {
109
+ EA_BY_NAME.set(name.toLowerCase(), family);
110
+ EA_NAMES.add(name.toLowerCase());
111
+ }
112
+ for (const name of family.stack.split(',')) {
113
+ const key = name.trim().toLowerCase();
114
+ if (key !== '' && !GENERIC.has(key)) EA_NAMES.add(key);
115
+ }
116
+ }
117
+
118
+ /** 含中日韩字符或全角形式:这类字体的字宽占满整字宽,直接决定断行 */
119
+ const HAS_CJK = /[\u1100-\u11ff\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\ufe30-\ufe4f\uff00-\uffef]/;
120
+
121
+ /** 该字体名是否东亚字体(判定只看名字,与本机装没装无关) */
122
+ export const isEaFont = (name) => HAS_CJK.test(String(name)) || EA_NAMES.has(String(name).toLowerCase());
123
+
124
+ /** 东亚字体的兜底链;未登记的东亚字体按宋体近似 */
125
+ const eaStackOf = (name) => (EA_BY_NAME.get(String(name).toLowerCase()) || EA_BY_NAME.get('宋体')).stack;
126
+
127
+ /**
128
+ * 一个 run 的 CSS 字体链:拉丁名 + 拉丁兜底 + 东亚名 + 东亚兜底
129
+ * @param {string} latin w:ascii(缺省取 w:hAnsi)
130
+ * @param {string} ea w:eastAsia
131
+ * @returns {string} 逗号分隔、不带引号的字体链;两个名都为空时 ''
132
+ */
133
+ export const fontFamilyOf = (latin, ea) => {
134
+ const names = [];
135
+ const seen = new Set();
136
+ let generic = 'serif';
137
+ const push = (list) => {
138
+ for (const raw of String(list).split(',')) {
139
+ const name = raw.trim();
140
+ if (name === '') continue;
141
+ const key = name.toLowerCase();
142
+ // 通用族只能在链尾出现一次:出现在中间会把后面的真字体全挡掉
143
+ if (GENERIC.has(key)) {
144
+ generic = key;
145
+ continue;
146
+ }
147
+ if (seen.has(key)) continue;
148
+ seen.add(key);
149
+ names.push(name);
150
+ }
151
+ };
152
+ const latinName = sanitizeFontName(latin);
153
+ const eaName = sanitizeFontName(ea);
154
+ if (latinName !== '') {
155
+ push(latinName);
156
+ push(isEaFont(latinName) ? eaStackOf(latinName) : latinFallbackOf(latinName));
157
+ }
158
+ if (eaName !== '' && eaName.toLowerCase() !== latinName.toLowerCase()) {
159
+ push(eaName);
160
+ push(eaStackOf(eaName));
161
+ }
162
+ if (names.length) names.push(generic);
163
+ return names.join(',');
164
+ };
165
+
166
+ /**
167
+ * 反解 HTML 的 font-family 链 → { latin, ea },供写出端还原 w:rFonts
168
+ * [口径] 链是「拉丁组 + 东亚组」,每组末尾跟着自己那一族的兜底链,兜底名不参与还原:
169
+ * 链首非东亚名 → 它是 ascii,链上第一个东亚名是 eastAsia;
170
+ * 链首就是东亚名(Word 里 ascii 也填了中文字体)→ 跳过它自己那一族的兜底链,下一个陌生东亚名才是 eastAsia。
171
+ */
172
+ export const parseFontFamily = (css) => {
173
+ const names = String(css || '')
174
+ .split(',')
175
+ .map((s) => sanitizeFontName(s))
176
+ .filter((s) => s !== '' && !GENERIC.has(s.toLowerCase()));
177
+ const first = names[0] || '';
178
+ if (first === '') return { latin: '', ea: '' };
179
+ if (!isEaFont(first)) return { latin: first, ea: names.find(isEaFont) || first };
180
+ const own = new Set(eaStackOf(first).split(',').map((s) => s.trim().toLowerCase()));
181
+ const next = names.slice(1).find((n) => !own.has(n.toLowerCase()));
182
+ return { latin: first, ea: next && isEaFont(next) ? next : first };
183
+ };
184
+
185
+ /**
186
+ * 落盘用的两个字体名:显式给了东亚名就用它,否则从字体链里反解
187
+ * (编辑器只存一条 font-family 链,两种来源必须收敛到同一处判定)
188
+ */
189
+ export const fontPartsOf = (font, ea) => {
190
+ const parsed = parseFontFamily(font);
191
+ const east = sanitizeFontName(ea) || parsed.ea;
192
+ const west = sanitizeFontName(parsed.latin) || east;
193
+ return { latin: west, ea: east || west };
194
+ };
195
+