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

Last change on this file since 2840 was 2840, checked in by ldelgass, 12 years ago

Use trace macros in image loader. Formatting fixes in libs

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