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 <Graphic3d_TextureParams.hxx>
20 #include <Standard_Assert.hxx>
21 #include <TCollection_ExtendedString.hxx>
24 // =======================================================================
25 // function : OpenGl_Font
27 // =======================================================================
28 OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)& theFont,
29 const TCollection_AsciiString& theKey)
38 myTextureFormat (GL_ALPHA)
40 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
43 // =======================================================================
44 // function : ~OpenGl_Font
46 // =======================================================================
47 OpenGl_Font::~OpenGl_Font()
52 // =======================================================================
55 // =======================================================================
56 void OpenGl_Font::Release (OpenGl_Context* theCtx)
58 if (myTextures.IsEmpty())
63 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
65 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter);
66 if (aTexture->IsValid())
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...",);
73 aTexture->Release (theCtx);
79 // =======================================================================
82 // =======================================================================
83 bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx)
85 Release (theCtx.operator->());
86 if (myFont.IsNull() || !myFont->IsValid())
91 myAscender = myFont->Ascender();
92 myDescender = myFont->Descender();
93 myLineSpacing = myFont->LineSpacing();
94 myTileSizeX = myFont->GlyphMaxSizeX();
95 myTileSizeY = myFont->GlyphMaxSizeY();
98 if (!createTexture (theCtx))
100 Release (theCtx.operator->());
106 // =======================================================================
107 // function : createTexture
109 // =======================================================================
110 bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx)
112 const Standard_Integer aMaxSize = theCtx->MaxTextureSize();
114 Standard_Integer aGlyphsNb = myFont->GlyphsNumber() - myLastTileId + 1;
116 const Standard_Integer aTextureSizeX = OpenGl_Context::GetPowerOfTwo (aGlyphsNb * myTileSizeX, aMaxSize);
117 const Standard_Integer aTilesPerRow = aTextureSizeX / myTileSizeX;
118 const Standard_Integer aTextureSizeY = OpenGl_Context::GetPowerOfTwo (GLint((aGlyphsNb / aTilesPerRow) + 1) * myTileSizeY, aMaxSize);
120 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
121 myLastTilePx.Bottom = myTileSizeY;
123 Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams();
124 aParams->SetModulate (Standard_False);
125 aParams->SetRepeat (Standard_False);
126 aParams->SetFilter (Graphic3d_TOTF_BILINEAR);
127 aParams->SetAnisoFilter (Graphic3d_LOTA_OFF);
129 myTextures.Append (new OpenGl_Texture (aParams));
130 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
132 Image_PixMap aBlackImg;
133 if (!aBlackImg.InitZero (Image_PixMap::ImgAlpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY))
134 || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat
136 TCollection_ExtendedString aMsg;
137 aMsg += "New texture intialization of size ";
138 aMsg += aTextureSizeX;
140 aMsg += aTextureSizeY;
141 aMsg += " for textured font has failed.";
142 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
149 // =======================================================================
150 // function : renderGlyph
152 // =======================================================================
153 bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx,
154 const Standard_Utf32Char theChar)
156 if (!myFont->RenderGlyph (theChar))
161 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
162 if (aTexture.IsNull()
163 || !aTexture->IsValid())
168 const Image_PixMap& anImg = myFont->GlyphImage();
169 const Standard_Integer aTileId = myLastTileId + 1;
170 myLastTilePx.Left = myLastTilePx.Right + 3;
171 myLastTilePx.Right = myLastTilePx.Left + (Standard_Integer )anImg.SizeX();
172 if (myLastTilePx.Right >= aTexture->SizeX())
174 myLastTilePx.Left = 0;
175 myLastTilePx.Right = (Standard_Integer )anImg.SizeX();
176 myLastTilePx.Top += myTileSizeY;
177 myLastTilePx.Bottom += myTileSizeY;
179 if (myLastTilePx.Bottom >= aTexture->SizeY())
181 if (!createTexture (theCtx))
185 return renderGlyph (theCtx, theChar);
189 aTexture->Bind (theCtx);
190 #if !defined(GL_ES_VERSION_2_0)
191 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
192 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
194 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
196 glTexSubImage2D (GL_TEXTURE_2D, 0,
197 myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(),
198 aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data());
200 OpenGl_Font::Tile aTile;
201 aTile.uv.Left = GLfloat(myLastTilePx.Left) / GLfloat(aTexture->SizeX());
202 aTile.uv.Right = GLfloat(myLastTilePx.Right) / GLfloat(aTexture->SizeX());
203 aTile.uv.Top = GLfloat(myLastTilePx.Top) / GLfloat(aTexture->SizeY());
204 aTile.uv.Bottom = GLfloat(myLastTilePx.Top + anImg.SizeY()) / GLfloat(aTexture->SizeY());
205 aTile.texture = aTexture->TextureId();
206 myFont->GlyphRect (aTile.px);
208 myLastTileId = aTileId;
209 myTiles.Append (aTile);
213 // =======================================================================
214 // function : RenderGlyph
216 // =======================================================================
217 void OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
218 const Standard_Utf32Char theUChar,
219 const Standard_Utf32Char theUCharNext,
220 OpenGl_Font::Tile& theGlyph,
223 Standard_Integer aTileId = 0;
224 if (!myGlyphMap.Find (theUChar, aTileId))
226 if (renderGlyph (theCtx, theUChar))
228 aTileId = myLastTileId;
232 thePen.x() += myFont->AdvanceX (theUChar, theUCharNext);
235 myGlyphMap.Bind (theUChar, aTileId);
238 const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
239 theGlyph.px.Top = thePen.y() + aTile.px.Top;
240 theGlyph.px.Bottom = thePen.y() + aTile.px.Bottom;
241 theGlyph.px.Left = thePen.x() + aTile.px.Left;
242 theGlyph.px.Right = thePen.x() + aTile.px.Right;
243 theGlyph.uv = aTile.uv;
244 theGlyph.texture = aTile.texture;
246 thePen.x() += myFont->AdvanceX (theUChar, theUCharNext);