7 #include <TCollection_AsciiString.hxx>
8 #include <TCollection_HAsciiString.hxx>
10 #include <Standard_Stream.hxx>
12 #include <OpenGl_FontMgr.hxx>
13 #include <OpenGl_tgl_funcs.hxx>
14 #include <OpenGl_TextRender.hxx>
15 #include <OpenGl_telem_attri.hxx>
16 #include <OpenGl_cmn_varargs.hxx>
18 #include <OSD_Environment.hxx>
19 #include <Quantity_NameOfColor.hxx>
20 #include <AlienImage.hxx>
27 /*-----------------------------------------------------------------------------*/
29 * Prototypes variables statiques
32 int OpenGl_TextRender::curFont = -1;
33 int OpenGl_TextRender::curSize = -1;
34 int OpenGl_TextRender::curScale = -1;
35 int OpenGl_TextRender::curTexFont = -1;
37 OpenGl_TextRender::FontMapNode OpenGl_TextRender::fontMap[] = {
41 { "Courier" , "Courier New" , OSD_FA_Regular },
42 { "Times-Roman" , "Times New Roman", OSD_FA_Regular },
43 { "Times-Bold" , "Times New Roman", OSD_FA_Bold },
44 { "Times-Italic" , "Times New Roman", OSD_FA_Italic },
45 { "Times-BoldItalic" , "Times New Roman", OSD_FA_BoldItalic },
46 { "ZapfChancery-MediumItalic", "Script" , OSD_FA_Regular },
47 { "Symbol" , "Symbol" , OSD_FA_Regular },
48 { "ZapfDingbats" , "WingDings" , OSD_FA_Regular },
49 { "Rock" , "Arial" , OSD_FA_Regular },
50 { "Iris" , "Lucida Console" , OSD_FA_Regular }
54 { "Courier" , "Courier" , OSD_FA_Regular },
55 { "Times-Roman" , "Times" , OSD_FA_Regular },
56 { "Times-Bold" , "Times" , OSD_FA_Bold },
57 { "Times-Italic" , "Times" , OSD_FA_Italic },
58 { "Times-BoldItalic" , "Times" , OSD_FA_BoldItalic },
59 { "Arial" , "Helvetica" , OSD_FA_Regular },
60 { "ZapfChancery-MediumItalic", "-adobe-itc zapf chancery-medium-i-normal--*-*-*-*-*-*-iso8859-1" , OSD_FA_Regular },
61 { "Symbol" , "-adobe-symbol-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , OSD_FA_Regular },
62 { "ZapfDingbats" , "-adobe-itc zapf dingbats-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , OSD_FA_Regular },
63 { "Rock" , "-sgi-rock-medium-r-normal--*-*-*-*-p-*-iso8859-1" , OSD_FA_Regular },
64 { "Iris" , "--iris-medium-r-normal--*-*-*-*-m-*-iso8859-1" , OSD_FA_Regular }
69 OpenGl_TextRender::FontEntry OpenGl_TextRender::fontEntry[] = {
71 { "Courier" , "Courier New" },
72 { "Times-Roman" , "Times New Roman" },
73 { "Times-Bold" , "Times New Roman Bold" },
74 { "Times-Italic" , "Times New Roman Italic" },
75 { "Times-BoldItalic" , "Times New Roman Bold Italic" },
76 { "ZapfChancery-MediumItalic", "Script" },
77 { "Symbol" , "Symbol" },
78 { "ZapfDingbats" , "WingDings" },
80 { "Iris" , "Lucida Console" }
84 #define NUM_FONT_ENTRIES (sizeof(fontMap)/sizeof(FontMapNode))
86 /*-----------------------------------------------------------------------------*/
93 void OpenGl_TextRender::getGL2PSFontName(char *src_font, char *ps_font)
96 Convert font name used for rendering to some "good" font names
97 that produce good vector text
99 static char const *family[] = {"Helvetica", "Courier", "Times"};
100 static char const *italic[] = {"Oblique", "Oblique", "Italic"};
101 static char const *base[] = {"", "", "-Roman"};
108 if( strstr( src_font, "Symbol" ) ){
109 sprintf( ps_font, "%s", "Symbol" );
113 if( strstr( src_font, "ZapfDingbats" ) ){
114 sprintf( ps_font, "%s", "WingDings" );
118 if ( strstr( src_font, "Courier" ) ){
121 else if ( strstr( src_font, "Times" ) ){
125 if ( strstr( src_font, "Bold" ) ){
129 if ( strstr( src_font, "Italic" ) || strstr( src_font, "Oblique" ) ){
134 sprintf( ps_font, "%s-%s", family[font], "Bold");
136 sprintf(ps_font, "%s%s", ps_font, italic[font]);
141 sprintf( ps_font, "%s-%s", family[font], italic[font] );
145 sprintf( ps_font, "%s%s", family[font], base[font] );
150 /*-----------------------------------------------------------------------------*/
156 OpenGl_TextRender::OpenGl_TextRender()
157 : _CurrentFontId(-1),
159 _YCurrentScale(1.f) {
164 /*-----------------------------------------------------------------------------*/
167 * Fonctions publiques
170 OpenGl_TextRender* OpenGl_TextRender::instance() {
171 static OpenGl_TextRender* _textRend = NULL;
172 if ( _textRend == NULL )
174 _textRend = new OpenGl_TextRender();
181 /*----------------------------------------------------------------------*/
182 OpenGl_TextRender::FontMapNode OpenGl_TextRender::searchFontInMap( Handle(TCollection_HAsciiString)& fontName ) {
183 for ( int i = 0; i < NUM_FONT_ENTRIES; ++i )
185 TCollection_AsciiString compare_String(fontMap[i].enumName) ;
186 if(compare_String.IsEqual( fontName->ToCString() ))
191 //default font returns
195 /*-----------------------------------------------------------------------------*/
197 Tint OpenGl_TextRender::FindFont ( Tchar* fontName,
198 OSD_FontAspect aspect,
205 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
207 Handle(TCollection_HAsciiString) family_name
208 = new TCollection_HAsciiString((char*)fontName);
210 curFont = mgr->request_font(family_name,
212 Standard_Integer(bestSize) );
214 if( curFont == -1 ) {
215 //try to use font names mapping
216 FontMapNode newTempFont = searchFontInMap ( family_name );
217 curFont = mgr->request_font( new TCollection_HAsciiString(newTempFont.FontName),
218 newTempFont.fontAspect,
219 Standard_Integer(bestSize) );
222 // Requested family name not found -> serach for any font family with given aspect and height
223 family_name = new TCollection_HAsciiString( "" );
224 if ( curFont == -1 ) {
225 curFont = mgr->request_font(family_name, aspect, Standard_Integer(bestSize) );
228 // The last resort: trying to use ANY font available in the system
229 if ( curFont == -1 ) {
230 curFont = mgr->request_font(family_name, OSD_FA_Undefined, -1 );
234 mgr->setCurrentScale( xScale, yScale );
239 /*-----------------------------------------------------------------------------*/
240 void OpenGl_TextRender::StringSize(char *str, GLint *Width, GLint *Ascent, GLint *Descent)
243 /* int dir, asc, des;*/
248 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
249 const FTFont* font = mgr->fontById( curFont );
251 *Width = GLint( mgr->computeWidth( curFont, str ) );
252 *Ascent = GLint( font->Ascender() );
253 *Descent = GLint( font->Descender() );
257 printf("sizeString::asc = %d des = %d width = %d \n", *Ascent, *Descent, *Width);
262 /*-----------------------------------------------------------------------------*/
264 void OpenGl_TextRender::RenderText ( char* str, GLuint base, int is2d, GLfloat x, GLfloat y, GLfloat z )
266 GLdouble projMatrix[4][4], modelMatrix[4][4];
268 GLint widthFont, ascentFont, descentFont;
269 GLdouble xdis = 0., ydis = 0.;
272 StringSize(str, &widthFont, &ascentFont, &descentFont );
274 GLdouble identityMatrix[4][4] =
282 TEL_ALIGN_DATA align;
285 key.id = TelTextAlign;
286 key.data.pdata = &align;
287 TsmGetAttri( 1, &key );
291 keyAngle.id = TelTextAngle;//This flag responding about Angle text
292 TsmGetAttri( 1, &keyAngle );
293 angle = keyAngle.data.ldata;
297 keyZoom.id = TelTextZoomable;//This flag responding about Zoomable text
298 TsmGetAttri( 1, &keyZoom );
299 zoom = keyZoom.data.ldata;
301 OpenGl_TextRender* textRender = OpenGl_TextRender::instance();
305 switch( align.Hmode )
307 case CALL_PHIGS_HOR_LEFT://0
310 case CALL_PHIGS_HOR_CENTER://1
311 xdis = -(GLdouble)widthFont / 2.0; vh = 2;
313 case CALL_PHIGS_HOR_RIGHT://2
314 xdis = -(GLdouble)widthFont; vh = 3;
320 switch( align.Vmode )
322 case CALL_PHIGS_VERT_BOTTOM://0
325 case CALL_PHIGS_VERT_CENTER://1
326 ydis = -(GLdouble)(ascentFont) / 2.0 - descentFont; vv = 2;
328 case CALL_PHIGS_VERT_TOP://2
329 ydis = -(GLdouble)ascentFont - descentFont; vv= 3;
335 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
337 const FTFont* fnt = mgr->fontById( curFont );
343 glMatrixMode(GL_MODELVIEW);
347 glTranslatef(x, y, 0.f);
348 glRotatef( 180, 1, 0, 0 );
355 glGetDoublev( GL_MODELVIEW_MATRIX, (GLdouble*)modelMatrix );
356 glGetDoublev( GL_PROJECTION_MATRIX, (GLdouble*)projMatrix );
357 glGetIntegerv( GL_VIEWPORT, (GLint*)viewport );
360 (GLdouble*)modelMatrix,
361 (GLdouble*)projMatrix,
365 gluUnProject( wx, wy, wz,
366 (GLdouble*)identityMatrix, (GLdouble*)projMatrix, (GLint*)viewport,
369 GLdouble h = (GLdouble)fnt->FaceSize();
372 gluUnProject( wx, wy + h - 1., wz,
373 (GLdouble*)identityMatrix, (GLdouble*)projMatrix, (GLint*)viewport,
378 glTranslated( x1, y1 , z1 );
379 glRotated(angle, 0, 0, 1);
380 glTranslated(xdis, ydis, 0);
391 glGetIntegerv(GL_RENDER_MODE, &renderMode);
392 if ( renderMode == GL_FEEDBACK )
396 keyfontName.id = TelTextFont;//This flag responding about TextFontName
397 TsmGetAttri( 1, &keyfontName );
398 char *fontName = new char[strlen((char*)keyfontName.data.pdata) + 1];
399 strcpy(fontName,(char*)keyfontName.data.pdata);
401 export_h = (GLdouble)fnt->FaceSize() / export_h;
402 int aligment = alignmentforgl2ps( vh, vv );
404 ExportText( str, fontName, export_h, angle, aligment, x, y, z, is2d!=0 );
410 mgr->render_text( curFont, str );
418 int OpenGl_TextRender::alignmentforgl2ps(int vh, int vy)
420 if( vh == 1 && vy == 1)
424 if( vh == 2 && vy == 1)
428 if( vh == 3 && vy == 1)
433 if( vh == 1 && vy == 2)
437 if( vh == 2 && vy == 2)
441 if( vh == 3 && vy == 2)
446 if( vh == 1 && vy == 3)
450 if( vh == 2 && vy == 3)
454 if( vh == 3 && vy == 3)
462 /*-----------------------------------------------------------------------------*/
463 void OpenGl_TextRender::ExportText( char* str, char* fontname, GLfloat height, GLfloat angle, GLint alignment,
464 GLfloat x, GLfloat y, GLfloat z, GLboolean is2d )
471 getGL2PSFontName(fontname, ps_font);
474 glRasterPos2f( x, y );
476 glRasterPos3f( x, y, z );
478 glBitmap( 1, 1, 0, 0, 0, 0, &zero );
480 gl2psTextOpt( str, ps_font, height, alignment, angle);