export interface GoogleFont { family: string; category: 'sans-serif' | 'serif' | 'display' | 'handwriting' | 'monospace'; label: string; } export const GOOGLE_FONTS: GoogleFont[] = [ // Sans Serif { family: 'Inter', category: 'sans-serif', label: 'Inter' }, { family: 'Roboto', category: 'sans-serif', label: 'Roboto' }, { family: 'Open Sans', category: 'sans-serif', label: 'Open Sans' }, { family: 'Montserrat', category: 'sans-serif', label: 'Montserrat' }, { family: 'Lato', category: 'sans-serif', label: 'Lato' }, { family: 'Poppins', category: 'sans-serif', label: 'Poppins' }, { family: 'Raleway', category: 'sans-serif', label: 'Raleway' }, { family: 'Nunito', category: 'sans-serif', label: 'Nunito' }, { family: 'Ubuntu', category: 'sans-serif', label: 'Ubuntu' }, { family: 'Rubik', category: 'sans-serif', label: 'Rubik' }, { family: 'Work Sans', category: 'sans-serif', label: 'Work Sans' }, { family: 'Quicksand', category: 'sans-serif', label: 'Quicksand' }, { family: 'Outfit', category: 'sans-serif', label: 'Outfit' }, { family: 'Plus Jakarta Sans', category: 'sans-serif', label: 'Plus Jakarta Sans' }, // Serif { family: 'Merriweather', category: 'serif', label: 'Merriweather' }, { family: 'Playfair Display', category: 'serif', label: 'Playfair Display' }, { family: 'Lora', category: 'serif', label: 'Lora' }, { family: 'PT Serif', category: 'serif', label: 'PT Serif' }, { family: 'Noto Serif', category: 'serif', label: 'Noto Serif' }, { family: 'Crimson Text', category: 'serif', label: 'Crimson Text' }, { family: 'Libre Baskerville', category: 'serif', label: 'Libre Baskerville' }, { family: 'Cinzel', category: 'serif', label: 'Cinzel' }, { family: 'Cormorant Garamond', category: 'serif', label: 'Cormorant Garamond' }, { family: 'DM Serif Display', category: 'serif', label: 'DM Serif Display' }, // Display { family: 'Bebas Neue', category: 'display', label: 'Bebas Neue' }, { family: 'Lobster', category: 'display', label: 'Lobster' }, { family: 'Comfortaa', category: 'display', label: 'Comfortaa' }, { family: 'Abril Fatface', category: 'display', label: 'Abril Fatface' }, { family: 'Righteous', category: 'display', label: 'Righteous' }, { family: 'Alfa Slab One', category: 'display', label: 'Alfa Slab One' }, { family: 'Bangers', category: 'display', label: 'Bangers' }, { family: 'Orbitron', category: 'display', label: 'Orbitron' }, { family: 'Rajdhani', category: 'sans-serif', label: 'Rajdhani' }, { family: 'Audiowide', category: 'display', label: 'Audiowide' }, { family: 'Press Start 2P', category: 'display', label: 'Press Start 2P' }, { family: 'Unbounded', category: 'display', label: 'Unbounded' }, { family: 'Syne', category: 'display', label: 'Syne' }, // Handwriting { family: 'Pacifico', category: 'handwriting', label: 'Pacifico' }, { family: 'Dancing Script', category: 'handwriting', label: 'Dancing Script' }, { family: 'Shadows Into Light', category: 'handwriting', label: 'Shadows Into Light' }, { family: 'Caveat', category: 'handwriting', label: 'Caveat' }, { family: 'Satisfy', category: 'handwriting', label: 'Satisfy' }, { family: 'Great Vibes', category: 'handwriting', label: 'Great Vibes' }, { family: 'Sacramento', category: 'handwriting', label: 'Sacramento' }, { family: 'Gloria Hallelujah', category: 'handwriting', label: 'Gloria Hallelujah' }, { family: 'Permanent Marker', category: 'handwriting', label: 'Permanent Marker' }, // Monospace { family: 'Roboto Mono', category: 'monospace', label: 'Roboto Mono' }, { family: 'Inconsolata', category: 'monospace', label: 'Inconsolata' }, { family: 'Source Code Pro', category: 'monospace', label: 'Source Code Pro' }, { family: 'Space Mono', category: 'monospace', label: 'Space Mono' }, { family: 'JetBrains Mono', category: 'monospace', label: 'JetBrains Mono' }, { family: 'Fira Code', category: 'monospace', label: 'Fira Code' }, ]; const loadedFonts = new Set(); /** * Dynamically loads a Google Font by adding a tag to the document head. * @param family The font family name (e.g., 'Inter', 'Playfair Display') */ export function loadGoogleFont(family: string) { // Clean family name (remove quotes, fallbacks) const cleanFamily = family.split(',')[0].replace(/['"]/g, '').trim(); // Check if it's a known Google Font if (!GOOGLE_FONTS.some(f => f.family === cleanFamily)) { return; // Skip system fonts or unknown fonts } if (loadedFonts.has(cleanFamily)) { return; // Already loaded } // Mark as loaded immediately to prevent duplicate requests loadedFonts.add(cleanFamily); const link = document.createElement('link'); link.rel = 'stylesheet'; // Request a wide range of weights to be safe const familyQuery = cleanFamily.replace(/ /g, '+'); link.href = `https://fonts.googleapis.com/css2?family=${familyQuery}:wght@300;400;500;600;700;800&display=swap`; document.head.appendChild(link); }