繁体   English   中英

带有 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 缓冲区进行进一步调试,给出以下“坏”字符: bitmap buffer of - gives: α α α α α α α α α α

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

所以我认为问题出在这里,但不确定如何解决

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

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

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

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表示heightint表示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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM