0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[occt.git] / src / OpenGl / OpenGl_Text.cxx
index fd9757f..1fc4503 100644 (file)
@@ -13,7 +13,7 @@
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#include <OpenGl_AspectText.hxx>
+#include <OpenGl_Aspects.hxx>
 #include <OpenGl_GlCore11.hxx>
 #include <OpenGl_GraphicDriver.hxx>
 #include <OpenGl_ShaderManager.hxx>
 #include <Graphic3d_TransformUtils.hxx>
 #include <TCollection_HAsciiString.hxx>
 
-#ifdef HAVE_GL2PS
-  #include <gl2ps.h>
-#endif
-
 namespace
 {
   static const GLdouble THE_IDENTITY_MATRIX[16] =
@@ -43,100 +39,39 @@ namespace
     0.0, 0.0, 0.0, 1.0
   };
 
-#ifdef HAVE_GL2PS
-  static char const* TheFamily[] = {"Helvetica", "Courier", "Times"};
-  static char const* TheItalic[] = {"Oblique",   "Oblique", "Italic"};
-  static char const* TheBase[]   = {"", "", "-Roman"};
+  static const TCollection_AsciiString THE_DEFAULT_FONT (Font_NOF_ASCII_MONO);
 
-  //! Convert font name used for rendering to some "good" font names
-  //! that produce good vector text.
-  static void getGL2PSFontName (const char* theSrcFont,
-                                char*       thePsFont)
+  //! Auxiliary tool for setting polygon offset temporarily.
+  struct BackPolygonOffsetSentry
   {
-    if (strstr (theSrcFont, "Symbol"))
-    {
-      sprintf (thePsFont, "%s", "Symbol");
-      return;
-    }
-    else if (strstr (theSrcFont, "ZapfDingbats"))
-    {
-      sprintf (thePsFont, "%s", "WingDings");
-      return;
-    }
-
-    int  aFontId  = 0;
-    bool isBold   = false;
-    bool isItalic = false;
-    if (strstr (theSrcFont, "Courier"))
-    {
-      aFontId = 1;
-    }
-    else if (strstr (theSrcFont, "Times"))
+    BackPolygonOffsetSentry (const Handle(OpenGl_Context)& theCtx)
+    : myCtx (theCtx),
+      myOffsetBack (!theCtx.IsNull() ? theCtx->PolygonOffset() : Graphic3d_PolygonOffset())
     {
-      aFontId = 2;
-    }
-
-    if (strstr (theSrcFont, "Bold"))
-    {
-      isBold = true;
-    }
-    if (strstr (theSrcFont, "Italic")
-     || strstr (theSrcFont, "Oblique"))
-    {
-      isItalic = true;
-    }
-
-    if (isBold)
-    {
-      if (isItalic)
-      {
-        sprintf (thePsFont, "%s-Bold%s", TheFamily[aFontId], TheItalic[aFontId]);
-      }
-      else
+      if (!theCtx.IsNull())
       {
-        sprintf (thePsFont, "%s-Bold", TheFamily[aFontId]);
+        Graphic3d_PolygonOffset aPolyOffset = myOffsetBack;
+        aPolyOffset.Mode = Aspect_POM_Fill;
+        aPolyOffset.Units += 1.0f;
+        theCtx->SetPolygonOffset (aPolyOffset);
       }
     }
-    else if (isItalic)
-    {
-      sprintf (thePsFont, "%s-%s", TheFamily[aFontId], TheItalic[aFontId]);
-    }
-    else
-    {
-      sprintf (thePsFont, "%s%s", TheFamily[aFontId], TheBase[aFontId]);
-    }
-  }
-
-  static void exportText (const NCollection_String& theText,
-                          const Standard_Boolean    theIs2d,
-                          const OpenGl_AspectText&  theAspect,
-                          const Standard_Integer    theHeight)
-  {
-
-    char aPsFont[64];
-    getGL2PSFontName (theAspect.Aspect()->Font().ToCString(), aPsFont);
 
-    (void)theIs2d;
-  #if !defined(GL_ES_VERSION_2_0)
-    if (theIs2d)
+    ~BackPolygonOffsetSentry()
     {
-      glRasterPos2f (0.0f, 0.0f);
-    }
-    else
-    {
-      glRasterPos3f (0.0f, 0.0f, 0.0f);
+      if (!myCtx.IsNull())
+      {
+        myCtx->SetPolygonOffset (myOffsetBack);
+      }
     }
 
-    GLubyte aZero = 0;
-    glBitmap (1, 1, 0, 0, 0, 0, &aZero);
-  #endif
-
-    // Standard GL2PS's alignment isn't used, because it doesn't work correctly
-    // for all formats, therefore alignment is calculated manually relative
-    // to the bottom-left corner, which corresponds to the GL2PS_TEXT_BL value
-    gl2psTextOpt (theText.ToCString(), aPsFont, (GLshort)theHeight, GL2PS_TEXT_BL, (float )theAspect.Aspect()->GetTextAngle());
-  }
-#endif
+  private:
+    BackPolygonOffsetSentry (const BackPolygonOffsetSentry& );
+    BackPolygonOffsetSentry& operator= (const BackPolygonOffsetSentry& );
+  private:
+    const Handle(OpenGl_Context)& myCtx;
+    const Graphic3d_PolygonOffset myOffsetBack;
+  };
 
 } // anonymous namespace
 
