From aa6174b3f21eadb32eb227bb595e74bc3efc8983 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Tue, 13 Aug 2019 18:32:44 +0100 Subject: [PATCH] Glyphs now regenerate with the rest of the game's textures God-damn this code is ugly --- src/Draw.cpp | 2 + src/Font.cpp | 103 +++++++++++++++++++++++++++++++++------------------ src/Font.h | 1 + 3 files changed, 70 insertions(+), 36 deletions(-) diff --git a/src/Draw.cpp b/src/Draw.cpp index e3d2eaf8..3213a590 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -530,6 +530,8 @@ void RestoreSurfaces() // Guessed function name - this doesn't exist in the Linu } } } + + RestoreGlyphs(gFont); } #ifdef WINDOWS diff --git a/src/Font.cpp b/src/Font.cpp index 7cbe22bf..4a02a9b7 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -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); + } +} diff --git a/src/Font.h b/src/Font.h index 2e56ffe7..0009ec4a 100644 --- a/src/Font.h +++ b/src/Font.h @@ -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);