0030537: Visualization - wrapping text in font text formatter
[occt.git] / src / OpenGl / OpenGl_Text.cxx
index 2aacc52..09c6397 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 <OpenGl_Text.hxx>
 #include <OpenGl_Workspace.hxx>
 #include <OpenGl_View.hxx>
+#include <OpenGl_VertexBufferCompat.hxx>
 
 #include <Font_FontMgr.hxx>
 #include <Font_FTFont.hxx>
+#include <Font_TextFormatter.hxx>
 #include <Graphic3d_TransformUtils.hxx>
 #include <TCollection_HAsciiString.hxx>
 
-#ifdef HAVE_GL2PS
-  #include <gl2ps.h>
-#endif
-
 namespace
 {
-  static const GLdouble THE_IDENTITY_MATRIX[16] =
-  {
-    1.0, 0.0, 0.0, 0.0,
-    0.0, 1.0, 0.0, 0.0,
-    0.0, 0.0, 1.0, 0.0,
-    0.0, 0.0, 0.0, 1.0
-  };
+  static const OpenGl_Mat4d THE_IDENTITY_MATRIX;
 
-#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"))
-    {
-      aFontId = 2;
-    }
-
-    if (strstr (theSrcFont, "Bold"))
+    BackPolygonOffsetSentry (OpenGl_Context* theCtx)
+    : myCtx (theCtx)
     {
-      isBold = true;
-    }
-    if (strstr (theSrcFont, "Italic")
-     || strstr (theSrcFont, "Oblique"))
-    {
-      isItalic = true;
-    }
-
-    if (isBold)
-    {
-      if (isItalic)
+      if (theCtx != NULL)
       {
-        sprintf (thePsFont, "%s-Bold%s", TheFamily[aFontId], TheItalic[aFontId]);
+        myOffsetBack = theCtx->PolygonOffset();
+        Graphic3d_PolygonOffset aPolyOffset = myOffsetBack;
+        aPolyOffset.Mode = Aspect_POM_Fill;
+        aPolyOffset.Units += 1.0f;
+        theCtx->SetPolygonOffset (aPolyOffset);
       }
-      else
-      {
-        sprintf (thePsFont, "%s-Bold", TheFamily[aFontId]);
-      }
-    }
-    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.FontName().ToCString(), aPsFont);
 
-  #if !defined(GL_ES_VERSION_2_0)
-    if (theIs2d)
-    {
-      glRasterPos2f (0.0f, 0.0f);
-    }
-    else
+    ~BackPolygonOffsetSentry()
     {
-      glRasterPos3f (0.0f, 0.0f, 0.0f);
+      if (myCtx != NULL)
+      {
+        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, theAspect.Angle());
-  }
-#endif
+  private:
+    BackPolygonOffsetSentry (const BackPolygonOffsetSentry& );
+    BackPolygonOffsetSentry& operator= (const BackPolygonOffsetSentry& );
+  private:
+    OpenGl_Context* myCtx;
+    Graphic3d_PolygonOffset myOffsetBack;
+  };
 
-};
+} // anonymous namespace
 
 // =======================================================================
 // function : OpenGl_Text
 // purpose  :
 // =======================================================================
 OpenGl_Text::OpenGl_Text()
-: myWinX (0.0f),
-  myWinY (0.0f),
-  myWinZ (0.0f),
-  myScaleHeight (1.0f),
-  myPoint  (0.0f, 0.0f, 0.0f),
-  myIs2d   (false),
-  myHasPlane (false)
+: myScaleHeight (1.0f),
+  myIs2d        (Standard_False)
 {
-  myParams.Height = 10;
-  myParams.HAlign = Graphic3d_HTA_LEFT;
-  myParams.VAlign = Graphic3d_VTA_BOTTOM;
+  myText = new Graphic3d_Text (10.);
 }
 
-// =======================================================================
-// function : OpenGl_Text
-// purpose  :
-// =======================================================================
-OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
-                          const OpenGl_Vec3&       thePoint,
-                          const OpenGl_TextParam&  theParams)
-: myWinX (0.0f),
-  myWinY (0.0f),
-  myWinZ (0.0f),
-  myScaleHeight  (1.0f),
-  myExportHeight (1.0f),
-  myParams (theParams),
-  myString (theText),
-  myPoint  (thePoint),
-  myIs2d   (false),
-  myHasPlane (false)
-{
-  //
-}
 
 // =======================================================================
 // function : OpenGl_Text
 // purpose  :
 // =======================================================================
-OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
-                          const gp_Ax2&            theOrientation,
-                          const OpenGl_TextParam&  theParams)
-: myWinX         (0.0),
-  myWinY         (0.0),
-  myWinZ         (0.0),
-  myScaleHeight  (1.0),
-  myExportHeight (1.0),
-  myParams       (theParams),
-  myString       (theText),
-  myIs2d         (false),
-  myOrientation  (theOrientation),
-  myHasPlane     (true)
+OpenGl_Text::OpenGl_Text (const Handle(Graphic3d_Text)& theTextParams)
+: myText        (theTextParams),
+  myScaleHeight (1.0f),
+  myIs2d        (Standard_False)
 {
-  const gp_Pnt& aPoint = theOrientation.Location();
-  myPoint = OpenGl_Vec3 (static_cast<Standard_ShortReal> (aPoint.X()),
-                         static_cast<Standard_ShortReal> (aPoint.Y()),
-                         static_cast<Standard_ShortReal> (aPoint.Z()));
 }
 
 // =======================================================================
@@ -207,7 +99,7 @@ OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
 // =======================================================================
 void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint)
 {
-  myPoint = thePoint;
+  myText->SetPosition (gp_Pnt (thePoint.x(), thePoint.y(), thePoint.z()));
 }
 
 // =======================================================================
@@ -217,37 +109,20 @@ void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint)
 void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx,
                                const Standard_Integer        theFontSize)
 {
-  if (myParams.Height != theFontSize)
+  if (myText->Height() != theFontSize)
   {
     Release (theCtx.operator->());
   }
-  myParams.Height = theFontSize;
-}
-
-// =======================================================================
-// function : Init
-// purpose  :
-// =======================================================================
-void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
-                        const Standard_Utf8Char*      theText,
-                        const OpenGl_Vec3&            thePoint)
-{
-  releaseVbos (theCtx.operator->());
-  myIs2d   = false;
-  myPoint  = thePoint;
-  myString.FromUnicode (theText);
+  myText->SetHeight ((Standard_ShortReal)theFontSize);
 }
 
 // =======================================================================
-// function : Init
+// function : Reset
 // purpose  :
 // =======================================================================
-void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
-                        const Standard_Utf8Char*      theText,
-                        const OpenGl_Vec3&            thePoint,
-                        const OpenGl_TextParam&       theParams)
+void OpenGl_Text::Reset (const Handle(OpenGl_Context)& theCtx)
 {
-  if (myParams.Height != theParams.Height)
+  if (!myFont.IsNull() && myFont->FTFont()->PointSize() != myText->Height())
   {
     Release (theCtx.operator->());
   }
@@ -255,34 +130,23 @@ void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
   {
     releaseVbos (theCtx.operator->());
   }
-  myIs2d   = false;
-  myParams = theParams;
-  myPoint  = thePoint;
-  myString.FromUnicode (theText);
 }
 
 // =======================================================================
 // function : Init
 // purpose  :
 // =======================================================================
-void OpenGl_Text::Init (const Handle(OpenGl_Context)&     theCtx,
-                        const TCollection_ExtendedString& theText,
-                        const OpenGl_Vec2&                thePoint,
-                        const OpenGl_TextParam&           theParams)
+void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
+                        const Standard_Utf8Char*      theText,
+                        const OpenGl_Vec3&            thePoint)
 {
-  if (myParams.Height != theParams.Height)
-  {
-    Release (theCtx.operator->());
-  }
-  else
-  {
-    releaseVbos (theCtx.operator->());
-  }
-  myIs2d       = true;
-  myParams     = theParams;
-  myPoint.xy() = thePoint;
-  myPoint.z()  = 0.0f;
-  myString.FromUnicode ((Standard_Utf16Char* )theText.ToExtString());
+  Reset (theCtx);
+  Set2D (Standard_False);
+
+  NCollection_String aText;
+  aText.FromUnicode (theText);
+  myText->SetText (aText);
+  myText->SetPosition (gp_Pnt (thePoint.x(), thePoint.y(), thePoint.z()));
 }
 
 // =======================================================================
@@ -305,7 +169,7 @@ void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx)
     Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.ChangeValue (anIter);
     Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.ChangeValue (anIter);
 
-    if (theCtx)
+    if (theCtx != NULL)
     {
       theCtx->DelayedRelease (aVerts);
       theCtx->DelayedRelease (aTCrds);
@@ -313,9 +177,16 @@ void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx)
     aVerts.Nullify();
     aTCrds.Nullify();
   }
+  if (theCtx != NULL
+  && !myBndVertsVbo.IsNull())
+  {
+    theCtx->DelayedRelease (myBndVertsVbo);
+  }
+
   myTextures.Clear();
   myVertsVbo.Clear();
   myTCrdsVbo.Clear();
+  myBndVertsVbo.Nullify();
 }
 
 // =======================================================================
