ASCII Art in the Terminal

One of the simpletest examples can be used to generate ASCII art output of the specified font file.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This example loads a font and uses it to print an
ASCII art representation of the given string specimen
"""

from adafruit_bitmap_font import bitmap_font  # pylint: disable=wrong-import-position

# you can change this to a different bdf or pcf font file
font_file = "fonts/LeagueSpartan-Bold-16.bdf"

# you can change the string that will get printed here
message = "<3 Blinka"

font = bitmap_font.load_font(font_file)

_, height, _, dy = font.get_bounding_box()
font.load_glyphs(message)

for y in range(height):
    for c in message:
        glyph = font.get_glyph(ord(c))
        if not glyph:
            continue
        glyph_y = y + (glyph.height - (height + dy)) + glyph.dy
        pixels = []
        if 0 <= glyph_y < glyph.height:
            for i in range(glyph.width):
                value = glyph.bitmap[i, glyph_y]
                pixel = " "
                if value > 0:
                    pixel = "#"
                pixels.append(pixel)
        else:
            pixels = ""
        print("".join(pixels) + " " * (glyph.shift_x - len(pixels)), end="")
    print()

To try it out on a PC or Raspberry Pi, run this command inside of the examples directory:

python bitmap_font_simpletest.py

To use it on a CircuitPython device save a copy of the script as code.py on your CIRCUITPY drive

It will print out an ASCII art representation of the string in the variable named message your input word in the given font. You can open the script and modify the message or the font used if you like.

Awesome Icons

The most typical way to use the library is for loading fonts to show letters, numbers, and other characters used to make words and strings. But font files can contain other types of glyphs as well. 

The Fork Awesome project is an open source collection of icons normally used in web interfaces. They've been converted to .pcf format for use with CircuitPython.

The example script below is included in the Bitmap_Font library examples directory.

Explore the forkawesome_icons.py file to learn the names of the available icons.

# SPDX-FileCopyrightText: 2021 Tim Cocks
# SPDX-License-Identifier: MIT

"""
This example uses adafruit_display_text.label to display fork awesome
icons.

More info here: https://emergent.unpythonic.net/01606790241
"""

import board
from bitmap_font_forkawesome_icons import microchip, python, terminal
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font

# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
display = board.DISPLAY

font_file = "fonts/forkawesome-42.pcf"

# Set text, font, and color
text = "{}  {}  {}".format(terminal, python, microchip)
font = bitmap_font.load_font(font_file)
color = 0xFF00FF

# Create the tet label
text_area = label.Label(font, text=text, color=color)

# Set the location
text_area.anchor_point = (0.5, 0.5)
text_area.anchored_position = (display.width // 2, display.height // 2)

# Show it
display.show(text_area)

while True:
    pass

This guide was first published on Feb 18, 2019. It was last updated on 2021-04-19 18:43:41 -0400.

This page (Example Scripts) was last updated on Dec 04, 2021.

Text editor powered by tinymce.