Debian Codegraph @

3rdparty/fonts/fntTXF.cxx

raw symbols SCIP
1 /*
2 PLIB - A Suite of Portable Game Libraries
3 Copyright (C) 1998,2002 Steve Baker
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 For further information visit http://plib.sourceforge.net
20
21 $Id: fntTXF.cxx 1735 2002-12-01 18:21:48Z sjbaker $
22 */
23
24
25 #include "fntLocal.h"
26
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sg_path.hxx>
29
30 FILE *_fntCurrImageFd ;
31 int _fntIsSwapped = FALSE ;
32
33 static void tex_make_mip_maps ( GLubyte *image, int xsize,
34 int ysize, int zsize )
35 {
36 GLubyte *texels [ 20 ] ; /* One element per level of MIPmap */
37
38 for ( int l = 0 ; l < 20 ; l++ )
39 texels [ l ] = NULL ;
40
41 texels [ 0 ] = image ;
42
43 int lev ;
44
45 for ( lev = 0 ; (( xsize >> (lev+1) ) != 0 ||
46 ( ysize >> (lev+1) ) != 0 ) ; lev++ )
47 {
48 /* Suffix '1' is the higher level map, suffix '2' is the lower level. */
49
50 int l1 = lev ;
51 int l2 = lev+1 ;
52 int w1 = xsize >> l1 ;
53 int h1 = ysize >> l1 ;
54 int w2 = xsize >> l2 ;
55 int h2 = ysize >> l2 ;
56
57 if ( w1 <= 0 ) w1 = 1 ;
58 if ( h1 <= 0 ) h1 = 1 ;
59 if ( w2 <= 0 ) w2 = 1 ;
60 if ( h2 <= 0 ) h2 = 1 ;
61
62 texels [ l2 ] = new GLubyte [ w2 * h2 * zsize ] ;
63
64 for ( int x2 = 0 ; x2 < w2 ; x2++ )
65 for ( int y2 = 0 ; y2 < h2 ; y2++ )
66 for ( int c = 0 ; c < zsize ; c++ )
67 {
68 int x1 = x2 + x2 ;
69 int x1_1 = ( x1 + 1 ) % w1 ;
70 int y1 = y2 + y2 ;
71 int y1_1 = ( y1 + 1 ) % h1 ;
72
73 int t1 = texels [ l1 ] [ (y1 * w1 + x1 ) * zsize + c ] ;
74 int t2 = texels [ l1 ] [ (y1_1 * w1 + x1 ) * zsize + c ] ;
75 int t3 = texels [ l1 ] [ (y1 * w1 + x1_1) * zsize + c ] ;
76 int t4 = texels [ l1 ] [ (y1_1 * w1 + x1_1) * zsize + c ] ;
77
78 texels [ l2 ] [ (y2 * w2 + x2) * zsize + c ] =
79 ( t1 + t2 + t3 + t4 ) / 4 ;
80 }
81 }
82
83 texels [ lev+1 ] = NULL ;
84
85 if ( ! ((xsize & (xsize-1))==0) ||
86 ! ((ysize & (ysize-1))==0) )
87 {
88 fntSetError ( SG_ALERT,
89 "TXFloader: TXF Map is not a power-of-two in size!" ) ;
90 }
91
92 glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 ) ;
93
94 int map_level = 0 ;
95
96 #ifdef PROXY_TEXTURES_ARE_NOT_BROKEN
97 int ww ;
98
99 do
100 {
101 glTexImage2D ( GL_PROXY_TEXTURE_2D,
102 map_level, zsize, xsize, ysize, FALSE /* Border */,
103 (zsize==1)?GL_LUMINANCE:
104 (zsize==2)?GL_LUMINANCE_ALPHA:
105 (zsize==3)?GL_RGB:
106 GL_RGBA,
107 GL_UNSIGNED_BYTE, NULL ) ;
108
109 glGetTexLevelParameteriv ( GL_PROXY_TEXTURE_2D, 0,GL_TEXTURE_WIDTH, &ww ) ;
110
111 if ( ww == 0 )
112 {
113 delete [] texels [ 0 ] ;
114 xsize >>= 1 ;
115 ysize >>= 1 ;
116
117 for ( int l = 0 ; texels [ l ] != NULL ; l++ )
118 texels [ l ] = texels [ l+1 ] ;
119
120 if ( xsize < 64 && ysize < 64 )
121 {
122 fntSetError ( SG_ALERT,
123 "FNT: OpenGL will not accept a font texture?!?" ) ;
124 }
125 }
126 } while ( ww == 0 ) ;
127 #endif
128
129 for ( int i = 0 ; texels [ i ] != NULL ; i++ )
130 {
131 int w = xsize>>i ;
132 int h = ysize>>i ;
133
134 if ( w <= 0 ) w = 1 ;
135 if ( h <= 0 ) h = 1 ;
136
137 glTexImage2D ( GL_TEXTURE_2D,
138 map_level, zsize, w, h, FALSE /* Border */,
139 (zsize==1)?GL_LUMINANCE:
140 (zsize==2)?GL_LUMINANCE_ALPHA:
141 (zsize==3)?GL_RGB:
142 GL_RGBA,
143 GL_UNSIGNED_BYTE, (GLvoid *) texels[i] ) ;
144 map_level++ ;
145 delete [] texels [ i ] ;
146 }
147 }
148
149
150
151 int fntTexFont::loadTXF ( const SGPath& path, GLenum mag, GLenum min )
152 {
153 FILE *fd ;
154 const auto ps = path.utf8Str();
155 #if defined(SG_WINDOWS)
156 std::wstring fp = path.wstr();
157 fd = _wfopen(fp.c_str(), L"rb");
158 #else
159 fd = fopen(ps.c_str(), "rb");
160 #endif
161 if ( fd == nullptr )
162 {
163 fntSetError ( SG_WARN,
164 "fntLoadTXF: Failed to open '%s' for reading.", ps.c_str() ) ;
165 return FNT_FALSE ;
166 }
167
168 _fntCurrImageFd = fd ;
169
170 unsigned char magic [ 4 ] ;
171
172 if ( (int)fread ( &magic, sizeof (unsigned int), 1, fd ) != 1 )
173 {
174 fntSetError ( SG_WARN,
175 "fntLoadTXF: '%s' an empty file!", ps.c_str() ) ;
176 return FNT_FALSE ;
177 }
178
179 if ( magic [ 0 ] != 0xFF || magic [ 1 ] != 't' ||
180 magic [ 2 ] != 'x' || magic [ 3 ] != 'f' )
181 {
182 fntSetError ( SG_WARN,
183 "fntLoadTXF: '%s' is not a 'txf' font file.", ps.c_str() ) ;
184 return FNT_FALSE ;
185 }
186
187 _fntIsSwapped = FALSE ;
188 int endianness = _fnt_readInt () ;
189
190 _fntIsSwapped = ( endianness != 0x12345678 ) ;
191
192 int format = _fnt_readInt () ;
193 int tex_width = _fnt_readInt () ;
194 int tex_height = _fnt_readInt () ;
195 int max_height = _fnt_readInt () ;
196 _fnt_readInt () ;
197 int num_glyphs = _fnt_readInt () ;
198
199 int w = tex_width ;
200 int h = tex_height ;
201
202 float xstep = 0.5f / (float) w ;
203 float ystep = 0.5f / (float) h ;
204
205 int i, j ;
206
207 /*
208 Load the TXF_Glyph array
209 */
210
211 TXF_Glyph glyph ;
212
213 for ( i = 0 ; i < num_glyphs ; i++ )
214 {
215 glyph . ch = _fnt_readShort () ;
216
217 glyph . w = _fnt_readByte () ;
218 glyph . h = _fnt_readByte () ;
219 glyph . x_off = _fnt_readByte () ;
220 glyph . y_off = _fnt_readByte () ;
221 glyph . step = _fnt_readByte () ;
222 glyph . unknown = _fnt_readByte () ;
223 glyph . x = _fnt_readShort () ;
224 glyph . y = _fnt_readShort () ;
225
226 setGlyph ( (char) glyph.ch,
227 (float) glyph.step / (float) max_height,
228 (float) glyph.x / (float) w + xstep,
229 (float)( glyph.x + glyph.w ) / (float) w + xstep,
230 (float) glyph.y / (float) h + ystep,
231 (float)( glyph.y + glyph.h ) / (float) h + ystep,
232 (float) glyph.x_off / (float) max_height,
233 (float)( glyph.x_off + glyph.w ) / (float) max_height,
234 (float) glyph.y_off / (float) max_height,
235 (float)( glyph.y_off + glyph.h ) / (float) max_height ) ;
236 }
237
238 exists [ static_cast<int>(' ') ] = FALSE ;
239
240 /*
241 Load the image part of the file
242 */
243
244 int ntexels = w * h ;
245
246 unsigned char *teximage ;
247 unsigned char *texbitmap ;
248
249 switch ( format )
250 {
251 case FNT_BYTE_FORMAT:
252 {
253 unsigned char *orig = new unsigned char [ ntexels ] ;
254
255 if ( (int)fread ( orig, 1, ntexels, fd ) != ntexels )
256 {
257 fntSetError ( SG_WARN,
258 "fntLoadTXF: Premature EOF in '%s'.", ps.c_str() ) ;
259 return FNT_FALSE ;
260 }
261
262 teximage = new unsigned char [ 2 * ntexels ] ;
263
264 for ( i = 0 ; i < ntexels ; i++ )
265 {
266 teximage [ i*2 ] = orig [ i ] ;
267 teximage [ i*2 + 1 ] = orig [ i ] ;
268 }
269
270 delete [] orig ;
271 }
272 break ;
273
274 case FNT_BITMAP_FORMAT:
275 {
276 int stride = (w + 7) >> 3;
277
278 texbitmap = new unsigned char [ stride * h ] ;
279
280 if ( (int)fread ( texbitmap, 1, stride * h, fd ) != stride * h )
281 {
282 delete [] texbitmap ;
283 fntSetError ( SG_WARN,
284 "fntLoadTXF: Premature EOF in '%s'.", ps.c_str() ) ;
285 return FNT_FALSE ;
286 }
287
288 teximage = new unsigned char [ 2 * ntexels ] ;
289
290 for ( i = 0 ; i < 2 * ntexels ; i++ )
291 teximage [ i ] = 0 ;
292
293 for (i = 0; i < h; i++)
294 for (j = 0; j < w; j++)
295 if (texbitmap[i * stride + (j >> 3)] & (1 << (j & 7)))
296 {
297 teximage[(i * w + j) * 2 ] = 255;
298 teximage[(i * w + j) * 2 + 1] = 255;
299 }
300
301 delete [] texbitmap ;
302 }
303 break ;
304
305 default:
306 fntSetError ( SG_WARN,
307 "fntLoadTXF: Unrecognised format type in '%s'.", ps.c_str() ) ;
308 return FNT_FALSE ;
309 }
310
311 fclose ( fd ) ;
312
313 fixed_pitch = FALSE ;
314
315 #ifdef GL_VERSION_1_1
316 glGenTextures ( 1, & texture ) ;
317 glBindTexture ( GL_TEXTURE_2D, texture ) ;
318 #else
319 /* This is only useful on some ancient SGI hardware */
320 glGenTexturesEXT ( 1, & texture ) ;
321 glBindTextureEXT ( GL_TEXTURE_2D, texture ) ;
322 #endif
323
324 tex_make_mip_maps ( teximage, w, h, 2 ) ;
325
326 glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
327
328 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag ) ;
329 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min ) ;
330 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) ;
331 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ) ;
332 #ifdef GL_VERSION_1_1
333 glBindTexture ( GL_TEXTURE_2D, 0 ) ;
334 #else
335 glBindTextureEXT ( GL_TEXTURE_2D, 0 ) ;
336 #endif
337
338 return FNT_TRUE ;
339 }
340
341