我正在尝试将FreeType GlyphSlot位图转换为BGRA格式。
void DrawText(const std::string &text) {
FT_GlyphSlot Slot = face->glyph;
buffer.resize(0);
for (auto c : text) {
FT_Error error = FT_Load_Char(face, c, FT_LOAD_RENDER);
if (error)
continue;
auto width = Slot->bitmap.width;
auto height = Slot->bitmap.rows;
auto bufferSize = width * height * 4;
buffer.resize(buffer.size()+bufferSize);
uint8_t* src = Slot->bitmap.buffer;
uint8_t* startOfLine = src;
int dst = 0;
for (int y = 0; y < height; ++y) {
src = startOfLine;
for (int x = 0; x < width; ++x) {
auto value = *src;
src++;
buffer[dst++] = 0xff;
buffer[dst++] = 0xff;
buffer[dst++] = 0xff;
buffer[dst++] = value;
}
startOfLine += Slot->bitmap.pitch;
}
}
}
这给我输出乱码。我不确定要正确转换为B8G8R8A8
所需执行的操作。
一个问题是您用每个字符调整buffer
的大小(这会将先前的数据保留在新分配的空间的开头),但是当存储新字符c
的数据时,您覆盖了缓冲区的开始,因为dst
为0。您可能想在dst
调用之前将buffer.size()
设置为resize
。
int dst = /*previous buffer size*/;