Files
boris/src/utils/fontLoader.ts
Gigi 0ccad88dfd feat: add configurable reading font using Bunny Fonts
- Add readingFont setting to UserSettings interface
- Create fontLoader utility to load fonts from Bunny Fonts
- Add font selector dropdown in settings with popular reading fonts
- Use CSS variable --reading-font to apply font to reader content
- Support fonts: Inter, Lora, Merriweather, Open Sans, Roboto, Source Serif 4, Crimson Text, Libre Baskerville, PT Serif
- Fonts loaded from https://fonts.bunny.net/ (GDPR-friendly)
2025-10-05 02:52:21 +01:00

44 lines
1.3 KiB
TypeScript

// Map of font names to their Bunny Fonts family names
const FONT_FAMILIES: Record<string, string> = {
'inter': 'Inter',
'lora': 'Lora',
'merriweather': 'Merriweather',
'open-sans': 'Open Sans',
'roboto': 'Roboto',
'source-serif-4': 'Source Serif 4',
'crimson-text': 'Crimson Text',
'libre-baskerville': 'Libre Baskerville',
'pt-serif': 'PT Serif'
}
const loadedFonts = new Set<string>()
export function loadFont(fontKey: string) {
if (fontKey === 'system' || loadedFonts.has(fontKey)) {
return
}
const fontFamily = FONT_FAMILIES[fontKey]
if (!fontFamily) {
console.warn(`Unknown font: ${fontKey}`)
return
}
// Create a link element to load the font from Bunny Fonts
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = `https://fonts.bunny.net/css?family=${encodeURIComponent(fontFamily.toLowerCase().replace(/ /g, '-'))}:400,400i,700,700i`
document.head.appendChild(link)
loadedFonts.add(fontKey)
}
export function getFontFamily(fontKey: string | undefined): string {
if (!fontKey || fontKey === 'system') {
return 'system-ui, -apple-system, sans-serif'
}
const fontFamily = FONT_FAMILIES[fontKey]
return fontFamily ? `'${fontFamily}', serif` : 'system-ui, -apple-system, sans-serif'
}