mirror of
https://github.com/dergigi/boris.git
synced 2026-01-14 12:24:24 +01:00
- 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)
44 lines
1.3 KiB
TypeScript
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'
|
|
}
|