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>
23 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource)
25 // =======================================================================
26 // function : OpenGl_Font
28 // =======================================================================
29 OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)& theFont,
30 const TCollection_AsciiString& theKey)
37 myTextureFormat (GL_ALPHA)
39 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
42 // =======================================================================
43 // function : ~OpenGl_Font
45 // =======================================================================
46 OpenGl_Font::~OpenGl_Font()
51 // =======================================================================
54 // =======================================================================
55 void OpenGl_Font::Release (OpenGl_Context* theCtx)
57 if (myTextures.IsEmpty())
62 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
64 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter);
65 if (aTexture->IsValid())
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...",);
72 aTexture->Release (theCtx);
78 // =======================================================================
79 // function : EstimatedDataSize
81 // =======================================================================
82 Standard_Size OpenGl_Font::EstimatedDataSize() const
84 Standard_Size aSize = 0;
85 for (NCollection_Vector<Handle(OpenGl_Texture)>::Iterator aTexIter (myTextures); aTexIter.More(); aTexIter.Next())
87 aSize += aTexIter.Value()->EstimatedDataSize();
92 // =======================================================================
95 // =======================================================================
96 bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx)
98 Release (theCtx.operator->());
99 if (myFont.IsNull() || !myFont->IsValid())
104 myAscender = myFont->Ascender();
105 myDescender = myFont->Descender();
106 myTileSizeY = myFont->GlyphMaxSizeY (true);
109 if (!createTexture (theCtx))
111 Release (theCtx.operator->());
117 // =======================================================================
118 // function : createTexture
120 // =======================================================================
121 bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx)
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;
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);
135 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
136 myLastTilePx.Bottom = myTileSizeY;
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);
144 myTextures.Append (new OpenGl_Texture (myKey + "_texture" + myTextures.Size(), aParams));
145 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
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
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.");
160 // =======================================================================
161 // function : renderGlyph
163 // =======================================================================
164 bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx,
165 const Standard_Utf32Char theChar)
167 if (!myFont->RenderGlyph (theChar))
172 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
173 if (aTexture.IsNull()
174 || !aTexture->IsValid())
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)
186 myTileSizeY = myFont->GlyphMaxSizeY (true);
188 myLastTilePx.Left = 0;
189 myLastTilePx.Right = (Standard_Integer )anImg.SizeX();
190 myLastTilePx.Top += myTileSizeY;
191 myLastTilePx.Bottom += myTileSizeY;
193 if (myLastTilePx.Bottom > aTexture->SizeY()
194 || myLastTilePx.Right > aTexture->SizeX())
196 if (!createTexture (theCtx))
200 return renderGlyph (theCtx, theChar);
204 aTexture->Bind (theCtx);
205 if (theCtx->GraphicsLibrary() != Aspect_GraphicsLibrary_OpenGLES)
207 theCtx->core11fwd->glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
209 if (theCtx->hasUnpackRowLength)
211 theCtx->core11fwd->glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
213 theCtx->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
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());
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);
227 myLastTileId = aTileId;
228 myTiles.Append (aTile);
232 // =======================================================================
233 // function : RenderGlyph
235 // =======================================================================
236 bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
237 const Standard_Utf32Char theUChar,
240 Standard_Integer aTileId = 0;
241 if (!myGlyphMap.Find (theUChar,aTileId))
243 if (renderGlyph (theCtx, theUChar))
245 aTileId = myLastTileId;
252 myGlyphMap.Bind (theUChar, aTileId);
255 const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
256 theGlyph.px = aTile.px;
257 theGlyph.uv = aTile.uv;
258 theGlyph.texture = aTile.texture;