Skip to content
This repository was archived by the owner on Jul 17, 2026. It is now read-only.

Commit d7f5b85

Browse files
Merge pull request #917 from MoYingJi/pr/f
refactor(FontManager): 优化字体的显示转换逻辑
2 parents 3877c9b + c3c26d8 commit d7f5b85

2 files changed

Lines changed: 57 additions & 30 deletions

File tree

‎electron/main/ipc/ipc-system.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const initSystemIpc = (): void => {
4242
// 获取系统全部字体
4343
ipcMain.handle("get-all-fonts", async () => {
4444
try {
45-
const fonts = await getFonts();
45+
const fonts = await getFonts({ disableQuoting: true });
4646
return fonts;
4747
} catch (error) {
4848
ipcLog.error(`❌ Failed to get all system fonts: ${error}`);

‎src/components/Modal/Setting/FontManager.vue‎

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@
5858
<n-select
5959
v-else-if="settingStore.fontSettingStyle === 'multi'"
6060
:value="fontFamilyToArray(settingStore.globalFont)"
61-
@update:value="
62-
(value: string[]) => (settingStore.globalFont = fontArrayToFamily(value))
63-
"
61+
@update:value="(val: string[]) => (settingStore.globalFont = fontArrayToFamily(val))"
6462
:options="getOptions('globalFont')"
6563
class="set"
6664
filterable
@@ -69,7 +67,8 @@
6967
/>
7068
<n-select
7169
v-else-if="settingStore.fontSettingStyle === 'single'"
72-
v-model:value="settingStore.globalFont"
70+
:value="fontFamilyToDisplay(settingStore.globalFont)"
71+
@update:value="(val) => (settingStore.globalFont = fontDisplayToFamily(val))"
7372
:options="getOptions('globalFont')"
7473
class="set"
7574
filterable
@@ -127,18 +126,18 @@
127126
multiple
128127
tag
129128
@update:value="
130-
(value: string[]) => (desktopLyricConfig.fontFamily = fontArrayToFamily(value))
129+
(val: string[]) => (desktopLyricConfig.fontFamily = fontArrayToFamily(val))
131130
"
132131
/>
133132
<n-select
134133
v-else-if="settingStore.fontSettingStyle === 'single'"
135-
:value="desktopLyricConfig.fontFamily"
134+
:value="fontFamilyToDisplay(desktopLyricConfig.fontFamily)"
136135
:options="getOptions('desktop')"
137136
class="set"
138137
filterable
139138
@update:value="
140139
(val) => {
141-
desktopLyricConfig.fontFamily = val;
140+
desktopLyricConfig.fontFamily = fontDisplayToFamily(val);
142141
saveDesktopLyricConfig();
143142
}
144143
"
@@ -185,7 +184,7 @@
185184
v-else-if="settingStore.fontSettingStyle === 'multi'"
186185
:value="fontFamilyToArray(settingStore[font.keySetting])"
187186
@update:value="
188-
(value: string[]) => (settingStore[font.keySetting] = fontArrayToFamily(value))
187+
(val: string[]) => (settingStore[font.keySetting] = fontArrayToFamily(val))
189188
"
190189
:options="getOptions(font.keySetting)"
191190
class="set"
@@ -195,7 +194,8 @@
195194
/>
196195
<n-select
197196
v-else-if="settingStore.fontSettingStyle === 'single'"
198-
v-model:value="settingStore[font.keySetting]"
197+
:value="fontFamilyToDisplay(settingStore[font.keySetting])"
198+
@update:value="(val) => (settingStore[font.keySetting] = fontDisplayToFamily(val))"
199199
:options="getOptions(font.keySetting)"
200200
class="set"
201201
filterable
@@ -273,6 +273,51 @@ const getAllSystemFonts = async () => {
273273
}
274274
};
275275
276+
/**
277+
* 字符串是否拥有在开头和结尾的成对引号
278+
* @note 不移除首尾空格,因为在下面的所有调用场景,都会先移除首尾空格
279+
* @param s 字符串
280+
* @returns 是否拥有成对引号
281+
*/
282+
const hasPairedQuotes = (s: string): boolean => {
283+
const l = s.length;
284+
if (l < 2) return false;
285+
if (s.startsWith('"')) {
286+
if (s.indexOf('"', 1) === l - 1) return true;
287+
} else if (s.startsWith("'")) {
288+
if (s.indexOf("'", 1) === l - 1) return true;
289+
}
290+
return false;
291+
};
292+
293+
/**
294+
* 将 Font Family 字符串转换为用户可见字符串
295+
* @param fontFamily Font Family 字符串
296+
* @return 用户可见字符串
297+
*/
298+
const fontFamilyToDisplay = (fontFamily: string): string => {
299+
// 移除首尾空格
300+
fontFamily = fontFamily.trim();
301+
// 移除引号
302+
if (hasPairedQuotes(fontFamily)) {
303+
fontFamily = fontFamily.substring(1, fontFamily.length - 1);
304+
}
305+
return fontFamily.trim();
306+
};
307+
308+
/**
309+
* 用户可见字符串转换为 Font Family 字符串
310+
* @param display 用户可见字符串(单一字体)
311+
* @return Font Family 字符串
312+
*/
313+
const fontDisplayToFamily = (display: string): string => {
314+
display = display.trim();
315+
if ((display.includes(",") || display.includes(" ")) && !hasPairedQuotes(display)) {
316+
return `"${display}"`;
317+
}
318+
return display;
319+
};
320+
276321
/**
277322
* 字体字符串转数组
278323
* @param fontFamily 字体字符串
@@ -284,17 +329,7 @@ const fontFamilyToArray = (fontFamily: string): string[] => {
284329
const matches = fontFamily.match(regex);
285330
if (!matches) return [];
286331
287-
return matches
288-
.map((s) => {
289-
// 移除首尾空格
290-
s = s.trim();
291-
// 移除引号
292-
if (s.match(/^"|^'/)) {
293-
s = s.substring(1, s.length - 1);
294-
}
295-
return s.trim();
296-
})
297-
.filter(Boolean);
332+
return matches.map(fontFamilyToDisplay).filter(Boolean);
298333
};
299334
300335
/**
@@ -303,15 +338,7 @@ const fontFamilyToArray = (fontFamily: string): string[] => {
303338
* @returns 字体字符串
304339
*/
305340
const fontArrayToFamily = (fontArray: string[]): string => {
306-
return fontArray
307-
.map((font) => {
308-
font = font.trim();
309-
if ((font.includes(",") || font.includes(" ")) && !/^["'].*["']$/.test(font)) {
310-
return `"${font}"`;
311-
}
312-
return font;
313-
})
314-
.join(", ");
341+
return fontArray.map(fontDisplayToFamily).join(", ");
315342
};
316343
317344
// 获取桌面歌词配置

0 commit comments

Comments
 (0)