]> OCCT Git - occt.git/commitdiff
0026298: Visualization, OpenGl_Text - make font resolution configurable.
authorisk <isk@opencascade.com>
Tue, 29 Sep 2015 09:17:58 +0000 (12:17 +0300)
committerbugmaster <bugmaster@opencascade.com>
Thu, 1 Oct 2015 10:54:22 +0000 (13:54 +0300)
Add THE_DEFAULT_RESOLUTION static const parameter to Graphic3d_RenderingParams.
Add resolution to Graphic3d_RenderingParams.
Drop redundant variables from OpenGl_Workspace.
Add a new parameter to Graphic3d_GraphicDriver::TextSize method (and to OpenGl_GraphicDriver).
Add a new parameter to OpenGl_Text::Render() and to OpenGl_Text::render() methods.
OpenGl_Text::FontKey() considers a resolution (PPI) now.
Add a new argument '-resolution' (sets a pixel density) in the 'VRenderParams' draw-command.

12 files changed:
src/AIS/AIS_ColorScale.cxx
src/Font/Font_FTFont.hxx
src/Graphic3d/Graphic3d_GraphicDriver.hxx
src/Graphic3d/Graphic3d_RenderingParams.hxx
src/OpenGl/OpenGl_GraphicDriver.cxx
src/OpenGl/OpenGl_GraphicDriver.hxx
src/OpenGl/OpenGl_Text.cxx
src/OpenGl/OpenGl_Text.hxx
src/OpenGl/OpenGl_Workspace.cxx
src/OpenGl/OpenGl_Workspace.hxx
src/ViewerTest/ViewerTest_ViewerCommands.cxx
tests/3rdparty/fonts/C1 [new file with mode: 0644]

