In this example, we're going to convert a .TTF (TrueType Format) into a .BDF (Bitmap Distribution Format). I'm using an open licensed font downloaded from Google Font, Source Serif Pro.
Use the file menu and choose Open Font from the list. Navigate to a directory where your desired font resides. Select the font and open it.
Set Font Size
From the element menu, select Bitmap Strikes Available. In this dialog, you will need to specific how large you want your font to be. The font size is fixed with Bitmap fonts, so if you want to use different sizes, you'll need to make separate files.
From the element menu, select Regenerate Bitmap Glyphs. Similar to the previous dialog, enter the font size of your liking. You can make it smaller here. Be aware, values too small will not generate BDF's.
Export Converted Font
From the file menu, select Generate Fonts. In the dialog, select No Outline Font and BDF from the dropdown options. Use the navigation UI to save the file in your directory of choice. Click the generate button to save the file.
This dialog menu will pop up after clicking generate. You can choose one of the options from the list. If you'd like a different font size, you can enter that in the Other labeled input box. Click OK to save it!
Optimize File Size
If you take a look at the file size of the .bdf, it's roughly around 900K – That can be a bit larger than needed, especially if you plan to store a lot of image and sound assets. In cases where you need to save on every byte, you can optimize the file size of your fonts by selecting only the characters you want to use. If you scroll through the full list of glyphs, you'll see there's extra special characters – A whole bunch of them! If you don't need them in your project, just select "space" (the glyph just before "!") plus the basic set of upper/lower and alphanumeric characters. You can click + hold and drag to make selections easier. With them selected, go through these steps:
- Select the glyphs you want to keep
- Use Edit→Select→Invert Selection to change the selection to the unwanted glyphs.
- Use Encoding→Detach & Remove Glyphs... to remove the unwanted glyphs. (You'll have to re-load your original font file to undo this step)
- Use Element→Regenerate Bitmap to reprocess the glyphs.
- Use File→Generate Font to save the reduced version of the file
Make sure your final font contains the letter capital M, which is used to estimate the height of letters in the font. Otherwise, the font will be incompatible with adafruit_display_text and give an error like
AttributeError: 'NoneType' object has no attribute 'height'
Optimize File Size (Manually)
If you prefer, you can also use a text editor to remove glyphs from a .bdf file. BDF files are just text!
Open a BDF file and search for “asciitilde” — this is usually the highest plain-ASCII-value glyph we want to preserve. A few lines down there will be an “ENDCHAR” line.
Delete everything after the ENDCHAR line, then add a line containing ENDFONT. That’s it! Save the file, which is usually just a small fraction of the original size.
You won’t get any accented characters or special punctuation this way, so it’s not always the right thing for every situation. For the majority of plain-text programs though, this can really help stretch your CIRCUITPY drive space!
The color of the fonts can be setup in your code. The CircuitPython library uses HEX color codes. This is similar to web color pickers but formatted slightly different. Most HEX color pickers use a hashtag in the front of the value, like, #000000. In CircuitPython, instead of a hashtag, 0x is used. Here's a few examples.
- Black = 0x000000
- White = 0xFFFFFF
- Purple = 0x8f42f4