aa88497c1925bfc674e0c1d604eb0947af3c5c1a
[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 //! This class intended to prepare formatted text.
28 class OpenGl_TextFormatter : public Standard_Transient
29 {
30
31 public:
32
33   //! Default constructor.
34   Standard_EXPORT OpenGl_TextFormatter();
35
36   //! Setup alignment style.
37   Standard_EXPORT void SetupAlignment (const Graphic3d_HorizontalTextAlignment theAlignX,
38                                        const Graphic3d_VerticalTextAlignment   theAlignY);
39
40   //! Reset current progress.
41   Standard_EXPORT void Reset();
42
43   //! Render specified text to inner buffer.
44   Standard_EXPORT void Append (const Handle(OpenGl_Context)& theCtx,
45                                const NCollection_String&     theString,
46                                OpenGl_Font&                  theFont);
47
48   //! Perform formatting on the buffered text.
49   //! Should not be called more than once after initialization!
50   Standard_EXPORT void Format();
51
52   //! Retrieve formatting results.
53   Standard_EXPORT void Result (NCollection_Vector<GLuint>& theTextures,
54                                NCollection_Vector< NCollection_Handle <NCollection_Vector <OpenGl_Vec2> > >& theVertsPerTexture,
55                                NCollection_Vector< NCollection_Handle <NCollection_Vector <OpenGl_Vec2> > >& theTCrdsPerTexture) const;
56
57   //! Retrieve formatting results.
58   Standard_EXPORT void Result (const Handle(OpenGl_Context)&                    theCtx,
59                                NCollection_Vector<GLuint>&                      theTextures,
60                                NCollection_Vector<Handle(OpenGl_VertexBuffer)>& theVertsPerTexture,
61                                NCollection_Vector<Handle(OpenGl_VertexBuffer)>& theTCrdsPerTexture) const;
62
63   //! @return width of formatted text.
64   inline Standard_ShortReal ResultWidth() const
65   {
66     return myBndWidth;
67   }
68
69   //! @return height of formatted text.
70   inline Standard_ShortReal ResultHeight() const
71   {
72     return myLineSpacing * Standard_ShortReal(myLinesNb);
73   }
74
75   //! @param bounding box.
76   inline void BndBox (Font_FTFont::Rect& theBndBox) const
77   {
78     theBndBox.Left = 0.0f;
79     switch (myAlignX)
80     {
81       default:
82       case Graphic3d_HTA_LEFT:  theBndBox.Right  =  myBndWidth; break;
83       case Graphic3d_HTA_RIGHT: theBndBox.Right  = -myBndWidth; break;
84       case Graphic3d_HTA_CENTER:
85       {
86         theBndBox.Left  = -0.5f * myBndWidth;
87         theBndBox.Right =  0.5f * myBndWidth;
88         break;
89       }
90     }
91     theBndBox.Top    = myBndTop;
92     theBndBox.Bottom = theBndBox.Top - myLineSpacing * Standard_ShortReal(myLinesNb);
93   }
94
95 protected: //! @name class auxiliary methods
96
97   //! Move glyphs on the current line to correct position.
98   Standard_EXPORT void newLine (const Standard_Integer theLastRect);
99
100 protected: //! @name configuration
101
102   Graphic3d_HorizontalTextAlignment myAlignX;  //!< horizontal alignment style
103   Graphic3d_VerticalTextAlignment   myAlignY;  //!< vertical   alignment style
104   Standard_Integer                  myTabSize; //!< horizontal tabulation width (number of space symbols)
105
106 protected: //! @name input data
107
108   NCollection_String myString;        //!< currently rendered text
109   OpenGl_Vec2        myPen;           //!< current pen position
110   NCollection_Vector<OpenGl_Font::Tile>
111                      myRects;         //!< glyphs rectangles
112   Standard_Integer   myRectsNb;       //!< rectangles number
113   NCollection_Vector<Standard_ShortReal>
114                      myNewLines;      //!< position at LF
115   Standard_ShortReal myLineSpacing;   //!< line spacing (computed as maximum of all fonts involved in text formatting)
116   Standard_ShortReal myAscender;      //!<
117   bool               myIsFormatted;   //!< formatting state
118
119 protected:
120
121   mutable OpenGl_VertexBufferEditor<OpenGl_Vec2> myVboEditor;
122
123 protected: //! @name temporary variables for formatting routines
124
125   Standard_Integer   myLinesNb;       //!< overall (new)lines number (including splitting by width limit)
126   Standard_Integer   myRectLineStart; //!< id of first rectangle on the current line
127   Standard_Integer   myRectWordStart; //!< id of first rectangle in the current word
128   Standard_Integer   myNewLineNb;
129
130   Standard_ShortReal myPenCurrLine;   //!< current baseline position
131   Standard_ShortReal myLineLeft;      //!< left x position of first glyph on line before formatting applied
132   Standard_ShortReal myLineTail;
133   Standard_ShortReal myBndTop;
134   Standard_ShortReal myBndWidth;
135   OpenGl_Vec2        myMoveVec;       //!< local variable
136
137 public:
138
139   DEFINE_STANDARD_RTTI(OpenGl_TextFormatter, Standard_Transient) // Type definition
140
141 };
142
143 DEFINE_STANDARD_HANDLE(OpenGl_TextFormatter, Standard_Transient)
144
145 #endif // _OpenGl_TextFormatter_H__