a174a3c5 |
1 | // Created on: 2013-01-29 |
2 | // Created by: Kirill GAVRILOV |
d5f74e42 |
3 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
a174a3c5 |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
a174a3c5 |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
a174a3c5 |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
a174a3c5 |
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_FTFont.hxx> |
23 | |
24 | #include <NCollection_DataMap.hxx> |
25 | #include <NCollection_Vector.hxx> |
26 | #include <TCollection_AsciiString.hxx> |
27 | |
28 | //! Texture font. |
29 | class OpenGl_Font : public OpenGl_Resource |
30 | { |
31 | |
32 | public: |
33 | |
34 | //! Simple structure stores tile rectangle. |
35 | struct Tile |
36 | { |
37 | Font_FTFont::Rect uv; //!< UV coordinates in texture |
38 | Font_FTFont::Rect px; //!< pixel displacement coordinates |
39 | GLuint texture; //!< GL texture ID |
40 | }; |
41 | |
42 | struct RectI |
43 | { |
44 | Standard_Integer Left; |
45 | Standard_Integer Right; |
46 | Standard_Integer Top; |
47 | Standard_Integer Bottom; |
48 | }; |
49 | |
50 | public: |
51 | |
52 | //! Main constructor. |
53 | Standard_EXPORT OpenGl_Font (const Handle(Font_FTFont)& theFont, |
54 | const TCollection_AsciiString& theKey = ""); |
55 | |
56 | //! Destroy object. |
57 | Standard_EXPORT virtual ~OpenGl_Font(); |
58 | |
59 | //! Destroy object - will release GPU memory if any |
10b9c7df |
60 | Standard_EXPORT virtual void Release (OpenGl_Context* theCtx); |
a174a3c5 |
61 | |
62 | //! @return key of shared resource |
63 | inline const TCollection_AsciiString& ResourceKey() const |
64 | { |
65 | return myKey; |
66 | } |
67 | |
68 | //! @return FreeType font instance specified on construction. |
69 | inline const Handle(Font_FTFont)& FTFont() const |
70 | { |
71 | return myFont; |
72 | } |
73 | |
317d68c9 |
74 | //! @return FreeType font instance specified on construction. |
75 | inline Handle(Font_FTFont)& FTFont() |
76 | { |
77 | return myFont; |
78 | } |
79 | |
a174a3c5 |
80 | //! @return true if font was loaded successfully. |
81 | inline bool IsValid() const |
82 | { |
83 | return !myTextures.IsEmpty() && myTextures.First()->IsValid(); |
84 | } |
85 | |
86 | //! Notice that this method doesn't return initialization success state. |
87 | //! Use IsValid() instead. |
88 | //! @return true if initialization was already called. |
89 | inline bool WasInitialized() const |
90 | { |
91 | return !myTextures.IsEmpty(); |
92 | } |
93 | |
94 | //! Initialize GL resources. |
95 | //! FreeType font instance should be already initialized! |
96 | Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx); |
97 | |
98 | //! Compute advance to the next character with kerning applied when applicable. |
99 | //! Assuming text rendered horizontally. |
100 | inline float AdvanceX (const Standard_Utf32Char theUChar, |
101 | const Standard_Utf32Char theUCharNext) |
102 | { |
103 | return myFont->AdvanceX (theUChar, theUCharNext); |
104 | } |
105 | |
106 | //! @return vertical distance from the horizontal baseline to the highest character coordinate |
107 | inline float Ascender() const |
108 | { |
109 | return myAscender; |
110 | } |
111 | |
112 | //! @return vertical distance from the horizontal baseline to the lowest character coordinate |
113 | inline float Descender() const |
114 | { |
115 | return myDescender; |
116 | } |
117 | |
118 | //! @return default line spacing (the baseline-to-baseline distance) |
119 | inline float LineSpacing() const |
120 | { |
121 | return myLineSpacing; |
122 | } |
123 | |
317d68c9 |
124 | //! Render glyph to texture if not already. |
a174a3c5 |
125 | //! @param theCtx active context |
126 | //! @param theUChar unicode symbol to render |
a174a3c5 |
127 | //! @param theGlyph computed glyph position rectangle, texture ID and UV coordinates |
317d68c9 |
128 | Standard_EXPORT bool RenderGlyph (const Handle(OpenGl_Context)& theCtx, |
a174a3c5 |
129 | const Standard_Utf32Char theUChar, |
317d68c9 |
130 | Tile& theGlyph); |
a174a3c5 |
131 | |
132 | protected: |
133 | |
134 | //! Render new glyph to the texture. |
135 | bool renderGlyph (const Handle(OpenGl_Context)& theCtx, |
136 | const Standard_Utf32Char theChar); |
137 | |
138 | //! Allocate new texture. |
139 | bool createTexture (const Handle(OpenGl_Context)& theCtx); |
140 | |
141 | protected: |
142 | |
143 | TCollection_AsciiString myKey; //!< key of shared resource |
144 | Handle(Font_FTFont) myFont; //!< FreeType font instance |
145 | Standard_ShortReal myAscender; //!< ascender provided my FT font |
146 | Standard_ShortReal myDescender; //!< descender provided my FT font |
147 | Standard_ShortReal myLineSpacing; //!< line spacing provided my FT font |
148 | Standard_Integer myTileSizeX; //!< tile width |
149 | Standard_Integer myTileSizeY; //!< tile height |
150 | Standard_Integer myLastTileId; //!< id of last tile |
151 | RectI myLastTilePx; |
152 | Standard_Integer myTextureFormat; //!< texture format |
153 | |
154 | NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< array of textures |
155 | NCollection_Vector<Tile> myTiles; //!< array of loaded tiles |
156 | |
157 | NCollection_DataMap<Standard_Utf32Char, Standard_Integer> myGlyphMap; |
158 | |
159 | public: |
160 | |
ec357c5c |
161 | DEFINE_STANDARD_RTTI(OpenGl_Font, OpenGl_Resource) // Type definition |
a174a3c5 |
162 | |
163 | }; |
164 | |
165 | DEFINE_STANDARD_HANDLE(OpenGl_Font, OpenGl_Resource) |
166 | |
167 | #endif // _OpenGl_Font_H__ |