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