From af8c55724d7d36d254ee873119efb01a59c8e40b Mon Sep 17 00:00:00 2001 From: sirius506 Date: Thu, 3 Aug 2023 22:55:58 +0900 Subject: [PATCH] fix(tiny_ttf): re-initialize cache contents when cleared (#4426) --- examples/libs/tiny_ttf/index.rst | 7 +- examples/libs/tiny_ttf/lv_example_tiny_ttf.h | 1 + .../libs/tiny_ttf/lv_example_tiny_ttf_1.c | 7 +- .../libs/tiny_ttf/lv_example_tiny_ttf_3.c | 73 +++++++++++++++++++ .../tiny_ttf/{ubuntu_font.h => ubuntu_font.c} | 9 ++- src/libs/tiny_ttf/lv_tiny_ttf.c | 3 + 6 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c rename examples/libs/tiny_ttf/{ubuntu_font.h => ubuntu_font.c} (99%) diff --git a/examples/libs/tiny_ttf/index.rst b/examples/libs/tiny_ttf/index.rst index df05f2e75..4da7db543 100644 --- a/examples/libs/tiny_ttf/index.rst +++ b/examples/libs/tiny_ttf/index.rst @@ -1,4 +1,4 @@ -Open a front with Tiny TTF from data array +Open a font with Tiny TTF from data array ------------------------------------------ .. lv_example:: libs/tiny_ttf/lv_example_tiny_ttf_1 @@ -11,3 +11,8 @@ Load a font with Tiny_TTF from file .. lv_example:: libs/tiny_ttf/lv_example_tiny_ttf_2 :language: c +Change font size with Tiny_TTF +------------------------------ + +.. lv_example:: libs/tiny_ttf/lv_example_tiny_ttf_3 + :language: c diff --git a/examples/libs/tiny_ttf/lv_example_tiny_ttf.h b/examples/libs/tiny_ttf/lv_example_tiny_ttf.h index 10082a9a6..8e73f7c12 100644 --- a/examples/libs/tiny_ttf/lv_example_tiny_ttf.h +++ b/examples/libs/tiny_ttf/lv_example_tiny_ttf.h @@ -27,6 +27,7 @@ extern "C" { **********************/ void lv_example_tiny_ttf_1(void); void lv_example_tiny_ttf_2(void); +void lv_example_tiny_ttf_3(void); /********************** * MACROS 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 9f2358323..6b5608ade 100644 --- a/examples/libs/tiny_ttf/lv_example_tiny_ttf_1.c +++ b/examples/libs/tiny_ttf/lv_example_tiny_ttf_1.c @@ -1,17 +1,18 @@ #include "../../lv_examples.h" #if LV_USE_TINY_TTF && LV_BUILD_EXAMPLES -#include "ubuntu_font.h" - /** * Load a font with Tiny_TTF */ void lv_example_tiny_ttf_1(void) { + extern const uint8_t ubuntu_font[]; + extern const int ubuntu_font_size; + /*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, sizeof(ubuntu_font), 30); + 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); diff --git a/examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c b/examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c new file mode 100644 index 000000000..f25ecad15 --- /dev/null +++ b/examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c @@ -0,0 +1,73 @@ +#include "../../lv_examples.h" +#if LV_USE_TINY_TTF && LV_BUILD_EXAMPLES && LV_USE_MSG + +#define DISPLAY_TEXT "Hello World!" +#define MSG_NEW_SIZE 1 + +static void slider_event_cb(lv_event_t * e); +static void label_event_cb(lv_event_t * e); + +/** + * Change font size with Tiny_TTF + */ +void lv_example_tiny_ttf_3(void) +{ + extern const uint8_t ubuntu_font[]; + extern const int ubuntu_font_size; + + int lsize = 25; + + /*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, lsize); + 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_scr_act()); + lv_obj_center(slider); + lv_slider_set_range(slider, 5, 50); + lv_slider_set_value(slider, lsize, LV_ANIM_OFF); + lv_obj_align(slider, LV_ALIGN_BOTTOM_MID, 0, -50); + + lv_obj_t * slider_label = lv_label_create(lv_scr_act()); + lv_label_set_text_fmt(slider_label, "%d", lsize); + lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10); + + lv_obj_add_event(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, slider_label); + + /*Create a label with the new style*/ + lv_obj_t * label = lv_label_create(lv_scr_act()); + lv_obj_add_style(label, &style, 0); + lv_obj_add_event(label, label_event_cb, LV_EVENT_MSG_RECEIVED, font); + lv_obj_set_size(label, LV_SIZE_CONTENT, LV_SIZE_CONTENT); + lv_obj_center(label); + + lv_msg_subscribe_obj(MSG_NEW_SIZE, label, NULL); + lv_msg_send(MSG_NEW_SIZE, &lsize); +} + +static void slider_event_cb(lv_event_t * e) +{ + lv_obj_t * slider = lv_event_get_target(e); + lv_obj_t * label = lv_event_get_user_data(e); + int lsize; + + lsize = (int)lv_slider_get_value(slider); + + lv_label_set_text_fmt(label, "%d", lsize); + + lv_msg_send(MSG_NEW_SIZE, &lsize); +} + +static void label_event_cb(lv_event_t * e) +{ + lv_obj_t * label = lv_event_get_target(e); + lv_font_t * font = lv_event_get_user_data(e); + lv_msg_t * m = lv_event_get_msg(e); + const int32_t * v = lv_msg_get_payload(m); + + lv_tiny_ttf_set_size(font, *v); + lv_label_set_text(label, DISPLAY_TEXT); +} +#endif diff --git a/examples/libs/tiny_ttf/ubuntu_font.h b/examples/libs/tiny_ttf/ubuntu_font.c similarity index 99% rename from examples/libs/tiny_ttf/ubuntu_font.h rename to examples/libs/tiny_ttf/ubuntu_font.c index 51b2a8d0e..5bf94563a 100644 --- a/examples/libs/tiny_ttf/ubuntu_font.h +++ b/examples/libs/tiny_ttf/ubuntu_font.c @@ -1,5 +1,6 @@ -#ifndef UBUNTU_FONT_H -#define UBUNTU_FONT_H +#include "../../lv_examples.h" +#if LV_USE_TINY_TTF && LV_BUILD_EXAMPLES + const uint8_t ubuntu_font[] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x04, 0x00, 0x10, 0x44, 0x53, 0x49, 0x47, 0x1b, 0x79, 0x61, 0x63, 0x00, 0x02, 0xd2, 0x78, 0x00, 0x00, 0x19, 0x30, 0x47, 0x53, 0x55, 0x42, @@ -11965,4 +11966,6 @@ const uint8_t ubuntu_font[] = { 0x98, 0x63, 0x66, 0xc6, 0xac, 0xfa, 0xf6, 0x69, 0x22, 0x0c, 0x37, 0xe6, 0xa0, 0x60, 0xb8, 0xee, 0xbc, 0xe5, 0x4e, 0xcb, 0xb3, 0x5a, 0x00, 0x00 }; -#endif \ No newline at end of file + +const int ubuntu_font_size = sizeof(ubuntu_font); +#endif diff --git a/src/libs/tiny_ttf/lv_tiny_ttf.c b/src/libs/tiny_ttf/lv_tiny_ttf.c index b03b44644..bee05ae8f 100644 --- a/src/libs/tiny_ttf/lv_tiny_ttf.c +++ b/src/libs/tiny_ttf/lv_tiny_ttf.c @@ -207,8 +207,11 @@ static void ttf_cache_clear(ttf_cache_handle_t handle) } } TTF_CACHE_FREE(cache->buckets[i].entries); + cache->buckets[i].entries = NULL; + cache->buckets[i].capacity = 0; } } + cache->total_size = 0; } static void ttf_cache_destroy(ttf_cache_handle_t handle) {