Glyphs now regenerate with the rest of the game's textures

God-damn this code is ugly
This commit is contained in:
Clownacy 2019-08-13 18:32:44 +01:00
parent 5da3b72fca
commit aa6174b3f2
3 changed files with 70 additions and 36 deletions

View file

@ -530,6 +530,8 @@ void RestoreSurfaces() // Guessed function name - this doesn't exist in the Linu
}
}
}
RestoreGlyphs(gFont);
}
#ifdef WINDOWS

View file

@ -955,6 +955,51 @@ static unsigned char GammaCorrect(unsigned char value)
return lookup[value];
}
static void SendGlyphToBackend(FontObject *font_object, Backend_Glyph *backend)
{
if (backend == NULL)
return;
FT_Bitmap bitmap;
FT_Bitmap_New(&bitmap);
FT_Bitmap_Convert(font_object->library, &font_object->face->glyph->bitmap, &bitmap, 1);
switch (font_object->face->glyph->bitmap.pixel_mode)
{
case FT_PIXEL_MODE_LCD:
for (unsigned int y = 0; y < bitmap.rows; ++y)
{
unsigned char *pixel_pointer = bitmap.buffer + y * bitmap.pitch;
for (unsigned int x = 0; x < bitmap.width; ++x)
{
*pixel_pointer = GammaCorrect(*pixel_pointer);
++pixel_pointer;
}
}
break;
case FT_PIXEL_MODE_GRAY:
for (unsigned int y = 0; y < bitmap.rows; ++y)
{
unsigned char *pixel_pointer = bitmap.buffer + y * bitmap.pitch;
for (unsigned int x = 0; x < bitmap.width; ++x)
{
*pixel_pointer = GammaCorrect((*pixel_pointer * 0xFF) / (bitmap.num_grays - 1));
++pixel_pointer;
}
}
break;
}
Backend_LoadGlyphPixels(backend, bitmap.buffer, bitmap.pitch);
FT_Bitmap_Done(font_object->library, &bitmap);
}
static CachedGlyph* GetGlyphCached(FontObject *font_object, unsigned long unicode_value)
{
CachedGlyph *glyph;
@ -978,48 +1023,15 @@ static CachedGlyph* GetGlyphCached(FontObject *font_object, unsigned long unicod
FT_Load_Glyph(font_object->face, glyph_index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
#endif
glyph->unicode_value = unicode_value;
glyph->x = font_object->face->glyph->bitmap_left;
glyph->y = (FT_MulFix(font_object->face->ascender, font_object->face->size->metrics.y_scale) - font_object->face->glyph->metrics.horiBearingY + (64 / 2)) / 64;
glyph->x_advance = font_object->face->glyph->advance.x / 64;
FT_Bitmap bitmap;
FT_Bitmap_New(&bitmap);
FT_Bitmap_Convert(font_object->library, &font_object->face->glyph->bitmap, &bitmap, 1);
unsigned char pixel_mode;
switch (font_object->face->glyph->bitmap.pixel_mode)
{
case FT_PIXEL_MODE_LCD:
pixel_mode = FONT_PIXEL_MODE_LCD;
for (unsigned int y = 0; y < bitmap.rows; ++y)
{
unsigned char *pixel_pointer = bitmap.buffer + y * bitmap.pitch;
for (unsigned int x = 0; x < bitmap.width; ++x)
{
*pixel_pointer = GammaCorrect(*pixel_pointer);
++pixel_pointer;
}
}
break;
case FT_PIXEL_MODE_GRAY:
pixel_mode = FONT_PIXEL_MODE_GRAY;
for (unsigned int y = 0; y < bitmap.rows; ++y)
{
unsigned char *pixel_pointer = bitmap.buffer + y * bitmap.pitch;
for (unsigned int x = 0; x < bitmap.width; ++x)
{
*pixel_pointer = GammaCorrect((*pixel_pointer * 0xFF) / (bitmap.num_grays - 1));
++pixel_pointer;
}
}
break;
case FT_PIXEL_MODE_MONO:
@ -1027,10 +1039,13 @@ static CachedGlyph* GetGlyphCached(FontObject *font_object, unsigned long unicod
break;
}
glyph->backend = Backend_CreateGlyph((pixel_mode == FT_PIXEL_MODE_LCD ? bitmap.width / 3 : bitmap.width), bitmap.rows, pixel_mode);
Backend_LoadGlyphPixels(glyph->backend, bitmap.buffer, bitmap.pitch);
glyph->backend = Backend_CreateGlyph((pixel_mode == FONT_PIXEL_MODE_LCD ? font_object->face->glyph->bitmap.width / 3 : font_object->face->glyph->bitmap.width), font_object->face->glyph->bitmap.rows, pixel_mode);
glyph->unicode_value = unicode_value;
glyph->x = font_object->face->glyph->bitmap_left;
glyph->y = (FT_MulFix(font_object->face->ascender, font_object->face->size->metrics.y_scale) - font_object->face->glyph->metrics.horiBearingY + (64 / 2)) / 64;
glyph->x_advance = font_object->face->glyph->advance.x / 64;
FT_Bitmap_Done(font_object->library, &bitmap);
SendGlyphToBackend(font_object, glyph->backend);
}
return glyph;
@ -1175,3 +1190,19 @@ void UnloadFont(FontObject *font_object)
free(font_object);
}
}
void RestoreGlyphs(FontObject *font_object)
{
for (CachedGlyph *glyph = font_object->glyph_list_head; glyph != NULL; glyph = glyph->next)
{
unsigned int glyph_index = FT_Get_Char_Index(font_object->face, glyph->unicode_value);
#ifndef DISABLE_FONT_ANTIALIASING
FT_Load_Glyph(font_object->face, glyph_index, FT_LOAD_RENDER | (font_object->lcd_mode ? FT_LOAD_TARGET_LCD : 0));
#else
FT_Load_Glyph(font_object->face, glyph_index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
#endif
SendGlyphToBackend(font_object, glyph->backend);
}
}

View file

@ -10,3 +10,4 @@ FontObject* LoadFontFromData(const unsigned char *data, size_t data_size, unsign
FontObject* LoadFont(const char *font_filename, unsigned int cell_width, unsigned int cell_height);
void DrawText(FontObject *font_object, Backend_Surface *surface, int x, int y, unsigned long colour, const char *string, size_t string_length);
void UnloadFont(FontObject *font_object);
void RestoreGlyphs(FontObject *font_object);