Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

glt/fontasci.cpp

Go to the documentation of this file.
00001 #include "fontasci.h"
00002 
00015 #include <math/real.h>
00016 
00017 #include <algorithm>
00018 #include <cstdio>
00019 #include <cassert>
00020 using namespace std;
00021 
00022 GltFontAscii::GltFontAscii(void *data)
00023 : _width(0), _height(0), _listBase(-1)
00024 {
00025     if (data)
00026         init(data);
00027 }
00028 
00029 GltFontAscii::~GltFontAscii()
00030 {
00031     init(NULL);
00032 }
00033 
00034 void
00035 GltFontAscii::init(void *data)
00036 {
00037     clear();
00038 
00039     if (data)
00040     {
00041         void *font = getHeader(data,_width,_height);
00042         if (_width!=0 && _height!=0 && font)
00043         {
00044             _hStep = _width;
00045             _vStep = _height;
00046             compileLists(font);
00047             _init = true;
00048         }
00049     }
00050 }
00051 
00052 void 
00053 GltFontAscii::clear()
00054 {
00055     _init = false;
00056 
00057     if (_listBase!=-1)
00058     {
00059         glDeleteLists(_listBase,256);
00060         _listBase = -1;
00061     }
00062 }
00063 
00064 void
00065 GltFontAscii::compileLists(void *data)
00066 {
00067     if (_listBase==-1 && data)
00068     {
00069         GLubyte *ptr = (GLubyte *) data;
00070         const int bytesPerChar = ((_width+7)>>3)*_height;
00071 
00072         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00073         _listBase = glGenLists(256);
00074         for (int i = 0; i < 255; i++) 
00075         {
00076             glNewList(i+_listBase, GL_COMPILE);
00077             glBitmap(_width,_height,0,_height-1,hStep(),0,(GLubyte *) ptr);
00078             glEndList();
00079             ptr += bytesPerChar;
00080         }
00081     }
00082 }
00083 
00084 bool 
00085 GltFontAscii::print(const wchar_t ch) const
00086 {
00087     assert(_init);
00088 
00089     if (_init)
00090         return print(char(ch));
00091     else
00092         return false;
00093 }
00094 
00095 bool 
00096 GltFontAscii::print(const wstring &str) const
00097 {
00098     assert(_init);
00099 
00100     if (_init)
00101     {
00102         string s;
00103         wstring2string(s,str);
00104         return print(s);
00105     }
00106     else
00107         return false;
00108 }
00109 
00110 bool 
00111 GltFontAscii::print(const char ch) const
00112 {
00113     assert(_init);
00114 
00115     if (_init)
00116     {
00117         glCallList(_listBase + GLubyte(ch));
00118         return true;
00119     }
00120     else
00121         return false;
00122 }
00123 
00124 bool 
00125 GltFontAscii::print(const string &str) const
00126 {
00127     assert(_init);
00128 
00129     if (_init)
00130     {
00131         glPushAttrib(GL_LIST_BIT|GL_CURRENT_BIT);
00132             glListBase(_listBase);
00133             glCallLists(str.length(), GL_UNSIGNED_BYTE, (GLubyte *) str.c_str());
00134         glPopAttrib();
00135 
00136         // Advance OpenGL raster position to next line
00137         glBitmap(0,0,0,0,0,-_vStep,NULL);
00138 
00139         return true;
00140     }
00141     else
00142         return false;
00143 }
00144 
00146 
00147 // Create variable-length header for font data
00148 
00149 bool 
00150 GltFontAscii::makeHeader
00151 (
00152     string &header,
00153     const int            width,
00154     const int            height
00155 )
00156 {
00157     // 11 characters is big enough for integer 2^32 + \0
00158 
00159     char buffer[5+11+11];
00160     sprintf(buffer,"GLTF %u %u",width,height);
00161     header = buffer;
00162     header += '\0';
00163 
00164     return true;
00165 }
00166 
00167 // Decode variable-length header for texture data
00168 
00169 void *
00170 GltFontAscii::getHeader
00171 (
00172     const void * const data,
00173     int            &width,
00174     int            &height
00175 )
00176 {
00177     const char * const h = (const char * const) data;
00178     
00179     if (h[0]=='G' && h[1]=='L' && h[2]=='T' && h[3]=='F' && h[4]==' ')
00180     {
00181         if (sscanf(h+5,"%i %i",&width,&height)==2)
00182             return (void *) (h + strlen(h) + 1);
00183     }
00184 
00185     return NULL;
00186 }

Generated on Tue Nov 5 11:11:03 2002 for GLT by doxygen1.2.18