sea_star 发表于 2013-8-9 18:02:39

emwin 汉字显示方法

本帖最后由 sea_star 于 2013-8-9 18:11 编辑

这几天想做个MP3播放器,用到了中文,搞了一天,增加俩个文件,放在库文件目录下,字库是我从文件加载的,现在共享。
/*
*********************************************************************************************************
*                                             uC/GUI V3.98
*                        Universal graphic software for embedded applications
*
*                     (c) Copyright 2002, Micrium Inc., Weston, FL
*                     (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
*            µC/GUI is protected by international copyright laws. Knowledge of the
*            source code may not be used to write a similar product. This file may
*            only be used in accordance with a license and should not be redistributed
*            in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File      : GUI_UC_EncodeNone.c
Purpose   : Encoding routines for non unicode systems (default)
---------------------------END-OF-HEADER------------------------------
*/

#include "GUI_Private.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;
}

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
/*********************************************************************
*
*       _API_Table
*/
const GUI_UC_ENC_APILIST GUI_UC_None = {
_GetCharCode,   /*return character code as U16 */
_GetCharSize,   /*return size of character: 1 */
_CalcSizeOfChar,/*return size of character: 1 */
_Encode         /*Encode character */
};
//const GUI_UC_ENC_APILIST GUI__API_TableNone = {
//_GetCharCode,   /*return character code as U16 */
//_GetCharSize,   /*return size of character: 1 */
//_CalcSizeOfChar,/*return size of character: 1 */
//_Encode         /*Encode character */
//};
/*********************************************************************
*
*       Exported code
*
**********************************************************************
*/
/*********************************************************************
*
*       GUI_UC_SetEncodeNone
*/
// void GUI_UC_SetEncodeNone(void) {
//   #if GUI_SUPPORT_UNICODE
//   GUI_LOCK();
//   GUI_Context.pUC_API = &GUI_UC_None;
//   GUI_UNLOCK();
//   #endif
// }

/*************************** End of file ****************************/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
#include <stddef.h>         /* needed for definition of NULL */
#include "GUI_Private.h"
#include <string.h>
/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
//×ÖÄ£Êý¾ÝµÄÔÝ´æÊý×é,ÒÔµ¥¸ö×ÖÄ£µÄ×î´ó×Ö½ÚÊýΪÉ趨ֵ
#define BYTES_PER_FONT              128
static U8 GUI_FontDataBuf;
extern unsigned char                *_Hzk_16;
extern unsigned char                *_Asc_16;
//´ÓÍⲿ´æ´¢Æ÷È¡µÃ×ÖÄ£Êý¾Ý
static void GUI_GetDataFromMemory(const GUI_FONT_PROP GUI_UNI_PTR *pProp, U16P c)
{
    U16 BytesPerFont;
    U32 oft;
    char *font = (char *)pProp->paCharInfo->pData;
    BytesPerFont = GUI_pContext->pAFont->YSize * pProp->paCharInfo->BytesPerLine; //ÿ¸ö×ÖÄ£µÄÊý¾Ý×Ö½ÚÊý
    if (BytesPerFont > BYTES_PER_FONT){BytesPerFont = BYTES_PER_FONT;}
    if (c < 0x80)                                                                       //Ó¢ÎÄ×Ö·ûµØÖ·Æ«ÒÆËã·¨
    {
           oft = c * BytesPerFont;
    }
    else                                                                        
    {
            oft = ((((c >> 8) - 0xA1)) + ((c & 0xFF) - 0xA1) * 94l) * BytesPerFont;                 //ÖÐÎÄ×Ö·ûµØÖ·Æ«ÒÆËã·¨°üÀ¨·ûºÅ
    }
        if (font == 'a')
        {
                memcpy(GUI_FontDataBuf, _Asc_16 + oft, BytesPerFont);                       
        }
        else
                memcpy(GUI_FontDataBuf, _Hzk_16 + oft, BytesPerFont);               
}
/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       GUIPROP_DispChar
*
* Purpose:
*   This is the routine that displays a character. It is used by all
*   other routines which display characters as a subroutine.
*/
void GUIPROP_X_DispChar(U16P c)
{
    int BytesPerLine;
    GUI_DRAWMODE DrawMode = GUI_pContext->TextMode;
    const GUI_FONT_PROP GUI_UNI_PTR *pProp = GUI_pContext->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_pContext->DispPosX, GUI_pContext->DispPosY,
                     pCharInfo->XSize, GUI_pContext->pAFont->YSize,
                     GUI_pContext->pAFont->XMag, GUI_pContext->pAFont->YMag,
                     1,   /* Bits per Pixel */
                     BytesPerLine,
                     &GUI_FontDataBuf,
                     &LCD_BKCOLORINDEX
                     );
      /* Fill empty pixel lines */
      if (GUI_pContext->pAFont->YDist > GUI_pContext->pAFont->YSize)
      {
            int YMag = GUI_pContext->pAFont->YMag;
            int YDist = GUI_pContext->pAFont->YDist * YMag;
            int YSize = GUI_pContext->pAFont->YSize * YMag;
            if (DrawMode != LCD_DRAWMODE_TRANS)
            {
                LCD_COLOR OldColor = GUI_GetColor();
                GUI_SetColor(GUI_GetBkColor());
                LCD_FillRect(GUI_pContext->DispPosX, GUI_pContext->DispPosY + YSize,
                           GUI_pContext->DispPosX + pCharInfo->XSize,
                           GUI_pContext->DispPosY + YDist);
                GUI_SetColor(OldColor);
            }
      }
      LCD_SetDrawMode(OldDrawMode); /* Restore draw mode */