@@ -327,11 +198,58 @@ void OpenGl_Text::Release (OpenGl_Context* theCtx)
   releaseVbos (theCtx);
   if (!myFont.IsNull())
   {
-    Handle(OpenGl_Context) aCtx = theCtx;
     const TCollection_AsciiString aKey = myFont->ResourceKey();
     myFont.Nullify();
-    if (! aCtx.IsNull())
-      aCtx->ReleaseResource (aKey, Standard_True);
+    if (theCtx != NULL)
+    {
+      theCtx->ReleaseResource (aKey, Standard_True);
+    }
+  }
+}
+
+// =======================================================================
+// function : EstimatedDataSize
+// purpose  :
+// =======================================================================
+Standard_Size OpenGl_Text::EstimatedDataSize() const
+{
+  Standard_Size aSize = 0;
+  for (Standard_Integer anIter = myVertsVbo.Lower(); anIter <= myVertsVbo.Upper(); ++anIter)
+  {
+    if (const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter))
+    {
+      aSize += aVerts->EstimatedDataSize();
+    }
+    if (const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter))
+    {
+      aSize += aTCrds->EstimatedDataSize();
+    }
+  }
+  if (!myBndVertsVbo.IsNull())
+  {
+    aSize += myBndVertsVbo->EstimatedDataSize();
+  }
+  return aSize;
+}
+
+// =======================================================================
+// function : UpdateDrawStats
+// purpose  :
+// =======================================================================
+void OpenGl_Text::UpdateDrawStats (Graphic3d_FrameStatsDataTmp& theStats,
+                                   bool theIsDetailed) const
+{
+  ++theStats[Graphic3d_FrameStatsCounter_NbElemsNotCulled];
+  ++theStats[Graphic3d_FrameStatsCounter_NbElemsTextNotCulled];
+  if (theIsDetailed)
+  {
+    for (Standard_Integer anIter = myVertsVbo.Lower(); anIter <= myVertsVbo.Upper(); ++anIter)
+    {
+      if (const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter))
+      {
+        theStats[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += aVerts->GetElemsNb() / 3; // 2 non-indexed triangles per glyph
+      }
+    }
   }
 }
 
@@ -341,8 +259,8 @@ 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_TextParam&       theParams,
+                              const OpenGl_Aspects&         theTextAspect,
+                              const Standard_ShortReal      theHeight,
                               const unsigned int            theResolution,
                               Standard_ShortReal&           theWidth,
                               Standard_ShortReal&           theAscent,
@@ -351,8 +269,8 @@ void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
   theWidth   = 0.0f;
   theAscent  = 0.0f;
   theDescent = 0.0f;
