1 // Created on: 2013-01-29
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #ifndef _OpenGl_TextFormatter_H__
21 #define _OpenGl_TextFormatter_H__
23 #include <OpenGl_Font.hxx>
24 #include <OpenGl_VertexBufferEditor.hxx>
26 #include <Graphic3d_HorizontalTextAlignment.hxx>
27 #include <Graphic3d_VerticalTextAlignment.hxx>
29 #include <NCollection_String.hxx>
31 typedef NCollection_Array1<OpenGl_Vec2> OpenGl_Vec2Array;
32 typedef NCollection_Handle<OpenGl_Vec2Array> Handle(OpenGl_Vec2Array);
34 //! This class intended to prepare formatted text.
35 class OpenGl_TextFormatter : public Standard_Transient
40 //! Default constructor.
41 Standard_EXPORT OpenGl_TextFormatter();
43 //! Setup alignment style.
44 Standard_EXPORT void SetupAlignment (const Graphic3d_HorizontalTextAlignment theAlignX,
45 const Graphic3d_VerticalTextAlignment theAlignY);
47 //! Reset current progress.
48 Standard_EXPORT void Reset();
50 //! Render specified text to inner buffer.
51 Standard_EXPORT void Append (const Handle(OpenGl_Context)& theCtx,
52 const NCollection_String& theString,
53 OpenGl_Font& theFont);
55 //! Perform formatting on the buffered text.
56 //! Should not be called more than once after initialization!
57 Standard_EXPORT void Format();
59 //! Retrieve formatting results.
60 Standard_EXPORT void Result (NCollection_Vector<GLuint>& theTextures,
61 NCollection_Vector< NCollection_Handle <NCollection_Vector <OpenGl_Vec2> > >& theVertsPerTexture,
62 NCollection_Vector< NCollection_Handle <NCollection_Vector <OpenGl_Vec2> > >& theTCrdsPerTexture) const;
64 //! Retrieve formatting results.
65 Standard_EXPORT void Result (const Handle(OpenGl_Context)& theCtx,
66 NCollection_Vector<GLuint>& theTextures,
67 NCollection_Vector<Handle(OpenGl_VertexBuffer)>& theVertsPerTexture,
68 NCollection_Vector<Handle(OpenGl_VertexBuffer)>& theTCrdsPerTexture) const;
70 //! Retrieve formatting results.
71 Standard_EXPORT void Result (const Handle(OpenGl_Context)& theCtx,
72 NCollection_Vector<GLuint>& theTextures,
73 NCollection_Vector<Handle(OpenGl_Vec2Array)>& theVertsPerTexture,
74 NCollection_Vector<Handle(OpenGl_Vec2Array)>& theTCrdsPerTexture) const;
76 //! @return width of formatted text.
77 inline Standard_ShortReal ResultWidth() const
82 //! @return height of formatted text.
83 inline Standard_ShortReal ResultHeight() const
85 return myLineSpacing * Standard_ShortReal(myLinesNb);
88 //! @param bounding box.
89 inline void BndBox (Font_FTFont::Rect& theBndBox) const
91 theBndBox.Left = 0.0f;
95 case Graphic3d_HTA_LEFT: theBndBox.Right = myBndWidth; break;
96 case Graphic3d_HTA_RIGHT: theBndBox.Right = -myBndWidth; break;
97 case Graphic3d_HTA_CENTER:
99 theBndBox.Left = -0.5f * myBndWidth;
100 theBndBox.Right = 0.5f * myBndWidth;
104 theBndBox.Top = myBndTop;
105 theBndBox.Bottom = theBndBox.Top - myLineSpacing * Standard_ShortReal(myLinesNb);
108 protected: //! @name class auxiliary methods
110 //! Move glyphs on the current line to correct position.
111 Standard_EXPORT void newLine (const Standard_Integer theLastRect);
113 protected: //! @name configuration
115 Graphic3d_HorizontalTextAlignment myAlignX; //!< horizontal alignment style
116 Graphic3d_VerticalTextAlignment myAlignY; //!< vertical alignment style
117 Standard_Integer myTabSize; //!< horizontal tabulation width (number of space symbols)
119 protected: //! @name input data
121 NCollection_String myString; //!< currently rendered text
122 OpenGl_Vec2 myPen; //!< current pen position
123 NCollection_Vector<OpenGl_Font::Tile>
124 myRects; //!< glyphs rectangles
125 Standard_Integer myRectsNb; //!< rectangles number
126 NCollection_Vector<Standard_ShortReal>
127 myNewLines; //!< position at LF
128 Standard_ShortReal myLineSpacing; //!< line spacing (computed as maximum of all fonts involved in text formatting)
129 Standard_ShortReal myAscender; //!<
130 bool myIsFormatted; //!< formatting state
134 mutable OpenGl_VertexBufferEditor<OpenGl_Vec2> myVboEditor;
136 protected: //! @name temporary variables for formatting routines
138 Standard_Integer myLinesNb; //!< overall (new)lines number (including splitting by width limit)
139 Standard_Integer myRectLineStart; //!< id of first rectangle on the current line
140 Standard_Integer myRectWordStart; //!< id of first rectangle in the current word
141 Standard_Integer myNewLineNb;
143 Standard_ShortReal myPenCurrLine; //!< current baseline position
144 Standard_ShortReal myLineLeft; //!< left x position of first glyph on line before formatting applied
145 Standard_ShortReal myLineTail;
146 Standard_ShortReal myBndTop;
147 Standard_ShortReal myBndWidth;
148 OpenGl_Vec2 myMoveVec; //!< local variable
152 DEFINE_STANDARD_RTTI(OpenGl_TextFormatter) // Type definition
156 DEFINE_STANDARD_HANDLE(OpenGl_TextFormatter, Standard_Transient)
158 #endif // _OpenGl_TextFormatter_H__