Code freezes when trying to use newline with custom font

Hi there,

I am trying to use my custom font on a label that has \n in it and it freezes unexpectedly.

Font has been created using the online LVGL font converter.

Code doesn’t crash when \n is not included in the text.

If I try using the built-in montserrat font, the code doesn’t crash.

Seems kind of weird to me as it has worked in my other codes.

If you need anything, (e.g. a snapshot of the code), please reply below, thank you. :smiley:

Hi @matejsworkshop ,

I believe that, after converting the font, you should follow these steps:

  1. Copy the .c file into your project

  2. Declare the font in your code:

    LV_FONT_DECLARE(my_font_name);

  3. Apply it to a widget:

    lv_obj_set_style_text_font(label, &my_font_name, 0);

  4. Optionally, make it globally available by adding to lv_conf.h:

    #define LV_FONT_CUSTOM_DECLARE LV_FONT_DECLARE(my_font_name)

If you need special characters, include their Unicode code points in the range when converting, and use their UTF-8 hex sequences in strings.

A very common issue if your style is a local variable (not static or global), it gets destroyed after the function returns, causing undefined behavior/crashes

Hello @halyssonJr ,

thank you for your reply.

I have already done the steps you mentioned

  1. .c file is copied into my project (named cascadiamono_20.c)
  2. Font is declared after including the LVGL library: LV_FONT_DECLARE(cascadiamono_20);
  3. Font is applied to a label: lv_obj_set_style_text_font(start_subtitle, &cascadiamono_20, LV_PART_MAIN);

I am currently trying to investigate what causes the crash when using \n

Hi @matejsworkshop

Could you share more details about your project?

Hello @halyssonJr,

I am making an AI assistant using the M5Stack stopwatch development kit. I paired the LVGL library with the M5Unified library.

Let me know if you need anything else.

Hi @matejsworkshop,

Could you share a code snippet (font part) ? I’d like to analyze how you implemented it.

Hello @halyssonJr,

lv_obj_t *start_subtitle;

start_subtitle = lv_label_create(start_scr);
lv_obj_set_style_text_font(start_subtitle, &cascadiamono_20, LV_PART_MAIN);
lv_obj_set_style_text_align(start_subtitle, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
lv_obj_set_style_text_color(start_subtitle, lv_color_hex(0x808080), LV_PART_MAIN);
lv_label_set_text(start_subtitle, "your ai companion");
lv_obj_align(start_subtitle, LV_ALIGN_TOP_MID, 0, 30);

(this is the whole part of the label configuration)

If you need other parts of code, please let me know.

Below is the structure of the font library declaration. You just need to replace “.fallback = &Myfont32” with “.fallback = NULL” to solve the problem.

#if LVGL_VERSION_MAJOR >= 8
const lv_font_t Myfont32 = {
#else
lv_font_t Myfont32 = {
#endif
.get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt, /*Function pointer to get glyph’s data*/
.get_glyph_bitmap = lv_font_get_bitmap_fmt_txt, /*Function pointer to get glyph’s bitmap*/
.line_height = 30, /*The maximum line height required by the font*/
.base_line = 6, /*Baseline measured from the bottom of the line*/
#if !(LVGL_VERSION_MAJOR == 6 && LVGL_VERSION_MINOR == 0)
.subpx = LV_FONT_SUBPX_NONE,
#endif
#if LV_VERSION_CHECK(7, 4, 0) || LVGL_VERSION_MAJOR >= 8
.underline_position = -6,
.underline_thickness = 1,
#endif
.dsc = &font_dsc, /*The custom font data. Will be accessed by `get_glyph_bitmap/dsc` */
#if LV_VERSION_CHECK(8, 2, 0) || LVGL_VERSION_MAJOR >= 9
.fallback = NULL,
#endif
.user_data = NULL,
};

Hi there, thank you for your reply. I’ll try it right away.

Hello @kyle,

that indeed did fix the issue. Thank you for your help.