自制和烧录单片机 unicode 字库芯片 - 创建字库
生活随笔
收集整理的這篇文章主要介紹了
自制和烧录单片机 unicode 字库芯片 - 创建字库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
此詞庫被應用于微信點餐系統的打印機中。
打開 Windows 命令行,進入取下的代碼目錄,執行: c:\Python27\python.exe make_font.py -s 23 -c
會在當前目錄下產生 cn_font.bin 文件,將此文件燒錄到 Flash ROM 中,安裝下面的字庫格式即可讀取。
/*** Get the glyph by its UCS2 character encoding.** If it does not exist, return a special glyph.*/void get_glyph(WCHAR ch, struct Glyph *glyph){u32 pos = FL_OFFSET_GLYPH_TABLE + FONT_GLYPH_INFO_SIZE * (u32) ch;u16 posHigh = FLASH_read_byte(pos);u16 posLow = FLASH_read_byte(pos + 1);glyph->width = FLASH_read_byte(pos + 2);glyph->data_pos = (posHigh << 8) | posLow;}void get_glyph_data(struct FontInfo *fontInfo, struct Glyph *glyph, u8 offset, u8 data[MAX_GLYPH_STRIDE]){u32 FL_OFFSET_GLYPH_DATA = fontInfo->offset + FL_OFFSET_GLYPH_TABLE+ fontInfo->glyph_count * (u32) FONT_GLYPH_INFO_SIZE;u32 addr = FL_OFFSET_GLYPH_DATA + (u32) (glyph->data_pos) * fontInfo->glyph_data_size + offset;FLASH_fast_read(data, addr, get_glyph_stride(glyph->width));}
生成的字庫優點
可以完全自定義創建自己的字庫:- 可自由選擇任何字體,宋體,楷體,黑體……
- 可自定義字體的大小
- 可自定義需要的字符編碼(簡體gb2312,gb18030,繁體,韓文,日文……)
- UNICODE支持
- 價格便宜,買 Flash ROM,自己寫進 Flash ROM就可以了。
創建字體庫需要的軟件
Windows系統,安裝 Python 2.7 和 PIL 庫( Python Image Library),下載 Make ROM Font 工具打開 Windows 命令行,進入取下的代碼目錄,執行: c:\Python27\python.exe make_font.py -s 23 -c
會在當前目錄下產生 cn_font.bin 文件,將此文件燒錄到 Flash ROM 中,安裝下面的字庫格式即可讀取。
- 如果要選擇不同的字體,修改 make_chinese_font 中 font_file 指向對應的字體文件;
- 如果要選擇不同的字符編碼,修改 make_chinese_font 中 get_glyph_set,添加新的編碼;
字庫格式
The format of font.bin: [File Header][Glyphs Table][Glyphs Data]File Header: BF [2 bytes]VersionNumber [1 byte]FontHeight [ 1 byte]FontWidthMinimum [1 byte]FontWidthMaximum [1 byte]Glyph Count [2 bytes] Glyphs Table: [ data position[2 bytes], width [1 byte] ] x Glyph CountGlyphs Data: (Glyph data) x Actual Glyph countGlyphDataSize: (FontWidthMaximum + 7) / 8 x FontHeight / 8字庫使用
初始化字體
/*** The font info.*/struct FontInfo{u32 offset;u8 height;u8 width_min;u8 width_stride;u8 glyph_data_size;u16 glyph_count;};typedef unsigned short WCHAR;int init_font(struct FontInfo *fontInfo){u32 offset = fontInfo->offset;// Not font formatif (FLASH_read_byte(offset + 0) != 'B' && FLASH_read_byte(offset + 1) != 'F')return 0;// Invalid font lib version.if (FLASH_read_byte(offset + FL_OFFSET_VERSION) != FONT_LIB_VERSION)return 0;fontInfo->height = FLASH_read_byte(offset + FL_OFFSET_HEIGHT);fontInfo->width_min = FLASH_read_byte(offset + FL_OFFSET_WIDTH_MIN);u8 width_max = FLASH_read_byte(offset + FL_OFFSET_WIDTH_MAX);fontInfo->width_stride = get_glyph_stride(width_max);fontInfo->glyph_data_size = (((width_max + 7) / 8) * fontInfo->height); //(fontInfo->width_stride + 7) / 8;fontInfo->glyph_count = FLASH_read_u16(offset + FL_OFFSET_GLYPH_COUNT);return 1;}尋址讀字體
在單片機程序中,通過下面的方式尋址讀字體:/*** Get the glyph by its UCS2 character encoding.** If it does not exist, return a special glyph.*/void get_glyph(WCHAR ch, struct Glyph *glyph){u32 pos = FL_OFFSET_GLYPH_TABLE + FONT_GLYPH_INFO_SIZE * (u32) ch;u16 posHigh = FLASH_read_byte(pos);u16 posLow = FLASH_read_byte(pos + 1);glyph->width = FLASH_read_byte(pos + 2);glyph->data_pos = (posHigh << 8) | posLow;}void get_glyph_data(struct FontInfo *fontInfo, struct Glyph *glyph, u8 offset, u8 data[MAX_GLYPH_STRIDE]){u32 FL_OFFSET_GLYPH_DATA = fontInfo->offset + FL_OFFSET_GLYPH_TABLE+ fontInfo->glyph_count * (u32) FONT_GLYPH_INFO_SIZE;u32 addr = FL_OFFSET_GLYPH_DATA + (u32) (glyph->data_pos) * fontInfo->glyph_data_size + offset;FLASH_fast_read(data, addr, get_glyph_stride(glyph->width));}
總結
以上是生活随笔為你收集整理的自制和烧录单片机 unicode 字库芯片 - 创建字库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android: eoeAndroid
- 下一篇: 检测字符串是否包含英文字母