目录

LVGL第三方库的使用中文库

LVGL第三方库的使用(中文库)

一、第三方库文档

https://i-blog.csdnimg.cn/direct/95acfa8f95e64353b005c85df2a89791.png

FreeType 中文字库

SDL 模拟器使用freetype中文字库

https://i-blog.csdnimg.cn/direct/8ad4560762154dc2b11595e308819063.png

1.开启字库

https://i-blog.csdnimg.cn/direct/7d0753ee870a4039997c6324a629b5b1.png

2.安装freetype 字库

sudo apt-get update sudo apt-get install libfreetype6-dev

3.修改makefile 添加字库

https://i-blog.csdnimg.cn/direct/909508a0a3e547fa8cbd6224f1a9bff2.png

4.显示中文字体

修改TTF字体文件

https://i-blog.csdnimg.cn/direct/d9a9264763484fddb8a18c84b78f8770.png

把中文字体拷贝到LVGL工程中

https://i-blog.csdnimg.cn/direct/52a244cbd363492ab4b9789ef78282de.png

 //修改为自己的中文字体 .ttf文件 👉./lvgl/examples/libs/freetype/simhei.ttf
  lv_font_t * font = lv_freetype_font_create("./lvgl/examples/libs/freetype/simhei.ttf",
                                               LV_FREETYPE_FONT_RENDER_MODE_BITMAP, 24, LV_FREETYPE_FONT_STYLE_NORMAL);
/**
 * Create a freetype font.
 * @param pathname font file path.
 * @param render_mode font render mode(see @lv_freetype_font_render_mode_t for details).
 * @param size font size.
 * @param style font style(see lv_freetype_font_style_t for details).
 * @return Created font, or NULL on failure.
 */
lv_font_t * lv_freetype_font_create(const char * pathname, lv_freetype_font_render_mode_t render_mode, uint32_t size,
                                    lv_freetype_font_style_t style);
pathname:字体文件路径 
render_mode :  渲染模式   👉LV_FREETYPE_FONT_RENDER_MODE_BITMAP 位图渲染模式 
        size:字体大小       
       style:字体样式    👉LV_FREETYPE_FONT_STYLE_NORMAL        普通样式   
       返回值: 成功 字体对象指针               
               失败  NULL 

demo例子

 /*Create a font*/
    lv_font_t * font = lv_freetype_font_create("./lvgl/examples/libs/freetype/simhei.ttf",
                                               LV_FREETYPE_FONT_RENDER_MODE_BITMAP, 24, LV_FREETYPE_FONT_STYLE_NORMAL);

    if(!font) {
        LV_LOG_ERROR("freetype font create failed.");
        return;
    }

    /*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_align(&style, LV_TEXT_ALIGN_CENTER); // 设置字体居中

    /*Create a label with the new style*/
    lv_obj_t * label = lv_label_create(lv_screen_active());                    // 创建一个标签
    lv_obj_add_style(label, &style, 0);                                        // 给标签添加字体样式
    lv_label_set_text(label, "我爱中国! I Love China"); // 设置表的内容
    lv_obj_center(label);

二、ARM 使用freetype 字库

1. 下载 freetype 字库源码&交叉编译源码

(有兴趣自己操作一下)

2.或者找一个配置好的库

1.解压交叉编译好的 freetype & zlib 字库到LVGL 工程中
 tar  -xvf   freetype_tmp.tar.gz    -C   ~/lv_port_linux/ 

3.修改Makefile 链接arm 版本的字库

https://i-blog.csdnimg.cn/direct/dc9f8057f1934771bde6fabbba2d6624.png

4.下载到开发板中运行

出现错误

https://i-blog.csdnimg.cn/direct/2443f3fbaf67453eb1673b1bb1097fc7.png

解决方法

把freetype_tmp/lib 中的所有文件下载到开发板中

https://i-blog.csdnimg.cn/direct/baa0e9f264da4eadb146a3044b278c89.png

下载字库文件

https://i-blog.csdnimg.cn/direct/df0c8e4adbfe40bf87dc3ab62b6e8c44.png



出现错误

https://i-blog.csdnimg.cn/direct/808b0a8ee0fc424b9f8704721ea4bad2.png

解决方法

https://i-blog.csdnimg.cn/direct/ed6ed855ca124ce5841c547f00825f64.png

 👉/usr/share/fonts/DroidSansFallback.ttf 开发板默认字库文件,如果不想用该文件自己下载新的字库
  
 lv_font_t * font = lv_freetype_font_create("/usr/share/fonts/DroidSansFallback.ttf",
                                               LV_FREETYPE_FONT_RENDER_MODE_BITMAP, 24, LV_FREETYPE_FONT_STYLE_NORMAL);

至此,希望看完这篇文章的你有所收获,我是Bardb,译音八分贝,道友,下期见!