From 150bfd36d71cafa0213f083a333fd686d844966f Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 9 Aug 2015 23:27:18 +0200 Subject: [PATCH] Remove font size guessing hack. According to the documentation, the point size given to TTF_OpenFont() is always assuming 72 DPI, so there is no need to repeatedly trying to load the font and checking the font metrics until we hit upon a point size that matches the pixel size we want. Instead we can just give the corresponding point size directly. --- src/CGraphics.cpp | 81 ++++++++++------------------------------------- src/init.cpp | 20 ++++-------- 2 files changed, 23 insertions(+), 78 deletions(-) diff --git a/src/CGraphics.cpp b/src/CGraphics.cpp index 80e8260..7dd4201 100644 --- a/src/CGraphics.cpp +++ b/src/CGraphics.cpp @@ -539,17 +539,9 @@ void Graphics::loadMapTiles(const char *baseDir) } } -/* -Note : We need to search for the right >>> PIXEL SIZE <<< and NOT point size!! -If a user has a resolution other than approximately 72dpi then -they will get a small or larger font and this won't work. This might look -weird since we'll load and delete multiple fonts, but it works... -*/ -void Graphics::loadFont(int i, const char *filename, int pixelSize) +void Graphics::loadFont(int i, const char *filename, int pointSize) { - int minx, maxx, miny, maxy, advance; - - debug(("Attempting to load a font with pixel size of %d...\n", pixelSize)); + debug(("Attempting to load font %s with point size of %d...\n", filename, pointSize)); if (font[i]) { @@ -557,65 +549,20 @@ void Graphics::loadFont(int i, const char *filename, int pixelSize) TTF_CloseFont(font[i]); } - char tempPath[PATH_MAX]; - - snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine->userHomeDirectory); + #if USEPAK + char tempPath[PATH_MAX]; + snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine->userHomeDirectory); + font[i] = TTF_OpenFont(tempPath, pointSize); + #else + font[i] = TTF_OpenFont("data/vera.ttf", pointSize); + #endif - bool found = false; - int size = 0; - - while (!found) + if (!font[i]) { - if (font[i]) - { - TTF_CloseFont(font[i]); - } - - #if USEPAK - font[i] = TTF_OpenFont(tempPath, ++size); - #else - font[i] = TTF_OpenFont("data/vera.ttf", ++size); - #endif - - if (!font[i]) - { - engine->reportFontFailure(); - } - - TTF_GlyphMetrics(font[i], '8', &minx, &maxx, &miny, &maxy, &advance); - - // great! we have an exact match - if (maxx == pixelSize) - { - break; - } - - // we've overshot, so we'll use the previous size! - if (maxx > pixelSize) - { - TTF_CloseFont(font[i]); - - #if USEPAK - font[i] = TTF_OpenFont(tempPath, size - 1); - #else - font[i] = TTF_OpenFont("data/vera.ttf", size - 1); - #endif - - TTF_GlyphMetrics(font[i], '8', &minx, &maxx, &miny, &maxy, &advance); - - break; - } - - if (size >= 100) - { - debug(("Pixel size has exceeded 99 pixels! I'm giving up!\n")); - engine->reportFontFailure(); - } + engine->reportFontFailure(); } TTF_SetFontStyle(font[i], TTF_STYLE_NORMAL); - - debug(("Got a match for font size %d - Nearest = %d\n", pixelSize, maxx)); } Sprite *Graphics::addSprite(const char *name) @@ -885,6 +832,12 @@ SDL_Surface *Graphics::getString(const char *in, bool transparent) text = TTF_RenderUTF8_Shaded(font[fontSize], "FONT_ERROR", fontForeground, fontBackground); } + if (!text) + { + fprintf(stderr, "Unable to render text: %s\n", SDL_GetError()); + abort(); + } + if (transparent) setTransparent(text); diff --git a/src/init.cpp b/src/init.cpp index a274ad8..3c333cc 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -386,26 +386,18 @@ void initSystem() debug(("Loading Fonts...\n")); #if USEPAK - - char tempPath[PATH_MAX]; - snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory); - remove(tempPath); - if (!engine.unpack("data/vera.ttf", PAK_FONT)) { engine.reportFontFailure(); } #endif - debug(("Trying to load correct font pixel sizes using a really half arsed routine!\n")); - debug(("If it crashes then you'll know why!\n")); - - graphics.loadFont(0, "data/vera.ttf", 7); - graphics.loadFont(1, "data/vera.ttf", 9); - graphics.loadFont(2, "data/vera.ttf", 11); - graphics.loadFont(3, "data/vera.ttf", 13); - graphics.loadFont(4, "data/vera.ttf", 15); - + graphics.loadFont(0, "data/vera.ttf", 12); + graphics.loadFont(1, "data/vera.ttf", 15); + graphics.loadFont(2, "data/vera.ttf", 19); + graphics.loadFont(3, "data/vera.ttf", 23); + graphics.loadFont(4, "data/vera.ttf", 24); + debug(("Font sizes all loaded!!\n")); audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!"); -- 2.39.2