-  const TCollection_AsciiString aFontKey = FontKey (theTextAspect, theParams.Height, theResolution);
-  Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, theResolution, aFontKey);
+  const TCollection_AsciiString aFontKey = FontKey (theTextAspect, (Standard_Integer)theHeight, theResolution);
+  Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, (Standard_Integer)theHeight, theResolution, aFontKey);
   if (aFont.IsNull() || !aFont->IsValid())
   {
     return;
@@ -407,46 +325,35 @@ void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
 // =======================================================================
 void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
 {
-  const OpenGl_AspectText*      aTextAspect  = theWorkspace->AspectText (Standard_True);
-  const Handle(OpenGl_Texture)  aPrevTexture = theWorkspace->DisableTexture();
-  const Handle(OpenGl_Context)& aCtx         = theWorkspace->GetGlContext();
+  const OpenGl_Aspects* aTextAspect = theWorkspace->ApplyAspects (false); // do not bind textures as they will be disabled anyway
+  const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
 
   // Bind custom shader program or generate default version
-  if (aCtx->core20fwd != NULL)
+  aCtx->ShaderManager()->BindFontProgram (aTextAspect->ShaderProgramRes (aCtx));
+  const Handle(OpenGl_TextureSet) aPrevTexture = aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
+
+  if (myText->HasPlane() && myText->HasOwnAnchorPoint())
   {
-    aCtx->ShaderManager()->BindProgram (
-      aTextAspect, aTextAspect->ShaderProgramRes (aCtx));
+    myOrientationMatrix = aCtx->Camera()->OrientationMatrix();
+    // reset translation part
+    myOrientationMatrix.ChangeValue (0, 3) = 0.0;
+    myOrientationMatrix.ChangeValue (1, 3) = 0.0;
+    myOrientationMatrix.ChangeValue (2, 3) = 0.0;
   }
 
-  myOrientationMatrix = theWorkspace->View()->Camera()->OrientationMatrix();
   myProjMatrix.Convert (aCtx->ProjectionState.Current());
 
   // use highlight color or colors from aspect
-  if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
-  {
-    render (theWorkspace->PrinterContext(),
-            aCtx,
-            *aTextAspect,
-            *theWorkspace->HighlightColor,
-            *theWorkspace->HighlightColor,
-            theWorkspace->View()->RenderingParams().Resolution);
-  }
-  else
-  {
-    render (theWorkspace->PrinterContext(),
-            aCtx,
-            *aTextAspect,
-            aTextAspect->Color(),
-            aTextAspect->SubtitleColor(),
-            theWorkspace->View()->RenderingParams().Resolution);
-  }
-
-  aCtx->BindProgram (NULL);
+  render (aCtx,
+          *aTextAspect,
+          theWorkspace->TextColor(),
+          theWorkspace->TextSubtitleColor(),
+          aCtx->Resolution());
 
   // restore aspects
   if (!aPrevTexture.IsNull())
   {
-    theWorkspace->EnableTexture (aPrevTexture);
+    aCtx->BindTextures (aPrevTexture, Handle(OpenGl_ShaderProgram)());
   }
 
   // restore Z buffer settings
@@ -460,27 +367,36 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
 // function : Render
 // purpose  :
 // =======================================================================
-void OpenGl_Text::Render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
-                          const Handle(OpenGl_Context)&        theCtx,
-                          const OpenGl_AspectText&             theTextAspect,
-                          const unsigned int                   theResolution) const
+void OpenGl_Text::Render (const Handle(OpenGl_Context)& theCtx,
+                          const OpenGl_Aspects& theTextAspect,
+                          unsigned int theResolution) const
 {
-  render (thePrintCtx, theCtx, theTextAspect, theTextAspect.Color(), theTextAspect.SubtitleColor(), theResolution);
+#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
 }
 
 // =======================================================================
 // function : setupMatrix
 // purpose  :
 // =======================================================================
-void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
-                               const Handle(OpenGl_Context)&        theCtx,
-                               const OpenGl_AspectText&             theTextAspect,
-                               const OpenGl_Vec3                    theDVec) const
+void OpenGl_Text::setupMatrix (const Handle(OpenGl_Context)& theCtx,
+                               const OpenGl_Aspects& theTextAspect,
+                               const OpenGl_Vec3& theDVec) const
 {
-  OpenGl_Mat4d aModViewMat;
-  OpenGl_Mat4d aProjectMat;
-
-  if (myHasPlane)
+  OpenGl_Mat4d aModViewMat, aProjectMat;
+  if (myText->HasPlane() && myText->HasOwnAnchorPoint())
   {
     aProjectMat = myProjMatrix * myOrientationMatrix;
   }
@@ -491,65 +407,84 @@ void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
 
   if (myIs2d)
   {
-    Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.f);
+    const gp_Pnt& aPoint = myText->Position();
+    Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, aPoint.X() + theDVec.x(), aPoint.Y() + theDVec.y(), 0.f);
     Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, 1.f, -1.f, 1.f);
