0029528: Visualization, TKOpenGl - allow defining sRGB textures
[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 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource)
25
26 // =======================================================================
27 // function : OpenGl_Font
28 // purpose  :
29 // =======================================================================
30 OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)&     theFont,
31                           const TCollection_AsciiString& theKey)
32 : myKey  (theKey),
33   myFont (theFont),
34   myAscender (0.0f),
35   myDescender (0.0f),
36   myTileSizeY (0),
37   myLastTileId (-1),
38   myTextureFormat (GL_ALPHA)
39 {
40   memset (&myLastTilePx, 0, sizeof(myLastTilePx));
41 }
42
43 // =======================================================================
44 // function : ~OpenGl_Font
45 // purpose  :
46 // =======================================================================
47 OpenGl_Font::~OpenGl_Font()
48 {
49   Release (NULL);
50 }
51
52 // =======================================================================
53 // function : Release
54 // purpose  :
55 // =======================================================================
56 void OpenGl_Font::Release (OpenGl_Context* theCtx)
57 {
58   if (myTextures.IsEmpty())
59   {
60     return;
61   }
62
63   for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
64   {
65     Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter);
66     if (aTexture->IsValid())
67     {
68       // application can not handle this case by exception - this is bug in code
69       Standard_ASSERT_RETURN (theCtx != NULL,
70         "OpenGl_Font destroyed without GL context! Possible GPU memory leakage...",);
71     }
72
73     aTexture->Release (theCtx);
74     aTexture.Nullify();
75   }
76   myTextures.Clear();
77 }
78
79 // =======================================================================
80 // function : EstimatedDataSize
81 // purpose  :
82 // =======================================================================
83 Standard_Size OpenGl_Font::EstimatedDataSize() const
84 {
85   Standard_Size aSize = 0;
86   for (NCollection_Vector<Handle(OpenGl_Texture)>::Iterator aTexIter (myTextures); aTexIter.More(); aTexIter.Next())
87   {
88     aSize += aTexIter.Value()->EstimatedDataSize();
89   }
90   return aSize;
91 }
92
93 // =======================================================================
94 // function : Init
95 // purpose  :
96 // =======================================================================
97 bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx)
98 {
99   Release (theCtx.operator->());
100   if (myFont.IsNull() || !myFont->IsValid())
101   {
102     return false;
103   }
104
105   myAscender  = myFont->Ascender();
106   myDescender = myFont->Descender();
107   myTileSizeY = myFont->GlyphMaxSizeY (true);
108
109   myLastTileId = -1;
110   if (!createTexture (theCtx))
111   {
112     Release (theCtx.operator->());
113     return false;
114   }
115   return true;
116 }
117
118 // =======================================================================
119 // function : createTexture
120 // purpose  :
121 // =======================================================================
122 bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx)
123 {
124   // Single font might define very wide range of symbols, with very few of them actually used in text.
125   // Limit single texture with circa 4096 glyphs.
126   static const Standard_Integer THE_MAX_GLYPHS_PER_TEXTURE = 4096;
127
128   myTileSizeY = myFont->GlyphMaxSizeY (true);
129   const Standard_Integer aGlyphsNb = Min (THE_MAX_GLYPHS_PER_TEXTURE, myFont->GlyphsNumber (true) - myLastTileId + 1);
130   const Standard_Integer aMaxTileSizeX = myFont->GlyphMaxSizeX (true);
131   const Standard_Integer aMaxSize      = theCtx->MaxTextureSize();
132   const Standard_Integer aTextureSizeX = OpenGl_Context::GetPowerOfTwo (aGlyphsNb * aMaxTileSizeX, aMaxSize);
133   const Standard_Integer aTilesPerRow  = aTextureSizeX / aMaxTileSizeX;
134   const Standard_Integer aTextureSizeY = OpenGl_Context::GetPowerOfTwo (GLint((aGlyphsNb / aTilesPerRow) + 1) * myTileSizeY, aMaxSize);
135
136   memset (&myLastTilePx, 0, sizeof(myLastTilePx));
137   myLastTilePx.Bottom = myTileSizeY;
138
139   Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams();
140   aParams->SetModulate    (Standard_False);
141   aParams->SetRepeat      (Standard_False);
142   aParams->SetFilter      (Graphic3d_TOTF_BILINEAR);
143   aParams->SetAnisoFilter (Graphic3d_LOTA_OFF);
144
145   myTextures.Append (new OpenGl_Texture (myKey + "_texture" + myTextures.Size(), aParams));
146   Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
147
148   Image_PixMap aBlackImg;
149   if (!aBlackImg.InitZero (Image_Format_Alpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY))
150    || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D, true)) // myTextureFormat
151   {
152     theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
153                          TCollection_AsciiString ("New texture initialization of size ")
154                        + aTextureSizeX + "x" + aTextureSizeY + " for textured font has failed.");
155     return false;
156   }
157
158   return true;
159 }
160
161 // =======================================================================
162 // function : renderGlyph
163 // purpose  :
164 // =======================================================================
165 bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx,
166                                const Standard_Utf32Char      theChar)
167 {
168   if (!myFont->RenderGlyph (theChar))
169   {
170     return false;
171   }
172
173   Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
174   if (aTexture.IsNull()
175   || !aTexture->IsValid())
176   {
177     return false;
178   }
179
180   const Image_PixMap& anImg = myFont->GlyphImage();
181   const Standard_Integer aTileId = myLastTileId + 1;
182   myLastTilePx.Left  = myLastTilePx.Right + 3;
183   myLastTilePx.Right = myLastTilePx.Left + (Standard_Integer )anImg.SizeX();
184   if (myLastTilePx.Right > aTexture->SizeX()
185    || (Standard_Integer )anImg.SizeY() > myTileSizeY)
186   {
187     myTileSizeY = myFont->GlyphMaxSizeY (true);
188
189     myLastTilePx.Left    = 0;
190     myLastTilePx.Right   = (Standard_Integer )anImg.SizeX();
191     myLastTilePx.Top    += myTileSizeY;
192     myLastTilePx.Bottom += myTileSizeY;
193
194     if (myLastTilePx.Bottom > aTexture->SizeY()
195      || myLastTilePx.Right  > aTexture->SizeX())
196     {
197       if (!createTexture (theCtx))
198       {
199         return false;
200       }
201       return renderGlyph (theCtx, theChar);
202     }
203   }
204
205   aTexture->Bind (theCtx);
206 #if !defined(GL_ES_VERSION_2_0)
207   glPixelStorei (GL_UNPACK_LSB_FIRST,  GL_FALSE);
208   glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
209 #endif
210   glPixelStorei (GL_UNPACK_ALIGNMENT,  1);
211
212   glTexSubImage2D (GL_TEXTURE_2D, 0,
213                    myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(),
214                    aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data());
215
216   OpenGl_Font::Tile aTile;
217   aTile.uv.Left   = GLfloat(myLastTilePx.Left)                / GLfloat(aTexture->SizeX());
218   aTile.uv.Right  = GLfloat(myLastTilePx.Right)               / GLfloat(aTexture->SizeX());
219   aTile.uv.Top    = GLfloat(myLastTilePx.Top)                 / GLfloat(aTexture->SizeY());
220   aTile.uv.Bottom = GLfloat(myLastTilePx.Top + anImg.SizeY()) / GLfloat(aTexture->SizeY());
221   aTile.texture   = aTexture->TextureId();
222   myFont->GlyphRect (aTile.px);
223
224   myLastTileId = aTileId;
225   myTiles.Append (aTile);
226   return true;
227 }
228
229 // =======================================================================
230 // function : RenderGlyph
231 // purpose  :
232 // =======================================================================
233 bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
234                                const Standard_Utf32Char      theUChar,
235                                Tile&                         theGlyph)
236 {
237   Standard_Integer aTileId = 0;
238   if (!myGlyphMap.Find (theUChar,aTileId))
239   {
240     if (renderGlyph (theCtx, theUChar))
241     {
242       aTileId = myLastTileId;
243     }
244     else
245     {
246       return false;
247     }
248
249     myGlyphMap.Bind (theUChar, aTileId);
250   }
251
252   const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
253   theGlyph.px      = aTile.px;
254   theGlyph.uv      = aTile.uv;
255   theGlyph.texture = aTile.texture;
256
257   return true;
258 }