1 // Created on: 2013-01-29
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_TextFormatter_H__
17 #define _OpenGl_TextFormatter_H__
19 #include <OpenGl_Font.hxx>
20 #include <OpenGl_VertexBufferEditor.hxx>
22 #include <Graphic3d_HorizontalTextAlignment.hxx>
23 #include <Graphic3d_VerticalTextAlignment.hxx>
25 #include <NCollection_String.hxx>
27 typedef NCollection_Array1<OpenGl_Vec2> OpenGl_Vec2Array;
28 typedef NCollection_Handle<OpenGl_Vec2Array> Handle(OpenGl_Vec2Array);
30 //! This class intended to prepare formatted text.
31 class OpenGl_TextFormatter : public Standard_Transient
36 //! Default constructor.
37 Standard_EXPORT OpenGl_TextFormatter();
39 //! Setup alignment style.
40 Standard_EXPORT void SetupAlignment (const Graphic3d_HorizontalTextAlignment theAlignX,
41 const Graphic3d_VerticalTextAlignment theAlignY);
43 //! Reset current progress.
44 Standard_EXPORT void Reset();
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);
51 //! Perform formatting on the buffered text.
52 //! Should not be called more than once after initialization!
53 Standard_EXPORT void Format();
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;
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;
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;
72 //! @return width of formatted text.
73 inline Standard_ShortReal ResultWidth() const
78 //! @return height of formatted text.
79 inline Standard_ShortReal ResultHeight() const
81 return myLineSpacing * Standard_ShortReal(myLinesNb);
84 //! @param bounding box.
85 inline void BndBox (Font_FTFont::Rect& theBndBox) const
87 theBndBox.Left = 0.0f;
91 case Graphic3d_HTA_LEFT: theBndBox.Right = myBndWidth; break;
92 case Graphic3d_HTA_RIGHT: theBndBox.Right = -myBndWidth; break;
93 case Graphic3d_HTA_CENTER:
95 theBndBox.Left = -0.5f * myBndWidth;
96 theBndBox.Right = 0.5f * myBndWidth;
100 theBndBox.Top = myBndTop;
101 theBndBox.Bottom = theBndBox.Top - myLineSpacing * Standard_ShortReal(myLinesNb);
104 protected: //! @name class auxiliary methods
106 //! Move glyphs on the current line to correct position.
107 Standard_EXPORT void newLine (const Standard_Integer theLastRect);
109 protected: //! @name configuration
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)
115 protected: //! @name input data
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
130 mutable OpenGl_VertexBufferEditor<OpenGl_Vec2> myVboEditor;
132 protected: //! @name temporary variables for formatting routines
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;
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
148 DEFINE_STANDARD_RTTI(OpenGl_TextFormatter) // Type definition
152 DEFINE_STANDARD_HANDLE(OpenGl_TextFormatter, Standard_Transient)
154 #endif // _OpenGl_TextFormatter_H__