Tiny TTF font engine

Usage

Allow using TrueType fonts LVGL. Based on https://github.com/nothings/stb

When enabled in lv_conf.h with LV_USE_TINY_TTF lv_tiny_ttf_create_data(data, data_size, line_height) can be used to create a TTF font instance at the specified line height. You can then use that font anywhere lv_font_t is accepted.

By default, the TTF or OTF file must be embedded as an array, either in a header, or loaded into RAM in order to function.

However, if LV_TINY_TTF_FILE_SUPPORT is enabled, lv_tiny_ttf_create_file(path, line_height) will also be available, allowing tiny_ttf to stream from a file. The file must remain open the entire time the font is being used.

After a font is created, you can change the size by using lv_tiny_ttf_set_size(font, line_height).

By default, a font will use up to 4KB of cache to speed up rendering glyphs. This maximum can be changed by using lv_tiny_ttf_create_data_ex(data, data_size, line_height, cache_size) or lv_tiny_ttf_create_file_ex(path, line_height, cache_size) (when available). The cache size is indicated in bytes.

Example

Open a front with Tiny TTF from data array

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_TINY_TTF && LV_BUILD_EXAMPLES

#include "ubuntu_font.h"

/**
 * Load a font with Tiny_TTF
 */
void lv_example_tiny_ttf_1(void)
{
    /*Create style with the new font*/
    static lv_style_t style;
    lv_style_init(&style);
    lv_font_t * font = lv_tiny_ttf_create_data(ubuntu_font, sizeof(ubuntu_font), 30);
    lv_style_set_text_font(&style, font);
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);

    /*Create a label with the new style*/
    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_obj_add_style(label, &style, 0);
    lv_label_set_text(label, "Hello world\nI'm a font\ncreated\nwith Tiny TTF");
    lv_obj_center(label);
}
#endif

MicroPython code  

 GitHub Simulator
from ubuntu_font import ubuntu_font
# 
# Load a font with Tiny_TTF
#
#Create style with the new font
style = lv.style_t()
style.init()
font = lv.tiny_ttf_create_data(ubuntu_font, len(ubuntu_font), 30)
style.set_text_font(font)
style.set_text_align(lv.TEXT_ALIGN.CENTER)

# Create a label with the new style
label = lv.label(lv.scr_act())
label.add_style(style, 0)
label.set_text("Hello world\nI'm a font created with Tiny TTF")
label.center()

Load a font with Tiny_TTF from file

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_TINY_TTF && LV_TINY_TTF_FILE_SUPPORT && LV_BUILD_EXAMPLES

/**
 * Load a font with Tiny_TTF from file
 */
void lv_example_tiny_ttf_2(void)
{
    /*Create style with the new font*/
    static lv_style_t style;
    lv_style_init(&style);
    lv_font_t * font = lv_tiny_ttf_create_file("A:lvgl/examples/libs/tiny_ttf/Ubuntu-Medium.ttf", 30);
    lv_style_set_text_font(&style, font);
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);

    /*Create a label with the new style*/
    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_obj_add_style(label, &style, 0);
    lv_label_set_text(label, "Hello world\nI'm a font\ncreated\nwith Tiny TTF");
    lv_obj_center(label);
}
#endif

MicroPython code  

 GitHub Simulator
import fs_driver

# needed for dynamic font loading
fs_drv = lv.fs_drv_t()
fs_driver.fs_register(fs_drv, 'S')

# get the directory in which the script is running
try:
    script_path = __file__[:__file__.rfind('/')] if __file__.find('/') >= 0 else '.'
except NameError: 
    script_path = ''

#
# Load a font with Tiny_TTF from file
#
# Create style with the new font
style = lv.style_t()
style.init()
font = lv.tiny_ttf_create_file("S:" + script_path + "/Ubuntu-Medium.ttf", 30)
style.set_text_font(font)
style.set_text_align(lv.TEXT_ALIGN.CENTER)

# Create a label with the new style
label = lv.label(lv.scr_act())
label.add_style(style, 0)
label.set_text("Hello world\nI'm a font created with Tiny TTF")
label.center()

API

Functions

lv_font_t *lv_tiny_ttf_create_file(const char *path, lv_coord_t line_height)
lv_font_t *lv_tiny_ttf_create_file_ex(const char *path, lv_coord_t line_height, size_t cache_size)
lv_font_t *lv_tiny_ttf_create_data(const void *data, size_t data_size, lv_coord_t line_height)
lv_font_t *lv_tiny_ttf_create_data_ex(const void *data, size_t data_size, lv_coord_t line_height, size_t cache_size)
void lv_tiny_ttf_set_size(lv_font_t *font, lv_coord_t line_height)
void lv_tiny_ttf_destroy(lv_font_t *font)