1 /* $Id: font.c,v 1.28 2004-05-22 08:10:26 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.
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 ubyte * 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 ubyte * 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 nchars = font->ft_maxchar-font->ft_minchar+1;
1200 int i,w,h,tw,th,x,y,curx=0,cury=0;
1202 // char data[32*32*4];
1204 int gap=0;//having a gap just wastes ram, since we don't filter text textures at all.
1206 ogl_font_choose_size(font,gap,&tw,&th);
1207 data=d_malloc(tw*th);
1208 memset(data, 0, tw * th);
1209 gr_init_bitmap(&font->ft_parent_bitmap,BM_LINEAR,0,0,tw,th,tw,data);
1211 font->ft_parent_bitmap.gltexture=ogl_get_free_texture();
1213 font->ft_bitmaps=(grs_bitmap*)d_malloc( nchars * sizeof(grs_bitmap));
1214 mprintf((0,"ogl_init_font %s, %s, nchars=%i, (%ix%i tex)\n",(font->ft_flags & FT_PROPORTIONAL)?"proportional":"fixedwidth",(font->ft_flags & FT_COLOR)?"color":"mono",nchars,tw,th));
1219 for(i=0;i<nchars;i++){
1220 // s[0]=font->ft_minchar+i;
1221 // gr_get_string_size(s,&w,&h,&aw);
1222 if (font->ft_flags & FT_PROPORTIONAL)
1223 w=font->ft_widths[i];
1226 // mprintf((0,"char %i(%ix%i): ",i,w,h));
1228 mprintf((0,"grr\n"));continue;
1235 Error("font doesn't really fit (%i/%i)?\n",i,nchars);
1236 if (font->ft_flags & FT_COLOR) {
1237 if (font->ft_flags & FT_PROPORTIONAL)
1238 fp = font->ft_chars[i];
1240 fp = font->ft_data + i * w*h;
1243 font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=fp[x+y*w];
1246 // gr_init_bitmap(&font->ft_bitmaps[i],BM_LINEAR,0,0,w,h,w,font->);
1248 int BitMask,bits=0,white=gr_find_closest_color(63,63,63);
1249 // if (w*h>sizeof(data))
1250 // Error("ogl_init_font: toobig\n");
1251 if (font->ft_flags & FT_PROPORTIONAL)
1252 fp = font->ft_chars[i];
1254 fp = font->ft_data + i * BITS_TO_BYTES(w)*h;
1257 for (x=0; x< w; x++ )
1265 font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=white;
1267 font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=255;
1272 gr_init_sub_bitmap(&font->ft_bitmaps[i],&font->ft_parent_bitmap,curx,cury,w,h);
1276 if (!(font->ft_flags & FT_COLOR)) {
1277 //use GL_INTENSITY instead of GL_RGB
1278 if (ogl_intensity4_ok){
1279 font->ft_parent_bitmap.gltexture->internalformat=GL_INTENSITY4;
1280 font->ft_parent_bitmap.gltexture->format=GL_LUMINANCE;
1281 }else if (ogl_luminance4_alpha4_ok){
1282 font->ft_parent_bitmap.gltexture->internalformat=GL_LUMINANCE4_ALPHA4;
1283 font->ft_parent_bitmap.gltexture->format=GL_LUMINANCE_ALPHA;
1284 }else if (ogl_rgba2_ok){
1285 font->ft_parent_bitmap.gltexture->internalformat=GL_RGBA2;
1286 font->ft_parent_bitmap.gltexture->format=GL_RGBA;
1288 font->ft_parent_bitmap.gltexture->internalformat=ogl_rgba_format;
1289 font->ft_parent_bitmap.gltexture->format=GL_RGBA;
1292 ogl_loadbmtexture_m(&font->ft_parent_bitmap,0);
1295 int ogl_internal_string(int x, int y, char *s )
1297 ubyte * text_ptr, * next_row, * text_ptr1;
1298 int width, spacing,letter;
1300 int orig_color=FG_COLOR;//to allow easy reseting to default string color with colored strings -MPM
1306 if (grd_curscreen->sc_canvas.cv_bitmap.bm_type != BM_OGL)
1308 while (next_row != NULL)
1310 text_ptr1 = next_row;
1313 text_ptr = text_ptr1;
1317 if (xx==0x8000) //centered
1318 xx = get_centered_x(text_ptr);
1322 if (*text_ptr == '\n' )
1324 next_row = &text_ptr[1];
1329 letter = *text_ptr-FMINCHAR;
1331 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1333 if (!INFONT(letter) || *text_ptr<=0x06) { //not in font, draw as space
1334 CHECK_EMBEDDED_COLORS() else{
1341 // ogl_ubitblt(FONT->ft_bitmaps[letter].bm_w,FONT->ft_bitmaps[letter].bm_h,xx,yy,0,0,&FONT->ft_bitmaps[letter],NULL);
1342 // if (*text_ptr>='0' && *text_ptr<='9'){
1343 if (FFLAGS&FT_COLOR)
1344 //gr_ubitmapm(xx,yy,&FONT->ft_bitmaps[letter]);
1345 gr_bitmapm(xx,yy,&FONT->ft_bitmaps[letter]); // credits need clipping
1347 if (grd_curcanv->cv_bitmap.bm_type==BM_OGL)
1348 ogl_ubitmapm_c(xx,yy,&FONT->ft_bitmaps[letter],FG_COLOR);
1350 Error("ogl_internal_string: non-color string to non-ogl dest\n");
1351 // gr_ubitmapm(xx,yy,&FONT->ft_bitmaps[letter]);//ignores color..
1364 int gr_internal_color_string(int x, int y, char *s ){
1365 return ogl_internal_string(x,y,s);
1369 int gr_string(int x, int y, char *s )
1374 Assert(FONT != NULL);
1376 if ( x == 0x8000 ) {
1377 if ( y<0 ) clipped |= 1;
1378 gr_get_string_size(s, &w, &h, &aw );
1379 // for x, since this will be centered, only look at
1381 if ( w > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
1382 if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
1384 if ( (y+h) < 0 ) clipped |= 2;
1385 if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
1388 if ( (x<0) || (y<0) ) clipped |= 1;
1389 gr_get_string_size(s, &w, &h, &aw );
1390 if ( (x+w) > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
1391 if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
1392 if ( (x+w) < 0 ) clipped |= 2;
1393 if ( (y+h) < 0 ) clipped |= 2;
1394 if ( x > grd_curcanv->cv_bitmap.bm_w ) clipped |= 2;
1395 if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
1399 return gr_ustring(x, y, s );
1401 if ( clipped & 2 ) {
1402 // Completely clipped...
1403 mprintf( (1, "Text '%s' at (%d,%d) is off screen!\n", s, x, y ));
1407 if ( clipped & 1 ) {
1408 // Partially clipped...
1409 //mprintf( (0, "Text '%s' at (%d,%d) is getting clipped!\n", s, x, y ));
1412 // Partially clipped...
1415 return ogl_internal_string(x,y,s);
1418 if (FFLAGS & FT_COLOR)
1419 return gr_internal_color_string( x, y, s);
1421 if ( BG_COLOR == -1)
1422 return gr_internal_string_clipped_m( x, y, s );
1424 return gr_internal_string_clipped( x, y, s );
1427 int gr_ustring(int x, int y, char *s )
1431 return ogl_internal_string(x,y,s);
1434 if (FFLAGS & FT_COLOR) {
1436 return gr_internal_color_string(x,y,s);
1443 if ( BG_COLOR == -1)
1444 return gr_internal_string0m(x,y,s);
1446 return gr_internal_string0(x,y,s);
1449 if ( BG_COLOR == -1)
1450 return gr_internal_string2m(x,y,s);
1452 return gr_internal_string2(x,y,s);
1454 #if defined(POLY_ACC)
1456 if ( BG_COLOR == -1)
1457 return gr_internal_string5m(x,y,s);
1459 return gr_internal_string5(x,y,s);
1466 void gr_get_string_size(char *s, int *string_width, int *string_height, int *average_width )
1468 int i = 0, longest_width = 0;
1471 *string_height = FHEIGHT;
1473 *average_width = FWIDTH;
1480 // if (*s == CC_UNDERLINE)
1485 *string_height += FHEIGHT;
1491 // 1 = next byte specifies color, so skip the 1 and the color value
1494 else if (*s == CC_LSPACING) {
1495 *string_height += *(s+1)-'0';
1498 get_char_width(s[0],s[1],&width,&spacing);
1500 *string_width += spacing;
1502 if (*string_width > longest_width)
1503 longest_width = *string_width;
1510 *string_width = longest_width;
1514 int gr_uprintf( int x, int y, char * format, ... )
1519 va_start(args, format );
1520 vsprintf(buffer,format,args);
1521 return gr_ustring( x, y, buffer );
1524 int gr_printf( int x, int y, char * format, ... )
1529 va_start(args, format );
1530 vsprintf(buffer,format,args);
1531 return gr_string( x, y, buffer );
1534 void gr_close_font( grs_font * font )
1542 for (fontnum=0;fontnum<MAX_OPEN_FONTS && open_font[fontnum].ptr!=font;fontnum++);
1543 Assert(fontnum<MAX_OPEN_FONTS); //did we find slot?
1545 font_data = open_font[fontnum].dataptr;
1546 d_free( font_data );
1548 open_font[fontnum].ptr = NULL;
1549 open_font[fontnum].dataptr = NULL;
1551 if ( font->ft_chars )
1552 d_free( font->ft_chars );
1554 if (font->ft_bitmaps)
1555 d_free( font->ft_bitmaps );
1556 gr_free_bitmap_data(&font->ft_parent_bitmap);
1557 // ogl_freebmtexture(&font->ft_parent_bitmap);
1565 //remap (by re-reading) all the color fonts
1566 void gr_remap_color_fonts()
1570 for (fontnum=0;fontnum<MAX_OPEN_FONTS;fontnum++) {
1573 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);
1580 void gr_remap_mono_fonts()
1583 con_printf (CON_DEBUG, "gr_remap_mono_fonts ()\n");
1584 for (fontnum=0;fontnum<MAX_OPEN_FONTS;fontnum++) {
1586 font = open_font[fontnum].ptr;
1587 if (font && !(font->ft_flags & FT_COLOR))
1588 gr_remap_font(font, open_font[fontnum].filename, open_font[fontnum].dataptr);
1593 #define grs_font_read(gf, fp) cfread(gf, GRS_FONT_SIZE, 1, fp)
1596 * reads a grs_font structure from a CFILE
1598 void grs_font_read(grs_font *gf, CFILE *fp)
1600 gf->ft_w = cfile_read_short(fp);
1601 gf->ft_h = cfile_read_short(fp);
1602 gf->ft_flags = cfile_read_short(fp);
1603 gf->ft_baseline = cfile_read_short(fp);
1604 gf->ft_minchar = cfile_read_byte(fp);
1605 gf->ft_maxchar = cfile_read_byte(fp);
1606 gf->ft_bytewidth = cfile_read_short(fp);
1607 gf->ft_data = (ubyte *)cfile_read_int(fp);
1608 gf->ft_chars = (ubyte **)cfile_read_int(fp);
1609 gf->ft_widths = (short *)cfile_read_int(fp);
1610 gf->ft_kerndata = (ubyte *)cfile_read_int(fp);
1614 grs_font * gr_init_font( char * fontname )
1616 static int first_time=1;
1620 unsigned char * ptr;
1624 int datasize; //size up to (but not including) palette
1628 for (i=0;i<MAX_OPEN_FONTS;i++)
1629 open_font[i].ptr = NULL;
1630 open_font[i].dataptr = NULL;
1634 //find free font slot
1635 for (fontnum=0;fontnum<MAX_OPEN_FONTS && open_font[fontnum].ptr!=NULL;fontnum++);
1636 Assert(fontnum<MAX_OPEN_FONTS); //did we find one?
1638 strncpy(open_font[fontnum].filename,fontname,FILENAME_LEN);
1640 fontfile = cfopen(fontname, "rb");
1643 con_printf(CON_VERBOSE, "Can't open font file %s\n", fontname);
1647 cfread(file_id, 4, 1, fontfile);
1648 if ( !strncmp( file_id, "NFSP", 4 ) ) {
1649 con_printf(CON_NORMAL, "File %s is not a font file\n", fontname );
1653 datasize = cfile_read_int(fontfile);
1654 datasize -= GRS_FONT_SIZE; // subtract the size of the header.
1656 MALLOC(font, grs_font, sizeof(grs_font));
1657 grs_font_read(font, fontfile);
1659 MALLOC(font_data, char, datasize);
1660 cfread(font_data, 1, datasize, fontfile);
1662 open_font[fontnum].ptr = font;
1663 open_font[fontnum].dataptr = font_data;
1665 // make these offsets relative to font_data
1666 font->ft_data = (ubyte *)((int)font->ft_data - GRS_FONT_SIZE);
1667 font->ft_widths = (short *)((int)font->ft_widths - GRS_FONT_SIZE);
1668 font->ft_kerndata = (ubyte *)((int)font->ft_kerndata - GRS_FONT_SIZE);
1670 nchars = font->ft_maxchar - font->ft_minchar + 1;
1672 if (font->ft_flags & FT_PROPORTIONAL) {
1674 font->ft_widths = (short *) &font_data[(int)font->ft_widths];
1675 font->ft_data = &font_data[(int)font->ft_data];
1676 font->ft_chars = (unsigned char **)d_malloc( nchars * sizeof(unsigned char *));
1678 ptr = font->ft_data;
1680 for (i=0; i< nchars; i++ ) {
1681 font->ft_widths[i] = INTEL_SHORT(font->ft_widths[i]);
1682 font->ft_chars[i] = ptr;
1683 if (font->ft_flags & FT_COLOR)
1684 ptr += font->ft_widths[i] * font->ft_h;
1686 ptr += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
1691 font->ft_data = font_data;
1692 font->ft_chars = NULL;
1693 font->ft_widths = NULL;
1695 ptr = font->ft_data + (nchars * font->ft_w * font->ft_h);
1698 if (font->ft_flags & FT_KERNED)
1699 font->ft_kerndata = &font_data[(int)font->ft_kerndata];
1701 if (font->ft_flags & FT_COLOR) { //remap palette
1702 ubyte palette[256*3];
1703 ubyte colormap[256];
1706 cfread(palette,3,256,fontfile); //read the palette
1708 #ifdef SWAP_0_255 // swap the first and last palette entries (black and white)
1713 for (i = 0; i < 3; i++) {
1715 palette[i] = palette[765+i];
1719 // we also need to swap the data entries as well. black is white and white is black
1721 for (i = 0; i < ptr-font->ft_data; i++) {
1722 if (font->ft_data[i] == 0)
1723 font->ft_data[i] = 255;
1724 else if (font->ft_data[i] == 255)
1725 font->ft_data[i] = 0;
1731 build_colormap_good( (ubyte *)&palette, colormap, freq );
1733 colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR; // changed from colormap[255] = 255 to this for macintosh
1735 decode_data_asm(font->ft_data, ptr - font->ft_data, colormap, freq );
1749 char tests[]="abcdefghij1234.A";
1750 gr_get_string_size(tests,&x,&y,&aw);
1751 // newfont->ft_aw=x/(float)strlen(tests);
1755 ogl_init_font(font);
1762 //remap a font by re-reading its data & palette
1763 void gr_remap_font( grs_font *font, char * fontname, char *font_data )
1769 int datasize; //size up to (but not including) palette
1772 if (! (font->ft_flags & FT_COLOR))
1775 fontfile = cfopen(fontname, "rb");
1778 Error( "Can't open font file %s", fontname );
1780 cfread(file_id, 4, 1, fontfile);
1781 if ( !strncmp( file_id, "NFSP", 4 ) )
1782 Error( "File %s is not a font file", fontname );
1784 datasize = cfile_read_int(fontfile);
1785 datasize -= GRS_FONT_SIZE; // subtract the size of the header.
1787 d_free(font->ft_chars);
1788 grs_font_read(font, fontfile); // have to reread in case mission hogfile overrides font.
1790 cfread(font_data, 1, datasize, fontfile); //read raw data
1792 // make these offsets relative to font_data
1793 font->ft_data = (ubyte *)((int)font->ft_data - GRS_FONT_SIZE);
1794 font->ft_widths = (short *)((int)font->ft_widths - GRS_FONT_SIZE);
1795 font->ft_kerndata = (ubyte *)((int)font->ft_kerndata - GRS_FONT_SIZE);
1797 nchars = font->ft_maxchar - font->ft_minchar + 1;
1799 if (font->ft_flags & FT_PROPORTIONAL) {
1801 font->ft_widths = (short *) &font_data[(int)font->ft_widths];
1802 font->ft_data = &font_data[(int)font->ft_data];
1803 font->ft_chars = (unsigned char **)d_malloc( nchars * sizeof(unsigned char *));
1805 ptr = font->ft_data;
1807 for (i=0; i< nchars; i++ ) {
1808 font->ft_widths[i] = INTEL_SHORT(font->ft_widths[i]);
1809 font->ft_chars[i] = ptr;
1810 if (font->ft_flags & FT_COLOR)
1811 ptr += font->ft_widths[i] * font->ft_h;
1813 ptr += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
1818 font->ft_data = font_data;
1819 font->ft_chars = NULL;
1820 font->ft_widths = NULL;
1822 ptr = font->ft_data + (nchars * font->ft_w * font->ft_h);
1825 if (font->ft_flags & FT_KERNED)
1826 font->ft_kerndata = &font_data[(int)font->ft_kerndata];
1828 if (font->ft_flags & FT_COLOR) { //remap palette
1829 ubyte palette[256*3];
1830 ubyte colormap[256];
1833 cfread(palette,3,256,fontfile); //read the palette
1835 #ifdef SWAP_0_255 // swap the first and last palette entries (black and white)
1840 for (i = 0; i < 3; i++) {
1842 palette[i] = palette[765+i];
1846 // we also need to swap the data entries as well. black is white and white is black
1848 for (i = 0; i < ptr-font->ft_data; i++) {
1849 if (font->ft_data[i] == 0)
1850 font->ft_data[i] = 255;
1851 else if (font->ft_data[i] == 255)
1852 font->ft_data[i] = 0;
1858 build_colormap_good( (ubyte *)&palette, colormap, freq );
1860 colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR; // changed from colormap[255] = 255 to this for macintosh
1862 decode_data_asm(font->ft_data, ptr - font->ft_data, colormap, freq );
1869 if (font->ft_bitmaps)
1870 d_free( font->ft_bitmaps );
1871 gr_free_bitmap_data(&font->ft_parent_bitmap);
1872 // ogl_freebmtexture(&font->ft_parent_bitmap);
1874 ogl_init_font(font);
1879 void gr_set_fontcolor( int fg, int bg )
1885 void gr_set_curfont( grs_font * new )
1891 int gr_internal_string_clipped(int x, int y, char *s )
1894 char * text_ptr, * next_row, * text_ptr1;
1895 int r, BitMask, i, bits, width, spacing, letter, underline;
1902 while (next_row != NULL )
1904 text_ptr1 = next_row;
1908 if (x==0x8000) //centered
1909 x = get_centered_x(text_ptr1);
1913 for (r=0; r<FHEIGHT; r++) {
1914 text_ptr = text_ptr1;
1918 if (*text_ptr == '\n' ) {
1919 next_row = &text_ptr[1];
1923 if (*text_ptr == CC_COLOR) {
1924 FG_COLOR = *(text_ptr+1);
1929 if (*text_ptr == CC_LSPACING) {
1930 Int3(); // Warning: skip lines not supported for clipped strings.
1936 if (*text_ptr == CC_UNDERLINE ) {
1937 if ((r==FBASELINE+2) || (r==FBASELINE+3))
1942 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1944 letter = *text_ptr-FMINCHAR;
1946 if (!INFONT(letter)) { //not in font, draw as space
1952 if (FFLAGS & FT_PROPORTIONAL)
1953 fp = FCHARS[letter];
1955 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
1958 for (i=0; i< width; i++ ) {
1959 gr_setcolor(FG_COLOR);
1963 fp += BITS_TO_BYTES(width)*r;
1967 for (i=0; i< width; i++ ) {
1973 gr_setcolor(FG_COLOR);
1975 gr_setcolor(BG_COLOR);
1981 x += spacing-width; //for kerning
1991 int gr_internal_string_clipped_m(int x, int y, char *s )
1994 char * text_ptr, * next_row, * text_ptr1;
1995 int r, BitMask, i, bits, width, spacing, letter, underline;
2002 while (next_row != NULL )
2004 text_ptr1 = next_row;
2008 if (x==0x8000) //centered
2009 x = get_centered_x(text_ptr1);
2013 for (r=0; r<FHEIGHT; r++) {
2016 text_ptr = text_ptr1;
2019 if (*text_ptr == '\n' ) {
2020 next_row = &text_ptr[1];
2024 if (*text_ptr == CC_COLOR) {
2025 FG_COLOR = *(text_ptr+1);
2030 if (*text_ptr == CC_LSPACING) {
2031 Int3(); // Warning: skip lines not supported for clipped strings.
2037 if (*text_ptr == CC_UNDERLINE ) {
2038 if ((r==FBASELINE+2) || (r==FBASELINE+3))
2043 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
2045 letter = *text_ptr-FMINCHAR;
2047 if (!INFONT(letter)) { //not in font, draw as space
2053 if (FFLAGS & FT_PROPORTIONAL)
2054 fp = FCHARS[letter];
2056 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
2059 for (i=0; i< width; i++ ) {
2060 gr_setcolor(FG_COLOR);
2064 fp += BITS_TO_BYTES(width)*r;
2068 for (i=0; i< width; i++ ) {
2073 if (bits & BitMask) {
2074 gr_setcolor(FG_COLOR);
2083 x += spacing-width; //for kerning