-    Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Angle(), 0.f, 0.f, 1.f);
+    Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.f, 0.f, 1.f);
   }
   else
   {
-    // align coordinates to the nearest integer
-    // to avoid extra interpolation issues
-    GLdouble anObjX, anObjY, anObjZ;
-    Graphic3d_TransformUtils::UnProject<Standard_Real> (std::floor (myWinX + theDVec.x()),
-                                                        std::floor (myWinY + theDVec.y()),
-                                                        myWinZ + theDVec.z(),
-                                                        OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
-                                                        OpenGl_Mat4d::Map (aProjectMat),
-                                                        myViewport,
-                                                        anObjX,
-                                                        anObjY,
-                                                        anObjZ);
-
-    if (myHasPlane)
-    {
-      const gp_Dir& aVectorDir   = myOrientation.XDirection();
-      const gp_Dir& aVectorUp    = myOrientation.Direction();
-      const gp_Dir& aVectorRight = myOrientation.YDirection();
-
-      aModViewMat.SetColumn (3, OpenGl_Vec3d (anObjX, anObjY, anObjZ));
+    OpenGl_Vec3d anObjXYZ;
+    OpenGl_Vec3d aWinXYZ = myWinXYZ + OpenGl_Vec3d (theDVec);
+    if (!myText->HasPlane() && !theTextAspect.Aspect()->IsTextZoomable())
+    {
+      // Align coordinates to the nearest integer to avoid extra interpolation issues.
+      // Note that for better readability we could also try aligning freely rotated in 3D text (myHasPlane),
+      // when camera orientation co-aligned with horizontal text orientation,
+      // but this might look awkward while rotating camera.
+      aWinXYZ.x() = Floor (aWinXYZ.x());
+      aWinXYZ.y() = Floor (aWinXYZ.y());
+    }
+    Graphic3d_TransformUtils::UnProject<Standard_Real> (aWinXYZ.x(), aWinXYZ.y(), aWinXYZ.z(),
+                                                        THE_IDENTITY_MATRIX, aProjectMat, theCtx->Viewport(),
+                                                        anObjXYZ.x(), anObjXYZ.y(), anObjXYZ.z());
+
+    if (myText->HasPlane())
+    {
+      const gp_Ax2& anOrientation = myText->Orientation();
+      const gp_Dir& aVectorDir   = anOrientation.XDirection();
+      const gp_Dir& aVectorUp    = anOrientation.Direction();
+      const gp_Dir& aVectorRight = anOrientation.YDirection();
+
       aModViewMat.SetColumn (2, OpenGl_Vec3d (aVectorUp.X(), aVectorUp.Y(), aVectorUp.Z()));
       aModViewMat.SetColumn (1, OpenGl_Vec3d (aVectorRight.X(), aVectorRight.Y(), aVectorRight.Z()));
       aModViewMat.SetColumn (0, OpenGl_Vec3d (aVectorDir.X(), aVectorDir.Y(), aVectorDir.Z()));
+
+      if (!myText->HasOwnAnchorPoint())
+      {
+        OpenGl_Mat4d aPosMat;
+        const gp_Pnt& aPoint = myText->Position();
+        aPosMat.SetColumn (3, OpenGl_Vec3d (aPoint.X(), aPoint.Y(), aPoint.Z()));
+        aPosMat *= aModViewMat;
+        aModViewMat.SetColumn (3, aPosMat.GetColumn (3));
+      }
+      else
+      {
+        aModViewMat.SetColumn (3, anObjXYZ);
+      }
     }
     else
     {
-      Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, anObjX, anObjY, anObjZ);
-      Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Angle(), 0.0, 0.0, 1.0);
+      Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, anObjXYZ.x(), anObjXYZ.y(), anObjXYZ.z());
+      Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.0, 0.0, 1.0);
     }
 
-    if (!theTextAspect.IsZoomable())
+    if (!theTextAspect.Aspect()->IsTextZoomable())
     {
-    #ifdef _WIN32
-      // if the context has assigned printer context, use it's parameters
-      if (!thePrintCtx.IsNull())
-      {
-        // get printing scaling in x and y dimensions
-        GLfloat aTextScalex = 1.0f, aTextScaley = 1.0f;
-        thePrintCtx->GetScale (aTextScalex, aTextScaley);
-
-        // text should be scaled in all directions with same
-        // factor to save its proportions, so use height (y) scaling
-        // as it is better for keeping text/3d graphics proportions
-        Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, aTextScaley, aTextScaley, aTextScaley);
-      }
-    #else
-      (void )thePrintCtx;
-    #endif
       Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, myScaleHeight, myScaleHeight, myScaleHeight);
     }
+    else if (theCtx->HasRenderScale())
+    {
+      Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, theCtx->RenderScaleInv(), theCtx->RenderScaleInv(), theCtx->RenderScaleInv());
+    }
   }
 
-  theCtx->WorldViewState.SetCurrent<Standard_Real> (aModViewMat);
+  if (myText->HasPlane() && !myText->HasOwnAnchorPoint())
+  {
+    OpenGl_Mat4d aCurrentWorldViewMat;
+    aCurrentWorldViewMat.Convert (theCtx->WorldViewState.Current());
+
+    // apply local transformation
+    OpenGl_Mat4d aModelWorld;
+    aModelWorld.Convert (theCtx->ModelWorldState.Current());
+    aCurrentWorldViewMat = aCurrentWorldViewMat * aModelWorld;
+
+    theCtx->WorldViewState.SetCurrent<Standard_Real> (aCurrentWorldViewMat * aModViewMat);
+  }
+  else
+  {
+    theCtx->WorldViewState.SetCurrent<Standard_Real> (aModViewMat);
+  }
   theCtx->ApplyWorldViewMatrix();
 
   if (!myIs2d)
@@ -558,35 +493,18 @@ void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
     theCtx->ApplyProjectionMatrix();
   }
 
-  if (!theCtx->ActiveProgram().IsNull())
-  {
-    // Upload updated state to shader program
-    theCtx->ShaderManager()->PushState (theCtx->ActiveProgram());
-  }
+  // Upload updated state to shader program
+  theCtx->ShaderManager()->PushState (theCtx->ActiveProgram());
 }
 
 // =======================================================================
 // function : drawText
 // purpose  :
 // =======================================================================
