Skip to content

Commit ff19df8

Browse files
committed
fix: 修正字體切換設定
- 在 renderer 與 pwa 的 Reader 中加入字體正規化,避免舊設定或不存在的字體值直接注入 EPUB CSS 導致顯示失敗。 - 將 PWA 的字體注入 selector 與 renderer 對齊,兩端統一使用 :root * 套用閱讀內容字體。 - 移除 renderer 字體清單中的已棄用源雲明體;舊 localStorage 若仍保存該值,會 fallback 到預設思源宋體。 - 已驗證 renderer 與 pwa 的 yarn build 皆通過,且全專案無源雲明體 / Gen'YouMin / YouMin 殘留。
1 parent 4f73f3c commit ff19df8

3 files changed

Lines changed: 42 additions & 17 deletions

File tree

pwa/src/components/Reader.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ChapterPanel from './ChapterPanel'
88
import type { TocItem } from './ChapterPanel'
99
import SettingsPanel from './SettingsPanel'
1010
import useTTS from '../hooks/useTTS'
11-
import { useReaderStore } from '../store/useReaderStore'
11+
import { FONT_OPTIONS, useReaderStore } from '../store/useReaderStore'
1212
import type { Script } from '../store/useReaderStore'
1313
import { useAnnotationStore, loadAnnotationsForBook, saveAnnotationsForBook } from '../store/useAnnotationStore'
1414
import { saveProgress, loadProgress, saveBookSettings, loadBookSettings, loadBookmarks, saveBookmarks } from '../hooks/useLibrary'
@@ -44,6 +44,11 @@ const WEB_FONT_URLS: Record<string, string> = {
4444
'LXGW WenKai TC': 'https://fonts.googleapis.com/css2?family=LXGW+WenKai+TC&display=swap',
4545
}
4646

