又开始安静的敲代码了。刚用了用UCGUI,移植好了准备用UCGUI懒人工具设计界面的时候,电脑出现状况了呀。有遇到过着情况的来帮忙解决一下。
这次是将字库放在SD卡中,来分享一下UCGUI用外部FLASH字库的方法。16_24_32HZK.zip
ucgui中,字符显示的底层函数是 GUICharP.c 中的 void GUIPROP_DispChar(U16P c) 函数,我们将这个函数修改如下:
/********************************************************************* * * GUIPROP_DispChar * * Purpose: * This is the routine that displays a character. It is used by all * other routines which display characters as a subroutine. */ extern U8 acFont8x16[][16]; void GUIPROP_DispChar(U16P c) { int BytesPerLine; U8 BytesPerFont; //一个字的字节数 U32 base,oft; //字库的起始地址和偏移量 unsigned char i_table = 0; GUI_DRAWMODE DrawMode = GUI_Context.TextMode; const GUI_FONT_PROP GUI_UNI_PTR * pProp = GUIPROP_FindChar(GUI_Context.pAFont->p.pProp, c); if (pProp) { GUI_DRAWMODE OldDrawMode; const GUI_CHARINFO GUI_UNI_PTR * pCharInfo; //支持3种字体== if((GUI_Context.pAFont == &GUI_FontHZ16)) { pCharInfo = pProp->paCharInfo; base = (U32)pProp->paCharInfo->pData; BytesPerFont = GUI_Context.pAFont->YSize * pProp->paCharInfo->BytesPerLine; //每个字模的数据字节数 if (BytesPerFont > BYTES_PER_FONT) { BytesPerFont = BYTES_PER_FONT; } if (c < 0x80) //英文字符地址偏移算法 { // GUI_SetFont(&GUI_Font8x16); // GUI_DispCharAt(c,GUI_Context.DispPosX,GUI_Context.DispPosY); // GUI_SetFont(&GUI_FontHZ16);//oft = base + (c - 0x20) * BytesPerFont; //计算出字码在flash中的偏移地址 for(i_table = 0;i_table < 16;i_table++) { GUI_FontDataBuf[i_table] = acFont8x16[c-0x20][i_table]; } } else //中文字符地址偏移算法 { oft = base + (((c>>8) - 0xa1) * 94 + ((c&0xff) - 0xa1)) * BytesPerFont; LCD_ReadFlashBit(oft, GUI_FontDataBuf, BytesPerFont);//取出字模数据 } 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, &LCD_BKCOLORINDEX ); } //-- else { pCharInfo = pProp->paCharInfo+(c-pProp->First); 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, pCharInfo->pData, &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; } }
在GUI.H中添加汉字库在flash中的首地址,及字库声明
/*汉字*/ //汉字库在FLASH中的首地址 #define GUI_FontHZ16_Flash_BaseAddr 0x0 #define GUI_FontHZ24_Flash_BaseAddr 0x00045080 #define GUI_FontHZ32_Flash_BaseAddr 0x000E05A0 extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ16; extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ24; extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ32;
注意三个字库的起始地址与GUI.h中定义的应该是一样的。还有字库生成时,也应该是标准的汉字库,满足 oft = base + (((c>>8) - 0xa1) * 94 + ((c&0xff) - 0xa1)) * BytesPerFont; 的计算方式。
完成之后就可以直接这样操作了哟:GUI_DispStringAt("GUI初始化完毕",10,10);