index 562f70e1f8e632ddf0914ebad6ac3c66a622cf6a..93a1f682dcd5e8b097ba379783b65264a171813d 100644 (file)
@@ -776,9 +776,10 @@ Standard_Integer AIS_ColorScale::TextHeight (const TCollection_ExtendedString& t
 //=======================================================================
 void AIS_ColorScale::TextSize (const TCollection_ExtendedString& theText, const Standard_Integer theHeight, Standard_Integer& theWidth, Standard_Integer& theAscent, Standard_Integer& theDescent) const
 {
+  const Handle(Graphic3d_CView)& aView = GetContext()->CurrentViewer()->ActiveView()->View();
   Standard_ShortReal aWidth(10.0), anAscent(1.0), aDescent(1.0);
   TCollection_AsciiString aText (theText.ToExtString(), '?');
-  GetContext()->CurrentViewer()->Driver()->TextSize (aText.ToCString(),(Standard_ShortReal)theHeight,aWidth,anAscent,aDescent);
+  GetContext()->CurrentViewer()->Driver()->TextSize (aView, aText.ToCString(), (Standard_ShortReal)theHeight, aWidth, anAscent, aDescent);
   theWidth = (Standard_Integer)aWidth;
   theAscent = (Standard_Integer)anAscent;
   theDescent = (Standard_Integer)aDescent;
index 9810abe6107c505f3ff2bed2f7d7ada9ab26da7b..d7ea03b0316c73717fa2e292ee5b1c1d6156a2d1 100755 (executable)
@@ -112,7 +112,7 @@ public:
   //! @return true on success
   Standard_EXPORT bool Init (const NCollection_String& theFontPath,
                              const unsigned int        thePointSize,
-                             const unsigned int        theResolution = 72);
+                             const unsigned int        theResolution);
 
   //! Initialize the font.
   //! @param theFontName   the font name
index 3bc5fa9e610497f789a302d5de1f6fe4c88b5ef1..c54b29b6923008ea5c6569efe07b911228546319 100644 (file)
@@ -99,8 +99,13 @@ public:
   
   virtual Standard_ShortReal DefaultTextHeight() const = 0;
   
-  //! call_togl_textsize2d
-  virtual void TextSize (const Standard_CString AText, const Standard_ShortReal AHeight, Standard_ShortReal& AWidth, Standard_ShortReal& AnAscent, Standard_ShortReal& ADescent) const = 0;
+  //! Computes text width.
+  virtual void TextSize (const Handle(Graphic3d_CView)& theView,
+                         const Standard_CString         theText,
+                         const Standard_ShortReal       theHeight,
+                         Standard_ShortReal&            theWidth,
+                         Standard_ShortReal&            theAscent,
+                         Standard_ShortReal&            theDescent) const = 0;
 
   //! Add a new top-level z layer with ID <theLayerId> for
   //! the view. Z layers allow drawing structures in higher layers
index 1ee0e656f7efdba0388c126a7fb585dfda38d9b6..b57421382f0b0954d744a380342e38f82f716d88 100644 (file)
@@ -25,6 +25,8 @@
 class Graphic3d_RenderingParams
 {
 public:
+  //! Default pixels density.
+  static const unsigned int THE_DEFAULT_RESOLUTION = 72u;
 
   //! Default number of samples per pixel.
   static const Standard_Integer THE_DEFAULT_SPP = 1;
@@ -60,7 +62,9 @@ public:
 
     StereoMode (Graphic3d_StereoMode_QuadBuffer),
     AnaglyphFilter (Anaglyph_RedCyan_Optimized),
-    ToReverseStereo (Standard_False)
+    ToReverseStereo (Standard_False),
+
+    Resolution (THE_DEFAULT_RESOLUTION)
   {
     const Graphic3d_Vec4 aZero (0.0f, 0.0f, 0.0f, 0.0f);
     AnaglyphLeft .SetRow (0, Graphic3d_Vec4 (1.0f,  0.0f,  0.0f, 0.0f));
@@ -93,6 +97,12 @@ public:
   Graphic3d_Mat4          AnaglyphRight;               //!< right anaglyph filter (in normalized colorspace), Color = AnaglyphRight * theColorRight + AnaglyphLeft * theColorLeft;
   Standard_Boolean        ToReverseStereo;             //!< flag to reverse stereo pair, FALSE by default
 
+  unsigned int            Resolution;                  //!< Pixels density (PPI), defines scaling factor for parameters like text size
+                                                       //!< (when defined in screen-space units rather than in 3D) to be properly displayed
+                                                       //!< on device (screen / printer). 72 is default value.
+                                                       //!< Note that using difference resolution in different Views in same Viewer
+                                                       //!< will lead to performance regression (for example, text will be recreated every time).
+
 };
 
 #endif // _Graphic3d_RenderingParams_HeaderFile
index 8a3e2106cb0a60774a63e7e91857120cead4c1e7..2e9c9594c0783eaa2c6e109715b1089ba815fec4 100644 (file)
@@ -472,11 +472,12 @@ void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
 // function : TextSize
 // purpose  :
 // =======================================================================
-void OpenGl_GraphicDriver::TextSize (const Standard_CString   theText,
-                                     const Standard_ShortReal theHeight,
-                                     Standard_ShortReal&      theWidth,
-                                     Standard_ShortReal&      theAscent,
-                                     Standard_ShortReal&      theDescent) const
+void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
+                                     const Standard_CString         theText,
+                                     const Standard_ShortReal       theHeight,
+                                     Standard_ShortReal&            theWidth,
+                                     Standard_ShortReal&            theAscent,
+                                     Standard_ShortReal&            theDescent) const
 {
   const Handle(OpenGl_Context)& aCtx = GetSharedContext();
   if (aCtx.IsNull())
@@ -506,7 +507,7 @@ void OpenGl_GraphicDriver::TextSize (const Standard_CString   theText,
   aTextAspect.SetAspect(aDefaultContextText);
   TCollection_ExtendedString anExtText = theText;
   NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
-  OpenGl_Text::StringSize (aCtx, aText, aTextAspect, aTextParam, theWidth, theAscent, theDescent);
+  OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
 }
 
 //=======================================================================
index b92e9e3f604561f50324b83352fce2bc3693d8b7..c78b8438c13f50bb36fe740b68fab839d91e9ed1 100644 (file)
@@ -132,7 +132,12 @@ public:
 
 public:
 
-  Standard_EXPORT void TextSize (const Standard_CString AText,const Standard_ShortReal AHeight,Standard_ShortReal& AWidth,Standard_ShortReal& AnAscent,Standard_ShortReal& ADescent) const;
+  Standard_EXPORT void TextSize (const Handle(Graphic3d_CView)& theView,
+                                 const Standard_CString         theText,
+                                 const Standard_ShortReal       theHeight,
+                                 Standard_ShortReal&            theWidth,
+                                 Standard_ShortReal&            theAscent,
+                                 Standard_ShortReal&            theDescent) const;
 
   Standard_EXPORT Standard_Integer InquirePlaneLimit();
 
index babd557c13223658170f4457612c9573144fc098..412b459854ca26ae6efd817ed2877e1ca5fa34cf 100644 (file)
@@ -342,6 +342,7 @@ void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
                               const NCollection_String&     theText,
                               const OpenGl_AspectText&      theTextAspect,
                               const OpenGl_TextParam&       theParams,
+                              const unsigned int            theResolution,
                               Standard_ShortReal&           theWidth,
                               Standard_ShortReal&           theAscent,
                               Standard_ShortReal&           theDescent)
@@ -349,8 +350,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);
-  Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, aFontKey);
+  const TCollection_AsciiString aFontKey = FontKey (theTextAspect, theParams.Height, theResolution);
+  Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, theResolution, aFontKey);
   if (aFont.IsNull() || !aFont->IsValid())
   {
     return;
@@ -426,7 +427,8 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
             aCtx,
             *aTextAspect,
             *theWorkspace->HighlightColor,
-            *theWorkspace->HighlightColor);
+            *theWorkspace->HighlightColor,
+            theWorkspace->View()->RenderingParams().Resolution);
   }
   else
   {
@@ -434,7 +436,8 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
             aCtx,
             *aTextAspect,
             aTextAspect->Color(),
-            aTextAspect->SubtitleColor());
+            aTextAspect->SubtitleColor(),
+            theWorkspace->View()->RenderingParams().Resolution);
   }
 
   aCtx->BindProgram (NULL);
