|
本人开始遇到的问题是,想在uCGUI界面上增加显示汉字的功能,但是每次需要把汉字的写成.c的文件编
写到CPU内,这样很占空间,也很不方便,用的时候也很麻烦,搜索了几天终于弄出点成果,拿出来跟大家分享一下。
1. 我们一共要修改2个文件:GUI.h 和 GUIType.h 新建两个文件:GUICharPEx.c GUI_Font12.c
GUI.h : 字库声明部分。
GUIType.h: 简单的宏定义,写字库的时候需要用到,也可以定义到别的位置。
GUICharPEx.c: 主要是自定义汉字的显示算法,和字库的获取方法。
GUI_Font12.c:这个是我们用用的字库,这里只做引导,实际字库在外围器件中,本人用的是SD卡+文件系统的方式,
也可以不加系统,用FLASH等等外部存储设备。
2. 每个文件我所做的修改:
Gui.H:
添加:
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ12;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ14;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ16;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ18;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ20
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ22;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ24;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ26;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ28;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ30;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ32;
只是简单的外部字库声明。
GUIType.h:
添加:
#define GUI_FONTTYPE_PROP_X \
GUIPROP_X_DispChar,\
GUIPROP_X_GetCharDistX,\
GUIMONO_GetFontInfo, \
GUIMONO_IsInFont, \
(tGUI_ENC_APIList*)0
UC_EncodeNone.c:
修改内容:
#include"GUI_Protected.h"
/*********************************************************************
*
* Static code
*
**********************************************************************
*/
/*********************************************************************
*
* _GetCharCode
*
* Purpose:
* Return the UNICODE character code of the current character.
*/
static U16 _GetCharCode(const char GUI_UNI_PTR * s) {
if((*s) > 0xA0)
{
return *(const U16 GUI_UNI_PTR *)s;
}
return *(const U8 GUI_UNI_PTR *)s;
}
/*********************************************************************
*
* _GetCharSize
*
* Purpose:
* Return the number of bytes of the current character.
*/
static int _GetCharSize(const char GUI_UNI_PTR * s) {
GUI_USE_PARA(s);
if((*s) > 0xA0)
{
return 2;
}
return 1;
}
/*********************************************************************
*
* _CalcSizeOfChar
*
* Purpose:
* Return the number of bytes needed for the given character.
*/
static int _CalcSizeOfChar(U16 Char) {
GUI_USE_PARA(Char);
if(Char > 0xA0A0)
{
return 2;
}
return 1;
}
/*********************************************************************
*
* _Encode
*
* Purpose:
* Encode character into 1/2/3 bytes.
*/
static int _Encode(char *s, U16 Char) {
if(Char > 0xA0A0)
{
*((U16 *)s) = (U16)(Char);
return 2;
}
*s = (U8)(Char);
return 1;
}
GUICharPEx.c:
新建立的文件
#include <stddef.h> /* needed for definition of NULL */
#include "GUI_Private.h"
#include "ff.h"
//字模数据的暂存数组,以单个字模的最大字节数为设定值
#define BYTES_PER_FONT 128
static U8 GUI_FontDataBuf[BYTES_PER_FONT];
/*---------------------------------------------------------------------------*/
/*字库外部函数部分-----------------------------------------------------------*/
void GUI_X_GetFontData(char* font, U32 oft, U8 *ptr, U8 bytes)
{
FIL fsrc; // 定义文件操作类
FRESULT res; // 定义操作结果变量
UINT br; // 定义读写数量变量
res = f_open(&fsrc, font, FA_OPEN_EXISTING | FA_READ); //打开字库文件
if(res != FR_OK)
{
}
res = f_lseek(&fsrc,oft); //找到首地址
res = f_read(&fsrc, ptr, bytes, &br); //读取32个字库点阵数据
res = f_close(&fsrc); //关闭字体
}
static void GUI_GetDataFromMemory(const GUI_FONT_PROP GUI_UNI_PTR *pProp, U16P c)
{
U8 BytesPerFont;
U32 oft;
char *font = (char *)pProp->paCharInfo->pData;
BytesPerFont = GUI_Context.pAFont->YSize * pProp->paCharInfo->BytesPerLine; //每个字模的数据字节数
if (BytesPerFont > BYTES_PER_FONT)
{BytesPerFont = BYTES_PER_FONT;}
if(c<0x80) {
oft = (c-0x20) * BytesPerFont; //英文字符地址偏移算法
}
else
{
oft = ((((c >> 8)-0xA1)) + ((c & 0xFF)-0xb0) * 94)* BytesPerFont; //中文字符地址偏移算法包括符号
}
GUI_X_GetFontData(font, oft, GUI_FontDataBuf, BytesPerFont);
}
void GUIPROP_X_DispChar(U16P c)
{
int BytesPerLine;
GUI_DRAWMODE DrawMode = GUI_Context.TextMode;
const GUI_FONT_PROP GUI_UNI_PTR *pProp = GUI_Context.pAFont->p.pProp;
//搜索定位字库数据信息
for (; pProp; pProp = pProp->pNext)
{
if ((c >= pProp->First) && (c <= pProp->Last))break;
}
if (pProp)
{
GUI_DRAWMODE OldDrawMode;
const GUI_CHARINFO GUI_UNI_PTR * pCharInfo = pProp->paCharInfo;
GUI_GetDataFromMemory(pProp, c);//取出字模数据
BytesPerLine = pCharInfo->BytesPerLine;
OldDrawMode = LCD_SetDrawMode(DrawMode);
LCD_DrawBitmap(GUI_Context.DispPosX, GUI_Context.DispPosY,
pCharInfo->XSize, GUI_Context.pAFont->YSize,
GUI_Context.pAFont->XMag, GUI_Context.pAFont->YMag,
1, /* Bits per Pixel */
BytesPerLine,
&GUI_FontDataBuf[0],
&LCD_BKCOLORINDEX
);
/* Fill empty pixel lines */
if (GUI_Context.pAFont->YDist > GUI_Context.pAFont->YSize)
{
int YMag = GUI_Context.pAFont->YMag;
int YDist = GUI_Context.pAFont->YDist * YMag;
int YSize = GUI_Context.pAFont->YSize * YMag;
if (DrawMode != LCD_DRAWMODE_TRANS)
{
LCD_COLOR OldColor = GUI_GetColor();
GUI_SetColor(GUI_GetBkColor());
LCD_FillRect(GUI_Context.DispPosX, GUI_Context.DispPosY + YSize,
GUI_Context.DispPosX + pCharInfo->XSize,
GUI_Context.DispPosY + YDist);
GUI_SetColor(OldColor);
}
}
LCD_SetDrawMode(OldDrawMode); /* Restore draw mode */
GUI_Context.DispPosX += pCharInfo->XDist * GUI_Context.pAFont->XMag;
}
}
/*********************************************************************
*
* GUIPROP_GetCharDistX
*/
int GUIPROP_X_GetCharDistX(U16P c)
{
const GUI_FONT_PROP GUI_UNI_PTR * pProp = GUI_Context.pAFont->p.pProp;
for (; pProp; pProp = pProp->pNext)
{
if ((c >= pProp->First) && (c <= pProp->Last))break;
}
return (pProp) ? (pProp->paCharInfo)->XSize * GUI_Context.pAFont->XMag : 0;
}
需要注意的是:
if(c<0x80) {
oft = (c-0x20) * BytesPerFont; //英文字符地址偏移算法
}
else
{
oft = ((((c >> 8)-0xA1)) + ((c & 0xFF)-0xb0) * 94)* BytesPerFont; //中文字符地址偏移算法包括符号
}
这个字库偏移的算法要根据实际字库的情况而定,我的中文字库全是中文没有字符,是这样的偏移算法,实际情况因个人所用的字库而定。
GUI_Font12.c:
新建内容:
#include "GUI.h"
extern int GUIPROP_X_GetCharDistX(U16P c);//声明部分
extern void GUIPROP_X_DispChar(U16P c) ;
GUI_CONST_STORAGE GUI_CHARINFO GUI_FontHZ12_CharInfo[2] =
{
{ 6, 6, 1, (void *)"1:/system/gui_font/ASC6x12.bin" }, //&ASC_ROM_6X12 字库的路径
{ 12, 12, 2,(void*)"1:/system/gui_font/HZ12x12.bin"}, //&HZ_ROM_12X12字库的路径
};
GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHZ12_PropHZ= {
0xA1A1, //第一个字符
0xF7FE, //最后一个字符
&GUI_FontHZ12_CharInfo[1], //字符信息
(void *)0,
};
GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHZ12_PropASC= {
0x0000, //第一个字符
0x007F, //最后一个字符
&GUI_FontHZ12_CharInfo[0], //字符信息
(void GUI_CONST_STORAGE *)&GUI_FontHZ12_PropHZ,
};
GUI_CONST_STORAGE GUI_FONT GUI_FontHZ12 =
{
GUI_FONTTYPE_PROP_X,
12, //xsize
12, //yszie
1, //x方向放大倍数
1, //y方向放大倍数
(void GUI_CONST_STORAGE *)&GUI_FontHZ12_PropASC
};
3.移植到这步就算移植完成了
举例:
#include”GUI.h”
void main(void)
{
GUI_Init();
GUI_SetBkColor(GUI_BLACK);
GUI_SetColor(GUI_WHITE);
GUI_Clear();
GUI_SetFont(&GUI_FontHZ12);
GUI_DispString(“洋洋kan世界”);
while(1);
}
实际遇到的问题是:由于SD卡速度有限加上频繁定位读取 ,速度有点慢,在控件中是可以实现的,但是拖动控件中会容易卡死现象。
4,字库的获取方法;
本人用的是字模III v3.91这个软件
用之前需要进行设置;
高级功能-->参数设置-->字模格式
需要设
然后选定 《二级汉字库》 再点击 《字库智能生成》,选择源字体,然后记录
字模宽度和高度在GUI_Font12.c进行相应的设置。
然后点击开始转换,会在软件的目录下自动生成Font.bat文件,将其改名为HZ12x12.bin存到SD卡相应的位置。ASC字符同理
到此结束。有问题可以及时联系我QQ18655199 QQ群18655199 。
PS:本人第一次写这种教程,尽情见谅,如果不妥之处,欢迎及时指出,本人愿意进行更正解答。
修正上一次的遗留问题,少修改一个文件。
uCGUI添加外置字库.zip
(419 KB, 下载次数: 332)
|
|