Convert Figma logo to code with AI

pwaller logopyfiglet

An implementation of figlet written in Python

1,355
140
1,355
4

Top Related Projects

2,089

🎨 ASCII art library for Python

1,349

Claudio's FIGlet tree

A FIG Driver written in JavaScript which aims to fully implement the FIGfont spec.

Quick Overview

PyFiglet is a Python library that allows you to create ASCII art text using a variety of different fonts. It is a Python port of the FIGlet project, a popular tool for generating ASCII art from text.

Pros

  • Extensive Font Library: PyFiglet comes with a wide variety of pre-loaded fonts, allowing you to create unique and visually appealing ASCII art.
  • Cross-Platform Compatibility: The library is designed to work across multiple platforms, including Windows, macOS, and Linux.
  • Easy to Use: The API is straightforward and easy to use, making it simple to integrate into your Python projects.
  • Customizable: You can create your own custom fonts or modify existing ones to suit your needs.

Cons

  • Limited Font Rendering: While the library has a wide range of fonts, the quality of the ASCII art may not be as high as some other specialized tools.
  • Dependency on External Fonts: If you want to use a font that is not included in the library, you'll need to provide the font file yourself.
  • Lack of Advanced Features: PyFiglet is a relatively simple library and may not offer the same level of customization and control as some other ASCII art tools.
  • Potential Performance Issues: Generating large ASCII art pieces may be computationally intensive, especially on older or less powerful systems.

Code Examples

Here are a few examples of how to use PyFiglet in your Python code:

from pyfiglet import Figlet

# Create a Figlet object with the 'standard' font
fig = Figlet(font='standard')

# Generate ASCII art from a string
text = "Hello, World!"
ascii_art = fig.renderText(text)
print(ascii_art)

This code creates a Figlet object with the 'standard' font and uses it to generate ASCII art from the string "Hello, World!".

from pyfiglet import Figlet

# Create a Figlet object with a custom font
fig = Figlet(font='slant')

# Generate ASCII art from a string
text = "PyFiglet is awesome!"
ascii_art = fig.renderText(text)
print(ascii_art)

This example demonstrates how to use a custom font (in this case, the 'slant' font) to generate ASCII art.

from pyfiglet import Figlet

# Create a Figlet object and set the output direction
fig = Figlet(font='standard', direction='right-to-left')

# Generate ASCII art from a string
text = "مرحبا بالعالم!"
ascii_art = fig.renderText(text)
print(ascii_art)

This code shows how to generate ASCII art with right-to-left text direction, which can be useful for languages like Arabic or Hebrew.

Getting Started

To get started with PyFiglet, you can install the library using pip:

pip install pyfiglet

Once you have the library installed, you can start using it in your Python code. Here's a simple example to get you started:

from pyfiglet import Figlet

# Create a Figlet object with the 'standard' font
fig = Figlet(font='standard')

# Generate ASCII art from a string
text = "Hello, World!"
ascii_art = fig.renderText(text)
print(ascii_art)

This code will output the following ASCII art:

 _   _      _ _        __        __         _     _ 
