refactor: rename figlet app to ascii and update all references
This commit is contained in:
80
lib/ascii/asciiService.ts
Normal file
80
lib/ascii/asciiService.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
'use client';
|
||||
|
||||
import figlet from 'figlet';
|
||||
import type { ASCIIOptions } from '@/types/ascii';
|
||||
import { loadFont } from './fontLoader';
|
||||
|
||||
/**
|
||||
* Convert text to ASCII art using figlet
|
||||
*/
|
||||
export async function textToAscii(
|
||||
text: string,
|
||||
fontName: string = 'Standard',
|
||||
options: ASCIIOptions = {}
|
||||
): Promise<string> {
|
||||
if (!text) {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
// Load the font
|
||||
const fontData = await loadFont(fontName);
|
||||
|
||||
if (!fontData) {
|
||||
throw new Error(`Font ${fontName} could not be loaded`);
|
||||
}
|
||||
|
||||
// Parse and load the font into figlet
|
||||
figlet.parseFont(fontName, fontData);
|
||||
|
||||
// Generate ASCII art
|
||||
return new Promise((resolve, reject) => {
|
||||
figlet.text(
|
||||
text,
|
||||
{
|
||||
font: fontName,
|
||||
horizontalLayout: options.horizontalLayout || 'default',
|
||||
verticalLayout: options.verticalLayout || 'default',
|
||||
width: options.width,
|
||||
whitespaceBreak: options.whitespaceBreak ?? true,
|
||||
},
|
||||
(err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(result || '');
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error generating ASCII art:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate ASCII art synchronously (requires font to be pre-loaded)
|
||||
*/
|
||||
export function textToAsciiSync(
|
||||
text: string,
|
||||
fontName: string = 'Standard',
|
||||
options: ASCIIOptions = {}
|
||||
): string {
|
||||
if (!text) {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
return figlet.textSync(text, {
|
||||
font: fontName as any,
|
||||
horizontalLayout: options.horizontalLayout || 'default',
|
||||
verticalLayout: options.verticalLayout || 'default',
|
||||
width: options.width,
|
||||
whitespaceBreak: options.whitespaceBreak ?? true,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error generating ASCII art (sync):', error);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
61
lib/ascii/fontLoader.ts
Normal file
61
lib/ascii/fontLoader.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import type { ASCIIFont } from '@/types/ascii';
|
||||
|
||||
// Cache for loaded fonts
|
||||
const fontCache = new Map<string, string>();
|
||||
|
||||
/**
|
||||
* Get list of all available ascii fonts
|
||||
*/
|
||||
export async function getFontList(): Promise<ASCIIFont[]> {
|
||||
try {
|
||||
const response = await fetch('/api/fonts');
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch font list');
|
||||
}
|
||||
const fonts: ASCIIFont[] = 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/ascii-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();
|
||||
}
|
||||
Reference in New Issue
Block a user