vshuo7013 2020-01-22 10:59 采纳率: 9.1%
浏览 832
已结题

用freetype转换字符串成位图数据问题?

这是我的转化代码,转换出来的效果图:
图片说明

我是转换一个字符串:HELLO,但是为什么最后成了两个,而且宽和高似乎都给成了两倍,以下是代码,请大神分析下下面的代码有问题吗?如何修改

static int GetBitmap(    char* szFontFile, wchar_t* swText, int ulLen, int nFontSize, int nSpace, BITMAP_S *stBitmap)
{
    FT_Library ftLib = NULL;
    FT_Face ftFace = NULL;
    FT_Error ftError = 0;
    FT_Bitmap ftBitmap;
    FT_BitmapGlyph ftBmpGlyph;
    FT_Glyph ftGlyph;
    FT_GlyphSlot ftGlyphslot;
    int i, j, k, temp;
    int start_x = 0, start_y = 0;
    int nChHeight = 0, nChWidth = 0, nTextWidth = 0;
    size_t size = 0;
    unsigned char** pTextAlpha = NULL;

    int BgHeight = nFontSize + 5; /* 背景图形的高度,这个高度要大于字体的高度,所以是+5 */

    ftError = FT_Init_FreeType(&ftLib);
    if (ftError)
    {
        printf("FT_Init_FreeType() Failed!\n");
        ftLib = 0;
        return -1;
    }
    printf("========fontfile=%s\n", szFontFile);
    ftError = FT_New_Face(ftLib, szFontFile, 0, &ftFace);
    /*if (ftError == FT_Err_Unknown_File_Format)
    {
        printf("FT_New_Face() Failed! FT_Err_Unknown_File_Format\n");
        return -2;
    }
    else */if (ftError)
    {   
        printf("FT_New_Face() Failed %d! Other Error!\n", ftError);
        return -3;
    }

    pTextAlpha = (uint8_t**)malloc(sizeof(uint8_t*)*BgHeight); //分配内存,用于存储字体背景的数据
    if (NULL == pTextAlpha)
    {
        printf("malloc() Failed!\n");
        return -1;
    }
    for (i=0; i < BgHeight; i++)
    {
        pTextAlpha[i] = (uint8_t*)malloc(sizeof(uint8_t)*1);     //为背景图的每一行分配内存
        if (NULL == pTextAlpha[i])
        {
            printf("malloc() Failed! %d\n", i);
            return -1;
        }
    }

    ftError = FT_Select_Charmap(ftFace, FT_ENCODING_UNICODE);   //设置字体的编码方式
    if (ftError)
    {
        printf("FT_Select_Charmap() Failed! error=%d\n", ftError);
        return ftError;
    }

    ftError = FT_Set_Pixel_Sizes(ftFace, 0, nFontSize); //设置字体大小
    if (ftError)
    {
        printf("FT_Set_Pixel_Sizes() Failed! error=%d\n",ftError);
        return ftError;
    }    

    uint8_t *data;
    ftGlyphslot = ftFace->glyph;
    printf("======================ulen = %d\n", ulLen);
    for (temp = 0; temp < ulLen; temp++)
    {
        printf("#######################swText[%d]=%04x\n", temp,swText[temp]);
        ftError = FT_Load_Char(ftFace, swText[temp], FT_LOAD_RENDER | FT_LOAD_NO_AUTOHINT);//提取一个字形图像
        if (ftError)
        {
            printf("FT_Load_Char() Failed!, ftError=%d\n", ftError);
            continue;
        }      
        ftError = FT_Get_Glyph(ftFace->glyph, &ftGlyph);
        if (ftError)
        {
            printf("FT_Get_Glyph() Failed!");
            return ftError;
        } 

        if (swText[temp] == L' ')//如果取得的为空格
        {
            printf("M MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM M\n");
            k = 0;
            nChWidth = (nFontSize - 2) / 2;
            nChHeight = nFontSize;
            nTextWidth = start_x + nChWidth;
            start_y = 0;
            for (i = 0; i < BgHeight; i++)
            {
                pTextAlpha[i] = (uint8_t*)realloc(pTextAlpha[i], sizeof(uint8_t)*nTextWidth);
                memset(pTextAlpha[i], 0 ,sizeof(uint8_t)*nTextWidth);
                for (j = start_x - nSpace; j < nTextWidth; j++)
                {
                    pTextAlpha[i][j] = 0;
                }
            }
            for (i = 0; i < nChHeight; i++)
            {
                for (j=0; j< nChWidth; j++)
                {
                    pTextAlpha[start_y+i][start_x+j] = 0;
                    k++;
                }
            }
            start_x += (nChWidth + nSpace); /* 画笔向右边移动 */
        }
        else
        {
            /* 256级灰度字形转换成位图 */
            ftError = FT_Glyph_To_Bitmap(&ftGlyph, FT_RENDER_MODE_NORMAL , NULL, 1);
            if (ftError)
            {
                printf("FT_Glyph_To_Bitmap() Failed!\n");
            }
            ftBmpGlyph = (FT_BitmapGlyph)ftGlyph;
            ftBitmap = ftBmpGlyph->bitmap;
            k = 0;
            printf("XXXXXXXXXX:nFontSize = %d, bitmap_top = %d\n", nFontSize, ftGlyphslot->bitmap_top);
            printf("XXXXXXXXXX:ftBitmap.rows = %d, BgHeight = %d\n", ftBitmap.rows, BgHeight);

            start_y = nFontSize - ftGlyphslot->bitmap_top + 2;  /* 获取起点的y轴坐标 */
            //start_y = nFontSize - ftBitmap.rows + 2;
            if(start_y < 0)
            {
                start_y = 0;
            }

            if (ftBitmap.rows > BgHeight)
            {
                nChHeight = nFontSize;
            }
            else
            {
                nChHeight = ftBitmap.rows;
            }

            if (nChHeight + start_y > BgHeight)
            {
                nChHeight = BgHeight - start_y;
            }
            nChWidth = ftBitmap.width;
            nTextWidth = start_x + ftBitmap.width;
            printf("XXXXXXXXXX:nChHeight = %d, nChWidth = %d, nTextWidth = %d\n",nChHeight, nChWidth, nTextWidth);
            for (i=0; i<BgHeight; i++)
            {
                pTextAlpha[i] = (uint8_t*)realloc(pTextAlpha[i], nTextWidth);
                for (j = start_x - nSpace; j < nTextWidth; j++)
                {
                    if(j < 0) j = 0;
                    pTextAlpha[i][j] = 0;
                }
            }
            for (i = 0; i < BgHeight; i++)
            {
                for (j = 0; j < nChWidth; j++)
                {
                    if (i >= start_y && i < start_y + nChHeight)
                    {
                        pTextAlpha[i][start_x + j] = ftBitmap.buffer[k];
                        k++;
                    }
                    else
                    {
                        pTextAlpha[i][start_x + j] = 0;
                    }
                }
            }
            start_x += (nChWidth + nSpace);
            FT_Done_Glyph(ftGlyph);
            printf("FT_Done_Glyph() Success!\n");
            ftGlyph = NULL;
        }
    }

    FT_Done_Face(ftFace);
    ftFace = NULL;
    FT_Done_FreeType(ftLib);
    ftLib = NULL;
    stBitmap->u32Width = nTextWidth;
    stBitmap->u32Height = BgHeight;
    size = sizeof(uint8_t) * nTextWidth * BgHeight;
    stBitmap->pData = (uint8_t*)malloc(size);
    memset(stBitmap->pData, 0, size);
    printf("===================nTextWidth = %d\n", nTextWidth);
    printf("===================BgHeight = %d\n", BgHeight);
    printf("===================nChHeight = %d\n", nChHeight);
    k = 0;

    for (i = 0; i < BgHeight; i++)
    {
        memcpy(((char*)stBitmap->pData) + k, pTextAlpha[i],  nTextWidth);
        k += nTextWidth;
        free(pTextAlpha[i]);
        pTextAlpha[i] = NULL;
    }

    free(pTextAlpha);
    pTextAlpha = NULL;
    return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-01-22 23:27
    关注

    无非两个原因:(1)字符串本身有2遍hello重复(2)你画了2次
    for (temp = 0; temp < ulLen; temp++) 这里下断点看 ulLen 的值是多少。

    具体代码就不详细看了,采纳率太低!

    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档