linux 下结合freetype 显示汉字

还是和之前的一篇文章结合使用,这里对freetype的深入,上位机或客户端跟解码盒子通信使用utf8的编码格式,

相关使用案例如下


std::string str = "\xE6\x98\x9F\xE6\x9C\x9F\xE5\x87\xA0";//星期几
printf("11111111111111  %s  ,length:%d\n", str.c_str(),str.size());
wchar_t  chiStr[20];

UTF2Unicode(chiStr, 3, (char*)str.c_str());
 printf(" **************************display chinese : %s \**********************************n",chiStr);

......

FT_Load_Glyph(pFTFace, FT_Get_Char_Index(pFTFace, chiStr[i]), FT_LOAD_DEFAULT); 




utf装Unicode的代码如下

int UTF2Unicode(wchar_t *wstr, int size, char *utf8)
{
    int size_s = strlen(utf8);
    int size_d = size;

    wchar_t *des =wstr;
    char *src = utf8;
    memset(des, 0, size * sizeof(wchar_t));

    int s = 0, d = 0;
//    bool toomuchbyte = true; //set true to skip error prefix.

    while (s < size_s && d < size_d)
    {
        unsigned char c = src[s];
        if ((c & 0x80) == 0) 
        {
            des[d++] += src[s++];
        } 
        else if((c & 0xE0) == 0xC0)  ///< 110x-xxxx 10xx-xxxx
        {
            wchar_t &wideChar = des[d++];
            wideChar  = (src[s + 0] & 0x3F) << 6;
            wideChar |= (src[s + 1] & 0x3F);
            s += 2;
        }
        else if((c & 0xF0) == 0xE0)  ///< 1110-xxxx 10xx-xxxx 10xx-xxxx
        {
            wchar_t &wideChar = des[d++];

            wideChar  = (src[s + 0] & 0x1F) << 12;
            wideChar |= (src[s + 1] & 0x3F) << 6;
            wideChar |= (src[s + 2] & 0x3F);

            s += 3;
        } 
        else if((c & 0xF8) == 0xF0)  ///< 1111-0xxx 10xx-xxxx 10xx-xxxx 10xx-xxxx 
        {
            wchar_t &wideChar = des[d++];

            wideChar  = (src[s + 0] & 0x0F) << 18;
            wideChar  = (src[s + 1] & 0x3F) << 12;
            wideChar |= (src[s + 2] & 0x3F) << 6;
            wideChar |= (src[s + 3] & 0x3F);
            s += 4;
        } 
        else 
        {
            wchar_t &wideChar = des[d++]; ///< 1111-10xx 10xx-xxxx 10xx-xxxx 10xx-xxxx 10xx-xxxx 

            wideChar  = (src[s + 0] & 0x07) << 24;
            wideChar  = (src[s + 1] & 0x3F) << 18;
            wideChar  = (src[s + 2] & 0x3F) << 12;
            wideChar |= (src[s + 3] & 0x3F) << 6;
            wideChar |= (src[s + 4] & 0x3F);
            s += 5;
        }
    }

    return d;
}

QQ截图20170413110339.jpg

最新评论

  1. 发布于:2017-04-13 11:12 回复
    http://www.xiaovdiy.cn/?post=320#368

sitemap