00001
00002
00003
00004
00005
00006
00007 #ifdef AGL
00008 # include <AvailabilityMacros.h>
00009 # undef DEPRECATED_ATTRIBUTE
00010 # define DEPRECATED_ATTRIBUTE
00011 #endif
00012
00013 #include "bitmapFont.h"
00014
00015 #include <eq/client/window.h>
00016
00017 #ifdef WGL
00018 # include <eq/client/wglWindow.h>
00019 #endif
00020 #ifdef AGL
00021 # include <eq/client/aglWindow.h>
00022 #endif
00023
00024 using namespace std;
00025
00026 namespace eq
00027 {
00028 namespace util
00029 {
00030 const string BitmapFont::normal( "EQ_UTIL_BITMAPFONT_NORMAL" );
00031
00032 BitmapFont::BitmapFont( Window* window )
00033 : _window( window )
00034 , _lists ( Window::ObjectManager::FAILED )
00035 {
00036 EQASSERT( window );
00037 }
00038
00039
00040 BitmapFont::~BitmapFont()
00041 {
00042 }
00043
00044 bool BitmapFont::initFont( const std::string& fontName )
00045 {
00046 const Pipe* pipe = _window->getPipe();
00047 switch( pipe->getWindowSystem( ))
00048 {
00049 case WINDOW_SYSTEM_GLX:
00050 return _initFontGLX( fontName );
00051 case WINDOW_SYSTEM_WGL:
00052 return _initFontWGL( fontName );
00053 case WINDOW_SYSTEM_AGL:
00054 return _initFontAGL( fontName );
00055 default:
00056 return false;
00057 }
00058 }
00059
00060 bool BitmapFont::_initFontGLX( const std::string& fontName )
00061 {
00062 #ifdef GLX
00063 Pipe* pipe = _window->getPipe();
00064 Display* display = pipe->getXDisplay();
00065 EQASSERT( display );
00066
00067 string font = fontName;
00068 if( font == normal )
00069 font = "-*-times-*-r-*-*-12-*-*-*-*-*-*-*";
00070
00071 XFontStruct* fontStruct = XLoadQueryFont( display, font.c_str( ));
00072 if( !fontStruct )
00073 {
00074 EQWARN << "Can't load font " << font << ", using fixed" << endl;
00075 fontStruct = XLoadQueryFont( display, "fixed" );
00076 }
00077
00078 EQASSERT( fontStruct );
00079
00080 _setupLists( 127 );
00081 glXUseXFont( fontStruct->fid, 0, 127, _lists );
00082
00083 XFreeFont( display, fontStruct );
00084 return true;
00085 #else
00086 return false;
00087 #endif
00088 }
00089
00090 bool BitmapFont::_initFontWGL( const std::string& fontName )
00091 {
00092 #ifdef WGL
00093 const OSWindow* osWindow = _window->getOSWindow();
00094 const WGLWindowIF* wglWindow = dynamic_cast< const WGLWindowIF* >(osWindow);
00095
00096 if( !wglWindow )
00097 {
00098 EQWARN << "Window does not use a WGL window" << endl;
00099 return false;
00100 }
00101
00102 HDC dc = wglWindow->getWGLDC();
00103 if( !dc )
00104 {
00105 EQWARN << "WGL window does not have a device context" << endl;
00106 return false;
00107 }
00108
00109 LOGFONT font;
00110 memset( &font, 0, sizeof( font ));
00111 font.lfHeight = -12;
00112 font.lfWeight = FW_NORMAL;
00113 font.lfCharSet = ANSI_CHARSET;
00114 font.lfOutPrecision = OUT_DEFAULT_PRECIS;
00115 font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
00116 font.lfQuality = DEFAULT_QUALITY;
00117 font.lfPitchAndFamily = FF_DONTCARE | DEFAULT_QUALITY;
00118
00119 if( fontName == normal )
00120 strncpy( font.lfFaceName, "Times New Roman", LF_FACESIZE );
00121 else
00122 strncpy( font.lfFaceName, fontName.c_str(), LF_FACESIZE );
00123 font.lfFaceName[ LF_FACESIZE-1 ] = '\0';
00124
00125 HFONT newFont = CreateFontIndirect( &font );
00126 if( !newFont )
00127 {
00128 EQWARN << "Can't load font " << fontName << ", using Times New Roman"
00129 << endl;
00130
00131 strncpy( font.lfFaceName, "Times New Roman", LF_FACESIZE );
00132 newFont = CreateFontIndirect( &font );
00133 }
00134 EQASSERT( newFont );
00135
00136 HFONT oldFont = static_cast< HFONT >( SelectObject( dc, newFont ));
00137
00138 _setupLists( 256 );
00139 const bool ret = wglUseFontBitmaps( dc, 0 , 255, _lists );
00140
00141 SelectObject( dc, oldFont );
00142
00143
00144 if( !ret )
00145 _setupLists( 0 );
00146
00147 return ret;
00148 #else
00149 return false;
00150 #endif
00151 }
00152
00153 bool BitmapFont::_initFontAGL( const std::string& fontName )
00154 {
00155 #ifdef AGL
00156 const OSWindow* osWindow = _window->getOSWindow();
00157 const AGLWindowIF* aglWindow = dynamic_cast< const AGLWindowIF* >(osWindow);
00158
00159 if( !aglWindow )
00160 {
00161 EQWARN << "Window does not use an AGL window" << endl;
00162 return false;
00163 }
00164
00165 AGLContext context = aglWindow->getAGLContext();
00166 EQASSERT( context );
00167
00168 CFStringRef cfFontName;
00169 if( fontName == normal )
00170 cfFontName = CFStringCreateWithCString( kCFAllocatorDefault, "Georgia",
00171 kCFStringEncodingMacRoman );
00172 else
00173 cfFontName =
00174 CFStringCreateWithCString( kCFAllocatorDefault, fontName.c_str(),
00175 kCFStringEncodingMacRoman );
00176
00177 ATSFontFamilyRef font = ATSFontFamilyFindFromName( cfFontName,
00178 kATSOptionFlagsDefault );
00179 CFRelease( cfFontName );
00180
00181 if( font == 0 )
00182 {
00183 EQWARN << "Can't load font " << fontName << ", using Georgia" << endl;
00184 cfFontName =
00185 CFStringCreateWithCString( kCFAllocatorDefault, "Georgia",
00186 kCFStringEncodingMacRoman );
00187
00188 font = ATSFontFamilyFindFromName( cfFontName, kATSOptionFlagsDefault );
00189 CFRelease( cfFontName );
00190 }
00191 EQASSERT( font );
00192
00193 _setupLists( 127 );
00194 if( !aglUseFont( context, font, ::normal, 12, 0, 256, (long)_lists ))
00195 {
00196 _setupLists( 0 );
00197 return false;
00198 }
00199
00200 return true;
00201 #else
00202 return false;
00203 #endif
00204 }
00205
00206 void BitmapFont::_setupLists( const GLsizei num )
00207 {
00208 Window::ObjectManager* gl = _window->getObjectManager();
00209 EQASSERT( gl );
00210
00211 if( _lists != Window::ObjectManager::FAILED )
00212 gl->deleteList( this );
00213
00214 if( num == 0 )
00215 _lists = Window::ObjectManager::FAILED;
00216 else
00217 {
00218 _lists = gl->newList( this, num );
00219 EQASSERT( _lists != Window::ObjectManager::FAILED );
00220 }
00221 }
00222
00223 void BitmapFont::draw( const std::string& text ) const
00224 {
00225 const Pipe* pipe = _window->getPipe();
00226 switch( pipe->getWindowSystem( ))
00227 {
00228 case WINDOW_SYSTEM_GLX:
00229 case WINDOW_SYSTEM_AGL:
00230 case WINDOW_SYSTEM_WGL:
00231 if( _lists != Window::ObjectManager::FAILED )
00232 {
00233 glListBase( _lists );
00234 glCallLists( text.size(), GL_UNSIGNED_BYTE, text.c_str( ));
00235 glListBase( 0 );
00236 }
00237 break;
00238
00239 default:
00240 EQUNIMPLEMENTED;
00241 }
00242 }
00243
00244 }
00245 }