0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / OpenGl / OpenGl_Font.hxx
CommitLineData
a174a3c5 1// Created on: 2013-01-29
2// Created by: Kirill GAVRILOV
3// Copyright (c) 2013 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
20#ifndef _OpenGl_Font_H__
21#define _OpenGl_Font_H__
22
23#include <OpenGl_Texture.hxx>
24#include <OpenGl_Vec.hxx>
25
26#include <Font_FTFont.hxx>
27
28#include <NCollection_DataMap.hxx>
29#include <NCollection_Vector.hxx>
30#include <TCollection_AsciiString.hxx>
31
32//! Texture font.
33class OpenGl_Font : public OpenGl_Resource
34{
35
36public:
37
38 //! Simple structure stores tile rectangle.
39 struct Tile
40 {
41 Font_FTFont::Rect uv; //!< UV coordinates in texture
42 Font_FTFont::Rect px; //!< pixel displacement coordinates
43 GLuint texture; //!< GL texture ID
44 };
45
46 struct RectI
47 {
48 Standard_Integer Left;
49 Standard_Integer Right;
50 Standard_Integer Top;
51 Standard_Integer Bottom;
52 };
53
54public:
55
56 //! Main constructor.
57 Standard_EXPORT OpenGl_Font (const Handle(Font_FTFont)& theFont,
58 const TCollection_AsciiString& theKey = "");
59
60 //! Destroy object.
61 Standard_EXPORT virtual ~OpenGl_Font();
62
63 //! Destroy object - will release GPU memory if any
64 Standard_EXPORT virtual void Release (const OpenGl_Context* theCtx);
65
66 //! @return key of shared resource
67 inline const TCollection_AsciiString& ResourceKey() const
68 {
69 return myKey;
70 }
71
72 //! @return FreeType font instance specified on construction.
73 inline const Handle(Font_FTFont)& FTFont() const
74 {
75 return myFont;
76 }
77
78 //! @return true if font was loaded successfully.
79 inline bool IsValid() const
80 {
81 return !myTextures.IsEmpty() && myTextures.First()->IsValid();
82 }
83
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
88 {
89 return !myTextures.IsEmpty();
90 }
91
92 //! Initialize GL resources.
93 //! FreeType font instance should be already initialized!
94 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx);
95
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)
100 {
101 return myFont->AdvanceX (theUChar, theUCharNext);
102 }
103
104 //! @return vertical distance from the horizontal baseline to the highest character coordinate
105 inline float Ascender() const
106 {
107 return myAscender;
108 }
109
110 //! @return vertical distance from the horizontal baseline to the lowest character coordinate
111 inline float Descender() const
112 {
113 return myDescender;
114 }
115
116 //! @return default line spacing (the baseline-to-baseline distance)
117 inline float LineSpacing() const
118 {
119 return myLineSpacing;
120 }
121
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,
132 Tile& theGlyph,
133 OpenGl_Vec2& thePen);
134
135protected:
136
137 //! Render new glyph to the texture.
138 bool renderGlyph (const Handle(OpenGl_Context)& theCtx,
139 const Standard_Utf32Char theChar);
140
141 //! Allocate new texture.
142 bool createTexture (const Handle(OpenGl_Context)& theCtx);
143
144protected:
145
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
154 RectI myLastTilePx;
155 Standard_Integer myTextureFormat; //!< texture format
156
157 NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< array of textures
158 NCollection_Vector<Tile> myTiles; //!< array of loaded tiles
159
160 NCollection_DataMap<Standard_Utf32Char, Standard_Integer> myGlyphMap;
161
162public:
163
164 DEFINE_STANDARD_RTTI(OpenGl_Font) // Type definition
165
166};
167
168DEFINE_STANDARD_HANDLE(OpenGl_Font, OpenGl_Resource)
169
170#endif // _OpenGl_Font_H__