-
-void OpenGl_Text::drawText (const Handle(OpenGl_PrinterContext)& ,
-                            const Handle(OpenGl_Context)&        theCtx,
-                          #ifdef HAVE_GL2PS
-                            const OpenGl_AspectText&             theTextAspect) const
-                          #else
-                            const OpenGl_AspectText&                          ) const
-                          #endif
+void OpenGl_Text::drawText (const Handle(OpenGl_Context)& theCtx,
+                            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;
-  }
-#endif
-
+  (void )theTextAspect;
   if (myVertsVbo.Length() != myTextures.Length()
    || myTextures.IsEmpty())
   {
@@ -615,12 +533,15 @@ void OpenGl_Text::drawText (const Handle(OpenGl_PrinterContext)& ,
 // 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.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
-  return theAspect.FontName()
+  const Font_FontAspect anAspect = theAspect.Aspect()->TextFontAspect() != Font_FA_Undefined
+                                 ? theAspect.Aspect()->TextFontAspect()
+                                 : Font_FA_Regular;
+  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;
@@ -631,10 +552,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)
@@ -645,48 +566,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.FontName());
-    const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : 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 (NULL);
-
-      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.FontName();
-          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.FontName();
-        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.FontName();
-      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);
     }
 
@@ -695,25 +599,75 @@ Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
   return aFont;
 }
 
+// =======================================================================
+// function : drawRect
+// purpose  :
+// =======================================================================
+void OpenGl_Text::drawRect (const Handle(OpenGl_Context)& theCtx,
+                            const OpenGl_Aspects& theTextAspect,
+                            const OpenGl_Vec4& theColorSubs) const
+{
+  Handle(OpenGl_ShaderProgram) aPrevProgram = theCtx->ActiveProgram();
+  if (myBndVertsVbo.IsNull())
+  {
+    OpenGl_Vec2 aQuad[4] =
+    {
+      OpenGl_Vec2(myBndBox.Right, myBndBox.Bottom),
+      OpenGl_Vec2(myBndBox.Right, myBndBox.Top),
+      OpenGl_Vec2(myBndBox.Left,  myBndBox.Bottom),
+      OpenGl_Vec2(myBndBox.Left,  myBndBox.Top)
+    };
+    if (theCtx->ToUseVbo())
+    {
+      myBndVertsVbo = new OpenGl_VertexBuffer();
+    }
+    else
+    {
+      myBndVertsVbo = new OpenGl_VertexBufferCompat();
+    }
+    myBndVertsVbo->Init (theCtx, 2, 4, aQuad[0].GetData());
+  }
+
+  // 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
+   && theCtx->ActiveProgram().IsNull())
+  {
+    glBindTexture (GL_TEXTURE_2D, 0);
+  }
+#endif
+  theCtx->SetColor4fv (theColorSubs);
+  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);
+
+  myBndVertsVbo->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
+  theCtx->BindProgram (aPrevProgram);
+}
+
 // =======================================================================
 // function : render
 // purpose  :
 // =======================================================================
