]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/font.cpp
Initial revision
[taylor/freespace2.git] / src / graphics / font.cpp
1 /*
2  * $Logfile: /Freespace2/code/Graphics/Font.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * source file for font stuff
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:09  root
11  * Initial revision
12  *
13  * 
14  * 9     7/09/99 10:32p Dave
15  * Command brief and red alert screens.
16  * 
17  * 8     12/14/98 9:21a Dan
18  * Put gr8_string() back in
19  * 
20  * 7     12/02/98 5:47p Dave
21  * Put in interface xstr code. Converted barracks screen to new format.
22  * 
23  * 6     11/30/98 1:07p Dave
24  * 16 bit conversion, first run.
25  * 
26  * 5     11/05/98 4:18p Dave
27  * First run nebula support. Beefed up localization a bit. Removed all
28  * conditional compiles for foreign versions. Modified mission file
29  * format.
30  * 
31  * 4     10/13/98 9:28a Dave
32  * Started neatening up freespace.h. Many variables renamed and
33  * reorganized. Added AlphaColors.[h,cpp]
34  * 
35  * 3     10/13/98 9:19a Andsager
36  * Add localization support to cfile.  Optional parameter with cfopen that
37  * looks for localized files.
38  * 
39  * 2     10/07/98 10:52a Dave
40  * Initial checkin.
41  * 
42  * 1     10/07/98 10:49a Dave
43  * 
44  * 49    7/02/98 3:41p Allender
45  * fix nasty assembler bug where pushf wasn't accompanied by corresponding
46  * pop in certain clipping conditions -- manifested only in software and
47  * usually on the PXO chat screen.
48  * 
49  * 48    6/18/98 10:10a Allender
50  * fixed compiler warnings
51  * 
52  * 47    6/13/98 10:48p Lawrance
53  * Changed code to utilize proper fixed-space 1 character.
54  * 
55  * 46    6/13/98 3:18p Hoffoss
56  * NOX()ed out a bunch of strings that shouldn't be translated.
57  * 
58  * 45    5/25/98 10:32a John
59  * Took out redundant code for font bitmap offsets that converted to a
60  * float, then later on converted back to an integer.  Quite unnecessary.
61  * 
62  * 44    5/12/98 11:45a John
63  * Fixed a bug with the length of string being passed to
64  * gr_get_string_size.  The bug occurred if you had two or more'\n'''s in
65  * a row.
66  * 
67  * 43    3/10/98 4:18p John
68  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
69  * & Glide have popups and print screen.  Took out all >8bpp software
70  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
71  * support Fred.  Made zbuffering key off of functions rather than one
72  * global variable.
73  * 
74  * 42    3/09/98 6:06p John
75  * Restructured font stuff to avoid duplicate code in Direct3D and Glide.
76  * Restructured Glide to avoid redundent state setting.
77  * 
78  * 41    3/02/98 10:33a John
79  * Fixed bug where Y clip was using X value.
80  * 
81  * 40    2/19/98 9:04a John
82  * Fixed fonts with glide
83  * 
84  * 39    2/17/98 7:27p John
85  * Got fonts and texturing working in Direct3D
86  * 
87  * 38    2/05/98 9:21p John
88  * Some new Direct3D code.   Added code to monitor a ton of stuff in the
89  * game.
90  * 
91  * 37    1/20/98 2:25p Hoffoss
92  * Fixed bug where timestamp printing incorrectly increments an hour at 6
93  * minutes.
94  * 
95  * 36    1/19/98 6:15p John
96  * Fixed all my Optimized Build compiler warnings
97  * 
98  * 35    11/30/97 12:18p John
99  * added more 24 & 32-bpp primitives
100  * 
101  * 34    11/29/97 2:24p John
102  * fixed warnings
103  * 
104  * 33    11/29/97 2:06p John
105  * added mode 16-bpp support
106  * 
107  * 32    11/10/97 2:35p Hoffoss
108  * Changed timestamp printing code to utilize new fixed-space '1'
109  * character.
110  * 
111  * 31    11/06/97 5:42p Hoffoss
112  * Added support for fixed size timstamp rendering.
113  * 
114  * 30    11/03/97 10:08p Hoffoss
115  * Changed gr_get_string_size to utilize an optional length specifier, if
116  * you want to use non-null terminated strings.
117  * 
118  * 29    11/03/97 10:59a John
119  * added support for more than one font.
120  * 
121  * 28    10/24/97 12:13p Hoffoss
122  * Added gr_force_fit_string().
123  * 
124  * 27    10/19/97 12:55p John
125  * new code to lock / unlock surfaces for smooth directx integration.
126  * 
127  * 26    10/14/97 4:50p John
128  * more 16 bpp stuff.
129  * 
130  * 25    10/14/97 8:08a John
131  * added a bunch more 16 bit support
132  * 
133  * 24    10/09/97 5:23p John
134  * Added support for more 16-bpp functions
135  * 
136  * 23    9/23/97 10:53a Hoffoss
137  * Fixed bug in assumptions made half the time, and not the other half.
138  * 
139  * 22    9/09/97 3:39p Sandeep
140  * warning level 4 bugs
141  * 
142  * 21    8/19/97 5:46p Hoffoss
143  * Changed font used in Fred, and added display to show current eye
144  * position.
145  * 
146  * 20    6/25/97 2:35p John
147  * added some functions to use the windows font for Fred.
148  * 
149  * 19    6/17/97 12:03p John
150  * Moved color/alphacolor functions into their own module.  Made all color
151  * functions be part of the low-level graphics drivers, not just the
152  * grsoft.
153  * 
154  * 18    6/13/97 2:37p John
155  * fixed a string bug that printed weird characters sometimes.
156  * 
157  * 17    6/11/97 6:00p John
158  * sped up alpha matching a bit.
159  * 
160  * 16    6/11/97 5:49p John
161  * Changed palette code to only recalculate alphacolors when needed, not
162  * when palette changes.
163  * 
164  * 15    6/11/97 5:01p John
165  * Fixed mission log text.  Added fast clipped font drawer.  
166  * 
167  * 14    6/11/97 4:11p John
168  * addec function to get font height
169  * 
170  * 13    6/11/97 1:12p John
171  * Started fixing all the text colors in the game.
172  * 
173  * 12    6/09/97 9:24a John
174  * Changed the way fonts are set.
175  * 
176  * 11    6/06/97 4:41p John
177  * Fixed alpha colors to be smoothly integrated into gr_set_color_fast
178  * code.
179  * 
180  * 10    6/06/97 2:40p John
181  * Made all the radar dim in/out
182  * 
183  * 9     6/05/97 4:53p John
184  * First rev of new antialiased font stuff.
185  * 
186  * 8     5/12/97 12:27p John
187  * Restructured Graphics Library to add support for multiple renderers.
188  * 
189  * 7     4/24/97 11:28a Hoffoss
190  * added code to handle gr_String being called before gr_init_font ever
191  * was called
192  * 
193  * 6     4/23/97 5:26p John
194  * First rev of new debug console stuff.
195  * 
196  * 5     4/22/97 12:20p John
197  * fixed more resource leaks
198  * 
199  * 4     4/22/97 10:33a John
200  * fixed the 2d resource leaks that Alan found.
201  * 
202  * 3     4/04/97 11:21a Hoffoss
203  * JOHN: Fixed invalid param that caused a trap in BoundsChecker.    
204  * 
205  * 2     4/01/97 9:26a Allender
206  * added support for descent style fonts although they are not used in the
207  * game yet
208  * 
209  * 1     3/31/97 9:42a Allender
210  *
211  * $NoKeywords: $
212  */
213
214 #ifndef PLAT_UNIX
215 #include <windows.h>
216 #include <windowsx.h>
217 #endif
218 #include <stdio.h>
219 #include <stdarg.h>
220 #include "grinternal.h"
221 #include "2d.h"
222 #include "cfile.h"
223 #include "font.h"
224 #include "palman.h"
225 #include "key.h"
226 #include "bmpman.h"
227 #include "localize.h"
228
229 int Num_fonts = 0;
230 font Fonts[MAX_FONTS];
231 font *Current_font = NULL;
232
233
234 // crops a string if required to force it to not exceed max_width pixels when printed.
235 // Does this by dropping characters at the end of the string and adding '...' to the end.
236 //    str = string to crop.  Modifies this string directly
237 //    max_str = max characters allowed in str
238 //    max_width = number of pixels to limit string to (less than or equal to).
239 //    Returns: returns same pointer passed in for str.
240 char *gr_force_fit_string(char *str, int max_str, int max_width)
241 {
242         int w;
243
244         gr_get_string_size(&w, NULL, str);
245         if (w > max_width) {
246                 if ((int) strlen(str) > max_str - 3) {
247                         Assert(max_str >= 3);
248                         str[max_str - 3] = 0;
249                 }
250
251                 strcpy(str + strlen(str) - 1, "...");
252                 gr_get_string_size(&w, NULL, str);
253                 while (w > max_width) {
254                         Assert(strlen(str) >= 4);  // if this is hit, a bad max_width was passed in and the calling function needs fixing.
255                         strcpy(str + strlen(str) - 4, "...");
256                         gr_get_string_size(&w, NULL, str);
257                 }
258         }
259
260         return str;
261 }
262
263 //takes the character BEFORE being offset into current font
264 // Returns the letter code
265 int get_char_width(ubyte c1,ubyte c2,int *width,int *spacing)
266 {
267         int i, letter;
268
269         Assert ( Current_font != NULL );
270         letter = c1-Current_font->first_ascii;
271
272         if (letter<0 || letter>=Current_font->num_chars) {                              //not in font, draw as space
273                 *width=0;
274                 *spacing = Current_font->w;
275                 return -1;
276         }
277
278         *width = Current_font->char_data[letter].byte_width;
279         *spacing = Current_font->char_data[letter].spacing;
280
281         i = Current_font->char_data[letter].kerning_entry;
282         if ( i > -1)  {
283                 if (!(c2==0 || c2=='\n')) {
284                         int letter2;
285
286                         letter2 = c2-Current_font->first_ascii;
287
288                         if ((letter2>=0) && (letter2<Current_font->num_chars) ) {                               //not in font, draw as space
289                                 font_kernpair   *k = &Current_font->kern_data[i];
290                                 while( (k->c1 == letter) && (k->c2<letter2) && (i<Current_font->num_kern_pairs) )       {
291                                         i++;
292                                         k++;
293                                 }
294                                 if ( k->c2 == letter2 ) {
295                                         *spacing += k->offset;
296                                 }
297                         }
298                 }
299         }
300         return letter;
301 }
302
303 int get_centered_x(char *s)
304 {
305         int w,w2,s2;
306
307         for (w=0;*s!=0 && *s!='\n';s++) {
308                 get_char_width(s[0],s[1],&w2,&s2);
309                 w += s2;
310         }
311
312         return ((gr_screen.clip_width - w) / 2);
313 }
314
315 // draws a character centered on x
316 void gr_char_centered(int x, int y, char chr)
317 {
318         char str[2];
319         int w, sc;
320
321         sc = Lcl_special_chars;
322         if (chr == '1')
323                 chr = (char)(sc + 1);
324
325         str[0] = chr;
326         str[1] = 0;
327         gr_get_string_size(&w, NULL, str);
328         gr_string(x - w / 2, y, str);
329 }
330
331 void gr_print_timestamp(int x, int y, int timestamp)
332 {
333         char h[2], m[3], s[3];
334         int w, c;
335
336         // format the time information into strings
337         sprintf(h, "%0.1d", (timestamp / 3600000) % 10);
338         sprintf(m, "%0.2d", (timestamp / 60000) % 60);
339         sprintf(s, "%0.2d", (timestamp / 1000) % 60);
340
341         gr_get_string_size(&w, NULL, "0");
342         gr_get_string_size(&c, NULL, ":");
343
344         gr_string(x + w, y, ":");
345         gr_string(x + w * 3 + c, y, ":");
346
347         x += w / 2;
348         gr_char_centered(x, y, h[0]);
349         x += w + c;
350         gr_char_centered(x, y, m[0]);
351         x += w;
352         gr_char_centered(x, y, m[1]);
353         x += w + c;
354         gr_char_centered(x, y, s[0]);
355         x += w;
356         gr_char_centered(x, y, s[1]);
357 }
358
359 int gr_get_font_height()
360 {
361         if (Current_font)       {
362                 return Current_font->h;
363         } else {
364                 return 16;
365         }
366 }
367
368 void gr_get_string_size(int *w1, int *h1, char *text, int len)
369 {
370         int longest_width;
371         int width,spacing;
372         int w, h;
373
374         if (!Current_font)      {
375                 if ( w1)
376                         *w1 = 16;
377
378                 if ( h1 )
379                         *h1 = 16;
380
381                 return;
382         }       
383
384         w = 0;
385         h = 0;
386         longest_width = 0;
387
388         if (text != NULL ) {
389                 h += Current_font->h;
390                 while (*text && (len>0) ) {
391
392                         // Process one or more 
393                         while ((*text == '\n') && (len>0) ) {
394                                 text++;
395                                 len--;
396                                 if ( *text )    {
397                                         h += Current_font->h;
398                                 }
399                                 w = 0;
400                         }
401
402                         if (*text == 0) {
403                                 break;
404                         }
405
406                         get_char_width(text[0], text[1], &width, &spacing);
407                         w += spacing;
408                         if (w > longest_width)
409                                 longest_width = w;
410
411                         text++;
412                         len--;
413                 }
414         }
415
416         if ( h1 )
417                 *h1 = h;
418
419         if ( w1 )
420                 *w1 = longest_width;
421 }
422
423
424 MONITOR( FontChars );   
425
426
427 /*
428
429 void gr8_char(int x,int y,int letter)
430 {
431         font_char *ch;
432         
433         ch = &Current_font->char_data[letter];
434
435         gr_aabitmap_ex( x, y, ch->byte_width, Current_font->h, Current_font->u[letter], Current_font->v[letter] );
436
437 //      mprintf(( "String = %s\n", text ));
438 }
439
440
441 void gr8_string( int sx, int sy, char *s )
442 {
443         int width, spacing, letter;
444         int x, y;
445
446         if ( !Current_font ) return;
447         if ( !s ) return;
448         
449         gr_set_bitmap(Current_font->bitmap);
450
451         x = sx;
452         y = sy;
453
454         if (sx==0x8000) {                       //centered
455                 x = get_centered_x(s);
456         } else {
457                 x = sx;
458         }
459
460         while (*s)      {
461                 while (*s== '\n' )      {
462                         s++;
463                         y += Current_font->h;
464                         if (sx==0x8000) {                       //centered
465                                 x = get_centered_x(s);
466                         } else {
467                                 x = sx;
468                         }
469                 }
470                 if (*s == 0 ) break;
471
472                 letter = get_char_width(s[0],s[1],&width,&spacing);
473
474                 if (letter<0) { //not in font, draw as space
475                         x += spacing;
476                         s++;
477                         continue;
478                 }
479                 gr8_char( x, y, letter );
480         
481                 x += spacing;
482                 s++;
483         }
484 }
485 */
486
487 void gr8_string(int sx, int sy, char *s )
488 {
489         int row,width, spacing, letter;
490         int x, y;
491
492         if ( !Current_font ) return;
493         if ( !s ) return;
494
495         x = sx;
496         y = sy;
497
498         if (sx==0x8000) {                       //centered
499                 x = get_centered_x(s);
500         } else {
501                 x = sx;
502         }
503
504         spacing = 0;
505
506         gr_lock();
507
508
509         while (*s)      {
510                 MONITOR_INC( FontChars, 1 );    
511
512                 x += spacing;
513                 while (*s== '\n' )      {
514                         s++;
515                         y += Current_font->h;
516                         if (sx==0x8000) {                       //centered
517                                 x = get_centered_x(s);
518                         } else {
519                                 x = sx;
520                         }
521                 }
522                 if (*s == 0 ) break;
523
524                 letter = get_char_width(s[0],s[1],&width,&spacing);
525                 s++;
526
527                 //If not in font, draw as space
528                 if (letter<0) continue;
529
530                 int xd, yd, xc, yc;
531                 int wc, hc;
532
533                 // Check if this character is totally clipped
534                 if ( x + width < gr_screen.clip_left ) continue;
535                 if ( y + Current_font->h < gr_screen.clip_top ) continue;
536                 if ( x > gr_screen.clip_right ) continue;
537                 if ( y > gr_screen.clip_bottom ) continue;
538
539                 xd = yd = 0;
540                 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
541                 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
542                 xc = x+xd;
543                 yc = y+yd;
544
545                 wc = width - xd; hc = Current_font->h - yd;
546                 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
547                 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
548
549                 if ( wc < 1 ) continue;
550                 if ( hc < 1 ) continue;
551
552                 ubyte *fp = Current_font->pixel_data + Current_font->char_data[letter].offset + xd + yd*width;
553                 ubyte *dptr = GR_SCREEN_PTR(ubyte, xc, yc);                     
554
555 #ifndef HARDWARE_ONLY
556                 if ( Current_alphacolor )       {
557                         for (row=0; row<hc; row++)      {
558                                 #ifdef USE_INLINE_ASM
559                                         ubyte *lookup = &Current_alphacolor->table.lookup[0][0];
560                                                 _asm mov edx, lookup
561                                                 _asm xor eax, eax
562                                                 _asm mov ecx, wc
563                                                 _asm xor ebx, ebx
564                                                 _asm mov edi, dptr
565                                                 _asm mov esi, fp
566                                                 _asm shr ecx, 1
567                                                 _asm jz  OnlyOne
568                                                 _asm pushf
569                                         InnerFontLoop:
570                                                 _asm mov al, [edi]
571                                                 _asm mov bl, [edi+1]
572                                                 _asm add edi,2
573
574                                                 _asm mov ah, [esi]
575                                                 _asm mov bh, [esi+1]
576                                                 _asm add esi,2
577
578                                                 _asm mov al, [edx+eax]
579                                                 _asm mov ah, [edx+ebx]
580
581                                                 _asm mov [edi-2], ax
582
583                                                 _asm dec ecx
584                                                 _asm jnz InnerFontLoop
585
586                                                 _asm popf
587                                                 _asm jnc NotOdd
588
589                                         OnlyOne:
590                                                 _asm mov al, [edi]
591                                                 _asm mov ah, [esi]
592                                                 _asm mov al, [edx+eax]
593                                                 _asm mov [edi], al
594
595                                         NotOdd:
596                                         dptr += gr_screen.rowsize;
597                                         fp += width;
598                                 #else
599                                         int i;
600                                         for (i=0; i< wc; i++ )  {
601                                                 *dptr++ = Current_alphacolor->table.lookup[*fp++][*dptr];
602                                         }
603                                         fp += width - wc;
604                                         dptr += gr_screen.rowsize - wc;
605                                 #endif
606                         }
607                 } else {                // No alpha color
608 #endif
609                         for (row=0; row<hc; row++)      {
610                                 int i;
611                                 for (i=0; i< wc; i++ )  {
612                                         if (*fp > 5 )
613                                                 *dptr = gr_screen.current_color.raw8;
614                                         dptr++;
615                                         fp++;
616                                 }
617                                 fp += width - wc;
618                                 dptr += gr_screen.rowsize - wc;
619                         }
620                 // }
621         }
622         gr_unlock();
623 }
624
625 #ifndef PLAT_UNIX
626 HFONT MyhFont = NULL;
627 extern HDC hDibDC;
628 #endif
629
630 void gr_string_win(int x, int y, char *s)
631 {
632 #ifdef PLAT_UNIX
633         STUB_FUNCTION;
634 #else
635         char *ptr;
636         SIZE size;
637
638         if ( MyhFont==NULL )    {
639                 MyhFont = CreateFont(14, 0, 0, 0,                               // height,width,?,?
640                                 700,
641                                 FALSE,
642                                 FALSE,
643                                 FALSE,                                                                                  // strikeout?
644                                 ANSI_CHARSET,                                                                   // character set
645                                 OUT_DEVICE_PRECIS,
646                                 CLIP_DEFAULT_PRECIS,
647                                 DEFAULT_QUALITY,
648                                 DEFAULT_PITCH | FF_DONTCARE,
649 //                              NULL );
650 //                              "Times New Roman" );
651 //XSTR:OFF
652                                 "Ariel" );
653 //XSTR:ON
654         }
655
656         SelectObject( hDibDC, MyhFont );
657
658         if ( gr_screen.bits_per_pixel==8 )
659                 SetTextColor(hDibDC, PALETTEINDEX(gr_screen.current_color.raw8));
660         else
661                 SetTextColor(hDibDC, RGB(gr_screen.current_color.red,gr_screen.current_color.green,gr_screen.current_color.blue));
662
663         SetBkMode(hDibDC,TRANSPARENT);
664
665
666         HRGN hclip;
667         hclip = CreateRectRgn( gr_screen.offset_x, 
668                                                                   gr_screen.offset_y, 
669                                                                   gr_screen.offset_x+gr_screen.clip_width-1, 
670                                                                   gr_screen.offset_y+gr_screen.clip_height-1 );
671
672         SelectClipRgn(hDibDC, hclip );
673         x += gr_screen.offset_x;
674         y += gr_screen.offset_y;
675         //ptr = strchr(s,'\n);
676         while ((ptr = strchr(s, '\n'))!=NULL) {
677                 TextOut(hDibDC, x, y, s, ptr - s);
678                 GetTextExtentPoint32(hDibDC, s, ptr - s, &size);
679                 y += size.cy;
680                 s = ptr + 1;
681         }
682
683         TextOut(hDibDC, x, y, s, strlen(s));
684         SelectClipRgn(hDibDC, NULL);
685         DeleteObject(hclip);
686 #endif
687 }
688
689 void gr_get_string_size_win(int *w, int *h, char *text)
690 {
691 #ifdef PLAT_UNIX
692         STUB_FUNCTION;
693 #else
694         char *ptr;
695         SIZE size;
696
697         ptr = strchr(text, '\n');
698
699         if (MyhFont==NULL)      {
700                 if (w) *w = 0;
701                 if (h) *h = 0;
702                 return;
703         }
704
705         SelectObject( hDibDC, MyhFont );
706
707         if (!ptr)       {
708                 GetTextExtentPoint32( hDibDC, text, strlen(text), &size);
709                 if (w) *w = size.cx;
710                 if (h) *h = size.cy;
711                 return;
712         }
713
714         GetTextExtentPoint32(hDibDC, text, ptr - text, &size);
715         gr_get_string_size_win(w, h, ptr+1);
716         if (w && (size.cx > *w) )
717                 *w = size.cx;
718
719         if (h)
720                 *h += size.cy;
721 #endif
722 }
723
724 char grx_printf_text[2048];     
725
726 void _cdecl gr_printf( int x, int y, char * format, ... )
727 {
728         va_list args;
729
730         if ( !Current_font ) return;
731         
732         va_start(args, format);
733         vsprintf(grx_printf_text,format,args);
734         va_end(args);
735
736         gr_string(x,y,grx_printf_text);
737 }
738
739 void gr_font_close()
740 {
741
742 }
743
744 // Returns -1 if couldn't init font, otherwise returns the
745 // font id number.
746 int gr_create_font(char * typeface)
747 {
748         CFILE *fp;
749         font *fnt;
750         int n, fontnum;
751
752         fnt = Fonts;
753         n = -1;
754         for (fontnum=0; fontnum<Num_fonts; fontnum++ )  {
755                 if (fnt->id != 0 )      {
756                         if ( !_strnicmp( fnt->filename, typeface, MAX_FILENAME_LEN ) )  {
757                                 return fontnum;
758                         }
759                 } else {
760                         if ( n < 0 )    {
761                                 n = fontnum;
762                         }                               
763                 }
764                 fnt++;
765         }
766
767         if ( fontnum==MAX_FONTS )       {
768                 Error( LOCATION, "Too many fonts!\nSee John, or change MAX_FONTS in Graphics\\Font.h\n" );
769         }
770
771         if ( fontnum == Num_fonts )     {
772                 Num_fonts++;
773         }
774         
775         bool localize = true;
776
777         fp = cfopen( typeface, "rb", CFILE_NORMAL, CF_TYPE_ANY, localize );
778         if ( fp == NULL ) return -1;
779
780         strncpy( fnt->filename, typeface, MAX_FILENAME_LEN );
781         cfread( &fnt->id, 4, 1, fp );
782         cfread( &fnt->version, sizeof(int), 1, fp );
783         cfread( &fnt->num_chars, sizeof(int), 1, fp );
784         cfread( &fnt->first_ascii, sizeof(int), 1, fp );
785         cfread( &fnt->w, sizeof(int), 1, fp );
786         cfread( &fnt->h, sizeof(int), 1, fp );
787         cfread( &fnt->num_kern_pairs, sizeof(int), 1, fp );
788         cfread( &fnt->kern_data_size, sizeof(int), 1, fp );
789         cfread( &fnt->char_data_size, sizeof(int), 1, fp );
790         cfread( &fnt->pixel_data_size, sizeof(int), 1, fp );
791
792         if ( fnt->kern_data_size )      {
793                 fnt->kern_data = (font_kernpair *)malloc( fnt->kern_data_size );
794                 Assert(fnt->kern_data!=NULL);
795                 cfread( fnt->kern_data, fnt->kern_data_size, 1, fp );
796         } else {
797                 fnt->kern_data = NULL;
798         }
799         if ( fnt->char_data_size )      {
800                 fnt->char_data = (font_char *)malloc( fnt->char_data_size );
801                 Assert( fnt->char_data != NULL );
802                 cfread( fnt->char_data, fnt->char_data_size, 1, fp );
803         } else {
804                 fnt->char_data = NULL;
805         }
806         if ( fnt->pixel_data_size )     {
807                 fnt->pixel_data = (ubyte *)malloc( fnt->pixel_data_size );
808                 Assert(fnt->pixel_data!=NULL);
809                 cfread( fnt->pixel_data, fnt->pixel_data_size, 1, fp );
810         } else {
811                 fnt->pixel_data = NULL;
812         }
813         cfclose(fp);
814
815         // Create a bitmap for hardware cards.
816         // JAS:  Try to squeeze this into the smallest square power of two texture.
817         // This should probably be done at font generation time, not here.
818         int w, h;
819         if ( fnt->pixel_data_size < 64*64 )     {
820                 w = h = 64;
821         } else if ( fnt->pixel_data_size < 128*128 )    {
822                 w = h = 128;
823         } else {
824                 w = h = 256;
825         }
826
827         fnt->bm_w = w;
828         fnt->bm_h = h;
829         fnt->bm_data = (ubyte *)malloc(fnt->bm_w*fnt->bm_h);
830         fnt->bm_u = (int *)malloc(sizeof(int)*fnt->num_chars);
831         fnt->bm_v = (int *)malloc(sizeof(int)*fnt->num_chars);
832
833         int i,x,y;
834         x = y = 0;
835         for (i=0; i<fnt->num_chars; i++ )       {
836                 ubyte * fp;
837                 int x1, y1;
838                 fp = &fnt->pixel_data[fnt->char_data[i].offset];
839                 if ( x + fnt->char_data[i].byte_width >= fnt->bm_w )    {
840                         x = 0;
841                         y += fnt->h;
842                         if ( y+fnt->h > fnt->bm_h ) {
843                                 Error( LOCATION, "Font too big!\n" );
844                         }
845                 }
846                 fnt->bm_u[i] = x;
847                 fnt->bm_v[i] = y;
848
849                 for( y1=0; y1<fnt->h; y1++ )    {
850                         for (x1=0; x1<fnt->char_data[i].byte_width; x1++ )      {
851                                 uint c = *fp++;
852                                 if ( c > 14 ) c = 14;
853                                 fnt->bm_data[(x+x1)+(y+y1)*fnt->bm_w] = (unsigned char)(c);     
854                         }
855                 }
856                 x += fnt->char_data[i].byte_width;
857         }
858
859         fnt->bitmap_id = bm_create( 8, fnt->bm_w, fnt->bm_h, fnt->bm_data, BMP_AABITMAP );
860
861         return fontnum;
862 }
863
864 void grx_set_font(int fontnum)
865 {
866         if ( fontnum < 0 ) {
867                 Current_font = NULL;
868                 return;
869         }
870
871         if ( fontnum >= 0 ) {
872                 Current_font = &Fonts[fontnum];
873         }
874 }
875
876 void gr_font_init()
877 {
878         gr_init_font( NOX("font01.vf") );
879         gr_init_font( NOX("font02.vf") );
880         gr_init_font( NOX("font03.vf") );
881 }
882
883 // Returns -1 if couldn't init font, otherwise returns the
884 // font id number.
885 int gr_init_font(char * typeface)
886 {
887         int Loaded_fontnum;
888
889         Loaded_fontnum = gr_create_font(typeface);
890
891         Assert( Loaded_fontnum > -1 );
892
893         gr_set_font( Loaded_fontnum );
894
895         return Loaded_fontnum;
896 }