0024023: Revamp the OCCT Handle -- general
[occt.git] / src / OpenGl / OpenGl_Text.hxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 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_Text_Header
17 #define OpenGl_Text_Header
18
19 #include <OpenGl_Element.hxx>
20
21 #include <OpenGl_AspectText.hxx>
22 #include <OpenGl_TextParam.hxx>
23 #include <OpenGl_TextFormatter.hxx>
24
25 #include <TCollection_ExtendedString.hxx>
26 #include <Graphic3d_Vertex.hxx>
27 #include <Graphic3d_HorizontalTextAlignment.hxx>
28 #include <Graphic3d_VerticalTextAlignment.hxx>
29
30 class OpenGl_PrinterContext;
31
32 //! Text rendering
33 class OpenGl_Text : public OpenGl_Element
34 {
35
36 public:
37
38   //! Main constructor
39   Standard_EXPORT OpenGl_Text (const Standard_Utf8Char* theText,
40                                const OpenGl_Vec3&       thePoint,
41                                const OpenGl_TextParam&  theParams);
42
43   //! Setup new string and position
44   Standard_EXPORT void Init (const Handle(OpenGl_Context)& theCtx,
45                              const Standard_Utf8Char*      theText,
46                              const OpenGl_Vec3&            thePoint);
47
48   //! Setup new string and parameters
49   Standard_EXPORT void Init (const Handle(OpenGl_Context)& theCtx,
50                              const Standard_Utf8Char*      theText,
51                              const OpenGl_Vec3&            thePoint,
52                              const OpenGl_TextParam&       theParams);
53
54   //! Setup new position
55   Standard_EXPORT void SetPosition (const OpenGl_Vec3& thePoint);
56
57   //! Setup new font size
58   Standard_EXPORT void SetFontSize (const Handle(OpenGl_Context)& theContext,
59                                     const Standard_Integer        theFontSize);
60
61   Standard_EXPORT virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const;
62   Standard_EXPORT virtual void Release (OpenGl_Context* theContext);
63
64 public: //! @name methods for compatibility with layers
65
66   //! Empty constructor
67   Standard_EXPORT OpenGl_Text();
68
69   //! Create key for shared resource
70   Standard_EXPORT static TCollection_AsciiString FontKey (const OpenGl_AspectText& theAspect,
71                                                           const Standard_Integer   theHeight);
72
73   //! Find shared resource for specified font or initialize new one
74   Standard_EXPORT static Handle(OpenGl_Font) FindFont (const Handle(OpenGl_Context)& theCtx,
75                                                        const OpenGl_AspectText&      theAspect,
76                                                        const Standard_Integer        theHeight,
77                                                        const TCollection_AsciiString theKey);
78
79   //! Compute text width
80   Standard_EXPORT static void StringSize (const Handle(OpenGl_Context)& theCtx,
81                                           const NCollection_String&     theText,
82                                           const OpenGl_AspectText&      theTextAspect,
83                                           const OpenGl_TextParam&       theParams,
84                                           Standard_ShortReal&           theWidth,
85                                           Standard_ShortReal&           theAscent,
86                                           Standard_ShortReal&           theDescent);
87
88   //! Setup new string and parameters
89   Standard_EXPORT void Init (const Handle(OpenGl_Context)&     theCtx,
90                              const TCollection_ExtendedString& theText,
91                              const OpenGl_Vec2&                thePoint,
92                              const OpenGl_TextParam&           theParams);
93
94   //! Perform rendering
95   Standard_EXPORT void Render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
96                                const Handle(OpenGl_Context)&        theCtx,
97                                const OpenGl_AspectText&             theTextAspect) const;
98
99 protected:
100
101   //! Destructor
102   Standard_EXPORT virtual ~OpenGl_Text();
103
104   friend class OpenGl_Trihedron;
105   friend class OpenGl_GraduatedTrihedron;
106
107 private:
108
109   //! Release cached VBO resources
110   void releaseVbos (OpenGl_Context* theCtx);
111
112   //! Setup matrix.
113   void setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
114                     const Handle(OpenGl_Context)&        theCtx,
115                     const OpenGl_AspectText&             theTextAspect,
116                     const OpenGl_Vec3                    theDVec) const;
117
118   //! Draw arrays of vertices.
119   void drawText (const Handle(OpenGl_PrinterContext)& thePrintCtx,
120                  const Handle(OpenGl_Context)&        theCtx,
121                  const OpenGl_AspectText&             theTextAspect) const;
122
123   //! Main rendering code
124   void render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
125                const Handle(OpenGl_Context)&        theCtx,
126                const OpenGl_AspectText&             theTextAspect,
127                const TEL_COLOUR&                    theColorText,
128                const TEL_COLOUR&                    theColorSubs) const;
129
130 protected:
131
132   mutable Handle(OpenGl_Font)                             myFont;
133   mutable NCollection_Vector<GLuint>                      myTextures;   //!< textures' IDs
134   mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myVertsVbo;   //!< VBOs of vertices
135   mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myTCrdsVbo;   //!< VBOs of texture coordinates
136   mutable Font_FTFont::Rect                               myBndBox;
137
138 protected:
139
140   mutable OpenGl_Mat4d myProjMatrix;
141   mutable OpenGl_Mat4d myModelMatrix;
142   mutable GLint    myViewport[4];
143   mutable GLdouble myWinX;
144   mutable GLdouble myWinY;
145   mutable GLdouble myWinZ;
146   mutable GLdouble myScaleHeight;
147   mutable GLdouble myExportHeight;
148
149 protected:
150
151   OpenGl_TextParam   myParams;
152   NCollection_String myString;
153   OpenGl_Vec3        myPoint;
154   bool               myIs2d;
155
156 public:
157
158   DEFINE_STANDARD_ALLOC
159
160 };
161
162 #endif //OpenGl_Text_Header