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