[英]Freetype with C++ not loading all glyphs correctly
我正在使用 FreeType 來加載字形紋理,基本上是使用 FT_Load_Char 來獲取紋理,然后創建我的自定義 class 字符的實例,其中包含用於稍后渲染的所有指標和紋理:
for (unsigned int c = min; c < max; c++)
{
if (FT_Get_Char_Index(faces[faceIndex].second, c) == 0)
continue;
if (FT_Load_Char(faces[faceIndex].second, c, FT_LOAD_RENDER))
{
cout << "Error: Failed to load Glyph: " << c << endl;
continue;
}
FT_GlyphSlot glyph = faces[faceIndex].second->glyph;
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, glyph->bitmap.width, glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, glyph->bitmap.buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Character character;
character.texture = texture;
character.bearingX = glyph->bitmap_left;
character.bearingY = glyph->bitmap_top;
character.width = glyph->bitmap.width;
character.height = glyph->bitmap.rows;
character.advance = glyph->advance.x;
characters[faceIndex][c] = character;
count++;
}
這對絕大多數字符都有效,但輸入類似asd-=_+123
的內容將產生以下結果:
所以它沒有像+=-
那樣正確加載一些字形,我使用的是 NotoMono-Regular 所以它顯然有這些基本字形
通過打印出字形 bitmap 緩沖區進行進一步調試,給出以下“壞”字符: bitmap buffer of - gives: α α α α α α α α α α
而對於“好”字符,它類似於: bitmap buffer of 1 gives: 94
所以我認為問題出在這里,但不確定如何解決
您確定您的免費類型庫正在使用的 TTF 文件是完整的嗎? 嘗試將 TTF 文件上傳到這些網站之一,看看它在解析您遇到問題的字符時是否有問題。
Character character;
character.texture = texture;
character.bearingX = glyph->bitmap_left;
character.bearingY = glyph->bitmap_top;
character.width = glyph->bitmap.width;
character.height = glyph->bitmap.rows;
character.advance = glyph->advance.x;
事實證明,使用unsigned int
表示height
和int
表示bearingY
在語義上是正確的,但是,下面的算術是這樣的
transformation.translation().y() - (ch.height - ch.bearingY) * size * renderer->aspectRatio
其中ch.height - ch.bearingY
隱式“提升”為unsigned int
,然后隱式轉換為float
,如您所料,如果ch.height - ch.bearingY
的預期結果為負,就會發生壞事。 無論如何,誰決定unsigned int
是更高的轉換等級....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.