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 Font_TextFormatter_Header
17 #define Font_TextFormatter_Header
19 #include <Font_FTFont.hxx>
20 #include <NCollection_DataMap.hxx>
21 #include <NCollection_Vector.hxx>
23 //! This class intended to prepare formatted text.
24 class Font_TextFormatter
28 //! Default constructor.
29 Standard_EXPORT Font_TextFormatter();
31 //! Setup alignment style.
32 Standard_EXPORT void SetupAlignment (const Graphic3d_HorizontalTextAlignment theAlignX,
33 const Graphic3d_VerticalTextAlignment theAlignY);
35 //! Reset current progress.
36 Standard_EXPORT void Reset();
38 //! Render specified text to inner buffer.
39 Standard_EXPORT void Append (const NCollection_String& theString,
40 Font_FTFont& theFont);
42 //! Perform formatting on the buffered text.
43 //! Should not be called more than once after initialization!
44 Standard_EXPORT void Format();
46 //! Returns specific glyph rectangle.
47 inline const NCollection_Vec2<Standard_ShortReal>& TopLeft (const Standard_Integer theIndex) const
49 return myCorners.Value (theIndex);
52 //! Returns current rendering string.
53 inline const NCollection_String& String() const
59 inline Standard_Integer TabSize() const
64 //! @return width of formatted text.
65 inline Standard_ShortReal ResultWidth() const
70 //! @return height of formatted text.
71 inline Standard_ShortReal ResultHeight() const
73 return myLineSpacing * Standard_ShortReal(myLinesNb);
76 //! @param bounding box.
77 inline void BndBox (Font_FTFont::Rect& theBndBox) const
79 theBndBox.Left = 0.0f;
83 case Graphic3d_HTA_LEFT: theBndBox.Right = myBndWidth; break;
84 case Graphic3d_HTA_RIGHT: theBndBox.Right = -myBndWidth; break;
85 case Graphic3d_HTA_CENTER:
87 theBndBox.Left = -0.5f * myBndWidth;
88 theBndBox.Right = 0.5f * myBndWidth;
92 theBndBox.Top = myBndTop;
93 theBndBox.Bottom = theBndBox.Top - myLineSpacing * Standard_ShortReal(myLinesNb);
96 protected: //! @name class auxiliary methods
98 //! Move glyphs on the current line to correct position.
99 Standard_EXPORT void newLine (const Standard_Integer theLastRect);
101 protected: //! @name configuration
103 Graphic3d_HorizontalTextAlignment myAlignX; //!< horizontal alignment style
104 Graphic3d_VerticalTextAlignment myAlignY; //!< vertical alignment style
105 Standard_Integer myTabSize; //!< horizontal tabulation width (number of space symbols)
107 protected: //! @name input data
109 NCollection_String myString; //!< currently rendered text
110 NCollection_Vec2<Standard_ShortReal>
111 myPen; //!< current pen position
112 NCollection_Vector < NCollection_Vec2<Standard_ShortReal> >
113 myCorners; //!< The top left corners of a formatted rectangles.
114 Standard_Integer myRectsNb; //!< rectangles number
115 NCollection_Vector<Standard_ShortReal>
116 myNewLines; //!< position at LF
117 Standard_ShortReal myLineSpacing; //!< line spacing (computed as maximum of all fonts involved in text formatting)
118 Standard_ShortReal myAscender; //!<
119 bool myIsFormatted; //!< formatting state
121 protected: //! @name temporary variables for formatting routines
123 Standard_Integer myLinesNb; //!< overall (new)lines number (including splitting by width limit)
124 Standard_Integer myRectLineStart; //!< id of first rectangle on the current line
125 Standard_Integer myRectWordStart; //!< id of first rectangle in the current word
126 Standard_Integer myNewLineNb;
128 Standard_ShortReal myPenCurrLine; //!< current baseline position
129 Standard_ShortReal myBndTop;
130 Standard_ShortReal myBndWidth;
131 NCollection_Vec2<Standard_ShortReal>
132 myMoveVec; //!< local variable
135 #endif // Font_TextFormatter_Header