mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-10 04:37:55 +08:00
feat(libs): add freetype font manager (#6482)
Signed-off-by: FASTSHIFT <vifextech@foxmail.com>
This commit is contained in:
@@ -1464,6 +1464,15 @@ menu "LVGL configuration"
|
||||
default y
|
||||
help
|
||||
This can save some memory, but not much. After the quick access bar is created, it can be hidden by clicking the button at the top left corner of the browsing area, which is very useful for small screen devices.
|
||||
|
||||
config LV_USE_FONT_MANAGER
|
||||
bool "Enable freetype font manager"
|
||||
depends on LV_USE_FREETYPE
|
||||
default n
|
||||
config LV_FONT_MANAGER_NAME_MAX_LEN
|
||||
int "Font manager name max length"
|
||||
depends on LV_USE_FONT_MANAGER
|
||||
default 32
|
||||
endmenu
|
||||
|
||||
menu "Devices"
|
||||
|
||||
@@ -970,6 +970,16 @@
|
||||
#define LV_FILE_EXPLORER_QUICK_ACCESS 1
|
||||
#endif
|
||||
|
||||
/*1: Enable freetype font manager*/
|
||||
/*Requires: LV_USE_FREETYPE*/
|
||||
#define LV_USE_FONT_MANAGER 0
|
||||
#if LV_USE_FONT_MANAGER
|
||||
|
||||
/*Font manager name max length*/
|
||||
#define LV_FONT_MANAGER_NAME_MAX_LEN 32
|
||||
|
||||
#endif
|
||||
|
||||
/*==================
|
||||
* DEVICES
|
||||
*==================*/
|
||||
|
||||
@@ -3083,6 +3083,28 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*1: Enable freetype font manager*/
|
||||
/*Requires: LV_USE_FREETYPE*/
|
||||
#ifndef LV_USE_FONT_MANAGER
|
||||
#ifdef CONFIG_LV_USE_FONT_MANAGER
|
||||
#define LV_USE_FONT_MANAGER CONFIG_LV_USE_FONT_MANAGER
|
||||
#else
|
||||
#define LV_USE_FONT_MANAGER 0
|
||||
#endif
|
||||
#endif
|
||||
#if LV_USE_FONT_MANAGER
|
||||
|
||||
/*Font manager name max length*/
|
||||
#ifndef LV_FONT_MANAGER_NAME_MAX_LEN
|
||||
#ifdef CONFIG_LV_FONT_MANAGER_NAME_MAX_LEN
|
||||
#define LV_FONT_MANAGER_NAME_MAX_LEN CONFIG_LV_FONT_MANAGER_NAME_MAX_LEN
|
||||
#else
|
||||
#define LV_FONT_MANAGER_NAME_MAX_LEN 32
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/*==================
|
||||
* DEVICES
|
||||
*==================*/
|
||||
|
||||
Executable
+601
File diff suppressed because it is too large
Load Diff
Executable
+106
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* @file lv_font_manager.h
|
||||
*
|
||||
*/
|
||||
#ifndef LV_FONT_MANAGER_H
|
||||
#define LV_FONT_MANAGER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
#include "../../misc/lv_types.h"
|
||||
|
||||
#if LV_USE_FONT_MANAGER
|
||||
|
||||
#if !LV_USE_FREETYPE
|
||||
#error "LV_USE_FONT_MANAGER requires LV_USE_FREETYPE"
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
typedef struct _lv_font_manager_t lv_font_manager_t;
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Create main font manager.
|
||||
* @param recycle_cache_size number of fonts that were recently deleted from the cache.
|
||||
* @return pointer to main font manager.
|
||||
*/
|
||||
lv_font_manager_t * lv_font_manager_create(uint32_t recycle_cache_size);
|
||||
|
||||
/**
|
||||
* Delete main font manager.
|
||||
* @param manager pointer to main font manager.
|
||||
* @return return true if the deletion was successful.
|
||||
*/
|
||||
bool lv_font_manager_delete(lv_font_manager_t * manager);
|
||||
|
||||
/**
|
||||
* Add the font file path.
|
||||
* @param manager pointer to main font manager.
|
||||
* @param name font name.
|
||||
* @param path font file path.
|
||||
*/
|
||||
void lv_font_manager_add_path(lv_font_manager_t * manager, const char * name, const char * path);
|
||||
|
||||
/**
|
||||
* Add the font file path with static memory.
|
||||
* @param manager pointer to main font manager.
|
||||
* @param name font name.
|
||||
* @param path font file path.
|
||||
*/
|
||||
void lv_font_manager_add_path_static(lv_font_manager_t * manager, const char * name, const char * path);
|
||||
|
||||
/**
|
||||
* Remove the font file path.
|
||||
* @param manager pointer to main font manager.
|
||||
* @param name font name.
|
||||
* @return return true if the remove was successful.
|
||||
*/
|
||||
bool lv_font_manager_remove_path(lv_font_manager_t * manager, const char * name);
|
||||
|
||||
/**
|
||||
* Create font.
|
||||
* @param manager pointer to main font manager.
|
||||
* @param font_family font family name.
|
||||
* @param render_mode font render mode, see lv_freetype_font_render_mode_t.
|
||||
* @param size font size.
|
||||
* @param style font style, see lv_freetype_font_style_t.
|
||||
* @return point to the created font
|
||||
*/
|
||||
lv_font_t * lv_font_manager_create_font(lv_font_manager_t * manager, const char * font_family, uint16_t render_mode,
|
||||
uint32_t size, uint16_t style);
|
||||
|
||||
/**
|
||||
* Delete font.
|
||||
* @param manager pointer to main font manager.
|
||||
* @param font point to the font.
|
||||
* @return return true if the deletion was successful.
|
||||
*/
|
||||
void lv_font_manager_delete_font(lv_font_manager_t * manager, lv_font_t * font);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#endif /* LV_USE_FONT_MANAGER */
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* FONT_MANAGER_MANAGER_H */
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* @file lv_font_manager_recycle.c
|
||||
*
|
||||
*/
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
#include "lv_font_manager_recycle.h"
|
||||
|
||||
#if LV_USE_FONT_MANAGER
|
||||
|
||||
#include "../../lvgl.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
struct _lv_font_manager_recycle_t {
|
||||
lv_ll_t recycle_ll;
|
||||
uint32_t max_size;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
lv_freetype_info_t ft_info;
|
||||
char name[LV_FONT_MANAGER_NAME_MAX_LEN];
|
||||
lv_font_t * font;
|
||||
} lv_font_recycle_t;
|
||||
|
||||
/**********************
|
||||
* STATIC PROTOTYPES
|
||||
**********************/
|
||||
|
||||
static void lv_font_recycle_close(lv_font_manager_recycle_t * manager, lv_font_recycle_t * recycle);
|
||||
static void lv_font_manager_recycle_remove_tail(lv_font_manager_recycle_t * manager);
|
||||
|
||||
/**********************
|
||||
* STATIC VARIABLES
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL FUNCTIONS
|
||||
**********************/
|
||||
|
||||
lv_font_manager_recycle_t * lv_font_manager_recycle_create(uint32_t max_size)
|
||||
{
|
||||
lv_font_manager_recycle_t * manager = lv_malloc_zeroed(sizeof(lv_font_manager_recycle_t));
|
||||
LV_ASSERT_MALLOC(manager);
|
||||
if(!manager) {
|
||||
LV_LOG_ERROR("malloc failed for lv_font_manager_recycle_t");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_lv_ll_init(&manager->recycle_ll, sizeof(lv_font_recycle_t));
|
||||
manager->max_size = max_size;
|
||||
|
||||
LV_LOG_INFO("success");
|
||||
return manager;
|
||||
}
|
||||
|
||||
void lv_font_manager_recycle_delete(lv_font_manager_recycle_t * manager)
|
||||
{
|
||||
LV_ASSERT_NULL(manager);
|
||||
|
||||
lv_ll_t * recycle_ll = &manager->recycle_ll;
|
||||
|
||||
lv_font_recycle_t * recycle = _lv_ll_get_head(recycle_ll);
|
||||
|
||||
/* clear all recycle */
|
||||
while(recycle != NULL) {
|
||||
lv_font_recycle_t * recycle_next = _lv_ll_get_next(recycle_ll, recycle);
|
||||
lv_font_recycle_close(manager, recycle);
|
||||
recycle = recycle_next;
|
||||
}
|
||||
|
||||
lv_free(manager);
|
||||
|
||||
LV_LOG_INFO("success");
|
||||
}
|
||||
|
||||
lv_font_t * lv_font_manager_recycle_get_reuse(lv_font_manager_recycle_t * manager, const lv_freetype_info_t * ft_info)
|
||||
{
|
||||
LV_ASSERT_NULL(manager);
|
||||
LV_ASSERT_NULL(ft_info);
|
||||
|
||||
lv_ll_t * recycle_ll = &manager->recycle_ll;
|
||||
|
||||
LV_LOG_INFO("font: %s(%d) searching...", ft_info->name, ft_info->size);
|
||||
|
||||
lv_font_recycle_t * recycle;
|
||||
_LV_LL_READ(recycle_ll, recycle) {
|
||||
/* match font */
|
||||
if(lv_freetype_info_is_equal(ft_info, &recycle->ft_info)) {
|
||||
lv_font_t * font = recycle->font;
|
||||
LV_LOG_INFO("found font: %p", font);
|
||||
|
||||
/* remove reused font */
|
||||
_lv_ll_remove(recycle_ll, recycle);
|
||||
lv_free(recycle);
|
||||
return font;
|
||||
}
|
||||
}
|
||||
|
||||
LV_LOG_INFO("NOT found");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void lv_font_manager_recycle_set_reuse(lv_font_manager_recycle_t * manager, lv_font_t * font,
|
||||
const lv_freetype_info_t * ft_info)
|
||||
{
|
||||
LV_ASSERT_NULL(manager);
|
||||
LV_ASSERT_NULL(ft_info);
|
||||
|
||||
lv_ll_t * recycle_ll = &manager->recycle_ll;
|
||||
|
||||
/* check recycled size */
|
||||
if(_lv_ll_get_len(recycle_ll) >= manager->max_size) {
|
||||
LV_LOG_INFO("recycle full, remove tail font...");
|
||||
lv_font_manager_recycle_remove_tail(manager);
|
||||
}
|
||||
|
||||
/* record reuse font */
|
||||
lv_font_recycle_t * recycle = _lv_ll_ins_head(recycle_ll);
|
||||
LV_ASSERT_MALLOC(recycle);
|
||||
lv_memzero(recycle, sizeof(lv_font_recycle_t));
|
||||
|
||||
lv_strncpy(recycle->name, ft_info->name, sizeof(recycle->name));
|
||||
recycle->name[sizeof(recycle->name) - 1] = '\0';
|
||||
|
||||
recycle->font = font;
|
||||
recycle->ft_info = *ft_info;
|
||||
recycle->ft_info.name = recycle->name;
|
||||
|
||||
LV_LOG_INFO("insert font: %s(%d) to reuse list", ft_info->name, ft_info->size);
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
static void lv_font_recycle_close(lv_font_manager_recycle_t * manager, lv_font_recycle_t * recycle)
|
||||
{
|
||||
LV_ASSERT_NULL(manager);
|
||||
LV_ASSERT_NULL(recycle);
|
||||
|
||||
LV_LOG_INFO("font: %s(%d) close", recycle->ft_info.name, recycle->ft_info.size);
|
||||
lv_freetype_font_delete(recycle->font);
|
||||
|
||||
_lv_ll_remove(&manager->recycle_ll, recycle);
|
||||
lv_free(recycle);
|
||||
}
|
||||
|
||||
static void lv_font_manager_recycle_remove_tail(lv_font_manager_recycle_t * manager)
|
||||
{
|
||||
lv_font_recycle_t * tail = _lv_ll_get_tail(&manager->recycle_ll);
|
||||
LV_ASSERT_NULL(tail);
|
||||
lv_font_recycle_close(manager, tail);
|
||||
}
|
||||
|
||||
#endif /* LV_USE_FONT_MANAGER */
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* @file lv_font_manager_recycle.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_FONT_MANAGER_RECYCLE_H
|
||||
#define LV_FONT_MANAGER_RECYCLE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
#include "lv_font_manager_utils.h"
|
||||
|
||||
#if LV_USE_FONT_MANAGER
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
typedef struct _lv_font_manager_recycle_t lv_font_manager_recycle_t;
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Create font recycle manager.
|
||||
* @param max_size recycle size.
|
||||
* @return pointer to font recycle manager.
|
||||
*/
|
||||
lv_font_manager_recycle_t * lv_font_manager_recycle_create(uint32_t max_size);
|
||||
|
||||
/**
|
||||
* Delete font recycle manager.
|
||||
* @param manager pointer to font recycle manager.
|
||||
*/
|
||||
void lv_font_manager_recycle_delete(lv_font_manager_recycle_t * manager);
|
||||
|
||||
/**
|
||||
* Get a reusable font.
|
||||
* @param manager pointer to font recycle manager.
|
||||
* @param ft_info font info.
|
||||
* @return returns true on success.
|
||||
*/
|
||||
lv_font_t * lv_font_manager_recycle_get_reuse(lv_font_manager_recycle_t * manager, const lv_freetype_info_t * ft_info);
|
||||
|
||||
/**
|
||||
* Set fonts to be reused.
|
||||
* @param manager pointer to font recycle manager.
|
||||
* @param ft_info font info.
|
||||
*/
|
||||
void lv_font_manager_recycle_set_reuse(lv_font_manager_recycle_t * manager, lv_font_t * font,
|
||||
const lv_freetype_info_t * ft_info);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#endif /* LV_USE_FONT_MANAGER */
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /* LV_FONT_MANAGER_RECYCLE_H */
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file lv_font_manager_utils.c
|
||||
*
|
||||
*/
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
#include "lv_font_manager_utils.h"
|
||||
|
||||
#if LV_USE_FONT_MANAGER
|
||||
#include "../../misc/lv_assert.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* STATIC PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* STATIC VARIABLES
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL FUNCTIONS
|
||||
**********************/
|
||||
|
||||
bool lv_freetype_info_is_equal(const lv_freetype_info_t * ft_info_1, const lv_freetype_info_t * ft_info_2)
|
||||
{
|
||||
LV_ASSERT_NULL(ft_info_1);
|
||||
LV_ASSERT_NULL(ft_info_2);
|
||||
|
||||
bool is_equal = (ft_info_1->size == ft_info_2->size
|
||||
&& ft_info_1->style == ft_info_2->style
|
||||
&& ft_info_1->render_mode == ft_info_2->render_mode
|
||||
&& lv_strcmp(ft_info_1->name, ft_info_2->name) == 0);
|
||||
|
||||
return is_equal;
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
#endif /* LV_USE_FONT_MANAGER */
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* @file lv_font_manager_utils.h
|
||||
*
|
||||
*/
|
||||
#ifndef LV_FONT_MANAGER_UTILS_H
|
||||
#define LV_FONT_MANAGER_UTILS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
#include "../../misc/lv_types.h"
|
||||
|
||||
#if LV_USE_FONT_MANAGER
|
||||
|
||||
#include "../../libs/freetype/lv_freetype.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
typedef struct {
|
||||
const char * name;
|
||||
lv_freetype_font_render_mode_t render_mode;
|
||||
lv_freetype_font_style_t style;
|
||||
uint32_t size;
|
||||
} lv_freetype_info_t;
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Compare font information.
|
||||
* @param ft_info_1 font information 1.
|
||||
* @param ft_info_2 font information 2.
|
||||
* @return return true if the fonts are equal.
|
||||
*/
|
||||
bool lv_freetype_info_is_equal(const lv_freetype_info_t * ft_info_1, const lv_freetype_info_t * ft_info_2);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#endif /*LV_USE_FONT_MANAGER*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /* LV_FONT_MANAGER_UTILS_H */
|
||||
@@ -262,6 +262,21 @@ char * lv_strncat(char * dst, const char * src, size_t src_len)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
char * lv_strchr(const char * s, int c)
|
||||
{
|
||||
for(; ; s++) {
|
||||
if(*s == c) {
|
||||
return (char *)s;
|
||||
}
|
||||
|
||||
if(*s == '\0') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
@@ -107,6 +107,11 @@ char * lv_strncat(char * dst, const char * src, size_t src_len)
|
||||
return strncat(dst, src, src_len);
|
||||
}
|
||||
|
||||
char * lv_strchr(const char * str, int c)
|
||||
{
|
||||
return strchr(str, c);
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
@@ -147,6 +147,14 @@ char * lv_strcat(char * dst, const char * src);
|
||||
*/
|
||||
char * lv_strncat(char * dst, const char * src, size_t src_len);
|
||||
|
||||
/**
|
||||
* @brief Searches for the first occurrence of character c in the string str.
|
||||
* @param str Pointer to the null-terminated byte string to be searched.
|
||||
* @param c The character to be searched for.
|
||||
* @return A pointer to the first occurrence of character c in the string str, or a null pointer if c is not found.
|
||||
*/
|
||||
char * lv_strchr(const char * str, int c);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
@@ -116,6 +116,21 @@ char * lv_strncat(char * dst, const char * src, size_t src_len)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
char * lv_strchr(const char * s, int c)
|
||||
{
|
||||
for(; ; s++) {
|
||||
if(*s == c) {
|
||||
return (char *)s;
|
||||
}
|
||||
|
||||
if(*s == '\0') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
Reference in New Issue
Block a user