~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to extern/bFTGL/src/FTGLTextureFont.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <string> // For memset
 
2
 
 
3
#include "FTGLTextureFont.h"
 
4
#include "FTTextureGlyph.h"
 
5
 
 
6
 
 
7
inline GLuint NextPowerOf2( GLuint in)
 
8
{
 
9
     in -= 1;
 
10
 
 
11
     in |= in >> 16;
 
12
     in |= in >> 8;
 
13
     in |= in >> 4;
 
14
     in |= in >> 2;
 
15
     in |= in >> 1;
 
16
 
 
17
     return in + 1;
 
18
}
 
19
 
 
20
 
 
21
FTGLTextureFont::FTGLTextureFont( const char* fontname)
 
22
:   FTFont( fontname),
 
23
    maxTextSize(0),
 
24
    textureWidth(0),
 
25
    textureHeight(0),
 
26
    glyphHeight(0),
 
27
    glyphWidth(0),
 
28
    padding(3),
 
29
    xOffset(0),
 
30
    yOffset(0)
 
31
{
 
32
    remGlyphs = numGlyphs = face.GlyphCount();
 
33
}
 
34
 
 
35
 
 
36
FTGLTextureFont::FTGLTextureFont( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
 
37
:   FTFont( pBufferBytes, bufferSizeInBytes),
 
38
    maxTextSize(0),
 
39
    textureWidth(0),
 
40
    textureHeight(0),
 
41
    glyphHeight(0),
 
42
    glyphWidth(0),
 
43
    padding(3),
 
44
    xOffset(0),
 
45
    yOffset(0)
 
46
{
 
47
    remGlyphs = numGlyphs = face.GlyphCount();
 
48
}
 
49
 
 
50
 
 
51
FTGLTextureFont::~FTGLTextureFont()
 
52
{
 
53
    glDeleteTextures( textureIDList.size(), (const GLuint*)&textureIDList[0]);
 
54
}
 
55
 
 
56
 
 
57
FTGlyph* FTGLTextureFont::MakeGlyph( unsigned int glyphIndex)
 
58
{
 
59
    FT_GlyphSlot ftGlyph = face.Glyph( glyphIndex, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
 
60
    
 
61
    if( ftGlyph)
 
62
    {
 
63
        glyphHeight = static_cast<int>( charSize.Height());
 
64
        glyphWidth = static_cast<int>( charSize.Width());
 
65
        
 
66
        if( textureIDList.empty())
 
67
        {
 
68
            textureIDList.push_back( CreateTexture());
 
69
            xOffset = yOffset = padding;
 
70
        }
 
71
        
 
72
        if( xOffset > ( textureWidth - glyphWidth))
 
73
        {
 
74
            xOffset = padding;
 
75
            yOffset += glyphHeight;
 
76
            
 
77
            if( yOffset > ( textureHeight - glyphHeight))
 
78
            {
 
79
                textureIDList.push_back( CreateTexture());
 
80
                yOffset = padding;
 
81
            }
 
82
        }
 
83
        
 
84
        FTTextureGlyph* tempGlyph = new FTTextureGlyph( ftGlyph, textureIDList[textureIDList.size() - 1],
 
85
                                                        xOffset, yOffset, textureWidth, textureHeight);
 
86
        xOffset += static_cast<int>( tempGlyph->BBox().upperX - tempGlyph->BBox().lowerX + padding);
 
87
        
 
88
        --remGlyphs;
 
89
        return tempGlyph;
 
90
    }
 
91
    
 
92
    err = face.Error();
 
93
    return NULL;
 
94
}
 
95
 
 
96
 
 
97
void FTGLTextureFont::CalculateTextureSize()
 
98
{
 
99
    if( !maxTextSize)
 
100
    {
 
101
        glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*)&maxTextSize);
 
102
    }
 
103
    
 
104
    textureWidth = NextPowerOf2( (remGlyphs * glyphWidth) + ( padding * 2));
 
105
    if( textureWidth > maxTextSize)
 
106
    {
 
107
        textureWidth = maxTextSize;
 
108
    }
 
109
    
 
110
    int h = static_cast<int>( (textureWidth - ( padding * 2)) / glyphWidth);
 
111
        
 
112
    textureHeight = NextPowerOf2( (( numGlyphs / h) + 1) * glyphHeight);
 
113
    textureHeight = textureHeight > maxTextSize ? maxTextSize : textureHeight;
 
114
}
 
115
 
 
116
 
 
117
GLuint FTGLTextureFont::CreateTexture()
 
118
{   
 
119
    CalculateTextureSize();
 
120
    
 
121
    int totalMemory = textureWidth * textureHeight;
 
122
    unsigned char* textureMemory = new unsigned char[totalMemory];
 
123
    memset( textureMemory, 0, totalMemory);
 
124
 
 
125
    GLuint textID;
 
126
    glGenTextures( 1, (GLuint*)&textID);
 
127
 
 
128
    glBindTexture( GL_TEXTURE_2D, textID);
 
129
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
 
130
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
 
131
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
132
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
133
 
 
134
    glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, textureMemory);
 
135
 
 
136
    delete [] textureMemory;
 
137
 
 
138
    return textID;
 
139
}
 
140
 
 
141
 
 
142
bool FTGLTextureFont::FaceSize( const unsigned int size, const unsigned int res)
 
143
{
 
144
    if( !textureIDList.empty())
 
145
    {
 
146
        glDeleteTextures( textureIDList.size(), (const GLuint*)&textureIDList[0]);
 
147
        remGlyphs = numGlyphs = face.GlyphCount();
 
148
    }
 
149
 
 
150
    return FTFont::FaceSize( size, res);
 
151
}
 
152
 
 
153
 
 
154
void FTGLTextureFont::Render( const char* string)
 
155
{   
 
156
    glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
 
157
    
 
158
    glEnable(GL_BLEND);
 
159
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
 
160
    
 
161
    FTFont::Render( string);
 
162
 
 
163
    glPopAttrib();
 
164
}
 
165
 
 
166
 
 
167
void FTGLTextureFont::Render( const wchar_t* string)
 
168
{   
 
169
    glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
 
170
    
 
171
    glEnable(GL_BLEND);
 
172
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
 
173
    
 
174
    FTFont::Render( string);
 
175
    
 
176
    glPopAttrib();
 
177
}
 
178