Skip to content

NaitikSanas/TTF_Font_to_sFont_C_Array

Repository files navigation

sFont Generator - TTF to C Array Converter

Overview

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.

Concept

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.

It supports:

  • Basic ASCII characters from 32 (space) to 126 (~)
  • Any TTF font file
  • Custom font sizes

The output includes:

  • A byte array containing pixel data for each character
  • An sFONT structure with metadata such as width and height

Requirements

  • Python 3.x
  • Pillow library (install with pip install pillow)

Usage

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
};

Integration

To use the generated font in your project:

  1. Include the "fonts.h" header file in your project.
  2. Add the generated .c file to your source files.
  3. Use the sFONT structure with your display rendering functions to draw text on screen.

About

Tool to convert TTF fonts to sFont C Array

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors