1 /* $Id: font.c,v 1.33 2004-11-25 11:51:15 btb Exp $ */
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.
17 * Graphical routines for drawing fonts.
30 #if !defined(_MSC_VER) && !defined(macintosh)
35 #include "pa_enabl.h" //$$POLY_ACC
53 #define MAX_OPEN_FONTS 50
54 #define FILENAME_LEN 13
56 typedef struct openfont {
57 char filename[FILENAME_LEN];
62 //list of open fonts, for use (for now) for palette remapping
63 openfont open_font[MAX_OPEN_FONTS];
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
78 #define BITS_TO_BYTES(x) (((x)+7)>>3)
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 );
83 ubyte *find_kern_entry(grs_font *font,ubyte first,ubyte second)
85 ubyte *p=font->ft_kerndata;
88 if (p[0]==first && p[1]==second)
96 //takes the character AFTER being offset into font
97 #define INFONT(_c) ((_c >= 0) && (_c <= FMAXCHAR-FMINCHAR))
99 //takes the character BEFORE being offset into current font
100 void get_char_width(ubyte c,ubyte c2,int *width,int *spacing)
106 if (!INFONT(letter)) { //not in font, draw as space
108 if (FFLAGS & FT_PROPORTIONAL)
115 if (FFLAGS & FT_PROPORTIONAL)
116 *width = FWIDTHS[letter];
122 if (FFLAGS & FT_KERNED) {
125 if (!(c2==0 || c2=='\n')) {
128 letter2 = c2-FMINCHAR;
130 if (INFONT(letter2)) {
132 p = find_kern_entry(FONT,(ubyte)letter,letter2);
141 int get_centered_x(char *s)
145 for (w=0;*s!=0 && *s!='\n';s++) {
149 continue;//skip color codes.
151 get_char_width(s[0],s[1],&w2,&s2);
155 return ((grd_curcanv->cv_bitmap.bm_w - w) / 2);
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)) { \
166 if (gr_message_color_level >= *(text_ptr-1)) \
167 FG_COLOR = *text_ptr - 1; \
171 else if ((*text_ptr >= 0x04) && (*text_ptr <= 0x06)){ \
172 if (gr_message_color_level >= *text_ptr - 3) \
173 FG_COLOR=orig_color; \
177 int gr_internal_string0(int x, int y, char *s )
180 char * text_ptr, * next_row, * text_ptr1;
181 int r, BitMask, i, bits, width, spacing, letter, underline;
184 unsigned int VideoOffset, VideoOffset1;
188 VideoOffset1 = y * ROWSIZE + x;
192 while (next_row != NULL )
194 text_ptr1 = next_row;
197 if (x==0x8000) { //centered
198 int xx = get_centered_x(text_ptr1);
199 VideoOffset1 = y * ROWSIZE + xx;
202 for (r=0; r<FHEIGHT; r++)
205 text_ptr = text_ptr1;
207 VideoOffset = VideoOffset1;
211 if (*text_ptr == '\n' )
213 next_row = &text_ptr[1];
217 if (*text_ptr == CC_COLOR) {
218 FG_COLOR = *(text_ptr+1);
223 if (*text_ptr == CC_LSPACING) {
224 skip_lines = *(text_ptr+1) - '0';
230 if (*text_ptr == CC_UNDERLINE )
232 if ((r==FBASELINE+2) || (r==FBASELINE+3))
237 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
239 letter = *text_ptr-FMINCHAR;
241 if (!INFONT(letter)) { //not in font, draw as space
242 VideoOffset += spacing;
247 if (FFLAGS & FT_PROPORTIONAL)
250 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
253 for (i=0; i< width; i++ )
254 DATA[VideoOffset++] = (unsigned char) FG_COLOR;
257 fp += BITS_TO_BYTES(width)*r;
261 for (i=0; i< width; i++ )
269 DATA[VideoOffset++] = (unsigned char) FG_COLOR;
271 DATA[VideoOffset++] = (unsigned char) BG_COLOR;
276 VideoOffset += spacing-width; //for kerning
281 VideoOffset1 += ROWSIZE; y++;
285 VideoOffset1 += ROWSIZE * skip_lines;
291 int gr_internal_string0m(int x, int y, char *s )
294 char * text_ptr, * next_row, * text_ptr1;
295 int r, BitMask, i, bits, width, spacing, letter, underline;
298 unsigned int VideoOffset, VideoOffset1;
300 int orig_color=FG_COLOR;//to allow easy reseting to default string color with colored strings -MPM
304 VideoOffset1 = y * ROWSIZE + x;
308 while (next_row != NULL )
310 text_ptr1 = next_row;
313 if (x==0x8000) { //centered
314 int xx = get_centered_x(text_ptr1);
315 VideoOffset1 = y * ROWSIZE + xx;
318 for (r=0; r<FHEIGHT; r++)
321 text_ptr = text_ptr1;
323 VideoOffset = VideoOffset1;
327 if (*text_ptr == '\n' )
329 next_row = &text_ptr[1];
333 if (*text_ptr == CC_COLOR) {
334 FG_COLOR = *(text_ptr+1);
339 if (*text_ptr == CC_LSPACING) {
340 skip_lines = *(text_ptr+1) - '0';
346 if (*text_ptr == CC_UNDERLINE )
348 if ((r==FBASELINE+2) || (r==FBASELINE+3))
353 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
355 letter = *text_ptr-FMINCHAR;
357 if (!INFONT(letter) || *text_ptr<=0x06) { //not in font, draw as space
358 CHECK_EMBEDDED_COLORS() else{
359 VideoOffset += spacing;
365 if (FFLAGS & FT_PROPORTIONAL)
368 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
371 for (i=0; i< width; i++ )
372 DATA[VideoOffset++] = (unsigned int) FG_COLOR;
375 fp += BITS_TO_BYTES(width)*r;
379 for (i=0; i< width; i++ )
387 DATA[VideoOffset++] = (unsigned int) FG_COLOR;
395 VideoOffset += spacing-width;
398 VideoOffset1 += ROWSIZE;
402 VideoOffset1 += ROWSIZE * skip_lines;
409 int gr_internal_string2(int x, int y, char *s )
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;
416 unsigned int VideoOffset, VideoOffset1;
418 VideoOffset1 = (size_t)DATA + y * ROWSIZE + x;
422 gr_vesa_setpage(VideoOffset1 >> 16);
424 VideoOffset1 &= 0xFFFF;
428 while (next_row != NULL )
430 text_ptr1 = next_row;
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;
440 for (r=0; r<FHEIGHT; r++)
442 text_ptr = text_ptr1;
444 VideoOffset = VideoOffset1;
450 if (*text_ptr == '\n' )
452 next_row = &text_ptr[1];
456 if (*text_ptr == CC_COLOR) {
457 FG_COLOR = *(text_ptr+1);
462 if (*text_ptr == CC_LSPACING) {
463 skip_lines = *(text_ptr+1) - '0';
469 if (*text_ptr == CC_UNDERLINE )
471 if ((r==FBASELINE+2) || (r==FBASELINE+3))
476 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
478 Assert(width==spacing); //no kerning support here
480 letter = *text_ptr-FMINCHAR;
482 if (!INFONT(letter)) { //not in font, draw as space
483 VideoOffset += spacing;
488 if (FFLAGS & FT_PROPORTIONAL)
491 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
495 if ( VideoOffset+width > 0xFFFF )
497 for (i=0; i< width; i++ )
499 gr_video_memory[VideoOffset++] = FG_COLOR;
501 if (VideoOffset > 0xFFFF )
503 VideoOffset -= 0xFFFF + 1;
511 for (i=0; i< width; i++ )
512 gr_video_memory[VideoOffset++] = FG_COLOR;
521 fp += BITS_TO_BYTES(width)*r;
525 if ( VideoOffset+width > 0xFFFF )
527 for (i=0; i< width; i++ )
535 gr_video_memory[VideoOffset++] = FG_COLOR;
537 gr_video_memory[VideoOffset++] = BG_COLOR;
541 if (VideoOffset > 0xFFFF )
543 VideoOffset -= 0xFFFF + 1;
555 if (bits & 0x80) gr_video_memory[VideoOffset+0] = FG_COLOR;
556 else gr_video_memory[VideoOffset+0] = BG_COLOR;
558 if (bits & 0x40) gr_video_memory[VideoOffset+1] = FG_COLOR;
559 else gr_video_memory[VideoOffset+1] = BG_COLOR;
561 if (bits & 0x20) gr_video_memory[VideoOffset+2] = FG_COLOR;
562 else gr_video_memory[VideoOffset+2] = BG_COLOR;
564 if (bits & 0x10) gr_video_memory[VideoOffset+3] = FG_COLOR;
565 else gr_video_memory[VideoOffset+3] = BG_COLOR;
567 if (bits & 0x08) gr_video_memory[VideoOffset+4] = FG_COLOR;
568 else gr_video_memory[VideoOffset+4] = BG_COLOR;
570 if (bits & 0x04) gr_video_memory[VideoOffset+5] = FG_COLOR;
571 else gr_video_memory[VideoOffset+5] = BG_COLOR;
573 if (bits & 0x02) gr_video_memory[VideoOffset+6] = FG_COLOR;
574 else gr_video_memory[VideoOffset+6] = BG_COLOR;
576 if (bits & 0x01) gr_video_memory[VideoOffset+7] = FG_COLOR;
577 else gr_video_memory[VideoOffset+7] = BG_COLOR;
581 for (i=0; i< width/2 ; i++ )
589 gr_video_memory[VideoOffset++] = FG_COLOR;
591 gr_video_memory[VideoOffset++] = BG_COLOR;
603 gr_video_memory[VideoOffset++] = FG_COLOR;
605 gr_video_memory[VideoOffset++] = BG_COLOR;
615 VideoOffset1 += ROWSIZE;
617 if (VideoOffset1 > 0xFFFF ) {
618 VideoOffset1 -= 0xFFFF + 1;
625 VideoOffset1 += ROWSIZE * skip_lines;
631 int gr_internal_string2m(int x, int y, char *s )
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;
638 unsigned int VideoOffset, VideoOffset1;
640 VideoOffset1 = (size_t)DATA + y * ROWSIZE + x;
642 gr_vesa_setpage(VideoOffset1 >> 16);
644 VideoOffset1 &= 0xFFFF;
648 while (next_row != NULL )
650 text_ptr1 = next_row;
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;
660 for (r=0; r<FHEIGHT; r++)
662 text_ptr = text_ptr1;
664 VideoOffset = VideoOffset1;
670 if (*text_ptr == '\n' )
672 next_row = &text_ptr[1];
676 if (*text_ptr == CC_COLOR) {
677 FG_COLOR = *(text_ptr+1);
682 if (*text_ptr == CC_LSPACING) {
683 skip_lines = *(text_ptr+1) - '0';
689 if (*text_ptr == CC_UNDERLINE )
691 if ((r==FBASELINE+2) || (r==FBASELINE+3))
696 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
698 letter = *text_ptr-FMINCHAR;
700 if (!INFONT(letter)) { //not in font, draw as space
701 VideoOffset += width;
706 if (FFLAGS & FT_PROPORTIONAL)
709 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
713 if ( VideoOffset+width > 0xFFFF )
715 for (i=0; i< width; i++ )
717 gr_video_memory[VideoOffset++] = FG_COLOR;
719 if (VideoOffset > 0xFFFF )
721 VideoOffset -= 0xFFFF + 1;
729 for (i=0; i< width; i++ )
730 gr_video_memory[VideoOffset++] = FG_COLOR;
735 fp += BITS_TO_BYTES(width)*r;
739 if ( VideoOffset+width > 0xFFFF )
741 for (i=0; i< width; i++ )
749 gr_video_memory[VideoOffset++] = FG_COLOR;
755 if (VideoOffset > 0xFFFF )
757 VideoOffset -= 0xFFFF + 1;
764 for (i=0; i< width; i++ )
772 gr_video_memory[VideoOffset++] = FG_COLOR;
781 VideoOffset += spacing-width;
785 VideoOffset1 += ROWSIZE;
787 if (VideoOffset1 > 0xFFFF ) {
788 VideoOffset1 -= 0xFFFF + 1;
795 VideoOffset1 += ROWSIZE * skip_lines;
803 #if defined(POLY_ACC)
804 int gr_internal_string5(int x, int y, char *s )
807 ubyte * text_ptr, * next_row, * text_ptr1;
808 int r, BitMask, i, bits, width, spacing, letter, underline;
811 unsigned int VideoOffset, VideoOffset1;
814 VideoOffset1 = y * ROWSIZE + x * PA_BPP;
818 while (next_row != NULL )
820 text_ptr1 = next_row;
823 if (x==0x8000) { //centered
824 int xx = get_centered_x(text_ptr1);
825 VideoOffset1 = y * ROWSIZE + xx * PA_BPP;
828 for (r=0; r<FHEIGHT; r++)
831 text_ptr = text_ptr1;
833 VideoOffset = VideoOffset1;
837 if (*text_ptr == '\n' )
839 next_row = &text_ptr[1];
843 if (*text_ptr == CC_COLOR) {
844 FG_COLOR = *(text_ptr+1);
849 if (*text_ptr == CC_LSPACING) {
850 skip_lines = *(text_ptr+1) - '0';
856 if (*text_ptr == CC_UNDERLINE )
858 if ((r==FBASELINE+2) || (r==FBASELINE+3))
863 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
865 letter = *text_ptr-FMINCHAR;
867 if (!INFONT(letter)) { //not in font, draw as space
868 VideoOffset += spacing * PA_BPP;
873 if (FFLAGS & FT_PROPORTIONAL)
876 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
879 for (i=0; i< width; i++ )
880 { *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
883 fp += BITS_TO_BYTES(width)*r;
887 for (i=0; i< width; i++ )
895 { *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
897 { *(short *)(DATA + VideoOffset) = pa_clut[BG_COLOR]; VideoOffset += PA_BPP; }
902 VideoOffset += PA_BPP * (spacing-width); //for kerning
907 VideoOffset1 += ROWSIZE; y++;
911 VideoOffset1 += ROWSIZE * skip_lines;
917 int gr_internal_string5m(int x, int y, char *s )
920 ubyte * text_ptr, * next_row, * text_ptr1;
921 int r, BitMask, i, bits, width, spacing, letter, underline;
924 unsigned int VideoOffset, VideoOffset1;
927 VideoOffset1 = y * ROWSIZE + x * PA_BPP;
931 while (next_row != NULL )
933 text_ptr1 = next_row;
936 if (x==0x8000) { //centered
937 int xx = get_centered_x(text_ptr1);
938 VideoOffset1 = y * ROWSIZE + xx * PA_BPP;
941 for (r=0; r<FHEIGHT; r++)
944 text_ptr = text_ptr1;
946 VideoOffset = VideoOffset1;
950 if (*text_ptr == '\n' )
952 next_row = &text_ptr[1];
956 if (*text_ptr == CC_COLOR) {
957 FG_COLOR = *(text_ptr+1);
962 if (*text_ptr == CC_LSPACING) {
963 skip_lines = *(text_ptr+1) - '0';
969 if (*text_ptr == CC_UNDERLINE )
971 if ((r==FBASELINE+2) || (r==FBASELINE+3))
976 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
978 letter = *text_ptr-FMINCHAR;
980 if (!INFONT(letter)) { //not in font, draw as space
981 VideoOffset += spacing * PA_BPP;
986 if (FFLAGS & FT_PROPORTIONAL)
989 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
992 for (i=0; i< width; i++ )
993 { *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
996 fp += BITS_TO_BYTES(width)*r;
1000 for (i=0; i< width; i++ )
1008 { *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
1010 VideoOffset += PA_BPP;
1016 VideoOffset += PA_BPP * (spacing-width);
1019 VideoOffset1 += ROWSIZE; y++;
1022 VideoOffset1 += ROWSIZE * skip_lines;
1030 //a bitmap for the character
1031 grs_bitmap char_bm = {
1034 BM_FLAG_TRANSPARENT, //flags
1037 #ifdef BITMAP_SELECTOR
1044 int gr_internal_color_string(int x, int y, char *s )
1047 ubyte * text_ptr, * next_row, * text_ptr1;
1048 int width, spacing,letter;
1051 char_bm.bm_h = FHEIGHT; //set height for chars of this font
1058 while (next_row != NULL)
1060 text_ptr1 = next_row;
1063 text_ptr = text_ptr1;
1067 if (xx==0x8000) //centered
1068 xx = get_centered_x(text_ptr);
1072 if (*text_ptr == '\n' )
1074 next_row = &text_ptr[1];
1079 letter = *text_ptr-FMINCHAR;
1081 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1083 if (!INFONT(letter)) { //not in font, draw as space
1089 if (FFLAGS & FT_PROPORTIONAL)
1090 fp = FCHARS[letter];
1092 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
1094 gr_init_bitmap (&char_bm, BM_LINEAR, 0, 0, width, FHEIGHT, width, fp);
1095 gr_bitmapm(xx,yy,&char_bm);
1108 #include "ogl_init.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
1112 int pow2ize(int x);//from ogl.c
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)
1120 w+=font->ft_widths[i];
1124 return font->ft_w*(font->ft_maxchar-font->ft_minchar+1);
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;
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);
1142 mprintf((0,"failed to fit (%ix%i, %ic)\n",w,h,nc));
1147 while(y+font->ft_h<=h){
1152 if (font->ft_flags & FT_PROPORTIONAL){
1153 if (x+font->ft_widths[nc]+gap>w)break;
1154 x+=font->ft_widths[nc++]+gap;
1156 if (x+font->ft_w+gap>w)break;
1170 mprintf((0,"fit: %ix%i %i tries\n",w,h,tries));
1172 if (w*h==smallest){//this gives squarer sizes priority (ie, 128x128 would be better than 512*32)
1193 Error("couldn't fit font?\n");
1194 mprintf((0,"using %ix%i\n",*rw,*rh));
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;
1203 // char data[32*32*4];
1205 int gap=0;//having a gap just wastes ram, since we don't filter text textures at all.
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);
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.
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));
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];
1230 // mprintf((0,"char %i(%ix%i): ",i,w,h));
1232 mprintf((0,"grr\n"));continue;
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];
1244 fp = font->ft_data + i * w*h;
1247 font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=fp[x+y*w];
1250 // gr_init_bitmap(&font->ft_bitmaps[i],BM_LINEAR,0,0,w,h,w,font->);
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];
1258 fp = font->ft_data + i * BITS_TO_BYTES(w)*h;
1261 for (x=0; x< w; x++ )
1269 font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=white;
1271 font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=255;
1276 gr_init_sub_bitmap(&font->ft_bitmaps[i],&font->ft_parent_bitmap,curx,cury,w,h);
1280 ogl_loadbmtexture_f(&font->ft_parent_bitmap, oglflags);
1283 int ogl_internal_string(int x, int y, char *s )
1285 char * text_ptr, * next_row, * text_ptr1;
1286 int width, spacing,letter;
1288 int orig_color=FG_COLOR;//to allow easy reseting to default string color with colored strings -MPM
1294 if (grd_curscreen->sc_canvas.cv_bitmap.bm_type != BM_OGL)
1296 while (next_row != NULL)
1298 text_ptr1 = next_row;
1301 text_ptr = text_ptr1;
1305 if (xx==0x8000) //centered
1306 xx = get_centered_x(text_ptr);
1310 if (*text_ptr == '\n' )
1312 next_row = &text_ptr[1];
1317 letter = (unsigned char)*text_ptr - FMINCHAR;
1319 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1321 if (!INFONT(letter) || (unsigned char)*text_ptr <= 0x06)
1322 { //not in font, draw as space
1323 CHECK_EMBEDDED_COLORS() else{
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]);
1335 if (grd_curcanv->cv_bitmap.bm_type==BM_OGL)
1336 ogl_ubitmapm_c(xx,yy,&FONT->ft_bitmaps[letter],FG_COLOR);
1338 Error("ogl_internal_string: non-color string to non-ogl dest\n");
1339 // gr_ubitmapm(xx,yy,&FONT->ft_bitmaps[letter]);//ignores color..
1352 int gr_internal_color_string(int x, int y, char *s ){
1353 return ogl_internal_string(x,y,s);
1357 int gr_string(int x, int y, char *s )
1362 Assert(FONT != NULL);
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
1369 if ( w > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
1370 if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
1372 if ( (y+h) < 0 ) clipped |= 2;
1373 if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
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;
1387 return gr_ustring(x, y, s );
1389 if ( clipped & 2 ) {
1390 // Completely clipped...
1391 mprintf( (1, "Text '%s' at (%d,%d) is off screen!\n", s, x, y ));
1395 if ( clipped & 1 ) {
1396 // Partially clipped...
1397 //mprintf( (0, "Text '%s' at (%d,%d) is getting clipped!\n", s, x, y ));
1400 // Partially clipped...
1403 return ogl_internal_string(x,y,s);
1406 if (FFLAGS & FT_COLOR)
1407 return gr_internal_color_string( x, y, s);
1409 if ( BG_COLOR == -1)
1410 return gr_internal_string_clipped_m( x, y, s );
1412 return gr_internal_string_clipped( x, y, s );
1415 int gr_ustring(int x, int y, char *s )
1419 return ogl_internal_string(x,y,s);
1422 if (FFLAGS & FT_COLOR) {
1424 return gr_internal_color_string(x,y,s);
1431 if ( BG_COLOR == -1)
1432 return gr_internal_string0m(x,y,s);
1434 return gr_internal_string0(x,y,s);
1437 if ( BG_COLOR == -1)
1438 return gr_internal_string2m(x,y,s);
1440 return gr_internal_string2(x,y,s);
1442 #if defined(POLY_ACC)
1444 if ( BG_COLOR == -1)
1445 return gr_internal_string5m(x,y,s);
1447 return gr_internal_string5(x,y,s);
1454 void gr_get_string_size(char *s, int *string_width, int *string_height, int *average_width )
1456 int i = 0, longest_width = 0;
1459 *string_height = FHEIGHT;
1461 *average_width = FWIDTH;
1468 // if (*s == CC_UNDERLINE)
1473 *string_height += FHEIGHT;
1479 // 1 = next byte specifies color, so skip the 1 and the color value
1482 else if (*s == CC_LSPACING) {
1483 *string_height += *(s+1)-'0';
1486 get_char_width(s[0],s[1],&width,&spacing);
1488 *string_width += spacing;
1490 if (*string_width > longest_width)
1491 longest_width = *string_width;
1498 *string_width = longest_width;
1502 int gr_uprintf( int x, int y, char * format, ... )
1507 va_start(args, format );
1508 vsprintf(buffer,format,args);
1509 return gr_ustring( x, y, buffer );
1512 int gr_printf( int x, int y, char * format, ... )
1517 va_start(args, format );
1518 vsprintf(buffer,format,args);
1519 return gr_string( x, y, buffer );
1522 void gr_close_font( grs_font * font )
1530 for (fontnum=0;fontnum<MAX_OPEN_FONTS && open_font[fontnum].ptr!=font;fontnum++);
1531 Assert(fontnum<MAX_OPEN_FONTS); //did we find slot?
1533 font_data = open_font[fontnum].dataptr;
1534 d_free( font_data );
1536 open_font[fontnum].ptr = NULL;
1537 open_font[fontnum].dataptr = NULL;
1539 if ( font->ft_chars )
1540 d_free( font->ft_chars );
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);
1553 //remap (by re-reading) all the color fonts
1554 void gr_remap_color_fonts()
1558 for (fontnum=0;fontnum<MAX_OPEN_FONTS;fontnum++) {
1561 font = open_font[fontnum].ptr;
1563 if (font && (font->ft_flags & FT_COLOR))
1564 gr_remap_font(font, open_font[fontnum].filename, open_font[fontnum].dataptr);
1568 void gr_remap_mono_fonts()
1571 con_printf (CON_DEBUG, "gr_remap_mono_fonts ()\n");
1572 for (fontnum=0;fontnum<MAX_OPEN_FONTS;fontnum++) {
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);
1581 #define grs_font_read(gf, fp) cfread(gf, GRS_FONT_SIZE, 1, fp)
1584 * reads a grs_font structure from a CFILE
1586 void grs_font_read(grs_font *gf, CFILE *fp)
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);
1602 grs_font * gr_init_font( char * fontname )
1604 static int first_time=1;
1608 unsigned char * ptr;
1612 int datasize; //size up to (but not including) palette
1616 for (i=0;i<MAX_OPEN_FONTS;i++)
1617 open_font[i].ptr = NULL;
1618 open_font[i].dataptr = NULL;
1622 //find free font slot
1623 for (fontnum=0;fontnum<MAX_OPEN_FONTS && open_font[fontnum].ptr!=NULL;fontnum++);
1624 Assert(fontnum<MAX_OPEN_FONTS); //did we find one?
1626 strncpy(open_font[fontnum].filename,fontname,FILENAME_LEN);
1628 fontfile = cfopen(fontname, "rb");
1631 con_printf(CON_VERBOSE, "Can't open font file %s\n", fontname);
1635 cfread(file_id, 4, 1, fontfile);
1636 if ( !strncmp( file_id, "NFSP", 4 ) ) {
1637 con_printf(CON_NORMAL, "File %s is not a font file\n", fontname );
1641 datasize = cfile_read_int(fontfile);
1642 datasize -= GRS_FONT_SIZE; // subtract the size of the header.
1644 MALLOC(font, grs_font, sizeof(grs_font));
1645 grs_font_read(font, fontfile);
1647 MALLOC(font_data, char, datasize);
1648 cfread(font_data, 1, datasize, fontfile);
1650 open_font[fontnum].ptr = font;
1651 open_font[fontnum].dataptr = font_data;
1653 // make these offsets relative to font_data
1654 font->ft_data = (ubyte *)((int)font->ft_data - GRS_FONT_SIZE);
1655 font->ft_widths = (short *)((int)font->ft_widths - GRS_FONT_SIZE);
1656 font->ft_kerndata = (ubyte *)((int)font->ft_kerndata - GRS_FONT_SIZE);
1658 nchars = font->ft_maxchar - font->ft_minchar + 1;
1660 if (font->ft_flags & FT_PROPORTIONAL) {
1662 font->ft_widths = (short *) &font_data[(int)font->ft_widths];
1663 font->ft_data = (unsigned char *) &font_data[(int)font->ft_data];
1664 font->ft_chars = (unsigned char **)d_malloc( nchars * sizeof(unsigned char *));
1666 ptr = font->ft_data;
1668 for (i=0; i< nchars; i++ ) {
1669 font->ft_widths[i] = INTEL_SHORT(font->ft_widths[i]);
1670 font->ft_chars[i] = ptr;
1671 if (font->ft_flags & FT_COLOR)
1672 ptr += font->ft_widths[i] * font->ft_h;
1674 ptr += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
1679 font->ft_data = (unsigned char *) font_data;
1680 font->ft_chars = NULL;
1681 font->ft_widths = NULL;
1683 ptr = font->ft_data + (nchars * font->ft_w * font->ft_h);
1686 if (font->ft_flags & FT_KERNED)
1687 font->ft_kerndata = (unsigned char *) &font_data[(int)font->ft_kerndata];
1689 if (font->ft_flags & FT_COLOR) { //remap palette
1690 ubyte palette[256*3];
1691 ubyte colormap[256];
1694 cfread(palette,3,256,fontfile); //read the palette
1696 #ifdef SWAP_0_255 // swap the first and last palette entries (black and white)
1701 for (i = 0; i < 3; i++) {
1703 palette[i] = palette[765+i];
1707 // we also need to swap the data entries as well. black is white and white is black
1709 for (i = 0; i < ptr-font->ft_data; i++) {
1710 if (font->ft_data[i] == 0)
1711 font->ft_data[i] = 255;
1712 else if (font->ft_data[i] == 255)
1713 font->ft_data[i] = 0;
1719 build_colormap_good( (ubyte *)&palette, colormap, freq );
1721 colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR; // changed from colormap[255] = 255 to this for macintosh
1723 decode_data_asm(font->ft_data, ptr - font->ft_data, colormap, freq );
1737 char tests[]="abcdefghij1234.A";
1738 gr_get_string_size(tests,&x,&y,&aw);
1739 // newfont->ft_aw=x/(float)strlen(tests);
1743 ogl_init_font(font);
1750 //remap a font by re-reading its data & palette
1751 void gr_remap_font( grs_font *font, char * fontname, char *font_data )
1757 int datasize; //size up to (but not including) palette
1760 if (! (font->ft_flags & FT_COLOR))
1763 fontfile = cfopen(fontname, "rb");
1766 Error( "Can't open font file %s", fontname );
1768 cfread(file_id, 4, 1, fontfile);
1769 if ( !strncmp( file_id, "NFSP", 4 ) )
1770 Error( "File %s is not a font file", fontname );
1772 datasize = cfile_read_int(fontfile);
1773 datasize -= GRS_FONT_SIZE; // subtract the size of the header.
1775 d_free(font->ft_chars);
1776 grs_font_read(font, fontfile); // have to reread in case mission hogfile overrides font.
1778 cfread(font_data, 1, datasize, fontfile); //read raw data
1780 // make these offsets relative to font_data
1781 font->ft_data = (ubyte *)((int)font->ft_data - GRS_FONT_SIZE);
1782 font->ft_widths = (short *)((int)font->ft_widths - GRS_FONT_SIZE);
1783 font->ft_kerndata = (ubyte *)((int)font->ft_kerndata - GRS_FONT_SIZE);
1785 nchars = font->ft_maxchar - font->ft_minchar + 1;
1787 if (font->ft_flags & FT_PROPORTIONAL) {
1789 font->ft_widths = (short *) &font_data[(int)font->ft_widths];
1790 font->ft_data = (unsigned char *) &font_data[(int)font->ft_data];
1791 font->ft_chars = (unsigned char **)d_malloc( nchars * sizeof(unsigned char *));
1793 ptr = font->ft_data;
1795 for (i=0; i< nchars; i++ ) {
1796 font->ft_widths[i] = INTEL_SHORT(font->ft_widths[i]);
1797 font->ft_chars[i] = ptr;
1798 if (font->ft_flags & FT_COLOR)
1799 ptr += font->ft_widths[i] * font->ft_h;
1801 ptr += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
1806 font->ft_data = (unsigned char *) font_data;
1807 font->ft_chars = NULL;
1808 font->ft_widths = NULL;
1810 ptr = font->ft_data + (nchars * font->ft_w * font->ft_h);
1813 if (font->ft_flags & FT_KERNED)
1814 font->ft_kerndata = (unsigned char *) &font_data[(int)font->ft_kerndata];
1816 if (font->ft_flags & FT_COLOR) { //remap palette
1817 ubyte palette[256*3];
1818 ubyte colormap[256];
1821 cfread(palette,3,256,fontfile); //read the palette
1823 #ifdef SWAP_0_255 // swap the first and last palette entries (black and white)
1828 for (i = 0; i < 3; i++) {
1830 palette[i] = palette[765+i];
1834 // we also need to swap the data entries as well. black is white and white is black
1836 for (i = 0; i < ptr-font->ft_data; i++) {
1837 if (font->ft_data[i] == 0)
1838 font->ft_data[i] = 255;
1839 else if (font->ft_data[i] == 255)
1840 font->ft_data[i] = 0;
1846 build_colormap_good( (ubyte *)&palette, colormap, freq );
1848 colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR; // changed from colormap[255] = 255 to this for macintosh
1850 decode_data_asm(font->ft_data, ptr - font->ft_data, colormap, freq );
1857 if (font->ft_bitmaps)
1858 d_free( font->ft_bitmaps );
1859 gr_free_bitmap_data(&font->ft_parent_bitmap);
1860 // ogl_freebmtexture(&font->ft_parent_bitmap);
1862 ogl_init_font(font);
1867 void gr_set_fontcolor( int fg, int bg )
1873 void gr_set_curfont( grs_font * new )
1879 int gr_internal_string_clipped(int x, int y, char *s )
1882 char * text_ptr, * next_row, * text_ptr1;
1883 int r, BitMask, i, bits, width, spacing, letter, underline;
1890 while (next_row != NULL )
1892 text_ptr1 = next_row;
1896 if (x==0x8000) //centered
1897 x = get_centered_x(text_ptr1);
1901 for (r=0; r<FHEIGHT; r++) {
1902 text_ptr = text_ptr1;
1906 if (*text_ptr == '\n' ) {
1907 next_row = &text_ptr[1];
1911 if (*text_ptr == CC_COLOR) {
1912 FG_COLOR = *(text_ptr+1);
1917 if (*text_ptr == CC_LSPACING) {
1918 Int3(); // Warning: skip lines not supported for clipped strings.
1924 if (*text_ptr == CC_UNDERLINE ) {
1925 if ((r==FBASELINE+2) || (r==FBASELINE+3))
1930 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1932 letter = *text_ptr-FMINCHAR;
1934 if (!INFONT(letter)) { //not in font, draw as space
1940 if (FFLAGS & FT_PROPORTIONAL)
1941 fp = FCHARS[letter];
1943 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
1946 for (i=0; i< width; i++ ) {
1947 gr_setcolor(FG_COLOR);
1951 fp += BITS_TO_BYTES(width)*r;
1955 for (i=0; i< width; i++ ) {
1961 gr_setcolor(FG_COLOR);
1963 gr_setcolor(BG_COLOR);
1969 x += spacing-width; //for kerning
1979 int gr_internal_string_clipped_m(int x, int y, char *s )
1982 char * text_ptr, * next_row, * text_ptr1;
1983 int r, BitMask, i, bits, width, spacing, letter, underline;
1990 while (next_row != NULL )
1992 text_ptr1 = next_row;
1996 if (x==0x8000) //centered
1997 x = get_centered_x(text_ptr1);
2001 for (r=0; r<FHEIGHT; r++) {
2004 text_ptr = text_ptr1;
2007 if (*text_ptr == '\n' ) {
2008 next_row = &text_ptr[1];
2012 if (*text_ptr == CC_COLOR) {
2013 FG_COLOR = *(text_ptr+1);
2018 if (*text_ptr == CC_LSPACING) {
2019 Int3(); // Warning: skip lines not supported for clipped strings.
2025 if (*text_ptr == CC_UNDERLINE ) {
2026 if ((r==FBASELINE+2) || (r==FBASELINE+3))
2031 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
2033 letter = *text_ptr-FMINCHAR;
2035 if (!INFONT(letter)) { //not in font, draw as space
2041 if (FFLAGS & FT_PROPORTIONAL)
2042 fp = FCHARS[letter];
2044 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
2047 for (i=0; i< width; i++ ) {
2048 gr_setcolor(FG_COLOR);
2052 fp += BITS_TO_BYTES(width)*r;
2056 for (i=0; i< width; i++ ) {
2061 if (bits & BitMask) {
2062 gr_setcolor(FG_COLOR);
2071 x += spacing-width; //for kerning