1 // Created on: 2013-01-29
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_Font.hxx>
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>
25 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource)
27 // =======================================================================
28 // function : OpenGl_Font
30 // =======================================================================
31 OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)& theFont,
32 const TCollection_AsciiString& theKey)
41 myTextureFormat (GL_ALPHA)
43 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
46 // =======================================================================
47 // function : ~OpenGl_Font
49 // =======================================================================
50 OpenGl_Font::~OpenGl_Font()
55 // =======================================================================
58 // =======================================================================
59 void OpenGl_Font::Release (OpenGl_Context* theCtx)
61 if (myTextures.IsEmpty())
66 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
68 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter);
69 if (aTexture->IsValid())
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...",);
76 aTexture->Release (theCtx);
82 // =======================================================================
83 // function : EstimatedDataSize
85 // =======================================================================
86 Standard_Size OpenGl_Font::EstimatedDataSize() const
88 Standard_Size aSize = 0;
89 for (NCollection_Vector<Handle(OpenGl_Texture)>::Iterator aTexIter (myTextures); aTexIter.More(); aTexIter.Next())
91 aSize += aTexIter.Value()->EstimatedDataSize();
96 // =======================================================================
99 // =======================================================================
100 bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx)
102 Release (theCtx.operator->());
103 if (myFont.IsNull() || !myFont->IsValid())
108 myAscender = myFont->Ascender();
109 myDescender = myFont->Descender();
110 myLineSpacing = myFont->LineSpacing();
111 myTileSizeX = myFont->GlyphMaxSizeX();
112 myTileSizeY = myFont->GlyphMaxSizeY();
115 if (!createTexture (theCtx))
117 Release (theCtx.operator->());
123 // =======================================================================
124 // function : createTexture
126 // =======================================================================
127 bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx)
129 const Standard_Integer aMaxSize = theCtx->MaxTextureSize();
131 Standard_Integer aGlyphsNb = myFont->GlyphsNumber() - myLastTileId + 1;
133 const Standard_Integer aTextureSizeX = OpenGl_Context::GetPowerOfTwo (aGlyphsNb * myTileSizeX, aMaxSize);
134 const Standard_Integer aTilesPerRow = aTextureSizeX / myTileSizeX;
135 const Standard_Integer aTextureSizeY = OpenGl_Context::GetPowerOfTwo (GLint((aGlyphsNb / aTilesPerRow) + 1) * myTileSizeY, aMaxSize);
137 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
138 myLastTilePx.Bottom = myTileSizeY;
140 Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams();
141 aParams->SetModulate (Standard_False);
142 aParams->SetRepeat (Standard_False);
143 aParams->SetFilter (Graphic3d_TOTF_BILINEAR);
144 aParams->SetAnisoFilter (Graphic3d_LOTA_OFF);
146 myTextures.Append (new OpenGl_Texture (myKey + "_texture" + myTextures.Size(), aParams));
147 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
149 Image_PixMap aBlackImg;
150 if (!aBlackImg.InitZero (Image_Format_Alpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY))
151 || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat
153 TCollection_ExtendedString aMsg;
154 aMsg += "New texture initialization of size ";
155 aMsg += aTextureSizeX;
157 aMsg += aTextureSizeY;
158 aMsg += " for textured font has failed.";
159 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
166 // =======================================================================
167 // function : renderGlyph
169 // =======================================================================
170 bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx,
171 const Standard_Utf32Char theChar)
173 if (!myFont->RenderGlyph (theChar))
178 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
179 if (aTexture.IsNull()
180 || !aTexture->IsValid())
185 const Image_PixMap& anImg = myFont->GlyphImage();
186 const Standard_Integer aTileId = myLastTileId + 1;
187 myLastTilePx.Left = myLastTilePx.Right + 3;
188 myLastTilePx.Right = myLastTilePx.Left + (Standard_Integer )anImg.SizeX();
189 if (myLastTilePx.Right >= aTexture->SizeX())
191 myLastTilePx.Left = 0;
192 myLastTilePx.Right = (Standard_Integer )anImg.SizeX();
193 myLastTilePx.Top += myTileSizeY;
194 myLastTilePx.Bottom += myTileSizeY;
196 if (myLastTilePx.Bottom >= aTexture->SizeY())
198 if (!createTexture (theCtx))
202 return renderGlyph (theCtx, theChar);
206 aTexture->Bind (theCtx);
207 #if !defined(GL_ES_VERSION_2_0)
208 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
209 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
211 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
213 glTexSubImage2D (GL_TEXTURE_2D, 0,
214 myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(),
215 aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data());
217 OpenGl_Font::Tile aTile;
218 aTile.uv.Left = GLfloat(myLastTilePx.Left) / GLfloat(aTexture->SizeX());
219 aTile.uv.Right = GLfloat(myLastTilePx.Right) / GLfloat(aTexture->SizeX());
220 aTile.uv.Top = GLfloat(myLastTilePx.Top) / GLfloat(aTexture->SizeY());
221 aTile.uv.Bottom = GLfloat(myLastTilePx.Top + anImg.SizeY()) / GLfloat(aTexture->SizeY());
222 aTile.texture = aTexture->TextureId();
223 myFont->GlyphRect (aTile.px);
225 myLastTileId = aTileId;
226 myTiles.Append (aTile);
230 // =======================================================================
231 // function : RenderGlyph
233 // =======================================================================
234 bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
235 const Standard_Utf32Char theUChar,
238 Standard_Integer aTileId = 0;
239 if (!myGlyphMap.Find (theUChar,aTileId))
241 if (renderGlyph (theCtx, theUChar))
243 aTileId = myLastTileId;
250 myGlyphMap.Bind (theUChar, aTileId);
253 const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
254 theGlyph.px = aTile.px;
255 theGlyph.uv = aTile.uv;
256 theGlyph.texture = aTile.texture;