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

Last change on this file since 3463 was 3463, checked in by ldelgass, 11 years ago

Begin process of renaming R2 library

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