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 // =======================================================================
85 // =======================================================================
86 bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx)
88 Release (theCtx.operator->());
89 if (myFont.IsNull() || !myFont->IsValid())
94 myAscender = myFont->Ascender();
95 myDescender = myFont->Descender();
96 myLineSpacing = myFont->LineSpacing();
97 myTileSizeX = myFont->GlyphMaxSizeX();
98 myTileSizeY = myFont->GlyphMaxSizeY();
101 if (!createTexture (theCtx))
103 Release (theCtx.operator->());
109 // =======================================================================
110 // function : createTexture
112 // =======================================================================
113 bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx)
115 const Standard_Integer aMaxSize = theCtx->MaxTextureSize();
117 Standard_Integer aGlyphsNb = myFont->GlyphsNumber() - myLastTileId + 1;
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);
123 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
124 myLastTilePx.Bottom = myTileSizeY;
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);
132 myTextures.Append (new OpenGl_Texture (aParams));
133 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
135 Image_PixMap aBlackImg;
136 if (!aBlackImg.InitZero (Image_Format_Alpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY))
137 || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat
139 TCollection_ExtendedString aMsg;
140 aMsg += "New texture initialization of size ";
141 aMsg += aTextureSizeX;
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);
152 // =======================================================================
153 // function : renderGlyph
155 // =======================================================================
156 bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx,
157 const Standard_Utf32Char theChar)
159 if (!myFont->RenderGlyph (theChar))
164 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
165 if (aTexture.IsNull()
166 || !aTexture->IsValid())
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())
177 myLastTilePx.Left = 0;
178 myLastTilePx.Right = (Standard_Integer )anImg.SizeX();
179 myLastTilePx.Top += myTileSizeY;
180 myLastTilePx.Bottom += myTileSizeY;
182 if (myLastTilePx.Bottom >= aTexture->SizeY())
184 if (!createTexture (theCtx))
188 return renderGlyph (theCtx, theChar);
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);
197 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
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());
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);
211 myLastTileId = aTileId;
212 myTiles.Append (aTile);
216 // =======================================================================
217 // function : RenderGlyph
219 // =======================================================================
220 bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
221 const Standard_Utf32Char theUChar,
224 Standard_Integer aTileId = 0;
225 if (!myGlyphMap.Find (theUChar,aTileId))
227 if (renderGlyph (theCtx, theUChar))
229 aTileId = myLastTileId;
236 myGlyphMap.Bind (theUChar, aTileId);
239 const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
240 theGlyph.px = aTile.px;
241 theGlyph.uv = aTile.uv;
242 theGlyph.texture = aTile.texture;