0027266: Coding, TKOpenGl - drop unused files OpenGl_telem_util.hxx and OpenGl_tgl_fu...
[occt.git] / src / OpenGl / OpenGl_Font.cxx
1 // Created on: 2013-01-29
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_Font.hxx>
17
18 #include <OpenGl_Context.hxx>
19 #include <Font_FTFont.hxx>
20 #include <Graphic3d_TextureParams.hxx>
21 #include <Standard_Assert.hxx>
22 #include <TCollection_ExtendedString.hxx>
23
24
25 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource)
26
27 // =======================================================================
28 // function : OpenGl_Font
29 // purpose  :
30 // =======================================================================
31 OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)&     theFont,
32                           const TCollection_AsciiString& theKey)
33 : myKey  (theKey),
34   myFont (theFont),
35   myAscender (0.0f),
36   myDescender (0.0f),
37   myLineSpacing (0.0f),
38   myTileSizeX (0),
39   myTileSizeY (0),
40   myLastTileId (-1),
41   myTextureFormat (GL_ALPHA)
42 {
43   memset (&myLastTilePx, 0, sizeof(myLastTilePx));
44 }
45
46 // =======================================================================
47 // function : ~OpenGl_Font
48 // purpose  :
49 // =======================================================================
50 OpenGl_Font::~OpenGl_Font()
51 {
52   Release (NULL);
53 }
54
55 // =======================================================================
56 // function : Release
57 // purpose  :
58 // =======================================================================
59 void OpenGl_Font::Release (OpenGl_Context* theCtx)
60 {
61   if (myTextures.IsEmpty())
62   {
63     return;
64   }
65
66   for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
67   {
68     Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter);
69     if (aTexture->IsValid())
70     {
71       // application can not handle this case by exception - this is bug in code
72       Standard_ASSERT_RETURN (theCtx != NULL,
73         "OpenGl_Font destroyed without GL context! Possible GPU memory leakage...",);
74     }
75
76     aTexture->Release (theCtx);
77     aTexture.Nullify();
78   }
79   myTextures.Clear();
80 }
81
82 // =======================================================================
83 // function : Init
84 // purpose  :
85 // =======================================================================
86 bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx)
87 {
88   Release (theCtx.operator->());
89   if (myFont.IsNull() || !myFont->IsValid())
90   {
91     return false;
92   }
93
94   myAscender    = myFont->Ascender();
95   myDescender   = myFont->Descender();
96   myLineSpacing = myFont->LineSpacing();
97   myTileSizeX   = myFont->GlyphMaxSizeX();
98   myTileSizeY   = myFont->GlyphMaxSizeY();
99
100   myLastTileId = -1;
101   if (!createTexture (theCtx))
102   {
103     Release (theCtx.operator->());
104     return false;
105   }
106   return true;
107 }
108
109 // =======================================================================
110 // function : createTexture
111 // purpose  :
112 // =======================================================================
113 bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx)
114 {
115   const Standard_Integer aMaxSize = theCtx->MaxTextureSize();
116
117   Standard_Integer aGlyphsNb = myFont->GlyphsNumber() - myLastTileId + 1;
118
119   const Standard_Integer aTextureSizeX = OpenGl_Context::GetPowerOfTwo (aGlyphsNb * myTileSizeX, aMaxSize);
120   const Standard_Integer aTilesPerRow  = aTextureSizeX / myTileSizeX;
121   const Standard_Integer aTextureSizeY = OpenGl_Context::GetPowerOfTwo (GLint((aGlyphsNb / aTilesPerRow) + 1) * myTileSizeY, aMaxSize);
122
123   memset (&myLastTilePx, 0, sizeof(myLastTilePx));
124   myLastTilePx.Bottom = myTileSizeY;
125
126   Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams();
127   aParams->SetModulate    (Standard_False);
128   aParams->SetRepeat      (Standard_False);
129   aParams->SetFilter      (Graphic3d_TOTF_BILINEAR);
130   aParams->SetAnisoFilter (Graphic3d_LOTA_OFF);
131
132   myTextures.Append (new OpenGl_Texture (aParams));
133   Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
134
135   Image_PixMap aBlackImg;
136   if (!aBlackImg.InitZero (Image_PixMap::ImgAlpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY))
137    || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat
138   {
139     TCollection_ExtendedString aMsg;
140     aMsg += "New texture intialization of size ";
141     aMsg += aTextureSizeX;
142     aMsg += "x";
143     aMsg += aTextureSizeY;
144     aMsg += " for textured font has failed.";
145     theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
146     return false;
147   }
148
149   return true;
150 }
151
152 // =======================================================================
153 // function : renderGlyph
154 // purpose  :
155 // =======================================================================
156 bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx,
157                                const Standard_Utf32Char      theChar)
158 {
159   if (!myFont->RenderGlyph (theChar))
160   {
161     return false;
162   }
163
164   Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
165   if (aTexture.IsNull()
166   || !aTexture->IsValid())
167   {
168     return false;
169   }
170
171   const Image_PixMap& anImg = myFont->GlyphImage();
172   const Standard_Integer aTileId = myLastTileId + 1;
173   myLastTilePx.Left  = myLastTilePx.Right + 3;
174   myLastTilePx.Right = myLastTilePx.Left + (Standard_Integer )anImg.SizeX();
175   if (myLastTilePx.Right >= aTexture->SizeX())
176   {
177     myLastTilePx.Left    = 0;
178     myLastTilePx.Right   = (Standard_Integer )anImg.SizeX();
179     myLastTilePx.Top    += myTileSizeY;
180     myLastTilePx.Bottom += myTileSizeY;
181
182     if (myLastTilePx.Bottom >= aTexture->SizeY())
183     {
184       if (!createTexture (theCtx))
185       {
186         return false;
187       }
188       return renderGlyph (theCtx, theChar);
189     }
190   }
191
192   aTexture->Bind (theCtx);
193 #if !defined(GL_ES_VERSION_2_0)
194   glPixelStorei (GL_UNPACK_LSB_FIRST,  GL_FALSE);
195   glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
196 #endif
197   glPixelStorei (GL_UNPACK_ALIGNMENT,  1);
198
199   glTexSubImage2D (GL_TEXTURE_2D, 0,
200                    myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(),
201                    aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data());
202
203   OpenGl_Font::Tile aTile;
204   aTile.uv.Left   = GLfloat(myLastTilePx.Left)                / GLfloat(aTexture->SizeX());
205   aTile.uv.Right  = GLfloat(myLastTilePx.Right)               / GLfloat(aTexture->SizeX());
206   aTile.uv.Top    = GLfloat(myLastTilePx.Top)                 / GLfloat(aTexture->SizeY());
207   aTile.uv.Bottom = GLfloat(myLastTilePx.Top + anImg.SizeY()) / GLfloat(aTexture->SizeY());
208   aTile.texture   = aTexture->TextureId();
209   myFont->GlyphRect (aTile.px);
210
211   myLastTileId = aTileId;
212   myTiles.Append (aTile);
213   return true;
214 }
215
216 // =======================================================================
217 // function : RenderGlyph
218 // purpose  :
219 // =======================================================================
220 bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
221                                const Standard_Utf32Char      theUChar,
222                                Tile&                         theGlyph)
223 {
224   Standard_Integer aTileId = 0;
225   if (!myGlyphMap.Find (theUChar,aTileId))
226   {
227     if (renderGlyph (theCtx, theUChar))
228     {
229       aTileId = myLastTileId;
230     }
231     else
232     {
233       return false;
234     }
235
236     myGlyphMap.Bind (theUChar, aTileId);
237   }
238
239   const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
240   theGlyph.px      = aTile.px;
241   theGlyph.uv      = aTile.uv;
242   theGlyph.texture = aTile.texture;
243
244   return true;
245 }