@@ -286,8 +221,7 @@ void OpenGl_Text::Init (const Handle(OpenGl_Context)&     theCtx,
   }
   myIs2d       = true;
   myParams     = theParams;
-  myPoint.xy() = thePoint;
-  myPoint.z()  = 0.0f;
+  myPoint.SetValues (thePoint, 0.0f);
   myString.FromUnicode (theText.ToExtString());
 }
 
@@ -355,7 +289,7 @@ void OpenGl_Text::Release (OpenGl_Context* theCtx)
 // =======================================================================
 void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
                               const NCollection_String&     theText,
-                              const OpenGl_AspectText&      theTextAspect,
+                              const OpenGl_Aspects&         theTextAspect,
                               const OpenGl_TextParam&       theParams,
                               const unsigned int            theResolution,
                               Standard_ShortReal&           theWidth,
@@ -421,13 +355,9 @@ void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
 // =======================================================================
 void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
 {
-  const OpenGl_AspectText*      aTextAspect  = theWorkspace->ApplyAspectText();
-  const Handle(OpenGl_Context)& aCtx         = theWorkspace->GetGlContext();
+  const OpenGl_Aspects* aTextAspect = theWorkspace->ApplyAspects();
+  const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
   const Handle(OpenGl_TextureSet) aPrevTexture = aCtx->BindTextures (Handle(OpenGl_TextureSet)());
-#if !defined(GL_ES_VERSION_2_0)
-  const Standard_Integer aPrevPolygonMode  = aCtx->SetPolygonMode (GL_FILL);
-  const bool             aPrevHatchingMode = aCtx->SetPolygonHatchEnabled (false);
-#endif
 
   // Bind custom shader program or generate default version
   aCtx->ShaderManager()->BindFontProgram (aTextAspect->ShaderProgramRes (aCtx));
@@ -447,10 +377,6 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
   {
     aCtx->BindTextures (aPrevTexture);
   }
-#if !defined(GL_ES_VERSION_2_0)
-  aCtx->SetPolygonMode         (aPrevPolygonMode);
-  aCtx->SetPolygonHatchEnabled (aPrevHatchingMode);
-#endif
 
   // restore Z buffer settings
   if (theWorkspace->UseZBuffer())
@@ -464,13 +390,23 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
 // purpose  :
 // =======================================================================
 void OpenGl_Text::Render (const Handle(OpenGl_Context)& theCtx,
-                          const OpenGl_AspectText&      theTextAspect,
-                          const unsigned int            theResolution) const
+                          const OpenGl_Aspects& theTextAspect,
+                          unsigned int theResolution) const
 {
+#if !defined(GL_ES_VERSION_2_0)
+  const Standard_Integer aPrevPolygonMode  = theCtx->SetPolygonMode (GL_FILL);
+  const bool             aPrevHatchingMode = theCtx->SetPolygonHatchEnabled (false);
+#endif
+
   render (theCtx, theTextAspect,
           theTextAspect.Aspect()->ColorRGBA(),
           theTextAspect.Aspect()->ColorSubTitleRGBA(),
           theResolution);
+
+#if !defined(GL_ES_VERSION_2_0)
+  theCtx->SetPolygonMode         (aPrevPolygonMode);
+  theCtx->SetPolygonHatchEnabled (aPrevHatchingMode);
+#endif
 }
 
 // =======================================================================
