@@ -8,7 +8,7 @@ import ChapterPanel from './ChapterPanel'
88import type { TocItem } from './ChapterPanel'
99import SettingsPanel from './SettingsPanel'
1010import useTTS from '../hooks/useTTS'
11- import { useReaderStore } from '../store/useReaderStore'
11+ import { FONT_OPTIONS , useReaderStore } from '../store/useReaderStore'
1212import type { Script } from '../store/useReaderStore'
1313import { useAnnotationStore , loadAnnotationsForBook , saveAnnotationsForBook } from '../store/useAnnotationStore'
1414import { 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+
4752const 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
6065const 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 ( ( ) => {
0 commit comments