| | | | ___| | | ___   \ \      / /__  _ __| | __| |
| |_| |/ _ \ | |/ _ \   \ \ /\ / / _ \| '__| |/ _` |
|  _  |  __/ | | (_) |   \ V  V / (_) | |  | | (_| |
|_| |_|\___|_|_|\___/     \_/\_/ \___/|_|  |_|\__,_|

You can customize the font, text direction, and other settings by creating a Figlet object with different parameters. The PyFiglet documentation provides more information on the available options and features.

Competitor Comparisons

2,089

🎨 ASCII art library for Python

Pros of art

  • Offers a wider variety of ASCII art styles and fonts
  • Includes additional features like text to ASCII art conversion and 1-line art
  • More actively maintained with frequent updates

Cons of art

  • Larger package size due to more features and fonts
  • May have a steeper learning curve for beginners
  • Potentially slower performance for simple ASCII art generation

Code Comparison

art:

from art import *
Art=text2art("Hello")
print(Art)

pyfiglet:

from pyfiglet import Figlet
f = Figlet(font='slant')
print(f.renderText('Hello'))

Both libraries allow for easy ASCII art generation, but art offers more customization options and additional features. pyfiglet is more focused on traditional FIGlet fonts and may be simpler for basic use cases. art's syntax is slightly more concise, while pyfiglet requires specifying the font explicitly. Overall, art provides a more comprehensive ASCII art toolkit, while pyfiglet remains a solid choice for straightforward FIGlet font rendering.

1,349

Claudio's FIGlet tree

Pros of figlet

  • Written in C, potentially offering better performance for large-scale operations
  • Provides a command-line interface for easy integration with shell scripts
  • Includes a wider variety of built-in fonts and layouts

Cons of figlet

  • Requires compilation and installation, which may be less convenient for some users
  • Less Python-friendly, making it harder to integrate directly into Python projects
  • May have a steeper learning curve for users unfamiliar with C or command-line tools

Code Comparison

figlet (C):

char *smush_lookup(long s_left, long s_right, int hardblank)
{
    static char result;
    if (s_left == s_right) return NULL;
    if (s_left == hardblank && s_right == ' ') return NULL;
    if (s_right == hardblank && s_left == ' ') return NULL;
    /* ... */
}

pyfiglet (Python):

def smush_lookup(self, s_left, s_right):
    if s_left == s_right:
        return None
    if s_left == self.SM_HARDBLANK and s_right == ' ':
        return None
    if s_right == self.SM_HARDBLANK and s_left == ' ':
        return None
    # ...

The code comparison shows similar logic implemented in C and Python, highlighting the language differences between the two projects.

A FIG Driver written in JavaScript which aims to fully implement the FIGfont spec.

Pros of figlet.js

  • JavaScript-based, allowing for easy integration in web applications and Node.js projects
  • Supports a wider range of fonts and layouts compared to pyfiglet
  • More actively maintained with recent updates and contributions

Cons of figlet.js

  • Larger file size and potentially slower performance compared to the Python-based pyfiglet
  • May require additional setup for non-JavaScript environments

Code Comparison

pyfiglet:

from pyfiglet import Figlet
f = Figlet(font='slant')
print(f.renderText('Hello'))

figlet.js:

const figlet = require('figlet');
figlet('Hello', { font: 'Slant' }, function(err, data) {
    if (err) {
        console.log('Something went wrong...');
        console.dir(err);
        return;
    }
    console.log(data);
});

Both libraries offer similar functionality for generating ASCII art text, but their implementation and usage differ based on the programming language. figlet.js provides a callback-based approach, while pyfiglet uses a more straightforward synchronous method. The choice between the two largely depends on the project's requirements and the developer's preferred programming language.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

pyfiglet

                        _|_|  _|            _|              _|
_|_|_|    _|    _|    _|            _|_|_|  _|    _|_|    _|_|_|_|
_|    _|  _|    _|  _|_|_|_|  _|  _|    _|  _|  _|_|_|_|    _|
_|    _|  _|    _|    _|      _|  _|    _|  _|  _|          _|
_|_|_|      _|_|_|    _|      _|    _|_|_|  _|    _|_|_|      _|_|
_|              _|                      _|
_|          _|_|                    _|_|

Synopsis

pyfiglet is a full port of FIGlet (http://www.figlet.org/) into pure python. It takes ASCII text and renders it in ASCII art fonts (like the title above, which is the 'block' font).

FAQ

  • Q: Why? WHY?!!

    A: I [cjones] was bored. Really bored.

  • Q: What the hell does this do that FIGlet doesn't?

    A: Not much, except allow your font collection to live in one big zipfile. The point of this code is to embed dynamic figlet rendering in Python without having to execute an external program, although it operates on the commandline as well. See below for USAGE details. You can think of this as a python FIGlet driver.

  • Q: Does this support kerning/smushing like FIGlet?

    A: Yes, yes it does. Output should be identical to FIGlet. If not, this is a bug, which you should report to me!

  • Q: Can I use/modify/redstribute this code?

    A: Yes, under the terms of the MIT (see LICENSE below).

  • Q: I improved this code, what should I do with it?

    A: You can submit changes to https://github.com/pwaller/pyfiglet/pulls. If you make changes to the kerning/mushing/rendering portion, PLEASE test it thoroughly. The code is fragile and complex.

  • Q: Where did my font go?

    A: It turns out that we didn't have distribution rights for some of the fonts and so we had to remove them. Full details of the change and why we did it are in https://github.com/pwaller/pyfiglet/issues/59.

  • Q: Where can I find these and other fonts?

    A: Do a quick search for "figlet fonts" on your favourite search engine should give you what you need. However, if you are looking for the specific removed fonts, please go to http://www.jave.de/figlet/fonts.html.

  • Q: Why are some fonts missing in distribution?

    A: Some Linux distributions have very strict legal restrictions on what contributions they will take. For these systems, we have divided the fonts into ones that have a clear redistribution license and those that don't. These are the fonts-standard and fonts-contrib directories in this repository.

  • Q: What about those other fonts?

    A: While there isn't a watertight case for the license, we believe that any legal constraint for these fonts has long expired and so they are public domain, so are continuing to redistribute via pypi. If an owner of any of these fonts wants us to stop, please just raise an issue on https://github.com/pwaller/pyfiglet/issues proving your ownership and we will remove the requested fonts.

Usage

You can use pyfiglet in one of two ways. First, it operates on the commandline as C figlet does and supports most of the same options. Run with --help to see a full list of tweaks. Mostly you will only use -f to change the font. It defaults to standard.flf.

tools/pyfiglet 'text to render'

Pyfiglet is also a library that can be used in python code:

from pyfiglet import Figlet
f = Figlet(font='slant')
print(f.renderText('text to render'))

or

import pyfiglet
f = pyfiglet.figlet_format("text to render", font="slant")
print(f)

If you have found some new fonts that you want to use, you can use the command line interface to install your font file as follows:

pyfiglet -L <font file>

The font file can be a ZIP file of lots of fonts or just a single font. Depending on how you installed pyfiglet, you may find that you need root access to install the font - e.g. sudo pyfiglet -L <font file>.

Author

All of the documentation and the majority of the work done was by Christopher Jones (cjones@insub.org) and many other contributors. Packaged by Peter Waller (p@pwaller.net), various enhancements by Stefano Rivera (stefano@rivera.za.net), and lots of help from many contributors!

Thank you all for your efforts, please send a pull request to add yourself to this list if you would like to take credit.

(In the words of the original author) pyfiglet is a port of FIGlet, and much of the code is directly translated from the C source. I optimized some bits where I could, but because the smushing and kerning code is so incredibly complex, it was safer and easier to port the logic almost exactly. Therefore, I can't really take much credit for authorship, just translation. The original authors of FIGlet are listed on their website at http://www.figlet.org/.

The Python port was done by Christopher Jones cjones@insub.org (http://gruntle.org/).

It is currently maintained by Peter Waller (p@pwaller.net, github:pwaller)

The toilet fonts (.tlf) were imported from toilet 0.3-1, by Sam Hocevar (sam@zoy.org).

Thanks

Lots of people have helped make pyfiglet what it is but I particularly want to call out.

github:stefanor for various bug fixes and improvements and the debian packaging. github:peterbrittain for helping to close lots of issues.

License

The MIT License (MIT) Copyright © 2007-2023

Christopher Jones <cjones@insub.org>
Stefano Rivera <stefano@rivera.za.net>
Peter Waller <p@pwaller.net>
And various contributors (see git history).

(see LICENSE for full details)

Packaging status

Packaging status