用Guiguider生成的字体代替LVGL默认字体

Guiguider版本:1.8.1

LVGL版本:8.3

    最近使用LVGL时,由于flash不太够用了,观察编译的map图发现一直有一个LV_FONT_MONTSERRAT_14的字体被编译,占用了我十几KB的Flash,由于我是用guider生成的界面和字体,我寻思我也没在guider中使用LV_FONT_MONTSERRAT_14啊,后面在lv_conf.h中发现了原因,在LVGL中,其必须要有一个默认的字体,默认字体可以从下面通过宏定义来修改,而这里默认的字体就是LV_FONT_MONTSERRAT_14。

lv_conf.h:
/*==================
 *   FONT USAGE
 *===================*/

/*Montserrat fonts with ASCII range and some symbols using bpp = 4
 *https://fonts.google.com/specimen/Montserrat*/
#define LV_FONT_MONTSERRAT_8  0
#define LV_FONT_MONTSERRAT_10 0
#define LV_FONT_MONTSERRAT_12 0
#define LV_FONT_MONTSERRAT_14 1
#define LV_FONT_MONTSERRAT_16 0
#define LV_FONT_MONTSERRAT_18 0
#define LV_FONT_MONTSERRAT_20 0
#define LV_FONT_MONTSERRAT_22 0
#define LV_FONT_MONTSERRAT_24 0
#define LV_FONT_MONTSERRAT_26 0
#define LV_FONT_MONTSERRAT_28 0
#define LV_FONT_MONTSERRAT_30 0
#define LV_FONT_MONTSERRAT_32 0
#define LV_FONT_MONTSERRAT_34 0
#define LV_FONT_MONTSERRAT_36 0
#define LV_FONT_MONTSERRAT_38 0
#define LV_FONT_MONTSERRAT_40 0
#define LV_FONT_MONTSERRAT_42 0
#define LV_FONT_MONTSERRAT_44 0
#define LV_FONT_MONTSERRAT_46 0
#define LV_FONT_MONTSERRAT_48 0

/*Demonstrate special features*/
#define LV_FONT_MONTSERRAT_12_SUBPX      0
#define LV_FONT_MONTSERRAT_28_COMPRESSED 0  /*bpp = 3*/
#define LV_FONT_DEJAVU_16_PERSIAN_HEBREW 0  /*Hebrew, Arabic, Persian letters and all their forms*/
#define LV_FONT_SIMSUN_16_CJK            0  /*1000 most common CJK radicals*/

/*Pixel perfect monospace fonts*/
#define LV_FONT_UNSCII_8  0
#define LV_FONT_UNSCII_16 0

/*Optionally declare custom fonts here.
 *You can use these fonts as default font too and they will be available globally.
 *E.g. #define LV_FONT_CUSTOM_DECLARE   LV_FONT_DECLARE(my_font_1) LV_FONT_DECLARE(my_font_2)*/
#define LV_FONT_CUSTOM_DECLARE

/*Always set a default font*/
#define LV_FONT_DEFAULT &lv_font_montserrat_14


lv_font.h

/**********************
 *      MACROS
 **********************/

#define LV_FONT_DECLARE(font_name) extern const lv_font_t font_name;

#if LV_FONT_MONTSERRAT_8
LV_FONT_DECLARE(lv_font_montserrat_8)
#endif

#if LV_FONT_MONTSERRAT_10
LV_FONT_DECLARE(lv_font_montserrat_10)
#endif

#if LV_FONT_MONTSERRAT_12
LV_FONT_DECLARE(lv_font_montserrat_12)
#endif

#if LV_FONT_MONTSERRAT_14
LV_FONT_DECLARE(lv_font_montserrat_14)
#endif

#if LV_FONT_MONTSERRAT_16
LV_FONT_DECLARE(lv_font_montserrat_16)
#endif

......
/*Declare the custom (user defined) fonts*/
#ifdef LV_FONT_CUSTOM_DECLARE
LV_FONT_CUSTOM_DECLARE
#endif

    那如何将默认字体设置为我guiguider生成的字体呢? 我们看到这里有一个宏定义LV_FONT_CUSTOM_DECLARE,还有一个LV_FONT_DECLARE(lv_font_montserrat_16)宏表示将lv_font_montserrat_16字体外部声明;因此我们只需要在lv_font.h中定义自己的字体声明即可。

lv_font.h:
/*Declare the custom (user defined) fonts*/
#ifdef LV_FONT_CUSTOM_DECLARE
LV_FONT_CUSTOM_DECLARE
#endif

     而其实我们在Guiguider生成的文件gui_guider.h的末尾也能看到一样的声明,如下:

gui_guider.h:
......

void setup_scr_Screen_Init(lv_ui *ui);
void setup_scr_Screen_Main(lv_ui *ui);
void setup_scr_Screen_Menu(lv_ui *ui);
void setup_scr_Screen_Calibration_1(lv_ui *ui);
void setup_scr_Screen_Calibration_2(lv_ui *ui);
void setup_scr_Screen_Calibration_3(lv_ui *ui);
void setup_scr_Screen_Calibration_4(lv_ui *ui);
void setup_scr_Screen_Calibration_5(lv_ui *ui);
LV_IMG_DECLARE(_8a9c224234acb1e44bbcd0dc94acba3_alpha_320x55);

LV_FONT_DECLARE(lv_font_SourceHanSerifSC_Regular_16)
LV_FONT_DECLARE(lv_font_SourceHanSerifSC_Regular_40)

    这里我把 lv_font_SourceHanSerifSC_Regular_16作为LVGL的默认字体,因此在lv_font.h中修改宏定义为如下即可:其实声明其他外部字体为lvgl默认字体的步骤也都类似,不过要注意的是外部字体的.c文件中字库的格式要和lvgl自带的字体.c文件字库格式一样,用guiguider生成的字体字库格式一般都符合LVGL要求,不用太担心。

    修改后就发现编译的map图中没有把先前的LV_FONT_MONTSERRAT_14字库编译进去了,Flash占用也少了十几KB;

lv_font.h:
/*Declare the custom (user defined) fonts*/
#ifdef LV_FONT_CUSTOM_DECLARE
LV_FONT_DECLARE(lv_font_SourceHanSerifSC_Regular_16)
#endif

/**************补充:关于guiguider生成字体后,使用时部分文字被方框替代问题***************/

     我们需要知道的是,当我们使用guiguider生成字体时,例如lv_font_SourceHanSerifSC_Regular_16字体,我们知道这个字体是能显示中文的。但是其.c字库文件中只有我们在guiguider中使用的中文,例如我在guiguider中设计UI时使用了“我”、“你”、“他”,那么生成的lv_font_SourceHanSerifSC_Regular_16字库中就只有上面三个中文,如果此时我在写程序时想显示“你好”,此时由于“好”字不在lv_font_SourceHanSerifSC_Regular_16字库中,因此我们就会发现其被被方框替代。

    解决办法:在guiguider中新建一个屏幕,在其中创建一个label控件,填入我们在程序中要使用的所有中文,然后加入编译即可,我们不使用该屏幕,仅仅是用于guiguider生成字体。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值