|
本帖最后由 akatsuki_lim 于 2018-9-20 15:05 编辑
这里介绍一种littlevgl下读取外部中文字库,实现汉字显示的方法。
littlevgl移植方法可百度,这里不多介绍。
////////////////////////////////////////////////////////////////////////////////////
参考官方的实现外语的方法,主要是注册好lv_font_t结构体里面的参数、函数即可。结构体里面主要的内容是字符内码的起始、结束序号,字体高度,字库表,字体描述(字款和字库偏移量),获取字库数据函数,获取字库宽度函数,下一字库指向,抗锯齿bpp。
- typedef struct _lv_font_struct
- {
- uint32_t unicode_first;
- uint32_t unicode_last;
- uint8_t h_px;
- const uint8_t * glyph_bitmap;
- const lv_font_glyph_dsc_t * glyph_dsc;
- const uint32_t * unicode_list;
- const uint8_t * (*get_bitmap)(const struct _lv_font_struct * ,uint32_t); /*获取字库数据函数指针*/
- int16_t (*get_width)(const struct _lv_font_struct * ,uint32_t); /*获取字体宽度函数指针*/
- struct _lv_font_struct * next_page; /*Pointer to a font extension*/
- uint32_t bpp :4; /*Bit per pixel: 1, 2 or 4*/
- }lv_font_t;
复制代码
以工程里面的实现GBK中文16*16大小为例,在lv_font_hz16.c里面填充结构体- lv_font_t lv_font_hz16 =
- {
- .unicode_first = 0x0020, //0xA1A1 /*First Unicode letter in this font*/
- .unicode_last = 0xFFFF, /*Last Unicode letter in this font*/
- .h_px = 16, //一个字符的高度 /*Font height in pixels*/
- //.glyph_bitmap = arial_ascii_20_glyph_bitmap, //字库指针,这里使用的是由外部读取的数据,暂时不注册/*Bitmap of glyphs*/
- //.glyph_dsc = arial_ascii_20_glyph_dsc, //字库描述指针,在这里亦用不上/*Description of glyphs*/
- .unicode_list = NULL, /*Every character in the font from 'unicode_first' to 'unicode_last'*/
- .get_bitmap = lv_font_hz_get_bitmap_continuous, //获取字符表函数指针,这里不使用vlg的函数方法 /*Function pointer to get glyph's bitmap*/
- .get_width = lv_font_hz_get_width_continuous, //获取字符宽度函数指针,这里不使用vlg的函数方法 /*Function pointer to get glyph's width*/
复制代码
其中 函数lv_font_hz_get_bitmap_conuinuous和 函数lv_font_hz_get_width_continuous 实现与ui回调处理的接口
字库为GBK编码,取模的方法可参考:
http://www.armbbs.cn/forum.ph ... 6%BF%E2%C9%FA%B3%C9
////////////////////////////////////////////////////////////////////////////////////
实现了获取字库函数之余,ui的lv_txt.c里面的lv_txt_utf8_next函数还得修改为:
- uint32_t lv_txt_utf8_next(const char * txt, uint32_t * i)
- {
- #if LV_TXT_UTF8 == 0
- if(i == NULL) return txt[1]; /*Get the next char */
- uint8_t letter = txt[*i] ;
- (*i)++;
- return letter;
- #else
- /* Unicode to UTF-8
- * 00000000 00000000 00000000 0xxxxxxx -> 0xxxxxxx
- * 00000000 00000000 00000yyy yyxxxxxx -> 110yyyyy 10xxxxxx
- * 00000000 00000000 zzzzyyyy yyxxxxxx -> 1110zzzz 10yyyyyy 10xxxxxx
- * 00000000 000wwwzz zzzzyyyy yyxxxxxx -> 11110www 10zzzzzz 10yyyyyy 10xxxxxx
- * */
- uint32_t result = 0;
- /*Dummy 'i' pointer is required*/
- uint32_t i_tmp = 0;
- if(i == NULL) i = &i_tmp;
- /*Normal ASCII*/
- if((txt[*i] & 0x80) == 0) {
- result = txt[*i];
- (*i)++;
- }
- /*Real UTF-8 decode*/
- else {
- //GBK
- result = (uint32_t)(txt[*i]) ;
- (*i)++; /*Not UTF-8 char. Go the next.*/
-
- result += (uint32_t)(txt[*i])<< 8;
- (*i)++;
- }
- return result;
- #endif
- }
复制代码 原本作者打算拓展的是utf-8编码的字库,但实际上字库生成器生成的是gbk编码,所以返回内码的时候需要另外做处理。
注:因为我这里是直接删除了utf-8 decode部分,所以ui不能使用utf-8的字符!!!(后续再填补这个bug)
//////////////////////////////////////////////////////////////////////////////////
这里就记录主要修改部分,具体的可参考附上的工程代码。
另外,可在github上看看原作者与网友讨论的内容,对增加中文支持应该会有更好的办法。
https://github.com/littlevgl/lvgl/issues/78
|
评分
-
查看全部评分
|