feat(font_load): add lv_font_* as paramater to lv_font_load

This way if a font is used by styles or widgets it can be easily reloaded
without updating the font pointer on all places.
This commit is contained in:
Gabor Kiss-Vamosi
2023-11-13 13:59:22 +01:00
parent d73f260002
commit ddc1bdf815
5 changed files with 111 additions and 53 deletions

View File

@@ -78,40 +78,43 @@ static unsigned int read_bits(bit_iterator_t * it, int n_bits, lv_fs_res_t * res
* GLOBAL FUNCTIONS
**********************/
lv_font_t * lv_font_load(const char * font_name)
lv_result_t lv_font_load(lv_font_t * font, const char * path)
{
lv_fs_file_t file;
lv_fs_res_t res = lv_fs_open(&file, font_name, LV_FS_MODE_RD);
if(res != LV_FS_RES_OK)
return NULL;
LV_ASSERT_NULL(font);
LV_ASSERT_NULL(path);
lv_font_t * font = lv_malloc(sizeof(lv_font_t));
if(font) {
memset(font, 0, sizeof(lv_font_t));
if(!lvgl_load_font(&file, font)) {
LV_LOG_WARN("Error loading font file: %s\n", font_name);
/*
* When `lvgl_load_font` fails it can leak some pointers.
* All non-null pointers can be assumed as allocated and
* `lv_font_free` should free them correctly.
*/
lv_font_free(font);
font = NULL;
}
lv_result_t result = LV_RESULT_INVALID;
lv_fs_file_t file;
lv_fs_res_t fs_res = lv_fs_open(&file, path, LV_FS_MODE_RD);
if(fs_res != LV_FS_RES_OK) return result;
lv_memzero(font, sizeof(lv_font_t));
if(lvgl_load_font(&file, font)) {
result = LV_RESULT_OK;
}
else {
LV_LOG_WARN("Error loading font file: %s\n", path);
/*
* When `lvgl_load_font` fails it can leak some pointers.
* All non-null pointers can be assumed as allocated and
* `lv_font_free` should free them correctly.
*/
lv_font_free(font);
}
lv_fs_close(&file);
return font;
return result;
}
#if LV_USE_FS_MEMFS
lv_font_t * lv_font_load_from_buffer(void * buffer, uint32_t size)
lv_result_t lv_font_load_from_buffer(lv_font_t * font, void * buffer, uint32_t size)
{
lv_fs_path_ex_t mempath;
lv_fs_make_path_from_buffer(&mempath, LV_FS_MEMFS_LETTER, buffer, size);
return lv_font_load((const char *)&mempath);
return lv_font_load(font, (const char *)&mempath);
}
#endif
@@ -175,7 +178,6 @@ void lv_font_free(lv_font_t * font)
}
lv_free(dsc);
}
lv_free(font);
}
}

View File