@@ -458,9 +461,10 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
 // =======================================================================
 void OpenGl_Text::Render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
                           const Handle(OpenGl_Context)&        theCtx,
-                          const OpenGl_AspectText&             theTextAspect) const
+                          const OpenGl_AspectText&             theTextAspect,
+                          const unsigned int                   theResolution) const
 {
-  render (thePrintCtx, theCtx, theTextAspect, theTextAspect.Color(), theTextAspect.SubtitleColor());
+  render (thePrintCtx, theCtx, theTextAspect, theTextAspect.Color(), theTextAspect.SubtitleColor(), theResolution);
 }
 
 // =======================================================================
@@ -609,11 +613,13 @@ void OpenGl_Text::drawText (const Handle(OpenGl_PrinterContext)& ,
 // purpose  :
 // =======================================================================
 TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_AspectText& theAspect,
-                                              const Standard_Integer   theHeight)
+                                              const Standard_Integer   theHeight,
+                                              const unsigned int       theResolution)
 {
   const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
   return theAspect.FontName()
        + TCollection_AsciiString(":") + Standard_Integer(anAspect)
+       + TCollection_AsciiString(":") + Standard_Integer(theResolution)
        + TCollection_AsciiString(":") + theHeight;
 }
 
@@ -624,6 +630,7 @@ TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_AspectText& theAspect
 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)
 {
   Handle(OpenGl_Font) aFont;
@@ -643,7 +650,7 @@ Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
     {
       aFontFt = new Font_FTFont (NULL);
 
-      if (aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight))
+      if (aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight, theResolution))
       {
         aFont = new OpenGl_Font (aFontFt, theKey);
         if (!aFont->Init (theCtx))
@@ -693,14 +700,17 @@ 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
+                          const TEL_COLOUR&                    theColorSubs,
+                          const unsigned int                   theResolution) const
 {
   if (myString.IsEmpty())
   {
     return;
   }
 
-  const TCollection_AsciiString aFontKey = FontKey (theTextAspect, myParams.Height);
+  // 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);
   if (!myFont.IsNull()
    && !myFont->ResourceKey().IsEqual (aFontKey))
   {
@@ -710,7 +720,7 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
 
   if (myFont.IsNull())
   {
-    myFont = FindFont (theCtx, theTextAspect, myParams.Height, aFontKey);
+    myFont = FindFont (theCtx, theTextAspect, myParams.Height, theResolution, aFontKey);
   }
   if (!myFont->WasInitialized())
   {
index aa15e1e1dde9b4d713102e879b9cb149f6d0c19a..207b6ac2cd4c2bf7c3186261efd74462c61a7563 100755 (executable)
@@ -25,6 +25,7 @@
 #include <TCollection_ExtendedString.hxx>
 #include <Graphic3d_Vertex.hxx>
 #include <Graphic3d_HorizontalTextAlignment.hxx>
+#include <Graphic3d_RenderingParams.hxx>
 #include <Graphic3d_VerticalTextAlignment.hxx>
 
 #include <gp_Ax2.hxx>
@@ -75,12 +76,14 @@ public: //! @name methods for compatibility with layers
 
   //! Create key for shared resource
   Standard_EXPORT static TCollection_AsciiString FontKey (const OpenGl_AspectText& theAspect,
-                                                          const Standard_Integer   theHeight);
+                                                          const Standard_Integer   theHeight,
+                                                          const unsigned int       theResolution);
 
   //! Find shared resource for specified font or initialize new one
   Standard_EXPORT static Handle(OpenGl_Font) FindFont (const Handle(OpenGl_Context)& theCtx,
                                                        const OpenGl_AspectText&      theAspect,
                                                        const Standard_Integer        theHeight,
+                                                       const unsigned int            theResolution,
                                                        const TCollection_AsciiString theKey);
 
   //! Compute text width
@@ -88,6 +91,7 @@ public: //! @name methods for compatibility with layers
                                           const NCollection_String&     theText,
                                           const OpenGl_AspectText&      theTextAspect,
                                           const OpenGl_TextParam&       theParams,
+                                          const unsigned int            theResolution,
                                           Standard_ShortReal&           theWidth,
                                           Standard_ShortReal&           theAscent,
                                           Standard_ShortReal&           theDescent);
@@ -101,7 +105,8 @@ public: //! @name methods for compatibility with layers
   //! Perform rendering
   Standard_EXPORT void Render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
                                const Handle(OpenGl_Context)&        theCtx,
-                               const OpenGl_AspectText&             theTextAspect) const;
+                               const OpenGl_AspectText&             theTextAspect,
+                               const unsigned int                   theResolution = Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION) const;
 
 protected:
 
