2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 /* $Source: /cvs/cvsroot/d2x/2d/font.c,v $
17 * $Date: 2001-11-02 02:03:35 $
19 * Graphical routines for drawing fonts.
21 * $Log: not supported by cvs2svn $
37 #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];
61 //list of open fonts, for use (for now) for palette remapping
62 openfont open_font[MAX_OPEN_FONTS];
64 #define FONT grd_curcanv->cv_font
65 #define FG_COLOR grd_curcanv->cv_font_fg_color
66 #define BG_COLOR grd_curcanv->cv_font_bg_color
67 #define FWIDTH FONT->ft_w
68 #define FHEIGHT FONT->ft_h
69 #define FBASELINE FONT->ft_baseline
70 #define FFLAGS FONT->ft_flags
71 #define FMINCHAR FONT->ft_minchar
72 #define FMAXCHAR FONT->ft_maxchar
73 #define FDATA FONT->ft_data
74 #define FCHARS FONT->ft_chars
75 #define FWIDTHS FONT->ft_widths
77 #define BITS_TO_BYTES(x) (((x)+7)>>3)
79 int gr_internal_string_clipped(int x, int y, char *s );
80 int gr_internal_string_clipped_m(int x, int y, char *s );
82 ubyte *find_kern_entry(grs_font *font,ubyte first,ubyte second)
84 ubyte *p=font->ft_kerndata;
87 if (p[0]==first && p[1]==second)
95 //takes the character AFTER being offset into font
96 #define INFONT(_c) ((_c >= 0) && (_c <= FMAXCHAR-FMINCHAR))
98 //takes the character BEFORE being offset into current font
99 void get_char_width(ubyte c,ubyte c2,int *width,int *spacing)
105 if (!INFONT(letter)) { //not in font, draw as space
107 if (FFLAGS & FT_PROPORTIONAL)
114 if (FFLAGS & FT_PROPORTIONAL)
115 *width = FWIDTHS[letter];
121 if (FFLAGS & FT_KERNED) {
124 if (!(c2==0 || c2=='\n')) {
127 letter2 = c2-FMINCHAR;
129 if (INFONT(letter2)) {
131 p = find_kern_entry(FONT,(ubyte)letter,letter2);
140 int get_centered_x(char *s)
144 for (w=0;*s!=0 && *s!='\n';s++) {
148 continue;//skip color codes.
150 get_char_width(s[0],s[1],&w2,&s2);
154 return ((grd_curcanv->cv_bitmap.bm_w - w) / 2);
157 //hack to allow color codes to be embedded in strings -MPM
158 //note we subtract one from color, since 255 is "transparent" so it'll never be used, and 0 would otherwise end the string.
159 //function must already have orig_color var set (or they could be passed as args...)
160 //perhaps some sort of recursive orig_color type thing would be better, but that would be way too much trouble for little gain
161 int gr_message_color_level=1;
162 #define CHECK_EMBEDDED_COLORS() if ((*text_ptr >= 0x01) && (*text_ptr <= 0x03)) { \
165 if (gr_message_color_level >= *(text_ptr-1)) \
166 FG_COLOR = *text_ptr - 1; \
170 else if ((*text_ptr >= 0x04) && (*text_ptr <= 0x06)){ \
171 if (gr_message_color_level >= *text_ptr - 3) \
172 FG_COLOR=orig_color; \
176 int gr_internal_string0(int x, int y, char *s )
179 ubyte * text_ptr, * next_row, * text_ptr1;
180 int r, BitMask, i, bits, width, spacing, letter, underline;
183 unsigned int VideoOffset, VideoOffset1;
187 VideoOffset1 = y * ROWSIZE + x;
191 while (next_row != NULL )
193 text_ptr1 = next_row;
196 if (x==0x8000) { //centered
197 int xx = get_centered_x(text_ptr1);
198 VideoOffset1 = y * ROWSIZE + xx;
201 for (r=0; r<FHEIGHT; r++)
204 text_ptr = text_ptr1;
206 VideoOffset = VideoOffset1;
210 if (*text_ptr == '\n' )
212 next_row = &text_ptr[1];
216 if (*text_ptr == CC_COLOR) {
217 FG_COLOR = *(text_ptr+1);
222 if (*text_ptr == CC_LSPACING) {
223 skip_lines = *(text_ptr+1) - '0';
229 if (*text_ptr == CC_UNDERLINE )
231 if ((r==FBASELINE+2) || (r==FBASELINE+3))
236 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
238 letter = *text_ptr-FMINCHAR;
240 if (!INFONT(letter)) { //not in font, draw as space
241 VideoOffset += spacing;
246 if (FFLAGS & FT_PROPORTIONAL)
249 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
252 for (i=0; i< width; i++ )
253 DATA[VideoOffset++] = (unsigned char) FG_COLOR;
256 fp += BITS_TO_BYTES(width)*r;
260 for (i=0; i< width; i++ )
268 DATA[VideoOffset++] = (unsigned char) FG_COLOR;
270 DATA[VideoOffset++] = (unsigned char) BG_COLOR;
275 VideoOffset += spacing-width; //for kerning
280 VideoOffset1 += ROWSIZE; y++;
284 VideoOffset1 += ROWSIZE * skip_lines;
290 int gr_internal_string0m(int x, int y, char *s )
293 ubyte * text_ptr, * next_row, * text_ptr1;
294 int r, BitMask, i, bits, width, spacing, letter, underline;
297 unsigned int VideoOffset, VideoOffset1;
299 int orig_color=FG_COLOR;//to allow easy reseting to default string color with colored strings -MPM
303 VideoOffset1 = y * ROWSIZE + x;
307 while (next_row != NULL )
309 text_ptr1 = next_row;
312 if (x==0x8000) { //centered
313 int xx = get_centered_x(text_ptr1);
314 VideoOffset1 = y * ROWSIZE + xx;
317 for (r=0; r<FHEIGHT; r++)
320 text_ptr = text_ptr1;
322 VideoOffset = VideoOffset1;
326 if (*text_ptr == '\n' )
328 next_row = &text_ptr[1];
332 if (*text_ptr == CC_COLOR) {
333 FG_COLOR = *(text_ptr+1);
338 if (*text_ptr == CC_LSPACING) {
339 skip_lines = *(text_ptr+1) - '0';
345 if (*text_ptr == CC_UNDERLINE )
347 if ((r==FBASELINE+2) || (r==FBASELINE+3))
352 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
354 letter = *text_ptr-FMINCHAR;
356 if (!INFONT(letter) || *text_ptr<=0x06) { //not in font, draw as space
357 CHECK_EMBEDDED_COLORS() else{
358 VideoOffset += spacing;
364 if (FFLAGS & FT_PROPORTIONAL)
367 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
370 for (i=0; i< width; i++ )
371 DATA[VideoOffset++] = (unsigned int) FG_COLOR;
374 fp += BITS_TO_BYTES(width)*r;
378 for (i=0; i< width; i++ )
386 DATA[VideoOffset++] = (unsigned int) FG_COLOR;
394 VideoOffset += spacing-width;
397 VideoOffset1 += ROWSIZE;
401 VideoOffset1 += ROWSIZE * skip_lines;
408 int gr_internal_string2(int x, int y, char *s )
411 ubyte * text_ptr, * next_row, * text_ptr1;
412 int r, BitMask, i, bits, width, spacing, letter, underline;
413 int page_switched, skip_lines = 0;
415 unsigned int VideoOffset, VideoOffset1;
417 VideoOffset1 = (size_t)DATA + y * ROWSIZE + x;
421 gr_vesa_setpage(VideoOffset1 >> 16);
423 VideoOffset1 &= 0xFFFF;
427 while (next_row != NULL )
429 text_ptr1 = next_row;
432 if (x==0x8000) { //centered
433 int xx = get_centered_x(text_ptr1);
434 VideoOffset1 = y * ROWSIZE + xx;
435 gr_vesa_setpage(VideoOffset1 >> 16);
436 VideoOffset1 &= 0xFFFF;
439 for (r=0; r<FHEIGHT; r++)
441 text_ptr = text_ptr1;
443 VideoOffset = VideoOffset1;
449 if (*text_ptr == '\n' )
451 next_row = &text_ptr[1];
455 if (*text_ptr == CC_COLOR) {
456 FG_COLOR = *(text_ptr+1);
461 if (*text_ptr == CC_LSPACING) {
462 skip_lines = *(text_ptr+1) - '0';
468 if (*text_ptr == CC_UNDERLINE )
470 if ((r==FBASELINE+2) || (r==FBASELINE+3))
475 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
477 Assert(width==spacing); //no kerning support here
479 letter = *text_ptr-FMINCHAR;
481 if (!INFONT(letter) || *text_ptr<=0x06) { //not in font, draw as space
482 CHECK_EMBEDDED_COLORS() else{
483 VideoOffset += spacing;
489 if (FFLAGS & FT_PROPORTIONAL)
492 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
496 if ( VideoOffset+width > 0xFFFF )
498 for (i=0; i< width; i++ )
500 gr_video_memory[VideoOffset++] = FG_COLOR;
502 if (VideoOffset > 0xFFFF )
504 VideoOffset -= 0xFFFF + 1;
512 for (i=0; i< width; i++ )
513 gr_video_memory[VideoOffset++] = FG_COLOR;
522 fp += BITS_TO_BYTES(width)*r;
526 if ( VideoOffset+width > 0xFFFF )
528 for (i=0; i< width; i++ )
536 gr_video_memory[VideoOffset++] = FG_COLOR;
538 gr_video_memory[VideoOffset++] = BG_COLOR;
542 if (VideoOffset > 0xFFFF )
544 VideoOffset -= 0xFFFF + 1;
556 if (bits & 0x80) gr_video_memory[VideoOffset+0] = FG_COLOR;
557 else gr_video_memory[VideoOffset+0] = BG_COLOR;
559 if (bits & 0x40) gr_video_memory[VideoOffset+1] = FG_COLOR;
560 else gr_video_memory[VideoOffset+1] = BG_COLOR;
562 if (bits & 0x20) gr_video_memory[VideoOffset+2] = FG_COLOR;
563 else gr_video_memory[VideoOffset+2] = BG_COLOR;
565 if (bits & 0x10) gr_video_memory[VideoOffset+3] = FG_COLOR;
566 else gr_video_memory[VideoOffset+3] = BG_COLOR;
568 if (bits & 0x08) gr_video_memory[VideoOffset+4] = FG_COLOR;
569 else gr_video_memory[VideoOffset+4] = BG_COLOR;
571 if (bits & 0x04) gr_video_memory[VideoOffset+5] = FG_COLOR;
572 else gr_video_memory[VideoOffset+5] = BG_COLOR;
574 if (bits & 0x02) gr_video_memory[VideoOffset+6] = FG_COLOR;
575 else gr_video_memory[VideoOffset+6] = BG_COLOR;
577 if (bits & 0x01) gr_video_memory[VideoOffset+7] = FG_COLOR;
578 else gr_video_memory[VideoOffset+7] = BG_COLOR;
582 for (i=0; i< width/2 ; i++ )
590 gr_video_memory[VideoOffset++] = FG_COLOR;
592 gr_video_memory[VideoOffset++] = BG_COLOR;
604 gr_video_memory[VideoOffset++] = FG_COLOR;
606 gr_video_memory[VideoOffset++] = BG_COLOR;
616 VideoOffset1 += ROWSIZE;
618 if (VideoOffset1 > 0xFFFF ) {
619 VideoOffset1 -= 0xFFFF + 1;
626 VideoOffset1 += ROWSIZE * skip_lines;
632 int gr_internal_string2m(int x, int y, char *s )
635 char * text_ptr, * next_row, * text_ptr1;
636 int r, BitMask, i, bits, width, spacing, letter, underline;
637 int page_switched, skip_lines = 0;
639 unsigned int VideoOffset, VideoOffset1;
641 VideoOffset1 = (size_t)DATA + y * ROWSIZE + x;
643 gr_vesa_setpage(VideoOffset1 >> 16);
645 VideoOffset1 &= 0xFFFF;
649 while (next_row != NULL )
651 text_ptr1 = next_row;
654 if (x==0x8000) { //centered
655 int xx = get_centered_x(text_ptr1);
656 VideoOffset1 = y * ROWSIZE + xx;
657 gr_vesa_setpage(VideoOffset1 >> 16);
658 VideoOffset1 &= 0xFFFF;
661 for (r=0; r<FHEIGHT; r++)
663 text_ptr = text_ptr1;
665 VideoOffset = VideoOffset1;
671 if (*text_ptr == '\n' )
673 next_row = &text_ptr[1];
677 if (*text_ptr == CC_COLOR) {
678 FG_COLOR = *(text_ptr+1);
683 if (*text_ptr == CC_LSPACING) {
684 skip_lines = *(text_ptr+1) - '0';
690 if (*text_ptr == CC_UNDERLINE )
692 if ((r==FBASELINE+2) || (r==FBASELINE+3))
697 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
699 letter = *text_ptr-FMINCHAR;
701 if (!INFONT(letter)) { //not in font, draw as space
702 VideoOffset += width;
707 if (FFLAGS & FT_PROPORTIONAL)
710 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
714 if ( VideoOffset+width > 0xFFFF )
716 for (i=0; i< width; i++ )
718 gr_video_memory[VideoOffset++] = FG_COLOR;
720 if (VideoOffset > 0xFFFF )
722 VideoOffset -= 0xFFFF + 1;
730 for (i=0; i< width; i++ )
731 gr_video_memory[VideoOffset++] = FG_COLOR;
736 fp += BITS_TO_BYTES(width)*r;
740 if ( VideoOffset+width > 0xFFFF )
742 for (i=0; i< width; i++ )
750 gr_video_memory[VideoOffset++] = FG_COLOR;
756 if (VideoOffset > 0xFFFF )
758 VideoOffset -= 0xFFFF + 1;
765 for (i=0; i< width; i++ )
773 gr_video_memory[VideoOffset++] = FG_COLOR;
782 VideoOffset += spacing-width;
786 VideoOffset1 += ROWSIZE;
788 if (VideoOffset1 > 0xFFFF ) {
789 VideoOffset1 -= 0xFFFF + 1;
796 VideoOffset1 += ROWSIZE * skip_lines;
802 #endif // __ENV_MSDOS__
804 #if defined(POLY_ACC)
805 int gr_internal_string5(int x, int y, char *s )
808 ubyte * text_ptr, * next_row, * text_ptr1;
809 int r, BitMask, i, bits, width, spacing, letter, underline;
812 unsigned int VideoOffset, VideoOffset1;
815 VideoOffset1 = y * ROWSIZE + x * PA_BPP;
819 while (next_row != NULL )
821 text_ptr1 = next_row;
824 if (x==0x8000) { //centered
825 int xx = get_centered_x(text_ptr1);
826 VideoOffset1 = y * ROWSIZE + xx * PA_BPP;
829 for (r=0; r<FHEIGHT; r++)
832 text_ptr = text_ptr1;
834 VideoOffset = VideoOffset1;
838 if (*text_ptr == '\n' )
840 next_row = &text_ptr[1];
844 if (*text_ptr == CC_COLOR) {
845 FG_COLOR = *(text_ptr+1);
850 if (*text_ptr == CC_LSPACING) {
851 skip_lines = *(text_ptr+1) - '0';
857 if (*text_ptr == CC_UNDERLINE )
859 if ((r==FBASELINE+2) || (r==FBASELINE+3))
864 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
866 letter = *text_ptr-FMINCHAR;
868 if (!INFONT(letter)) { //not in font, draw as space
869 VideoOffset += spacing * PA_BPP;
874 if (FFLAGS & FT_PROPORTIONAL)
877 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
880 for (i=0; i< width; i++ )
881 { *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
884 fp += BITS_TO_BYTES(width)*r;
888 for (i=0; i< width; i++ )
896 { *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
898 { *(short *)(DATA + VideoOffset) = pa_clut[BG_COLOR]; VideoOffset += PA_BPP; }
903 VideoOffset += PA_BPP * (spacing-width); //for kerning
908 VideoOffset1 += ROWSIZE; y++;
912 VideoOffset1 += ROWSIZE * skip_lines;
918 int gr_internal_string5m(int x, int y, char *s )
921 ubyte * text_ptr, * next_row, * text_ptr1;
922 int r, BitMask, i, bits, width, spacing, letter, underline;
925 unsigned int VideoOffset, VideoOffset1;
928 VideoOffset1 = y * ROWSIZE + x * PA_BPP;
932 while (next_row != NULL )
934 text_ptr1 = next_row;
937 if (x==0x8000) { //centered
938 int xx = get_centered_x(text_ptr1);
939 VideoOffset1 = y * ROWSIZE + xx * PA_BPP;
942 for (r=0; r<FHEIGHT; r++)
945 text_ptr = text_ptr1;
947 VideoOffset = VideoOffset1;
951 if (*text_ptr == '\n' )
953 next_row = &text_ptr[1];
957 if (*text_ptr == CC_COLOR) {
958 FG_COLOR = *(text_ptr+1);
963 if (*text_ptr == CC_LSPACING) {
964 skip_lines = *(text_ptr+1) - '0';
970 if (*text_ptr == CC_UNDERLINE )
972 if ((r==FBASELINE+2) || (r==FBASELINE+3))
977 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
979 letter = *text_ptr-FMINCHAR;
981 if (!INFONT(letter)) { //not in font, draw as space
982 VideoOffset += spacing * PA_BPP;
987 if (FFLAGS & FT_PROPORTIONAL)
990 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
993 for (i=0; i< width; i++ )
994 { *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
997 fp += BITS_TO_BYTES(width)*r;
1001 for (i=0; i< width; i++ )
1009 { *(short *)(DATA + VideoOffset) = pa_clut[FG_COLOR]; VideoOffset += PA_BPP; }
1011 VideoOffset += PA_BPP;
1017 VideoOffset += PA_BPP * (spacing-width);
1020 VideoOffset1 += ROWSIZE; y++;
1023 VideoOffset1 += ROWSIZE * skip_lines;
1031 //a bitmap for the character
1032 grs_bitmap char_bm = {
1035 BM_FLAG_TRANSPARENT, //flags
1038 #ifdef BITMAP_SELECTOR
1045 int gr_internal_color_string(int x, int y, char *s )
1048 ubyte * text_ptr, * next_row, * text_ptr1;
1049 int width, spacing,letter;
1052 char_bm.bm_h = FHEIGHT; //set height for chars of this font
1059 while (next_row != NULL)
1061 text_ptr1 = next_row;
1064 text_ptr = text_ptr1;
1068 if (xx==0x8000) //centered
1069 xx = get_centered_x(text_ptr);
1073 if (*text_ptr == '\n' )
1075 next_row = &text_ptr[1];
1080 letter = *text_ptr-FMINCHAR;
1082 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1084 if (!INFONT(letter)) { //not in font, draw as space
1090 if (FFLAGS & FT_PROPORTIONAL)
1091 fp = FCHARS[letter];
1093 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
1095 gr_init_bitmap (&char_bm, BM_LINEAR, 0, 0, width, FHEIGHT, width, fp);
1096 gr_bitmapm(xx,yy,&char_bm);
1108 #include "../main/inferno.h"
1109 #include "ogl_init.h"
1111 //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
1113 int pow2ize(int x);//from ogl.c
1115 int get_font_total_width(grs_font * font){
1116 if (font->ft_flags & FT_PROPORTIONAL){
1117 int i,w=0,c=font->ft_minchar;
1118 for (i=0;c<=font->ft_maxchar;i++,c++){
1119 if (font->ft_widths[i]<0)
1121 w+=font->ft_widths[i];
1125 return font->ft_w*(font->ft_maxchar-font->ft_minchar+1);
1128 void ogl_font_choose_size(grs_font * font,int gap,int *rw,int *rh){
1129 int nchars = font->ft_maxchar-font->ft_minchar+1;
1130 int r,x,y,nc=0,smallest=999999,smallr=-1,tries;
1131 int smallprop=10000;
1133 for (h=32;h<=256;h*=2){
1134 // h=pow2ize(font->ft_h*rows+gap*(rows-1));
1135 if (font->ft_h>h)continue;
1136 r=(h/(font->ft_h+gap));
1137 w=pow2ize((get_font_total_width(font)+(nchars-r)*gap)/r);
1143 mprintf((0,"failed to fit (%ix%i, %ic)\n",w,h,nc));
1148 while(y+font->ft_h<=h){
1153 if (font->ft_flags & FT_PROPORTIONAL){
1154 if (x+font->ft_widths[nc]+gap>w)break;
1155 x+=font->ft_widths[nc++]+gap;
1157 if (x+font->ft_w+gap>w)break;
1171 mprintf((0,"fit: %ix%i %i tries\n",w,h,tries));
1173 if (w*h==smallest){//this gives squarer sizes priority (ie, 128x128 would be better than 512*32)
1194 Error("couldn't fit font?\n");
1195 mprintf((0,"using %ix%i\n",*rw,*rh));
1199 void ogl_init_font(grs_font * font){
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 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]);
1346 if (grd_curcanv->cv_bitmap.bm_type==BM_OGL)
1347 ogl_ubitmapm_c(xx,yy,&FONT->ft_bitmaps[letter],FG_COLOR);
1349 Error("ogl_internal_string: non-color string to non-ogl dest\n");
1350 // gr_ubitmapm(xx,yy,&FONT->ft_bitmaps[letter]);//ignores color..
1363 int gr_internal_color_string(int x, int y, char *s ){
1364 return ogl_internal_string(x,y,s);
1368 int gr_string(int x, int y, char *s )
1373 Assert(FONT != NULL);
1375 if ( x == 0x8000 ) {
1376 if ( y<0 ) clipped |= 1;
1377 gr_get_string_size(s, &w, &h, &aw );
1378 // for x, since this will be centered, only look at
1380 if ( w > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
1381 if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
1383 if ( (y+h) < 0 ) clipped |= 2;
1384 if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
1387 if ( (x<0) || (y<0) ) clipped |= 1;
1388 gr_get_string_size(s, &w, &h, &aw );
1389 if ( (x+w) > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
1390 if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
1391 if ( (x+w) < 0 ) clipped |= 2;
1392 if ( (y+h) < 0 ) clipped |= 2;
1393 if ( x > grd_curcanv->cv_bitmap.bm_w ) clipped |= 2;
1394 if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
1398 return gr_ustring(x, y, s );
1400 if ( clipped & 2 ) {
1401 // Completely clipped...
1402 mprintf( (1, "Text '%s' at (%d,%d) is off screen!\n", s, x, y ));
1406 if ( clipped & 1 ) {
1407 // Partially clipped...
1408 //mprintf( (0, "Text '%s' at (%d,%d) is getting clipped!\n", s, x, y ));
1411 // Partially clipped...
1414 return ogl_internal_string(x,y,s);
1417 if (FFLAGS & FT_COLOR)
1418 return gr_internal_color_string( x, y, s);
1420 if ( BG_COLOR == -1)
1421 return gr_internal_string_clipped_m( x, y, s );
1423 return gr_internal_string_clipped( x, y, s );
1426 int gr_ustring(int x, int y, char *s )
1430 return ogl_internal_string(x,y,s);
1433 if (FFLAGS & FT_COLOR) {
1435 return gr_internal_color_string(x,y,s);
1442 if ( BG_COLOR == -1)
1443 return gr_internal_string0m(x,y,s);
1445 return gr_internal_string0(x,y,s);
1446 #ifdef __ENV_MSDOS__
1448 if ( BG_COLOR == -1)
1449 return gr_internal_string2m(x,y,s);
1451 return gr_internal_string2(x,y,s);
1452 #endif // __ENV_MSDOS__
1453 #if defined(POLY_ACC)
1455 if ( BG_COLOR == -1)
1456 return gr_internal_string5m(x,y,s);
1458 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);
1499 *string_width += spacing;
1500 if (*string_width > longest_width)
1501 longest_width = *string_width;
1507 *string_width = longest_width;
1511 int gr_uprintf( int x, int y, char * format, ... )
1516 va_start(args, format );
1517 vsprintf(buffer,format,args);
1518 return gr_ustring( x, y, buffer );
1521 int gr_printf( int x, int y, char * format, ... )
1526 va_start(args, format );
1527 vsprintf(buffer,format,args);
1528 return gr_string( x, y, buffer );
1531 void gr_close_font( grs_font * font )
1538 for (fontnum=0;fontnum<MAX_OPEN_FONTS && open_font[fontnum].ptr!=font;fontnum++);
1539 Assert(fontnum<MAX_OPEN_FONTS); //did we find slot?
1541 open_font[fontnum].ptr = NULL;
1543 if ( font->ft_chars )
1544 d_free( font->ft_chars );
1545 d_free( font->oldfont );
1547 if (font->ft_bitmaps)
1548 d_free( font->ft_bitmaps );
1549 gr_free_bitmap_data(&font->ft_parent_bitmap);
1550 // ogl_freebmtexture(&font->ft_parent_bitmap);
1557 //remap (by re-reading) all the color fonts
1558 void gr_remap_color_fonts()
1562 for (fontnum=0;fontnum<MAX_OPEN_FONTS;fontnum++) {
1565 font = open_font[fontnum].ptr;
1567 if (font && (font->ft_flags & FT_COLOR))
1568 // Unless gr_remap_font is fixed, we gotta just
1569 // reload the whole font
1571 char fontname[FILENAME_LEN];
1573 strncpy(fontname,open_font[fontnum].filename,FILENAME_LEN);
1574 gr_close_font(font);
1575 font = gr_init_font(fontname);
1577 // gr_remap_font(font,open_font[fontnum].filename);
1581 void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq );
1582 void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
1584 #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
1588 "inc dword ptr [ebx+eax*4]" \
1589 "mov al,[edi+eax]" \
1597 grs_font * gr_init_font( char * fontname )
1599 static int first_time=1;
1603 unsigned char * ptr;
1607 int32_t datasize; //size up to (but not including) palette
1611 for (i=0;i<MAX_OPEN_FONTS;i++)
1612 open_font[i].ptr = NULL;
1616 //find free font slot
1617 for (fontnum=0;fontnum<MAX_OPEN_FONTS && open_font[fontnum].ptr!=NULL;fontnum++);
1618 Assert(fontnum<MAX_OPEN_FONTS); //did we find one?
1620 strncpy(open_font[fontnum].filename,fontname,FILENAME_LEN);
1622 fontfile = cfopen(fontname, "rb");
1625 Error( "Can't open font file %s", fontname );
1627 cfread(&file_id,sizeof(file_id),1,fontfile);
1628 file_id=swapint(file_id);
1629 cfread(&datasize,sizeof(datasize),1,fontfile);
1630 datasize=swapint(datasize);
1632 if (file_id != 0x4e465350) /* 'NFSP' */
1633 Error( "File %s is not a font file", fontname );
1635 font = (old_grs_font *) d_malloc(datasize);
1636 newfont = (grs_font *) d_malloc(sizeof(grs_font));
1637 newfont->oldfont=font;
1639 open_font[fontnum].ptr = newfont;
1641 cfread(font,1,datasize,fontfile);
1643 newfont->ft_flags=swapint(font->ft_flags);
1644 newfont->ft_w=swapshort(font->ft_w);
1645 newfont->ft_h=swapshort(font->ft_h);
1646 newfont->ft_baseline=swapshort(font->ft_baseline);
1647 newfont->ft_maxchar=font->ft_maxchar;
1648 newfont->ft_minchar=font->ft_minchar;
1649 newfont->ft_bytewidth=swapshort(font->ft_bytewidth);
1651 nchars = newfont->ft_maxchar-newfont->ft_minchar+1;
1653 if (newfont->ft_flags & FT_PROPORTIONAL) {
1655 newfont->ft_widths = (short *) (swapint(font->ft_widths) + ((ubyte *) font));
1656 newfont->ft_data = (swapint(font->ft_data)) + ((ubyte *) font);
1657 newfont->ft_chars = (unsigned char **)d_malloc( nchars * sizeof(unsigned char *));
1659 ptr = newfont->ft_data;
1661 for (i=0; i< nchars; i++ ) {
1662 newfont->ft_chars[i] = ptr;
1663 if (newfont->ft_flags & FT_COLOR)
1664 ptr += newfont->ft_widths[i] * newfont->ft_h;
1666 ptr += BITS_TO_BYTES(newfont->ft_widths[i]) * newfont->ft_h;
1671 newfont->ft_data = ((unsigned char *) font) + sizeof(*font);
1672 newfont->ft_chars = NULL;
1673 newfont->ft_widths = NULL;
1675 ptr = newfont->ft_data + (nchars * newfont->ft_w * newfont->ft_h);
1678 if (newfont->ft_flags & FT_KERNED)
1679 newfont->ft_kerndata = swapint(font->ft_kerndata) + ((ubyte *) font);
1681 if (newfont->ft_flags & FT_COLOR) { //remap palette
1682 ubyte palette[256*3];
1683 ubyte colormap[256];
1686 cfread(palette,3,256,fontfile); //read the palette
1688 #ifdef MACINTOSH // swap the first and last palette entries (black and white)
1693 for (i = 0; i < 3; i++) {
1695 palette[i] = palette[765+i];
1699 // we also need to swap the data entries as well. black is white and white is black
1701 for (i = 0; i < ptr-newfont->ft_data; i++) {
1702 if (newfont->ft_data[i] == 0)
1703 newfont->ft_data[i] = 255;
1704 else if (newfont->ft_data[i] == 255)
1705 newfont->ft_data[i] = 0;
1711 build_colormap_good( (ubyte *)&palette, colormap, freq );
1713 colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR; // chaged from colormap[255] = 255 to this for macintosh
1715 decode_data_asm(newfont->ft_data, ptr-newfont->ft_data, colormap, freq );
1721 // memcpy(newfont,font,(ubyte*)&newfont->oldfont-(ubyte*)newfont);//fill in newfont data from oldfont struct
1722 // mprintf((0,"%i %i %i\n",sizeof(grs_font),sizeof(old_grs_font),(ubyte*)&newfont->oldfont-(ubyte*)newfont));
1732 char tests[]="abcdefghij1234.A";
1733 gr_get_string_size(tests,&x,&y,&aw);
1734 // newfont->ft_aw=x/(float)strlen(tests);
1738 ogl_init_font(newfont);
1745 //remap a font by re-reading its data & palette
1746 void gr_remap_font( grs_font *font, char * fontname )
1752 int32_t datasize; //size up to (but not including) palette
1753 int data_ofs,data_len;
1755 if (! (font->ft_flags & FT_COLOR))
1758 fontfile = cfopen(fontname, "rb");
1761 Error( "Can't open font file %s", fontname );
1763 file_id = cfile_read_int(fontfile);
1764 datasize = cfile_read_int(fontfile);
1766 if (file_id != 0x4e465350) /* 'NFSP' */
1767 Error( "File %s is not a font file", fontname );
1769 nchars = font->ft_maxchar-font->ft_minchar+1;
1771 //compute data length
1773 if (font->ft_flags & FT_PROPORTIONAL) {
1775 for (i=0; i< nchars; i++ ) {
1776 if (font->ft_flags & FT_COLOR)
1777 data_len += font->ft_widths[i] * font->ft_h;
1779 data_len += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
1783 data_len = nchars * font->ft_w * font->ft_h;
1785 data_ofs = font->ft_data - ((ubyte *) font);
1787 cfseek(fontfile,data_ofs,SEEK_CUR);
1788 cfread(font->ft_data,1,data_len,fontfile); //read raw data
1790 if (font->ft_flags & FT_COLOR) { //remap palette
1791 ubyte palette[256*3];
1792 ubyte colormap[256];
1795 cfseek(fontfile,-sizeof(palette),SEEK_END);
1797 cfread(palette,3,256,fontfile); //read the palette
1799 #ifdef MACINTOSH // swap the first and last palette entries (black and white)
1804 for (i = 0; i < 3; i++) {
1806 palette[i] = palette[765+i];
1810 // we also need to swap the data entries as well. black is white and white is black
1812 for (i = 0; i < data_len; i++) {
1813 if (font->ft_data[i] == 0)
1814 font->ft_data[i] = 255;
1815 else if (font->ft_data[i] == 255)
1816 font->ft_data[i] = 0;
1822 build_colormap_good( (ubyte *)&palette, colormap, freq );
1824 colormap[TRANSPARENCY_COLOR] = TRANSPARENCY_COLOR; // changed from colormap[255] = 255;
1826 decode_data_asm(font->ft_data, data_len, colormap, freq );
1835 void gr_set_fontcolor( int fg, int bg )
1841 void gr_set_curfont( grs_font * new )
1847 int gr_internal_string_clipped(int x, int y, char *s )
1850 char * text_ptr, * next_row, * text_ptr1;
1851 int r, BitMask, i, bits, width, spacing, letter, underline;
1858 while (next_row != NULL )
1860 text_ptr1 = next_row;
1864 if (x==0x8000) //centered
1865 x = get_centered_x(text_ptr1);
1869 for (r=0; r<FHEIGHT; r++) {
1870 text_ptr = text_ptr1;
1874 if (*text_ptr == '\n' ) {
1875 next_row = &text_ptr[1];
1879 if (*text_ptr == CC_COLOR) {
1880 FG_COLOR = *(text_ptr+1);
1885 if (*text_ptr == CC_LSPACING) {
1886 Int3(); // Warning: skip lines not supported for clipped strings.
1892 if (*text_ptr == CC_UNDERLINE ) {
1893 if ((r==FBASELINE+2) || (r==FBASELINE+3))
1898 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
1900 letter = *text_ptr-FMINCHAR;
1902 if (!INFONT(letter)) { //not in font, draw as space
1908 if (FFLAGS & FT_PROPORTIONAL)
1909 fp = FCHARS[letter];
1911 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
1914 for (i=0; i< width; i++ ) {
1915 gr_setcolor(FG_COLOR);
1919 fp += BITS_TO_BYTES(width)*r;
1923 for (i=0; i< width; i++ ) {
1929 gr_setcolor(FG_COLOR);
1931 gr_setcolor(BG_COLOR);
1937 x += spacing-width; //for kerning
1947 int gr_internal_string_clipped_m(int x, int y, char *s )
1950 char * text_ptr, * next_row, * text_ptr1;
1951 int r, BitMask, i, bits, width, spacing, letter, underline;
1958 while (next_row != NULL )
1960 text_ptr1 = next_row;
1964 if (x==0x8000) //centered
1965 x = get_centered_x(text_ptr1);
1969 for (r=0; r<FHEIGHT; r++) {
1972 text_ptr = text_ptr1;
1975 if (*text_ptr == '\n' ) {
1976 next_row = &text_ptr[1];
1980 if (*text_ptr == CC_COLOR) {
1981 FG_COLOR = *(text_ptr+1);
1986 if (*text_ptr == CC_LSPACING) {
1987 Int3(); // Warning: skip lines not supported for clipped strings.
1993 if (*text_ptr == CC_UNDERLINE ) {
1994 if ((r==FBASELINE+2) || (r==FBASELINE+3))
1999 get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
2001 letter = *text_ptr-FMINCHAR;
2003 if (!INFONT(letter)) { //not in font, draw as space
2009 if (FFLAGS & FT_PROPORTIONAL)
2010 fp = FCHARS[letter];
2012 fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
2015 for (i=0; i< width; i++ ) {
2016 gr_setcolor(FG_COLOR);
2020 fp += BITS_TO_BYTES(width)*r;
2024 for (i=0; i< width; i++ ) {
2029 if (bits & BitMask) {
2030 gr_setcolor(FG_COLOR);
2039 x += spacing-width; //for kerning