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:
@@ -78,40 +78,43 @@ static unsigned int read_bits(bit_iterator_t * it, int n_bits, lv_fs_res_t * res
|
|||||||
* GLOBAL FUNCTIONS
|
* 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_ASSERT_NULL(font);
|
||||||
lv_fs_res_t res = lv_fs_open(&file, font_name, LV_FS_MODE_RD);
|
LV_ASSERT_NULL(path);
|
||||||
if(res != LV_FS_RES_OK)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
lv_font_t * font = lv_malloc(sizeof(lv_font_t));
|
lv_result_t result = LV_RESULT_INVALID;
|
||||||
if(font) {
|
|
||||||
memset(font, 0, sizeof(lv_font_t));
|
lv_fs_file_t file;
|
||||||
if(!lvgl_load_font(&file, font)) {
|
lv_fs_res_t fs_res = lv_fs_open(&file, path, LV_FS_MODE_RD);
|
||||||
LV_LOG_WARN("Error loading font file: %s\n", font_name);
|
if(fs_res != LV_FS_RES_OK) return result;
|
||||||
/*
|
|
||||||
* When `lvgl_load_font` fails it can leak some pointers.
|
lv_memzero(font, sizeof(lv_font_t));
|
||||||
* All non-null pointers can be assumed as allocated and
|
if(lvgl_load_font(&file, font)) {
|
||||||
* `lv_font_free` should free them correctly.
|
result = LV_RESULT_OK;
|
||||||
*/
|
}
|
||||||
lv_font_free(font);
|
else {
|
||||||
font = NULL;
|
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);
|
lv_fs_close(&file);
|
||||||
|
|
||||||
return font;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if LV_USE_FS_MEMFS
|
#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_path_ex_t mempath;
|
||||||
|
|
||||||
lv_fs_make_path_from_buffer(&mempath, LV_FS_MEMFS_LETTER, buffer, size);
|
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
|
#endif
|
||||||
|
|
||||||
@@ -175,7 +178,6 @@ void lv_font_free(lv_font_t * font)
|
|||||||
}
|
}
|
||||||
lv_free(dsc);
|
lv_free(dsc);
|
||||||
}
|
}
|
||||||
lv_free(font);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,25 +28,27 @@ extern "C" {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a `lv_font_t` object from a binary font file
|
* Loads a `lv_font_t` object from a binary font file
|
||||||
* @param font_name filename where the font file is located
|
* @param font pointer to font where to load
|
||||||
* @return a pointer to the font or NULL in case of error
|
* @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
|
#if LV_USE_FS_MEMFS
|
||||||
/**
|
/**
|
||||||
* Loads a `lv_font_t` object from a memory buffer containing the binary font file.
|
* Loads a `lv_font_t` object from a memory buffer containing the binary font file.
|
||||||
* Requires LV_USE_FS_MEMFS
|
* Requires LV_USE_FS_MEMFS
|
||||||
* @param buffer address of the font file in the memory
|
* @param font pointer to font where to load
|
||||||
* @param size size of the font file buffer
|
* @param buffer address of the font file in the memory
|
||||||
* @return a pointer to the font or NULL in case of error
|
* @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
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Frees the memory allocated by the `lv_font_load()` function
|
* 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);
|
void lv_font_free(lv_font_t * font);
|
||||||
|
|
||||||
|
|||||||
BIN
tests/ref_imgs/font_loader_2.png
Normal file
BIN
tests/ref_imgs/font_loader_2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
BIN
tests/ref_imgs/font_loader_3.png
Normal file
BIN
tests/ref_imgs/font_loader_3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.7 KiB |
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
#if LV_BUILD_TEST
|
#if LV_BUILD_TEST
|
||||||
#include "../../lvgl.h"
|
#include "../../lvgl.h"
|
||||||
//#include "lvgl.h"
|
|
||||||
|
|
||||||
#include "unity/unity.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_2_buf[7252];
|
||||||
extern uint8_t const font_3_buf[4892];
|
extern uint8_t const font_3_buf[4892];
|
||||||
|
|
||||||
static lv_font_t * font_1_bin;
|
static lv_font_t font_1_bin;
|
||||||
static lv_font_t * font_2_bin;
|
static lv_font_t font_2_bin;
|
||||||
static lv_font_t * font_3_bin;
|
static lv_font_t font_3_bin;
|
||||||
|
|
||||||
void setUp(void)
|
void setUp(void)
|
||||||
{
|
{
|
||||||
@@ -64,18 +63,14 @@ void setUp(void)
|
|||||||
void tearDown(void)
|
void tearDown(void)
|
||||||
{
|
{
|
||||||
/* Function run after every test */
|
/* 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)
|
static void common(void)
|
||||||
{
|
{
|
||||||
compare_fonts(&font_1, font_1_bin);
|
compare_fonts(&font_1, &font_1_bin);
|
||||||
compare_fonts(&font_2, font_2_bin);
|
compare_fonts(&font_2, &font_2_bin);
|
||||||
compare_fonts(&font_3, font_3_bin);
|
compare_fonts(&font_3, &font_3_bin);
|
||||||
|
|
||||||
/* create labels for testing */
|
/* create labels for testing */
|
||||||
lv_obj_t * scr = lv_screen_active();
|
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_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_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_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_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");
|
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)
|
void test_font_loader_with_cache(void)
|
||||||
{
|
{
|
||||||
/*Test with cache ('A' has cache)*/
|
/*Test with cache ('A' has cache)*/
|
||||||
font_1_bin = lv_font_load("A:src/test_assets/font_1.fnt");
|
lv_result_t res;
|
||||||
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");
|
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();
|
common();
|
||||||
}
|
}
|
||||||
@@ -109,9 +117,16 @@ void test_font_loader_with_cache(void)
|
|||||||
void test_font_loader_no_cache(void)
|
void test_font_loader_no_cache(void)
|
||||||
{
|
{
|
||||||
/*Test without cache ('B' has NO cache)*/
|
/*Test without cache ('B' has NO cache)*/
|
||||||
font_1_bin = lv_font_load("B:src/test_assets/font_1.fnt");
|
lv_result_t res;
|
||||||
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");
|
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();
|
common();
|
||||||
}
|
}
|
||||||
@@ -119,13 +134,52 @@ void test_font_loader_no_cache(void)
|
|||||||
void test_font_loader_from_buffer(void)
|
void test_font_loader_from_buffer(void)
|
||||||
{
|
{
|
||||||
/*Test with memfs*/
|
/*Test with memfs*/
|
||||||
font_1_bin = lv_font_load_from_buffer((void *)&font_1_buf, sizeof(font_1_buf));
|
lv_result_t res;
|
||||||
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));
|
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();
|
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)
|
static int compare_fonts(lv_font_t * f1, lv_font_t * f2)
|
||||||
{
|
{
|
||||||
TEST_ASSERT_NOT_NULL_MESSAGE(f1, "font not null");
|
TEST_ASSERT_NOT_NULL_MESSAGE(f1, "font not null");
|
||||||
|
|||||||
Reference in New Issue
Block a user