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