0024234: occt master is not compiled by VC++ 2005 (vc8 32/64 bit TKBO)
[occt.git] / src / OpenGl / OpenGl_TextFormatter.hxx
1 // Created on: 2013-01-29
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
4 //
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.
9 //
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.
12 //
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.
19
20 #ifndef _OpenGl_TextFormatter_H__
21 #define _OpenGl_TextFormatter_H__
22
23 #include <OpenGl_Font.hxx>
24 #include <OpenGl_VertexBufferEditor.hxx>
25
26 #include <Graphic3d_HorizontalTextAlignment.hxx>
27 #include <Graphic3d_VerticalTextAlignment.hxx>
28
29 #include <NCollection_String.hxx>
30
31 typedef NCollection_Array1<OpenGl_Vec2> OpenGl_Vec2Array;
32 typedef NCollection_Handle<OpenGl_Vec2Array> Handle(OpenGl_Vec2Array);
33
34 //! This class intended to prepare formatted text.
35 class OpenGl_TextFormatter : public Standard_Transient
36 {
37
38 public:
39
40   //! Default constructor.
41   Standard_EXPORT OpenGl_TextFormatter();
42
43   //! Setup alignment style.
44   Standard_EXPORT void SetupAlignment (const Graphic3d_HorizontalTextAlignment theAlignX,
45                                        const Graphic3d_VerticalTextAlignment   theAlignY);
46
47   //! Reset current progress.
48   Standard_EXPORT void Reset();
49
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);
54
55   //! Perform formatting on the buffered text.
56   //! Should not be called more than once after initialization!
57   Standard_EXPORT void Format();
58
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;
63
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;
69
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;
75
76   //! @return width of formatted text.
77   inline Standard_ShortReal ResultWidth() const
78   {
79     return myBndWidth;
80   }
81
82   //! @return height of formatted text.
83   inline Standard_ShortReal ResultHeight() const
84   {
85     return myLineSpacing * Standard_ShortReal(myLinesNb);
86   }
87
88   //! @param bounding box.
89   inline void BndBox (Font_FTFont::Rect& theBndBox) const
90   {
91     theBndBox.Left = 0.0f;
92     switch (myAlignX)
93     {
94       default:
95       case Graphic3d_HTA_LEFT:  theBndBox.Right  =  myBndWidth; break;
96       case Graphic3d_HTA_RIGHT: theBndBox.Right  = -myBndWidth; break;
97       case Graphic3d_HTA_CENTER:
98       {
99         theBndBox.Left  = -0.5f * myBndWidth;
100         theBndBox.Right =  0.5f * myBndWidth;
101         break;
102       }
103     }
104     theBndBox.Top    = myBndTop;
105     theBndBox.Bottom = theBndBox.Top - myLineSpacing * Standard_ShortReal(myLinesNb);
106   }
107
108 protected: //! @name class auxiliary methods
109
110   //! Move glyphs on the current line to correct position.
111   Standard_EXPORT void newLine (const Standard_Integer theLastRect);
112
113 protected: //! @name configuration
114
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)
118
119 protected: //! @name input data
120
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
131
132 protected:
133
134   mutable OpenGl_VertexBufferEditor<OpenGl_Vec2> myVboEditor;
135
136 protected: //! @name temporary variables for formatting routines
137
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;
142
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
149
150 public:
151
152   DEFINE_STANDARD_RTTI(OpenGl_TextFormatter) // Type definition
153
154 };
155
156 DEFINE_STANDARD_HANDLE(OpenGl_TextFormatter, Standard_Transient)
157
158 #endif // _OpenGl_TextFormatter_H__