日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

littlevgl抗锯齿_「VGL」littlevGL:字体与汉字 - seo实验室

發布時間:2024/4/11 编程问答 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 littlevgl抗锯齿_「VGL」littlevGL:字体与汉字 - seo实验室 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

VGL

使用各種嵌入式GUI時,總會遇到“漢字顯示”“字體”這些關卡。

閱讀本文前,最好已經了解Uincode,UTF-8,UTF-16,GBK,gb2312相關知識,不懂最好網絡搜索相關知識。

1.內置字體

littlevGL內置了好幾種字體。在lv_conf.h中開關相關字體

/*==================

* FONT USAGE

*===================*/

/* More info about fonts: https://littlevgl.com/basics#fonts

* To enable a built-in font use 1,2,4 or 8 values

* which will determine the bit-per-pixel */

#define USE_LV_FONT_DEJAVU_10 0

#define USE_LV_FONT_DEJAVU_10_LATIN_SUP 0

#define USE_LV_FONT_DEJAVU_10_CYRILLIC 0

#define USE_LV_FONT_symbol_10 0

#define USE_LV_FONT_DEJAVU_20 4

#define USE_LV_FONT_DEJAVU_20_LATIN_SUP 0

#define USE_LV_FONT_DEJAVU_20_CYRILLIC 0

#define USE_LV_FONT_SYMBOL_20 4

#define USE_LV_FONT_DEJAVU_30 0

#define USE_LV_FONT_DEJAVU_30_LATIN_SUP 0

#define USE_LV_FONT_DEJAVU_30_CYRILLIC 0

#define USE_LV_FONT_SYMBOL_30 0

#define USE_LV_FONT_DEJAVU_40 0

#define USE_LV_FONT_DEJAVU_40_LATIN_SUP 0

#define USE_LV_FONT_DEJAVU_40_CYRILLIC 0

#define USE_LV_FONT_SYMBOL_40 0

#define USE_LV_FONT_MONOSPACE_8 0

其中0代表不使用,1,2,4,8使能并設置抗鋸齒值。

字體文件在lv_fonts文件夾下。

littlevGL支持UTF-8。在lv_conf.h中開啟UTF8

/*Text settings*/

#define LV_TXT_UTF8 1 /*Enable UTF-8 coded unicode character usage */

2.自定義字體

littlevGL支持自定義字體。

官方字體生成網站:https://littlevgl.com/ttf-font-to-c-array

不知為何作者沒有發布離線字體生成工具。我打算過段時間有空就自己用C#寫一個。

生成的字體文件類似于lv_fonts文件夾下面的各種字體文件,使用方式也一樣。

使用之前需要把源文件轉為UTF8格式。

//myfont是自定義的字體

void lv_tutorial_fonts(void)

{

static lv_style_t style1;

char *str="我的祖國\nis 好好的!";

/*Create a style and use the new font*/

lv_style_copy(&style1, &lv_style_plain);

style1.text.font = &myfont;

/*Create a label and set new text*/

lv_obj_t * label = lv_label_create(lv_scr_act(), NULL);

lv_obj_set_pos(label, 100, 10);

lv_label_set_style(label, &style1);

lv_label_set_text(label, str );

}

3.外置字體

上面的方法只適合字體文件比較小的情況,要是字體太大,放不進MCU,那就需要更進一步。

對源文件的顯示字體源碼分析后,發現關鍵在于字體結構

typedef struct _lv_font_struct

{

uint32_t unicode_first;

uint32_t unicode_last;

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); /*Get a glyph's bitmap from a font*/

int16_t (*get_width)(const struct _lv_font_struct *,uint32_t); /*Get a glyph's with with a given font*/

struct _lv_font_struct * next_page; /*Pointer to a font extension*/

uint32_t h_px :8;

uint32_t bpp :4; /*Bit per pixel: 1, 2 or 4*/

uint32_t monospace :8; /*Fix width (0: normal width)*/

uint16_t glyph_cnt; /*Number of glyphs (letters) in the font*/

} lv_font_t;

打開一個字體文件,其中字體定義

lv_font_t lv_font_dejavu_20 = {

.unicode_first = 32, /*First Unicode letter in this font*/

.unicode_last = 126, /*Last Unicode letter in this font*/

.h_px = 20, /*Font height in pixels*/

.glyph_bitmap = lv_font_dejavu_20_glyph_bitmap, /*Bitmap of glyphs*/

.glyph_dsc = lv_font_dejavu_20_glyph_dsc, /*Description of glyphs*/

.glyph_cnt = 95, /*Number of glyphs in the font*/

.unicode_list = NULL, /*Every character in the font from 'unicode_first' to 'unicode_last'*/

.get_bitmap = lv_font_get_bitmap_continuous, /*Function pointer to get glyph's bitmap*/

.get_width = lv_font_get_width_continuous, /*Function pointer to get glyph's width*/

#if USE_LV_FONT_DEJAVU_20 == 1

.bpp = 1, /*Bit per pixel*/

#elif USE_LV_FONT_DEJAVU_20 == 2

.bpp = 2, /*Bit per pixel*/

#elif USE_LV_FONT_DEJAVU_20 == 4

.bpp = 4, /*Bit per pixel*/

#elif USE_LV_FONT_DEJAVU_20 == 8

.bpp = 8, /*Bit per pixel*/

#endif

.monospace = 0,

.next_page = NULL, /*Pointer to a font extension*/

};

