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>
24 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource)
26 // =======================================================================
27 // function : OpenGl_Font
29 // =======================================================================
30 OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)& theFont,
31 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 // =======================================================================
80 // function : EstimatedDataSize
82 // =======================================================================
83 Standard_Size OpenGl_Font::EstimatedDataSize() const
85 Standard_Size aSize = 0;
86 for (NCollection_Vector<Handle(OpenGl_Texture)>::Iterator aTexIter (myTextures); aTexIter.More(); aTexIter.Next())
88 aSize += aTexIter.Value()->EstimatedDataSize();
93 // =======================================================================
96 // =======================================================================
97 bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx)
99 Release (theCtx.operator->());
100 if (myFont.IsNull() || !myFont->IsValid())
105 myAscender = myFont->Ascender();
106 myDescender = myFont->Descender();
107 myTileSizeY = myFont->GlyphMaxSizeY (true);
110 if (!createTexture (theCtx))
112 Release (theCtx.operator->());
118 // =======================================================================
119 // function : createTexture
121 // =======================================================================
122 bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx)
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;
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);
136 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
137 myLastTilePx.Bottom = myTileSizeY;
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);
145 myTextures.Append (new OpenGl_Texture (myKey + "_texture" + myTextures.Size(), aParams));
146 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
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)) // myTextureFormat
152 TCollection_ExtendedString aMsg;
153 aMsg += "New texture initialization of size ";
154 aMsg += aTextureSizeX;
156 aMsg += aTextureSizeY;
157 aMsg += " for textured font has failed.";
158 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
165 // =======================================================================
166 // function : renderGlyph
168 // =======================================================================
169 bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx,
170 const Standard_Utf32Char theChar)
172 if (!myFont->RenderGlyph (theChar))
177 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
178 if (aTexture.IsNull()
179 || !aTexture->IsValid())
184 const Image_PixMap& anImg = myFont->GlyphImage();
185 const Standard_Integer aTileId = myLastTileId + 1;
186 myLastTilePx.Left = myLastTilePx.Right + 3;
187 myLastTilePx.Right = myLastTilePx.Left + (Standard_Integer )anImg.SizeX();
188 if (myLastTilePx.Right > aTexture->SizeX()
189 || (Standard_Integer )anImg.SizeY() > myTileSizeY)
191 myTileSizeY = myFont->GlyphMaxSizeY (true);
193 myLastTilePx.Left = 0;
194 myLastTilePx.Right = (Standard_Integer )anImg.SizeX();
195 myLastTilePx.Top += myTileSizeY;
196 myLastTilePx.Bottom += myTileSizeY;
198 if (myLastTilePx.Bottom > aTexture->SizeY()
199 || myLastTilePx.Right > aTexture->SizeX())
201 if (!createTexture (theCtx))
205 return renderGlyph (theCtx, theChar);
209 aTexture->Bind (theCtx);
210 #if !defined(GL_ES_VERSION_2_0)
211 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
212 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
214 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
216 glTexSubImage2D (GL_TEXTURE_2D, 0,
217 myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(),
218 aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data());
220 OpenGl_Font::Tile aTile;
221 aTile.uv.Left = GLfloat(myLastTilePx.Left) / GLfloat(aTexture->SizeX());
222 aTile.uv.Right = GLfloat(myLastTilePx.Right) / GLfloat(aTexture->SizeX());
223 aTile.uv.Top = GLfloat(myLastTilePx.Top) / GLfloat(aTexture->SizeY());
224 aTile.uv.Bottom = GLfloat(myLastTilePx.Top + anImg.SizeY()) / GLfloat(aTexture->SizeY());
225 aTile.texture = aTexture->TextureId();
226 myFont->GlyphRect (aTile.px);
228 myLastTileId = aTileId;
229 myTiles.Append (aTile);
233 // =======================================================================
234 // function : RenderGlyph
236 // =======================================================================
237 bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
238 const Standard_Utf32Char theUChar,
241 Standard_Integer aTileId = 0;
242 if (!myGlyphMap.Find (theUChar,aTileId))
244 if (renderGlyph (theCtx, theUChar))
246 aTileId = myLastTileId;
253 myGlyphMap.Bind (theUChar, aTileId);
256 const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
257 theGlyph.px = aTile.px;
258 theGlyph.uv = aTile.uv;
259 theGlyph.texture = aTile.texture;