|
板凳
楼主 |
发表于 2022-1-4 16:44:02
|
只看该作者
本帖最后由 feihongkld 于 2022-1-4 16:45 编辑
为了方便大家修改成8.0一上版本可以使用,贴出修改代码: 测试显示OK 我的版本是8.11
static lv_font_fmt_txt_glyph_cache_t glyph_cache = {
.last_letter = 0x9fa0,
.last_glyph_id = 6858,
};
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,
};
int binsearch(const uint16_t *sortedSeq, int seqLength, uint16_t keyData) {
int low = 0, mid, high = seqLength - 1;
while (low <= high) {
mid = (low + high)>>1;//右移1位等于是/2,奇数,无论奇偶,有个值就行
if (keyData < sortedSeq[mid]) {
high = mid - 1;//是mid-1,因为mid已经比较过了
}
else if (keyData > sortedSeq[mid]) {
low = mid + 1;
}
else {
return mid;
}
}
return -1;
}
const uint8_t * __user_font_get_bitmap(const lv_font_t * font, uint32_t unicode_letter) {
lv_font_fmt_txt_dsc_t * fdsc = (lv_font_fmt_txt_dsc_t *) font->dsc;
if( unicode_letter<fdsc->cmaps[0].range_start || unicode_letter>fdsc->cmaps[0].range_length ) return false;
int i;
if( unicode_letter==fdsc->cache->last_letter ){
i = fdsc->cache->last_glyph_id;
}
else{
i = binsearch(fdsc->cmaps[0].unicode_list, fdsc->cmaps[0].list_length, unicode_letter);
}
if( i != -1 ) {
const lv_font_fmt_txt_glyph_dsc_t * gdsc = &fdsc->glyph_dsc;
fdsc->cache->last_glyph_id = i;
fdsc->cache->last_letter = unicode_letter;
return &fdsc->glyph_bitmap[gdsc->bitmap_index];
}
return NULL;
}
static bool __user_font_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, uint32_t unicode_letter_next) {
lv_font_fmt_txt_dsc_t * fdsc = (lv_font_fmt_txt_dsc_t *) font->dsc;
if( unicode_letter<fdsc->cmaps[0].range_start || unicode_letter>fdsc->cmaps[0].range_length ) return false;
int i;
if( unicode_letter==fdsc->cache->last_letter ){
i = fdsc->cache->last_glyph_id;
}
else{
i = binsearch(fdsc->cmaps[0].unicode_list, fdsc->cmaps[0].list_length, unicode_letter);
}
if( i != -1 ) {
const lv_font_fmt_txt_glyph_dsc_t * gdsc = &fdsc->glyph_dsc;
fdsc->cache->last_glyph_id = i;
fdsc->cache->last_letter = unicode_letter;
dsc_out->adv_w = gdsc->adv_w;
dsc_out->box_h = gdsc->box_h;
dsc_out->box_w = gdsc->box_w;
dsc_out->ofs_x = gdsc->ofs_x;
dsc_out->ofs_y = gdsc->ofs_y;
dsc_out->bpp = fdsc->bpp;
return true;
}
return false;
}
//Microsoft JhengHei,,-1
//字模高度:19
//内部字体
//使用排序和二分查表
const lv_font_t myFont1 = {
.dsc = &font_dsc,
.get_glyph_bitmap = __user_font_get_bitmap,
.get_glyph_dsc = __user_font_get_glyph_dsc,
.line_height = 19,
.base_line = 0,
};
测试代码:
void lvgl_test(void)
{
// lv_style_init(&style_label);
// lv_style_set_bg_opa(&style_label, LV_OPA_0);
// lv_style_set_text_font(&style_label, &myFont1);
lv_obj_t *scr = lv_disp_get_scr_act(NULL);/* 获取当前屏幕 */
lv_scr_load(scr);
lv_obj_t *label1 = lv_label_create(scr); /* 创建 label 控件 */
lv_obj_set_pos(label1,0,0); /* 设置控件的坐标 */
lv_label_set_text(label1,"Hello欢迎到来"); /* 设置文字 */
lv_obj_align(label1,LV_ALIGN_CENTER,0,0); /* 设置控件的对*/
}
注意需要修改lv_conf.h
/*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 LV_FONT_DECLARE(myFont1)
/*Always set a default font*/
#define LV_FONT_DEFAULT &myFont1
|
|