1 // Created on: 2013-01-29
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #ifndef _OpenGl_Font_H__
21 #define _OpenGl_Font_H__
23 #include <OpenGl_Texture.hxx>
24 #include <OpenGl_Vec.hxx>
26 #include <Font_FTFont.hxx>
28 #include <NCollection_DataMap.hxx>
29 #include <NCollection_Vector.hxx>
30 #include <TCollection_AsciiString.hxx>
33 class OpenGl_Font : public OpenGl_Resource
38 //! Simple structure stores tile rectangle.
41 Font_FTFont::Rect uv; //!< UV coordinates in texture
42 Font_FTFont::Rect px; //!< pixel displacement coordinates
43 GLuint texture; //!< GL texture ID
48 Standard_Integer Left;
49 Standard_Integer Right;
51 Standard_Integer Bottom;
57 Standard_EXPORT OpenGl_Font (const Handle(Font_FTFont)& theFont,
58 const TCollection_AsciiString& theKey = "");
61 Standard_EXPORT virtual ~OpenGl_Font();
63 //! Destroy object - will release GPU memory if any
64 Standard_EXPORT virtual void Release (const OpenGl_Context* theCtx);
66 //! @return key of shared resource
67 inline const TCollection_AsciiString& ResourceKey() const
72 //! @return FreeType font instance specified on construction.
73 inline const Handle(Font_FTFont)& FTFont() const
78 //! @return true if font was loaded successfully.
79 inline bool IsValid() const
81 return !myTextures.IsEmpty() && myTextures.First()->IsValid();
84 //! Notice that this method doesn't return initialization success state.
85 //! Use IsValid() instead.
86 //! @return true if initialization was already called.
87 inline bool WasInitialized() const
89 return !myTextures.IsEmpty();
92 //! Initialize GL resources.
93 //! FreeType font instance should be already initialized!
94 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx);
96 //! Compute advance to the next character with kerning applied when applicable.
97 //! Assuming text rendered horizontally.
98 inline float AdvanceX (const Standard_Utf32Char theUChar,
99 const Standard_Utf32Char theUCharNext)
101 return myFont->AdvanceX (theUChar, theUCharNext);
104 //! @return vertical distance from the horizontal baseline to the highest character coordinate
105 inline float Ascender() const
110 //! @return vertical distance from the horizontal baseline to the lowest character coordinate
111 inline float Descender() const
116 //! @return default line spacing (the baseline-to-baseline distance)
117 inline float LineSpacing() const
119 return myLineSpacing;
122 //! Compute glyph rectangle at specified pen position (on baseline)
123 //! and render it to texture if not already.
124 //! @param theCtx active context
125 //! @param theUChar unicode symbol to render
126 //! @param theUCharNext next symbol to compute advance with kerning when available
127 //! @param theGlyph computed glyph position rectangle, texture ID and UV coordinates
128 //! @param thePen pen position on baseline to place new glyph
129 Standard_EXPORT void RenderGlyph (const Handle(OpenGl_Context)& theCtx,
130 const Standard_Utf32Char theUChar,
131 const Standard_Utf32Char theUCharNext,
133 OpenGl_Vec2& thePen);
137 //! Render new glyph to the texture.
138 bool renderGlyph (const Handle(OpenGl_Context)& theCtx,
139 const Standard_Utf32Char theChar);
141 //! Allocate new texture.
142 bool createTexture (const Handle(OpenGl_Context)& theCtx);
146 TCollection_AsciiString myKey; //!< key of shared resource
147 Handle(Font_FTFont) myFont; //!< FreeType font instance
148 Standard_ShortReal myAscender; //!< ascender provided my FT font
149 Standard_ShortReal myDescender; //!< descender provided my FT font
150 Standard_ShortReal myLineSpacing; //!< line spacing provided my FT font
151 Standard_Integer myTileSizeX; //!< tile width
152 Standard_Integer myTileSizeY; //!< tile height
153 Standard_Integer myLastTileId; //!< id of last tile
155 Standard_Integer myTextureFormat; //!< texture format
157 NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< array of textures
158 NCollection_Vector<Tile> myTiles; //!< array of loaded tiles
160 NCollection_DataMap<Standard_Utf32Char, Standard_Integer> myGlyphMap;
164 DEFINE_STANDARD_RTTI(OpenGl_Font) // Type definition
168 DEFINE_STANDARD_HANDLE(OpenGl_Font, OpenGl_Resource)
170 #endif // _OpenGl_Font_H__