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