高通字库
版本 V0.1 · 更新于 2024-10-15

三、LVGL 使用字库

1. 定义字体变量

首先定义一个文字数据存储空间,可以根据实际使用情况调整大小,但不能小于单个文字的大小。

1.1 点阵字库 — LVGL V8.4

芯片:GT30L24A3W

static unsigned char glyph_bitmap[1024] = {0}; /* 根据实际使用更改大小 */

#if LV_VERSION_CHECK(8, 0, 0)
/* 存储所有自定义字体数据 */
static lv_font_fmt_txt_glyph_cache_t cache;
static const lv_font_fmt_txt_dsc_t font_dsc = {
#else
static lv_font_fmt_txt_dsc_t font_dsc = {
#endif
    .glyph_bitmap = glyph_bitmap,     // 数据存储数组,最小为一个文字大小
    .glyph_dsc = NULL,                // glyph_dsc
    .bpp = 1,                         // 每像素比特数:1
    .cmaps = NULL,
    .kern_dsc = NULL,
    .kern_scale = 0,
    .cmap_num = 0,
    .kern_classes = 0,
    .bitmap_format = 0,
#if LV_VERSION_CHECK(8, 0, 0)
    .cache = &cache
#endif
};

#if LV_VERSION_CHECK(8, 0, 0)
const lv_font_t gt30l24a3w_montserrat_16 = {
#else
lv_font_t gt30l24a3w_montserrat_16 = {
#endif
    .get_glyph_dsc = _get_gt_font_glyph_dsc_16,         // 获取字符信息的函数指针
    .get_glyph_bitmap = _get_gt_font_bitmap_fmt_txt_16, // 获取字符位图的函数指针
    .line_height = 16,          // 字体需要的最大行高
    .base_line = 0,             // 基线(从行底测量)
#if !(LVGL_VERSION_MAJOR == 6 && LVGL_VERSION_MINOR == 0)
    .subpx = LV_FONT_SUBPX_NONE,
#endif
#if LV_VERSION_CHECK(7, 4, 0) || LVGL_VERSION_MAJOR >= 8
    .underline_position = -1,
    .underline_thickness = 1,
#endif
    .dsc = &font_dsc            // 自定义字体数据,供 get_glyph_bitmap/dsc 访问
};

1.2 点阵字库 — LVGL V9.3

芯片:GT30L24A3W

static unsigned char glyph_bitmap[130] = {0};

static const lv_font_fmt_txt_dsc_t font_dsc = {
    .glyph_bitmap = glyph_bitmap,     // 数据存储数组
    .glyph_dsc = NULL,                // glyph_dsc
    .bpp = 1,                         // 每像素比特数:1
    .cmaps = NULL,
    .kern_dsc = NULL,
    .kern_scale = 0,
    .cmap_num = 0,
    .kern_classes = 0,
    .bitmap_format = 0,
};

const lv_font_t gt30l24a3w_montserrat_16 = {
    .get_glyph_dsc = _get_gt_font_glyph_dsc_16,
    .get_glyph_bitmap = _get_gt_font_bitmap_fmt_txt_16,
    .line_height = 16,
    .base_line = 0,
    .subpx = LV_FONT_SUBPX_NONE,
    .underline_position = -1,
    .underline_thickness = 1,
    .dsc = &font_dsc
};

视频教程:LVGL 移植高通点阵字库


1.3 灰度字库 — LVGL V8.4

芯片:GT5HL24A2W,bpp = 4(4 位灰度)

static unsigned char glyph_bitmap[130] = {0};

#if LV_VERSION_CHECK(8, 0, 0)
static lv_font_fmt_txt_glyph_cache_t cache;
static const lv_font_fmt_txt_dsc_t font_dsc = {
#else
static lv_font_fmt_txt_dsc_t font_dsc = {
#endif
    .glyph_bitmap = glyph_bitmap,
    .glyph_dsc = NULL,
    .bpp = 4,                         // 灰度字库:每像素 4 比特
    .cmaps = NULL,
    .kern_dsc = NULL,
    .kern_scale = 0,
    .cmap_num = 0,
    .kern_classes = 0,
    .bitmap_format = 0,
#if LV_VERSION_CHECK(8, 0, 0)
    .cache = &cache
#endif
};

#if LV_VERSION_CHECK(8, 0, 0)
const lv_font_t gt5hl24a2w_montserrat_16 = {
#else
lv_font_t gt5hl24a2w_montserrat_16 = {
#endif
    .get_glyph_dsc = _get_gt_font_glyph_dsc_16,
    .get_glyph_bitmap = _get_gt_font_bitmap_fmt_txt_16,
    .line_height = 16,
    .base_line = 0,
#if !(LVGL_VERSION_MAJOR == 6 && LVGL_VERSION_MINOR == 0)
    .subpx = LV_FONT_SUBPX_NONE,
#endif
#if LV_VERSION_CHECK(7, 4, 0) || LVGL_VERSION_MAJOR >= 8
    .underline_position = -1,
    .underline_thickness = 1,
#endif
    .dsc = &font_dsc
};

1.4 灰度字库 — LVGL V9.3

芯片:GT5HL24A2W,bpp = 4

static unsigned char glyph_bitmap[130] = {0};

static const lv_font_fmt_txt_dsc_t font_dsc = {
    .glyph_bitmap = glyph_bitmap,
    .glyph_dsc = NULL,
    .bpp = 4,                         // 灰度字库:每像素 4 比特
    .cmaps = NULL,
    .kern_dsc = NULL,
    .kern_scale = 0,
    .cmap_num = 0,
    .kern_classes = 0,
    .bitmap_format = 0,
};

const lv_font_t gt5hl24a2w_montserrat_16 = {
    .get_glyph_dsc = _get_gt_font_glyph_dsc_16,
    .get_glyph_bitmap = _get_gt_font_bitmap_fmt_txt_16,
    .line_height = 16,
    .base_line = 0,
    .subpx = LV_FONT_SUBPX_NONE,
    .underline_position = -1,
    .underline_thickness = 1,
    .dsc = &font_dsc
};

视频教程:LVGL 移植高通灰度字库


1.5 矢量字库 — LVGL V8.4

芯片:GT5SLAD3BFA,字号 24

static unsigned char glyph_bitmap[130] = {0};

#if LV_VERSION_CHECK(8, 0, 0)
static lv_font_fmt_txt_glyph_cache_t cache;          // 缓存最后使用的字符编码和字形 ID
static const lv_font_fmt_txt_dsc_t font_dsc = {       // 存储字体的所有自定义数据
#else
static lv_font_fmt_txt_dsc_t font_dsc = {
#endif
    .glyph_bitmap = glyph_bitmap,     // 数据存储数组
    .glyph_dsc = NULL,                // 描述字形
    .bpp = 1,                         // 每像素比特数:1、2、3、4、8
    .cmaps = NULL,                    // lv_font_cmap_fmt_txt_t 变量数组
    .kern_dsc = NULL,                 // 存储字距值
    .kern_scale = 0,                  // 以 12.4 格式缩放 kern 值
    .cmap_num = 0,                    // cmap 表的数量
    .kern_classes = 0,                // kern_dsc 类型
    .bitmap_format = 0,               // lv_font_fmt_txt_bitmap_format_t 位图的存储格式
#if LV_VERSION_CHECK(8, 0, 0)
    .cache = &cache                   // 缓存最后一个字母和字形 id
#endif
};

#if LV_VERSION_CHECK(8, 0, 0)
const lv_font_t gt5slad3bfa_montserrat_24 = {
#else
lv_font_t gt5slad3bfa_montserrat_24 = {
#endif
    .get_glyph_dsc = _get_gt_font_glyph_dsc_24,         // 指向获取字符信息的函数
    .get_glyph_bitmap = _get_gt_font_bitmap_fmt_txt_24, // 指向获取字符数据的函数
    .line_height = 24,               // 字体需要的最大行高
    .base_line = 0,                  // 基线(从行底测量)
#if !(LVGL_VERSION_MAJOR == 6 && LVGL_VERSION_MINOR == 0)
    .subpx = LV_FONT_SUBPX_NONE,     // lv_font_subpx_t 中的一个元素
#endif
#if LV_VERSION_CHECK(7, 4, 0) || LVGL_VERSION_MAJOR >= 8
    .underline_position = -1,        // 下划线顶部与基线之间的距离(<0 表示基线以下)
    .underline_thickness = 1,        // 下划线的厚度
#endif
    .dsc = &font_dsc                 // 存储的字符数据
};

视频教程:LVGL 移植高通矢量字库