How to load font at runtime with C?

What do you want to achieve?

I’m trying to load a font from the filesystem at runtime and use it in a label.

What have you tried so far?

I’ve created a *.bin file using the online converter. I’ve also read the documentation about loading fonts and the built-in LVGL filesystem but still can’t get things to work.

I’ve also copy and pasted the filesystem example for PC to use with the code below.

Code to reproduce

// Initialize filesystem from https://github.com/lvgl/lv_fs_if/
lv_fs_if_pc_init();

// Create style with font
lv_font_t *notosans = lv_font_load("S:notosans.bin"); // notosans.bin is in project root
static lv_style_t noto_sans_font_style;
lv_style_init(&noto_sans_font_style);
lv_style_set_text_font(&noto_sans_font_style, notosans);

// Create label with loaded font
lv_obj_t *label = lv_label_create(lv_scr_act());
lv_obj_add_style(label, &noto_sans_font_style, LV_PART_MAIN); // note removing this line causes the code to run, but without the correct font obviously
lv_label_set_text(label, "Hello World!");
lv_obj_center(label);

lv_font_free(notosans);

I’ve also set LV_USE_FS_STDIO to 1 and LV_FS_STDIO_LETTER to S in lv_conf.h

Screenshot and/or video

Result without loading font at runtime.
image

Window looks like this before seg-faulting with the code above.
image

Environment

  • MCU/MPU/Board: Using the simulator on Ubuntu 24.04 and C stdio functions from the filesystem example. Ultimately I want to do this on an RP2040 with and SD Card but I want to have working LVGL code before trying that.
  • LVGL version: 8.4

Can you spot anything obvious I’m doing wrong?

Please? Is there any working example of loading a font from the filesystem that I can use for reference?

Not using v8.4 but tried this in the simulator and it seems to work.

One note, you can’t call lv_font_free(notosans); while the font is required by LVGL to draw something on screen otherwise you get a segmentation fault. As long the object using that font is “valid”/“on Screen” the font cannot be destroyed.
Another thing, make sure you are not selecting the option Enable Font compression don’t know if V8 is compatible with that.

_test_font = lv_binfont_create("A:E:/test123.bin");
  if (_test_font)
  {
    printf("Custom bin font loaded successfully!\n");
  }
  else
  {
    printf("Failed to load custom bin font!\n");
  }

  lv_obj_set_style_text_font(objects.text_check_box, _test_font, LV_PART_MAIN | LV_STATE_DEFAULT);

Turns out I had to increase LV_MEM_SIZE :upside_down_face: (my font file is 1.2MB)