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 | |
a174a3c5 |
24 | |
25 | // ======================================================================= |
26 | // function : OpenGl_Font |
27 | // purpose : |
28 | // ======================================================================= |
29 | OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)& theFont, |
30 | const TCollection_AsciiString& theKey) |
31 | : myKey (theKey), |
32 | myFont (theFont), |
33 | myAscender (0.0f), |
34 | myDescender (0.0f), |
35 | myLineSpacing (0.0f), |
36 | myTileSizeX (0), |
37 | myTileSizeY (0), |
38 | myLastTileId (-1), |
ca3c13d1 |
39 | myTextureFormat (GL_ALPHA) |
a174a3c5 |
40 | { |
41 | memset (&myLastTilePx, 0, sizeof(myLastTilePx)); |
42 | } |
43 | |
44 | // ======================================================================= |
45 | // function : ~OpenGl_Font |
46 | // purpose : |
47 | // ======================================================================= |
48 | OpenGl_Font::~OpenGl_Font() |
49 | { |
50 | Release (NULL); |
51 | } |
52 | |
53 | // ======================================================================= |
54 | // function : Release |
55 | // purpose : |
56 | // ======================================================================= |
10b9c7df |
57 | void OpenGl_Font::Release (OpenGl_Context* theCtx) |
a174a3c5 |
58 | { |
59 | if (myTextures.IsEmpty()) |
60 | { |
61 | return; |
62 | } |
63 | |
a174a3c5 |
64 | for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter) |
65 | { |
66 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter); |
c6ad5e5f |
67 | if (aTexture->IsValid()) |
68 | { |
69 | // application can not handle this case by exception - this is bug in code |
70 | Standard_ASSERT_RETURN (theCtx != NULL, |
71 | "OpenGl_Font destroyed without GL context! Possible GPU memory leakage...",); |
72 | } |
73 | |
a174a3c5 |
74 | aTexture->Release (theCtx); |
75 | aTexture.Nullify(); |
76 | } |
77 | myTextures.Clear(); |
78 | } |
79 | |
80 | // ======================================================================= |
81 | // function : Init |
82 | // purpose : |
83 | // ======================================================================= |
84 | bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx) |
85 | { |
86 | Release (theCtx.operator->()); |
87 | if (myFont.IsNull() || !myFont->IsValid()) |
88 | { |
89 | return false; |
90 | } |
91 | |
92 | myAscender = myFont->Ascender(); |
93 | myDescender = myFont->Descender(); |
94 | myLineSpacing = myFont->LineSpacing(); |
95 | myTileSizeX = myFont->GlyphMaxSizeX(); |
96 | myTileSizeY = myFont->GlyphMaxSizeY(); |
97 | |
98 | myLastTileId = -1; |
c6ad5e5f |
99 | if (!createTexture (theCtx)) |
100 | { |
101 | Release (theCtx.operator->()); |
102 | return false; |
103 | } |
104 | return true; |
a174a3c5 |
105 | } |
106 | |
107 | // ======================================================================= |
108 | // function : createTexture |
109 | // purpose : |
110 | // ======================================================================= |
111 | bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx) |
112 | { |
113 | const Standard_Integer aMaxSize = theCtx->MaxTextureSize(); |
114 | |
115 | Standard_Integer aGlyphsNb = myFont->GlyphsNumber() - myLastTileId + 1; |
116 | |
117 | const Standard_Integer aTextureSizeX = OpenGl_Context::GetPowerOfTwo (aGlyphsNb * myTileSizeX, aMaxSize); |
118 | const Standard_Integer aTilesPerRow = aTextureSizeX / myTileSizeX; |
119 | const Standard_Integer aTextureSizeY = OpenGl_Context::GetPowerOfTwo (GLint((aGlyphsNb / aTilesPerRow) + 1) * myTileSizeY, aMaxSize); |
120 | |
121 | memset (&myLastTilePx, 0, sizeof(myLastTilePx)); |
122 | myLastTilePx.Bottom = myTileSizeY; |
123 | |
fe3a29bc |
124 | Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams(); |
125 | aParams->SetModulate (Standard_False); |
126 | aParams->SetRepeat (Standard_False); |
127 | aParams->SetFilter (Graphic3d_TOTF_BILINEAR); |
128 | aParams->SetAnisoFilter (Graphic3d_LOTA_OFF); |
129 | |
130 | myTextures.Append (new OpenGl_Texture (aParams)); |
a174a3c5 |
131 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast(); |
132 | |
133 | Image_PixMap aBlackImg; |
076ca35c |
134 | if (!aBlackImg.InitZero (Image_PixMap::ImgAlpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY)) |
a174a3c5 |
135 | || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat |
136 | { |
cbf18624 |
137 | TCollection_ExtendedString aMsg; |
138 | aMsg += "New texture intialization of size "; |
139 | aMsg += aTextureSizeX; |
140 | aMsg += "x"; |
141 | aMsg += aTextureSizeY; |
142 | aMsg += " for textured font has failed."; |
3b523c4c |
143 | theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg); |
a174a3c5 |
144 | return false; |
145 | } |
146 | |
a174a3c5 |
147 | return true; |
148 | } |
149 | |
150 | // ======================================================================= |
151 | // function : renderGlyph |
152 | // purpose : |
153 | // ======================================================================= |
154 | bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx, |
155 | const Standard_Utf32Char theChar) |
156 | { |
157 | if (!myFont->RenderGlyph (theChar)) |
158 | { |
159 | return false; |
160 | } |
161 | |
162 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast(); |
c6ad5e5f |
163 | if (aTexture.IsNull() |
164 | || !aTexture->IsValid()) |
165 | { |
166 | return false; |
167 | } |
a174a3c5 |
168 | |
169 | const Image_PixMap& anImg = myFont->GlyphImage(); |
170 | const Standard_Integer aTileId = myLastTileId + 1; |
171 | myLastTilePx.Left = myLastTilePx.Right + 3; |
172 | myLastTilePx.Right = myLastTilePx.Left + (Standard_Integer )anImg.SizeX(); |
173 | if (myLastTilePx.Right >= aTexture->SizeX()) |
174 | { |
175 | myLastTilePx.Left = 0; |
176 | myLastTilePx.Right = (Standard_Integer )anImg.SizeX(); |
177 | myLastTilePx.Top += myTileSizeY; |
178 | myLastTilePx.Bottom += myTileSizeY; |
179 | |
180 | if (myLastTilePx.Bottom >= aTexture->SizeY()) |
181 | { |
182 | if (!createTexture (theCtx)) |
183 | { |
184 | return false; |
185 | } |
186 | return renderGlyph (theCtx, theChar); |
187 | } |
188 | } |
189 | |
190 | aTexture->Bind (theCtx); |
ca3c13d1 |
191 | #if !defined(GL_ES_VERSION_2_0) |
a174a3c5 |
192 | glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE); |
193 | glPixelStorei (GL_UNPACK_ROW_LENGTH, 0); |
ca3c13d1 |
194 | #endif |
a174a3c5 |
195 | glPixelStorei (GL_UNPACK_ALIGNMENT, 1); |
196 | |
197 | glTexSubImage2D (GL_TEXTURE_2D, 0, |
198 | myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(), |
c6ad5e5f |
199 | aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data()); |
a174a3c5 |
200 | |
201 | OpenGl_Font::Tile aTile; |
202 | aTile.uv.Left = GLfloat(myLastTilePx.Left) / GLfloat(aTexture->SizeX()); |
203 | aTile.uv.Right = GLfloat(myLastTilePx.Right) / GLfloat(aTexture->SizeX()); |
204 | aTile.uv.Top = GLfloat(myLastTilePx.Top) / GLfloat(aTexture->SizeY()); |
205 | aTile.uv.Bottom = GLfloat(myLastTilePx.Top + anImg.SizeY()) / GLfloat(aTexture->SizeY()); |
206 | aTile.texture = aTexture->TextureId(); |
207 | myFont->GlyphRect (aTile.px); |
208 | |
209 | myLastTileId = aTileId; |
210 | myTiles.Append (aTile); |
211 | return true; |
212 | } |
213 | |
214 | // ======================================================================= |
215 | // function : RenderGlyph |
216 | // purpose : |
217 | // ======================================================================= |
317d68c9 |
218 | bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx, |
a174a3c5 |
219 | const Standard_Utf32Char theUChar, |
317d68c9 |
220 | Tile& theGlyph) |
a174a3c5 |
221 | { |
222 | Standard_Integer aTileId = 0; |
317d68c9 |
223 | if (!myGlyphMap.Find (theUChar,aTileId)) |
a174a3c5 |
224 | { |
225 | if (renderGlyph (theCtx, theUChar)) |
226 | { |
227 | aTileId = myLastTileId; |
228 | } |
229 | else |
230 | { |
317d68c9 |
231 | return false; |
a174a3c5 |
232 | } |
317d68c9 |
233 | |
a174a3c5 |
234 | myGlyphMap.Bind (theUChar, aTileId); |
235 | } |
236 | |
237 | const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId); |
317d68c9 |
238 | theGlyph.px = aTile.px; |
239 | theGlyph.uv = aTile.uv; |
240 | theGlyph.texture = aTile.texture; |
241 | |
242 | return true; |
a174a3c5 |
243 | } |