]> icculus.org git repositories - btb/d2x.git/blob - 2d/font.c
Use both ints with the robot producer flags.
[btb/d2x.git] / 2d / font.c
1 /* $Id: font.c,v 1.35 2004-12-17 13:42:11 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Graphical routines for drawing fonts.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #if !defined(_MSC_VER) && !defined(macintosh)
31 #include <fcntl.h>
32 #include <unistd.h>
33 #endif
34
35 #include "pa_enabl.h"                   //$$POLY_ACC
36 #include "u_mem.h"
37
38 #include "gr.h"
39 #include "grdef.h"
40 #include "error.h"
41
42 #include "cfile.h"
43 #include "mono.h"
44 #include "byteswap.h"
45 #include "bitmap.h"
46
47 #if defined(POLY_ACC)
48 #include "poly_acc.h"
49 #endif
50
51 #include "makesig.h"
52
53 #define MAX_OPEN_FONTS  50
54 #define FILENAME_LEN            13
55
56 typedef struct openfont {
57         char filename[FILENAME_LEN];
58         grs_font *ptr;
59         char *dataptr;
60 } openfont;
61
62 //list of open fonts, for use (for now) for palette remapping
63 openfont open_font[MAX_OPEN_FONTS];
64
65 #define FONT        grd_curcanv->cv_font
66 #define FG_COLOR    grd_curcanv->cv_font_fg_color
67 #define BG_COLOR    grd_curcanv->cv_font_bg_color
68 #define FWIDTH       FONT->ft_w
69 #define FHEIGHT      FONT->ft_h
70 #define FBASELINE    FONT->ft_baseline
71 #define FFLAGS       FONT->ft_flags
72 #define FMINCHAR     FONT->ft_minchar
73 #define FMAXCHAR     FONT->ft_maxchar
74 #define FDATA        FONT->ft_data
75 #define FCHARS       FONT->ft_chars
76 #define FWIDTHS      FONT->ft_widths
77
78 #define BITS_TO_BYTES(x)    (((x)+7)>>3)
79
80 int gr_internal_string_clipped(int x, int y, char *s );
81 int gr_internal_string_clipped_m(int x, int y, char *s );
82
83 ubyte *find_kern_entry(grs_font *font,ubyte first,ubyte second)
84 {
85         ubyte *p=font->ft_kerndata;
86
87         while (*p!=255)
88                 if (p[0]==first && p[1]==second)
89                         return p;
90                 else p+=3;
91
92         return NULL;
93
94 }
95
96 //takes the character AFTER being offset into font
97 #define INFONT(_c) ((_c >= 0) && (_c <= FMAXCHAR-FMINCHAR))
98
99 //takes the character BEFORE being offset into current font
100 void get_char_width(ubyte c,ubyte c2,int *width,int *spacing)
101 {
102         int letter;
103
104         letter = c-FMINCHAR;
105
106         if (!INFONT(letter)) {                          //not in font, draw as space
107                 *width=0;
108                 if (FFLAGS & FT_PROPORTIONAL)
109                         *spacing = FWIDTH/2;
110                 else
111                         *spacing = FWIDTH;
112                 return;
113         }
114
115         if (FFLAGS & FT_PROPORTIONAL)
116                 *width = FWIDTHS[letter];
117         else
118                 *width = FWIDTH;
119
120         *spacing = *width;
121
122         if (FFLAGS & FT_KERNED)  {
123                 ubyte *p;
124
125                 if (!(c2==0 || c2=='\n')) {
126                         int letter2;
127
128                         letter2 = c2-FMINCHAR;
129
130                         if (INFONT(letter2)) {
131
132                                 p = find_kern_entry(FONT,(ubyte)letter,letter2);
133
134                                 if (p)
135                                         *spacing = p[2];
136                         }
137                 }
138         }
139 }
140
141 int get_centered_x(char *s)
142 {
143         int w,w2,s2;
144
145         for (w=0;*s!=0 && *s!='\n';s++) {
146                 if (*s<=0x06) {
147                         if (*s<=0x03)
148                                 s++;
149                         continue;//skip color codes.
150                 }
151                 get_char_width(s[0],s[1],&w2,&s2);
152                 w += s2;
153         }
154
155         return ((grd_curcanv->cv_bitmap.bm_w - w) / 2);
156 }
157
158 //hack to allow color codes to be embedded in strings -MPM
159 //note we subtract one from color, since 255 is "transparent" so it'll never be used, and 0 would otherwise end the string.
160 //function must already have orig_color var set (or they could be passed as args...)
161 //perhaps some sort of recursive orig_color type thing would be better, but that would be way too much trouble for little gain
162 int gr_message_color_level=1;
163 #define CHECK_EMBEDDED_COLORS() if ((*text_ptr >= 0x01) && (*text_ptr <= 0x03)) { \
164                 text_ptr++; \
165                 if (*text_ptr){ \
166                         if (gr_message_color_level >= *(text_ptr-1)) \
167                                 FG_COLOR = *text_ptr - 1; \
168                         text_ptr++; \
169                 } \
170         } \
171         else if ((*text_ptr >= 0x04) && (*text_ptr <= 0x06)){ \
172                 if (gr_message_color_level >= *text_ptr - 3) \
173                         FG_COLOR=orig_color; \
174                 text_ptr++; \
175         }
176
177 int gr_internal_string0(int x, int y, char *s )
178 {
179         unsigned char * fp;
180         char * text_ptr, * next_row, * text_ptr1;
181         int r, BitMask, i, bits, width, spacing, letter, underline;
182         int     skip_lines = 0;
183
184         unsigned int VideoOffset, VideoOffset1;
185
186         bits=0;
187
188         VideoOffset1 = y * ROWSIZE + x;
189
190         next_row = s;
191
192         while (next_row != NULL )
193         {
194                 text_ptr1 = next_row;
195                 next_row = NULL;
196
197                 if (x==0x8000) {                        //centered
198                         int xx = get_centered_x(text_ptr1);
199                         VideoOffset1 = y * ROWSIZE + xx;
200                 }
201
202                 for (r=0; r<FHEIGHT; r++)
203                 {
204
205                         text_ptr = text_ptr1;
206
207                         VideoOffset = VideoOffset1;
208
209                         while (*text_ptr)
210                         {
211                                 if (*text_ptr == '\n' )
212                                 {
213                                         next_row = &text_ptr[1];
214                                         break;
215                                 }
216
217                                 if (*text_ptr == CC_COLOR) {
218                                         FG_COLOR = *(text_ptr+1);
219                                         text_ptr += 2;
220                                         continue;
221                                 }
222
223                                 if (*text_ptr == CC_LSPACING) {
224                                         skip_lines = *(text_ptr+1) - '0';
225                                         text_ptr += 2;
226                                         continue;
227                                 }
228
229                                 underline = 0;
230                                 if (*text_ptr == CC_UNDERLINE )
231                                 {
232                                         if ((r==FBASELINE+2) || (r==FBASELINE+3))
233                                                 underline = 1;
234                                         text_ptr++;
235                                 }
236
237                                 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
238
239                                 letter = *text_ptr-FMINCHAR;
240
241                                 if (!INFONT(letter)) {  //not in font, draw as space
242                                         VideoOffset += spacing;
243                                         text_ptr++;
244                                         continue;
245                                 }
246
247                                 if (FFLAGS & FT_PROPORTIONAL)
248                                         fp = FCHARS[letter];
249                                 else
250                                         fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
251
252                                 if (underline)
253                                         for (i=0; i< width; i++ )
254                                                 DATA[VideoOffset++] = (unsigned char) FG_COLOR;
255                                 else
256                                 {
257                                         fp += BITS_TO_BYTES(width)*r;
258
259                                         BitMask = 0;
260
261                                         for (i=0; i< width; i++ )
262                                         {
263                                                 if (BitMask==0) {
264                                                         bits = *fp++;
265                                                         BitMask = 0x80;
266                                                 }
267
268                                                 if (bits & BitMask)
269                                                         DATA[VideoOffset++] = (unsigned char) FG_COLOR;
270                                                 else
271                                                         DATA[VideoOffset++] = (unsigned char) BG_COLOR;
272                                                 BitMask >>= 1;
273                                         }
274                                 }
275
276                                 VideoOffset += spacing-width;           //for kerning
277
278                                 text_ptr++;
279                         }
280
281                         VideoOffset1 += ROWSIZE; y++;
282                 }
283
284                 y += skip_lines;
285                 VideoOffset1 += ROWSIZE * skip_lines;
286                 skip_lines = 0;
287         }
288         return 0;
289 }
290
291 int gr_internal_string0m(int x, int y, char *s )
292 {
293         unsigned char * fp;
294         char * text_ptr, * next_row, * text_ptr1;
295         int r, BitMask, i, bits, width, spacing, letter, underline;
296         int     skip_lines = 0;
297
298         unsigned int VideoOffset, VideoOffset1;
299
300         int orig_color=FG_COLOR;//to allow easy reseting to default string color with colored strings -MPM
301
302         bits=0;
303
304         VideoOffset1 = y * ROWSIZE + x;
305
306         next_row = s;
307
308         while (next_row != NULL )
309         {
310                 text_ptr1 = next_row;
311                 next_row = NULL;
312
313                 if (x==0x8000) {                        //centered
314                         int xx = get_centered_x(text_ptr1);
315                         VideoOffset1 = y * ROWSIZE + xx;
316                 }
317
318                 for (r=0; r<FHEIGHT; r++)
319                 {
320
321                         text_ptr = text_ptr1;
322
323                         VideoOffset = VideoOffset1;
324
325                         while (*text_ptr)
326                         {
327                                 if (*text_ptr == '\n' )
328                                 {
329                                         next_row = &text_ptr[1];
330                                         break;
331                                 }
332
333                                 if (*text_ptr == CC_COLOR) {
334                                         FG_COLOR = *(text_ptr+1);
335                                         text_ptr += 2;
336                                         continue;
337                                 }
338
339                                 if (*text_ptr == CC_LSPACING) {
340                                         skip_lines = *(text_ptr+1) - '0';
341                                         text_ptr += 2;
342                                         continue;
343                                 }
344
345                                 underline = 0;
346                                 if (*text_ptr == CC_UNDERLINE )
347                                 {
348                                         if ((r==FBASELINE+2) || (r==FBASELINE+3))
349                                                 underline = 1;
350                                         text_ptr++;
351                                 }
352
353                                 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
354
355                                 letter = *text_ptr-FMINCHAR;
356
357                                 if (!INFONT(letter) || *text_ptr<=0x06) {       //not in font, draw as space
358                                         CHECK_EMBEDDED_COLORS() else{
359                                                 VideoOffset += spacing;
360                                                 text_ptr++;
361                                         }
362                                         continue;
363                                 }
364
365                                 if (FFLAGS & FT_PROPORTIONAL)
366                                         fp = FCHARS[letter];
367                                 else
368                                         fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
369
370                                 if (underline)
371                                         for (i=0; i< width; i++ )
372                                                 DATA[VideoOffset++] = (unsigned int) FG_COLOR;
373                                 else
374                                 {
375                                         fp += BITS_TO_BYTES(width)*r;
376
377                                         BitMask = 0;
378
379                                         for (i=0; i< width; i++ )
380                                         {
381                                                 if (BitMask==0) {
382                                                         bits = *fp++;
383                                                         BitMask = 0x80;
384                                                 }
385
386                                                 if (bits & BitMask)
387                                                         DATA[VideoOffset++] = (unsigned int) FG_COLOR;
388                                                 else
389                                                         VideoOffset++;
390                                                 BitMask >>= 1;
391                                         }
392                                 }
393                                 text_ptr++;
394
395                                 VideoOffset += spacing-width;
396                         }
397
398                         VideoOffset1 += ROWSIZE;
399                         y++;
400                 }
401                 y += skip_lines;
402                 VideoOffset1 += ROWSIZE * skip_lines;
403                 skip_lines = 0;
404         }
405         return 0;
406 }
407
408 #ifdef __MSDOS__
409 int gr_internal_string2(int x, int y, char *s )
410 {
411         unsigned char * fp;
412         ubyte * text_ptr, * next_row, * text_ptr1;
413         int r, BitMask, i, bits, width, spacing, letter, underline;
414         int page_switched, skip_lines = 0;
415
416         unsigned int VideoOffset, VideoOffset1;
417
418         VideoOffset1 = (size_t)DATA + y * ROWSIZE + x;
419
420         bits = 0;
421
422         gr_vesa_setpage(VideoOffset1 >> 16);
423
424         VideoOffset1 &= 0xFFFF;
425
426         next_row = s;
427
428         while (next_row != NULL )
429         {
430                 text_ptr1 = next_row;
431                 next_row = NULL;
432
433                 if (x==0x8000) {                        //centered
434                         int xx = get_centered_x(text_ptr1);
435                         VideoOffset1 = y * ROWSIZE + xx;
436                         gr_vesa_setpage(VideoOffset1 >> 16);
437                         VideoOffset1 &= 0xFFFF;
438                 }
439
440                 for (r=0; r<FHEIGHT; r++)
441                 {
442                         text_ptr = text_ptr1;
443
444                         VideoOffset = VideoOffset1;
445
446                         page_switched = 0;
447
448                         while (*text_ptr)
449                         {
450                                 if (*text_ptr == '\n' )
451                                 {
452                                         next_row = &text_ptr[1];
453                                         break;
454                                 }
455
456                                 if (*text_ptr == CC_COLOR) {
457                                         FG_COLOR = *(text_ptr+1);
458                                         text_ptr += 2;
459                                         continue;
460                                 }
461
462                                 if (*text_ptr == CC_LSPACING) {
463                                         skip_lines = *(text_ptr+1) - '0';
464                                         text_ptr += 2;
465                                         continue;
466                                 }
467
468                                 underline = 0;
469                                 if (*text_ptr == CC_UNDERLINE )
470                                 {
471                                         if ((r==FBASELINE+2) || (r==FBASELINE+3))
472                                                 underline = 1;
473                                         text_ptr++;
474                                 }
475
476                                 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
477
478                                 Assert(width==spacing);         //no kerning support here
479
480                                 letter = *text_ptr-FMINCHAR;
481
482                                 if (!INFONT(letter)) {  //not in font, draw as space
483                                         VideoOffset += spacing;
484                                         text_ptr++;
485                                         continue;
486                                 }
487
488                                 if (FFLAGS & FT_PROPORTIONAL)
489                                         fp = FCHARS[letter];
490                                 else
491                                         fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
492
493                                 if (underline)
494                                 {
495                                         if ( VideoOffset+width > 0xFFFF )
496                                         {
497                                                 for (i=0; i< width; i++ )
498                                                 {
499                                                         gr_video_memory[VideoOffset++] = FG_COLOR;
500
501                                                         if (VideoOffset > 0xFFFF )
502                                                         {
503                                                                 VideoOffset -= 0xFFFF + 1;
504                                                                 page_switched = 1;
505                                                                 gr_vesa_incpage();
506                                                         }
507                                                 }
508                                         }
509                                         else
510                                         {
511                                                 for (i=0; i< width; i++ )
512                                                         gr_video_memory[VideoOffset++] = FG_COLOR;
513                                         }
514                                 }
515                                 else
516                                 {
517                                         // fp -- dword
518                                         // VideoOffset
519                                         // width
520
521                                         fp += BITS_TO_BYTES(width)*r;
522
523                                         BitMask = 0;
524
525                                         if ( VideoOffset+width > 0xFFFF )
526                                         {
527                                                 for (i=0; i< width; i++ )
528                                                 {
529                                                         if (BitMask==0) {
530                                                                 bits = *fp++;
531                                                                 BitMask = 0x80;
532                                                         }
533
534                                                         if (bits & BitMask)
535                                                                 gr_video_memory[VideoOffset++] = FG_COLOR;
536                                                         else
537                                                                 gr_video_memory[VideoOffset++] = BG_COLOR;
538
539                                                         BitMask >>= 1;
540
541                                                         if (VideoOffset > 0xFFFF )
542                                                         {
543                                                                 VideoOffset -= 0xFFFF + 1;
544                                                                 page_switched = 1;
545                                                                 gr_vesa_incpage();
546                                                         }
547
548                                                 }
549                                         } else {
550
551                                                 if (width == 8 )
552                                                 {
553                                                         bits = *fp++;
554
555                                                         if (bits & 0x80) gr_video_memory[VideoOffset+0] = FG_COLOR;
556                                                         else gr_video_memory[VideoOffset+0] = BG_COLOR;
557
558                                                         if (bits & 0x40) gr_video_memory[VideoOffset+1] = FG_COLOR;
559                                                         else gr_video_memory[VideoOffset+1] = BG_COLOR;
560
561                                                         if (bits & 0x20) gr_video_memory[VideoOffset+2] = FG_COLOR;
562                                                         else gr_video_memory[VideoOffset+2] = BG_COLOR;
563
564                                                         if (bits & 0x10) gr_video_memory[VideoOffset+3] = FG_COLOR;
565                                                         else gr_video_memory[VideoOffset+3] = BG_COLOR;
566
567                                                         if (bits & 0x08) gr_video_memory[VideoOffset+4] = FG_COLOR;
568                                                         else gr_video_memory[VideoOffset+4] = BG_COLOR;
569
570                                                         if (bits & 0x04) gr_video_memory[VideoOffset+5] = FG_COLOR;
571                                                         else gr_video_memory[VideoOffset+5] = BG_COLOR;
572
573                                                         if (bits & 0x02) gr_video_memory[VideoOffset+6] = FG_COLOR;
574                                                         else gr_video_memory[VideoOffset+6] = BG_COLOR;
575
576                                                         if (bits & 0x01) gr_video_memory[VideoOffset+7] = FG_COLOR;
577                                                         else gr_video_memory[VideoOffset+7] = BG_COLOR;
578
579                                                         VideoOffset += 8;
580                                                 } else {
581                                                         for (i=0; i< width/2 ; i++ )
582                                                         {
583                                                                 if (BitMask==0) {
584                                                                         bits = *fp++;
585                                                                         BitMask = 0x80;
586                                                                 }
587
588                                                                 if (bits & BitMask)
589                                                                         gr_video_memory[VideoOffset++] = FG_COLOR;
590                                                                 else
591                                                                         gr_video_memory[VideoOffset++] = BG_COLOR;
592                                                                 BitMask >>= 1;
593
594
595                                                                 // Unroll twice
596
597                                                                 if (BitMask==0) {
598                                                                         bits = *fp++;
599                                                                         BitMask = 0x80;
600                                                                 }
601
602                                                                 if (bits & BitMask)
603                                                                         gr_video_memory[VideoOffset++] = FG_COLOR;
604                                                                 else
605                                                                         gr_video_memory[VideoOffset++] = BG_COLOR;
606                                                                 BitMask >>= 1;
607                                                         }
608                                                 }
609                                         }
610                                 }
611                                 text_ptr++;
612                         }
613
614                         y ++;
615                         VideoOffset1 += ROWSIZE;
616
617                         if (VideoOffset1 > 0xFFFF ) {
618                                 VideoOffset1 -= 0xFFFF + 1;
619                                 if (!page_switched)
620                                         gr_vesa_incpage();
621                         }
622                 }
623
624                 y += skip_lines;
625                 VideoOffset1 += ROWSIZE * skip_lines;
626                 skip_lines = 0;
627         }
628         return 0;
629 }
630
631 int gr_internal_string2m(int x, int y, char *s )
632 {
633         unsigned char * fp;
634         char * text_ptr, * next_row, * text_ptr1;
635         int r, BitMask, i, bits, width, spacing, letter, underline;
636         int page_switched, skip_lines = 0;
637
638         unsigned int VideoOffset, VideoOffset1;
639
640         VideoOffset1 = (size_t)DATA + y * ROWSIZE + x;
641
642         gr_vesa_setpage(VideoOffset1 >> 16);
643
644         VideoOffset1 &= 0xFFFF;
645
646         next_row = s;
647
648         while (next_row != NULL )
649         {
650                 text_ptr1 = next_row;
651                 next_row = NULL;
652
653                 if (x==0x8000) {                        //centered
654                         int xx = get_centered_x(text_ptr1);
655                         VideoOffset1 = y * ROWSIZE + xx;
656                         gr_vesa_setpage(VideoOffset1 >> 16);
657                         VideoOffset1 &= 0xFFFF;
658                 }
659
660                 for (r=0; r<FHEIGHT; r++)
661                 {
662                         text_ptr = text_ptr1;
663
664                         VideoOffset = VideoOffset1;
665
666                         page_switched = 0;
667
668                         while (*text_ptr)
669                         {
670                                 if (*text_ptr == '\n' )
671                                 {
672                                         next_row = &text_ptr[1];
673                                         break;
674                                 }
675
676                                 if (*text_ptr == CC_COLOR) {
677                                         FG_COLOR = *(text_ptr+1);
678                                         text_ptr += 2;
679                                         continue;
680                                 }
681
682                                 if (*text_ptr == CC_LSPACING) {
683                                         skip_lines = *(text_ptr+1) - '0';
684                                         text_ptr += 2;
685                                         continue;
686                                 }
687
688                                 underline = 0;
689                                 if (*text_ptr == CC_UNDERLINE )
690                                 {
691                                         if ((r==FBASELINE+2) || (r==FBASELINE+3))
692                                                 underline = 1;
693                                         text_ptr++;
694                                 }
695
696                                 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
697
698                                 letter = *text_ptr-FMINCHAR;
699
700                                 if (!INFONT(letter)) {  //not in font, draw as space
701                                         VideoOffset += width;
702                                         text_ptr++;
703                                         continue;
704                                 }
705
706                                 if (FFLAGS & FT_PROPORTIONAL)
707                                         fp = FCHARS[letter];
708                                 else
709                                         fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
710
711                                 if (underline)
712                                 {
713                                         if ( VideoOffset+width > 0xFFFF )
714                                         {
715                                                 for (i=0; i< width; i++ )
716                                                 {
717                                                         gr_video_memory[VideoOffset++] = FG_COLOR;
718
719                                                         if (VideoOffset > 0xFFFF )
720                                                         {
721                                                                 VideoOffset -= 0xFFFF + 1;
722                                                                 page_switched = 1;
723                                                                 gr_vesa_incpage();
724                                                         }
725                                                 }
726                                         }
727                                         else
728                                         {
729                                                 for (i=0; i< width; i++ )
730                                                         gr_video_memory[VideoOffset++] = FG_COLOR;
731                                         }
732                                 }
733                                 else
734                                 {
735                                         fp += BITS_TO_BYTES(width)*r;
736
737                                         BitMask = 0;
738
739                                         if ( VideoOffset+width > 0xFFFF )
740                                         {
741                                                 for (i=0; i< width; i++ )
742                                                 {
743                                                         if (BitMask==0) {
744                                                                 bits = *fp++;
745                                                                 BitMask = 0x80;
746                                                         }
747
748                                                         if (bits & BitMask)
749                                                                 gr_video_memory[VideoOffset++] = FG_COLOR;
750                                                         else
751                                                                 VideoOffset++;
752
753                                                         BitMask >>= 1;
754
755                                                         if (VideoOffset > 0xFFFF )
756                                                         {
757                                                                 VideoOffset -= 0xFFFF + 1;
758                                                                 page_switched = 1;
759                                                                 gr_vesa_incpage();
760                                                         }
761
762                                                 }
763                                         } else {
764                                                 for (i=0; i< width; i++ )
765                                                 {
766                                                         if (BitMask==0) {
767                                                                 bits = *fp++;
768                                                                 BitMask = 0x80;
769                                                         }
770
771                                                         if (bits & BitMask)
772                                                                 gr_video_memory[VideoOffset++] = FG_COLOR;
773                                                         else
774                                                                 VideoOffset++;;
775                                                         BitMask >>= 1;
776                                                 }
777                                         }
778                                 }
779                                 text_ptr++;
780
781                                 VideoOffset += spacing-width;
782                         }
783
784                         y ++;
785                         VideoOffset1 += ROWSIZE;
786
787                         if (VideoOffset1 > 0xFFFF ) {
788                                 VideoOffset1 -= 0xFFFF + 1;
789                                 if (!page_switched)
790                                         gr_vesa_incpage();
791                         }
792                 }
793
794                 y += skip_lines;
795                 VideoOffset1 += ROWSIZE * skip_lines;
796                 skip_lines = 0;
797         }
798         return 0;
799 }
800
801 #endif // __MSDOS__
802
803 #if defined(POLY_ACC)
804 int gr_internal_string5(int x, int y, char *s )
805 {
806         unsigned char * fp;
807         ubyte * text_ptr, * next_row, * text_ptr1;
808         int r, BitMask, i, bits, width, spacing, letter, underline;
809         int     skip_lines = 0;
810
811         unsigned int VideoOffset, VideoOffset1;
812
813     pa_flush();
814     VideoOffset1 = y * ROWSIZE + x * PA_BPP;
815
816         next_row = s;
817
818         while (next_row != NULL )
819         {
820                 text_ptr1 = next_row;
821                 next_row = NULL;
822
823                 if (x==0x8000) {                        //centered
824                         int xx = get_centered_x(text_ptr1);
825             VideoOffset1 = y * ROWSIZE + xx * PA_BPP;
826                 }
827
828                 for (r=0; r<FHEIGHT; r++)
829                 {
830
831                         text_ptr = text_ptr1;
832
833                         VideoOffset = VideoOffset1;
834
835                         while (*text_ptr)
836                         {
837                                 if (*text_ptr == '\n' )
838                                 {
839                                         next_row = &text_ptr[1];
840                                         break;
841                                 }
842
843                                 if (*text_ptr == CC_COLOR) {
844                                         FG_COLOR = *(text_ptr+1);
845                                         text_ptr += 2;
846                                         continue;
847                                 }
848
849                                 if (*text_ptr == CC_LSPACING) {
850                                         skip_lines = *(text_ptr+1) - '0';
851                                         text_ptr += 2;
852                                         continue;
853                                 }
854
855                                 underline = 0;
856                                 if (*text_ptr == CC_UNDERLINE )
857                                 {
858                                         if ((r==FBASELINE+2) || (r==FBASELINE+3))
859                                                 underline = 1;
860                                         text_ptr++;
861                                 }
862
863                                 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
864
865                                 letter = *text_ptr-FMINCHAR;
866
867                                 if (!INFONT(letter)) {  //not in font, draw as space
868                     VideoOffset += spacing * PA_BPP;
869                                         text_ptr++;
870                                         continue;
871                                 }
872
873                                 if (FFLAGS & FT_PROPORTIONAL)
874                                         fp = FCHARS[letter];
875                                 else
876                                         fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
877
878                                 if (underline)
879                                         for (i=0; i< width; i++ )
880                     {    *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
881                                 else
882                                 {
883                                         fp += BITS_TO_BYTES(width)*r;
884
885                                         BitMask = 0;
886
887                                         for (i=0; i< width; i++ )
888                                         {
889                                                 if (BitMask==0) {
890                                                         bits = *fp++;
891                                                         BitMask = 0x80;
892                                                 }
893
894                                                 if (bits & BitMask)
895                         {    *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
896                         else
897                         {    *(short *)(DATA + VideoOffset) = pa_clut[BG_COLOR]; VideoOffset += PA_BPP; }
898                         BitMask >>= 1;
899                                         }
900                                 }
901
902                 VideoOffset += PA_BPP * (spacing-width);       //for kerning
903
904                                 text_ptr++;
905                         }
906
907                         VideoOffset1 += ROWSIZE; y++;
908                 }
909
910                 y += skip_lines;
911                 VideoOffset1 += ROWSIZE * skip_lines;
912                 skip_lines = 0;
913         }
914         return 0;
915 }
916
917 int gr_internal_string5m(int x, int y, char *s )
918 {
919         unsigned char * fp;
920         ubyte * text_ptr, * next_row, * text_ptr1;
921         int r, BitMask, i, bits, width, spacing, letter, underline;
922         int     skip_lines = 0;
923
924         unsigned int VideoOffset, VideoOffset1;
925
926     pa_flush();
927     VideoOffset1 = y * ROWSIZE + x * PA_BPP;
928
929         next_row = s;
930
931         while (next_row != NULL )
932         {
933                 text_ptr1 = next_row;
934                 next_row = NULL;
935
936                 if (x==0x8000) {                        //centered
937                         int xx = get_centered_x(text_ptr1);
938             VideoOffset1 = y * ROWSIZE + xx * PA_BPP;
939                 }
940
941                 for (r=0; r<FHEIGHT; r++)
942                 {
943
944                         text_ptr = text_ptr1;
945
946                         VideoOffset = VideoOffset1;
947
948                         while (*text_ptr)
949                         {
950                                 if (*text_ptr == '\n' )
951                                 {
952                                         next_row = &text_ptr[1];
953                                         break;
954                                 }
955
956                                 if (*text_ptr == CC_COLOR) {
957                                         FG_COLOR = *(text_ptr+1);
958                                         text_ptr += 2;
959                                         continue;
960                                 }
961
962                                 if (*text_ptr == CC_LSPACING) {
963                                         skip_lines = *(text_ptr+1) - '0';
964                                         text_ptr += 2;
965                                         continue;
966                                 }
967
968                                 underline = 0;
969                                 if (*text_ptr == CC_UNDERLINE )
970                                 {
971                                         if ((r==FBASELINE+2) || (r==FBASELINE+3))
972                                                 underline = 1;
973                                         text_ptr++;
974                                 }
975
976                                 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
977
978                                 letter = *text_ptr-FMINCHAR;
979
980                                 if (!INFONT(letter)) {  //not in font, draw as space
981                     VideoOffset += spacing * PA_BPP;
982                                         text_ptr++;
983                                         continue;
984                                 }
985
986                                 if (FFLAGS & FT_PROPORTIONAL)
987                                         fp = FCHARS[letter];
988                                 else
989                                         fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
990
991                                 if (underline)
992                                         for (i=0; i< width; i++ )
993                     {    *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
994                 else
995                                 {
996                                         fp += BITS_TO_BYTES(width)*r;
997
998                                         BitMask = 0;
999
1000                                         for (i=0; i< width; i++ )
1001                                         {
1002                                                 if (BitMask==0) {
1003                                                         bits = *fp++;
1004                                                         BitMask = 0x80;
1005                                                 }
1006
1007                                                 if (bits & BitMask)
1008                         {    *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
1009                         else
1010                             VideoOffset += PA_BPP;
1011                                                 BitMask >>= 1;
1012                                         }
1013                                 }
1014                                 text_ptr++;
1015
1016                 VideoOffset += PA_BPP * (spacing-width);
1017                         }
1018
1019                         VideoOffset1 += ROWSIZE; y++;
1020                 }
1021                 y += skip_lines;
1022                 VideoOffset1 += ROWSIZE * skip_lines;
1023                 skip_lines = 0;
1024         }
1025         return 0;
1026 }
1027 #endif
1028
1029 #ifndef OGL
1030 //a bitmap for the character
1031 grs_bitmap char_bm = {
1032                                 0,0,0,0,                                                //x,y,w,h
1033                                 BM_LINEAR,                                      //type
1034                                 BM_FLAG_TRANSPARENT,            //flags
1035                                 0,                                                              //rowsize
1036                                 NULL,                                                   //data
1037 #ifdef BITMAP_SELECTOR
1038                                 0,                              //selector
1039 #endif
1040                                 0,     //avg_color
1041                                 0      //unused
1042 };
1043
1044 int gr_internal_color_string(int x, int y, char *s )
1045 {
1046         unsigned char * fp;
1047         char *text_ptr, *next_row, *text_ptr1;
1048         int width, spacing,letter;
1049         int xx,yy;
1050
1051         char_bm.bm_h = FHEIGHT;         //set height for chars of this font
1052
1053         next_row = s;
1054
1055         yy = y;
1056
1057
1058         while (next_row != NULL)
1059         {
1060                 text_ptr1 = next_row;
1061                 next_row = NULL;
1062
1063                 text_ptr = text_ptr1;
1064
1065                 xx = x;
1066
1067                 if (xx==0x8000)                 //centered
1068                         xx = get_centered_x(text_ptr);
1069
1070                 while (*text_ptr)
1071                 {
1072                         if (*text_ptr == '\n' )
1073                         {
1074                                 next_row = &text_ptr[1];
1075                                 yy += FHEIGHT;
1076                                 break;
1077                         }
1078
1079                         letter = *text_ptr-FMINCHAR;
1080
1081                         get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1082
1083                         if (!INFONT(letter)) {  //not in font, draw as space
1084                                 xx += spacing;
1085                                 text_ptr++;
1086                                 continue;
1087                         }
1088
1089                         if (FFLAGS & FT_PROPORTIONAL)
1090                                 fp = FCHARS[letter];
1091                         else
1092                                 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
1093
1094                         gr_init_bitmap (&char_bm, BM_LINEAR, 0, 0, width, FHEIGHT, width, fp);
1095                         gr_bitmapm(xx,yy,&char_bm);
1096
1097                         xx += spacing;
1098
1099                         text_ptr++;
1100                 }
1101
1102         }
1103         return 0;
1104 }
1105
1106 #else //OGL
1107
1108 #include "ogl_init.h"
1109 #include "args.h"
1110 //font handling routines for OpenGL - Added 9/25/99 Matthew Mueller - they are here instead of in arch/ogl because they use all these defines
1111
1112 int pow2ize(int x);//from ogl.c
1113
1114 int get_font_total_width(grs_font * font){
1115         if (font->ft_flags & FT_PROPORTIONAL){
1116                 int i,w=0,c=font->ft_minchar;
1117                 for (i=0;c<=font->ft_maxchar;i++,c++){
1118                         if (font->ft_widths[i]<0)
1119                                 Error("heh?\n");
1120                         w+=font->ft_widths[i];
1121                 }
1122                 return w;
1123         }else{
1124                 return font->ft_w*(font->ft_maxchar-font->ft_minchar+1);
1125         }
1126 }
1127 void ogl_font_choose_size(grs_font * font,int gap,int *rw,int *rh){
1128         int     nchars = font->ft_maxchar-font->ft_minchar+1;
1129         int r,x,y,nc=0,smallest=999999,smallr=-1,tries;
1130         int smallprop=10000;
1131         int h,w;
1132         for (h=32;h<=256;h*=2){
1133 //              h=pow2ize(font->ft_h*rows+gap*(rows-1));
1134                 if (font->ft_h>h)continue;
1135                 r=(h/(font->ft_h+gap));
1136                 w=pow2ize((get_font_total_width(font)+(nchars-r)*gap)/r);
1137                 tries=0;
1138                 do {
1139                         if (tries)
1140                                 w=pow2ize(w+1);
1141                         if(tries>3){
1142                                 mprintf((0,"failed to fit (%ix%i, %ic)\n",w,h,nc));
1143                                 break;
1144                         }
1145                         nc=0;
1146                         y=0;
1147                         while(y+font->ft_h<=h){
1148                                 x=0;
1149                                 while (x<w){
1150                                         if (nc==nchars)
1151                                                 break;
1152                                         if (font->ft_flags & FT_PROPORTIONAL){
1153                                                 if (x+font->ft_widths[nc]+gap>w)break;
1154                                                 x+=font->ft_widths[nc++]+gap;
1155                                         }else{
1156                                                 if (x+font->ft_w+gap>w)break;
1157                                                 x+=font->ft_w+gap;
1158                                                 nc++;
1159                                         }
1160                                 }
1161                                 if (nc==nchars)
1162                                         break;
1163                                 y+=font->ft_h+gap;
1164                         }
1165                         
1166                         tries++;
1167                 }while(nc!=nchars);
1168                 if (nc!=nchars)
1169                         continue;
1170                 mprintf((0,"fit: %ix%i  %i tries\n",w,h,tries));
1171
1172                 if (w*h==smallest){//this gives squarer sizes priority (ie, 128x128 would be better than 512*32)
1173                         if (w>=h){
1174                                 if (w/h<smallprop){
1175                                         smallprop=w/h;
1176                                         smallest++;//hack
1177                                 }
1178                         }else{
1179                                 if (h/w<smallprop){
1180                                         smallprop=h/w;
1181                                         smallest++;//hack
1182                                 }
1183                         }
1184                 }
1185                 if (w*h<smallest){
1186                         smallr=1;
1187                         smallest=w*h;
1188                         *rw=w;
1189                         *rh=h;
1190                 }
1191         }
1192         if (smallr<=0)
1193                 Error("couldn't fit font?\n");
1194         mprintf((0,"using %ix%i\n",*rw,*rh));
1195         
1196 }
1197
1198 void ogl_init_font(grs_font * font){
1199         int oglflags = OGL_FLAG_ALPHA;
1200         int     nchars = font->ft_maxchar-font->ft_minchar+1;
1201         int i,w,h,tw,th,x,y,curx=0,cury=0;
1202         unsigned char *fp;
1203         //      char data[32*32*4];
1204         ubyte *data;
1205         int gap=0;//having a gap just wastes ram, since we don't filter text textures at all.
1206         //      char s[2];
1207         ogl_font_choose_size(font,gap,&tw,&th);
1208         data=d_malloc(tw*th);
1209         memset(data, 0, tw * th);
1210         gr_init_bitmap(&font->ft_parent_bitmap,BM_LINEAR,0,0,tw,th,tw,data);
1211         gr_set_transparent(&font->ft_parent_bitmap, 1);
1212
1213         if (!(font->ft_flags & FT_COLOR))
1214                 oglflags |= OGL_FLAG_NOCOLOR;
1215         ogl_init_texture(font->ft_parent_bitmap.gltexture = ogl_get_free_texture(), tw, th, oglflags); // have to init the gltexture here so the subbitmaps will find it.
1216
1217         font->ft_bitmaps=(grs_bitmap*)d_malloc( nchars * sizeof(grs_bitmap));
1218         mprintf((0,"ogl_init_font %s, %s, nchars=%i, (%ix%i tex)\n",(font->ft_flags & FT_PROPORTIONAL)?"proportional":"fixedwidth",(font->ft_flags & FT_COLOR)?"color":"mono",nchars,tw,th));
1219         //      s[1]=0;
1220         h=font->ft_h;
1221         //      sleep(5);
1222
1223         for(i=0;i<nchars;i++){
1224                 //              s[0]=font->ft_minchar+i;
1225                 //              gr_get_string_size(s,&w,&h,&aw);
1226                 if (font->ft_flags & FT_PROPORTIONAL)
1227                         w=font->ft_widths[i];
1228                 else
1229                         w=font->ft_w;
1230 //              mprintf((0,"char %i(%ix%i): ",i,w,h));
1231                 if (w<1 || w>256){
1232                         mprintf((0,"grr\n"));continue;
1233                 }
1234                 if (curx+w+gap>tw){
1235                         cury+=h+gap;
1236                         curx=0;
1237                 }
1238                 if (cury+h>th)
1239                         Error("font doesn't really fit (%i/%i)?\n",i,nchars);
1240                 if (font->ft_flags & FT_COLOR) {
1241                         if (font->ft_flags & FT_PROPORTIONAL)
1242                                 fp = font->ft_chars[i];
1243                         else
1244                                 fp = font->ft_data + i * w*h;
1245                         for (y=0;y<h;y++)
1246                                 for (x=0;x<w;x++){
1247                                         font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=fp[x+y*w];
1248                                 }
1249
1250                         //                      gr_init_bitmap(&font->ft_bitmaps[i],BM_LINEAR,0,0,w,h,w,font->);
1251                 }else{
1252                         int BitMask,bits=0,white=gr_find_closest_color(63,63,63);
1253                         //                      if (w*h>sizeof(data))
1254                         //                              Error("ogl_init_font: toobig\n");
1255                         if (font->ft_flags & FT_PROPORTIONAL)
1256                                 fp = font->ft_chars[i];
1257                         else
1258                                 fp = font->ft_data + i * BITS_TO_BYTES(w)*h;
1259                         for (y=0;y<h;y++){
1260                                 BitMask=0;
1261                                 for (x=0; x< w; x++ )
1262                                 {
1263                                         if (BitMask==0) {
1264                                                 bits = *fp++;
1265                                                 BitMask = 0x80;
1266                                         }
1267
1268                                         if (bits & BitMask)
1269                                                 font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=white;
1270                                         else
1271                                                 font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=255;
1272                                         BitMask >>= 1;
1273                                 }
1274                         }
1275                 }
1276                 gr_init_sub_bitmap(&font->ft_bitmaps[i],&font->ft_parent_bitmap,curx,cury,w,h);
1277
1278                 curx+=w+gap;
1279         }
1280         ogl_loadbmtexture_f(&font->ft_parent_bitmap, oglflags);
1281 }
1282
1283 int ogl_internal_string(int x, int y, char *s )
1284 {
1285         char * text_ptr, * next_row, * text_ptr1;
1286         int width, spacing,letter;
1287         int xx,yy;
1288         int orig_color=FG_COLOR;//to allow easy reseting to default string color with colored strings -MPM
1289
1290         next_row = s;
1291
1292         yy = y;
1293
1294         if (grd_curscreen->sc_canvas.cv_bitmap.bm_type != BM_OGL)
1295                 Error("carp.\n");
1296         while (next_row != NULL)
1297         {
1298                 text_ptr1 = next_row;
1299                 next_row = NULL;
1300
1301                 text_ptr = text_ptr1;
1302
1303                 xx = x;
1304
1305                 if (xx==0x8000)                 //centered
1306                         xx = get_centered_x(text_ptr);
1307
1308                 while (*text_ptr)
1309                 {
1310                         if (*text_ptr == '\n' )
1311                         {
1312                                 next_row = &text_ptr[1];
1313                                 yy += FHEIGHT;
1314                                 break;
1315                         }
1316
1317                         letter = (unsigned char)*text_ptr - FMINCHAR;
1318
1319                         get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1320
1321                         if (!INFONT(letter) || (unsigned char)*text_ptr <= 0x06)
1322                         {   //not in font, draw as space
1323                                 CHECK_EMBEDDED_COLORS() else{
1324                                         xx += spacing;
1325                                         text_ptr++;
1326                                 }
1327                                 continue;
1328                         }
1329                         
1330 //                      ogl_ubitblt(FONT->ft_bitmaps[letter].bm_w,FONT->ft_bitmaps[letter].bm_h,xx,yy,0,0,&FONT->ft_bitmaps[letter],NULL);
1331 //                      if (*text_ptr>='0' && *text_ptr<='9'){
1332                         if (FFLAGS&FT_COLOR)
1333                                 gr_ubitmapm(xx,yy,&FONT->ft_bitmaps[letter]);
1334                         else{
1335                                 if (grd_curcanv->cv_bitmap.bm_type==BM_OGL)
1336                                         ogl_ubitmapm_c(xx,yy,&FONT->ft_bitmaps[letter],FG_COLOR);
1337                                 else
1338                                         Error("ogl_internal_string: non-color string to non-ogl dest\n");
1339 //                                      gr_ubitmapm(xx,yy,&FONT->ft_bitmaps[letter]);//ignores color..
1340                         }
1341                         //}
1342
1343                         xx += spacing;
1344
1345                         text_ptr++;
1346                 }
1347
1348         }
1349         return 0;
1350 }
1351
1352 int gr_internal_color_string(int x, int y, char *s ){
1353         return ogl_internal_string(x,y,s);
1354 }
1355 #endif //OGL
1356
1357 int gr_string(int x, int y, char *s )
1358 {
1359         int w, h, aw;
1360         int clipped=0;
1361
1362         Assert(FONT != NULL);
1363
1364         if ( x == 0x8000 )      {
1365                 if ( y<0 ) clipped |= 1;
1366                 gr_get_string_size(s, &w, &h, &aw );
1367                 // for x, since this will be centered, only look at
1368                 // width.
1369                 if ( w > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
1370                 if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
1371
1372                 if ( (y+h) < 0 ) clipped |= 2;
1373                 if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
1374
1375         } else {
1376                 if ( (x<0) || (y<0) ) clipped |= 1;
1377                 gr_get_string_size(s, &w, &h, &aw );
1378                 if ( (x+w) > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
1379                 if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
1380                 if ( (x+w) < 0 ) clipped |= 2;
1381                 if ( (y+h) < 0 ) clipped |= 2;
1382                 if ( x > grd_curcanv->cv_bitmap.bm_w ) clipped |= 2;
1383                 if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
1384         }
1385
1386         if ( !clipped )
1387                 return gr_ustring(x, y, s );
1388
1389         if ( clipped & 2 )      {
1390                 // Completely clipped...
1391                 mprintf( (1, "Text '%s' at (%d,%d) is off screen!\n", s, x, y ));
1392                 return 0;
1393         }
1394
1395         if ( clipped & 1 )      {
1396                 // Partially clipped...
1397                 //mprintf( (0, "Text '%s' at (%d,%d) is getting clipped!\n", s, x, y ));
1398         }
1399
1400         // Partially clipped...
1401 #ifdef OGL
1402         if (TYPE==BM_OGL)
1403                 return ogl_internal_string(x,y,s);
1404 #endif
1405
1406         if (FFLAGS & FT_COLOR)
1407                 return gr_internal_color_string( x, y, s);
1408
1409         if ( BG_COLOR == -1)
1410                 return gr_internal_string_clipped_m( x, y, s );
1411
1412         return gr_internal_string_clipped( x, y, s );
1413 }
1414
1415 int gr_ustring(int x, int y, char *s )
1416 {
1417 #ifdef OGL
1418         if (TYPE==BM_OGL)
1419                 return ogl_internal_string(x,y,s);
1420 #endif
1421         
1422         if (FFLAGS & FT_COLOR) {
1423
1424                 return gr_internal_color_string(x,y,s);
1425
1426         }
1427         else
1428                 switch( TYPE )
1429                 {
1430                 case BM_LINEAR:
1431                         if ( BG_COLOR == -1)
1432                                 return gr_internal_string0m(x,y,s);
1433                         else
1434                                 return gr_internal_string0(x,y,s);
1435 #ifdef __MSDOS__
1436                 case BM_SVGA:
1437                         if ( BG_COLOR == -1)
1438                                 return gr_internal_string2m(x,y,s);
1439                         else
1440                                 return gr_internal_string2(x,y,s);
1441 #endif // __MSDOS__
1442 #if defined(POLY_ACC)
1443         case BM_LINEAR15:
1444                         if ( BG_COLOR == -1)
1445                                 return gr_internal_string5m(x,y,s);
1446                         else
1447                                 return gr_internal_string5(x,y,s);
1448 #endif
1449                 }
1450         return 0;
1451 }
1452
1453
1454 void gr_get_string_size(char *s, int *string_width, int *string_height, int *average_width )
1455 {
1456         int i = 0, longest_width = 0;
1457         int width,spacing;
1458
1459         *string_height = FHEIGHT;
1460         *string_width = 0;
1461         *average_width = FWIDTH;
1462
1463         if (s != NULL )
1464         {
1465                 *string_width = 0;
1466                 while (*s)
1467                 {
1468 //                      if (*s == CC_UNDERLINE)
1469 //                              s++;
1470                         while (*s == '\n')
1471                         {
1472                                 s++;
1473                                 *string_height += FHEIGHT;
1474                                 *string_width = 0;
1475                         }
1476
1477                         if (*s == 0) break;
1478
1479                         //      1 = next byte specifies color, so skip the 1 and the color value
1480                         if (*s == CC_COLOR)
1481                                 s += 2;
1482                         else if (*s == CC_LSPACING) {
1483                                 *string_height += *(s+1)-'0';
1484                                 s += 2;
1485                         } else {
1486                                 get_char_width(s[0],s[1],&width,&spacing);
1487
1488                                 *string_width += spacing;
1489
1490                                 if (*string_width > longest_width)
1491                                         longest_width = *string_width;
1492
1493                                 i++;
1494                                 s++;
1495                         }
1496                 }
1497         }
1498         *string_width = longest_width;
1499 }
1500
1501
1502 int gr_uprintf( int x, int y, char * format, ... )
1503 {
1504         char buffer[1000];
1505         va_list args;
1506
1507         va_start(args, format );
1508         vsprintf(buffer,format,args);
1509         return gr_ustring( x, y, buffer );
1510 }
1511
1512 int gr_printf( int x, int y, char * format, ... )
1513 {
1514         char buffer[1000];
1515         va_list args;
1516
1517         va_start(args, format );
1518         vsprintf(buffer,format,args);
1519         return gr_string( x, y, buffer );
1520 }
1521
1522 void gr_close_font( grs_font * font )
1523 {
1524         if (font)
1525         {
1526                 int fontnum;
1527                 char * font_data;
1528
1529                 //find font in list
1530                 for (fontnum=0;fontnum<MAX_OPEN_FONTS && open_font[fontnum].ptr!=font;fontnum++);
1531                 Assert(fontnum<MAX_OPEN_FONTS); //did we find slot?
1532
1533                 font_data = open_font[fontnum].dataptr;
1534                 d_free( font_data );
1535
1536                 open_font[fontnum].ptr = NULL;
1537                 open_font[fontnum].dataptr = NULL;
1538
1539                 if ( font->ft_chars )
1540                         d_free( font->ft_chars );
1541 #ifdef OGL
1542                 if (font->ft_bitmaps)
1543                         d_free( font->ft_bitmaps );
1544                 gr_free_bitmap_data(&font->ft_parent_bitmap);
1545 //              ogl_freebmtexture(&font->ft_parent_bitmap);
1546 #endif
1547                 d_free( font );
1548
1549
1550         }
1551 }
1552
1553 //remap (by re-reading) all the color fonts
1554 void gr_remap_color_fonts()
1555 {
1556         int fontnum;
1557
1558         for (fontnum=0;fontnum<MAX_OPEN_FONTS;fontnum++) {
1559                 grs_font *font;
1560
1561                 font = open_font[fontnum].ptr;
1562
1563                 if (font && (font->ft_flags & FT_COLOR))
1564                         gr_remap_font(font, open_font[fontnum].filename, open_font[fontnum].dataptr);
1565         }
1566 }
1567
1568 void gr_remap_mono_fonts()
1569 {
1570         int fontnum;
1571         con_printf (CON_DEBUG, "gr_remap_mono_fonts ()\n");
1572         for (fontnum=0;fontnum<MAX_OPEN_FONTS;fontnum++) {
1573                 grs_font *font;
1574                 font = open_font[fontnum].ptr;
1575                 if (font && !(font->ft_flags & FT_COLOR))
1576                         gr_remap_font(font, open_font[fontnum].filename, open_font[fontnum].dataptr);
1577         }
1578 }
1579
1580 #ifdef FAST_FILE_IO
1581 #define grs_font_read(gf, fp) cfread(gf, GRS_FONT_SIZE, 1, fp)
1582 #else
1583 /*
1584  * reads a grs_font structure from a CFILE
1585  */
1586 void grs_font_read(grs_font *gf, CFILE *fp)
1587 {
1588         gf->ft_w = cfile_read_short(fp);
1589         gf->ft_h = cfile_read_short(fp);
1590         gf->ft_flags = cfile_read_short(fp);
1591         gf->ft_baseline = cfile_read_short(fp);
1592         gf->ft_minchar = cfile_read_byte(fp);
1593         gf->ft_maxchar = cfile_read_byte(fp);
1594         gf->ft_bytewidth = cfile_read_short(fp);
1595         gf->ft_data = (ubyte *)cfile_read_int(fp);
1596         gf->ft_chars = (ubyte **)cfile_read_int(fp);
1597         gf->ft_widths = (short *)cfile_read_int(fp);
1598         gf->ft_kerndata = (ubyte *)cfile_read_int(fp);
1599 }
1600 #endif
1601
1602 grs_font * gr_init_font( char * fontname )
1603 {
1604         static int first_time=1;
1605         grs_font *font;
1606         char *font_data;
1607         int i,fontnum;
1608         unsigned char * ptr;
1609         int nchars;
1610         CFILE *fontfile;
1611         char file_id[4];
1612         int datasize;   //size up to (but not including) palette
1613
1614         if (first_time) {
1615                 int i;
1616                 for (i=0;i<MAX_OPEN_FONTS;i++)
1617                 {
1618                         open_font[i].ptr = NULL;
1619                         open_font[i].dataptr = NULL;
1620                 }
1621                 first_time=0;
1622         }
1623
1624         //find free font slot
1625         for (fontnum=0;fontnum<MAX_OPEN_FONTS && open_font[fontnum].ptr!=NULL;fontnum++);
1626         Assert(fontnum<MAX_OPEN_FONTS); //did we find one?
1627
1628         strncpy(open_font[fontnum].filename,fontname,FILENAME_LEN);
1629
1630         fontfile = cfopen(fontname, "rb");
1631
1632         if (!fontfile) {
1633                 con_printf(CON_VERBOSE, "Can't open font file %s\n", fontname);
1634                 return NULL;
1635         }
1636
1637         cfread(file_id, 4, 1, fontfile);
1638         if ( !strncmp( file_id, "NFSP", 4 ) ) {
1639                 con_printf(CON_NORMAL, "File %s is not a font file\n", fontname );
1640                 return NULL;
1641         }
1642
1643         datasize = cfile_read_int(fontfile);
1644         datasize -= GRS_FONT_SIZE; // subtract the size of the header.
1645
1646         MALLOC(font, grs_font, sizeof(grs_font));
1647         grs_font_read(font, fontfile);
1648
1649         MALLOC(font_data, char, datasize);
1650         cfread(font_data, 1, datasize, fontfile);
1651
1652         open_font[fontnum].ptr = font;
1653         open_font[fontnum].dataptr = font_data;
1654
1655         // make these offsets relative to font_data
1656         font->ft_data = (ubyte *)((int)font->ft_data - GRS_FONT_SIZE);
1657         font->ft_widths = (short *)((int)font->ft_widths - GRS_FONT_SIZE);
1658         font->ft_kerndata = (ubyte *)((int)font->ft_kerndata - GRS_FONT_SIZE);
1659
1660         nchars = font->ft_maxchar - font->ft_minchar + 1;
1661
1662         if (font->ft_flags & FT_PROPORTIONAL) {
1663
1664                 font->ft_widths = (short *) &font_data[(int)font->ft_widths];
1665                 font->ft_data = (unsigned char *) &font_data[(int)font->ft_data];
1666                 font->ft_chars = (unsigned char **)d_malloc( nchars * sizeof(unsigned char *));
1667
1668                 ptr = font->ft_data;
1669
1670                 for (i=0; i< nchars; i++ ) {
1671                         font->ft_widths[i] = INTEL_SHORT(font->ft_widths[i]);
1672                         font->ft_chars[i] = ptr;
1673                         if (font->ft_flags & FT_COLOR)
1674                                 ptr += font->ft_widths[i] * font->ft_h;
1675                         else
1676                                 ptr += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
1677                 }
1678
1679         } else  {
1680
1681                 font->ft_data   = (unsigned char *) font_data;
1682                 font->ft_chars  = NULL;
1683                 font->ft_widths = NULL;
1684
1685                 ptr = font->ft_data + (nchars * font->ft_w * font->ft_h);
1686         }
1687
1688         if (font->ft_flags & FT_KERNED)
1689                 font->ft_kerndata = (unsigned char *) &font_data[(int)font->ft_kerndata];
1690
1691         if (font->ft_flags & FT_COLOR) {                //remap palette
1692                 ubyte palette[256*3];
1693                 ubyte colormap[256];
1694                 int freq[256];
1695
1696                 cfread(palette,3,256,fontfile);         //read the palette
1697
1698 #ifdef SWAP_0_255                       // swap the first and last palette entries (black and white)
1699                 {
1700                         int i;
1701                         ubyte c;
1702
1703                         for (i = 0; i < 3; i++) {
1704                                 c = palette[i];
1705                                 palette[i] = palette[765+i];
1706                                 palette[765+i] = c;
1707                         }
1708
1709 //  we also need to swap the data entries as well.  black is white and white is black
1710
1711                         for (i = 0; i < ptr-font->ft_data; i++) {
1712                                 if (font->ft_data[i] == 0)
1713                                         font->ft_data[i] = 255;
1714                                 else if (font->ft_data[i] == 255)
1715                                         font->ft_data[i] = 0;
1716                         }
1717
1718                 }
1719 #endif
1720
1721                 build_colormap_good( (ubyte *)&palette, colormap, freq );
1722
1723                 colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR;              // changed from colormap[255] = 255 to this for macintosh
1724
1725                 decode_data_asm(font->ft_data, ptr - font->ft_data, colormap, freq );
1726
1727         }
1728
1729         cfclose(fontfile);
1730
1731         //set curcanv vars
1732
1733         FONT        = font;
1734         FG_COLOR    = 0;
1735         BG_COLOR    = 0;
1736
1737         {
1738                 int x,y,aw;
1739                 char tests[]="abcdefghij1234.A";
1740                 gr_get_string_size(tests,&x,&y,&aw);
1741 //              newfont->ft_aw=x/(float)strlen(tests);
1742         }
1743
1744 #ifdef OGL
1745         ogl_init_font(font);
1746 #endif
1747
1748         return font;
1749
1750 }
1751
1752 //remap a font by re-reading its data & palette
1753 void gr_remap_font( grs_font *font, char * fontname, char *font_data )
1754 {
1755         int i;
1756         int nchars;
1757         CFILE *fontfile;
1758         char file_id[4];
1759         int datasize;        //size up to (but not including) palette
1760         unsigned char *ptr;
1761
1762         if (! (font->ft_flags & FT_COLOR))
1763                 return;
1764
1765         fontfile = cfopen(fontname, "rb");
1766
1767         if (!fontfile)
1768                 Error( "Can't open font file %s", fontname );
1769
1770         cfread(file_id, 4, 1, fontfile);
1771         if ( !strncmp( file_id, "NFSP", 4 ) )
1772                 Error( "File %s is not a font file", fontname );
1773
1774         datasize = cfile_read_int(fontfile);
1775         datasize -= GRS_FONT_SIZE; // subtract the size of the header.
1776
1777         d_free(font->ft_chars);
1778         grs_font_read(font, fontfile); // have to reread in case mission hogfile overrides font.
1779
1780         cfread(font_data, 1, datasize, fontfile);  //read raw data
1781
1782         // make these offsets relative to font_data
1783         font->ft_data = (ubyte *)((int)font->ft_data - GRS_FONT_SIZE);
1784         font->ft_widths = (short *)((int)font->ft_widths - GRS_FONT_SIZE);
1785         font->ft_kerndata = (ubyte *)((int)font->ft_kerndata - GRS_FONT_SIZE);
1786
1787         nchars = font->ft_maxchar - font->ft_minchar + 1;
1788
1789         if (font->ft_flags & FT_PROPORTIONAL) {
1790
1791                 font->ft_widths = (short *) &font_data[(int)font->ft_widths];
1792                 font->ft_data = (unsigned char *) &font_data[(int)font->ft_data];
1793                 font->ft_chars = (unsigned char **)d_malloc( nchars * sizeof(unsigned char *));
1794
1795                 ptr = font->ft_data;
1796
1797                 for (i=0; i< nchars; i++ ) {
1798                         font->ft_widths[i] = INTEL_SHORT(font->ft_widths[i]);
1799                         font->ft_chars[i] = ptr;
1800                         if (font->ft_flags & FT_COLOR)
1801                                 ptr += font->ft_widths[i] * font->ft_h;
1802                         else
1803                                 ptr += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
1804                 }
1805
1806         } else  {
1807
1808                 font->ft_data   = (unsigned char *) font_data;
1809                 font->ft_chars  = NULL;
1810                 font->ft_widths = NULL;
1811
1812                 ptr = font->ft_data + (nchars * font->ft_w * font->ft_h);
1813         }
1814
1815         if (font->ft_flags & FT_KERNED)
1816                 font->ft_kerndata = (unsigned char *) &font_data[(int)font->ft_kerndata];
1817
1818         if (font->ft_flags & FT_COLOR) {                //remap palette
1819                 ubyte palette[256*3];
1820                 ubyte colormap[256];
1821                 int freq[256];
1822
1823                 cfread(palette,3,256,fontfile);         //read the palette
1824
1825 #ifdef SWAP_0_255                       // swap the first and last palette entries (black and white)
1826                 {
1827                         int i;
1828                         ubyte c;
1829
1830                         for (i = 0; i < 3; i++) {
1831                                 c = palette[i];
1832                                 palette[i] = palette[765+i];
1833                                 palette[765+i] = c;
1834                         }
1835
1836 //  we also need to swap the data entries as well.  black is white and white is black
1837
1838                         for (i = 0; i < ptr-font->ft_data; i++) {
1839                                 if (font->ft_data[i] == 0)
1840                                         font->ft_data[i] = 255;
1841                                 else if (font->ft_data[i] == 255)
1842                                         font->ft_data[i] = 0;
1843                         }
1844
1845                 }
1846 #endif
1847
1848                 build_colormap_good( (ubyte *)&palette, colormap, freq );
1849
1850                 colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR;              // changed from colormap[255] = 255 to this for macintosh
1851
1852                 decode_data_asm(font->ft_data, ptr - font->ft_data, colormap, freq );
1853
1854         }
1855
1856         cfclose(fontfile);
1857
1858 #ifdef OGL
1859         if (font->ft_bitmaps)
1860                 d_free( font->ft_bitmaps );
1861         gr_free_bitmap_data(&font->ft_parent_bitmap);
1862 //      ogl_freebmtexture(&font->ft_parent_bitmap);
1863
1864         ogl_init_font(font);
1865 #endif
1866 }
1867
1868
1869 void gr_set_fontcolor( int fg, int bg )
1870 {
1871         FG_COLOR    = fg;
1872         BG_COLOR    = bg;
1873 }
1874
1875 void gr_set_curfont( grs_font * new )
1876 {
1877         FONT = new;
1878 }
1879
1880
1881 int gr_internal_string_clipped(int x, int y, char *s )
1882 {
1883         unsigned char * fp;
1884         char * text_ptr, * next_row, * text_ptr1;
1885         int r, BitMask, i, bits, width, spacing, letter, underline;
1886         int x1 = x, last_x;
1887
1888         bits=0;
1889
1890         next_row = s;
1891
1892         while (next_row != NULL )
1893         {
1894                 text_ptr1 = next_row;
1895                 next_row = NULL;
1896
1897                 x = x1;
1898                 if (x==0x8000)                  //centered
1899                         x = get_centered_x(text_ptr1);
1900
1901                 last_x = x;
1902
1903                 for (r=0; r<FHEIGHT; r++)       {
1904                         text_ptr = text_ptr1;
1905                         x = last_x;
1906
1907                         while (*text_ptr)       {
1908                                 if (*text_ptr == '\n' ) {
1909                                         next_row = &text_ptr[1];
1910                                         break;
1911                                 }
1912
1913                                 if (*text_ptr == CC_COLOR) {
1914                                         FG_COLOR = *(text_ptr+1);
1915                                         text_ptr += 2;
1916                                         continue;
1917                                 }
1918
1919                                 if (*text_ptr == CC_LSPACING) {
1920                                         Int3(); //      Warning: skip lines not supported for clipped strings.
1921                                         text_ptr += 2;
1922                                         continue;
1923                                 }
1924
1925                                 underline = 0;
1926                                 if (*text_ptr == CC_UNDERLINE ) {
1927                                         if ((r==FBASELINE+2) || (r==FBASELINE+3))
1928                                                 underline = 1;
1929                                         text_ptr++;
1930                                 }
1931
1932                                 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1933
1934                                 letter = *text_ptr-FMINCHAR;
1935
1936                                 if (!INFONT(letter)) {  //not in font, draw as space
1937                                         x += spacing;
1938                                         text_ptr++;
1939                                         continue;
1940                                 }
1941
1942                                 if (FFLAGS & FT_PROPORTIONAL)
1943                                         fp = FCHARS[letter];
1944                                 else
1945                                         fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
1946
1947                                 if (underline)  {
1948                                         for (i=0; i< width; i++ )       {
1949                                                 gr_setcolor(FG_COLOR);
1950                                                 gr_pixel( x++, y );
1951                                         }
1952                                 } else {
1953                                         fp += BITS_TO_BYTES(width)*r;
1954
1955                                         BitMask = 0;
1956
1957                                         for (i=0; i< width; i++ )       {
1958                                                 if (BitMask==0) {
1959                                                         bits = *fp++;
1960                                                         BitMask = 0x80;
1961                                                 }
1962                                                 if (bits & BitMask)
1963                                                         gr_setcolor(FG_COLOR);
1964                                                 else
1965                                                         gr_setcolor(BG_COLOR);
1966                                                 gr_pixel( x++, y );
1967                                                 BitMask >>= 1;
1968                                         }
1969                                 }
1970
1971                                 x += spacing-width;             //for kerning
1972
1973                                 text_ptr++;
1974                         }
1975                         y++;
1976                 }
1977         }
1978         return 0;
1979 }
1980
1981 int gr_internal_string_clipped_m(int x, int y, char *s )
1982 {
1983         unsigned char * fp;
1984         char * text_ptr, * next_row, * text_ptr1;
1985         int r, BitMask, i, bits, width, spacing, letter, underline;
1986         int x1 = x, last_x;
1987
1988         bits=0;
1989
1990         next_row = s;
1991
1992         while (next_row != NULL )
1993         {
1994                 text_ptr1 = next_row;
1995                 next_row = NULL;
1996
1997                 x = x1;
1998                 if (x==0x8000)                  //centered
1999                         x = get_centered_x(text_ptr1);
2000
2001                 last_x = x;
2002
2003                 for (r=0; r<FHEIGHT; r++)       {
2004                         x = last_x;
2005
2006                         text_ptr = text_ptr1;
2007
2008                         while (*text_ptr)       {
2009                                 if (*text_ptr == '\n' ) {
2010                                         next_row = &text_ptr[1];
2011                                         break;
2012                                 }
2013
2014                                 if (*text_ptr == CC_COLOR) {
2015                                         FG_COLOR = *(text_ptr+1);
2016                                         text_ptr += 2;
2017                                         continue;
2018                                 }
2019
2020                                 if (*text_ptr == CC_LSPACING) {
2021                                         Int3(); //      Warning: skip lines not supported for clipped strings.
2022                                         text_ptr += 2;
2023                                         continue;
2024                                 }
2025
2026                                 underline = 0;
2027                                 if (*text_ptr == CC_UNDERLINE ) {
2028                                         if ((r==FBASELINE+2) || (r==FBASELINE+3))
2029                                                 underline = 1;
2030                                         text_ptr++;
2031                                 }
2032
2033                                 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
2034
2035                                 letter = *text_ptr-FMINCHAR;
2036
2037                                 if (!INFONT(letter)) {  //not in font, draw as space
2038                                         x += spacing;
2039                                         text_ptr++;
2040                                         continue;
2041                                 }
2042
2043                                 if (FFLAGS & FT_PROPORTIONAL)
2044                                         fp = FCHARS[letter];
2045                                 else
2046                                         fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
2047
2048                                 if (underline)  {
2049                                         for (i=0; i< width; i++ )       {
2050                                                 gr_setcolor(FG_COLOR);
2051                                                 gr_pixel( x++, y );
2052                                         }
2053                                 } else {
2054                                         fp += BITS_TO_BYTES(width)*r;
2055
2056                                         BitMask = 0;
2057
2058                                         for (i=0; i< width; i++ )       {
2059                                                 if (BitMask==0) {
2060                                                         bits = *fp++;
2061                                                         BitMask = 0x80;
2062                                                 }
2063                                                 if (bits & BitMask)     {
2064                                                         gr_setcolor(FG_COLOR);
2065                                                         gr_pixel( x++, y );
2066                                                 } else {
2067                                                         x++;
2068                                                 }
2069                                                 BitMask >>= 1;
2070                                         }
2071                                 }
2072
2073                                 x += spacing-width;             //for kerning
2074
2075                                 text_ptr++;
2076                         }
2077                         y++;
2078                 }
2079         }
2080         return 0;
2081 }