//          if (!GUI_MoveRTL)
              GUI_pContext->DispPosX += pCharInfo->XDist * GUI_pContext->pAFont->XMag;
    }
}
/*********************************************************************
*
*       GUIPROP_GetCharDistX
*/
int GUIPROP_X_GetCharDistX(U16P c)
{
    const GUI_FONT_PROP GUI_UNI_PTR * pProp = GUI_pContext->pAFont->p.pProp;
    for (; pProp; pProp = pProp->pNext)                                       
    {
      if ((c >= pProp->First) && (c <= pProp->Last))break;
    }
    return (pProp) ? (pProp->paCharInfo)->XSize * GUI_pContext->pAFont->XMag : 0;
}

GUI_CONST_STORAGE GUI_CHARINFO GUI_FontHZ16_CharInfo =
{   
    {8,    8,1, (void *)"a"},      
    {16,16,2, (void *)"b"},         
};

GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHZ16_PropHZ = {
      0xA1A1,
      0xFFFF,
      &GUI_FontHZ16_CharInfo,
      (void *)0,
};

GUI_CONST_STORAGEGUI_FONT_PROP GUI_FontHZ16_PropASC = {
      0x0000,
      0x007F,
      &GUI_FontHZ16_CharInfo,
      (void GUI_CONST_STORAGE *)&GUI_FontHZ16_PropHZ,
};

GUI_CONST_STORAGEGUI_FONT GUI_FontHZ16 =
{
      GUI_FONTTYPE_PROP_USER,
      16,
      16,
      1,
      1,
      (void GUI_CONST_STORAGE *)&GUI_FontHZ16_PropASC
};

GUI_CONST_STORAGEGUI_FONT GUI_FontHZ16x2 =
{
      GUI_FONTTYPE_PROP_USER,
      16,
      16,
      2,
      2,
      (void GUI_CONST_STORAGE *)&GUI_FontHZ16_PropASC
};

在 gui_type.h 加
/*PROP_USER: chian */
#define GUI_FONTTYPE_PROP_USER       \
GUIPROP_X_DispChar,             \
GUIPROP_X_GetCharDistX,         \
GUIMONO_GetFontInfo,          \
GUIMONO_IsInFont,             \
(GUI_GETCHARINFO *)0,         \
(tGUI_ENC_APIList*)0                                                                                                  
在 gui.h 加       
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ12;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ16;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHZ16x2;   

yuanbuqi 发表于 2013-8-9 23:56:17

楼主开源{:lol:}

ersha4877 发表于 2013-8-10 16:28:38

不知道这种方法能否用在 EMWIN 5.20上,能否详细点

sea_star 发表于 2013-8-10 18:33:06

我就是在emwin5.20上用的

xiefy21 发表于 2013-8-12 14:51:53

mark……
顶一个…

zhoujunming27 发表于 2013-9-4 21:08:20

楼主无私奉献,{:lol:},学习了!顶!

gongjian32 发表于 2013-9-5 08:06:33

请问楼主,你这个是emWin,还是uCGUI3.98的版本?或者说你这种方法也能够在emWin下面实现吗?

love0679 发表于 2013-9-5 08:46:42

先mark,回去试试

keinYe 发表于 2013-9-9 17:49:21

收藏了,以后研究下,想弄这个很久了.

lzyr 发表于 2013-9-9 18:29:44

