Files
figlet-ui/lib/figlet/fontLoader.ts

62 lines
1.4 KiB
TypeScript
Raw Normal View History

import type { FigletFont } from '@/types/figlet';
// Cache for loaded fonts
const fontCache = new Map<string, string>();
/**
* Get list of all available figlet fonts
*/
export async function getFontList(): Promise<FigletFont[]> {
try {
const response = await fetch('/api/fonts');
if (!response.ok) {
throw new Error('Failed to fetch font list');
}
const fonts: FigletFont[] = await response.json();
return fonts;
} catch (error) {
console.error('Error fetching font list:', error);
return [];
}
}
/**
* Load a specific font file content
*/
export async function loadFont(fontName: string): Promise<string | null> {
// Check cache first
if (fontCache.has(fontName)) {
return fontCache.get(fontName)!;
}
try {
const response = await fetch(`/fonts/figlet-fonts/${fontName}.flf`);
if (!response.ok) {
throw new Error(`Failed to load font: ${fontName}`);
}
const fontData = await response.text();
// Cache the font
fontCache.set(fontName, fontData);
return fontData;
} catch (error) {
console.error(`Error loading font ${fontName}:`, error);
return null;
}
}
/**
* Preload a font into cache
*/
export async function preloadFont(fontName: string): Promise<void> {
await loadFont(fontName);
}
/**
* Clear font cache
*/
export function clearFontCache(): void {
fontCache.clear();
}