feat(tiny_ttf): add lv_font_* as paramater to lv_tiny_ttf_create

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 14:46:22 +01:00
parent ddc1bdf815
commit 90a7e3e98f
6 changed files with 54 additions and 47 deletions

View File

@ -9,11 +9,14 @@ void lv_example_tiny_ttf_1(void)
extern const uint8_t ubuntu_font[];
extern const int ubuntu_font_size;
static lv_font_t font;
lv_result_t res = lv_tiny_ttf_create_data(&font, ubuntu_font, ubuntu_font_size, 30);
if(res == LV_RESULT_INVALID) return;
/*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, ubuntu_font_size, 30);
lv_style_set_text_font(&style, font);
lv_style_set_text_font(&style, &font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
/*Create a label with the new style*/

View File

@ -6,11 +6,14 @@
*/
void lv_example_tiny_ttf_2(void)
{
static lv_font_t font;
lv_result_t res = lv_tiny_ttf_create_file(&font, "A:lvgl/examples/libs/tiny_ttf/Ubuntu-Medium.ttf", 30);
if(res == LV_RESULT_INVALID) return;
/*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_font(&style, &font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
/*Create a label with the new style*/

View File

@ -4,7 +4,7 @@
static void font_size_observer_cb(lv_subject_t * subject, lv_observer_t * observer);
static lv_subject_t subject_font;
static lv_font_t font;
/**
* Change font size with Tiny_TTF
*/
@ -18,8 +18,9 @@ void lv_example_tiny_ttf_3(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, ubuntu_font_size, 25);
lv_style_set_text_font(&style, font);
lv_result_t res = lv_tiny_ttf_create_data(&font, ubuntu_font, ubuntu_font_size, 25);
if(res == LV_RESULT_INVALID) return;
lv_style_set_text_font(&style, &font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
lv_obj_t * slider = lv_slider_create(lv_screen_active());
@ -45,12 +46,9 @@ void lv_example_tiny_ttf_3(void)
static void font_size_observer_cb(lv_subject_t * subject, lv_observer_t * observer)
{
lv_style_t * style = observer->user_data;
lv_style_value_t v;
lv_style_get_prop(style, LV_STYLE_TEXT_FONT, &v);
lv_font_t * font = (lv_font_t *) v.ptr;
int32_t size = lv_subject_get_int(subject);
lv_tiny_ttf_set_size(font, size);
lv_tiny_ttf_set_size(&font, size);
lv_obj_report_style_change(style);
}

View File

@ -172,26 +172,26 @@ static const uint8_t * ttf_get_glyph_bitmap_cb(const lv_font_t * font, uint32_t
return buffer; /*Or NULL if not found*/
}
static lv_font_t * lv_tiny_ttf_create(const char * path, const void * data, size_t data_size, int32_t font_size,
static lv_result_t lv_tiny_ttf_create(lv_font_t * out_font, const char * path, const void * data, size_t data_size, int32_t font_size,
size_t cache_size)
{
LV_UNUSED(data_size);
LV_UNUSED(cache_size);
if((path == NULL && data == NULL) || 0 >= font_size) {
LV_LOG_ERROR("tiny_ttf: invalid argument\n");
return NULL;
return LV_RESULT_INVALID;
}
ttf_font_desc_t * dsc = lv_malloc_zeroed(sizeof(ttf_font_desc_t));
if(dsc == NULL) {
LV_LOG_ERROR("tiny_ttf: out of memory\n");
return NULL;
return LV_RESULT_INVALID;
}
#if LV_TINY_TTF_FILE_SUPPORT != 0
if(path != NULL) {
if(LV_FS_RES_OK != lv_fs_open(&dsc->file, path, LV_FS_MODE_RD)) {
lv_free(dsc);
LV_LOG_ERROR("tiny_ttf: unable to open %s\n", path);
return NULL;
return LV_RESULT_INVALID;
}
dsc->stream.file = &dsc->file;
}
@ -202,7 +202,7 @@ static lv_font_t * lv_tiny_ttf_create(const char * path, const void * data, size
if(0 == stbtt_InitFont(&dsc->info, &dsc->stream, stbtt_GetFontOffsetForIndex(&dsc->stream, 0))) {
lv_free(dsc);
LV_LOG_ERROR("tiny_ttf: init failed\n");
return NULL;
return LV_RESULT_INVALID;
}
#else
@ -210,39 +210,34 @@ static lv_font_t * lv_tiny_ttf_create(const char * path, const void * data, size
if(0 == stbtt_InitFont(&dsc->info, dsc->stream, stbtt_GetFontOffsetForIndex(dsc->stream, 0))) {
lv_free(dsc);
LV_LOG_ERROR("tiny_ttf: init failed\n");
return NULL;
return LV_RESULT_INVALID;
}
#endif
lv_font_t * out_font = lv_malloc_zeroed(sizeof(lv_font_t));
if(out_font == NULL) {
lv_free(dsc);
LV_LOG_ERROR("tiny_ttf: out of memory\n");
return NULL;
}
lv_memzero(out_font, sizeof(lv_font_t));
out_font->get_glyph_dsc = ttf_get_glyph_dsc_cb;
out_font->get_glyph_bitmap = ttf_get_glyph_bitmap_cb;
out_font->dsc = dsc;
lv_tiny_ttf_set_size(out_font, font_size);
return out_font;
return LV_RESULT_OK;
}
#if LV_TINY_TTF_FILE_SUPPORT != 0
lv_font_t * lv_tiny_ttf_create_file_ex(const char * path, int32_t font_size, size_t cache_size)
lv_result_t lv_tiny_ttf_create_file_ex(lv_font_t * font, const char * path, int32_t font_size, size_t cache_size)
{
return lv_tiny_ttf_create(path, NULL, 0, font_size, cache_size);
return lv_tiny_ttf_create(font, path, NULL, 0, font_size, cache_size);
}
lv_font_t * lv_tiny_ttf_create_file(const char * path, int32_t font_size)
lv_result_t lv_tiny_ttf_create_file(lv_font_t * font, const char * path, int32_t font_size)
{
return lv_tiny_ttf_create(path, NULL, 0, font_size, 0);
return lv_tiny_ttf_create(font, path, NULL, 0, font_size, 0);
}
#endif
lv_font_t * lv_tiny_ttf_create_data_ex(const void * data, size_t data_size, int32_t font_size, size_t cache_size)
lv_result_t lv_tiny_ttf_create_data_ex(lv_font_t * font, const void * data, size_t data_size, int32_t font_size, size_t cache_size)
{
return lv_tiny_ttf_create(NULL, data, data_size, font_size, cache_size);
return lv_tiny_ttf_create(font, NULL, data, data_size, font_size, cache_size);
}
lv_font_t * lv_tiny_ttf_create_data(const void * data, size_t data_size, int32_t font_size)
lv_result_t lv_tiny_ttf_create_data(lv_font_t * font, const void * data, size_t data_size, int32_t font_size)
{
return lv_tiny_ttf_create(NULL, data, data_size, font_size, 0);
return lv_tiny_ttf_create(font, NULL, data, data_size, font_size, 0);
}
void lv_tiny_ttf_set_size(lv_font_t * font, int32_t font_size)
{
@ -269,7 +264,6 @@ void lv_tiny_ttf_destroy(lv_font_t * font)
#endif
lv_free(ttf);
}
lv_free(font);
}
}
#endif

View File

@ -30,17 +30,17 @@ extern "C" {
**********************/
#if LV_TINY_TTF_FILE_SUPPORT !=0
/* create a font from the specified file or path with the specified line height.*/
lv_font_t * lv_tiny_ttf_create_file(const char * path, int32_t font_size);
lv_result_t lv_tiny_ttf_create_file(lv_font_t * font, const char * path, int32_t font_size);
/* create a font from the specified file or path with the specified line height with the specified cache size.*/
lv_font_t * lv_tiny_ttf_create_file_ex(const char * path, int32_t font_size, size_t cache_size);
lv_result_t lv_tiny_ttf_create_file_ex(lv_font_t * font, const char * path, int32_t font_size, size_t cache_size);
#endif
/* create a font from the specified data pointer with the specified line height.*/
lv_font_t * lv_tiny_ttf_create_data(const void * data, size_t data_size, int32_t font_size);
lv_result_t lv_tiny_ttf_create_data(lv_font_t * font, const void * data, size_t data_size, int32_t font_size);
/* create a font from the specified data pointer with the specified line height and the specified cache size.*/
lv_font_t * lv_tiny_ttf_create_data_ex(const void * data, size_t data_size, int32_t font_size, size_t cache_size);
lv_result_t lv_tiny_ttf_create_data_ex(lv_font_t * font, const void * data, size_t data_size, int32_t font_size, size_t cache_size);
/* set the size of the font to a new font_size*/
void lv_tiny_ttf_set_size(lv_font_t * font, int32_t font_size);

View File

@ -16,15 +16,18 @@ void tearDown(void)
void test_tiny_ttf_rendering_test(void)
{
#if LV_USE_TINY_TTF
static lv_font_t font;
/*Create a font*/
extern const uint8_t ubuntu_font[];
extern size_t ubuntu_font_size;
lv_font_t * font = lv_tiny_ttf_create_data(ubuntu_font, ubuntu_font_size, 30);
lv_result_t res = lv_tiny_ttf_create_data(&font, ubuntu_font, ubuntu_font_size, 30);
TEST_ASSERT_EQUAL(res, LV_RESULT_OK);
/*Create style with the new font*/
static lv_style_t style;
lv_style_init(&style);
lv_style_set_text_font(&style, font);
lv_style_set_text_font(&style, &font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
lv_style_set_bg_opa(&style, LV_OPA_COVER);
lv_style_set_bg_color(&style, lv_color_hex(0xffaaaa));
@ -40,7 +43,7 @@ void test_tiny_ttf_rendering_test(void)
TEST_ASSERT_EQUAL_SCREENSHOT("libs/tiny_ttf_1.png");
lv_obj_del(label);
lv_tiny_ttf_destroy(font);
lv_tiny_ttf_destroy(&font);
#else
TEST_PASS();
#endif
@ -51,10 +54,16 @@ void test_tiny_ttf_kerning()
#if LV_USE_TINY_TTF
extern const uint8_t kern_one_otf[];
extern size_t kern_one_otf_size;
lv_font_t * font_normal = lv_tiny_ttf_create_data(kern_one_otf, kern_one_otf_size, 80);
lv_font_t * font_none = lv_tiny_ttf_create_data(kern_one_otf, kern_one_otf_size, 80);
lv_font_set_kerning(font_none, LV_FONT_KERNING_NONE);
lv_font_t font_normal;
lv_font_t font_none;
lv_result_t res;
res = lv_tiny_ttf_create_data(&font_normal, kern_one_otf, kern_one_otf_size, 80);
TEST_ASSERT_EQUAL(res, LV_RESULT_OK);
res = lv_tiny_ttf_create_data(&font_none, kern_one_otf, kern_one_otf_size, 80);
TEST_ASSERT_EQUAL(res, LV_RESULT_OK);
lv_font_set_kerning(&font_none, LV_FONT_KERNING_NONE);
lv_obj_t * cont = lv_obj_create(lv_screen_active());
lv_obj_set_size(cont, lv_pct(90), lv_pct(90));
lv_obj_center(cont);
@ -63,17 +72,17 @@ void test_tiny_ttf_kerning()
lv_obj_t * label_normal = lv_label_create(cont);
lv_label_set_text(label_normal, "ıTuTuTı");
lv_obj_set_style_text_font(label_normal, font_normal, LV_PART_MAIN);
lv_obj_set_style_text_font(label_normal, &font_normal, LV_PART_MAIN);
lv_obj_t * label_none = lv_label_create(cont);
lv_label_set_text(label_none, "ıTuTuTı");
lv_obj_set_style_text_font(label_none, font_none, LV_PART_MAIN);
lv_obj_set_style_text_font(label_none, &font_none, LV_PART_MAIN);
TEST_ASSERT_EQUAL_SCREENSHOT("libs/tiny_ttf_2.png");
lv_obj_del(cont);
lv_tiny_ttf_destroy(font_normal);
lv_tiny_ttf_destroy(font_none);
lv_tiny_ttf_destroy(&font_normal);
lv_tiny_ttf_destroy(&font_none);
#else
TEST_PASS();
#endif