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 _OpenGl_Font_H__
17 #define _OpenGl_Font_H__
19 #include <OpenGl_Texture.hxx>
20 #include <OpenGl_Vec.hxx>
22 #include <Font_FTFont.hxx>
24 #include <NCollection_DataMap.hxx>
25 #include <NCollection_Vector.hxx>
26 #include <TCollection_AsciiString.hxx>
29 class OpenGl_Font : public OpenGl_Resource
34 //! Simple structure stores tile rectangle.
37 Font_FTFont::Rect uv; //!< UV coordinates in texture
38 Font_FTFont::Rect px; //!< pixel displacement coordinates
39 GLuint texture; //!< GL texture ID
44 Standard_Integer Left;
45 Standard_Integer Right;
47 Standard_Integer Bottom;
53 Standard_EXPORT OpenGl_Font (const Handle(Font_FTFont)& theFont,
54 const TCollection_AsciiString& theKey = "");
57 Standard_EXPORT virtual ~OpenGl_Font();
59 //! Destroy object - will release GPU memory if any
60 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
62 //! @return key of shared resource
63 inline const TCollection_AsciiString& ResourceKey() const
68 //! @return FreeType font instance specified on construction.
69 inline const Handle(Font_FTFont)& FTFont() const
74 //! @return true if font was loaded successfully.
75 inline bool IsValid() const
77 return !myTextures.IsEmpty() && myTextures.First()->IsValid();
80 //! Notice that this method doesn't return initialization success state.
81 //! Use IsValid() instead.
82 //! @return true if initialization was already called.
83 inline bool WasInitialized() const
85 return !myTextures.IsEmpty();
88 //! Initialize GL resources.
89 //! FreeType font instance should be already initialized!
90 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx);
92 //! Compute advance to the next character with kerning applied when applicable.
93 //! Assuming text rendered horizontally.
94 inline float AdvanceX (const Standard_Utf32Char theUChar,
95 const Standard_Utf32Char theUCharNext)
97 return myFont->AdvanceX (theUChar, theUCharNext);
100 //! @return vertical distance from the horizontal baseline to the highest character coordinate
101 inline float Ascender() const
106 //! @return vertical distance from the horizontal baseline to the lowest character coordinate
107 inline float Descender() const
112 //! @return default line spacing (the baseline-to-baseline distance)
113 inline float LineSpacing() const
115 return myLineSpacing;
118 //! Compute glyph rectangle at specified pen position (on baseline)
119 //! and render it to texture if not already.
120 //! @param theCtx active context
121 //! @param theUChar unicode symbol to render
122 //! @param theUCharNext next symbol to compute advance with kerning when available
123 //! @param theGlyph computed glyph position rectangle, texture ID and UV coordinates
124 //! @param thePen pen position on baseline to place new glyph
125 Standard_EXPORT void RenderGlyph (const Handle(OpenGl_Context)& theCtx,
126 const Standard_Utf32Char theUChar,
127 const Standard_Utf32Char theUCharNext,
129 OpenGl_Vec2& thePen);
133 //! Render new glyph to the texture.
134 bool renderGlyph (const Handle(OpenGl_Context)& theCtx,
135 const Standard_Utf32Char theChar);
137 //! Allocate new texture.
138 bool createTexture (const Handle(OpenGl_Context)& theCtx);
142 TCollection_AsciiString myKey; //!< key of shared resource
143 Handle(Font_FTFont) myFont; //!< FreeType font instance
144 Standard_ShortReal myAscender; //!< ascender provided my FT font
145 Standard_ShortReal myDescender; //!< descender provided my FT font
146 Standard_ShortReal myLineSpacing; //!< line spacing provided my FT font
147 Standard_Integer myTileSizeX; //!< tile width
148 Standard_Integer myTileSizeY; //!< tile height
149 Standard_Integer myLastTileId; //!< id of last tile
151 Standard_Integer myTextureFormat; //!< texture format
153 NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< array of textures
154 NCollection_Vector<Tile> myTiles; //!< array of loaded tiles
156 NCollection_DataMap<Standard_Utf32Char, Standard_Integer> myGlyphMap;
160 DEFINE_STANDARD_RTTI(OpenGl_Font) // Type definition
164 DEFINE_STANDARD_HANDLE(OpenGl_Font, OpenGl_Resource)
166 #endif // _OpenGl_Font_H__