-void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
-                          const Handle(OpenGl_Context)&        theCtx,
-                          const OpenGl_AspectText&             theTextAspect,
-                          const TEL_COLOUR&                    theColorText,
-                          const TEL_COLOUR&                    theColorSubs,
-                          const unsigned int                   theResolution) const
+void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
+                          const OpenGl_Aspects& theTextAspect,
+                          const OpenGl_Vec4& theColorText,
+                          const OpenGl_Vec4& theColorSubs,
+                          unsigned int theResolution) const
 {
-  if (myString.IsEmpty())
+  if (myText->Text().IsEmpty() && myText->TextFormatter().IsNull())
   {
     return;
   }
 
   // Note that using difference resolution in different Views in same Viewer
   // will lead to performance regression (for example, text will be recreated every time).
-  const TCollection_AsciiString aFontKey = FontKey (theTextAspect, myParams.Height, theResolution);
+  const TCollection_AsciiString aFontKey = FontKey (theTextAspect, (Standard_Integer)myText->Height(), theResolution);
   if (!myFont.IsNull()
    && !myFont->ResourceKey().IsEqual (aFontKey))
   {
@@ -723,7 +677,7 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
 
   if (myFont.IsNull())
   {
-    myFont = FindFont (theCtx, theTextAspect, myParams.Height, theResolution, aFontKey);
+    myFont = FindFont (theCtx, theTextAspect, (Standard_Integer)myText->Height(), theResolution, aFontKey);
   }
   if (!myFont->WasInitialized())
   {
@@ -732,12 +686,16 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
 
   if (myTextures.IsEmpty())
   {
-    Font_TextFormatter aFormatter;
-    aFormatter.SetupAlignment (myParams.HAlign, myParams.VAlign);
-    aFormatter.Reset();
+    Handle(Font_TextFormatter) aFormatter = myText->TextFormatter();
+    if (aFormatter.IsNull())
+    {
+      aFormatter = new Font_TextFormatter();
+    }
+    aFormatter->SetupAlignment (myText->HorizontalAlignment(), myText->VerticalAlignment());
+    aFormatter->Reset();
 
-    aFormatter.Append (myString, *myFont->FTFont().operator->());
-    aFormatter.Format();
+    aFormatter->Append (myText->Text(), *myFont->FTFont());
+    aFormatter->Format();
 
     OpenGl_TextBuilder aBuilder;
     aBuilder.Perform (aFormatter,
@@ -747,7 +705,12 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
                       myVertsVbo,
                       myTCrdsVbo);
 
-    aFormatter.BndBox (myBndBox);
+    aFormatter->BndBox (myBndBox);
+    if (!myBndVertsVbo.IsNull())
+    {
+      myBndVertsVbo->Release (theCtx.get());
+      myBndVertsVbo.Nullify();
+    }
   }
 
   if (myTextures.IsEmpty())
@@ -755,69 +718,45 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
     return;
   }
 
-  myExportHeight = 1.0f;
   myScaleHeight  = 1.0f;
 
   theCtx->WorldViewState.Push();
-
   myModelMatrix.Convert (theCtx->WorldViewState.Current() * theCtx->ModelWorldState.Current());
 
+  const GLdouble aPointSize = (GLdouble )myFont->FTFont()->PointSize();
   if (!myIs2d)
   {
-    glGetIntegerv (GL_VIEWPORT,          myViewport);
-
-    Graphic3d_TransformUtils::Project<Standard_Real> (myPoint.x(),
-                                                      myPoint.y(),
-                                                      myPoint.z(),
-                                                      myModelMatrix,
-                                                      myProjMatrix,
-                                                      myViewport,
-                                                      myWinX,
-                                                      myWinY,
-                                                      myWinZ);
+    const gp_Pnt& aPoint = myText->Position();
+    Graphic3d_TransformUtils::Project<Standard_Real> (aPoint.X(), aPoint.Y(), aPoint.Z(),
+                                                      myModelMatrix, myProjMatrix, theCtx->Viewport(),
+                                                      myWinXYZ.x(), myWinXYZ.y(), myWinXYZ.z());
 
     // compute scale factor for constant text height
-    GLdouble x1, y1, z1;
-    Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinX,
-                                                        myWinY,
-                                                        myWinZ,
-                                                        OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
-                                                        myProjMatrix,
-                                                        myViewport,
-                                                        x1,
-                                                        y1,
-                                                        z1);
-
-    GLdouble x2, y2, z2;
-    const GLdouble h = (GLdouble )myFont->FTFont()->PointSize();
-    Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinX,
-                                                        myWinY + h,
-                                                        myWinZ,
-                                                        OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
-                                                        myProjMatrix,
-                                                        myViewport,
-                                                        x2,
-                                                        y2,
-                                                        z2);
-
-    myScaleHeight = (y2 - y1) / h;
-    if (theTextAspect.IsZoomable())
-    {
-      myExportHeight = (float )h;
+    if (!theTextAspect.Aspect()->IsTextZoomable())
+    {
+      Graphic3d_Vec3d aPnt1, aPnt2;
+      Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinXYZ.x(), myWinXYZ.y(), myWinXYZ.z(),
+                                                          THE_IDENTITY_MATRIX, myProjMatrix, theCtx->Viewport(),
+                                                          aPnt1.x(), aPnt1.y(), aPnt1.z());
+      Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinXYZ.x(), myWinXYZ.y() + aPointSize, myWinXYZ.z(),
+                                                          THE_IDENTITY_MATRIX, myProjMatrix, theCtx->Viewport(),
+                                                          aPnt2.x(), aPnt2.y(), aPnt2.z());
+      myScaleHeight = (aPnt2.y() - aPnt1.y()) / aPointSize;
     }
   }
-  myExportHeight = (float )myFont->FTFont()->PointSize() / myExportHeight;
 
 #if !defined(GL_ES_VERSION_2_0)
-  if (theCtx->core11 != NULL)
+  if (theCtx->core11 != NULL
+   && theCtx->caps->ffpEnable)
   {
     glDisable (GL_LIGHTING);
   }
 #endif
 
   // setup depth test
-  if (myIs2d
-   || theTextAspect.StyleType() == Aspect_TOST_ANNOTATION)
+  const bool hasDepthTest = !myIs2d
+                         && theTextAspect.Aspect()->TextStyle() != Aspect_TOST_ANNOTATION;
+  if (!hasDepthTest)
   {
     glDisable (GL_DEPTH_TEST);
   }
