Home

Awesome

MicroFont is a MicroPython font rendering libray. It is based on the great MicroPython font conversion tool written by Peter Hinch.

Compared to the original code, I made this changes:

MicroFont library demo

So with the code you find in this repository you can:

  1. Convert TTF/OTF fonts to MicroFont files.
  2. Upload such files on your device flash.
  3. Use the microfont.py MicroPython library to load fonts and render text on the framebuffer.

Currently only two framebuffer formats are supported: MONO_HLSB and RGB565. They are by far the most widespread.

Converting fonts

In order to use your own fonts, you need to convert TTF (or OTF) fonts using the provided Python program:

python3 font_to_microfont.py -k charset.txt myfont_regular.ttf 18 victor:R:18.mfnt

The font generated will have all the characters specified inside the charset.txt file, so make sure that it contains what you need, but not more than you need in order to avoid wasting space on the flash.

Usage

font = MicroFont("victor:B:18.mfnt",cache_index=True)
fb = ... some MicroPython framebuffer
color = 1 # Color must be in the framebuffer color mode format.
angle = 90
font.write("Some text", fb, framebuf.MONO_HLSB, fb_width, fb_height, x, y, color, rot=angle, x_spacing=0, y_spacing=0)

Unfortunately the MicroPython framebuffer lacks methods to query its width, height, color format and other attributes. So you will have to pass them as arguments to the write() method, as in the example above.

If you want to use the lower-level functions, check the code: there are methods to obtain the characters bitmaps, write a single character on the screen and so forth. They are well commented inside the code.

Please note that:

These additional methods are also available, in order to retrieve generic fonts information:

def height(): return self.height
def baseline(): return self.baseline
def max_width(): return self.max_width
def monospaced(): return self.monospaced

Caching

You can cache chars or both chars an indexes by passing cache_index or also cache_chars arguments set to True during initialization. When character caching is enabled, index caching is automatically enabled regardless of what you provide.

Caching will store the index in memory at the first access. The index is quite small: four bytes per character, so this is a very good deal if you want to speed up rendering. However the major speed-up happens when also characters are cached in memory. In this case the memory usage is non trivial (and depends on the bitmap size of the characters and how many of them you use: they are cached independently as soon as a character is used the first time).

Here is an example of rendering speed in a Raspberry Pico 2040 with the three different modes. The timings were obtained by rendering "Hello World" with a 18 point font in a monochrome framebuffer. The rotation angle used does not affect speed.

For most use cases, just index caching or no caching seems the best bet, rendering is anyway fast enough for many kinds of applications.

Take in mind that larger fonts take more rendering time than smaller fonts, and also more memory if character caching is used, since they are bitmap representations. Instead, the memory used by indexes only depends on the number of characters mapped, not their size.

Fonts included in this repository

This repository contains a few example font files already converted into MFNT format. They are provided in a few variants. The original font used for the conversion is the Victor Mono font, released under the Open Font License.