2026-02-22 21:35:53 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import figlet from 'figlet';
|
2026-02-26 12:31:10 +01:00
|
|
|
import type { ASCIIOptions } from '@/types/ascii';
|
2026-02-22 21:35:53 +01:00
|
|
|
import { loadFont } from './fontLoader';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert text to ASCII art using figlet
|
|
|
|
|
*/
|
|
|
|
|
export async function textToAscii(
|
|
|
|
|
text: string,
|
|
|
|
|
fontName: string = 'Standard',
|
2026-02-26 12:31:10 +01:00
|
|
|
options: ASCIIOptions = {}
|
2026-02-22 21:35:53 +01:00
|
|
|
): 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',
|
2026-02-26 12:31:10 +01:00
|
|
|
options: ASCIIOptions = {}
|
2026-02-22 21:35:53 +01:00
|
|
|
): 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 '';
|
|
|
|
|
}
|
|
|
|
|
}
|