This Python script converts TrueType Font (TTF) files into a simple bitmap format called sFONT that can be used in C-based embedded projects. It rasterizes printable ASCII characters (from 0x20 to 0x7E) into byte arrays representing the pixel data. The output is a C source file containing the bitmap data and a structure describing the font's width and height.
The script takes a TTF file and a specified font size, then generates a monochrome bitmap for each character by drawing it onto an image and converting the pixels into bytes. These byte arrays are organized into a format that can be easily included in embedded systems where dynamic font rendering is not feasible.
- Basic ASCII characters from 32 (space) to 126 (~)
- Any TTF font file
- Custom font sizes
- A byte array containing pixel data for each character
- An sFONT structure with metadata such as width and height
- Python 3.x
- Pillow library (install with pip install pillow)
Run the script from the command line with the following syntax:
python ttf_to_sfont.py path/to/font.ttf font_size
For example:
python ttf_to_sfont.py Arial.ttf 16
This will generate a file named Arial_16.c containing the bitmap data and sFONT structure.
Output The output C file includes:
- A constant unsigned char array with bitmap data for all supported characters
- An sFONT structure pointing to this array, along with width and height fields
Example snippet from the generated file:
#include "fonts.h"
const unsigned char Arial_16_table[] = {
// 'A' (0x41)
0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,
// 'B' (0x42)
0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,
...
};
sFONT Arial_16 = {
Arial_16_table,
8, // Width
16 // Height
};To use the generated font in your project:
- Include the "fonts.h" header file in your project.
- Add the generated .c file to your source files.
- Use the sFONT structure with your display rendering functions to draw text on screen.