@@ -478,8 +414,8 @@ void OpenGl_Text::Render (const Handle(OpenGl_Context)& theCtx,
 // purpose  :
 // =======================================================================
 void OpenGl_Text::setupMatrix (const Handle(OpenGl_Context)& theCtx,
-                               const OpenGl_AspectText&      theTextAspect,
-                               const OpenGl_Vec3             theDVec) const
+                               const OpenGl_Aspects& theTextAspect,
+                               const OpenGl_Vec3& theDVec) const
 {
   OpenGl_Mat4d aModViewMat;
   OpenGl_Mat4d aProjectMat;
@@ -496,7 +432,7 @@ void OpenGl_Text::setupMatrix (const Handle(OpenGl_Context)& theCtx,
   {
     Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.f);
     Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, 1.f, -1.f, 1.f);
-    Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->GetTextAngle(), 0.f, 0.f, 1.f);
+    Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.f, 0.f, 1.f);
   }
   else
   {
@@ -538,10 +474,10 @@ void OpenGl_Text::setupMatrix (const Handle(OpenGl_Context)& theCtx,
     else
     {
       Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, anObjX, anObjY, anObjZ);
-      Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->GetTextAngle(), 0.0, 0.0, 1.0);
+      Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.0, 0.0, 1.0);
     }
 
-    if (!theTextAspect.Aspect()->GetTextZoomable())
+    if (!theTextAspect.Aspect()->IsTextZoomable())
     {
       Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, myScaleHeight, myScaleHeight, myScaleHeight);
     }
@@ -578,19 +514,9 @@ void OpenGl_Text::setupMatrix (const Handle(OpenGl_Context)& theCtx,
 // purpose  :
 // =======================================================================
 void OpenGl_Text::drawText (const Handle(OpenGl_Context)& theCtx,
-                            const OpenGl_AspectText&      theTextAspect) const
+                            const OpenGl_Aspects& theTextAspect) const
 {
-#ifdef HAVE_GL2PS
-  if (theCtx->IsFeedback())
-  {
-    // position of the text and alignment is calculated by transformation matrix
-    exportText (myString, myIs2d, theTextAspect, (Standard_Integer )myExportHeight);
-    return;
-  }
-#else
   (void )theTextAspect;
-#endif
-
   if (myVertsVbo.Length() != myTextures.Length()
    || myTextures.IsEmpty())
   {
@@ -619,14 +545,15 @@ void OpenGl_Text::drawText (const Handle(OpenGl_Context)& theCtx,
 // function : FontKey
 // purpose  :
 // =======================================================================
-TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_AspectText& theAspect,
-                                              const Standard_Integer   theHeight,
-                                              const unsigned int       theResolution)
+TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_Aspects& theAspect,
+                                              Standard_Integer theHeight,
+                                              unsigned int theResolution)
 {
-  const Font_FontAspect anAspect = theAspect.Aspect()->GetTextFontAspect() != Font_FA_Undefined
-                                 ? theAspect.Aspect()->GetTextFontAspect()
+  const Font_FontAspect anAspect = theAspect.Aspect()->TextFontAspect() != Font_FA_Undefined
+                                 ? theAspect.Aspect()->TextFontAspect()
                                  : Font_FA_Regular;
-  return theAspect.Aspect()->Font()
+  const TCollection_AsciiString& aFont = !theAspect.Aspect()->TextFont().IsNull() ? theAspect.Aspect()->TextFont()->String() : THE_DEFAULT_FONT;
+  return aFont
        + TCollection_AsciiString(":") + Standard_Integer(anAspect)
        + TCollection_AsciiString(":") + Standard_Integer(theResolution)
        + TCollection_AsciiString(":") + theHeight;
@@ -637,10 +564,10 @@ TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_AspectText& theAspect
 // purpose  :
 // =======================================================================
 Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
-                                           const OpenGl_AspectText&      theAspect,
-                                           const Standard_Integer        theHeight,
-                                           const unsigned int            theResolution,
-                                           const TCollection_AsciiString theKey)
+                                           const OpenGl_Aspects& theAspect,
+                                           Standard_Integer theHeight,
+                                           unsigned int theResolution,
+                                           const TCollection_AsciiString& theKey)
 {
   Handle(OpenGl_Font) aFont;
   if (theHeight < 2)
@@ -651,50 +578,31 @@ Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
   if (!theCtx->GetResource (theKey, aFont))
   {
     Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance();
-    const Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (theAspect.Aspect()->Font());
-    const Font_FontAspect anAspect = theAspect.Aspect()->GetTextFontAspect() != Font_FA_Undefined
-                                   ? theAspect.Aspect()->GetTextFontAspect()
-                                   : Font_FA_Regular;
-    Handle(Font_SystemFont) aRequestedFont = aFontMgr->FindFont (aFontName, anAspect, theHeight);
-    Handle(Font_FTFont) aFontFt;
-    if (!aRequestedFont.IsNull())
+    const TCollection_AsciiString& aFontName = !theAspect.Aspect()->TextFont().IsNull()
+                                             ?  theAspect.Aspect()->TextFont()->String()
+                                             :  THE_DEFAULT_FONT;
+    Font_FontAspect anAspect = theAspect.Aspect()->TextFontAspect() != Font_FA_Undefined
+                             ? theAspect.Aspect()->TextFontAspect()
+                             : Font_FA_Regular;
+    Font_FTFontParams aParams;
+    aParams.PointSize  = theHeight;
+    aParams.Resolution = theResolution;
+    if (Handle(Font_FTFont) aFontFt = Font_FTFont::FindAndCreate (aFontName, anAspect, aParams, Font_StrictLevel_Any))
     {
-      aFontFt = new Font_FTFont (Handle(Font_FTLibrary)());
-
-      if (aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight, theResolution))
-      {
-        aFont = new OpenGl_Font (aFontFt, theKey);
-        if (!aFont->Init (theCtx))
-        {
-          TCollection_ExtendedString aMsg;
-          aMsg += "Font '";
-          aMsg += theAspect.Aspect()->Font();
-          aMsg += "' - initialization of GL resources has failed!";
-          theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
-          aFontFt.Nullify();
-          aFont->Release (theCtx.operator->());
-          aFont = new OpenGl_Font (aFontFt, theKey);
-        }
-      }
-      else
+      aFont = new OpenGl_Font (aFontFt, theKey);
+      if (!aFont->Init (theCtx))
       {
-        TCollection_ExtendedString aMsg;
-        aMsg += "Font '";
-        aMsg += theAspect.Aspect()->Font();
-        aMsg += "' is broken or has incompatible format! File path: ";
-        aMsg += aRequestedFont->FontPath()->ToCString();
-        theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
+        theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
+                              TCollection_AsciiString ("Font '") + aFontName + "' - initialization of GL resources has failed!");
         aFontFt.Nullify();
+        aFont->Release (theCtx.get());
         aFont = new OpenGl_Font (aFontFt, theKey);
       }
     }
     else
     {
-      TCollection_ExtendedString aMsg;
-      aMsg += "Font '";
-      aMsg += theAspect.Aspect()->Font();
-      aMsg += "' is not found in the system!";
-      theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
+      theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
+                           TCollection_AsciiString ("Font '") + aFontName + "' is not found in the system!");
       aFont = new OpenGl_Font (aFontFt, theKey);
     }
 
