在ESP32的Lvgl8上使用LvglFontTool显示汉字

一、使用LvglFontTool4.0转换字体

这个工具是阿里兄大佬提供的,他的论坛下载地址
不过由于lvgl8的字体结构体变了一下,最新4.0转换出内部字体的话,需要对生成的c文修改一下才能用
在这里插入图片描述
输入要用到的字,或者直接选加入常用汉字,就看flash够不够大了。
然后选择字体。由于我win10权限的关系,我windows目录里面看不到Font文件夹,需要到控制面板——字体打开Font文件夹,然后将需要的字体文件拷贝到其他地方,才能选择字体
在这里插入图片描述
最后点开始转换,就能生成c文件,存到电脑本地目录

二、修改生成的C文件

因为lvgl8的lv_font_fmt_txt_dsc_t结构体将原来的last_letter和last_glyph_id放到了新的成员cache里面了,所以需要对应改过来

/*Describe store additional data for fonts*/
typedef struct {
    
    
    /*The bitmaps of all glyphs*/
    const uint8_t * glyph_bitmap;
    /*Describe the glyphs*/
    const lv_font_fmt_txt_glyph_dsc_t * glyph_dsc;
    /*Map the glyphs to Unicode characters.
     *Array of `lv_font_cmap_fmt_txt_t` variables*/
    const lv_font_fmt_txt_cmap_t * cmaps;
    /**
     * Store kerning values.
     * Can be  `lv_font_fmt_txt_kern_pair_t *  or `lv_font_kern_classes_fmt_txt_t *`
     * depending on `kern_classes`
     */
    const void * kern_dsc;
    /*Scale kern values in 12.4 format*/
    uint16_t kern_scale;
    /*Number of cmap tables*/
    uint16_t cmap_num       : 9;
    /*Bit per pixel: 1, 2, 3, 4, 8*/
    uint16_t bpp            : 4;
    /*Type of `kern_dsc`*/
    uint16_t kern_classes   : 1;
    /*
     * storage format of the bitmap
     * from `lv_font_fmt_txt_bitmap_format_t`
     */
    uint16_t bitmap_format  : 2;
    /*Cache the last letter and is glyph id*/
    lv_font_fmt_txt_glyph_cache_t * cache;
} lv_font_fmt_txt_dsc_t;

新建一个lv_font_fmt_txt_glyph_cache_t

static lv_font_fmt_txt_glyph_cache_t glyph_cache = {
    
    
    .last_letter = 0x6c14,
    .last_glyph_id = 98,
};

将原来的font_dsc里面的last_letter和last_glyph_id删掉,放上新的cache

static lv_font_fmt_txt_dsc_t font_dsc = {
    
    
    .glyph_bitmap = glyph_bitmap,
    .glyph_dsc = glyph_dsc,
    .cmaps = cmaps,
    .cmap_num = 1,
    .bpp = 4,

    .kern_scale = 0,
    .kern_dsc = NULL,
    .kern_classes = 0,

    .cache = &glyph_cache
};

最后将下面所有的fdsc结构体的引用改为fdsc->cache->last_letter和fdsc->cache->last_glyph_id,这样编译就不会报错了

 if (unicode_letter == fdsc->cache->last_letter)
    {
    
    
        i = fdsc->cache->last_glyph_id;
    }
    else
    {
    
    

二、使用字体

用法1:在canvas上绘制

LV_FONT_DECLARE(myFont) //声明自定义字体

void test_canvas_font(void)
{
    
    
   //创建画布
   static lv_color_t cbuf[LV_CANVAS_BUF_SIZE_TRUE_COLOR_ALPHA(128, 160)];
   static lv_obj_t *canvas;
   canvas = lv_canvas_create(lv_scr_act());
   lv_canvas_set_buffer(canvas, cbuf, 128, 160, LV_IMG_CF_TRUE_COLOR_ALPHA);
   lv_obj_set_align(canvas, LV_ALIGN_CENTER);
   // 绘制中文字体
   lv_draw_label_dsc_t label_dsc;
   lv_draw_label_dsc_init(&label_dsc);
   label_dsc.color = lv_palette_main(LV_PALETTE_RED);
   label_dsc.font = &myFont;	//设置自定义字体
   lv_canvas_draw_text(canvas, 0, 0, 160, &label_dsc, "天气:晴(Happy!)");
}

效果
在这里插入图片描述

用法2:在label上显示

LV_FONT_DECLARE(myFont) //声明自定义字体

void test_label_font(void)
{
    
    
   // 创建字体样式
   static lv_style_t style_font;
   lv_style_init(&style_font);
   lv_style_set_text_font(&style_font,  &myFont);  //样式使用自定义字体
   lv_style_set_text_color(&style_font, lv_palette_main(LV_PALETTE_LIGHT_GREEN));   //设置字体颜色
   //创建标签
   lv_obj_t *label = lv_label_create(lv_scr_act());
   lv_label_set_text(label, "天气:晴(label)");  //设置标签文字
   lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
   lv_obj_add_style(label, &style_font, LV_STATE_DEFAULT);  //应用字体样式到标签
   lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, -20);
}

猜你喜欢

转载自blog.csdn.net/flamebox/article/details/122202194