source: trunk/packages/vizservers/nanovis/R2/src/R2Fonts.cpp @ 1115

Last change on this file since 1115 was 1115, checked in by gah, 16 years ago
File size: 7.3 KB
Line 
1#include <R2/R2Fonts.h>
2#include <R2/R2FilePath.h>
3#include <fstream>
4#include <stdarg.h>
5
6// constants
7const int c_nFileMagicHeader = 6666;
8
9R2Fonts::R2Fonts() :
10    _fontIndex(-1),
11    _screenWidth(512),
12    _screenHeight(512)
13{
14}
15
16R2Fonts::~R2Fonts()
17{
18    for (unsigned index = 0; index < _fonts.size(); ++index) {
19        glDeleteLists(_fonts[index]._displayLists, 256);
20        glDeleteTextures(1, &(_fonts[_fontIndex]. _fontTextureID));
21    }
22}
23
24void
25R2Fonts::setFont(const char* fontName)
26{
27    if (fontName != NULL) {
28        unsigned int i;
29        for (i = 0; i < _fonts.size(); ++i) {
30            if (strcmp(_fonts[i]._fontName, fontName) == 0) {
31                _fontIndex = i;
32                break;
33            }
34        }
35    }
36}
37
38
39void
40R2Fonts::addFont(const char* fontName, const char* fontFileName)
41{
42    R2FontAttributes sFont;
43   
44    loadFont(fontName, fontFileName, sFont);
45    initializeFont(sFont);
46    _fonts.push_back(sFont);
47}
48
49void
50R2Fonts::draw(const char* pString, ...) const
51{
52    va_list vlArgs;
53    char szVargsBuffer[1024];
54   
55    va_start(vlArgs, pString);
56    vsprintf(szVargsBuffer, pString, vlArgs);
57   
58    if (_fontIndex != -1) {
59        int length = strlen(szVargsBuffer);
60       
61        glListBase(_fonts[_fontIndex]._displayLists);
62        glCallLists(length, GL_UNSIGNED_BYTE,
63                    reinterpret_cast<const GLvoid*>(szVargsBuffer));
64    }
65}
66
67void
68R2Fonts::begin()
69{
70    glEnable(GL_TEXTURE_2D);
71    glBindTexture(GL_TEXTURE_2D, _fonts[_fontIndex]. _fontTextureID);
72   
73    glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT);
74    glMatrixMode(GL_PROJECTION);
75    glPushMatrix();
76   
77    glLoadIdentity( );
78    gluOrtho2D(0.0f, _screenWidth, _screenHeight, 0.0f);
79   
80    glMatrixMode(GL_MODELVIEW);
81    glPushMatrix( );
82    glLoadIdentity( );
83
84    glEnable(GL_TEXTURE_2D);
85    glEnable(GL_BLEND);
86    glDisable(GL_DEPTH_TEST);
87}
88
89void
90R2Fonts::end()
91{
92    glBindTexture(GL_TEXTURE_2D, 0);
93
94    glMatrixMode(GL_PROJECTION);
95    glPopMatrix( );
96
97    glMatrixMode(GL_MODELVIEW);
98    glPopMatrix( );
99
100    glPopAttrib( );
101}
102
103void
104R2Fonts::initializeFont(R2FontAttributes& attr)
105{
106    attr._displayLists = glGenLists(256);
107
108    R2int32 index;
109    for (index = 0; index < 256; ++index) {
110        R2FontAttributes::R2CharInfo& charInfo = attr._chars[index];
111        glNewList(attr._displayLists + index, GL_COMPILE);
112        if (charInfo._valid) {
113            glBegin(GL_TRIANGLE_STRIP);
114           
115            glTexCoord2f(charInfo._left, charInfo._top);
116            glVertex2i(0, 0);
117           
118            glTexCoord2f(charInfo._left, charInfo._bottom);
119            glVertex2i(0, (GLint)attr._fontHeight);
120           
121            glTexCoord2f(charInfo._right, charInfo._top);
122            glVertex2i((GLint)charInfo._width, 0);
123           
124            glTexCoord2f(charInfo._right,  charInfo._bottom);
125            glVertex2i((GLint)charInfo._width, (GLint)attr._fontHeight);
126           
127            glEnd();
128            glTranslatef(charInfo._width, 0.0f, 0.0f);
129        }
130        glEndList();
131    }
132}
133
134R2bool
135R2Fonts::loadFont(const char* fontName, const char* fontFileName,
136                  R2FontAttributes& sFont)
137{
138    R2bool bSuccess = false;
139
140    const char *path = R2FilePath::getInstance()->getPath(fontFileName);
141    if (path == NULL) {
142        return false;
143    }
144    std::ifstream fsInput(path, std::ios::binary);
145    if (fsInput) {
146        sFont._fontName = new char [strlen(fontName)+1];
147        strcpy(sFont._fontName, fontName);
148
149        // make sure this file is the correct type by checking the header
150        unsigned int uiFileId = 0;
151        fsInput.read(reinterpret_cast<char*>(&uiFileId), sizeof(unsigned int));
152        if (uiFileId == (unsigned int)c_nFileMagicHeader) {
153            // read general font/texture dimensions
154            unsigned int uiTextureWidth, uiTextureHeight, uiFontHeight;
155            uiTextureWidth = uiTextureHeight = uiFontHeight = 0;
156            fsInput.read(reinterpret_cast<char*>(&sFont._textureWidth),
157                         sizeof(unsigned int));
158            fsInput.read(reinterpret_cast<char*>(&sFont._textureHeight),
159                         sizeof(unsigned int));
160            fsInput.read(reinterpret_cast<char*>(&sFont._fontHeight),
161                         sizeof(unsigned int));
162
163            // read dimensions for each charactor in 256-char ASCII chart
164            for (int i = 0; i < 256; ++i) {
165                unsigned int uiSize = 0;
166
167                // top
168                fsInput.read(reinterpret_cast<char*>(&uiSize),
169                             sizeof(unsigned int));
170                sFont._chars[i]._top = static_cast<float>(uiSize) / sFont._textureHeight;
171                // left
172                fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int));
173                sFont._chars[i]._left = static_cast<float>(uiSize) / sFont._textureWidth;
174                // bottom
175                fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int));
176                sFont._chars[i]._bottom = static_cast<float>(uiSize) / sFont._textureHeight;
177                // right
178                fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int));
179                sFont._chars[i]._right = static_cast<float>(uiSize) / sFont._textureWidth;
180                // enabled
181                fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int));
182                sFont._chars[i]._valid = (uiSize != 0);
183                // width factor
184                float fWidthFactor = 1.0f;
185                fsInput.read(reinterpret_cast<char*>(&fWidthFactor), sizeof(float));
186                sFont._chars[i]._width = fWidthFactor * sFont._fontHeight;
187            }
188        }
189
190        // allocate and read the texture map
191        if (!fsInput.eof() && !fsInput.fail()) {
192            unsigned int uiArea = sFont._textureWidth * sFont._textureHeight;
193            unsigned char* pRawMap = new unsigned char[uiArea];
194            fsInput.read(reinterpret_cast<char*>(pRawMap), uiArea);
195
196            // we've only read the luminance values, but we need a luminance +
197            // alpha buffer, so we make a new buffer and duplicate the
198            // luminance values
199            unsigned char* pTexMap = new unsigned char[2 * uiArea];
200            unsigned char* pMap = pTexMap;
201            for (unsigned int i = 0; i < uiArea; ++i) {
202                *pMap++ = pRawMap[i];
203                *pMap++ = pRawMap[i];
204            }
205            delete[] pRawMap;
206            pRawMap = NULL;
207
208            // make texture map out of new buffer
209            glGenTextures(1, &sFont._fontTextureID);
210            glBindTexture(GL_TEXTURE_2D, sFont._fontTextureID);
211            glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
212                         sFont._textureWidth, sFont._textureHeight, 0,
213                         GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pTexMap);
214            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
215            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
216            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
217            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
218            glBindTexture(GL_TEXTURE_2D, 0);
219
220            delete[] pTexMap;
221            pTexMap = NULL;
222
223            bSuccess = true;
224        }
225
226        fsInput.close( );
227    }
228    delete [] path;
229    return bSuccess;
230}
231
232void
233R2Fonts::resize(R2int32 width, R2int32 height)
234{
235    _screenWidth = width;
236    _screenHeight = height;
237}
238
Note: See TracBrowser for help on using the repository browser.