@@ -708,8 +616,8 @@ Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
 // purpose  :
 // =======================================================================
 void OpenGl_Text::drawRect (const Handle(OpenGl_Context)& theCtx,
-                            const OpenGl_AspectText&      theTextAspect,
-                            const OpenGl_Vec4&            theColorSubs) const
+                            const OpenGl_Aspects& theTextAspect,
+                            const OpenGl_Vec4& theColorSubs) const
 {
   Handle(OpenGl_ShaderProgram) aPrevProgram = theCtx->ActiveProgram();
   if (myBndVertsVbo.IsNull())
@@ -732,8 +640,10 @@ void OpenGl_Text::drawRect (const Handle(OpenGl_Context)& theCtx,
     myBndVertsVbo->Init (theCtx, 2, 4, aQuad[0].GetData());
   }
 
-  // bind flat program
-  theCtx->ShaderManager()->BindFaceProgram (Handle(OpenGl_TextureSet)(), Standard_False, Standard_False, Standard_False, Handle(OpenGl_ShaderProgram)());
+  // bind unlit program
+  theCtx->ShaderManager()->BindFaceProgram (Handle(OpenGl_TextureSet)(), Graphic3d_TOSM_UNLIT,
+                                            Graphic3d_AlphaMode_Opaque, Standard_False, Standard_False,
+                                            Handle(OpenGl_ShaderProgram)());
 
 #if !defined(GL_ES_VERSION_2_0)
   if (theCtx->core11 != NULL
@@ -743,7 +653,7 @@ void OpenGl_Text::drawRect (const Handle(OpenGl_Context)& theCtx,
   }
 #endif
   theCtx->SetColor4fv (theColorSubs);
-  setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
+  setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
   myBndVertsVbo->BindAttribute (theCtx, Graphic3d_TOA_POS);
 
   theCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
@@ -757,10 +667,10 @@ void OpenGl_Text::drawRect (const Handle(OpenGl_Context)& theCtx,
 // purpose  :
 // =======================================================================
 void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
-                          const OpenGl_AspectText&      theTextAspect,
-                          const OpenGl_Vec4&            theColorText,
-                          const OpenGl_Vec4&            theColorSubs,
-                          const unsigned int            theResolution) const
+                          const OpenGl_Aspects& theTextAspect,
+                          const OpenGl_Vec4& theColorText,
+                          const OpenGl_Vec4& theColorSubs,
+                          unsigned int theResolution) const
 {
   if (myString.IsEmpty())
   {
@@ -830,7 +740,7 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
                                                       myWinX, myWinY, myWinZ);
 
     // compute scale factor for constant text height
-    if (theTextAspect.Aspect()->GetTextZoomable())
+    if (theTextAspect.Aspect()->IsTextZoomable())
     {
       myExportHeight = aPointSize;
     }
@@ -857,8 +767,9 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
 #endif
 
   // setup depth test
-  if (myIs2d
-   || theTextAspect.Aspect()->Style() == Aspect_TOST_ANNOTATION)
+  const bool hasDepthTest = !myIs2d
+                         && theTextAspect.Aspect()->TextStyle() != Aspect_TOST_ANNOTATION;
+  if (!hasDepthTest)
   {
     glDisable (GL_DEPTH_TEST);
   }
@@ -872,10 +783,6 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
   GLint aTexEnvParam = GL_REPLACE;
   if (theCtx->core11 != NULL)
   {
-    // setup alpha test
-    glAlphaFunc (GL_GEQUAL, 0.285f);
-    glEnable (GL_ALPHA_TEST);
-
     glDisable (GL_TEXTURE_1D);
     glEnable  (GL_TEXTURE_2D);
     glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam);
@@ -890,8 +797,11 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
   glEnable (GL_BLEND);
   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
+  // alpha to coverage makes text too thin
+  theCtx->SetSampleAlphaToCoverage (false);
+
   // extra drawings
-  switch (theTextAspect.Aspect()->DisplayType())
+  switch (theTextAspect.Aspect()->TextDisplayType())
   {
     case Aspect_TODT_BLEND:
     {
@@ -903,19 +813,29 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
     }
     case Aspect_TODT_SUBTITLE:
     {
+      BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
       drawRect (theCtx, theTextAspect, theColorSubs);
       break;
     }
     case Aspect_TODT_DEKALE:
     {
+      BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
       theCtx->SetColor4fv (theColorSubs);
-      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.00001f));
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.0f));
       drawText    (theCtx, theTextAspect);
