From: nds Date: Fri, 2 Aug 2019 04:11:31 +0000 (+0300) Subject: 0030537: Visualization - wrapping text in font text formatter - correction after... X-Git-Url: http://git.dev.opencascade.org/gitweb/?a=commitdiff_plain;h=9e7172d37227121ae550f7e4f10fc59725622ed3;p=occt-copy.git 0030537: Visualization - wrapping text in font text formatter - correction after review --- diff --git a/src/Font/Font_BRepTextBuilder.cxx b/src/Font/Font_BRepTextBuilder.cxx index dc476acf60..b1c979cfa3 100644 --- a/src/Font/Font_BRepTextBuilder.cxx +++ b/src/Font/Font_BRepTextBuilder.cxx @@ -34,7 +34,7 @@ TopoDS_Shape Font_BRepTextBuilder::Perform (Font_BRepFont& theFont, myBuilder.MakeCompound (aResult); Standard_Real aScaleUnits = theFont.Scale(); - for (Font_TextFormatter::Iterator aFormatterIt (theFormatter, Font_TextFormatter::IterationFilter_ExcludeInvisible); + for (Font_TextFormatter::Iterator aFormatterIt (*theFormatter, Font_TextFormatter::IterationFilter_ExcludeInvisible); aFormatterIt .More(); aFormatterIt .Next()) { const NCollection_Vec2& aCorner = theFormatter->BottomLeft (aFormatterIt.SymbolPosition()); diff --git a/src/Font/Font_FTFont.cxx b/src/Font/Font_FTFont.cxx index 5e91d4e10f..22a1297c4d 100755 --- a/src/Font/Font_FTFont.cxx +++ b/src/Font/Font_FTFont.cxx @@ -406,14 +406,14 @@ Font_Rect Font_FTFont::BoundingBox (const NCollection_String& theS const Graphic3d_HorizontalTextAlignment theAlignX, const Graphic3d_VerticalTextAlignment theAlignY) { - Handle(Font_TextFormatter) aFormatter = new Font_TextFormatter(); - aFormatter->SetupAlignment (theAlignX, theAlignY); - aFormatter->Reset(); + Font_TextFormatter aFormatter; + aFormatter.SetupAlignment (theAlignX, theAlignY); + aFormatter.Reset(); - aFormatter->Append (theString, *this); - aFormatter->Format(); + aFormatter.Append (theString, *this); + aFormatter.Format(); Font_Rect aBndBox; - aFormatter->BndBox (aBndBox); + aFormatter.BndBox (aBndBox); return aBndBox; } diff --git a/src/Font/Font_TextFormatter.cxx b/src/Font/Font_TextFormatter.cxx index 7cbb083e19..dfb764f4e8 100644 --- a/src/Font/Font_TextFormatter.cxx +++ b/src/Font/Font_TextFormatter.cxx @@ -126,15 +126,14 @@ void Font_TextFormatter::Append (const NCollection_String& theString, int aSymbolsCounter = 0; // special counter to process tabulation symbols // first pass - render all symbols using associated font on single ZERO baseline - Handle(Font_TextFormatter) aFormatter (this); Standard_Utf32Char aCharThis; - for (Font_TextFormatter::Iterator aFormatterIt (aFormatter); aFormatterIt .More(); aFormatterIt .Next()) + for (Font_TextFormatter::Iterator aFormatterIt (*this); aFormatterIt .More(); aFormatterIt .Next()) { aCharThis = aFormatterIt.Symbol(); const Standard_Utf32Char aCharNext = aFormatterIt.SymbolNext(); Standard_ShortReal anAdvanceX = 0; - if (Font_TextFormatter::IsCommandSymbol (aCharThis)) + if (IsCommandSymbol (aCharThis)) { continue; // skip unsupported carriage control codes } @@ -176,7 +175,7 @@ void Font_TextFormatter::newLine (const Standard_Integer theLastRect, Standard_Integer aFirstCornerId = myRectLineStart; Standard_Integer aLastCornerId = theLastRect; - if (aFirstCornerId >= getRectsNb()) + if (aFirstCornerId >= myCorners.Length()) { ++myLinesNb; myPenCurrLine -= myLineSpacing; @@ -210,7 +209,7 @@ void Font_TextFormatter::newLine (const Standard_Integer theLastRect, // ======================================================================= void Font_TextFormatter::Format() { - if (getRectsNb() == 0 || myIsFormatted) + if (myCorners.Length() == 0 || myIsFormatted) { return; } @@ -243,8 +242,7 @@ void Font_TextFormatter::Format() } } - Handle(Font_TextFormatter) aFormatter (this); - for (Font_TextFormatter::Iterator aFormatterIt (aFormatter); + for (Font_TextFormatter::Iterator aFormatterIt (*this); aFormatterIt .More(); aFormatterIt .Next()) { const Standard_Utf32Char aCharThis = aFormatterIt.Symbol(); @@ -275,7 +273,7 @@ void Font_TextFormatter::Format() myBndWidth = aMaxLineWidth; // move last line - newLine (getRectsNb() - 1, aMaxLineWidth); + newLine (myCorners.Length() - 1, aMaxLineWidth); // apply vertical alignment style if (myAlignY == Graphic3d_VTA_BOTTOM) @@ -293,7 +291,7 @@ void Font_TextFormatter::Format() if (myAlignY != Graphic3d_VTA_TOP) { - moveY (myCorners, myBndTop, 0, getRectsNb() - 1); + moveY (myCorners, myBndTop, 0, myCorners.Length() - 1); } } @@ -303,7 +301,7 @@ void Font_TextFormatter::Format() // ======================================================================= Standard_Boolean Font_TextFormatter::BndBox (const Standard_Integer theIndex, Font_Rect& theBndBox) const { - if (theIndex < 0 || theIndex >= GetCorners().Size()) + if (theIndex < 0 || theIndex >= Corners().Size()) return Standard_False; const NCollection_Vec2& aLeftCorner = BottomLeft (theIndex); @@ -355,10 +353,10 @@ Standard_Boolean Font_TextFormatter::IsLFSymbol (const Standard_Integer theIndex } // ======================================================================= -// function : GetFirstPosition +// function : FirstPosition // purpose : // ======================================================================= -Standard_ShortReal Font_TextFormatter::GetFirstPosition() const +Standard_ShortReal Font_TextFormatter::FirstPosition() const { switch (myAlignX) { @@ -399,22 +397,6 @@ Standard_Integer Font_TextFormatter::LineIndex (const Standard_Integer theIndex) return (Standard_Integer)Abs((BottomLeft (theIndex).y() + myAscender) / myLineSpacing); } -// ======================================================================= -// function : IsCommandSymbol -// purpose : -// ======================================================================= -Standard_Boolean Font_TextFormatter::IsCommandSymbol (const Standard_Utf32Char& theSymbol) -{ - if (theSymbol == '\x0D' // CR (carriage return) - || theSymbol == '\a' // BEL (alarm) - || theSymbol == '\f' // FF (form feed) NP (new page) - || theSymbol == '\b' // BS (backspace) - || theSymbol == '\v') // VT (vertical tab) - return Standard_True; - - return Standard_False; -} - // ======================================================================= // function : LineWidth // purpose : diff --git a/src/Font/Font_TextFormatter.hxx b/src/Font/Font_TextFormatter.hxx index 4044407868..7a950091bf 100755 --- a/src/Font/Font_TextFormatter.hxx +++ b/src/Font/Font_TextFormatter.hxx @@ -61,9 +61,9 @@ public: { public: //! Constructor with initialization. - Iterator (const Handle(Font_TextFormatter)& theFormatter, + Iterator (const Font_TextFormatter& theFormatter, IterationFilter theFilter = IterationFilter_None) - : myFormatter (theFormatter), myFilter (theFilter), myIter (theFormatter->myString.Iterator()) + : myFilter (theFilter), myIter (theFormatter.myString.Iterator()) { mySymbolPosition = readNextSymbol (-1, mySymbolChar); mySymbolNext = readNextSymbol (mySymbolPosition, mySymbolCharNext); @@ -76,16 +76,16 @@ public: Standard_Boolean HasNext() const { return mySymbolNext >= 0; } //! Returns current symbol. - const Standard_Utf32Char& Symbol() const { return mySymbolChar; } + Standard_Utf32Char Symbol() const { return mySymbolChar; } //! Returns the next symbol if exists. - const Standard_Utf32Char& SymbolNext() const { return mySymbolCharNext; } + Standard_Utf32Char SymbolNext() const { return mySymbolCharNext; } //! Returns current symbol position. - const Standard_Integer& SymbolPosition() const { return mySymbolPosition; } + Standard_Integer SymbolPosition() const { return mySymbolPosition; } //! Returns the next symbol position. - const Standard_Integer& SymbolPositionNext() const { return mySymbolNext; } + Standard_Integer SymbolPositionNext() const { return mySymbolNext; } //! Moves to the next item. void Next() @@ -122,7 +122,6 @@ public: } protected: - Handle(Font_TextFormatter) myFormatter; //!< source class for iterating IterationFilter myFilter; //!< possibility to filter not-necessary symbols NCollection_Utf8Iter myIter; //!< the next symbol iterator value over the text formatter string @@ -170,7 +169,7 @@ public: Standard_EXPORT Standard_Boolean IsLFSymbol (const Standard_Integer theIndex) const; //! Returns position of the first symbol in a line using alignment - Standard_EXPORT Standard_ShortReal GetFirstPosition() const; + Standard_EXPORT Standard_ShortReal FirstPosition() const; //! Returns column index of the corner index in the current line Standard_EXPORT Standard_Integer LinePositionIndex (const Standard_Integer theIndex) const; @@ -178,13 +177,6 @@ public: //! Returns row index of the corner index among text lines Standard_EXPORT Standard_Integer LineIndex (const Standard_Integer theIndex) const; - //! Returns current rendering string. - //! Obsolete method, please use Iterator to get the string characters - //inline const NCollection_String& String() const - //{ - // return myString; - //} - //! Returns tab size. inline Standard_Integer TabSize() const { @@ -242,12 +234,22 @@ public: } //!< Returns internal container of the top left corners of a formatted rectangles. - const NCollection_Vector < NCollection_Vec2 >& GetCorners() const { return myCorners; } + const NCollection_Vector < NCollection_Vec2 >& Corners() const { return myCorners; } - const NCollection_Vector& GetNewLines() const { return myNewLines; } + const NCollection_Vector& NewLines() const { return myNewLines; } //!< Returns true if the symbol is CR, BEL, FF, NP, BS or VT - Standard_EXPORT static Standard_Boolean IsCommandSymbol (const Standard_Utf32Char& theSymbol); + static inline Standard_Boolean IsCommandSymbol (const Standard_Utf32Char& theSymbol) + { + if (theSymbol == '\x0D' // CR (carriage return) + || theSymbol == '\a' // BEL (alarm) + || theSymbol == '\f' // FF (form feed) NP (new page) + || theSymbol == '\b' // BS (backspace) + || theSymbol == '\v') // VT (vertical tab) + return Standard_True; + + return Standard_False; + } DEFINE_STANDARD_RTTIEXT (Font_TextFormatter, Standard_Transient) @@ -257,9 +259,6 @@ protected: //! @name class auxiliary methods Standard_EXPORT void newLine (const Standard_Integer theLastRect, const Standard_ShortReal theMaxLineWidth); - //!< Returns rectangle number - Standard_Integer getRectsNb() { return myCorners.Length(); } - protected: //! @name configuration Graphic3d_HorizontalTextAlignment myAlignX; //!< horizontal alignment style diff --git a/src/OpenGl/OpenGl_Context.cxx b/src/OpenGl/OpenGl_Context.cxx index ef2a608da6..aa6d7b40bb 100644 --- a/src/OpenGl/OpenGl_Context.cxx +++ b/src/OpenGl/OpenGl_Context.cxx @@ -36,6 +36,8 @@ #include #include +#include + #include #include @@ -248,6 +250,8 @@ OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps) memset (myFuncs.operator->(), 0, sizeof(OpenGl_GlFunctions)); myShaderManager = new OpenGl_ShaderManager (this); + + myDefaultFormatter = new Font_TextFormatter(); } // ======================================================================= diff --git a/src/OpenGl/OpenGl_Context.hxx b/src/OpenGl/OpenGl_Context.hxx index b4c5ca85f7..93c2e16949 100644 --- a/src/OpenGl/OpenGl_Context.hxx +++ b/src/OpenGl/OpenGl_Context.hxx @@ -136,6 +136,7 @@ template struct OpenGl_TmplCore45; typedef OpenGl_TmplCore45 OpenGl_GlCore45Back; typedef OpenGl_TmplCore45 OpenGl_GlCore45; +class Font_TextFormatter; class Graphic3d_PresentationAttributes; class OpenGl_Aspects; class OpenGl_FrameBuffer; @@ -483,6 +484,9 @@ public: //! @return tool for management of shader programs within this context. inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; } + //! @return default formatter of text withing this context + inline const Handle(Font_TextFormatter)& DefaultTextFormatter() const { return myDefaultFormatter; } + public: //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1). @@ -984,6 +988,7 @@ private: // context info Standard_Boolean myHasRayTracingAdaptiveSamplingAtomic; //! indicates whether atomic adaptive screen sampling in ray tracing mode is supported Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs + Handle(Font_TextFormatter) myDefaultFormatter;//!< default text formatter, an alternative to text params private: //! @name fields tracking current state diff --git a/src/OpenGl/OpenGl_Text.cxx b/src/OpenGl/OpenGl_Text.cxx index 08caff903f..be20067ee4 100644 --- a/src/OpenGl/OpenGl_Text.cxx +++ b/src/OpenGl/OpenGl_Text.cxx @@ -702,7 +702,7 @@ void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx, Handle(Font_TextFormatter) aFormatter = myFormatter; if (myFormatter.IsNull()) { - aFormatter = new Font_TextFormatter(); + aFormatter = theCtx->DefaultTextFormatter(); aFormatter->SetupAlignment (myParams.HAlign, myParams.VAlign); aFormatter->Reset(); diff --git a/src/OpenGl/OpenGl_TextBuilder.cxx b/src/OpenGl/OpenGl_TextBuilder.cxx index 338cec15fe..7c97ed0451 100644 --- a/src/OpenGl/OpenGl_TextBuilder.cxx +++ b/src/OpenGl/OpenGl_TextBuilder.cxx @@ -59,7 +59,7 @@ void OpenGl_TextBuilder::createGlyphs (const Handle(Font_TextFormatter)& theTCrdsPerTexture.Clear(); OpenGl_Font::Tile aTile = {Font_Rect(), Font_Rect(), 0u}; - for (Font_TextFormatter::Iterator aFormatterIt (theFormatter, Font_TextFormatter::IterationFilter_ExcludeInvisible); + for (Font_TextFormatter::Iterator aFormatterIt (*theFormatter, Font_TextFormatter::IterationFilter_ExcludeInvisible); aFormatterIt .More(); aFormatterIt .Next()) { theFont.RenderGlyph (theCtx, aFormatterIt.Symbol(), aTile);