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