使用 C++ 的 Freetype 未正确加载所有字形
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 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
是更高的转化率....
我正在使用 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 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
是更高的转化率....