0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[occt.git] / src / OpenGl / OpenGl_Font.cxx
CommitLineData
a174a3c5 1// Created on: 2013-01-29
2// Created by: Kirill GAVRILOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
a174a3c5 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
a174a3c5 6//
d5f74e42 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
973c2be1 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.
a174a3c5 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
a174a3c5 15
16#include <OpenGl_Font.hxx>
17
18#include <OpenGl_Context.hxx>
d2eddacc 19#include <Font_FTFont.hxx>
fe3a29bc 20#include <Graphic3d_TextureParams.hxx>
a174a3c5 21#include <Standard_Assert.hxx>
cbf18624 22#include <TCollection_ExtendedString.hxx>
a174a3c5 23
92efcf78 24IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource)
25
a174a3c5 26// =======================================================================
27// function : OpenGl_Font
28// purpose :
29// =======================================================================
30OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)& theFont,
31 const TCollection_AsciiString& theKey)
32: myKey (theKey),
33 myFont (theFont),
34 myAscender (0.0f),
35 myDescender (0.0f),
a174a3c5 36 myTileSizeY (0),
37 myLastTileId (-1),
ca3c13d1 38 myTextureFormat (GL_ALPHA)
a174a3c5 39{
40 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
41}
42
43// =======================================================================
44// function : ~OpenGl_Font
45// purpose :
46// =======================================================================
47OpenGl_Font::~OpenGl_Font()
48{
49 Release (NULL);
50}
51
52// =======================================================================
53// function : Release
54// purpose :
55// =======================================================================
10b9c7df 56void OpenGl_Font::Release (OpenGl_Context* theCtx)
a174a3c5 57{
58 if (myTextures.IsEmpty())
59 {
60 return;
61 }
62
a174a3c5 63 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
64 {
65 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter);
c6ad5e5f 66 if (aTexture->IsValid())
67 {
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...",);
71 }
72
a174a3c5 73 aTexture->Release (theCtx);
74 aTexture.Nullify();
75 }
76 myTextures.Clear();
77}
78
79// =======================================================================
15669413 80// function : EstimatedDataSize
81// purpose :
82// =======================================================================
83Standard_Size OpenGl_Font::EstimatedDataSize() const
84{
85 Standard_Size aSize = 0;
86 for (NCollection_Vector<Handle(OpenGl_Texture)>::Iterator aTexIter (myTextures); aTexIter.More(); aTexIter.Next())
87 {
88 aSize += aTexIter.Value()->EstimatedDataSize();
89 }
90 return aSize;
91}
92
93// =======================================================================
a174a3c5 94// function : Init
95// purpose :
96// =======================================================================
97bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx)
98{
99 Release (theCtx.operator->());
100 if (myFont.IsNull() || !myFont->IsValid())
101 {
102 return false;
103 }
104
912761ea 105 myAscender = myFont->Ascender();
106 myDescender = myFont->Descender();
107 myTileSizeY = myFont->GlyphMaxSizeY (true);
a174a3c5 108
109 myLastTileId = -1;
c6ad5e5f 110 if (!createTexture (theCtx))
111 {
112 Release (theCtx.operator->());
113 return false;
114 }
115 return true;
a174a3c5 116}
117
118// =======================================================================
119// function : createTexture
120// purpose :
121// =======================================================================
122bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx)
123{
912761ea 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;
127
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;
a174a3c5 134 const Standard_Integer aTextureSizeY = OpenGl_Context::GetPowerOfTwo (GLint((aGlyphsNb / aTilesPerRow) + 1) * myTileSizeY, aMaxSize);
135
136 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
137 myLastTilePx.Bottom = myTileSizeY;
138
fe3a29bc 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);
144
cc8cbabe 145 myTextures.Append (new OpenGl_Texture (myKey + "_texture" + myTextures.Size(), aParams));
a174a3c5 146 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
147
148 Image_PixMap aBlackImg;
dc858f4c 149 if (!aBlackImg.InitZero (Image_Format_Alpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY))
a174a3c5 150 || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat
151 {
cbf18624 152 TCollection_ExtendedString aMsg;
dc858f4c 153 aMsg += "New texture initialization of size ";
cbf18624 154 aMsg += aTextureSizeX;
155 aMsg += "x";
156 aMsg += aTextureSizeY;
157 aMsg += " for textured font has failed.";
3b523c4c 158 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
a174a3c5 159 return false;
160 }
161
a174a3c5 162 return true;
163}
164
165// =======================================================================
166// function : renderGlyph
167// purpose :
168// =======================================================================
169bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx,
170 const Standard_Utf32Char theChar)
171{
172 if (!myFont->RenderGlyph (theChar))
173 {
174 return false;
175 }
176
177 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
c6ad5e5f 178 if (aTexture.IsNull()
179 || !aTexture->IsValid())
180 {
181 return false;
182 }
a174a3c5 183
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();
912761ea 188 if (myLastTilePx.Right > aTexture->SizeX()
189 || (Standard_Integer )anImg.SizeY() > myTileSizeY)
a174a3c5 190 {
912761ea 191 myTileSizeY = myFont->GlyphMaxSizeY (true);
192
a174a3c5 193 myLastTilePx.Left = 0;
194 myLastTilePx.Right = (Standard_Integer )anImg.SizeX();
195 myLastTilePx.Top += myTileSizeY;
196 myLastTilePx.Bottom += myTileSizeY;
197
912761ea 198 if (myLastTilePx.Bottom > aTexture->SizeY()
199 || myLastTilePx.Right > aTexture->SizeX())
a174a3c5 200 {
201 if (!createTexture (theCtx))
202 {
203 return false;
204 }
205 return renderGlyph (theCtx, theChar);
206 }
207 }
208
209 aTexture->Bind (theCtx);
ca3c13d1 210#if !defined(GL_ES_VERSION_2_0)
a174a3c5 211 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
212 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
ca3c13d1 213#endif
a174a3c5 214 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
215
216 glTexSubImage2D (GL_TEXTURE_2D, 0,
217 myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(),
c6ad5e5f 218 aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data());
a174a3c5 219
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);
227
228 myLastTileId = aTileId;
229 myTiles.Append (aTile);
230 return true;
231}
232
233// =======================================================================
234// function : RenderGlyph
235// purpose :
236// =======================================================================
317d68c9 237bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
a174a3c5 238 const Standard_Utf32Char theUChar,
317d68c9 239 Tile& theGlyph)
a174a3c5 240{
241 Standard_Integer aTileId = 0;
317d68c9 242 if (!myGlyphMap.Find (theUChar,aTileId))
a174a3c5 243 {
244 if (renderGlyph (theCtx, theUChar))
245 {
246 aTileId = myLastTileId;
247 }
248 else
249 {
317d68c9 250 return false;
a174a3c5 251 }
317d68c9 252
a174a3c5 253 myGlyphMap.Bind (theUChar, aTileId);
254 }
255
256 const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
317d68c9 257 theGlyph.px = aTile.px;
258 theGlyph.uv = aTile.uv;
259 theGlyph.texture = aTile.texture;
260
261 return true;
a174a3c5 262}