1 // File: OpenGl_Display_1.cxx
2 // Created: 25 October 2011
3 // Author: Sergey ZERCHANINOV
4 // Copyright: OPEN CASCADE 2011
6 #include <OpenGl_Display.hxx>
8 #include <TCollection_AsciiString.hxx>
9 #include <TCollection_HAsciiString.hxx>
11 #include <OpenGl_FontMgr.hxx>
13 #include <OpenGl_AspectText.hxx>
19 /*-----------------------------------------------------------------------------*/
21 * Prototypes variables statiques
26 const char * EnumName;
27 const char * FontName;
28 OSD_FontAspect FontAspect;
31 static const FontMapNode myFontMap[] =
36 { "Courier" , "Courier New" , OSD_FA_Regular },
37 { "Times-Roman" , "Times New Roman", OSD_FA_Regular },
38 { "Times-Bold" , "Times New Roman", OSD_FA_Bold },
39 { "Times-Italic" , "Times New Roman", OSD_FA_Italic },
40 { "Times-BoldItalic" , "Times New Roman", OSD_FA_BoldItalic },
41 { "ZapfChancery-MediumItalic", "Script" , OSD_FA_Regular },
42 { "Symbol" , "Symbol" , OSD_FA_Regular },
43 { "ZapfDingbats" , "WingDings" , OSD_FA_Regular },
44 { "Rock" , "Arial" , OSD_FA_Regular },
45 { "Iris" , "Lucida Console" , OSD_FA_Regular }
49 { "Courier" , "Courier" , OSD_FA_Regular },
50 { "Times-Roman" , "Times" , OSD_FA_Regular },
51 { "Times-Bold" , "Times" , OSD_FA_Bold },
52 { "Times-Italic" , "Times" , OSD_FA_Italic },
53 { "Times-BoldItalic" , "Times" , OSD_FA_BoldItalic },
54 { "Arial" , "Helvetica" , OSD_FA_Regular },
55 { "ZapfChancery-MediumItalic", "-adobe-itc zapf chancery-medium-i-normal--*-*-*-*-*-*-iso8859-1" , OSD_FA_Regular },
56 { "Symbol" , "-adobe-symbol-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , OSD_FA_Regular },
57 { "ZapfDingbats" , "-adobe-itc zapf dingbats-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , OSD_FA_Regular },
58 { "Rock" , "-sgi-rock-medium-r-normal--*-*-*-*-p-*-iso8859-1" , OSD_FA_Regular },
59 { "Iris" , "--iris-medium-r-normal--*-*-*-*-m-*-iso8859-1" , OSD_FA_Regular }
64 #define NUM_FONT_ENTRIES (sizeof(myFontMap)/sizeof(FontMapNode))
66 /*-----------------------------------------------------------------------------*/
73 void OpenGl_Display::getGL2PSFontName (const char *src_font, char *ps_font)
76 Convert font name used for rendering to some "good" font names
77 that produce good vector text
79 static char const *family[] = {"Helvetica", "Courier", "Times"};
80 static char const *italic[] = {"Oblique", "Oblique", "Italic"};
81 static char const *base[] = {"", "", "-Roman"};
87 if( strstr( src_font, "Symbol" ) ){
88 sprintf( ps_font, "%s", "Symbol" );
92 if( strstr( src_font, "ZapfDingbats" ) ){
93 sprintf( ps_font, "%s", "WingDings" );
97 if ( strstr( src_font, "Courier" ) ){
100 else if ( strstr( src_font, "Times" ) ){
104 if ( strstr( src_font, "Bold" ) ){
108 if ( strstr( src_font, "Italic" ) || strstr( src_font, "Oblique" ) ){
113 sprintf( ps_font, "%s-%s", family[font], "Bold");
115 sprintf(ps_font, "%s%s", ps_font, italic[font]);
120 sprintf( ps_font, "%s-%s", family[font], italic[font] );
124 sprintf( ps_font, "%s%s", family[font], base[font] );
129 /*-----------------------------------------------------------------------------*/
132 * Fonctions publiques
135 /*-----------------------------------------------------------------------------*/
137 int OpenGl_Display::FindFont (const char* AFontName, const OSD_FontAspect AFontAspect,
138 const int ABestSize, const float AXScale, const float AYScale)
144 myFontSize = ABestSize;
146 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
148 Handle(TCollection_HAsciiString) family_name = new TCollection_HAsciiString(AFontName);
149 myFont = mgr->request_font( family_name, AFontAspect, myFontSize );
153 //try to use font names mapping
154 FontMapNode newTempFont = myFontMap[0];
155 for ( int i = 0; i < NUM_FONT_ENTRIES; ++i )
157 if ( TCollection_AsciiString(myFontMap[i].EnumName).IsEqual( family_name->ToCString() ) )
159 newTempFont = myFontMap[i];
163 family_name = new TCollection_HAsciiString(newTempFont.FontName);
164 myFont = mgr->request_font( family_name, newTempFont.FontAspect, myFontSize );
167 // Requested family name not found -> serach for any font family with given aspect and height
170 family_name = new TCollection_HAsciiString( "" );
171 myFont = mgr->request_font( family_name, AFontAspect, myFontSize );
174 // The last resort: trying to use ANY font available in the system
177 myFont = mgr->request_font( family_name, OSD_FA_Undefined, -1 );
181 mgr->setCurrentScale( AXScale, AYScale );
186 /*-----------------------------------------------------------------------------*/
188 void OpenGl_Display::StringSize (const wchar_t *str, int &width, int &ascent, int &descent)
194 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
195 const FTFont* font = mgr->fontById( myFont );
197 width = int( mgr->computeWidth( myFont, str ) );
198 ascent = int( font->Ascender() );
199 descent = int( font->Descender() );
204 /*-----------------------------------------------------------------------------*/
206 void OpenGl_Display::RenderText (const wchar_t* str, const int is2d, const float x, const float y, const float z,
207 const OpenGl_AspectText *aspect, const OpenGl_TextParam *param)
209 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
210 const FTFont* fnt = mgr->fontById( myFont );
214 // FTFont changes texture state when it renders and computes size for the text
215 glPushAttrib(GL_TEXTURE_BIT);
217 int widthFont, ascentFont, descentFont;
218 StringSize( str, widthFont, ascentFont, descentFont );
221 switch (param->HAlign)
223 case Graphic3d_HTA_CENTER:
224 xdis = -0.5 * (GLdouble)widthFont;
226 case Graphic3d_HTA_RIGHT:
227 xdis = -(GLdouble)widthFont;
229 //case Graphic3d_HTA_LEFT:
233 switch (param->VAlign)
235 case Graphic3d_VTA_CENTER:
236 ydis = -0.5 * (GLdouble)ascentFont - descentFont;
238 case Graphic3d_VTA_TOP:
239 ydis = -(GLdouble)ascentFont - descentFont;
241 //case Graphic3d_VTA_BOTTOM:
247 glMatrixMode(GL_MODELVIEW);
252 glTranslatef(x, y, 0.f);
253 glRotatef( 180, 1, 0, 0 );
257 const GLdouble identityMatrix[4][4] =
265 GLdouble projMatrix[4][4], modelMatrix[4][4];
272 glGetDoublev( GL_MODELVIEW_MATRIX, (GLdouble*)modelMatrix );
273 glGetDoublev( GL_PROJECTION_MATRIX, (GLdouble*)projMatrix );
274 glGetIntegerv( GL_VIEWPORT, (GLint*)viewport );
277 (GLdouble*)modelMatrix,
278 (GLdouble*)projMatrix,
282 gluUnProject( wx, wy, wz,
283 (GLdouble*)identityMatrix, (GLdouble*)projMatrix, (GLint*)viewport,
286 GLdouble h = (GLdouble)fnt->FaceSize();
288 gluUnProject( wx, wy + h - 1., wz,
289 (GLdouble*)identityMatrix, (GLdouble*)projMatrix, (GLint*)viewport,
294 glTranslated( x1, y1 , z1 );
295 glRotated(aspect->Angle(), 0, 0, 1);
296 glTranslated(xdis, ydis, 0);
298 if( !aspect->IsZoomable() )
304 export_h = (float )h;
309 glGetIntegerv(GL_RENDER_MODE, &renderMode);
310 if ( renderMode == GL_FEEDBACK )
313 export_h = (GLdouble)fnt->FaceSize() / export_h;
315 ExportText( str, is2d, x, y, z, aspect, param, export_h );
320 mgr->render_text( myFont, str, is2d );
326 /*-----------------------------------------------------------------------------*/
328 static const int alignmentforgl2ps[3][3] = { {5,2,8}, {4,1,7}, {6,3,9} };
330 void OpenGl_Display::ExportText (const wchar_t* text, const int is2d, const float x, const float y, const float z,
331 const OpenGl_AspectText *aspect, const OpenGl_TextParam *param, const float height)
336 switch (param->HAlign)
338 case Graphic3d_HTA_CENTER: vh = 2; break;
339 case Graphic3d_HTA_RIGHT: vh = 3; break;
340 //case Graphic3d_HTA_LEFT:
345 switch (param->VAlign)
347 case Graphic3d_VTA_CENTER: vv = 2; break;
348 case Graphic3d_VTA_TOP: vv = 3; break;
349 //case Graphic3d_VTA_BOTTOM:
353 const int alignment = alignmentforgl2ps[vh-1][vv-1];
355 const char* fontname = aspect->Font();
356 float angle = aspect->Angle();
361 getGL2PSFontName(fontname, ps_font);
364 glRasterPos2f( x, y );
366 glRasterPos3f( x, y, z );
368 glBitmap( 1, 1, 0, 0, 0, 0, &zero );
370 //szv: workaround for gl2ps!
371 const int len = 4 * (wcslen(text) + 1); //szv: should be more than enough
372 char *astr = new char[len];
373 wcstombs(astr,text,len);
374 gl2psTextOpt(astr, ps_font, height, alignment, angle);