0030153: Visualization, TKOpenGl - AIS_ColoredShape::SynchronizeAspects() doesn't...
[occt.git] / src / OpenGl / OpenGl_Font.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_Font_H__
17 #define _OpenGl_Font_H__
18
19 #include <OpenGl_Texture.hxx>
20 #include <OpenGl_Vec.hxx>
21
22 #include <Font_Rect.hxx>
23
24 #include <NCollection_DataMap.hxx>
25 #include <NCollection_Vector.hxx>
26 #include <TCollection_AsciiString.hxx>
27
28 class Font_FTFont;
29
30 //! Texture font.
31 class OpenGl_Font : public OpenGl_Resource
32 {
33
34 public:
35
36   //! Simple structure stores tile rectangle.
37   struct Tile
38   {
39     Font_Rect uv;      //!< UV coordinates in texture
40     Font_Rect px;      //!< pixel displacement coordinates
41     GLuint    texture; //!< GL texture ID
42   };
43
44   struct RectI
45   {
46     Standard_Integer Left;
47     Standard_Integer Right;
48     Standard_Integer Top;
49     Standard_Integer Bottom;
50   };
51
52 public:
53
54   //! Main constructor.
55   Standard_EXPORT OpenGl_Font (const Handle(Font_FTFont)&     theFont,
56                                const TCollection_AsciiString& theKey = "");
57
58   //! Destroy object.
59   Standard_EXPORT virtual ~OpenGl_Font();
60
61   //! Destroy object - will release GPU memory if any
62   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
63
64   //! Returns estimated GPU memory usage.
65   Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
66
67   //! @return key of shared resource
68   inline const TCollection_AsciiString& ResourceKey() const
69   {
70     return myKey;
71   }
72
73   //! @return FreeType font instance specified on construction.
74   inline const Handle(Font_FTFont)& FTFont() const
75   {
76     return myFont;
77   }
78
79   //! @return true if font was loaded successfully.
80   inline bool IsValid() const
81   {
82     return !myTextures.IsEmpty() && myTextures.First()->IsValid();
83   }
84
85   //! Notice that this method doesn't return initialization success state.
86   //! Use IsValid() instead.
87   //! @return true if initialization was already called.
88   inline bool WasInitialized() const
89   {
90     return !myTextures.IsEmpty();
91   }
92
93   //! Initialize GL resources.
94   //! FreeType font instance should be already initialized!
95   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx);
96
97   //! @return vertical distance from the horizontal baseline to the highest character coordinate
98   inline float Ascender() const
99   {
100     return myAscender;
101   }
102
103   //! @return vertical distance from the horizontal baseline to the lowest character coordinate
104   inline float Descender() const
105   {
106     return myDescender;
107   }
108
109   //! @return default line spacing (the baseline-to-baseline distance)
110   inline float LineSpacing() const
111   {
112     return myLineSpacing;
113   }
114
115   //! Render glyph to texture if not already.
116   //! @param theCtx       active context
117   //! @param theUChar     unicode symbol to render
118   //! @param theGlyph     computed glyph position rectangle, texture ID and UV coordinates
119   Standard_EXPORT bool RenderGlyph (const Handle(OpenGl_Context)& theCtx,
120                                     const Standard_Utf32Char      theUChar,
121                                     Tile&                         theGlyph);
122
123 protected:
124
125   //! Render new glyph to the texture.
126   bool renderGlyph (const Handle(OpenGl_Context)& theCtx,
127                     const Standard_Utf32Char      theChar);
128
129   //! Allocate new texture.
130   bool createTexture (const Handle(OpenGl_Context)& theCtx);
131
132 protected:
133
134   TCollection_AsciiString myKey;           //!< key of shared resource
135   Handle(Font_FTFont)     myFont;          //!< FreeType font instance
136   Standard_ShortReal      myAscender;      //!< ascender     provided my FT font
137   Standard_ShortReal      myDescender;     //!< descender    provided my FT font
138   Standard_ShortReal      myLineSpacing;   //!< line spacing provided my FT font
139   Standard_Integer        myTileSizeX;     //!< tile width
140   Standard_Integer        myTileSizeY;     //!< tile height
141   Standard_Integer        myLastTileId;    //!< id of last tile
142   RectI                   myLastTilePx;
143   Standard_Integer        myTextureFormat; //!< texture format
144
145   NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< array of textures
146   NCollection_Vector<Tile>                   myTiles;    //!< array of loaded tiles
147
148   NCollection_DataMap<Standard_Utf32Char, Standard_Integer> myGlyphMap;
149
150 public:
151
152   DEFINE_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource) // Type definition
153
154 };
155
156 DEFINE_STANDARD_HANDLE(OpenGl_Font, OpenGl_Resource)
157
158 #endif // _OpenGl_Font_H__