Blitting 自由字形缓冲区时遇到问题

Having trouble blitting freetype glyph buffers

我在拼接由 freetype 创建的字形时遇到问题。使用 freeimage 完美工作,但我不想只为其中的一小部分包含一个巨大的库。

完整代码在githubhttps://github.com/live627/Engine/blob/master/Engine/Engine/fontmanager.cpp#L171

我分配缓冲区

unsigned char * charmap = new unsigned char[m_width * m_height]();

我像这样 blit 像素缓冲区并遇到访问冲突

void Font::StitchGlyph(const GlyphInfo g,
    unsigned int px, unsigned int py, unsigned int total_width,
    unsigned int max_height, unsigned char * charmap)
{
    auto WIDTH = total_width, HEIGHT = max_height;

    for (int y = 0; y < g.bh; y++)
    {
        for (int x = 0; x < g.bw; x++)
        {
            //if (x >= WIDTH || y >= HEIGHT)
            //  continue; 

            charmap[(py + y) * WIDTH + (px + x)] = g.img[y * g.bw + x];
        }
    }
}

我会在代码中验证您的偏移量与字形宽度和高度相结合不超过分配的范围。所以检查

if (px + GlyphInfo.bw > m_width || py + GlyphInfo.bh > m_height) 
    // error

我猜这就是它的来源。