diff --git a/examples/libs/tiny_ttf/lv_example_tiny_ttf_1.c b/examples/libs/tiny_ttf/lv_example_tiny_ttf_1.c index 728adc56b0..adbeca2d6f 100644 --- a/examples/libs/tiny_ttf/lv_example_tiny_ttf_1.c +++ b/examples/libs/tiny_ttf/lv_example_tiny_ttf_1.c @@ -9,14 +9,11 @@ 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_style_set_text_font(&style, &font); + 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_align(&style, LV_TEXT_ALIGN_CENTER); /*Create a label with the new style*/ diff --git a/examples/libs/tiny_ttf/lv_example_tiny_ttf_2.c b/examples/libs/tiny_ttf/lv_example_tiny_ttf_2.c index f057f97827..3133b955e8 100644 --- a/examples/libs/tiny_ttf/lv_example_tiny_ttf_2.c +++ b/examples/libs/tiny_ttf/lv_example_tiny_ttf_2.c @@ -6,14 +6,11 @@ */ 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_style_set_text_font(&style, &font); + 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_align(&style, LV_TEXT_ALIGN_CENTER); /*Create a label with the new style*/ diff --git a/examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c b/examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c index 35072cdb5b..8b237e17a9 100644 --- a/examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c +++ b/examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c @@ -4,7 +4,7 @@ static void font_size_observer_cb(lv_observer_t * observer, lv_subject_t * subject); static lv_subject_t subject_font; -static lv_font_t font; + /** * Change font size with Tiny_TTF */ @@ -18,9 +18,8 @@ void lv_example_tiny_ttf_3(void) /*Create style with the new font*/ static lv_style_t style; lv_style_init(&style); - 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_font_t * font = lv_tiny_ttf_create_data(ubuntu_font, ubuntu_font_size, 25); + 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()); @@ -46,9 +45,12 @@ void lv_example_tiny_ttf_3(void) static void font_size_observer_cb(lv_observer_t * observer, lv_subject_t * subject) { 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); } diff --git a/src/libs/tiny_ttf/lv_tiny_ttf.c b/src/libs/tiny_ttf/lv_tiny_ttf.c index 1053637a08..7852202735 100644 --- a/src/libs/tiny_ttf/lv_tiny_ttf.c +++ b/src/libs/tiny_ttf/lv_tiny_ttf.c @@ -75,7 +75,7 @@ static bool ttf_get_glyph_dsc_cb(const lv_font_t * font, lv_font_glyph_dsc_t * d static const void * ttf_get_glyph_bitmap_cb(lv_font_glyph_dsc_t * g_dsc, uint32_t unicode_letter, lv_draw_buf_t * draw_buf); static void ttf_release_glyph_cb(const lv_font_t * font, lv_font_glyph_dsc_t * g_dsc); -static lv_result_t lv_tiny_ttf_create(lv_font_t * out_font, const char * path, const void * data, size_t data_size, +static lv_font_t * lv_tiny_ttf_create(const char * path, const void * data, size_t data_size, int32_t font_size, size_t cache_size); @@ -98,16 +98,6 @@ static lv_cache_compare_res_t tiny_ttf_cache_compare_cb(const tiny_ttf_cache_dat /********************** * GLOBAL FUNCTIONS **********************/ -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(font, NULL, data, data_size, font_size, cache_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(font, NULL, data, data_size, font_size, 0); -} void lv_tiny_ttf_set_size(lv_font_t * font, int32_t font_size) { @@ -125,19 +115,21 @@ void lv_tiny_ttf_set_size(lv_font_t * font, int32_t font_size) void lv_tiny_ttf_destroy(lv_font_t * font) { - if(font != NULL) { - if(font->dsc != NULL) { - ttf_font_desc_t * ttf = (ttf_font_desc_t *)font->dsc; + LV_ASSERT_NULL(font); + + if(font->dsc != NULL) { + ttf_font_desc_t * ttf = (ttf_font_desc_t *)font->dsc; #if LV_TINY_TTF_FILE_SUPPORT != 0 - if(ttf->stream.file != NULL) { - lv_fs_close(&ttf->file); - } -#endif - lv_cache_drop_all(tiny_ttf_cache, (void *)font->dsc); - lv_free(ttf); - font->dsc = NULL; + if(ttf->stream.file != NULL) { + lv_fs_close(&ttf->file); } +#endif + lv_cache_drop_all(tiny_ttf_cache, (void *)font->dsc); + lv_free(ttf); + font->dsc = NULL; } + + lv_free(font); } void lv_tiny_ttf_init(void) @@ -156,16 +148,6 @@ void lv_tiny_ttf_deinit(void) lv_cache_destroy(tiny_ttf_cache, NULL); } -#if LV_TINY_TTF_FILE_SUPPORT != 0 -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(font, path, NULL, 0, font_size, cache_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(font, path, NULL, 0, font_size, 0); -} -#endif /********************** * STATIC FUNCTIONS **********************/ @@ -276,27 +258,26 @@ static void ttf_release_glyph_cb(const lv_font_t * font, lv_font_glyph_dsc_t * g g_dsc->entry = NULL; } -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, +static lv_font_t * lv_tiny_ttf_create(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 LV_RESULT_INVALID; + return NULL; } 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 LV_RESULT_INVALID; + return NULL; } #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 LV_RESULT_INVALID; + return NULL; } dsc->stream.file = &dsc->file; } @@ -307,7 +288,7 @@ static lv_result_t lv_tiny_ttf_create(lv_font_t * out_font, const char * path, c 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 LV_RESULT_INVALID; + return NULL; } #else @@ -315,17 +296,40 @@ static lv_result_t lv_tiny_ttf_create(lv_font_t * out_font, const char * path, c 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 LV_RESULT_INVALID; + return NULL; } #endif - lv_memzero(out_font, sizeof(lv_font_t)); + 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; + } out_font->get_glyph_dsc = ttf_get_glyph_dsc_cb; out_font->get_glyph_bitmap = ttf_get_glyph_bitmap_cb; out_font->release_glyph = ttf_release_glyph_cb; out_font->dsc = dsc; lv_tiny_ttf_set_size(out_font, font_size); - return LV_RESULT_OK; + return out_font; +} +#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) +{ + return lv_tiny_ttf_create(path, NULL, 0, font_size, cache_size); +} +lv_font_t * lv_tiny_ttf_create_file(const char * path, int32_t font_size) +{ + return lv_tiny_ttf_create(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) +{ + return lv_tiny_ttf_create(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) +{ + return lv_tiny_ttf_create(NULL, data, data_size, font_size, 0); } /*----------------- @@ -363,12 +367,14 @@ static bool tiny_ttf_cache_create_cb(tiny_ttf_cache_data_t * node, void * user_d node->draw_buf = draw_buf; return true; } + static void tiny_ttf_cache_free_cb(tiny_ttf_cache_data_t * node, void * user_data) { LV_UNUSED(user_data); lv_draw_buf_destroy(node->draw_buf); } + static lv_cache_compare_res_t tiny_ttf_cache_compare_cb(const tiny_ttf_cache_data_t * lhs, const tiny_ttf_cache_data_t * rhs) { diff --git a/src/libs/tiny_ttf/lv_tiny_ttf.h b/src/libs/tiny_ttf/lv_tiny_ttf.h index 4866b72c5c..3ad65219d6 100644 --- a/src/libs/tiny_ttf/lv_tiny_ttf.h +++ b/src/libs/tiny_ttf/lv_tiny_ttf.h @@ -31,10 +31,10 @@ extern "C" { #if LV_TINY_TTF_FILE_SUPPORT !=0 /* create a font from the specified file or path with the specified line height.*/ -lv_result_t lv_tiny_ttf_create_file(lv_font_t * font, const char * path, int32_t font_size); +lv_font_t * lv_tiny_ttf_create_file(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_result_t lv_tiny_ttf_create_file_ex(lv_font_t * font, const char * path, int32_t font_size, size_t cache_size); +lv_font_t * lv_tiny_ttf_create_file_ex(const char * path, int32_t font_size, size_t cache_size); #endif void lv_tiny_ttf_init(void); @@ -42,11 +42,10 @@ void lv_tiny_ttf_init(void); void lv_tiny_ttf_deinit(void); /* create a font from the specified data pointer with the specified line height.*/ -lv_result_t lv_tiny_ttf_create_data(lv_font_t * font, const void * data, size_t data_size, int32_t font_size); +lv_font_t * lv_tiny_ttf_create_data(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_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); +lv_font_t * lv_tiny_ttf_create_data_ex(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); diff --git a/tests/micropy_test/micropy.py b/tests/micropy_test/micropy.py index 2083b67452..9891d3a8a6 100644 --- a/tests/micropy_test/micropy.py +++ b/tests/micropy_test/micropy.py @@ -115,8 +115,7 @@ def create_ui(): try: font_montserrat_24 = lv.font_montserrat_24 except AttributeError: - font_montserrat_24 = lv.font_t() - lv.tiny_ttf_create_file(font_montserrat_24, 'A:font_montserrat_24.ttf', 32) + font_montserrat_24 = lv.tiny_ttf_create_file('A:font_montserrat_24.ttf', 32) style_big_font.set_text_font(font_montserrat_24) diff --git a/tests/src/test_cases/libs/test_tiny_ttf.c b/tests/src/test_cases/libs/test_tiny_ttf.c index b5c21487ff..6ac2768ada 100644 --- a/tests/src/test_cases/libs/test_tiny_ttf.c +++ b/tests/src/test_cases/libs/test_tiny_ttf.c @@ -16,18 +16,15 @@ 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_result_t res = lv_tiny_ttf_create_data(&font, ubuntu_font, ubuntu_font_size, 30); - TEST_ASSERT_EQUAL(res, LV_RESULT_OK); + lv_font_t * font = lv_tiny_ttf_create_data(ubuntu_font, ubuntu_font_size, 30); /*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)); @@ -43,7 +40,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 @@ -54,16 +51,10 @@ 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_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); + 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); - 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); @@ -72,17 +63,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