47+
const DEFAULT_FONT_FAMILY = FONT_OPTIONS[0].value
48+
49+
const normalizeFontFamily = (family: string | null | undefined): string =>
50+
family && FONT_OPTIONS.some(option => option.value === family) ? family : DEFAULT_FONT_FAMILY
51+
4752
const injectWebFontLink = (doc: Document, href: string | null) => {
4853
const id = 'tit-webfont-link'
4954
let el = doc.getElementById(id) as HTMLLinkElement | null
@@ -58,8 +63,9 @@ const injectWebFontLink = (doc: Document, href: string | null) => {
5863
}
5964

6065
const applyFontFamilyOverride = (doc: Document, family: string) => {
61-
injectStyle(doc, 'tit-font', `* { font-family: ${family} !important; }`)
62-
const fontKey = Object.keys(WEB_FONT_URLS).find(k => family.includes(k))
66+
const normalizedFamily = normalizeFontFamily(family)
67+
injectStyle(doc, 'tit-font', `:root * { font-family: ${normalizedFamily} !important; }`)
68+
const fontKey = Object.keys(WEB_FONT_URLS).find(k => normalizedFamily.includes(k))
6369
injectWebFontLink(doc, fontKey ? WEB_FONT_URLS[fontKey] : null)
6470
}
6571

@@ -664,13 +670,15 @@ const Reader = ({ bookPath, bookId, bookRecord, getCoverDataUrl, onBack, darkMod
664670
// 載入此書上次儲存的閱讀設定,套用到 store
665671
const savedSettings = loadBookSettings(bookId)
666672
if (savedSettings) {
673+
const normalizedFamily = normalizeFontFamily(savedSettings.fontFamily)
667674
const store = useReaderStore.getState()
668675
store.setFontSize(savedSettings.fontSize)
669-
store.setFontFamily(savedSettings.fontFamily)
676+
store.setFontFamily(normalizedFamily)
670677
store.setLineHeight(savedSettings.lineHeight)
671678
store.setLetterSpacing(savedSettings.letterSpacing)
672679
store.setReadingDirection(savedSettings.readingDirection)
673680
store.setScript(savedSettings.script)
681+
fontFamilyRef.current = normalizedFamily
674682
scriptRef.current = savedSettings.script
675683
} else {
676684
resetScript()
@@ -1053,12 +1061,17 @@ const Reader = ({ bookPath, bookId, bookRecord, getCoverDataUrl, onBack, darkMod
10531061
// 字體家族(獨立,不影響其他設定)
10541062
useEffect(() => {
10551063
if (!ready) return
1056-
fontFamilyRef.current = fontFamily
1057-
try { renditionRef.current?.themes.override('font-family', fontFamily) } catch { /* epubjs 時序問題,忽略 */ }
1058-
applyToCurrentDoc(doc => applyFontFamilyOverride(doc, fontFamily))
1064+
const normalizedFamily = normalizeFontFamily(fontFamily)
1065+
if (normalizedFamily !== fontFamily) {
1066+
setFontFamily(normalizedFamily)
1067+
return
1068+
}
1069+
fontFamilyRef.current = normalizedFamily
1070+
try { renditionRef.current?.themes.override('font-family', normalizedFamily) } catch { /* epubjs 時序問題,忽略 */ }
1071+
applyToCurrentDoc(doc => applyFontFamilyOverride(doc, normalizedFamily))
10591072
rerenderAnnotationPane()
10601073
triggerScan()
1061-
}, [fontFamily, ready])
1074+
}, [fontFamily, ready, setFontFamily])
10621075

10631076
// 行距(獨立,不影響其他設定)
10641077
useEffect(() => {

renderer/src/components/Reader.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ChapterPanel from './ChapterPanel'
88
import type { TocItem } from './ChapterPanel'
99
import SettingsPanel from './SettingsPanel'
1010
import useTTS from '../hooks/useTTS'
11-
import { useReaderStore } from '../store/useReaderStore'
11+
import { FONT_OPTIONS, useReaderStore } from '../store/useReaderStore'
1212
import type { Script } from '../store/useReaderStore'
1313
import { useAnnotationStore, loadAnnotationsForBook, saveAnnotationsForBook } from '../store/useAnnotationStore'
1414
import { saveProgress, loadProgress, saveBookSettings, loadBookSettings, loadBookmarks, saveBookmarks } from '../hooks/useLibrary'
@@ -44,6 +44,11 @@ const WEB_FONT_URLS: Record<string, string> = {
4444
'LXGW WenKai TC': 'https://fonts.googleapis.com/css2?family=LXGW+WenKai+TC&display=swap',
4545
}
4646

47+
const DEFAULT_FONT_FAMILY = FONT_OPTIONS[0].value
48+
49+
const normalizeFontFamily = (family: string | null | undefined): string =>
50+
family && FONT_OPTIONS.some(option => option.value === family) ? family : DEFAULT_FONT_FAMILY
51+
4752
const injectWebFontLink = (doc: Document, href: string | null) => {
4853
const id = 'tit-webfont-link'
4954
let el = doc.getElementById(id) as HTMLLinkElement | null
@@ -58,8 +63,9 @@ const injectWebFontLink = (doc: Document, href: string | null) => {
5863
}
5964

6065
const applyFontFamilyOverride = (doc: Document, family: string) => {
61-
injectStyle(doc, 'tit-font', `:root * { font-family: ${family} !important; }`)
62-
const fontKey = Object.keys(WEB_FONT_URLS).find(k => family.includes(k))
66+
const normalizedFamily = normalizeFontFamily(family)
67+
injectStyle(doc, 'tit-font', `:root * { font-family: ${normalizedFamily} !important; }`)
68+
const fontKey = Object.keys(WEB_FONT_URLS).find(k => normalizedFamily.includes(k))
6369
injectWebFontLink(doc, fontKey ? WEB_FONT_URLS[fontKey] : null)
6470
}
6571

@@ -535,13 +541,15 @@ const Reader = ({ bookPath, bookId, bookRecord, getCoverDataUrl, onBack, darkMod
535541
// 載入此書上次儲存的閱讀設定,套用到 store
536542
const savedSettings = loadBookSettings(bookId)
537543
if (savedSettings) {
544+
const normalizedFamily = normalizeFontFamily(savedSettings.fontFamily)
538545
const store = useReaderStore.getState()
539546
store.setFontSize(savedSettings.fontSize)
540-
store.setFontFamily(savedSettings.fontFamily)
547+
store.setFontFamily(normalizedFamily)
541548
store.setLineHeight(savedSettings.lineHeight)
542549
store.setLetterSpacing(savedSettings.letterSpacing)
543550
store.setReadingDirection(savedSettings.readingDirection)
544551
store.setScript(savedSettings.script)
552+
fontFamilyRef.current = normalizedFamily
545553
scriptRef.current = savedSettings.script
546554
} else {
547555
resetScript()
@@ -841,12 +849,17 @@ const Reader = ({ bookPath, bookId, bookRecord, getCoverDataUrl, onBack, darkMod
841849
// 字體家族(獨立,不影響其他設定)
842850
useEffect(() => {
843851
if (!ready) return
844-
fontFamilyRef.current = fontFamily
845-
try { renditionRef.current?.themes.override('font-family', fontFamily) } catch { /* epubjs 時序問題,忽略 */ }
846-
applyToCurrentDoc(doc => applyFontFamilyOverride(doc, fontFamily))
852+
const normalizedFamily = normalizeFontFamily(fontFamily)
853+
if (normalizedFamily !== fontFamily) {
854+
setFontFamily(normalizedFamily)
855+
return
856+
}
857+
fontFamilyRef.current = normalizedFamily
858+
try { renditionRef.current?.themes.override('font-family', normalizedFamily) } catch { /* epubjs 時序問題,忽略 */ }
859+
applyToCurrentDoc(doc => applyFontFamilyOverride(doc, normalizedFamily))
847860
rerenderAnnotationPane()
848861
triggerScan()
849-
}, [fontFamily, ready])
862+
}, [fontFamily, ready, setFontFamily])
850863

851864
// 行距(獨立,不影響其他設定)
852865
useEffect(() => {

renderer/src/store/useReaderStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const FONT_OPTIONS = [
77
{ label: '思源宋體', value: '"Noto Serif TC", serif' },
88
{ label: '霞鶩文楷', value: '"LXGW WenKai TC", cursive' },
99
{ label: '思源黑體', value: '"Noto Sans TC", sans-serif' },
10-
{ label: '源雲明體', value: '"Gen\'YouMin TW", "源雲明體", serif' },
1110
{ label: '粉圓體', value: '"Huninn", sans-serif' },
1211
]
1312

0 commit comments

Comments
 (0)