1 // Created on: 2011-10-25
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <InterfaceGraphic.hxx>
22 #include <OpenGl_Display.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_HAsciiString.hxx>
27 #include <OpenGl_FontMgr.hxx>
28 #include <OpenGl_PrinterContext.hxx>
29 #include <OpenGl_AspectText.hxx>
39 /*-----------------------------------------------------------------------------*/
41 * Prototypes variables statiques
46 const char * EnumName;
47 const char * FontName;
48 OSD_FontAspect FontAspect;
51 static const FontMapNode myFontMap[] =
56 { "Courier" , "Courier New" , OSD_FA_Regular },
57 { "Times-Roman" , "Times New Roman", OSD_FA_Regular },
58 { "Times-Bold" , "Times New Roman", OSD_FA_Bold },
59 { "Times-Italic" , "Times New Roman", OSD_FA_Italic },
60 { "Times-BoldItalic" , "Times New Roman", OSD_FA_BoldItalic },
61 { "ZapfChancery-MediumItalic", "Script" , OSD_FA_Regular },
62 { "Symbol" , "Symbol" , OSD_FA_Regular },
63 { "ZapfDingbats" , "WingDings" , OSD_FA_Regular },
64 { "Rock" , "Arial" , OSD_FA_Regular },
65 { "Iris" , "Lucida Console" , OSD_FA_Regular }
69 { "Courier" , "Courier" , OSD_FA_Regular },
70 { "Times-Roman" , "Times" , OSD_FA_Regular },
71 { "Times-Bold" , "Times" , OSD_FA_Bold },
72 { "Times-Italic" , "Times" , OSD_FA_Italic },
73 { "Times-BoldItalic" , "Times" , OSD_FA_BoldItalic },
74 { "Arial" , "Helvetica" , OSD_FA_Regular },
75 { "ZapfChancery-MediumItalic", "-adobe-itc zapf chancery-medium-i-normal--*-*-*-*-*-*-iso8859-1" , OSD_FA_Regular },
76 { "Symbol" , "-adobe-symbol-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , OSD_FA_Regular },
77 { "ZapfDingbats" , "-adobe-itc zapf dingbats-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , OSD_FA_Regular },
78 { "Rock" , "-sgi-rock-medium-r-normal--*-*-*-*-p-*-iso8859-1" , OSD_FA_Regular },
79 { "Iris" , "--iris-medium-r-normal--*-*-*-*-m-*-iso8859-1" , OSD_FA_Regular }
84 #define NUM_FONT_ENTRIES (sizeof(myFontMap)/sizeof(FontMapNode))
86 /*-----------------------------------------------------------------------------*/
93 void OpenGl_Display::getGL2PSFontName (const 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"};
107 if( strstr( src_font, "Symbol" ) ){
108 sprintf( ps_font, "%s", "Symbol" );
112 if( strstr( src_font, "ZapfDingbats" ) ){
113 sprintf( ps_font, "%s", "WingDings" );
117 if ( strstr( src_font, "Courier" ) ){
120 else if ( strstr( src_font, "Times" ) ){
124 if ( strstr( src_font, "Bold" ) ){
128 if ( strstr( src_font, "Italic" ) || strstr( src_font, "Oblique" ) ){
133 sprintf( ps_font, "%s-%s", family[font], "Bold");
135 sprintf(ps_font, "%s%s", ps_font, italic[font]);
140 sprintf( ps_font, "%s-%s", family[font], italic[font] );
144 sprintf( ps_font, "%s%s", family[font], base[font] );
149 /*-----------------------------------------------------------------------------*/
152 * Fonctions publiques
155 /*-----------------------------------------------------------------------------*/
157 int OpenGl_Display::FindFont (const char* AFontName, const OSD_FontAspect AFontAspect,
158 const int ABestSize, const float AXScale, const float AYScale)
164 myFontSize = ABestSize;
166 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
168 Handle(TCollection_HAsciiString) family_name = new TCollection_HAsciiString(AFontName);
169 myFont = mgr->request_font( family_name, AFontAspect, myFontSize );
173 //try to use font names mapping
174 FontMapNode newTempFont = myFontMap[0];
175 for ( int i = 0; i < NUM_FONT_ENTRIES; ++i )
177 if ( TCollection_AsciiString(myFontMap[i].EnumName).IsEqual( family_name->ToCString() ) )
179 newTempFont = myFontMap[i];
183 family_name = new TCollection_HAsciiString(newTempFont.FontName);
184 myFont = mgr->request_font( family_name, newTempFont.FontAspect, myFontSize );
187 // Requested family name not found -> serach for any font family with given aspect and height
190 family_name = new TCollection_HAsciiString( "" );
191 myFont = mgr->request_font( family_name, AFontAspect, myFontSize );
194 // The last resort: trying to use ANY font available in the system
197 myFont = mgr->request_font( family_name, OSD_FA_Undefined, -1 );
201 mgr->setCurrentScale( AXScale, AYScale );
206 /*-----------------------------------------------------------------------------*/
208 void OpenGl_Display::StringSize (const wchar_t *str, int &width, int &ascent, int &descent)
214 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
215 const FTFont* font = mgr->fontById( myFont );
217 width = int( mgr->computeWidth( myFont, str ) );
218 ascent = int( font->Ascender() );
219 descent = int( font->Descender() );
225 Class : MultilineTextRenderer
226 Description : Class for constructing text and multi-line text
229 class MultilineTextRenderer
233 Standard_Integer myLFNum; // Number of '\n' (Line Feed) '\x00\x0A'
234 Standard_Integer myCurrPos; // Position of the current substring
235 Standard_Integer myNewStrLen; // Length of the new string
236 Standard_Integer mySubstrNum; // Number of substrings
237 wchar_t *myNewStr; // New string
238 const wchar_t *myStrPtr; // Pointer to the original string
242 // ----------------------------------------------
243 // Function: MultilineTextRenderer
244 // Purpose: Constructor with initialization
245 // ----------------------------------------------
246 MultilineTextRenderer ( const wchar_t *theStr,
249 const OpenGl_TextParam *theParam,
250 const FTFont *theFnt,
253 GLint theDescentFont ) :
260 myStrPtr (&theStr[0])
262 const Standard_Integer aStrLen = wcslen(theStr); // Length of the original string
263 Standard_Integer aNextCRChar = 0; // Character after '\r' (Carriage Return) '\x00\x0D'
264 Standard_Integer aHTNum = 0; // Number of '\t' (Horizontal Tabulation) '\x00\x09'
265 Standard_Integer aDumpNum = 0; // Number of '\a', '\b', '\v' and '\f'
266 Standard_Integer anAllSpaces = 0; // Number of spaces instead of all '\t' characters
267 Standard_Integer aMaxSubstrLen = 0; // Length of the largest substring in the new string
269 Standard_Integer aTimeVar = 0;
271 // Calculation index after last '\r' character
272 for ( Standard_Integer anIndex = 0; anIndex < aStrLen; ++anIndex )
274 if ( theStr[anIndex] == '\r' ) aNextCRChar = anIndex+1;
277 // Calculation numbers of the some control characters
278 for (Standard_Integer anIndex = aNextCRChar; anIndex < aStrLen; anIndex++)
281 switch ( theStr[anIndex] )
296 anAllSpaces += ( 8 - ( aTimeVar - 1 )%8 );
302 // Calculation length of the new string
303 myStrPtr += aNextCRChar;
304 myNewStrLen = aStrLen - aNextCRChar + anAllSpaces - aHTNum - aDumpNum;
306 myNewStr = new wchar_t[myNewStrLen + 1];
307 myNewStr[myNewStrLen]='\0';
309 CalcString (aStrLen, aMaxSubstrLen);
310 CalcHAlign (theXdis, theParam, theWidthFont, aStrLen, aMaxSubstrLen);
311 CalcVAlign (theYdis, theParam, theFnt, theAscentFont, theDescentFont);
314 // ----------------------------------------------
315 // Function: ~MultilineTextRenderer
316 // Purpose: Default destructor
317 // ----------------------------------------------
318 ~MultilineTextRenderer ()
323 // ----------------------------------------------
325 // Purpose: Calculate position of the next substring
326 // ----------------------------------------------
329 for ( Standard_Integer anIndex = 0; anIndex <= myNewStrLen; ++anIndex )
331 if ( myNewStr[myCurrPos + anIndex] == '\0' )
334 myCurrPos += anIndex + 1;
340 // ----------------------------------------------
342 // Purpose: Calculate position of the next substring
343 // ----------------------------------------------
344 Standard_Boolean More ()
346 if ( mySubstrNum <= myLFNum ) return Standard_True;
347 else return Standard_False;
350 // ----------------------------------------------
351 // Function: GetValue
352 // Purpose: Returns current substring
353 // ----------------------------------------------
356 return ( myNewStr + myCurrPos );
361 // ----------------------------------------------
362 // Function: CalcString
363 // Purpose: Calculate new string separated by '\0' without '\n', '\t', '\b', '\v', '\f', '\a'
364 // ----------------------------------------------
365 void CalcString ( const Standard_Integer theStrLen, Standard_Integer &theMaxSubstrLen )
372 for ( Standard_Integer anIndex1 = 0, anIndex2 = 0;
373 (anIndex1 < theStrLen)&&(anIndex2 < myNewStrLen);
374 ++anIndex1, ++anIndex2 )
378 if ( *(myStrPtr + anIndex1) == '\n' ) aTimeVar = 0;
380 while ( (*(myStrPtr + anIndex1)=='\b')||(*(myStrPtr + anIndex1)=='\f')
381 ||(*(myStrPtr + anIndex1)=='\v')||(*(myStrPtr + anIndex1)=='\a') )
386 if ( *(myStrPtr + anIndex1) == '\t' )
388 aSpacesNum = ( 8 - ( aTimeVar - 1 )%8 );
390 for ( aHelpIndex = 0; aHelpIndex < aSpacesNum; aHelpIndex++ )
392 myNewStr[anIndex2 + aHelpIndex] = ' ';
394 anIndex2 += aHelpIndex - 1;
399 myNewStr[anIndex2] = *(myStrPtr + anIndex1);
403 // After this part of code we will have a string separated by '\0' characters
404 Standard_Integer aHelpLength = 0;
408 for( Standard_Integer anIndex = 0; anIndex <= myNewStrLen; ++anIndex )
410 if ( myNewStr[anIndex] == '\n' )
412 myNewStr[anIndex] = '\0';
415 // Calculating length of the largest substring of the new string.
416 // It's needed for horizontal alignment
417 if ( myNewStr[anIndex] != '\0' )
423 if ( aHelpLength > theMaxSubstrLen ) theMaxSubstrLen = aHelpLength;
431 // ----------------------------------------------
432 // Function: CalcVAlign
433 // Purpose: Calculate vertical alignment for text
434 // ----------------------------------------------
435 void CalcVAlign ( GLdouble &theYdis,
436 const OpenGl_TextParam *theParam,
437 const FTFont *theFnt,
439 GLint theDescentFont )
441 switch (theParam->VAlign)
443 case Graphic3d_VTA_BOTTOM:
444 theYdis = (GLdouble)(myLFNum * theFnt->FaceSize());
446 case Graphic3d_VTA_CENTER:
447 if ( (myLFNum%2) == 0 ) // An odd number of strings
449 theYdis = (GLdouble)((myLFNum/2.0) * theFnt->FaceSize()) + theDescentFont;
451 else // An even number of strings
453 theYdis = (GLdouble)((myLFNum - 1)/2.0 * theFnt->FaceSize()) - theDescentFont/2.0;
456 case Graphic3d_VTA_TOP:
457 theYdis = -(GLdouble)theAscentFont - theDescentFont;
460 theYdis = (GLdouble)(myLFNum * theFnt->FaceSize());
465 // ----------------------------------------------
466 // Function: CalcHAlign
467 // Purpose: Calculate horizontal alignment for text
468 // ----------------------------------------------
469 void CalcHAlign ( GLdouble &theXdis,
470 const OpenGl_TextParam *theParam,
472 const Standard_Integer theStrLen,
473 Standard_Integer theMaxSubstrLen)
475 GLdouble aWidth = (GLdouble)(theMaxSubstrLen * theWidthFont)/theStrLen;
477 switch (theParam->HAlign)
479 case Graphic3d_HTA_LEFT:
482 case Graphic3d_HTA_CENTER:
483 theXdis = -0.5 * (GLdouble)aWidth;
485 case Graphic3d_HTA_RIGHT:
486 theXdis = -(GLdouble)aWidth;
495 /*-----------------------------------------------------------------------------*/
497 void OpenGl_Display::RenderText (const wchar_t* str, const int is2d, const float x, const float y, const float z,
498 const OpenGl_AspectText *aspect, const OpenGl_TextParam *param)
500 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
501 const FTFont* fnt = mgr->fontById( myFont );
505 // FTFont changes texture state when it renders and computes size for the text
506 glPushAttrib(GL_TEXTURE_BIT);
508 int widthFont, ascentFont, descentFont;
509 StringSize( str, widthFont, ascentFont, descentFont );
511 GLdouble xdis = 0., ydis = 0.;
515 MultilineTextRenderer aRenderer( str,
524 glMatrixMode(GL_MODELVIEW);
529 glTranslatef(x, y, 0.f);
530 glRotatef( 180, 1, 0, 0 );
534 const GLdouble identityMatrix[4][4] =
542 GLdouble projMatrix[4][4], modelMatrix[4][4];
549 glGetDoublev( GL_MODELVIEW_MATRIX, (GLdouble*)modelMatrix );
550 glGetDoublev( GL_PROJECTION_MATRIX, (GLdouble*)projMatrix );
551 glGetIntegerv( GL_VIEWPORT, (GLint*)viewport );
554 (GLdouble*)modelMatrix,
555 (GLdouble*)projMatrix,
559 gluUnProject( wx, wy, wz,
560 (GLdouble*)identityMatrix, (GLdouble*)projMatrix, (GLint*)viewport,
563 GLdouble h = (GLdouble)fnt->FaceSize();
565 gluUnProject( wx, wy + h - 1., wz,
566 (GLdouble*)identityMatrix, (GLdouble*)projMatrix, (GLint*)viewport,
571 glTranslated( x1, y1 , z1 );
572 glRotated(aspect->Angle(), 0, 0, 1);
573 glTranslated(xdis, ydis, 0);
575 if( !aspect->IsZoomable() )
578 // if the context has assigned printer context, use it's parameters
579 OpenGl_PrinterContext* aPrinterContext =
580 OpenGl_PrinterContext::GetPrinterContext( GET_GL_CONTEXT() );
581 if( aPrinterContext )
583 // get printing scaling in x and y dimensions
584 GLfloat aTextScalex = 1, aTextScaley = 1;
585 aPrinterContext->GetScale( aTextScalex, aTextScaley );
587 // text should be scaled in all directions with same
588 // factor to save its proportions, so use height (y) scaling
589 // as it is better for keeping text/3d graphics proportions
590 glScalef( aTextScaley, aTextScaley, aTextScaley );
602 glGetIntegerv(GL_RENDER_MODE, &renderMode);
603 if ( renderMode == GL_FEEDBACK )
606 export_h = (float)fnt->FaceSize() / export_h;
607 for ( ; aRenderer.More(); aRenderer.Next() )
609 // x, y, z coordinates are used here as zero values, because position of the text
610 // and alignment is calculated in the code above using glTranslated methods
611 ExportText( aRenderer.GetValue(), is2d, 0, 0, 0, aspect, param, (short)export_h );
612 glTranslated(0, -(GLdouble)fnt->FaceSize(), 0);
618 for ( ; aRenderer.More(); aRenderer.Next() )
620 mgr->render_text( myFont, aRenderer.GetValue(), is2d );
621 glTranslated(0, -(GLdouble)fnt->FaceSize(), 0);
628 /*-----------------------------------------------------------------------------*/
630 void OpenGl_Display::ExportText (const wchar_t* text, const int is2d, const float x, const float y, const float z,
631 const OpenGl_AspectText *aspect, const OpenGl_TextParam *param, const short height)
635 const char* fontname = aspect->Font();
636 float angle = aspect->Angle();
641 getGL2PSFontName(fontname, ps_font);
644 glRasterPos2f( x, y );
646 glRasterPos3f( x, y, z );
648 glBitmap( 1, 1, 0, 0, 0, 0, &zero );
650 //szv: workaround for gl2ps!
651 const int len = 4 * (wcslen(text) + 1); //szv: should be more than enough
652 char *astr = new char[len];
653 wcstombs(astr,text,len);
655 // Standard GL2PS's alignment isn't used, because it doesn't work correctly
656 // for all formats, therefore alignment is calculated manually relative
657 // to the bottom-left corner, which corresponds to the GL2PS_TEXT_BL value
658 gl2psTextOpt(astr, ps_font, height, GL2PS_TEXT_BL, angle);