@@ -831,10 +770,6 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   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);
@@ -849,8 +784,11 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   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.DisplayType())
+  switch (theTextAspect.Aspect()->TextDisplayType())
   {
     case Aspect_TODT_BLEND:
     {
@@ -862,34 +800,30 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
     }
     case Aspect_TODT_SUBTITLE:
     {
-    #if !defined(GL_ES_VERSION_2_0)
-      if (theCtx->core11 != NULL)
-      {
-        theCtx->core11->glColor3fv (theColorSubs.rgb);
-        setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
-
-        glBindTexture (GL_TEXTURE_2D, 0);
-        glBegin (GL_QUADS);
-        glVertex2f (myBndBox.Left,  myBndBox.Top);
-        glVertex2f (myBndBox.Right, myBndBox.Top);
-        glVertex2f (myBndBox.Right, myBndBox.Bottom);
-        glVertex2f (myBndBox.Left,  myBndBox.Bottom);
-        glEnd();
-      }
-    #endif
+      BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
+      drawRect (theCtx, theTextAspect, theColorSubs);
       break;
     }
     case Aspect_TODT_DEKALE:
     {
-      theCtx->SetColor4fv (*(const OpenGl_Vec4* )theColorSubs.rgb);
-      setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.00001f));
-      drawText    (thePrintCtx, theCtx, theTextAspect);
-      setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.00001f));
-      drawText    (thePrintCtx, theCtx, theTextAspect);
-      setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.00001f));
-      drawText    (thePrintCtx, theCtx, theTextAspect);
-      setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.00001f));
-      drawText    (thePrintCtx, theCtx, theTextAspect);
+      BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
+      theCtx->SetColor4fv (theColorSubs);
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.0f));
+      drawText    (theCtx, theTextAspect);
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.0f));
+      drawText    (theCtx, theTextAspect);
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.0f));
+      drawText    (theCtx, theTextAspect);
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
+      drawText    (theCtx, theTextAspect);
+      break;
+    }
+    case Aspect_TODT_SHADOW:
+    {
+      BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
+      theCtx->SetColor4fv (theColorSubs);
+      setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
+      drawText    (theCtx, theTextAspect);
       break;
     }
     case Aspect_TODT_DIMENSION:
@@ -900,9 +834,9 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   }
 
   // main draw call
-  theCtx->SetColor4fv (*(const OpenGl_Vec4* )theColorText.rgb);
-  setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
-  drawText    (thePrintCtx, theCtx, theTextAspect);
+  theCtx->SetColor4fv (theColorText);
+  setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
+  drawText    (theCtx, theTextAspect);
 
   if (!myIs2d)
   {
@@ -917,10 +851,8 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   }
 #endif
 
-  if (theTextAspect.DisplayType() == Aspect_TODT_DIMENSION)
+  if (theTextAspect.Aspect()->TextDisplayType() == Aspect_TODT_DIMENSION)
   {
-    setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
-
     glDisable (GL_BLEND);
     if (!myIs2d)
     {
@@ -930,41 +862,26 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
     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);
     glStencilFunc (GL_ALWAYS, 1, 0xFF);
     glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
 
-  #if !defined(GL_ES_VERSION_2_0)
-    if (theCtx->core11 != NULL)
-    {
-      glBegin (GL_QUADS);
-      glVertex2f (myBndBox.Left,  myBndBox.Top);
-      glVertex2f (myBndBox.Right, myBndBox.Top);
-      glVertex2f (myBndBox.Right, myBndBox.Bottom);
-      glVertex2f (myBndBox.Left,  myBndBox.Bottom);
-      glEnd();
-    }
-  #endif
+    drawRect (theCtx, theTextAspect, OpenGl_Vec4 (1.0f, 1.0f, 1.0f, 1.0f));
 
     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
 
@@ -972,3 +889,23 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   theCtx->WorldViewState.Pop();
   theCtx->ApplyModelViewMatrix();
 }
+
+// =======================================================================
+// function : DumpJson
+// purpose  :
+// =======================================================================
+void OpenGl_Text::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
+{
+  OCCT_DUMP_CLASS_BEGIN (theOStream, OpenGl_Text)
+  OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_Element)
+
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTextures.Size())
+
+  for (NCollection_Vector<Handle(OpenGl_VertexBuffer)>::Iterator aCrdsIt (myTCrdsVbo); aCrdsIt.More(); aCrdsIt.Next())
+  {
+    const Handle(OpenGl_VertexBuffer)& aVertexBuffer = aCrdsIt.Value();
+    OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aVertexBuffer.get())
+  }
+
+  OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myBndBox)
+}