UNPKG

@revenuecat/purchases-ui-js

Version:

Web components for Paywalls. Powered by RevenueCat

48 lines (47 loc) 2.05 kB
export function isFontRCFMManaged(fontName) { return fontName.startsWith("RCFM:"); } export function getScopedFontFamily(fontFamily) { return `RevCatPaywall-${fontFamily}`; } export async function registerFonts(uiConfig) { if (!uiConfig.app.fonts || typeof document === "undefined") { return; } const fontPromises = []; for (const [fontId, fontConfig] of Object.entries(uiConfig.app.fonts)) { const webConfig = fontConfig.web; if (!webConfig || !webConfig.url || !webConfig.family) { console.warn(`[Font Registration] Skipping font ${fontId}: missing web config or required fields`); continue; } try { const registration = `${getScopedFontFamily(webConfig.family)}-${webConfig.style}-${webConfig.weight}`; const fontFace = new FontFace(registration, `url(${webConfig.url})`, { weight: webConfig.weight?.toString() || "400", style: webConfig.style || "normal", }); fontFace.family = getScopedFontFamily(webConfig.family); const loadPromise = fontFace .load() .then(() => { document.fonts.add(fontFace); console.debug(`[Font Registration] Successfully registered font: ${registration} (${webConfig.family}, ${webConfig.weight}, ${webConfig.style})`); }) .catch((error) => { console.warn(`[Font Registration] Failed to load font ${webConfig.family}:`, error); }); fontPromises.push(loadPromise); } catch (error) { console.warn(`[Font Registration] Failed to create FontFace for ${fontId}:`, error); } } try { await Promise.allSettled(fontPromises); console.debug(`[Font Registration] Font registration complete. Processed ${fontPromises.length} fonts.`); } catch (error) { console.warn("[Font Registration] Unexpected error during font registration:", error); } }