bitmapFont.cpp

00001 
00002 /* Copyright (c) 2008-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 // HACK: Get rid of deprecated warning for aglUseFont
00019 //   -Wno-deprecated-declarations would do as well, but here it is more isolated
00020 #ifdef AGL
00021 #  include <AvailabilityMacros.h>
00022 #  undef DEPRECATED_ATTRIBUTE
00023 #  define DEPRECATED_ATTRIBUTE
00024 #endif
00025 
00026 #include "bitmapFont.h"
00027 
00028 #include <eq/client/pipe.h>
00029 #include <eq/client/window.h>
00030 
00031 #ifdef WGL
00032 #  include <eq/client/wglWindow.h>
00033 #endif
00034 #ifdef AGL
00035 #  include <eq/client/aglWindow.h>
00036 #endif
00037 #ifdef GLX
00038 #  include <eq/client/glXPipe.h>
00039 #endif
00040 
00041 using namespace std;
00042 
00043 namespace eq
00044 {
00045 namespace util
00046 {
00047 const string BitmapFont::normal( "EQ_UTIL_BITMAPFONT_NORMAL" );
00048 
00049 BitmapFont::BitmapFont( Window* window )
00050         : _window( window )
00051         , _lists ( Window::ObjectManager::INVALID )
00052 {
00053     EQASSERT( window );
00054 }
00055 
00056 
00057 BitmapFont::~BitmapFont()
00058 {
00059 }
00060 
00061 bool BitmapFont::initFont( const std::string& name, const uint32_t size )
00062 {
00063     const Pipe* pipe = _window->getPipe();
00064     switch( pipe->getWindowSystem( ))
00065     {
00066         case WINDOW_SYSTEM_GLX:
00067             return _initFontGLX( name, size );
00068         case WINDOW_SYSTEM_WGL:
00069             return _initFontWGL( name, size );
00070         case WINDOW_SYSTEM_AGL:
00071             return _initFontAGL( name, size );
00072         default:
00073             return false;
00074     }
00075 }
00076 
00077 bool BitmapFont::_initFontGLX( const std::string& name,
00078                                const uint32_t size )
00079 {
00080 #ifdef GLX
00081     Pipe*    pipe    = _window->getPipe();
00082     EQASSERT( pipe );
00083     EQASSERT( pipe->getOSPipe( ));
00084     EQASSERT( dynamic_cast< const GLXPipe* >( pipe->getOSPipe( )));
00085     const GLXPipe* osPipe = static_cast< const GLXPipe* >( pipe->getOSPipe( ));
00086 
00087     Display* display = osPipe->getXDisplay();
00088     EQASSERT( display );
00089 
00090     // see xfontsel
00091     stringstream font;
00092     font << "-*-";
00093 
00094     if( name == normal )
00095         font << "times";
00096     else
00097         font << name;
00098     font << "-*-r-*-*-" << size << "-*-*-*-*-*-*-*";
00099 
00100     XFontStruct* fontStruct = XLoadQueryFont( display, font.str().c_str( )); 
00101     if( !fontStruct )
00102     {
00103         EQWARN << "Can't load font " << font.str() << ", using fixed" << endl;
00104         fontStruct = XLoadQueryFont( display, "fixed" ); 
00105     }
00106 
00107     EQASSERT( fontStruct );
00108 
00109     _setupLists( 127 );
00110     glXUseXFont( fontStruct->fid, 0, 127, _lists );
00111 
00112     XFreeFont( display, fontStruct );
00113     return true;
00114 #else
00115     return false;
00116 #endif
00117 }
00118 
00119 bool BitmapFont::_initFontWGL( const std::string& name,
00120                                const uint32_t size )
00121 {
00122 #ifdef WGL
00123     const OSWindow*    osWindow  = _window->getOSWindow();
00124     const WGLWindowIF* wglWindow = dynamic_cast< const WGLWindowIF* >(osWindow);
00125 
00126     if( !wglWindow )
00127     {
00128         EQWARN << "Window does not use a WGL window" << endl;
00129         return false;
00130     }
00131 
00132     HDC dc = wglWindow->getWGLDC();
00133     if( !dc )
00134     {
00135         EQWARN << "WGL window does not have a device context" << endl;
00136         return false;
00137     }
00138 
00139     LOGFONT font;
00140     memset( &font, 0, sizeof( font ));
00141     font.lfHeight = -static_cast< LONG >( size );
00142     font.lfWeight = FW_NORMAL;
00143     font.lfCharSet = ANSI_CHARSET;
00144     font.lfOutPrecision = OUT_DEFAULT_PRECIS;
00145     font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
00146     font.lfQuality = DEFAULT_QUALITY;
00147     font.lfPitchAndFamily = FF_DONTCARE | DEFAULT_QUALITY;
00148 
00149     if( name == normal )
00150         strncpy( font.lfFaceName, "Times New Roman", LF_FACESIZE );
00151     else
00152         strncpy( font.lfFaceName, name.c_str(), LF_FACESIZE );
00153 
00154     font.lfFaceName[ LF_FACESIZE-1 ] = '\0';
00155 
00156     HFONT newFont = CreateFontIndirect( &font );
00157     if( !newFont )
00158     {
00159         EQWARN << "Can't load font " << name << ", using Times New Roman" 
00160                << endl;
00161 
00162         strncpy( font.lfFaceName, "Times New Roman", LF_FACESIZE );
00163         newFont = CreateFontIndirect( &font );
00164     }
00165     EQASSERT( newFont );
00166 
00167     HFONT oldFont = static_cast< HFONT >( SelectObject( dc, newFont ));
00168 
00169     _setupLists( 256 );
00170     const bool ret = wglUseFontBitmaps( dc, 0 , 255, _lists );
00171     
00172     SelectObject( dc, oldFont );
00173     //DeleteObject( newFont );
00174     
00175     if( !ret )
00176         _setupLists( 0 );
00177 
00178     return ret;
00179 #else
00180     return false;
00181 #endif
00182 }
00183 
00184 bool BitmapFont::_initFontAGL( const std::string& name,
00185                                const uint32_t size )
00186 {
00187 #ifdef AGL
00188     const OSWindow*    osWindow  = _window->getOSWindow();
00189     const AGLWindowIF* aglWindow = dynamic_cast< const AGLWindowIF* >(osWindow);
00190 
00191     if( !aglWindow )
00192     {
00193         EQWARN << "Window does not use an AGL window" << endl;
00194         return false;
00195     }
00196 
00197     AGLContext context = aglWindow->getAGLContext();
00198     EQASSERT( context );
00199 
00200     CFStringRef cfFontName;
00201     if( name == normal )
00202         cfFontName = CFStringCreateWithCString( kCFAllocatorDefault, "Georgia",
00203                                                 kCFStringEncodingMacRoman );
00204     else
00205         cfFontName = 
00206             CFStringCreateWithCString( kCFAllocatorDefault, name.c_str(),
00207                                    kCFStringEncodingMacRoman );
00208 
00209     ATSFontFamilyRef font = ATSFontFamilyFindFromName( cfFontName, 
00210                                                        kATSOptionFlagsDefault );
00211     CFRelease( cfFontName );
00212 
00213     if( font == 0 )
00214     {
00215         EQWARN << "Can't load font " << name << ", using Georgia" << endl;
00216         cfFontName = 
00217             CFStringCreateWithCString( kCFAllocatorDefault, "Georgia",
00218                                        kCFStringEncodingMacRoman );
00219 
00220         font = ATSFontFamilyFindFromName( cfFontName, kATSOptionFlagsDefault );
00221         CFRelease( cfFontName );
00222     }
00223     EQASSERT( font );
00224 
00225     _setupLists( 127 );
00226     if( !aglUseFont( context, font, ::normal, size, 0, 256, (long)_lists ))
00227     {
00228         _setupLists( 0 );
00229         return false;
00230     }
00231     
00232     return true;
00233 #else
00234     return false;
00235 #endif
00236 }
00237 
00238 void BitmapFont::_setupLists( const GLsizei num )
00239 {
00240     Window::ObjectManager* gl = _window->getObjectManager();
00241     EQASSERT( gl );
00242 
00243     if( _lists != Window::ObjectManager::INVALID )
00244         gl->deleteList( this );
00245 
00246     if( num == 0 )
00247         _lists = Window::ObjectManager::INVALID;
00248     else
00249     {
00250         _lists = gl->newList( this, num );
00251         EQASSERT( _lists != Window::ObjectManager::INVALID );
00252     }
00253 }
00254 
00255 void BitmapFont::draw( const std::string& text ) const
00256 {
00257     const Pipe* pipe = _window->getPipe();
00258     switch( pipe->getWindowSystem( ))
00259     {
00260         case WINDOW_SYSTEM_GLX:
00261         case WINDOW_SYSTEM_AGL:
00262         case WINDOW_SYSTEM_WGL:
00263             if( _lists != Window::ObjectManager::INVALID )
00264             {
00265                 glListBase( _lists );
00266                 glCallLists( text.size(), GL_UNSIGNED_BYTE, text.c_str( ));
00267                 glListBase( 0 );
00268             }
00269             break;
00270 
00271         default:
00272             EQUNIMPLEMENTED;
00273     }
00274 }
00275 
00276 }
00277 }
Generated on Mon Aug 10 18:58:31 2009 for Equalizer 0.9 by  doxygen 1.5.8