问题
When I call FT_Load_Char, the glyph slot of the font face has garbage data for the bitmap, which changes every time the program is run.
I've ensured that FT_Init_FreeType and FT_New_Face are returning successfully. I've set the pixel size with FT_Set_Pixel_Sizes(face, 0, 48). The FT_Load_Char function is being called with FT_LOAD_RENDER and is returning successfully. I've also tried downloading the source code for freetype2 and building it myself just in case the pacman distribution of it (for MinGW64) is broken.
// Note: I've removed some irrelevant declarations
void fill_text(const char *text, int x, int y) {
static bool freetype_initialized = false;
static FT_Library ft = NULL;
static FT_Face face = NULL;
if (!freetype_initialized) {
!FT_Init_FreeType(&ft)
|| error("failed to initialize freetype library");
!FT_New_Face(ft, "VeraMono.ttf", 0, &face)
|| error("failed to load font");
FT_Set_Pixel_Sizes(face, 0, 48);
for (int c = 32; c < 128; c++) {
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
warning("failed to load character '%c'\n", c);
continue;
}
printf("%c = %i, %I\n", c
face->glyph->bitmap.width,
face->glyph->bitmap.rows);
...
The output ends up being something like:
wajid@DESKTOP-0TJ0HS4 MINGW64 ~/src/cgame
$ ./cgame.exe
= 0, 39226112
! = 0, 39226112
" = 0, 39226112
# = 0, 39226112
$ = 0, 39226112
% = 0, 39226112
& = 0, 39226112
' = 0, 39226112
( = 0, 39226112
) = 0, 39226112
* = 0, 39226112
+ = 0, 39226112
, = 0, 39226112
- = 0, 39226112
. = 0, 39226112
/ = 0, 39226112
0 = 0, 39226112
1 = 0, 39226112
2 = 0, 39226112
Every once in a while, the garbage is bad enough that it just crashes the program. I'm expecting to get a bitmap with proper metrics that I can use to render text with OpenGL.
回答1:
Turns out that there's nothing wrong with my code at all. There's actually a bug in one of the dependencies that's installed alongside freetype when using pacman on MinGW64. It's either harfbuzz, graphite2, glib2, pcre, or wineditline.
Thankfully, the library can still be built from source without these being installed. Do this, and the bug will go away.
来源:https://stackoverflow.com/questions/57451714/why-is-ft-load-char-giving-me-a-garbage-glyph-bitmap