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.
20 #include <OpenGl_GlCore11.hxx>
22 #include <InterfaceGraphic.hxx>
23 #include <OpenGl_Display.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TCollection_HAsciiString.hxx>
28 #include <OpenGl_FontMgr.hxx>
29 #include <OpenGl_PrinterContext.hxx>
30 #include <OpenGl_AspectText.hxx>
40 /*-----------------------------------------------------------------------------*/
42 * Prototypes variables statiques
47 const char * EnumName;
48 const char * FontName;
49 Font_FontAspect FontAspect;
52 static const FontMapNode myFontMap[] =
57 { "Courier" , "Courier New" , Font_FA_Regular },
58 { "Times-Roman" , "Times New Roman", Font_FA_Regular },
59 { "Times-Bold" , "Times New Roman", Font_FA_Bold },
60 { "Times-Italic" , "Times New Roman", Font_FA_Italic },
61 { "Times-BoldItalic" , "Times New Roman", Font_FA_BoldItalic },
62 { "ZapfChancery-MediumItalic", "Script" , Font_FA_Regular },
63 { "Symbol" , "Symbol" , Font_FA_Regular },
64 { "ZapfDingbats" , "WingDings" , Font_FA_Regular },
65 { "Rock" , "Arial" , Font_FA_Regular },
66 { "Iris" , "Lucida Console" , Font_FA_Regular }
70 { "Courier" , "Courier" , Font_FA_Regular },
71 { "Times-Roman" , "Times" , Font_FA_Regular },
72 { "Times-Bold" , "Times" , Font_FA_Bold },
73 { "Times-Italic" , "Times" , Font_FA_Italic },
74 { "Times-BoldItalic" , "Times" , Font_FA_BoldItalic },
75 { "Arial" , "Helvetica" , Font_FA_Regular },
76 { "ZapfChancery-MediumItalic", "-adobe-itc zapf chancery-medium-i-normal--*-*-*-*-*-*-iso8859-1" , Font_FA_Regular },
77 { "Symbol" , "-adobe-symbol-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , Font_FA_Regular },
78 { "ZapfDingbats" , "-adobe-itc zapf dingbats-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , Font_FA_Regular },
79 { "Rock" , "-sgi-rock-medium-r-normal--*-*-*-*-p-*-iso8859-1" , Font_FA_Regular },
80 { "Iris" , "--iris-medium-r-normal--*-*-*-*-m-*-iso8859-1" , Font_FA_Regular }
85 #define NUM_FONT_ENTRIES (sizeof(myFontMap)/sizeof(FontMapNode))
87 /*-----------------------------------------------------------------------------*/
94 void OpenGl_Display::getGL2PSFontName (const char *src_font, char *ps_font)
97 Convert font name used for rendering to some "good" font names
98 that produce good vector text
100 static char const *family[] = {"Helvetica", "Courier", "Times"};
101 static char const *italic[] = {"Oblique", "Oblique", "Italic"};
102 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 /*-----------------------------------------------------------------------------*/
153 * Fonctions publiques
156 /*-----------------------------------------------------------------------------*/
158 int OpenGl_Display::FindFont (const char* AFontName, const Font_FontAspect AFontAspect,
159 const int ABestSize, const float AXScale, const float AYScale)
165 myFontSize = ABestSize;
167 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
169 Handle(TCollection_HAsciiString) family_name = new TCollection_HAsciiString(AFontName);
170 myFont = mgr->request_font( family_name, AFontAspect, myFontSize );
174 //try to use font names mapping
175 FontMapNode newTempFont = myFontMap[0];
176 for ( int i = 0; i < NUM_FONT_ENTRIES; ++i )
178 if ( TCollection_AsciiString(myFontMap[i].EnumName).IsEqual( family_name->ToCString() ) )
180 newTempFont = myFontMap[i];
184 family_name = new TCollection_HAsciiString(newTempFont.FontName);
185 myFont = mgr->request_font( family_name, newTempFont.FontAspect, myFontSize );
188 // Requested family name not found -> serach for any font family with given aspect and height
191 family_name = new TCollection_HAsciiString( "" );
192 myFont = mgr->request_font( family_name, AFontAspect, myFontSize );
195 // The last resort: trying to use ANY font available in the system
198 myFont = mgr->request_font( family_name, Font_FA_Undefined, -1 );
202 mgr->setCurrentScale( AXScale, AYScale );
207 /*-----------------------------------------------------------------------------*/
209 void OpenGl_Display::StringSize (const wchar_t *str, int &width, int &ascent, int &descent)
215 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
216 const FTFont* font = mgr->fontById( myFont );
218 width = int( mgr->computeWidth( myFont, str ) );
219 ascent = int( font->Ascender() );
220 descent = int( font->Descender() );
226 Class : MultilineTextRenderer
227 Description : Class for constructing text and multi-line text
230 class MultilineTextRenderer
234 Standard_Integer myLFNum; // Number of '\n' (Line Feed) '\x00\x0A'
235 Standard_Integer myCurrPos; // Position of the current substring
236 Standard_Integer myNewStrLen; // Length of the new string
237 Standard_Integer mySubstrNum; // Number of substrings
238 wchar_t *myNewStr; // New string
239 const wchar_t *myStrPtr; // Pointer to the original string
243 // ----------------------------------------------
244 // Function: MultilineTextRenderer
245 // Purpose: Constructor with initialization
246 // ----------------------------------------------
247 MultilineTextRenderer ( const wchar_t *theStr,
250 const OpenGl_TextParam *theParam,
251 const FTFont *theFnt,
254 GLint theDescentFont ) :
261 myStrPtr (&theStr[0])
263 const Standard_Integer aStrLen = (Standard_Integer) wcslen(theStr); // Length of the original string
264 Standard_Integer aNextCRChar = 0; // Character after '\r' (Carriage Return) '\x00\x0D'
265 Standard_Integer aHTNum = 0; // Number of '\t' (Horizontal Tabulation) '\x00\x09'
266 Standard_Integer aDumpNum = 0; // Number of '\a', '\b', '\v' and '\f'
267 Standard_Integer anAllSpaces = 0; // Number of spaces instead of all '\t' characters
268 Standard_Integer aMaxSubstrLen = 0; // Length of the largest substring in the new string
270 Standard_Integer aTimeVar = 0;
272 // Calculation index after last '\r' character
273 for ( Standard_Integer anIndex = 0; anIndex < aStrLen; ++anIndex )
275 if ( theStr[anIndex] == '\r' ) aNextCRChar = anIndex+1;
278 // Calculation numbers of the some control characters
279 for (Standard_Integer anIndex = aNextCRChar; anIndex < aStrLen; anIndex++)
282 switch ( theStr[anIndex] )
297 anAllSpaces += ( 8 - ( aTimeVar - 1 )%8 );
303 // Calculation length of the new string
304 myStrPtr += aNextCRChar;
305 myNewStrLen = aStrLen - aNextCRChar + anAllSpaces - aHTNum - aDumpNum;
307 myNewStr = new wchar_t[myNewStrLen + 1];
308 myNewStr[myNewStrLen]='\0';
310 CalcString (aStrLen, aMaxSubstrLen);
311 CalcHAlign (theXdis, theParam, theWidthFont, aStrLen, aMaxSubstrLen);
312 CalcVAlign (theYdis, theParam, theFnt, theAscentFont, theDescentFont);
315 // ----------------------------------------------
316 // Function: ~MultilineTextRenderer
317 // Purpose: Default destructor
318 // ----------------------------------------------
319 ~MultilineTextRenderer ()
324 // ----------------------------------------------
326 // Purpose: Calculate position of the next substring
327 // ----------------------------------------------
330 for ( Standard_Integer anIndex = 0; anIndex <= myNewStrLen; ++anIndex )
332 if ( myNewStr[myCurrPos + anIndex] == '\0' )
335 myCurrPos += anIndex + 1;
341 // ----------------------------------------------
343 // Purpose: Calculate position of the next substring
344 // ----------------------------------------------
345 Standard_Boolean More ()
347 if ( mySubstrNum <= myLFNum ) return Standard_True;
348 else return Standard_False;
351 // ----------------------------------------------
352 // Function: GetValue
353 // Purpose: Returns current substring
354 // ----------------------------------------------
357 return ( myNewStr + myCurrPos );
362 // ----------------------------------------------
363 // Function: CalcString
364 // Purpose: Calculate new string separated by '\0' without '\n', '\t', '\b', '\v', '\f', '\a'
365 // ----------------------------------------------
366 void CalcString ( const Standard_Integer theStrLen, Standard_Integer &theMaxSubstrLen )
373 for ( Standard_Integer anIndex1 = 0, anIndex2 = 0;
374 (anIndex1 < theStrLen)&&(anIndex2 < myNewStrLen);
375 ++anIndex1, ++anIndex2 )
379 if ( *(myStrPtr + anIndex1) == '\n' ) aTimeVar = 0;
381 while ( (*(myStrPtr + anIndex1)=='\b')||(*(myStrPtr + anIndex1)=='\f')
382 ||(*(myStrPtr + anIndex1)=='\v')||(*(myStrPtr + anIndex1)=='\a') )
387 if ( *(myStrPtr + anIndex1) == '\t' )
389 aSpacesNum = ( 8 - ( aTimeVar - 1 )%8 );
391 for ( aHelpIndex = 0; aHelpIndex < aSpacesNum; aHelpIndex++ )
393 myNewStr[anIndex2 + aHelpIndex] = ' ';
395 anIndex2 += aHelpIndex - 1;
400 myNewStr[anIndex2] = *(myStrPtr + anIndex1);
404 // After this part of code we will have a string separated by '\0' characters
405 Standard_Integer aHelpLength = 0;
409 for( Standard_Integer anIndex = 0; anIndex <= myNewStrLen; ++anIndex )
411 if ( myNewStr[anIndex] == '\n' )
413 myNewStr[anIndex] = '\0';
416 // Calculating length of the largest substring of the new string.
417 // It's needed for horizontal alignment
418 if ( myNewStr[anIndex] != '\0' )
424 if ( aHelpLength > theMaxSubstrLen ) theMaxSubstrLen = aHelpLength;
432 // ----------------------------------------------
433 // Function: CalcVAlign
434 // Purpose: Calculate vertical alignment for text
435 // ----------------------------------------------
436 void CalcVAlign ( GLdouble &theYdis,
437 const OpenGl_TextParam *theParam,
438 const FTFont *theFnt,
440 GLint theDescentFont )
442 switch (theParam->VAlign)
444 case Graphic3d_VTA_BOTTOM:
445 theYdis = (GLdouble)(myLFNum * theFnt->FaceSize());
447 case Graphic3d_VTA_CENTER:
448 if ( (myLFNum%2) == 0 ) // An odd number of strings
450 theYdis = (GLdouble)((myLFNum/2.0) * theFnt->FaceSize()) + theDescentFont;
452 else // An even number of strings
454 theYdis = (GLdouble)((myLFNum - 1)/2.0 * theFnt->FaceSize()) - theDescentFont/2.0;
457 case Graphic3d_VTA_TOP:
458 theYdis = -(GLdouble)theAscentFont - theDescentFont;
461 theYdis = (GLdouble)(myLFNum * theFnt->FaceSize());
466 // ----------------------------------------------
467 // Function: CalcHAlign
468 // Purpose: Calculate horizontal alignment for text
469 // ----------------------------------------------
470 void CalcHAlign ( GLdouble &theXdis,
471 const OpenGl_TextParam *theParam,
473 const Standard_Integer theStrLen,
474 Standard_Integer theMaxSubstrLen)
476 GLdouble aWidth = (GLdouble)(theMaxSubstrLen * theWidthFont)/theStrLen;
478 switch (theParam->HAlign)
480 case Graphic3d_HTA_LEFT:
483 case Graphic3d_HTA_CENTER:
484 theXdis = -0.5 * (GLdouble)aWidth;
486 case Graphic3d_HTA_RIGHT:
487 theXdis = -(GLdouble)aWidth;
496 /*-----------------------------------------------------------------------------*/
498 void OpenGl_Display::RenderText (const wchar_t* str, const int is2d, const float x, const float y, const float z,
499 const OpenGl_AspectText *aspect, const OpenGl_TextParam *param)
501 OpenGl_FontMgr* mgr = OpenGl_FontMgr::instance();
502 const FTFont* fnt = mgr->fontById( myFont );
506 // FTFont changes texture state when it renders and computes size for the text
507 glPushAttrib(GL_TEXTURE_BIT);
509 int widthFont, ascentFont, descentFont;
510 StringSize( str, widthFont, ascentFont, descentFont );
512 GLdouble xdis = 0., ydis = 0.;
516 MultilineTextRenderer aRenderer( str,
525 glMatrixMode(GL_MODELVIEW);
530 glTranslatef(x, y, 0.f);
531 glRotatef( 180, 1, 0, 0 );
535 const GLdouble identityMatrix[4][4] =
543 GLdouble projMatrix[4][4], modelMatrix[4][4];
550 glGetDoublev( GL_MODELVIEW_MATRIX, (GLdouble*)modelMatrix );
551 glGetDoublev( GL_PROJECTION_MATRIX, (GLdouble*)projMatrix );
552 glGetIntegerv( GL_VIEWPORT, (GLint*)viewport );
555 (GLdouble*)modelMatrix,
556 (GLdouble*)projMatrix,
560 gluUnProject( wx, wy, wz,
561 (GLdouble*)identityMatrix, (GLdouble*)projMatrix, (GLint*)viewport,
564 GLdouble h = (GLdouble)fnt->FaceSize();
566 gluUnProject( wx, wy + h - 1., wz,
567 (GLdouble*)identityMatrix, (GLdouble*)projMatrix, (GLint*)viewport,
572 glTranslated( x1, y1 , z1 );
573 glRotated(aspect->Angle(), 0, 0, 1);
574 glTranslated(xdis, ydis, 0);
576 if( !aspect->IsZoomable() )
579 // if the context has assigned printer context, use it's parameters
580 OpenGl_PrinterContext* aPrinterContext =
581 OpenGl_PrinterContext::GetPrinterContext( GET_GL_CONTEXT() );
582 if( aPrinterContext )
584 // get printing scaling in x and y dimensions
585 GLfloat aTextScalex = 1, aTextScaley = 1;
586 aPrinterContext->GetScale( aTextScalex, aTextScaley );
588 // text should be scaled in all directions with same
589 // factor to save its proportions, so use height (y) scaling
590 // as it is better for keeping text/3d graphics proportions
591 glScalef( aTextScaley, aTextScaley, aTextScaley );
603 glGetIntegerv(GL_RENDER_MODE, &renderMode);
604 if ( renderMode == GL_FEEDBACK )
607 export_h = (float)fnt->FaceSize() / export_h;
608 for ( ; aRenderer.More(); aRenderer.Next() )
610 // x, y, z coordinates are used here as zero values, because position of the text
611 // and alignment is calculated in the code above using glTranslated methods
612 ExportText( aRenderer.GetValue(), is2d, 0, 0, 0, aspect, param, (short)export_h );
613 glTranslated(0, -(GLdouble)fnt->FaceSize(), 0);
619 for ( ; aRenderer.More(); aRenderer.Next() )
621 mgr->render_text( myFont, aRenderer.GetValue(), is2d );
622 glTranslated(0, -(GLdouble)fnt->FaceSize(), 0);
629 /*-----------------------------------------------------------------------------*/
631 void OpenGl_Display::ExportText (const wchar_t* text, const int is2d, const float x, const float y, const float z,
632 const OpenGl_AspectText *aspect, const OpenGl_TextParam *param, const short height)
636 const char* fontname = aspect->Font();
637 float angle = aspect->Angle();
642 getGL2PSFontName(fontname, ps_font);
645 glRasterPos2f( x, y );
647 glRasterPos3f( x, y, z );
649 glBitmap( 1, 1, 0, 0, 0, 0, &zero );
651 //szv: workaround for gl2ps!
652 const int len = 4 * (wcslen(text) + 1); //szv: should be more than enough
653 char *astr = new char[len];
654 wcstombs(astr,text,len);
656 // Standard GL2PS's alignment isn't used, because it doesn't work correctly
657 // for all formats, therefore alignment is calculated manually relative
658 // to the bottom-left corner, which corresponds to the GL2PS_TEXT_BL value
659 gl2psTextOpt(astr, ps_font, height, GL2PS_TEXT_BL, angle);