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