Commit 822d0f8f8232e2a3dffd393d0491cce10c887456

sammy 2008-05-01T18:30:29

* Avoid crashing when the texture size is so small that its integer size becomes zero. At the same time, round many floats instead of simply flooring them to int. First part by Sean Morrison from bzflag commit r14590.

diff --git a/src/FTFont/FTTextureFont.cpp b/src/FTFont/FTTextureFont.cpp
index 7828bd1..73be355 100644
--- a/src/FTFont/FTTextureFont.cpp
+++ b/src/FTFont/FTTextureFont.cpp
@@ -126,8 +126,11 @@ FTGlyph* FTTextureFontImpl::MakeGlyph(unsigned int glyphIndex)
 
     if(ftGlyph)
     {
-        glyphHeight = static_cast<int>(charSize.Height());
-        glyphWidth = static_cast<int>(charSize.Width());
+        glyphHeight = static_cast<int>(charSize.Height() + 0.5);
+        glyphWidth = static_cast<int>(charSize.Width() + 0.5);
+
+        if(glyphHeight < 1) glyphHeight = 1;
+        if(glyphWidth < 1) glyphWidth = 1;
 
         if(textureIDList.empty())
         {
@@ -149,7 +152,7 @@ FTGlyph* FTTextureFontImpl::MakeGlyph(unsigned int glyphIndex)
 
         FTTextureGlyph* tempGlyph = new FTTextureGlyph(ftGlyph, textureIDList[textureIDList.size() - 1],
                                                         xOffset, yOffset, textureWidth, textureHeight);
-        xOffset += static_cast<int>(tempGlyph->BBox().Upper().X() - tempGlyph->BBox().Lower().X() + padding);
+        xOffset += static_cast<int>(tempGlyph->BBox().Upper().X() - tempGlyph->BBox().Lower().X() + padding + 0.5);
 
         --remGlyphs;
         return tempGlyph;
@@ -172,7 +175,7 @@ void FTTextureFontImpl::CalculateTextureSize()
     textureWidth = NextPowerOf2((remGlyphs * glyphWidth) + (padding * 2));
     textureWidth = textureWidth > maximumGLTextureSize ? maximumGLTextureSize : textureWidth;
 
-    int h = static_cast<int>((textureWidth - (padding * 2)) / glyphWidth);
+    int h = static_cast<int>((textureWidth - (padding * 2)) / glyphWidth + 0.5);
 
     textureHeight = NextPowerOf2(((numGlyphs / h) + 1) * glyphHeight);
     textureHeight = textureHeight > maximumGLTextureSize ? maximumGLTextureSize : textureHeight;