@@ -132,7 +137,8 @@ private:
                const Handle(OpenGl_Context)&        theCtx,
                const OpenGl_AspectText&             theTextAspect,
                const TEL_COLOUR&                    theColorText,
-               const TEL_COLOUR&                    theColorSubs) const;
+               const TEL_COLOUR&                    theColorSubs,
+               const unsigned int                   theResolution) const;
 
 protected:
 
index 6195c6626404cf63d9258ef65013c7ff629f99cd..8b94e7264bb3b4eb6b43df8a0624d79a90d5c4be 100644 (file)
@@ -53,7 +53,6 @@ namespace
   static const OpenGl_AspectLine myDefaultAspectLine;
   static const OpenGl_AspectFace myDefaultAspectFace;
   static const OpenGl_AspectMarker myDefaultAspectMarker;
-  static const OpenGl_AspectText myDefaultAspectText;
 
   static const OpenGl_TextParam myDefaultTextParam =
   {
@@ -156,10 +155,6 @@ OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Wi
   AspectFace_applied (NULL),
   AspectMarker_set (&myDefaultAspectMarker),
   AspectMarker_applied (NULL),
-  AspectText_set (&myDefaultAspectText),
-  AspectText_applied (NULL),
-  TextParam_set (&myDefaultTextParam),
-  TextParam_applied (NULL),
   ViewMatrix_applied (&myDefaultMatrix),
   StructureMatrix_applied (&myDefaultMatrix),
   myCullingMode (TelCullUndefined),
@@ -252,10 +247,6 @@ void OpenGl_Workspace::ResetAppliedAspect()
   AspectFace_applied    = NULL;
   AspectMarker_set      = &myDefaultAspectMarker;
   AspectMarker_applied  = NULL;
-  AspectText_set        = &myDefaultAspectText;
-  AspectText_applied    = NULL;
-  TextParam_set         = &myDefaultTextParam;
-  TextParam_applied     = NULL;
   PolygonOffset_applied = THE_DEFAULT_POFFSET;
   myCullingMode         = TelCullUndefined;
 
@@ -1063,7 +1054,6 @@ const OpenGl_AspectText* OpenGl_Workspace::AspectText (const Standard_Boolean th
   if (theWithApply)
   {
     AspectText_applied = AspectText_set;
-    TextParam_applied  = TextParam_set;
   }
 
   return AspectText_set;
index 29c341bfec4d0465101cbcaf918139aab87f47c1..737d26472fa5922492b55a2ba2a030935d9371e5 100644 (file)
@@ -177,17 +177,11 @@ public:
   Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
   Standard_EXPORT const OpenGl_AspectText*   SetAspectText   (const OpenGl_AspectText*   theAspect);
 
-  void SetTextParam (const OpenGl_TextParam* theParam) { TextParam_set = theParam; }
-
   //// THESE METHODS ARE EXPORTED AS THEY PROVIDE STATE INFO TO USERDRAW
   Standard_EXPORT const OpenGl_AspectLine*   AspectLine   (const Standard_Boolean theWithApply);
   Standard_EXPORT const OpenGl_AspectFace*   AspectFace   (const Standard_Boolean theWithApply);
   Standard_EXPORT const OpenGl_AspectMarker* AspectMarker (const Standard_Boolean theWithApply);
   Standard_EXPORT const OpenGl_AspectText*   AspectText   (const Standard_Boolean theWithApply);
-  inline const OpenGl_TextParam* AspectTextParams() const
-  {
-    return TextParam_applied;
-  }
 
   //! Clear the applied aspect state.
   void ResetAppliedAspect();
@@ -274,8 +268,6 @@ protected: //! @name fields related to status
   const OpenGl_AspectMarker *AspectMarker_set, *AspectMarker_applied;
   const OpenGl_AspectText *AspectText_set, *AspectText_applied;
 
-  const OpenGl_TextParam *TextParam_set, *TextParam_applied;
-
   const OpenGl_Matrix* ViewMatrix_applied;
   const OpenGl_Matrix* StructureMatrix_applied;
 
index 64aef0ad567aaddd2b18d6aa24c57302dbc40758..a16b9360876ed72cf9fc71d7ec644c9e2267b243 100644 (file)
@@ -8444,6 +8444,25 @@ static Standard_Integer VRenderParams (Draw_Interpretor& theDI,
         return 1;
       }
     }
+    else if (aFlag == "-resolution")
+    {
+      if (++anArgIter >= theArgNb)
+      {
+        std::cerr << "Error: wrong syntax at argument '" << anArg << "'\n";
+        return 1;
+      }
+
+      TCollection_AsciiString aResolution (theArgVec[anArgIter]);
+      if (aResolution.IsIntegerValue())
+      {
+        aView->ChangeRenderingParams().Resolution = static_cast<unsigned int> (Draw::Atoi (aResolution.ToCString()));
+      }
+      else
+      {
+        std::cout << "Error: wrong syntax at argument'" << anArg << "'.\n";
+        return 1;
+      }
+    }
     else
     {
       std::cout << "Error: wrong syntax, unknown flag '" << anArg << "'\n";
@@ -9164,6 +9183,7 @@ void ViewerTest::ViewerCommands(Draw_Interpretor& theCommands)
     "\n      '-env          on|off'  Enables/disables environment map background"
     "\n      '-shadingModel model'   Controls shading model from enumeration"
     "\n                              color, flat, gouraud, phong"
+    "\n      '-resolution   value'   Sets a new pixels density (PPI), defines scaling factor for parameters like text size"
     "\n    Unlike vcaps, these parameters dramatically change visual properties."
     "\n    Command is intended to control presentation quality depending on"
     "\n    hardware capabilities and performance.",
diff --git a/tests/3rdparty/fonts/C1 b/tests/3rdparty/fonts/C1
new file mode 100644 (file)
index 0000000..bfbd711
--- /dev/null
@@ -0,0 +1,31 @@
+puts "============"
+puts "OCC26298 make font resolution configurable"
+puts "============"
+puts ""
+pload VISUALIZATION
+
+vfont add [locate_data_file DejaVuSans.ttf] SansFont
+
+vclear
+vclose all
+
+vinit
+vtop
+
+vrenderparams -resolution 72
+
+vdrawtext t0 TopLeftText -pos -100 100 0 -font SansFont -color yellow -valign top -halign left
+vdrawtext t1 CenterText -pos 0 0 0 -font SansFont -color green -valign center -halign center
+vdrawtext t2 BottomRightText -pos 100 -100 0 -font SansFont -color red -valign bottom -halign right
+
+vfit
+
+vdump $imagedir/${casename}_1.png
+
+vrenderparams -resolution 96
+
+vdump $imagedir/${casename}_2.png
+
+vrenderparams -resolution 144
+
+vdump $imagedir/${casename}_3.png