Remove font size guessing hack.
authorGuus Sliepen <guus@debian.org>
Sun, 9 Aug 2015 21:27:18 +0000 (23:27 +0200)
committerGuus Sliepen <guus@debian.org>
Sun, 9 Aug 2015 21:27:18 +0000 (23:27 +0200)
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
src/init.cpp

index 80e8260e99123c6143fef3ff0f5350c292d8c55b..7dd4201200769e5deae1518016d03fd3ea9aaecf 100644 (file)
@@ -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);
 
index a274ad8a9d48a9f69c367eafe46cc0c01c5c3fbe..3c333cc10d8d3ed632c712b57ad9fd174e55e5fb 100644 (file)
@@ -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!!!");