使用 C++ 的 Freetype 未正确加载所有字形

时间:2021-02-17 08:45:30

标签: c++ opengl freetype

我正在使用 FreeType 加载字形纹理,基本上使用 FT_Load_Char 来获取纹理,然后创建我的自定义类 Character 的实例,其中包含用于稍后渲染的所有指标和纹理:

  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 之类的内容会产生以下结果:

enter image description here

所以它没有像 +=- 那样正确加载一些字形,我使用的是 NotoMono-Regular,所以它显然有这些基本字形

通过打印字形位图缓冲区的进一步调试为“坏”字符提供以下内容: bitmap buffer of - gives: α α α α α α α α α α

对于“好”字符,它类似于:bitmap buffer of 1 gives: 94

所以我认为问题就在这里,但不知道如何解决

2 个答案:

答案 0 :(得分:0)

您确定您的免费类型库正在使用的 TTF 文件完整吗?尝试将 TTF 文件上传到这些网站之一,看看在解析您遇到问题的字符时是否有问题。

http://mathew-kurian.github.io/CharacterMap/

http://www.glyphrstudio.com/online/

答案 1 :(得分:-1)

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 是更高的转化排名....

相关问题