8f3f8a40da11b649c122584ca9fbefa30094adc1
[occt.git] / src / OpenGl / OpenGl_Text.cxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_AspectText.hxx>
17 #include <OpenGl_GlCore11.hxx>
18 #include <OpenGl_GraphicDriver.hxx>
19 #include <OpenGl_ShaderManager.hxx>
20 #include <OpenGl_ShaderProgram.hxx>
21 #include <OpenGl_ShaderStates.hxx>
22 #include <OpenGl_Text.hxx>
23 #include <OpenGl_Utils.hxx>
24 #include <OpenGl_Workspace.hxx>
25
26 #include <Font_FontMgr.hxx>
27 #include <TCollection_HAsciiString.hxx>
28
29 #ifdef HAVE_GL2PS
30   #include <gl2ps.h>
31 #endif
32
33 namespace
34 {
35   static const GLdouble THE_IDENTITY_MATRIX[16] =
36   {
37     1.0, 0.0, 0.0, 0.0,
38     0.0, 1.0, 0.0, 0.0,
39     0.0, 0.0, 1.0, 0.0,
40     0.0, 0.0, 0.0, 1.0
41   };
42
43 #ifdef HAVE_GL2PS
44   static char const* TheFamily[] = {"Helvetica", "Courier", "Times"};
45   static char const* TheItalic[] = {"Oblique",   "Oblique", "Italic"};
46   static char const* TheBase[]   = {"", "", "-Roman"};
47
48   //! Convert font name used for rendering to some "good" font names
49   //! that produce good vector text.
50   static void getGL2PSFontName (const char* theSrcFont,
51                                 char*       thePsFont)
52   {
53     if (strstr (theSrcFont, "Symbol"))
54     {
55       sprintf (thePsFont, "%s", "Symbol");
56       return;
57     }
58     else if (strstr (theSrcFont, "ZapfDingbats"))
59     {
60       sprintf (thePsFont, "%s", "WingDings");
61       return;
62     }
63
64     int  aFontId  = 0;
65     bool isBold   = false;
66     bool isItalic = false;
67     if (strstr (theSrcFont, "Courier"))
68     {
69       aFontId = 1;
70     }
71     else if (strstr (theSrcFont, "Times"))
72     {
73       aFontId = 2;
74     }
75
76     if (strstr (theSrcFont, "Bold"))
77     {
78       isBold = true;
79     }
80     if (strstr (theSrcFont, "Italic")
81      || strstr (theSrcFont, "Oblique"))
82     {
83       isItalic = true;
84     }
85
86     if (isBold)
87     {
88       if (isItalic)
89       {
90         sprintf (thePsFont, "%s-Bold%s", TheFamily[aFontId], TheItalic[aFontId]);
91       }
92       else
93       {
94         sprintf (thePsFont, "%s-Bold", TheFamily[aFontId]);
95       }
96     }
97     else if (isItalic)
98     {
99       sprintf (thePsFont, "%s-%s", TheFamily[aFontId], TheItalic[aFontId]);
100     }
101     else
102     {
103       sprintf (thePsFont, "%s%s", TheFamily[aFontId], TheBase[aFontId]);
104     }
105   }
106
107   static void exportText (const NCollection_String& theText,
108                           const Standard_Boolean    theIs2d,
109                           const OpenGl_AspectText&  theAspect,
110                           const Standard_Integer    theHeight)
111   {
112
113     char aPsFont[64];
114     getGL2PSFontName (theAspect.FontName().ToCString(), aPsFont);
115
116   #if !defined(GL_ES_VERSION_2_0)
117     if (theIs2d)
118     {
119       glRasterPos2f (0.0f, 0.0f);
120     }
121     else
122     {
123       glRasterPos3f (0.0f, 0.0f, 0.0f);
124     }
125
126     GLubyte aZero = 0;
127     glBitmap (1, 1, 0, 0, 0, 0, &aZero);
128   #endif
129
130     // Standard GL2PS's alignment isn't used, because it doesn't work correctly
131     // for all formats, therefore alignment is calculated manually relative
132     // to the bottom-left corner, which corresponds to the GL2PS_TEXT_BL value
133     gl2psTextOpt (theText.ToCString(), aPsFont, (GLshort)theHeight, GL2PS_TEXT_BL, theAspect.Angle());
134   }
135 #endif
136
137 };
138
139 // =======================================================================
140 // function : OpenGl_Text
141 // purpose  :
142 // =======================================================================
143 OpenGl_Text::OpenGl_Text()
144 : myWinX (0.0f),
145   myWinY (0.0f),
146   myWinZ (0.0f),
147   myScaleHeight (1.0f),
148   myPoint  (0.0f, 0.0f, 0.0f),
149   myIs2d   (false)
150 {
151   myParams.Height = 10;
152   myParams.HAlign = Graphic3d_HTA_LEFT;
153   myParams.VAlign = Graphic3d_VTA_BOTTOM;
154 }
155
156 // =======================================================================
157 // function : OpenGl_Text
158 // purpose  :
159 // =======================================================================
160 OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
161                           const OpenGl_Vec3&       thePoint,
162                           const OpenGl_TextParam&  theParams)
163 : myWinX (0.0f),
164   myWinY (0.0f),
165   myWinZ (0.0f),
166   myScaleHeight  (1.0f),
167   myExportHeight (1.0f),
168   myParams (theParams),
169   myString (theText),
170   myPoint  (thePoint),
171   myIs2d   (false)
172 {
173   //
174 }
175
176 // =======================================================================
177 // function : SetPosition
178 // purpose  :
179 // =======================================================================
180 void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint)
181 {
182   myPoint = thePoint;
183 }
184
185 // =======================================================================
186 // function : SetFontSize
187 // purpose  :
188 // =======================================================================
189 void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx,
190                                const Standard_Integer        theFontSize)
191 {
192   if (myParams.Height != theFontSize)
193   {
194     Release (theCtx.operator->());
195   }
196   myParams.Height = theFontSize;
197 }
198
199 // =======================================================================
200 // function : Init
201 // purpose  :
202 // =======================================================================
203 void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
204                         const Standard_Utf8Char*      theText,
205                         const OpenGl_Vec3&            thePoint)
206 {
207   releaseVbos (theCtx.operator->());
208   myIs2d   = false;
209   myPoint  = thePoint;
210   myString.FromUnicode (theText);
211 }
212
213 // =======================================================================
214 // function : Init
215 // purpose  :
216 // =======================================================================
217 void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
218                         const Standard_Utf8Char*      theText,
219                         const OpenGl_Vec3&            thePoint,
220                         const OpenGl_TextParam&       theParams)
221 {
222   if (myParams.Height != theParams.Height)
223   {
224     Release (theCtx.operator->());
225   }
226   else
227   {
228     releaseVbos (theCtx.operator->());
229   }
230   myIs2d   = false;
231   myParams = theParams;
232   myPoint  = thePoint;
233   myString.FromUnicode (theText);
234 }
235
236 // =======================================================================
237 // function : Init
238 // purpose  :
239 // =======================================================================
240 void OpenGl_Text::Init (const Handle(OpenGl_Context)&     theCtx,
241                         const TCollection_ExtendedString& theText,
242                         const OpenGl_Vec2&                thePoint,
243                         const OpenGl_TextParam&           theParams)
244 {
245   if (myParams.Height != theParams.Height)
246   {
247     Release (theCtx.operator->());
248   }
249   else
250   {
251     releaseVbos (theCtx.operator->());
252   }
253   myIs2d       = true;
254   myParams     = theParams;
255   myPoint.xy() = thePoint;
256   myPoint.z()  = 0.0f;
257   myString.FromUnicode ((Standard_Utf16Char* )theText.ToExtString());
258 }
259
260 // =======================================================================
261 // function : ~OpenGl_Text
262 // purpose  :
263 // =======================================================================
264 OpenGl_Text::~OpenGl_Text()
265 {
266   //
267 }
268
269 // =======================================================================
270 // function : releaseVbos
271 // purpose  :
272 // =======================================================================
273 void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx)
274 {
275   for (Standard_Integer anIter = 0; anIter < myVertsVbo.Length(); ++anIter)
276   {
277     Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.ChangeValue (anIter);
278     Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.ChangeValue (anIter);
279
280     if (theCtx)
281     {
282       theCtx->DelayedRelease (aVerts);
283       theCtx->DelayedRelease (aTCrds);
284     }
285     aVerts.Nullify();
286     aTCrds.Nullify();
287   }
288   myTextures.Clear();
289   myVertsVbo.Clear();
290   myTCrdsVbo.Clear();
291 }
292
293 // =======================================================================
294 // function : Release
295 // purpose  :
296 // =======================================================================
297 void OpenGl_Text::Release (OpenGl_Context* theCtx)
298 {
299   releaseVbos (theCtx);
300   if (!myFont.IsNull())
301   {
302     Handle(OpenGl_Context) aCtx = theCtx;
303     const TCollection_AsciiString aKey = myFont->ResourceKey();
304     myFont.Nullify();
305     if (aCtx)
306       aCtx->ReleaseResource (aKey, Standard_True);
307   }
308 }
309
310 // =======================================================================
311 // function : StringSize
312 // purpose  :
313 // =======================================================================
314 void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
315                               const NCollection_String&     theText,
316                               const OpenGl_AspectText&      theTextAspect,
317                               const OpenGl_TextParam&       theParams,
318                               Standard_ShortReal&           theWidth,
319                               Standard_ShortReal&           theAscent,
320                               Standard_ShortReal&           theDescent)
321 {
322   theWidth   = 0.0f;
323   theAscent  = 0.0f;
324   theDescent = 0.0f;
325   const TCollection_AsciiString aFontKey = FontKey (theTextAspect, theParams.Height);
326   Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, aFontKey);
327   if (aFont.IsNull() || !aFont->IsValid())
328   {
329     return;
330   }
331
332   theAscent  = aFont->Ascender();
333   theDescent = aFont->Descender();
334
335   GLfloat aWidth = 0.0f;
336   for (NCollection_Utf8Iter anIter = theText.Iterator(); *anIter != 0;)
337   {
338     const Standard_Utf32Char aCharThis =   *anIter;
339     const Standard_Utf32Char aCharNext = *++anIter;
340
341     if (aCharThis == '\x0D' // CR  (carriage return)
342      || aCharThis == '\a'   // BEL (alarm)
343      || aCharThis == '\f'   // FF  (form feed) NP (new page)
344      || aCharThis == '\b'   // BS  (backspace)
345      || aCharThis == '\v')  // VT  (vertical tab)
346     {
347       continue; // skip unsupported carriage control codes
348     }
349     else if (aCharThis == '\x0A') // LF (line feed, new line)
350     {
351       theWidth = Max (theWidth, aWidth);
352       aWidth   = 0.0f;
353       continue;
354     }
355     else if (aCharThis == ' ')
356     {
357       aWidth += aFont->AdvanceX (aCharThis, aCharNext);
358       continue;
359     }
360     else if (aCharThis == '\t')
361     {
362       aWidth += aFont->AdvanceX (' ', aCharNext) * 8.0f;
363       continue;
364     }
365
366     aWidth += aFont->AdvanceX (aCharThis, aCharNext);
367   }
368   theWidth = Max (theWidth, aWidth);
369
370   Handle(OpenGl_Context) aCtx = theCtx;
371   aFont.Nullify();
372   aCtx->ReleaseResource (aFontKey, Standard_True);
373 }
374
375 // =======================================================================
376 // function : Render
377 // purpose  :
378 // =======================================================================
379 void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
380 {
381   const OpenGl_AspectText*      aTextAspect  = theWorkspace->AspectText (Standard_True);
382   const Handle(OpenGl_Texture)  aPrevTexture = theWorkspace->DisableTexture();
383   const Handle(OpenGl_Context)& aCtx         = theWorkspace->GetGlContext();
384
385   // Bind custom shader program or generate default version
386   if (aCtx->core20fwd != NULL)
387   {
388     aCtx->ShaderManager()->BindProgram (
389       aTextAspect, aTextAspect->ShaderProgramRes (aCtx));
390   }
391
392   // use highlight color or colors from aspect
393   if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
394   {
395     render (theWorkspace->PrinterContext(),
396             aCtx,
397             *aTextAspect,
398             *theWorkspace->HighlightColor,
399             *theWorkspace->HighlightColor);
400   }
401   else
402   {
403     render (theWorkspace->PrinterContext(),
404             aCtx,
405             *aTextAspect,
406             aTextAspect->Color(),
407             aTextAspect->SubtitleColor());
408   }
409
410   // restore aspects
411   if (!aPrevTexture.IsNull())
412   {
413     theWorkspace->EnableTexture (aPrevTexture);
414   }
415 }
416
417 // =======================================================================
418 // function : Render
419 // purpose  :
420 // =======================================================================
421 void OpenGl_Text::Render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
422                           const Handle(OpenGl_Context)&        theCtx,
423                           const OpenGl_AspectText&             theTextAspect) const
424 {
425   render (thePrintCtx, theCtx, theTextAspect, theTextAspect.Color(), theTextAspect.SubtitleColor());
426 }
427
428 // =======================================================================
429 // function : setupMatrix
430 // purpose  :
431 // =======================================================================
432 void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
433                                const Handle(OpenGl_Context)&        theCtx,
434                                const OpenGl_AspectText&             theTextAspect,
435                                const OpenGl_Vec3                    theDVec) const
436 {
437   OpenGl_Mat4d aModViewMat;
438
439   if (myIs2d)
440   {
441     OpenGl_Utils::Translate<GLdouble> (aModViewMat, myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.f);
442     OpenGl_Utils::Scale<GLdouble> (aModViewMat, 1.f, -1.f, 1.f);
443     OpenGl_Utils::Rotate<GLdouble> (aModViewMat, theTextAspect.Angle(), 0.f, 0.f, 1.f);
444   }
445   else
446   {
447     // align coordinates to the nearest integer
448     // to avoid extra interpolation issues
449     GLdouble anObjX, anObjY, anObjZ;
450     OpenGl_Utils::UnProject<Standard_Real> (std::floor (myWinX + theDVec.x()),
451                                             std::floor (myWinY + theDVec.y()),
452                                             myWinZ + theDVec.z(),
453                                             OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
454                                             OpenGl_Mat4d::Map (myProjMatrix),
455                                             myViewport,
456                                             anObjX,
457                                             anObjY,
458                                             anObjZ);
459
460     OpenGl_Utils::Translate<GLdouble> (aModViewMat, anObjX, anObjY, anObjZ);
461     OpenGl_Utils::Rotate<GLdouble> (aModViewMat, theTextAspect.Angle(), 0.0, 0.0, 1.0);
462
463     if (!theTextAspect.IsZoomable())
464     {
465     #ifdef _WIN32
466       // if the context has assigned printer context, use it's parameters
467       if (!thePrintCtx.IsNull())
468       {
469         // get printing scaling in x and y dimensions
470         GLfloat aTextScalex = 1.0f, aTextScaley = 1.0f;
471         thePrintCtx->GetScale (aTextScalex, aTextScaley);
472
473         // text should be scaled in all directions with same
474         // factor to save its proportions, so use height (y) scaling
475         // as it is better for keeping text/3d graphics proportions
476         OpenGl_Utils::Scale<GLdouble> (aModViewMat, aTextScaley, aTextScaley, aTextScaley);
477       }
478     #endif
479       OpenGl_Utils::Scale<GLdouble> (aModViewMat, myScaleHeight, myScaleHeight, myScaleHeight);
480     }
481   }
482
483   theCtx->WorldViewState.SetCurrent<Standard_Real> (aModViewMat);
484   theCtx->ApplyWorldViewMatrix();
485
486   if (!theCtx->ActiveProgram().IsNull())
487   {
488     // Upload updated state to shader program
489     theCtx->ShaderManager()->PushState (theCtx->ActiveProgram());
490   }
491 }
492
493 // =======================================================================
494 // function : drawText
495 // purpose  :
496 // =======================================================================
497
498 void OpenGl_Text::drawText (const Handle(OpenGl_PrinterContext)& ,
499                             const Handle(OpenGl_Context)&        theCtx,
500                           #ifdef HAVE_GL2PS
501                             const OpenGl_AspectText&             theTextAspect) const
502                           #else
503                             const OpenGl_AspectText&                          ) const
504                           #endif
505 {
506 #ifdef HAVE_GL2PS
507   if (theCtx->IsFeedback())
508   {
509     // position of the text and alignment is calculated by transformation matrix
510     exportText (myString, myIs2d, theTextAspect, (Standard_Integer )myExportHeight);
511     return;
512   }
513 #endif
514
515   if (myVertsVbo.Length() != myTextures.Length()
516    || myTextures.IsEmpty())
517   {
518     return;
519   }
520
521   for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
522   {
523     const GLuint aTexId = myTextures.Value (anIter);
524     glBindTexture (GL_TEXTURE_2D, aTexId);
525
526     const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter);
527     const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter);
528     aVerts->BindAttribute (theCtx, Graphic3d_TOA_POS);
529     aTCrds->BindAttribute (theCtx, Graphic3d_TOA_UV);
530
531     glDrawArrays (GL_TRIANGLES, 0, GLsizei(aVerts->GetElemsNb()));
532
533     aTCrds->UnbindAttribute (theCtx, Graphic3d_TOA_UV);
534     aVerts->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
535   }
536   glBindTexture (GL_TEXTURE_2D, 0);
537 }
538
539 // =======================================================================
540 // function : FontKey
541 // purpose  :
542 // =======================================================================
543 TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_AspectText& theAspect,
544                                               const Standard_Integer   theHeight)
545 {
546   const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
547   return theAspect.FontName()
548        + TCollection_AsciiString(":") + Standard_Integer(anAspect)
549        + TCollection_AsciiString(":") + theHeight;
550 }
551
552 // =======================================================================
553 // function : FindFont
554 // purpose  :
555 // =======================================================================
556 Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
557                                            const OpenGl_AspectText&      theAspect,
558                                            const Standard_Integer        theHeight,
559                                            const TCollection_AsciiString theKey)
560 {
561   Handle(OpenGl_Font) aFont;
562   if (theHeight < 2)
563   {
564     return aFont; // invalid parameters
565   }
566
567   if (!theCtx->GetResource (theKey, aFont))
568   {
569     Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance();
570     const Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (theAspect.FontName());
571     const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
572     Handle(Font_SystemFont) aRequestedFont = aFontMgr->FindFont (aFontName, anAspect, theHeight);
573     if (aRequestedFont.IsNull())
574     {
575       return aFont;
576     }
577
578     Handle(Font_FTFont) aFontFt = new Font_FTFont (NULL);
579     if (!aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight))
580     {
581       return aFont;
582     }
583
584     Handle(OpenGl_Context) aCtx = theCtx;
585   #if !defined(GL_ES_VERSION_2_0)
586     glPushAttrib (GL_TEXTURE_BIT);
587   #endif
588     aFont = new OpenGl_Font (aFontFt, theKey);
589     if (!aFont->Init (aCtx))
590     {
591       //glPopAttrib();
592       //return aFont; // out of resources?
593     }
594   #if !defined(GL_ES_VERSION_2_0)
595     glPopAttrib(); // texture bit
596   #endif
597
598     aCtx->ShareResource (theKey, aFont);
599   }
600   return aFont;
601 }
602
603 // =======================================================================
604 // function : render
605 // purpose  :
606 // =======================================================================
607 void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
608                           const Handle(OpenGl_Context)&        theCtx,
609                           const OpenGl_AspectText&             theTextAspect,
610                           const TEL_COLOUR&                    theColorText,
611                           const TEL_COLOUR&                    theColorSubs) const
612 {
613   if (myString.IsEmpty())
614   {
615     return;
616   }
617
618   const TCollection_AsciiString aFontKey = FontKey (theTextAspect, myParams.Height);
619   if (!myFont.IsNull()
620    && !myFont->ResourceKey().IsEqual (aFontKey))
621   {
622     // font changed
623     const_cast<OpenGl_Text* > (this)->Release (theCtx.operator->());
624   }
625
626   if (myFont.IsNull())
627   {
628     myFont = FindFont (theCtx, theTextAspect, myParams.Height, aFontKey);
629     if (myFont.IsNull())
630     {
631       return;
632     }
633   }
634
635   if (myTextures.IsEmpty())
636   {
637     OpenGl_TextFormatter aFormatter;
638     aFormatter.SetupAlignment (myParams.HAlign, myParams.VAlign);
639     aFormatter.Reset();
640     aFormatter.Append (theCtx, myString, *myFont.operator->());
641     aFormatter.Format();
642
643     aFormatter.Result (theCtx, myTextures, myVertsVbo, myTCrdsVbo);
644     aFormatter.BndBox (myBndBox);
645   }
646
647   if (myTextures.IsEmpty())
648   {
649     return;
650   }
651
652   myExportHeight = 1.0f;
653   myScaleHeight  = 1.0f;
654
655   theCtx->WorldViewState.Push();
656
657   myModelMatrix.Convert (theCtx->WorldViewState.Current() * theCtx->ModelWorldState.Current());
658
659   if (!myIs2d)
660   {
661     glGetIntegerv (GL_VIEWPORT,          myViewport);
662     myProjMatrix.Convert (theCtx->ProjectionState.Current());
663
664     OpenGl_Utils::Project<Standard_Real> (myPoint.x(),
665                                           myPoint.y(),
666                                           myPoint.z(),
667                                           myModelMatrix,
668                                           myProjMatrix,
669                                           myViewport,
670                                           myWinX,
671                                           myWinY,
672                                           myWinZ);
673
674     // compute scale factor for constant text height
675     GLdouble x1, y1, z1;
676     OpenGl_Utils::UnProject<Standard_Real> (myWinX,
677                                             myWinY,
678                                             myWinZ,
679                                             OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
680                                             myProjMatrix,
681                                             myViewport,
682                                             x1,
683                                             y1,
684                                             z1);
685
686     GLdouble x2, y2, z2;
687     const GLdouble h = (GLdouble )myFont->FTFont()->PointSize();
688     OpenGl_Utils::UnProject<Standard_Real> (myWinX,
689                                             myWinY + h,
690                                             myWinZ,
691                                             OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
692                                             myProjMatrix,
693                                             myViewport,
694                                             x2,
695                                             y2,
696                                             z2);
697
698     myScaleHeight = (y2 - y1) / h;
699     if (theTextAspect.IsZoomable())
700     {
701       myExportHeight = (float )h;
702     }
703   }
704   myExportHeight = (float )myFont->FTFont()->PointSize() / myExportHeight;
705
706 #if !defined(GL_ES_VERSION_2_0)
707
708   // push enabled flags to the stack
709   glPushAttrib (GL_ENABLE_BIT);
710   glDisable (GL_LIGHTING);
711
712   // setup depth test
713   if (!myIs2d
714    && theTextAspect.StyleType() != Aspect_TOST_ANNOTATION)
715   {
716     glEnable (GL_DEPTH_TEST);
717   }
718   else
719   {
720     glDisable (GL_DEPTH_TEST);
721   }
722
723   // setup alpha test
724   GLint aTexEnvParam = GL_REPLACE;
725   glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam);
726   if (aTexEnvParam != GL_REPLACE)
727   {
728     glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
729   }
730   glAlphaFunc (GL_GEQUAL, 0.285f);
731   glEnable (GL_ALPHA_TEST);
732
733   // setup blending
734   glEnable (GL_BLEND);
735   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
736
737   // activate texture unit
738   glDisable (GL_TEXTURE_1D);
739   glEnable  (GL_TEXTURE_2D);
740   if (theCtx->core15fwd != NULL)
741   {
742     theCtx->core15fwd->glActiveTexture (GL_TEXTURE0);
743   }
744
745   // extra drawings
746   switch (theTextAspect.DisplayType())
747   {
748     case Aspect_TODT_BLEND:
749     {
750       glEnable  (GL_COLOR_LOGIC_OP);
751       glLogicOp (GL_XOR);
752       break;
753     }
754     case Aspect_TODT_SUBTITLE:
755     {
756       theCtx->core11->glColor3fv (theColorSubs.rgb);
757       setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
758
759       glBindTexture (GL_TEXTURE_2D, 0);
760       glBegin (GL_QUADS);
761       glVertex2f (myBndBox.Left,  myBndBox.Top);
762       glVertex2f (myBndBox.Right, myBndBox.Top);
763       glVertex2f (myBndBox.Right, myBndBox.Bottom);
764       glVertex2f (myBndBox.Left,  myBndBox.Bottom);
765       glEnd();
766       break;
767     }
768     case Aspect_TODT_DEKALE:
769     {
770       theCtx->SetColor4fv (*(const OpenGl_Vec4* )theColorSubs.rgb);
771       setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.00001f));
772       drawText    (thePrintCtx, theCtx, theTextAspect);
773       setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.00001f));
774       drawText    (thePrintCtx, theCtx, theTextAspect);
775       setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.00001f));
776       drawText    (thePrintCtx, theCtx, theTextAspect);
777       setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.00001f));
778       drawText    (thePrintCtx, theCtx, theTextAspect);
779       break;
780     }
781     case Aspect_TODT_DIMENSION:
782     case Aspect_TODT_NORMAL:
783     {
784       break;
785     }
786   }
787
788   // main draw call
789   theCtx->SetColor4fv (*(const OpenGl_Vec4* )theColorText.rgb);
790   setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
791   drawText    (thePrintCtx, theCtx, theTextAspect);
792
793   glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aTexEnvParam);
794
795   if (theTextAspect.DisplayType() == Aspect_TODT_DIMENSION)
796   {
797     setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
798
799     glDisable (GL_BLEND);
800     glDisable (GL_TEXTURE_2D);
801     glDisable (GL_ALPHA_TEST);
802     if (!myIs2d)
803     {
804       glDisable (GL_DEPTH_TEST);
805     }
806     glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
807
808     glClear (GL_STENCIL_BUFFER_BIT);
809     glEnable (GL_STENCIL_TEST);
810     glStencilFunc (GL_ALWAYS, 1, 0xFF);
811     glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
812
813     glBegin (GL_QUADS);
814     glVertex2f (myBndBox.Left,  myBndBox.Top);
815     glVertex2f (myBndBox.Right, myBndBox.Top);
816     glVertex2f (myBndBox.Right, myBndBox.Bottom);
817     glVertex2f (myBndBox.Left,  myBndBox.Bottom);
818     glEnd();
819
820     glStencilFunc (GL_ALWAYS, 0, 0xFF);
821     // glPopAttrib() will reset state for us
822     //glDisable (GL_STENCIL_TEST);
823     //if (!myIs2d) glEnable (GL_DEPTH_TEST);
824
825     glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
826   }
827
828   // revert OpenGL state
829   glPopAttrib(); // enable bit
830
831   // model view matrix was modified
832   theCtx->WorldViewState.Pop();
833   theCtx->ApplyModelViewMatrix();
834 #endif
835 }