mark一下1准备用

Eric2013 发表于 2013-9-10 21:56:52

看了一下,这个不是在UCGUI3.xx时候的移植办法么,都说emWin只有库,没有源码,要修改源码,谁知试了试真的能用。。。
今天尝试了一下,真的可以用,哎,只怪自己以前没有去尝试,感谢楼主让我勇敢的去尝试

测试STemWin5.20,字库放到SD卡里面,显示效果如下:

nuoya99 发表于 2013-9-11 00:11:48

lz的播放器效果真赞

tiger5 发表于 2013-9-12 10:11:59

伤心病狂的MARK.

ABCDELF 发表于 2013-11-17 22:25:08

今天没白包宿,终于找到一个能使得例子了,万分感谢楼主的奉献

chenhou_1984 发表于 2013-11-22 13:55:40

强烈的MARK一下

brwang1983 发表于 2013-11-22 18:06:30

强烈的的感谢楼主啊

fengyunyu 发表于 2013-11-22 18:52:51

字库会不会太占FLASH?

happyliu 发表于 2013-12-8 20:32:18

很好 大部分系统都会在界面上用到中文 谢谢楼主分享

kalo425 发表于 2013-12-28 21:27:20

恩,现在显示汉字大体方法。这2天看了看。

1、外置字库,然后GBK 或者GB2312 之类的 解码 如楼主所说
2、分散加载内部flash
3、自制字库,utf8 转码之类的

由于自己半路出家。像楼主所用的那几个 文件,能否简单的解释下,说说下出处。现在是能用。但是不知道具体实现。很头疼···

witlong 发表于 2014-1-4 17:12:11

感谢楼主的分享。

xq13799775162 发表于 2014-1-25 13:21:26

记号,多谢楼主分享

看不见 发表于 2014-4-3 23:28:53

能上传一个工程文件吗

chengpiaopiao 发表于 2014-4-4 12:48:22

{:lol:}{:lol:}{:lol:}{:lol:}{:lol:}{:lol:}

gujiamao_love 发表于 2014-4-4 15:19:14

不错 点个赞

fengyunyu 发表于 2014-4-4 16:22:16

LZ,你说的“放在库文件目录下”,是指的字库文件么?字库文件如何生成?

zhangtuan 发表于 2014-4-8 08:54:44

先学习下

zhq_0510 发表于 2014-5-1 20:33:31

ok,可用

honami520 发表于 2014-7-15 05:12:12

兄弟,今天弄外部汉字显示弄了好几个小时!还是没搞出来!查了几个小时的原因,最终定位到这个地方
int GUIPROP_X_GetCharDistX(U16P c)
void GUIPROP_X_DispChar(U16P c)

这两个函数,按照代码里面,应该是读取一个汉字对应的内码。c就是这个汉字对应的内码。但是,我的程序里面每次读到的都是汉字的双字节内码的单独的一个字节。
第一次读第一字节,第二次读第二字节。GUI_UC_ENCODENONE.c这个文件里面,最后的那个函数又无法实现。不能实现这个函数,就没办法一次使用这个文件里面的读2汉字的方法。

我看你的代码里面直接把他注释掉了。那你是怎么样做到可以正确识别汉字的内码的呢?

jaky80000 发表于 2014-10-19 10:26:55

请问如果用stm32f10x的资源,能使用emwin吗?
谢谢!

kisscontrol 发表于 2014-10-23 16:45:02

准备什么时候玩下emwin,看似很炫的样子。

dzf050727 发表于 2014-10-27 14:15:25

收藏         

ymkfbj 发表于 2014-11-2 00:31:11

emWin可以用官方配的软件把电脑系统的字库生成专用的XBF字库的,可以存在TF卡中,也可以存在SPI FLASH中,那个的显示效果灰常好,硬件资源足可以圆滑字体,但是圆滑字体我没试过。

leo_advance 发表于 2014-11-2 00:42:37

谢谢楼主分享

hubinghuandi 发表于 2015-1-10 21:52:42

好东西收藏先!!

yangzhong316 发表于 2015-3-9 11:07:55

用得到.感谢楼主.

shouqiang_zhang 发表于 2015-3-9 11:50:31

参考下楼主的方法, 回去也试下.

zsfddd 发表于 2015-7-1 11:43:20

用得到.感谢楼主.

z903057128 发表于 2016-6-30 11:08:23

楼主可以分享一下gbk的字符集么
页: [1]
查看完整版本: emwin 汉字显示方法