@@ -28,25 +28,27 @@ extern "C" {
/**
* Loads a `lv_font_t` object from a binary font file
* @param font_name filename where the font file is located
* @return a pointer to the font or NULL in case of error
* @param font pointer to font where to load
* @param path path where the font file is located
* @return LV_RESULT_OK on success; LV_RESULT_INVALID on error
*/
lv_font_t * lv_font_load(const char * fontName);
lv_result_t lv_font_load(lv_font_t * font, const char * font_name);
#if LV_USE_FS_MEMFS
/**
* Loads a `lv_font_t` object from a memory buffer containing the binary font file.
* Requires LV_USE_FS_MEMFS
* @param buffer address of the font file in the memory
* @param size size of the font file buffer
* @return a pointer to the font or NULL in case of error
* @param font pointer to font where to load
* @param buffer address of the font file in the memory
* @param size size of the font file buffer
* @return LV_RESULT_OK on success; LV_RESULT_INVALID on error
*/
lv_font_t * lv_font_load_from_buffer(void * buffer, uint32_t size);
lv_result_t lv_font_load_from_buffer(lv_font_t * font, void * buffer, uint32_t size);
#endif
/**
* Frees the memory allocated by the `lv_font_load()` function
* @param font lv_font_t object created by the lv_font_load function
* @param font lv_font_t object created by the lv_font_load function
*/
void lv_font_free(lv_font_t * font);

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

@@ -9,7 +9,6 @@
#if LV_BUILD_TEST
#include "../../lvgl.h"
//#include "lvgl.h"
#include "unity/unity.h"
@@ -52,9 +51,9 @@ extern uint8_t const font_1_buf[6876];
extern uint8_t const font_2_buf[7252];
extern uint8_t const font_3_buf[4892];
static lv_font_t * font_1_bin;
static lv_font_t * font_2_bin;
static lv_font_t * font_3_bin;
static lv_font_t font_1_bin;
static lv_font_t font_2_bin;
static lv_font_t font_3_bin;
void setUp(void)
{
@@ -64,18 +63,14 @@ void setUp(void)
void tearDown(void)
{
/* Function run after every test */
lv_obj_clean(lv_screen_active());
lv_font_free(font_1_bin);
lv_font_free(font_2_bin);
lv_font_free(font_3_bin);
}
static void common(void)
{
compare_fonts(&font_1, font_1_bin);
compare_fonts(&font_2, font_2_bin);
compare_fonts(&font_3, font_3_bin);
compare_fonts(&font_1, &font_1_bin);
compare_fonts(&font_2, &font_2_bin);
compare_fonts(&font_3, &font_3_bin);
/* create labels for testing */
lv_obj_t * scr = lv_screen_active();
@@ -87,21 +82,34 @@ static void common(void)
lv_obj_set_flex_align(scr, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_label_set_text(label1, "The quick brown fox jumped over the lazy dog");
lv_obj_set_style_text_font(label1, font_1_bin, 0);
lv_obj_set_style_text_font(label1, &font_1_bin, 0);
lv_label_set_text(label2, "The quick brown fox jumped over the lazy dog");
lv_obj_set_style_text_font(label2, font_2_bin, 0);
lv_obj_set_style_text_font(label2, &font_2_bin, 0);
lv_label_set_text(label3, "The quick brown fox jumped over the lazy dog");
lv_obj_set_style_text_font(label3, font_3_bin, 0);
lv_obj_set_style_text_font(label3, &font_3_bin, 0);
TEST_ASSERT_EQUAL_SCREENSHOT("font_loader_1.png");
lv_obj_clean(lv_screen_active());
lv_font_free(&font_1_bin);
lv_font_free(&font_2_bin);
lv_font_free(&font_3_bin);
}
void test_font_loader_with_cache(void)
{
/*Test with cache ('A' has cache)*/
font_1_bin = lv_font_load("A:src/test_assets/font_1.fnt");
font_2_bin = lv_font_load("A:src/test_assets/font_2.fnt");
font_3_bin = lv_font_load("A:src/test_assets/font_3.fnt");
lv_result_t res;
res = lv_font_load(&font_1_bin, "A:src/test_assets/font_1.fnt");
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
res = lv_font_load(&font_2_bin, "A:src/test_assets/font_2.fnt");
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
res = lv_font_load(&font_3_bin, "A:src/test_assets/font_3.fnt");
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
common();
}
@@ -109,9 +117,16 @@ void test_font_loader_with_cache(void)
void test_font_loader_no_cache(void)
{
/*Test without cache ('B' has NO cache)*/
font_1_bin = lv_font_load("B:src/test_assets/font_1.fnt");
font_2_bin = lv_font_load("B:src/test_assets/font_2.fnt");
font_3_bin = lv_font_load("B:src/test_assets/font_3.fnt");
lv_result_t res;
res = lv_font_load(&font_1_bin, "B:src/test_assets/font_1.fnt");
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
res = lv_font_load(&font_2_bin, "B:src/test_assets/font_2.fnt");
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
res = lv_font_load(&font_3_bin, "B:src/test_assets/font_3.fnt");
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
common();
}
@@ -119,13 +134,52 @@ void test_font_loader_no_cache(void)
void test_font_loader_from_buffer(void)
{
/*Test with memfs*/
font_1_bin = lv_font_load_from_buffer((void *)&font_1_buf, sizeof(font_1_buf));
font_2_bin = lv_font_load_from_buffer((void *)&font_2_buf, sizeof(font_2_buf));
font_3_bin = lv_font_load_from_buffer((void *)&font_3_buf, sizeof(font_3_buf));
lv_result_t res;
res = lv_font_load_from_buffer(&font_1_bin, (void *)&font_1_buf, sizeof(font_1_buf));
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
res = lv_font_load_from_buffer(&font_2_bin, (void *)&font_2_buf, sizeof(font_2_buf));
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
res = lv_font_load_from_buffer(&font_3_bin, (void *)&font_3_buf, sizeof(font_3_buf));
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
common();
}
void test_font_loader_reload(void)
{
/*Reload a font which is being used by a label*/
lv_obj_t * scr = lv_screen_active();
lv_obj_t * label = lv_label_create(scr);
lv_obj_center(label);
lv_label_set_text(label, "The quick brown fox jumped over the lazy dog");
lv_font_t font;
lv_result_t res;
res = lv_font_load(&font, "A:src/test_assets/font_2.fnt");
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
lv_obj_set_style_text_font(label, &font, 0);
TEST_ASSERT_EQUAL_SCREENSHOT("font_loader_2.png");
lv_font_free(&font);
res = lv_font_load(&font, "A:src/test_assets/font_3.fnt");
TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
lv_obj_report_style_change(NULL);
TEST_ASSERT_EQUAL_SCREENSHOT("font_loader_3.png");
lv_obj_delete(label);
lv_font_free(&font);
}
static int compare_fonts(lv_font_t * f1, lv_font_t * f2)
{
TEST_ASSERT_NOT_NULL_MESSAGE(f1, "font not null");