123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278 |
- /*
- THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
- SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
- END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
- ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
- IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
- SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
- FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
- CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
- AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
- COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
- */
- /* $Source: f:/miner/source/2d/rcs/font.c $
- * $Revision: 1.30 $
- * $Author: john $
- * $Date: 1995/01/25 20:02:03 $
- *
- * Graphical routines for drawing fonts.
- *
- * $Log: font.c $
- * Revision 1.30 1995/01/25 20:02:03 john
- * Fixed bug with mono clipped fonts only drawing 1 row.
- *
- * Revision 1.29 1994/12/08 19:03:54 john
- * Made functions use cfile.
- *
- * Revision 1.28 1994/11/27 23:08:22 matt
- * Changes for new mprintf calling convention
- *
- * Revision 1.27 1994/11/18 23:54:50 john
- * Made centering work on x = 0x8000 not -0x8000.
- *
- * Revision 1.26 1994/11/18 22:50:23 john
- * Changed shorts to ints in parameters.
- *
- * Revision 1.25 1994/11/04 11:10:31 john
- * Took the & out of string sizing.
- *
- * Revision 1.24 1994/11/04 10:06:36 john
- * Added fade table for fading fonts. Made font that partially clips
- * not print a warning message.
- *
- * Revision 1.23 1994/09/29 10:09:06 john
- * Added real clipping to text.
- *
- * Revision 1.22 1994/09/12 19:27:51 john
- * Fixed bug with unclipped fonts clipping.
- *
- * Revision 1.21 1994/09/12 17:16:59 john
- * Added clipping.
- *
- * Revision 1.20 1994/08/28 16:43:01 matt
- * Added centering if x==0x8000
- *
- * Revision 1.19 1994/08/15 15:01:11 matt
- * Close font file after reading
- *
- * Revision 1.18 1994/08/12 18:17:42 matt
- * Fixed stupid bug that cause first char in font not to print
- *
- * Revision 1.17 1994/08/11 18:00:17 matt
- * Cleaned up code, fixed string width bug.
- *
- * Revision 1.16 1994/08/10 18:46:11 matt
- * Fixed a couple of little things
- *
- * Revision 1.15 1994/08/10 18:36:20 matt
- * Changed font file format. Made chars not in font plot as spaces. Font file
- * now stores palette for color font & remaps on load.
- *
- * Revision 1.14 1994/08/10 12:27:27 matt
- * Took out unneeded int3
- *
- * Revision 1.13 1994/08/10 12:25:26 matt
- * Added support for colors fonts & kerned fonts
- * Made our own font file format
- *
- * Revision 1.12 1994/07/22 17:19:00 john
- * made proportional font width be the max width.
- *
- * Revision 1.11 1994/05/06 12:50:26 john
- * Added supertransparency; neatend things up; took out warnings.
- *
- * Revision 1.10 1994/01/31 11:08:12 john
- * fixed bug with order of frees.
- *
- * Revision 1.9 1994/01/31 10:36:40 john
- * Free'd variable size font data.
- *
- * Revision 1.8 1994/01/27 17:17:40 john
- * Made error if font file doesn't exist.
- *
- * Revision 1.7 1993/10/26 13:17:48 john
- * *** empty log message ***
- *
- * Revision 1.6 1993/10/15 16:23:25 john
- * y
- *
- * Revision 1.5 1993/09/28 19:05:50 john
- * added support for \n in gr_string and gr_get_string_size
- *
- * Revision 1.4 1993/09/28 12:46:49 matt
- * On error, now call Error() instead of exit().
- *
- * Revision 1.3 1993/09/20 11:35:31 john
- * *** empty log message ***
- *
- * Revision 1.2 1993/09/08 15:54:39 john
- * renamed ReadFile to readfontfile to prevent conflicts with others.
- *
- * Revision 1.1 1993/09/08 11:43:34 john
- * Initial revision
- *
- *
- */
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <fcntl.h>
- #include <io.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include "mem.h"
- #include "gr.h"
- #include "grdef.h"
- #include "error.h"
- #include "cfile.h"
- #include "mono.h"
- #define FONT grd_curcanv->cv_font
- #define FG_COLOR grd_curcanv->cv_font_fg_color
- #define BG_COLOR grd_curcanv->cv_font_bg_color
- #define FWIDTH FONT->ft_w
- #define FHEIGHT FONT->ft_h
- #define FBASELINE FONT->ft_baseline
- #define FFLAGS FONT->ft_flags
- #define FMINCHAR FONT->ft_minchar
- #define FMAXCHAR FONT->ft_maxchar
- #define FDATA FONT->ft_data
- #define FCHARS FONT->ft_chars
- #define FWIDTHS FONT->ft_widths
- #define BITS_TO_BYTES(x) (((x)+7)>>3)
- int gr_internal_string_clipped(int x, int y, char *s );
- int gr_internal_string_clipped_m(int x, int y, char *s );
- char *find_kern_entry(grs_font *font,char first,char second)
- {
- ubyte *p=font->ft_kerndata;
- while (*p!=255)
- if (p[0]==first && p[1]==second)
- return p;
- else p+=3;
- return NULL;
- }
- //takes the character AFTER being offset into font
- #define INFONT(_c) ((_c >= 0) && (_c <= FMAXCHAR-FMINCHAR))
- //takes the character BEFORE being offset into current font
- get_char_width(int c,int c2,int *width,int *spacing)
- {
- int letter;
- letter = c-FMINCHAR;
- if (!INFONT(letter)) { //not in font, draw as space
- *width=0;
- if (FFLAGS & FT_PROPORTIONAL)
- *spacing = FWIDTH/2;
- else
- *spacing = FWIDTH;
- return;
- }
- if (FFLAGS & FT_PROPORTIONAL)
- *width = FWIDTHS[letter];
- else
- *width = FWIDTH;
- *spacing = *width;
- if (FFLAGS & FT_KERNED) {
- ubyte *p;
- if (!(c2==0 || c2=='\n')) {
- int letter2;
- letter2 = c2-FMINCHAR;
- if (INFONT(letter2)) {
- p = find_kern_entry(FONT,letter,letter2);
- if (p)
- *spacing = p[2];
- }
- }
- }
- }
- int get_centered_x(char *s)
- {
- int w,w2,s2;
- for (w=0;*s!=0 && *s!='\n';s++) {
- get_char_width(s[0],s[1],&w2,&s2);
- w += s2;
- }
- return ((grd_curcanv->cv_bitmap.bm_w - w) / 2);
- }
- int gr_internal_string0(int x, int y, char *s )
- {
- unsigned char * fp;
- char * text_ptr, * next_row, * text_ptr1;
- int r, BitMask, i, bits, width, spacing, letter, underline;
- unsigned int VideoOffset, VideoOffset1;
- VideoOffset1 = y * ROWSIZE + x;
- next_row = s;
- while (next_row != NULL )
- {
- text_ptr1 = next_row;
- next_row = NULL;
- if (x==0x8000) { //centered
- int xx = get_centered_x(text_ptr1);
- VideoOffset1 = y * ROWSIZE + xx;
- }
- for (r=0; r<FHEIGHT; r++)
- {
- text_ptr = text_ptr1;
- VideoOffset = VideoOffset1;
- while (*text_ptr)
- {
- if (*text_ptr == '\n' )
- {
- next_row = &text_ptr[1];
- break;
- }
- underline = 0;
- if (*text_ptr == '&' )
- {
- if ((r==FBASELINE+2) || (r==FBASELINE+3))
- underline = 1;
- text_ptr++;
- }
- get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
- letter = *text_ptr-FMINCHAR;
- if (!INFONT(letter)) { //not in font, draw as space
- VideoOffset += spacing;
- text_ptr++;
- continue;
- }
- if (FFLAGS & FT_PROPORTIONAL)
- fp = FCHARS[letter];
- else
- fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
- if (underline)
- for (i=0; i< width; i++ )
- DATA[VideoOffset++] = FG_COLOR;
- else
- {
- fp += BITS_TO_BYTES(width)*r;
- BitMask = 0;
- for (i=0; i< width; i++ )
- {
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask)
- DATA[VideoOffset++] = FG_COLOR;
- else
- DATA[VideoOffset++] = BG_COLOR;
- BitMask >>= 1;
- }
- }
- VideoOffset += spacing-width; //for kerning
- text_ptr++;
- }
- VideoOffset1 += ROWSIZE; y++;
- }
- }
- return 0;
- }
- int gr_internal_string0m(int x, int y, char *s )
- {
- unsigned char * fp;
- char * text_ptr, * next_row, * text_ptr1;
- int r, BitMask, i, bits, width, spacing, letter, underline;
- unsigned int VideoOffset, VideoOffset1;
- VideoOffset1 = y * ROWSIZE + x;
- next_row = s;
- while (next_row != NULL )
- {
- text_ptr1 = next_row;
- next_row = NULL;
- if (x==0x8000) { //centered
- int xx = get_centered_x(text_ptr1);
- VideoOffset1 = y * ROWSIZE + xx;
- }
- for (r=0; r<FHEIGHT; r++)
- {
- text_ptr = text_ptr1;
- VideoOffset = VideoOffset1;
- while (*text_ptr)
- {
- if (*text_ptr == '\n' )
- {
- next_row = &text_ptr[1];
- break;
- }
- underline = 0;
- if (*text_ptr == '&' )
- {
- if ((r==FBASELINE+2) || (r==FBASELINE+3))
- underline = 1;
- text_ptr++;
- }
- get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
- letter = *text_ptr-FMINCHAR;
- if (!INFONT(letter)) { //not in font, draw as space
- VideoOffset += spacing;
- text_ptr++;
- continue;
- }
- if (FFLAGS & FT_PROPORTIONAL)
- fp = FCHARS[letter];
- else
- fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
- if (underline)
- for (i=0; i< width; i++ )
- DATA[VideoOffset++] = FG_COLOR;
- else
- {
- fp += BITS_TO_BYTES(width)*r;
- BitMask = 0;
- for (i=0; i< width; i++ )
- {
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask)
- DATA[VideoOffset++] = FG_COLOR;
- else
- VideoOffset++;
- BitMask >>= 1;
- }
- }
- text_ptr++;
- VideoOffset += spacing-width;
- }
- VideoOffset1 += ROWSIZE; y++;
- }
- }
- return 0;
- }
- int gr_internal_string2(int x, int y, char *s )
- {
- unsigned char * fp;
- char * text_ptr, * next_row, * text_ptr1;
- int r, BitMask, i, bits, width, spacing, letter, underline;
- int page_switched;
- unsigned int VideoOffset, VideoOffset1;
- VideoOffset1 = (unsigned int)DATA + y * ROWSIZE + x;
- gr_vesa_setpage(VideoOffset1 >> 16);
- VideoOffset1 &= 0xFFFF;
- next_row = s;
- while (next_row != NULL )
- {
- text_ptr1 = next_row;
- next_row = NULL;
- if (x==0x8000) { //centered
- int xx = get_centered_x(text_ptr1);
- VideoOffset1 = y * ROWSIZE + xx;
- gr_vesa_setpage(VideoOffset1 >> 16);
- VideoOffset1 &= 0xFFFF;
- }
- for (r=0; r<FHEIGHT; r++)
- {
- text_ptr = text_ptr1;
- VideoOffset = VideoOffset1;
- page_switched = 0;
- while (*text_ptr)
- {
- if (*text_ptr == '\n' )
- {
- next_row = &text_ptr[1];
- break;
- }
- underline = 0;
- if (*text_ptr == '&' )
- {
- if ((r==FBASELINE+2) || (r==FBASELINE+3))
- underline = 1;
- text_ptr++;
- }
- get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
- Assert(width==spacing); //no kerning support here
- letter = *text_ptr-FMINCHAR;
- if (!INFONT(letter)) { //not in font, draw as space
- VideoOffset += spacing;
- text_ptr++;
- continue;
- }
- if (FFLAGS & FT_PROPORTIONAL)
- fp = FCHARS[letter];
- else
- fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
- if (underline)
- {
- if ( VideoOffset+width > 0xFFFF )
- {
- for (i=0; i< width; i++ )
- {
- gr_video_memory[VideoOffset++] = FG_COLOR;
- if (VideoOffset > 0xFFFF )
- {
- VideoOffset -= 0xFFFF + 1;
- page_switched = 1;
- gr_vesa_incpage();
- }
- }
- }
- else
- {
- for (i=0; i< width; i++ )
- gr_video_memory[VideoOffset++] = FG_COLOR;
- }
- }
- else
- {
- // fp -- dword
- // VideoOffset
- // width
- fp += BITS_TO_BYTES(width)*r;
- BitMask = 0;
- if ( VideoOffset+width > 0xFFFF )
- {
- for (i=0; i< width; i++ )
- {
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask)
- gr_video_memory[VideoOffset++] = FG_COLOR;
- else
- gr_video_memory[VideoOffset++] = BG_COLOR;
- BitMask >>= 1;
- if (VideoOffset > 0xFFFF )
- {
- VideoOffset -= 0xFFFF + 1;
- page_switched = 1;
- gr_vesa_incpage();
- }
- }
- } else {
- if (width == 8 )
- {
- bits = *fp++;
- if (bits & 0x80) gr_video_memory[VideoOffset+0] = FG_COLOR;
- else gr_video_memory[VideoOffset+0] = BG_COLOR;
- if (bits & 0x40) gr_video_memory[VideoOffset+1] = FG_COLOR;
- else gr_video_memory[VideoOffset+1] = BG_COLOR;
- if (bits & 0x20) gr_video_memory[VideoOffset+2] = FG_COLOR;
- else gr_video_memory[VideoOffset+2] = BG_COLOR;
- if (bits & 0x10) gr_video_memory[VideoOffset+3] = FG_COLOR;
- else gr_video_memory[VideoOffset+3] = BG_COLOR;
- if (bits & 0x08) gr_video_memory[VideoOffset+4] = FG_COLOR;
- else gr_video_memory[VideoOffset+4] = BG_COLOR;
- if (bits & 0x04) gr_video_memory[VideoOffset+5] = FG_COLOR;
- else gr_video_memory[VideoOffset+5] = BG_COLOR;
- if (bits & 0x02) gr_video_memory[VideoOffset+6] = FG_COLOR;
- else gr_video_memory[VideoOffset+6] = BG_COLOR;
- if (bits & 0x01) gr_video_memory[VideoOffset+7] = FG_COLOR;
- else gr_video_memory[VideoOffset+7] = BG_COLOR;
- VideoOffset += 8;
- } else {
- for (i=0; i< width/2 ; i++ )
- {
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask)
- gr_video_memory[VideoOffset++] = FG_COLOR;
- else
- gr_video_memory[VideoOffset++] = BG_COLOR;
- BitMask >>= 1;
- // Unroll twice
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask)
- gr_video_memory[VideoOffset++] = FG_COLOR;
- else
- gr_video_memory[VideoOffset++] = BG_COLOR;
- BitMask >>= 1;
- }
- }
- }
- }
- text_ptr++;
- }
- VideoOffset1 += ROWSIZE; y++;
- if (VideoOffset1 > 0xFFFF ) {
- VideoOffset1 -= 0xFFFF + 1;
- if (!page_switched)
- gr_vesa_incpage();
- }
- }
- }
- return 0;
- }
- int gr_internal_string2m(int x, int y, char *s )
- {
- unsigned char * fp;
- char * text_ptr, * next_row, * text_ptr1;
- int r, BitMask, i, bits, width, spacing, letter, underline;
- int page_switched;
- unsigned int VideoOffset, VideoOffset1;
- VideoOffset1 = (unsigned int)DATA + y * ROWSIZE + x;
- gr_vesa_setpage(VideoOffset1 >> 16);
- VideoOffset1 &= 0xFFFF;
- next_row = s;
- while (next_row != NULL )
- {
- text_ptr1 = next_row;
- next_row = NULL;
- if (x==0x8000) { //centered
- int xx = get_centered_x(text_ptr1);
- VideoOffset1 = y * ROWSIZE + xx;
- gr_vesa_setpage(VideoOffset1 >> 16);
- VideoOffset1 &= 0xFFFF;
- }
- for (r=0; r<FHEIGHT; r++)
- {
- text_ptr = text_ptr1;
- VideoOffset = VideoOffset1;
- page_switched = 0;
- while (*text_ptr)
- {
- if (*text_ptr == '\n' )
- {
- next_row = &text_ptr[1];
- break;
- }
- underline = 0;
- if (*text_ptr == '&' )
- {
- if ((r==FBASELINE+2) || (r==FBASELINE+3))
- underline = 1;
- text_ptr++;
- }
- get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
- letter = *text_ptr-FMINCHAR;
- if (!INFONT(letter)) { //not in font, draw as space
- VideoOffset += width;
- text_ptr++;
- continue;
- }
- if (FFLAGS & FT_PROPORTIONAL)
- fp = FCHARS[letter];
- else
- fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
- if (underline)
- {
- if ( VideoOffset+width > 0xFFFF )
- {
- for (i=0; i< width; i++ )
- {
- gr_video_memory[VideoOffset++] = FG_COLOR;
- if (VideoOffset > 0xFFFF )
- {
- VideoOffset -= 0xFFFF + 1;
- page_switched = 1;
- gr_vesa_incpage();
- }
- }
- }
- else
- {
- for (i=0; i< width; i++ )
- gr_video_memory[VideoOffset++] = FG_COLOR;
- }
- }
- else
- {
- fp += BITS_TO_BYTES(width)*r;
- BitMask = 0;
- if ( VideoOffset+width > 0xFFFF )
- {
- for (i=0; i< width; i++ )
- {
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask)
- gr_video_memory[VideoOffset++] = FG_COLOR;
- else
- VideoOffset++;
- BitMask >>= 1;
- if (VideoOffset > 0xFFFF )
- {
- VideoOffset -= 0xFFFF + 1;
- page_switched = 1;
- gr_vesa_incpage();
- }
- }
- } else {
- for (i=0; i< width; i++ )
- {
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask)
- gr_video_memory[VideoOffset++] = FG_COLOR;
- else
- VideoOffset++;;
- BitMask >>= 1;
- }
- }
- }
- text_ptr++;
- VideoOffset += spacing-width;
- }
- VideoOffset1 += ROWSIZE; y++;
- if (VideoOffset1 > 0xFFFF ) {
- VideoOffset1 -= 0xFFFF + 1;
- if (!page_switched)
- gr_vesa_incpage();
- }
- }
- }
- return 0;
- }
- //a bitmap for the character
- grs_bitmap char_bm = {
- 0,0,0,0, //x,y,w,h
- BM_LINEAR, //type
- BM_FLAG_TRANSPARENT, //flags
- 0, //rowsize
- NULL, //data
- 0 //selector
- };
- int gr_internal_color_string(int x, int y, char *s )
- {
- unsigned char * fp;
- char * text_ptr, * next_row, * text_ptr1;
- int width, spacing,letter;
- int xx,yy;
- char_bm.bm_h = FHEIGHT; //set height for chars of this font
- next_row = s;
- yy = y;
- while (next_row != NULL)
- {
- text_ptr1 = next_row;
- next_row = NULL;
- text_ptr = text_ptr1;
- xx = x;
- if (xx==0x8000) //centered
- xx = get_centered_x(text_ptr);
- while (*text_ptr)
- {
- if (*text_ptr == '\n' )
- {
- next_row = &text_ptr[1];
- yy += FHEIGHT;
- break;
- }
- letter = *text_ptr-FMINCHAR;
- get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
- if (!INFONT(letter)) { //not in font, draw as space
- xx += spacing;
- text_ptr++;
- continue;
- }
- if (FFLAGS & FT_PROPORTIONAL)
- fp = FCHARS[letter];
- else
- fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
- char_bm.bm_w = char_bm.bm_rowsize = width;
- char_bm.bm_data = fp;
- gr_bitmapm(xx,yy,&char_bm);
- xx += spacing;
- text_ptr++;
- }
- }
- return 0;
- }
- int gr_string(int x, int y, char *s )
- {
- int w, h, aw;
- int clipped=0;
- if ( x == 0x8000 ) {
- if ( y<0 ) clipped |= 1;
- gr_get_string_size(s, &w, &h, &aw );
- // for x, since this will be centered, only look at
- // width.
- if ( w > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
- if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
- if ( (y+h) < 0 ) clipped |= 2;
- if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
- } else {
- if ( (x<0) || (y<0) ) clipped |= 1;
- gr_get_string_size(s, &w, &h, &aw );
- if ( (x+w) > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
- if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
- if ( (x+w) < 0 ) clipped |= 2;
- if ( (y+h) < 0 ) clipped |= 2;
- if ( x > grd_curcanv->cv_bitmap.bm_w ) clipped |= 2;
- if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
- }
- if ( !clipped )
- return gr_ustring(x, y, s );
-
- if ( clipped & 2 ) {
- // Completely clipped...
- mprintf( (1, "Text '%s' at (%d,%d) is off screen!\n", s, x, y ));
- return 0;
- }
- if ( clipped & 1 ) {
- // Partially clipped...
- //mprintf( (0, "Text '%s' at (%d,%d) is getting clipped!\n", s, x, y ));
- }
- // Partially clipped...
- if (FFLAGS & FT_COLOR)
- return gr_internal_color_string( x, y, s);
- if ( BG_COLOR == -1)
- return gr_internal_string_clipped_m( x, y, s );
-
- return gr_internal_string_clipped( x, y, s );
- }
- int gr_ustring(int x, int y, char *s )
- {
- if (FFLAGS & FT_COLOR) {
- return gr_internal_color_string(x,y,s);
- }
- else
- switch( TYPE )
- {
- case BM_LINEAR:
- if ( BG_COLOR == -1)
- return gr_internal_string0m(x,y,s);
- else
- return gr_internal_string0(x,y,s);
- case BM_SVGA:
- if ( BG_COLOR == -1)
- return gr_internal_string2m(x,y,s);
- else
- return gr_internal_string2(x,y,s);
- }
- return 0;
- }
- void gr_get_string_size(char *s, int *string_width, int *string_height, int *average_width )
- {
- int i = 0, longest_width = 0;
- int width,spacing;
- *string_height = FHEIGHT;
- *string_width = 0;
- *average_width = FWIDTH;
- if (s != NULL )
- {
- *string_width = 0;
- while (*s)
- {
- // if (*s == '&')
- // s++;
- while (*s == '\n')
- {
- s++;
- *string_height += FHEIGHT;
- *string_width = 0;
- }
- if (*s == 0) break;
- get_char_width(s[0],s[1],&width,&spacing);
- *string_width += spacing;
- if (*string_width > longest_width)
- longest_width = *string_width;
- i++;
- s++;
- }
- }
- *string_width = longest_width;
- }
- int gr_uprintf( int x, int y, char * format, ... )
- {
- char buffer[1000];
- va_list args;
- va_start(args, format );
- vsprintf(buffer,format,args);
- return gr_ustring( x, y, buffer );
- }
- int gr_printf( int x, int y, char * format, ... )
- {
- char buffer[1000];
- va_list args;
- va_start(args, format );
- vsprintf(buffer,format,args);
- return gr_string( x, y, buffer );
- }
- void gr_close_font( grs_font * font )
- {
- if (font)
- {
- if ( font->ft_chars )
- free( font->ft_chars );
- free( font );
- }
- }
- void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq );
- void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
- #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
- "again_ddn:" \
- "xor eax,eax" \
- "mov al,[esi]" \
- "inc dword ptr [ebx+eax*4]" \
- "mov al,[edi+eax]" \
- "mov [esi],al" \
- "inc esi" \
- "dec ecx" \
- "jne again_ddn"
- grs_font * gr_init_font( char * fontname )
- {
- grs_font *font;
- int i;
- unsigned char * ptr;
- int nchars;
- CFILE *fontfile;
- int file_id;
- int datasize; //size up to (but not including) palette
- fontfile = cfopen(fontname, "rb");
- if (!fontfile)
- Error( "Can't open font file %s", fontname );
- cfread(&file_id,sizeof(file_id),1,fontfile);
- cfread(&datasize,sizeof(datasize),1,fontfile);
- if (file_id != 'NFSP')
- Error( "File %s is not a font file", fontname );
- font = (grs_font *) malloc(datasize);
- cfread(font,1,datasize,fontfile);
- nchars = font->ft_maxchar-font->ft_minchar+1;
- if (font->ft_flags & FT_PROPORTIONAL) {
- font->ft_widths = (short *) (((int) font->ft_widths) + ((ubyte *) font));
- font->ft_data = ((int) font->ft_data) + ((ubyte *) font);
- font->ft_chars = (unsigned char **)malloc( nchars * sizeof(unsigned char *));
- ptr = font->ft_data;
- for (i=0; i< nchars; i++ ) {
- font->ft_chars[i] = ptr;
- if (font->ft_flags & FT_COLOR)
- ptr += font->ft_widths[i] * font->ft_h;
- else
- ptr += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
- }
- } else {
- font->ft_data = ((unsigned char *) font) + sizeof(*font);
- font->ft_chars = NULL;
- font->ft_widths = NULL;
- ptr = font->ft_data + (nchars * font->ft_w * font->ft_h);
- }
- if (font->ft_flags & FT_KERNED)
- font->ft_kerndata = ((int) font->ft_kerndata) + ((ubyte *) font);
- if (font->ft_flags & FT_COLOR) { //remap palette
- ubyte palette[256*3];
- ubyte colormap[256];
- int freq[256];
- cfread(palette,3,256,fontfile); //read the palette
- build_colormap_good( &palette, colormap, freq );
-
- colormap[255] = 255;
- decode_data_asm(font->ft_data, ptr-font->ft_data, colormap, freq );
- }
- cfclose(fontfile);
- //set curcanv vars
- FONT = font;
- FG_COLOR = 0;
- BG_COLOR = 0;
- return font;
- }
- void gr_set_fontcolor( int fg, int bg )
- {
- FG_COLOR = fg;
- BG_COLOR = bg;
- }
- void gr_set_curfont( grs_font * new )
- {
- FONT = new;
- }
- int gr_internal_string_clipped(int x, int y, char *s )
- {
- unsigned char * fp;
- char * text_ptr, * next_row, * text_ptr1;
- int r, BitMask, i, bits, width, spacing, letter, underline;
- int x1 = x, last_x;
-
- next_row = s;
- while (next_row != NULL )
- {
- text_ptr1 = next_row;
- next_row = NULL;
- x = x1;
- if (x==0x8000) //centered
- x = get_centered_x(text_ptr1);
- last_x = x;
- for (r=0; r<FHEIGHT; r++) {
- text_ptr = text_ptr1;
- x = last_x;
- while (*text_ptr) {
- if (*text_ptr == '\n' ) {
- next_row = &text_ptr[1];
- break;
- }
- underline = 0;
- if (*text_ptr == '&' ) {
- if ((r==FBASELINE+2) || (r==FBASELINE+3))
- underline = 1;
- text_ptr++;
- }
- get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
- letter = *text_ptr-FMINCHAR;
- if (!INFONT(letter)) { //not in font, draw as space
- x += spacing;
- text_ptr++;
- continue;
- }
- if (FFLAGS & FT_PROPORTIONAL)
- fp = FCHARS[letter];
- else
- fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
- if (underline) {
- for (i=0; i< width; i++ ) {
- gr_setcolor(FG_COLOR);
- gr_pixel( x++, y );
- }
- } else {
- fp += BITS_TO_BYTES(width)*r;
- BitMask = 0;
- for (i=0; i< width; i++ ) {
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask)
- gr_setcolor(FG_COLOR);
- else
- gr_setcolor(BG_COLOR);
- gr_pixel( x++, y );
- BitMask >>= 1;
- }
- }
- x += spacing-width; //for kerning
- text_ptr++;
- }
- y++;
- }
- }
- return 0;
- }
- int gr_internal_string_clipped_m(int x, int y, char *s )
- {
- unsigned char * fp;
- char * text_ptr, * next_row, * text_ptr1;
- int r, BitMask, i, bits, width, spacing, letter, underline;
- int x1 = x, last_x;
-
- next_row = s;
- while (next_row != NULL )
- {
- text_ptr1 = next_row;
- next_row = NULL;
- x = x1;
- if (x==0x8000) //centered
- x = get_centered_x(text_ptr1);
- last_x = x;
- for (r=0; r<FHEIGHT; r++) {
- x = last_x;
- text_ptr = text_ptr1;
- while (*text_ptr) {
- if (*text_ptr == '\n' ) {
- next_row = &text_ptr[1];
- break;
- }
- underline = 0;
- if (*text_ptr == '&' ) {
- if ((r==FBASELINE+2) || (r==FBASELINE+3))
- underline = 1;
- text_ptr++;
- }
- get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
- letter = *text_ptr-FMINCHAR;
- if (!INFONT(letter)) { //not in font, draw as space
- x += spacing;
- text_ptr++;
- continue;
- }
- if (FFLAGS & FT_PROPORTIONAL)
- fp = FCHARS[letter];
- else
- fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
- if (underline) {
- for (i=0; i< width; i++ ) {
- gr_setcolor(FG_COLOR);
- gr_pixel( x++, y );
- }
- } else {
- fp += BITS_TO_BYTES(width)*r;
- BitMask = 0;
- for (i=0; i< width; i++ ) {
- if (BitMask==0) {
- bits = *fp++;
- BitMask = 0x80;
- }
- if (bits & BitMask) {
- gr_setcolor(FG_COLOR);
- gr_pixel( x++, y );
- } else {
- x++;
- }
- BitMask >>= 1;
- }
- }
- x += spacing-width; //for kerning
- text_ptr++;
- }
- y++;
- }
- }
- return 0;
- }
|