1 #include <OpenGl_FontMgr.hxx>
3 #include <FTGLTextureFont.h>
6 #include <Standard_Stream.hxx>
10 #define DEFAULT_FONT_HEIGHT 16
14 void dump_texture( int id);
17 public NCollection_DataMap< Handle(TCollection_HAsciiString),
18 Handle(TCollection_HAsciiString)>
22 (const Standard_Integer NbBuckets = 1,
23 const Handle(NCollection_BaseAllocator)& theAllocator = 0L)
24 : NCollection_DataMap<Handle(TCollection_HAsciiString),
25 Handle(TCollection_HAsciiString)> (NbBuckets, theAllocator)
28 inline Font_DataMap (const Font_DataMap& theOther)
29 : NCollection_DataMap<Handle(TCollection_HAsciiString),
30 Handle(TCollection_HAsciiString)> (theOther)
32 friend Standard_Boolean IsEqual( const Handle(TCollection_HAsciiString)& h1,
33 const Handle(TCollection_HAsciiString)& h2 );
35 inline Standard_Boolean
36 IsEqual( const Handle(TCollection_HAsciiString)& h1,
37 const Handle(TCollection_HAsciiString)& h2 )
39 return (!h1->IsLess(h2) && !h1->IsGreater(h2));
42 OpenGl_FontMgr::OpenGl_FontMgr()
49 OpenGl_FontMgr* OpenGl_FontMgr::instance()
51 static OpenGl_FontMgr* _mgr = NULL;
54 _mgr = new OpenGl_FontMgr();
55 _mgr->_initializeFontDB();
60 void OpenGl_FontMgr::_initializeFontDB()
62 Handle(OSD_FontMgr) fntMgr = OSD_FontMgr::GetInstance();
63 if ( !fntMgr.IsNull() ) {
65 OSD_NListOfSystemFont fontList = fntMgr->GetAvalableFonts();
66 if ( fontList.Size() != 0 ) {
68 OSD_NListOfSystemFont::Iterator it(fontList);
69 for ( ; it.More(); it.Next() ) {
70 OGLFont_SysInfo* info = new OGLFont_SysInfo();
71 if ( it.Value()->FontAspect() == OSD_FA_Regular ) {
72 //this workaround for fonts with names dependent on system locale.
73 //for example: "Times New Roman Fett Kursive" or "Times New Roman Gras Italiqui"
74 FTFace face(it.Value()->FontPath()->ToCString());
76 if ( face.Error() == FT_Err_Ok ) {
77 if ( (*face.Face())->style_flags == 0 ) {
78 info->SysFont = it.Value();
81 //new aspect detected for current font item
83 cout << "TKOpenGl::initializeFontDB() detected new font!\n"
84 << "\tFont Previous Name: " << it.Value()->FontName()->ToCString() << endl
85 << "\tFont New Name: " << (*face.Face())->family_name << endl
86 << "\tFont Aspect: " << (*face.Face())->style_flags << endl;
88 OSD_FontAspect aspect = OSD_FA_Regular;
89 if ( (*face.Face())->style_flags == (FT_STYLE_FLAG_ITALIC | FT_STYLE_FLAG_BOLD) )
90 aspect = OSD_FA_BoldItalic;
91 else if ( (*face.Face())->style_flags == FT_STYLE_FLAG_ITALIC )
92 aspect = OSD_FA_Italic;
93 else if ( (*face.Face())->style_flags == FT_STYLE_FLAG_BOLD )
97 cout << "\tOSD_FontAspect: " << aspect << endl;
99 Handle(TCollection_HAsciiString) aFontName =
100 new TCollection_HAsciiString( (*face.Face())->family_name );
101 info->SysFont = new OSD_SystemFont( aFontName, aspect, it.Value()->FontPath() );
107 info->SysFont = it.Value();
109 _FontDB.Append(info);
116 if ( !_FontDB.Size() ) {
117 cout << "\nTKOpenGl::initializeFontDB() FAILED!!!\n"
118 << "No fonts detected in the system!\n"
119 << "Text rendering in 3D viewer will not be available." << endl;
124 bool OpenGl_FontMgr::requestFontList( Graphic3d_NListOfHAsciiString& lst)
126 FontDBIt DBit(_FontDB);
128 for ( ; DBit.More(); DBit.Next() ) {
129 lst.Append( DBit.Value()->SysFont->FontName() );
134 // Empty fontName means that ANY family name can be used.
135 // fontAspect == OSD_FA_Undefined means ANY font aspect is acceptable.
136 // fontheight == -1 means ANY font height is acceptable.
137 int OpenGl_FontMgr::request_font( const Handle(TCollection_HAsciiString)& fontName,
138 const OSD_FontAspect fontAspect,
139 const Standard_Integer fontHeight )
141 Standard_Integer aFontHeight = fontHeight;
142 if ( aFontHeight < 2 && aFontHeight != -1 )
145 cout << "TKOpenGl::request_font\n"
146 << " Error: font height is invalid!!!\n"
147 << " font height:" << aFontHeight << "\n";
153 cout << "TKOpenGl::request_font\n"
154 << " font name: " << fontName->ToCString() << endl
155 << " font aspect: " << fontAspect << endl
156 << " font height: " << aFontHeight << endl;
159 GLCONTEXT ctx = GET_GL_CONTEXT();
162 FontDBIt DBit(_FontDB);
165 cout << "Searching font in font database...\n";
168 for ( ; DBit.More(); DBit.Next() ) {
169 // san (23.07.2010): comparing font names in case-insensitive mode,
170 // as on SUN and SGI system font names are in lower-case
171 if ( fontName->IsEmpty() || DBit.Value()->SysFont->FontName()->IsSameString(fontName, Standard_False) ) {
173 cout << "\tName is found...\n\tCheking aspect...\n";
176 //check for font aspect
177 if (fontAspect != OSD_FA_Undefined && DBit.Value()->SysFont->FontAspect() != fontAspect) {
179 cout << "\tAspect of candidate font: " << DBit.Value()->SysFont->FontAspect() << endl;
180 cout << "\tAspects are not equal! Continue seaching...\n";
186 cout << "\tAspect is found...\n\tCheking height...\n";
188 //check for fixed height
189 if (DBit.Value()->SysFont->FontHeight() != -1) {
191 cout << "\tChecking fixed height: " << DBit.Value()->SysFont->FontHeight() << endl;
194 if ( aFontHeight != -1 && DBit.Value()->SysFont->FontHeight() != aFontHeight ){
196 cout << "\tHeights are not equal! Continue seaching...\n";
202 // We need to remember the font height to be used
203 if ( aFontHeight == -1 )
204 aFontHeight = DBit.Value()->SysFont->FontHeight();
206 cout << "\tHeight is found\n";
212 // If font height is not specified -> use DEFAULT_FONT_HEIGHT for variable-height fonts
213 if ( aFontHeight == -1 )
214 aFontHeight = DEFAULT_FONT_HEIGHT;
216 cout << "\tFont has variable height == -1. height is found\n";
220 cout << "\tChecking font manager cache...\n";
222 //check in loaded fonts
223 IDList::Iterator fIt(DBit.Value()->GeneratedFonts);
224 for ( ; fIt.More(); fIt.Next() ) {
225 OGLFont_Cache cache = _FontCache.Find( fIt.Value() );
226 if ( cache.FontHeight == aFontHeight && cache.GlContext == ctx ) {
227 //requested font is generated already
230 cout << "\tRequested font is generated already\n\tId = " ;
231 cout << fIt.Value() << endl;
233 _CurrentFontId = fIt.Value();
234 return _CurrentFontId;
237 cout << "\t\tHeights or contexts are not equal:\n"
238 << "\t\t\tfont height: " << aFontHeight << "\tchache height: " << cache.FontHeight << endl
239 << "\t\t\tfont context: " << ctx << "\tchache context: " << cache.GlContext << endl;
240 cout << "\t\tContinue searching in cache...\n";
245 cout << "\tRequested font is not found among genarated fonts.\n\tCreating new...\n";
247 //if not found in generated fonts
249 FTGLTextureFont* font = new FTGLTextureFont(DBit.Value()->SysFont->FontPath()->ToCString());
250 //by default creates regular font
251 if ( ! font || font->Error() != FT_Err_Ok) {
253 cout << "\t\tError during creation FTGL font object!\n";
258 if ( ! font->FaceSize( aFontHeight) || font->Error() != FT_Err_Ok ){
260 cout << "\t\tError during setup FTGL font height!\n";
264 font->UseDisplayList( false );
269 cache.FontHeight = aFontHeight;
270 cache.GlContext = ctx;
272 _CurrentFontId =_FontCache.Size() + 1;
273 _FontCache.Bind( _CurrentFontId, cache );
274 DBit.Value()->GeneratedFonts.Append(_CurrentFontId);
277 cout << "TKOpenGl::Loaded New FTGL font:\n"
278 << " font name: " << fontName->ToCString() << "\n"
284 cout << "OSD_FA_Bold\n";
286 case OSD_FA_BoldItalic:
287 cout << "OSD_FA_BoldItalic\n";
290 cout << "OSD_FA_Italic\n";
293 cout << "OSD_FA_Regular\n";
296 cout << "OSD_FA_Undefined\n";
299 cout << " font height: "<<aFontHeight<<"\n";
300 cout << " font id: " << _CurrentFontId << "\n";
303 return _CurrentFontId;
306 //invalid family name
308 cout << "\n---Invalid Family Name!!!---\n";
313 void OpenGl_FontMgr::render_text( const Standard_Integer id, const wchar_t* text,
314 const Standard_Boolean is2d )
317 cout << "TKOpenGl::render_text\n"
318 << "\tfont id = " << id << endl
319 << "\ttext = " << text << endl;
321 if ( text && _FontCache.IsBound( id ) ) {
322 glMatrixMode( GL_MODELVIEW );
325 glScalef( _XCurrentScale, _YCurrentScale, 1 );
326 glPushAttrib( GL_ENABLE_BIT );
328 GLboolean enableTexture = glIsEnabled(GL_TEXTURE_2D);
329 GLboolean enableDepthTest = glIsEnabled(GL_DEPTH_TEST);
332 glEnable(GL_TEXTURE_2D);
334 if ( !enableDepthTest )
335 glEnable(GL_DEPTH_TEST);
337 else if ( enableDepthTest ) {
338 glDisable(GL_DEPTH_TEST);
342 glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, ¶m);
344 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
345 glAlphaFunc(GL_GEQUAL, 0.285f);
346 glEnable(GL_ALPHA_TEST);
347 OGLFont_Cache cache = _FontCache.Find( id );
348 cache.Font->Render( text );
350 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, param);
353 glDisable(GL_TEXTURE_2D);
354 if( !enableDepthTest )
355 glDisable(GL_DEPTH_TEST);
358 glMatrixMode( GL_MODELVIEW );
364 void OpenGl_FontMgr::render_text ( const wchar_t* text, const Standard_Boolean is2d )
366 render_text( _CurrentFontId, text, is2d );
369 const FTFont* OpenGl_FontMgr::fontById (const Standard_Integer id)
371 return _FontCache.IsBound( id ) ? _FontCache.Find( id ).Font: NULL;
374 Standard_ShortReal OpenGl_FontMgr::computeWidth( const Standard_Integer id, const wchar_t* text )
376 if( !_FontCache.IsBound( id ) )
379 OGLFont_Cache cache = _FontCache.Find( id );
381 Standard_ShortReal w = cache.Font->Advance( text );
386 void OpenGl_FontMgr::setCurrentScale (const Standard_ShortReal xScale,
387 const Standard_ShortReal yScale)
389 _XCurrentScale = xScale;
390 _YCurrentScale = yScale;
393 #include <AlienImage_BMPAlienData.hxx>
394 #include <OSD_File.hxx>
395 #include <OSD_Protection.hxx>
396 #include <Aspect_GenericColorMap.hxx>
397 #include <Image_ColorImage.hxx>
398 #include <Quantity_Color.hxx>
400 void dump_texture( int id)
402 Handle(AlienImage_BMPAlienData) image = new AlienImage_BMPAlienData();
404 if (!glIsTexture(id))
407 unsigned char* pixels = new unsigned char[8192*1024];
408 memset( pixels, 0, 8192*1024 );
409 static int index = 0;
412 glBindTexture(GL_TEXTURE_2D, id );
413 glGetTexImage( GL_TEXTURE_2D ,
419 Handle(Image_ColorImage) anImage = new Image_ColorImage( 0, 0, 1024, 8192 );
421 Aspect_ColorPixel mark( Quantity_Color (0.,0.5,1., Quantity_TOC_RGB ) ),
422 space( Quantity_Color (1.,1.,1., Quantity_TOC_RGB ) );
424 for( int i = 0; i < 1024; i++ ) {
425 for (int j = 0; j < 8192; j++ )
426 if (pixels[i*8192+j]>0) {
427 anImage->SetPixel( anImage->LowerX()+i,
432 anImage->SetPixel( anImage->LowerX()+i,
438 image->FromImage( anImage );
439 TCollection_AsciiString name( index );
440 name.Prepend( "D:\\Temp_image" );
442 OSD_File file ( name );
443 file.Build( OSD_WriteOnly, OSD_Protection());