The rule of thumb is that a TTF TrueType Font file has to be obtained somewhere.
You have to do the followings:
- Obtain a TTF file for the selected font
- Install the fontconvert utility from the Adafruit GFX Library
The fontconvert custom font creator utility can be found in the fontconvert subfolder of the Adafruit GFX Library. This is a script written in the C programming language, which means it has to be compiled before it can be used. The code of this script depends on the FreeType project, which is for rendering fonts on Unix type operating systems. It is a problem, if you - like me - mostly work on Windows! Adafruit provided a guide about how to setup a Windows based environment for font conversion. You might try it out, but for this post I took another approach.
I quickly installed the latest Ubuntu 16.04.03 LTS edition as a VirtualBox VM (Virtual Machine) into my host Windows operating system. You can exchange files through shared folders, so it is not a problem to move TTF font and the generated custom font files between Ubuntu Linux and Windows.
If your Ubuntu is up and running, you have to install FreeType 2.8 version onto it. It can be done based on this article. Don’t forget, that the development files are also required!
Open a Terminal window and type the followings:
sudo add-apt-repository ppa:glasen/freetype2
Now you are ready to compile the fontconvert utility:
- Go to the fontconvert subfolder of the Adafruit GFX Library
- Type make
As a result a new fontconvert executable was created, which can be used for font conversions.
Now you are able to follow the original guide to create your custom fonts. Let’s see an example!
- Dowload a font - e.g. Droid Sans Mono
- Unpack the zip file
- Copy the DroidSansMono.ttf file into the fontconvert subfolder
- Create a new 12 pt font - sudo ./fontconvert DroidSansMono.ttf 12 >DroidSansMono12pt.h
- Copy the created C header file into your project and use this font as the other custom fonts
I usually generate different size of fonts at once.
I keep the fonts in the Fonts subfolder to keep the Arduino project free.
If you use Sloeber IDE - like me, a separate C header file should be created for the font includes, because as part of the project source code compilation a file called sloeber.ino.cpp gets generated. This file contains the includes from the main ino sketch. If font imports can be found in this sketch, it will be included in the generated sloeber.ino.cpp file as well. This causes double imports, so the compilation will fail. If all the fonts are included in a separate C header file, this file will appear in the sloeber.ino.cpp file instead of the individual fonts, so double include won’t happen.