Freetype在位图中呈现废话

时间:2017-01-08 14:10:59

标签: c++ freetype

我正在尝试创建单色字形图集,但遇到了问题。 Freetype在字形的位图中呈现'废话'。我责怪freetype因为一些字形仍然正确呈现。

生成的纹理图集:

Texture atlas

为什么会这样,我该如何解决?

但是我仍然错了,这里是位图处理代码:

static std::vector<unsigned char> generateBitmap(FT_Face &face, unsigned int glyph, size_t *width, size_t *height) {
        FT_Load_Glyph(face, FT_Get_Char_Index(face, glyph), FT_LOAD_RENDER | FT_LOAD_MONOCHROME );

        FT_Bitmap bitmap;
        FT_Bitmap_New(&bitmap);
        FT_Bitmap_Convert(ftLib, &face->glyph->bitmap, &bitmap, 1);
        *width = bitmap.width;
        *height = bitmap.rows;


        std::vector<unsigned char> result(bitmap.width * bitmap.rows);//
        for (size_t y = 0; y < bitmap.rows; ++y)
        {
            for (size_t x = 0; x < bitmap.width; ++x)
            {
                result[(bitmap.width * y) + x] = bitmap.buffer[(bitmap.width * y) + x];
            }

        }
        FT_Bitmap_Done(ftLib, &bitmap);
        return result;
    }

将代码放在主缓冲区上的代码:

        static void putOnBuffer(std::vector<unsigned char> &buffer, std::vector<unsigned char> &bitmap, size_t height, size_t width) {
        int r = 0;
        while (r < height) {
            int w = 0;
            while (w < width) {
                //assume buffer is enough large
                size_t mainBufPos = ((currentBufferPositionY + r) * imageWidth) + (currentBufferPositionX + w);
                size_t bitmapBufPos = (r * width) + w;
                buffer[mainBufPos] = clamp(int(bitmap[bitmapBufPos] * 0x100), 0xff);
                w++;
            }
            r++;
        }
    }

1 个答案:

答案 0 :(得分:2)

来自文档:

  

将深度为1bpp,2bpp,4bpp,8bpp或32bpp的位图对象转换为深度为8bpp的位图对象,使得使用的字节数[ per ]为line(又称“pitch”) '对齐'的倍数。

在您的代码中,您将{1}}作为alignment参数的值传递给FT_Bitmap_Convert。在单色中,一个字节将是八个像素,因此水平渲染循环需要对宽度强制执行八的倍数。

参考:https://www.freetype.org/freetype2/docs/reference/ft2-bitmap_handling.html