-      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.00001f));
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.0f));
       drawText    (theCtx, theTextAspect);
-      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.00001f));
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.0f));
       drawText    (theCtx, theTextAspect);
-      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.00001f));
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
+      drawText    (theCtx, theTextAspect);
+      break;
+    }
+    case Aspect_TODT_SHADOW:
+    {
+      BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
+      theCtx->SetColor4fv (theColorSubs);
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
       drawText    (theCtx, theTextAspect);
       break;
     }
@@ -944,7 +864,7 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
   }
 #endif
 
-  if (theTextAspect.Aspect()->DisplayType() == Aspect_TODT_DIMENSION)
+  if (theTextAspect.Aspect()->TextDisplayType() == Aspect_TODT_DIMENSION)
   {
     glDisable (GL_BLEND);
     if (!myIs2d)
@@ -955,10 +875,9 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
     if (theCtx->core11 != NULL)
     {
       glDisable (GL_TEXTURE_2D);
-      glDisable (GL_ALPHA_TEST);
     }
   #endif
-    glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+    const bool aColorMaskBack = theCtx->SetColorMask (false);
 
     glClear (GL_STENCIL_BUFFER_BIT);
     glEnable (GL_STENCIL_TEST);
@@ -969,17 +888,13 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
 
     glStencilFunc (GL_ALWAYS, 0, 0xFF);
 
-    glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+    theCtx->SetColorMask (aColorMaskBack);
   }
 
   // reset OpenGL state
   glDisable (GL_BLEND);
   glDisable (GL_STENCIL_TEST);
 #if !defined(GL_ES_VERSION_2_0)
-  if (theCtx->core11 != NULL)
-  {
-    glDisable (GL_ALPHA_TEST);
-  }
   glDisable (GL_COLOR_LOGIC_OP);
 #endif