From 4b1c8733e875829c8776a6b0be9a3086dde0397c Mon Sep 17 00:00:00 2001 From: isk Date: Tue, 29 Sep 2015 12:17:58 +0300 Subject: [PATCH] 0026298: Visualization, OpenGl_Text - make font resolution configurable. 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. --- src/AIS/AIS_ColorScale.cxx | 3 +- src/Font/Font_FTFont.hxx | 2 +- src/Graphic3d/Graphic3d_GraphicDriver.hxx | 9 ++++-- src/Graphic3d/Graphic3d_RenderingParams.hxx | 12 +++++++- src/OpenGl/OpenGl_GraphicDriver.cxx | 13 ++++---- src/OpenGl/OpenGl_GraphicDriver.hxx | 7 ++++- src/OpenGl/OpenGl_Text.cxx | 32 +++++++++++++------- src/OpenGl/OpenGl_Text.hxx | 12 ++++++-- src/OpenGl/OpenGl_Workspace.cxx | 10 ------ src/OpenGl/OpenGl_Workspace.hxx | 8 ----- src/ViewerTest/ViewerTest_ViewerCommands.cxx | 20 ++++++++++++ tests/3rdparty/fonts/C1 | 31 +++++++++++++++++++ 12 files changed, 115 insertions(+), 44 deletions(-) create mode 100644 tests/3rdparty/fonts/C1 diff --git a/src/AIS/AIS_ColorScale.cxx b/src/AIS/AIS_ColorScale.cxx index 562f70e1f8..93a1f682dc 100644 --- a/src/AIS/AIS_ColorScale.cxx +++ b/src/AIS/AIS_ColorScale.cxx @@ -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; diff --git a/src/Font/Font_FTFont.hxx b/src/Font/Font_FTFont.hxx index 9810abe610..d7ea03b031 100755 --- a/src/Font/Font_FTFont.hxx +++ b/src/Font/Font_FTFont.hxx @@ -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 diff --git a/src/Graphic3d/Graphic3d_GraphicDriver.hxx b/src/Graphic3d/Graphic3d_GraphicDriver.hxx index 3bc5fa9e61..c54b29b692 100644 --- a/src/Graphic3d/Graphic3d_GraphicDriver.hxx +++ b/src/Graphic3d/Graphic3d_GraphicDriver.hxx @@ -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 for //! the view. Z layers allow drawing structures in higher layers diff --git a/src/Graphic3d/Graphic3d_RenderingParams.hxx b/src/Graphic3d/Graphic3d_RenderingParams.hxx index 1ee0e656f7..b57421382f 100644 --- a/src/Graphic3d/Graphic3d_RenderingParams.hxx +++ b/src/Graphic3d/Graphic3d_RenderingParams.hxx @@ -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 diff --git a/src/OpenGl/OpenGl_GraphicDriver.cxx b/src/OpenGl/OpenGl_GraphicDriver.cxx index 8a3e2106cb..2e9c9594c0 100644 --- a/src/OpenGl/OpenGl_GraphicDriver.cxx +++ b/src/OpenGl/OpenGl_GraphicDriver.cxx @@ -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); } //======================================================================= diff --git a/src/OpenGl/OpenGl_GraphicDriver.hxx b/src/OpenGl/OpenGl_GraphicDriver.hxx index b92e9e3f60..c78b8438c1 100644 --- a/src/OpenGl/OpenGl_GraphicDriver.hxx +++ b/src/OpenGl/OpenGl_GraphicDriver.hxx @@ -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(); diff --git a/src/OpenGl/OpenGl_Text.cxx b/src/OpenGl/OpenGl_Text.cxx index babd557c13..412b459854 100644 --- a/src/OpenGl/OpenGl_Text.cxx +++ b/src/OpenGl/OpenGl_Text.cxx @@ -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()) { diff --git a/src/OpenGl/OpenGl_Text.hxx b/src/OpenGl/OpenGl_Text.hxx index aa15e1e1dd..207b6ac2cd 100755 --- a/src/OpenGl/OpenGl_Text.hxx +++ b/src/OpenGl/OpenGl_Text.hxx @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -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: diff --git a/src/OpenGl/OpenGl_Workspace.cxx b/src/OpenGl/OpenGl_Workspace.cxx index 6195c66264..8b94e7264b 100644 --- a/src/OpenGl/OpenGl_Workspace.cxx +++ b/src/OpenGl/OpenGl_Workspace.cxx @@ -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; diff --git a/src/OpenGl/OpenGl_Workspace.hxx b/src/OpenGl/OpenGl_Workspace.hxx index 29c341bfec..737d26472f 100644 --- a/src/OpenGl/OpenGl_Workspace.hxx +++ b/src/OpenGl/OpenGl_Workspace.hxx @@ -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; diff --git a/src/ViewerTest/ViewerTest_ViewerCommands.cxx b/src/ViewerTest/ViewerTest_ViewerCommands.cxx index 64aef0ad56..a16b936087 100644 --- a/src/ViewerTest/ViewerTest_ViewerCommands.cxx +++ b/src/ViewerTest/ViewerTest_ViewerCommands.cxx @@ -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 (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 index 0000000000..bfbd7110fd --- /dev/null +++ b/tests/3rdparty/fonts/C1 @@ -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 -- 2.20.1