figlet.js

repository·main·Indexed 25 days ago

https://github.com/patorjk/figlet.js

A full implementation of the FIGfont specification in TypeScript that creates ASCII art from text. It supports both Node.js and browser environments, providing asynchronous (text()) and synchronous (textSync()) methods, a command-line interface (CLI), and the ability to load custom fonts via parseFont().

Tokens
10.7K
Snippets
10
Records
142
Agent score
81%

What's inside figlet.js

  1. Use figlet in the browser with ES modules

    main

    When using ES modules in the browser, you should import the font files and parse them into figlet to ensure they are available.

    import figlet from "figlet";
    import standard from "figlet/fonts/Standard";
    
    // Register the font
    figlet.parseFont("Standard", standard);
    
    async function doStuff() {
      const text = await figlet.text("test", { font: "Standard" });
      console.log(text);
    }
    doStuff();

    To use textSync() in the browser, you must preload the fonts first using preloadFonts().

    figlet.defaults({ fontPath: "assets/fonts" });
    
    figlet.preloadFonts(["Standard", "Ghost"], () => {
      console.log(figlet.textSync("ASCII"));
      console.log(figlet.textSync("Art", "Ghost"));
    });
  2. Configure figlet options

    main

    You can pass an options object to text() or textSync() to customize the output:

    OptionTypeDefaultDescription
    fontString'Standard'The FIGlet font to use.
    horizontalLayoutString'default'Horizontal layout: "default", "full", "fitted", "controlled smushing", or "universal smushing".
    verticalLayoutString'default'Vertical layout: "default", "full", "fitted", "controlled smushing", or "universal smushing".
    widthNumberundefinedLimits the output width in characters.
    whitespaceBreakBooleanfalseIf true, attempts to break text at whitespace when limiting width.
  3. Manual kerning for NV Script font

    main

    When using smushmode -1 with the NV Script font, you may need to perform manual kerning to ensure letters connect properly:

    • Delete portions of the area between two letters to connect them.
    • Add an '8' to fill gaps between letters.
    • Always aim to leave a minimum of two spaces between letters, except where they are intended to touch.
  4. Use NV Script font with special ligatures

    main

    The NV Script font includes a second set of lowercase characters with special ligatures. To access these characters, use the character code 200 + the letter of the alphabet.

    For example:

    • 201 is 'a'
    • 226 is 'z'
    • To create the word 'bomb', use the sequence: b + 215 + 213 + b.

    On Windows, you can enter these codes using Alt+# on the numpad. In other environments, you may need to use character encoding methods (e.g., perl -e 'print chr(210)' to get a special 'j').

  5. Generate ASCII art synchronously with textSync()

    main

    The textSync() method is the synchronous version of text(). It is useful in environments where blocking is acceptable or when fonts are already preloaded (especially in the browser).

    Parameters:

    • Input Text: A string to convert.
    • Font Options: A string (font name) or an options object.
    import figlet from "figlet";
    
    const ascii = figlet.textSync("Boo!", {
      font: "Ghost",
      horizontalLayout: "default",
      verticalLayout: "default",
      width: 80,
      whitespaceBreak: true,
    });
    console.log(ascii);
  6. Load custom fonts with parseFont()

    main

    You can use fonts from external sources by providing the raw font data via parseFont(name, data).

    const fs = require("fs");
    const path = require("path");
    
    let data = fs.readFileSync(path.join(__dirname, "myfont.flf"), "utf8");
    figlet.parseFont("myfont", data);
    console.log(figlet.textSync("myfont!", "myfont"));
  7. Retrieve font metadata with metadata()

    main

    The metadata() function retrieves a font's default options and header comment. It supports both a callback pattern and a Promise pattern (returning an array [options, headerComment]).

    try {
      const [options, headerComment] = await figlet.metadata("Standard");
      console.dir(options);
      console.log(headerComment);
    } catch (err) {
      console.error(err);
    }
  8. Generate ASCII art with text()

    main

    The text() method (or calling the figlet object directly as a function) creates ASCII art from text. It is asynchronous and returns a Promise that resolves to the generated ASCII art. It also supports a classic callback pattern.

    Parameters:

    • Input Text: A string to convert.
    • Options: A string (font name) or an options object.
    • Callback (Optional): A function (err, data) => void.
    import figlet from "figlet";
    
    async function doStuff() {
      const text = await figlet.text("Hello World!!");
      console.log(text);
    }
    
    doStuff();
  9. List available fonts with fonts()

    main

    Use fonts() (asynchronous) or fontsSync() (synchronous) to get a list of available fonts.

    • In Node.js, fonts() looks in the local fonts folder (configurable via defaults()).
    • In the browser, it returns all fonts included with the library.
    • To see only fonts currently loaded into memory, use loadedFonts().