Edit

kc3-lang/ftgl/src/FTFont/FTTextureFont.cpp

Branch :

  • Show log

    Commit

  • Author : sammy
    Date : 2008-05-01 18:30:29
    Hash : 822d0f8f
    Message : * 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.

  • src/FTFont/FTTextureFont.cpp
  • /*
     * FTGL - OpenGL font library
     *
     * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
     *
     * Permission is hereby granted, free of charge, to any person obtaining
     * a copy of this software and associated documentation files (the
     * "Software"), to deal in the Software without restriction, including
     * without limitation the rights to use, copy, modify, merge, publish,
     * distribute, sublicense, and/or sell copies of the Software, and to
     * permit persons to whom the Software is furnished to do so, subject to
     * the following conditions:
     *
     * The above copyright notice and this permission notice shall be
     * included in all copies or substantial portions of the Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     */
    
    #include "config.h"
    
    #include <cassert>
    #include <string> // For memset
    
    #include "FTGL/ftgl.h"
    
    #include "FTInternals.h"
    #include "FTTextureFontImpl.h"
    #include "FTTextureGlyphImpl.h"
    
    
    //
    //  FTTextureFont
    //
    
    
    FTTextureFont::FTTextureFont(char const *fontFilePath)
    {
        impl = new FTTextureFontImpl(fontFilePath);
    }
    
    
    FTTextureFont::FTTextureFont(const unsigned char *pBufferBytes,
                                 size_t bufferSizeInBytes)
    {
        impl = new FTTextureFontImpl(pBufferBytes, bufferSizeInBytes);
    }
    
    
    FTTextureFont::~FTTextureFont()
    {
        ;
    }
    
    
    //
    //  FTTextureFontImpl
    //
    
    
    static inline GLuint NextPowerOf2(GLuint in)
    {
         in -= 1;
    
         in |= in >> 16;
         in |= in >> 8;
         in |= in >> 4;
         in |= in >> 2;
         in |= in >> 1;
    
         return in + 1;
    }
    
    
    FTTextureFontImpl::FTTextureFontImpl(const char* fontFilePath)
    :   FTFontImpl(fontFilePath),
        maximumGLTextureSize(0),
        textureWidth(0),
        textureHeight(0),
        glyphHeight(0),
        glyphWidth(0),
        padding(3),
        xOffset(0),
        yOffset(0)
    {
        remGlyphs = numGlyphs = face.GlyphCount();
    }
    
    
    FTTextureFontImpl::FTTextureFontImpl(const unsigned char *pBufferBytes,
                                             size_t bufferSizeInBytes)
    :   FTFontImpl(pBufferBytes, bufferSizeInBytes),
        maximumGLTextureSize(0),
        textureWidth(0),
        textureHeight(0),
        glyphHeight(0),
        glyphWidth(0),
        padding(3),
        xOffset(0),
        yOffset(0)
    {
        remGlyphs = numGlyphs = face.GlyphCount();
    }
    
    
    FTTextureFontImpl::~FTTextureFontImpl()
    {
        if(textureIDList.size())
        {
            glDeleteTextures(textureIDList.size(),
                             (const GLuint*)&textureIDList[0]);
        }
    }
    
    
    FTGlyph* FTTextureFontImpl::MakeGlyph(unsigned int glyphIndex)
    {
        FT_GlyphSlot ftGlyph = face.Glyph(glyphIndex, FT_LOAD_NO_HINTING
                                                       | FT_LOAD_NO_BITMAP);
    
        if(ftGlyph)
        {
            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())
            {
                textureIDList.push_back(CreateTexture());
                xOffset = yOffset = padding;
            }
    
            if(xOffset > (textureWidth - glyphWidth))
            {
                xOffset = padding;
                yOffset += glyphHeight;
    
                if(yOffset > (textureHeight - glyphHeight))
                {
                    textureIDList.push_back(CreateTexture());
                    yOffset = padding;
                }
            }
    
            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 + 0.5);
    
            --remGlyphs;
            return tempGlyph;
        }
    
        err = face.Error();
        return NULL;
    }
    
    
    void FTTextureFontImpl::CalculateTextureSize()
    {
        if(!maximumGLTextureSize)
        {
            maximumGLTextureSize = 1024;
            glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&maximumGLTextureSize);
            assert(maximumGLTextureSize); // If you hit this then you have an invalid OpenGL context.
        }
    
        textureWidth = NextPowerOf2((remGlyphs * glyphWidth) + (padding * 2));
        textureWidth = textureWidth > maximumGLTextureSize ? maximumGLTextureSize : textureWidth;
    
        int h = static_cast<int>((textureWidth - (padding * 2)) / glyphWidth + 0.5);
    
        textureHeight = NextPowerOf2(((numGlyphs / h) + 1) * glyphHeight);
        textureHeight = textureHeight > maximumGLTextureSize ? maximumGLTextureSize : textureHeight;
    }
    
    
    GLuint FTTextureFontImpl::CreateTexture()
    {
        CalculateTextureSize();
    
        int totalMemory = textureWidth * textureHeight;
        unsigned char* textureMemory = new unsigned char[totalMemory];
        memset(textureMemory, 0, totalMemory);
    
        GLuint textID;
        glGenTextures(1, (GLuint*)&textID);
    
        glBindTexture(GL_TEXTURE_2D, textID);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    
        glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight,
                     0, GL_ALPHA, GL_UNSIGNED_BYTE, textureMemory);
    
        delete [] textureMemory;
    
        return textID;
    }
    
    
    bool FTTextureFontImpl::FaceSize(const unsigned int size, const unsigned int res)
    {
        if(!textureIDList.empty())
        {
            glDeleteTextures(textureIDList.size(), (const GLuint*)&textureIDList[0]);
            textureIDList.clear();
            remGlyphs = numGlyphs = face.GlyphCount();
        }
    
        return FTFontImpl::FaceSize(size, res);
    }
    
    
    template <typename T>
    inline void FTTextureFontImpl::RenderI(const T* string)
    {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
    
        FTTextureGlyphImpl::ResetActiveTexture();
    
        FTFontImpl::Render(string);
    }
    
    
    void FTTextureFontImpl::Render(const char* string)
    {
        RenderI(string);
    }
    
    
    void FTTextureFontImpl::Render(const char* string, int renderMode)
    {
        RenderI(string);
    }
    
    
    void FTTextureFontImpl::Render(const wchar_t* string)
    {
        RenderI(string);
    }
    
    
    void FTTextureFontImpl::Render(const wchar_t* string, int renderMode)
    {
        RenderI(string);
    }