上面代碼,lv_font_get_bitmap_continuous()函數為字體點陣獲取函數,返回的是對應字符在點陣數組的位置。

另外還有些字體使用的是lv_font_get_bitmap_sparse();這兩個函數功能是一樣的。

由此,我們可以仿照此函數,想辦法從外部存儲器獲得某個字符的點陣數據,并且保存在一個靜態數組里,最后返回此數組首地址即可。

要完成這些,前提是1.把字符的點陣數組轉化為BIN文件(去原子的論壇搜索下載C2B 1.1版本,2.0版本有BUG);2.把此文件弄到外置存儲器。

對于第2點。有很多方法。比如通過寫一個PC串口助手把文件發送燒寫進SPI flash(W25QXX之類);或者直接使用專門的燒錄工具寫進SPI flash;或者偷懶,直接放在SD卡里讓MCU調用。

這里我使用偷懶的方法,把字體文件放到SD卡里,以文件的形式讀取數據。必須要說明,此方法頻繁地進行文件操作,速度不咋地(最好把堆棧調大些,否則可能溢出)。

內置的字體點陣獲取函數

/**

* Generic bitmap get function used in 'font->get_bitmap' when the font NOT contains all characters in the range (sparse)

* @param font pointer to font

* @param unicode_letter an unicode letter which bitmap should be get

* @return pointer to the bitmap or NULL if not found

*/

const uint8_t * lv_font_get_bitmap_sparse(const lv_font_t * font, uint32_t unicode_letter)

{

/*Check the range*/

if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL;

uint32_t i;

for(i = 0; font->unicode_list[i] != 0; i++) {

if(font->unicode_list[i] == unicode_letter) {

return &font->glyph_bitmap[font->glyph_dsc[i].glyph_index];

}

}

return NULL;

}

仿照上面,自定義的函數

//look above

const uint8_t * ex_lv_font_get_bitmap_sparse(const lv_font_t * font, uint32_t unicode_letter)

{

uint8_t * pval=NULL;

uint32_t i;

/*Check the range*/

if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last){ return NULL;}

for(i = 0; font->unicode_list[i] != 0; i++)

{

if(font->unicode_list[i] == unicode_letter)

{

FIL Binfile;

FRESULT res;

uint32_t br,fsize;

i=font->glyph_dsc[i].glyph_index;

res=f_open(&Binfile, (const TCHAR*)font->glyph_bitmap ,FA_OPEN_EXISTING|FA_READ);

if(res != FR_OK) { return NULL;}

fsize = Binfile.fsize ;

if(i+LetterSIZE <= fsize)

{

f_lseek(&Binfile,i );

res = f_read(&Binfile, letterBuff ,LetterSIZE ,&br);

if( res == FR_OK && ( br == LetterSIZE || br == LetterSIZE/2 ) || br==0 )

{

pval=letterBuff;

}

}

f_close(&Binfile);

return pval;

}

}

return NULL;

}

然后要把字體的點陣數組刪除,在字體定義里修改.glyph_bitmap和.glyph_bitmap成員。如下

lv_font_t myfont =

{

.unicode_first = 32, /*First Unicode letter in this font*/

.unicode_last = 40664, /*First Unicode letter in this font*/

.h_px = 37, /*Font height in pixels*/

.glyph_bitmap = "0:/FONT/HZ1A.bin", /*Bitmap of glyphs*/

.glyph_dsc = glyph_dsc, /*Description of glyphs*/

.unicode_list = unicode_list, /*List of unicode characters*/

.get_bitmap = ex_lv_font_get_bitmap_sparse, /*Function pointer to get glyph's bitmap*/

.get_width = lv_font_get_width_sparse, /*Function pointer to get glyph's width*/

.bpp = 1, /*Bit per pixel*/

.next_page = NULL, /*Pointer to a font extension*/

};

"0:/FONT/HZ1A.bin"是字體文件路徑。

再次說明,此方法不應該在實際項目中使用,效率不好。建議把字體文件按地址燒錄進SPI flash,需要的時候再按照地址直接讀取。上面的".glyph_bitmap"可賦值為該地址。原理都是一樣的。

相關閱讀

一、Qt環境設置文件從window上傳到Ubuntu后會顯示亂碼,原因是因為ubuntu環境設置默認是utf-8,Windows默認都是GBK.Windows環境下,

免費輕松識別手寫漢字工具-python話不多說,先上效果圖!開發過程小工具打包為.exe程序了。[下載連接](https://download.csdn.net/do

首先,什么是Unicode,百科知識:Unicode(統一碼、萬國碼、單一碼)是計算機科學領域里的一項業界標準,包括字符集、編碼方案等;Unicode

背景:

對接第三方接口需要傳個age和ageTyoe字段,其中age字段為integer型,ageType取“歲/月/天”,但是我們系統存的年齡格式為N歲M月,

1.

/**

*PHP漢字轉拼音

*@authorJerryli(hzjerry@gmail.com)

*@versionV0.20140715

*@packageSPFW.core.lib.final

*@globa

總結

以上是生活随笔為你收集整理的littlevgl抗锯齿_「VGL」littlevGL:字体与汉字 - seo实验室的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。