diff --git a/apps/readest-app/src/app/reader/components/FoliateViewer.tsx b/apps/readest-app/src/app/reader/components/FoliateViewer.tsx index bf105115..f8d65be0 100644 --- a/apps/readest-app/src/app/reader/components/FoliateViewer.tsx +++ b/apps/readest-app/src/app/reader/components/FoliateViewer.tsx @@ -61,6 +61,7 @@ import { getViewInsets } from '@/utils/insets'; import { handleA11yNavigation } from '@/utils/a11y'; import { isCJKLang } from '@/utils/lang'; import { getLocale } from '@/utils/misc'; +import { isFontType } from '@/utils/font'; import { ParagraphControl } from './paragraph'; import Spinner from '@/components/Spinner'; import KOSyncConflictResolver from './KOSyncResolver'; @@ -84,7 +85,7 @@ const FoliateViewer: React.FC<{ const { appService, envConfig } = useEnv(); const { themeCode, isDarkMode } = useThemeStore(); const { settings } = useSettingsStore(); - const { loadCustomFonts, getLoadedFonts } = useCustomFontStore(); + const { loadFont, loadCustomFonts, getLoadedFonts, getAvailableFonts } = useCustomFontStore(); const { getView, setView: setFoliateView, setViewInited, setProgress } = useReaderStore(); const { getViewState, getViewSettings, setViewSettings } = useReaderStore(); const { getParallels } = useParallelViewStore(); @@ -439,11 +440,32 @@ const FoliateViewer: React.FC<{ const { book } = view; - book.transformTarget?.addEventListener('load', (event: Event) => { - const { detail } = event as CustomEvent; + book.transformTarget?.addEventListener('load', async (event: Event) => { + const { detail } = event as CustomEvent<{ + isScript: boolean; + type: string; + href: string; + url?: string; + allow?: boolean; + }>; if (detail.isScript) { detail.allow = viewSettings.allowScript ?? false; } + if (isFontType(detail.type) && detail.href?.startsWith('fonts/')) { + const fontFileName = detail.href.split('/').pop()?.toLowerCase(); + getAvailableFonts().forEach(async (font) => { + const customFontFileName = font.path.split('/').pop()?.toLowerCase(); + if (fontFileName && fontFileName === customFontFileName) { + if (!font.loaded) { + const loadedFont = await loadFont(envConfig, font.id); + font.blobUrl = loadedFont?.blobUrl; + } + if (font.blobUrl) { + detail.url = font.blobUrl; + } + } + }); + } }); const viewWidth = appService?.isMobile ? screen.width : window.innerWidth; const viewHeight = appService?.isMobile ? screen.height : window.innerHeight; diff --git a/apps/readest-app/src/utils/font.ts b/apps/readest-app/src/utils/font.ts index cb6cd29a..0d6a070b 100644 --- a/apps/readest-app/src/utils/font.ts +++ b/apps/readest-app/src/utils/font.ts @@ -358,3 +358,19 @@ export const parseFontInfo = (fontData: ArrayBuffer, filename: string) => { }; } }; + +export const isFontType = (mimeType: string) => { + const fontMimeTypes = [ + 'font/woff', + 'font/woff2', + 'font/ttf', + 'font/otf', + 'application/font-woff', + 'application/font-woff2', + 'application/x-font-woff', + 'application/x-font-woff2', + 'application/x-font-ttf', + 'application/x-font-otf', + ]; + return fontMimeTypes.includes(mimeType); +}; diff --git a/packages/foliate-js b/packages/foliate-js index 9545e4dd..69cf5c12 160000 --- a/packages/foliate-js +++ b/packages/foliate-js @@ -1 +1 @@ -Subproject commit 9545e4dd56d6a279f7763c3522bcaa5da4725b08 +Subproject commit 69cf5c1215ee3a624e972de6a176c1c3a095d341