0024624: Lost word in license statement in source files
[occt.git] / src / OpenGl / OpenGl_TextFormatter.hxx
1 // Created on: 2013-01-29
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _OpenGl_TextFormatter_H__
17 #define _OpenGl_TextFormatter_H__
18
19 #include <OpenGl_Font.hxx>
20 #include <OpenGl_VertexBufferEditor.hxx>
21
22 #include <Graphic3d_HorizontalTextAlignment.hxx>
23 #include <Graphic3d_VerticalTextAlignment.hxx>
24
25 #include <NCollection_String.hxx>
26
27 typedef NCollection_Array1<OpenGl_Vec2> OpenGl_Vec2Array;
28 typedef NCollection_Handle<OpenGl_Vec2Array> Handle(OpenGl_Vec2Array);
29
30 //! This class intended to prepare formatted text.
31 class OpenGl_TextFormatter : public Standard_Transient
32 {
33
34 public:
35
36   //! Default constructor.
37   Standard_EXPORT OpenGl_TextFormatter();
38
39   //! Setup alignment style.
40   Standard_EXPORT void SetupAlignment (const Graphic3d_HorizontalTextAlignment theAlignX,
41                                        const Graphic3d_VerticalTextAlignment   theAlignY);
42
43   //! Reset current progress.
44   Standard_EXPORT void Reset();
45
46   //! Render specified text to inner buffer.
47   Standard_EXPORT void Append (const Handle(OpenGl_Context)& theCtx,
48                                const NCollection_String&     theString,
49                                OpenGl_Font&                  theFont);
50
51   //! Perform formatting on the buffered text.
52   //! Should not be called more than once after initialization!
53   Standard_EXPORT void Format();
54
55   //! Retrieve formatting results.
56   Standard_EXPORT void Result (NCollection_Vector<GLuint>& theTextures,
57                                NCollection_Vector< NCollection_Handle <NCollection_Vector <OpenGl_Vec2> > >& theVertsPerTexture,
58                                NCollection_Vector< NCollection_Handle <NCollection_Vector <OpenGl_Vec2> > >& theTCrdsPerTexture) const;
59
60   //! Retrieve formatting results.
61   Standard_EXPORT void Result (const Handle(OpenGl_Context)&                    theCtx,
62                                NCollection_Vector<GLuint>&                      theTextures,
63                                NCollection_Vector<Handle(OpenGl_VertexBuffer)>& theVertsPerTexture,
64                                NCollection_Vector<Handle(OpenGl_VertexBuffer)>& theTCrdsPerTexture) const;
65
66   //! Retrieve formatting results.
67   Standard_EXPORT void Result (const Handle(OpenGl_Context)&                 theCtx,
68                                NCollection_Vector<GLuint>&                   theTextures,
69                                NCollection_Vector<Handle(OpenGl_Vec2Array)>& theVertsPerTexture,
70                                NCollection_Vector<Handle(OpenGl_Vec2Array)>& theTCrdsPerTexture) const;
71
72   //! @return width of formatted text.
73   inline Standard_ShortReal ResultWidth() const
74   {
75     return myBndWidth;
76   }
77
78   //! @return height of formatted text.
79   inline Standard_ShortReal ResultHeight() const
80   {
81     return myLineSpacing * Standard_ShortReal(myLinesNb);
82   }
83
84   //! @param bounding box.
85   inline void BndBox (Font_FTFont::Rect& theBndBox) const
86   {
87     theBndBox.Left = 0.0f;
88     switch (myAlignX)
89     {
90       default:
91       case Graphic3d_HTA_LEFT:  theBndBox.Right  =  myBndWidth; break;
92       case Graphic3d_HTA_RIGHT: theBndBox.Right  = -myBndWidth; break;
93       case Graphic3d_HTA_CENTER:
94       {
95         theBndBox.Left  = -0.5f * myBndWidth;
96         theBndBox.Right =  0.5f * myBndWidth;
97         break;
98       }
99     }
100     theBndBox.Top    = myBndTop;
101     theBndBox.Bottom = theBndBox.Top - myLineSpacing * Standard_ShortReal(myLinesNb);
102   }
103
104 protected: //! @name class auxiliary methods
105
106   //! Move glyphs on the current line to correct position.
107   Standard_EXPORT void newLine (const Standard_Integer theLastRect);
108
109 protected: //! @name configuration
110
111   Graphic3d_HorizontalTextAlignment myAlignX;  //!< horizontal alignment style
112   Graphic3d_VerticalTextAlignment   myAlignY;  //!< vertical   alignment style
113   Standard_Integer                  myTabSize; //!< horizontal tabulation width (number of space symbols)
114
115 protected: //! @name input data
116
117   NCollection_String myString;        //!< currently rendered text
118   OpenGl_Vec2        myPen;           //!< current pen position
119   NCollection_Vector<OpenGl_Font::Tile>
120                      myRects;         //!< glyphs rectangles
121   Standard_Integer   myRectsNb;       //!< rectangles number
122   NCollection_Vector<Standard_ShortReal>
123                      myNewLines;      //!< position at LF
124   Standard_ShortReal myLineSpacing;   //!< line spacing (computed as maximum of all fonts involved in text formatting)
125   Standard_ShortReal myAscender;      //!<
126   bool               myIsFormatted;   //!< formatting state
127
128 protected:
129
130   mutable OpenGl_VertexBufferEditor<OpenGl_Vec2> myVboEditor;
131
132 protected: //! @name temporary variables for formatting routines
133
134   Standard_Integer   myLinesNb;       //!< overall (new)lines number (including splitting by width limit)
135   Standard_Integer   myRectLineStart; //!< id of first rectangle on the current line
136   Standard_Integer   myRectWordStart; //!< id of first rectangle in the current word
137   Standard_Integer   myNewLineNb;
138
139   Standard_ShortReal myPenCurrLine;   //!< current baseline position
140   Standard_ShortReal myLineLeft;      //!< left x position of first glyph on line before formatting applied
141   Standard_ShortReal myLineTail;
142   Standard_ShortReal myBndTop;
143   Standard_ShortReal myBndWidth;
144   OpenGl_Vec2        myMoveVec;       //!< local variable
145
146 public:
147
148   DEFINE_STANDARD_RTTI(OpenGl_TextFormatter) // Type definition
149
150 };
151
152 DEFINE_STANDARD_HANDLE(OpenGl_TextFormatter, Standard_Transient)
153
154 #endif // _OpenGl_TextFormatter_H__