home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / EGA_VGA / VWFONT13.ZIP / VIEWFONT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-27  |  6.4 KB  |  292 lines

  1. /*********************************************************************
  2.  * ViewFont.C    Version 1.3       Copyright (c) 1989 Jim Bumgardner
  3.  *
  4.  * Second Release (Version 1.3)   8-25-89
  5.  * First Release (Version 1.2)    11-08-88
  6.  *
  7.  **********************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12. #include <alloc.h>
  13. #include <graphics.h>
  14.  
  15. /* This is the maximum character bitmap size */
  16. #define MAXCHARSIZE    18000
  17.  
  18. /* Keyboard Definitions */
  19. #define ESC        27
  20. #define DOWN        0x5000
  21. #define UP        0x4800
  22. #define RIGHT        0x4D00
  23. #define LEFT        0x4B00
  24. #define PGUP        0x4900
  25. #define PGDN        0x5100
  26. #define HOME        0x4700
  27. #define END        0x4F00
  28.  
  29. struct fhead_struct {
  30.     int fil26;
  31.     char nul1,font_type;
  32.     int nul2,baseline,width,height;
  33.     char orient,prop;
  34.     int symset,pitch,points,nul3;
  35.     char nul4,style,weight,type_face;
  36. } font_header;
  37.  
  38. struct chead_struct {
  39.     char fil4,nul1,fil14,cil1;
  40.     char orient,nul2;
  41.     int left_offset,top_offset,width,height,delta_x;
  42. } char_header;
  43.  
  44. int g_driver = DETECT,g_mode;
  45. int maxx,maxy;
  46.  
  47. long *char_ptrs;
  48. int *ibuf,*bmap;
  49.  
  50. FILE *ifile;
  51.  
  52. int mbytes,maxcx,maxcy,maxcs;
  53. int min_c = 256,max_c = 0;
  54.  
  55. char syntax[] = 
  56. "VIEWFONT Version 1.3\t\t\t\tCopyright (c) 1989 Jim Bumgarder\n\n"
  57. "Syntax:\n"
  58. "\tVIEWFONT fontfile[.sfp]\n\n"
  59. "\tUse Arrow keys or letters to view, ESC to quit.\n";
  60.  
  61. main(argc,argv)
  62. char *argv[];
  63. {
  64.     int c,ch,och;
  65.     char fname[33];
  66.  
  67.     if (argc == 1 || argv[1][0] == '?')
  68.         erxit(syntax);
  69.  
  70.     if ((ibuf = malloc(MAXCHARSIZE)) == NULL ||
  71.         (char_ptrs = malloc(sizeof(long)*256)) == NULL)
  72.         erxit("Not Enough Memory");
  73.  
  74.     bmap = &ibuf[2];
  75.  
  76.     strcpy(fname,argv[1]);
  77.     if (strchr(fname,'.') == 0)
  78.         strcat(fname,".SFP");
  79.     if ((ifile = fopen(fname,"rb")) == NULL)
  80.         erxit("Can't open Input File: %s\n",fname);
  81.  
  82.     for (c=0; c<256; ++c)
  83.         char_ptrs[c]=0L;
  84.  
  85.     get_header();
  86.     mark_chars();
  87.  
  88.     registerbgidriver(EGAVGA_driver); 
  89.     registerbgidriver(CGA_driver);     
  90.     registerbgidriver(Herc_driver);
  91.  
  92.     initgraph(&g_driver,&g_mode,"");
  93.     if (g_driver < 0)
  94.         erxit("No Graphics, or .BGI driver Available");
  95.     setbkcolor(1);
  96.  
  97.     maxx = getmaxx();
  98.     maxy = getmaxy();
  99.     maxcx = maxx/(mbytes<<3);
  100.     maxcy = maxy/font_header.height;
  101.     maxcs = maxcx*maxcy;
  102.     
  103.     ch = min_c;
  104.     och = -1;
  105.     while (1) {
  106.         if (och != ch) {
  107.             och = ch;
  108.             show_bmaps(ch);
  109.         }
  110.  
  111.         c = getch();
  112.         if (c == 0)    c = getch() << 8;
  113.         switch (c) {
  114.         case 3:
  115.         case 26:
  116.         case ESC:    fr_exit();
  117.         case HOME:    ch = min_c;    break;
  118.         case END:    ch = max_c - maxcs + 1;
  119.                 if (ch < min_c)    ch = min_c;
  120.                 break;
  121.         case RIGHT:    if (ch < max_c)    ++ch;    break;
  122.         case '\b':
  123.         case LEFT:    if (ch > min_c)    --ch;    break;
  124.         case ' ':
  125.         case DOWN:    if (ch+maxcx <= max_c)    ch += maxcx;
  126.                 break;
  127.         case UP:    if (ch-maxcx >= min_c)    ch -= maxcx;
  128.                 break;
  129.         case '\r':
  130.         case PGDN:    if (ch+maxcs <= max_c)    ch += maxcs;
  131.                 break;
  132.         case PGUP:    if (ch-maxcs >= min_c)    ch -= maxcs;
  133.                 break;
  134.         default:    if (c >= '!' && c <= max_c)
  135.                     ch = c;
  136.                 break;
  137.         }
  138.     }
  139. }
  140.  
  141. get_header()
  142. {
  143.     unsigned int tmp;
  144.     int c;
  145.  
  146.     fseek(ifile,0L,0);
  147.     do {
  148.         while ((c = getc(ifile)) != EOF && c != ESC)
  149.             ;
  150.         if (c == EOF)
  151.             erxit("Invalid Font File");
  152.         tmp = getw(ifile);
  153.     } while (tmp != ')s');
  154.     tmp = get_number();
  155.     fread(&ibuf[0],tmp,1,ifile);
  156.     memcpy(&font_header,&ibuf[0],sizeof(font_header));
  157.     font_header.width = iswap(font_header.width);
  158.     font_header.height = iswap(font_header.height);
  159.     font_header.baseline = iswap(font_header.baseline);
  160.     mbytes = ((font_header.width-1) >> 3) + 1;
  161. }
  162.  
  163. mark_chars()
  164. {
  165.     long fptr,ftell();
  166.     int cno;
  167.     while (1) {
  168.         fptr = ftell(ifile);
  169.         if ((cno = get_char_header()) == EOF)    return;
  170.         if (cno > max_c)    max_c = cno;
  171.         if (cno < min_c)    min_c = cno;
  172.         char_ptrs[cno] = fptr;
  173.     }
  174. }
  175.  
  176. get_char(cno)
  177. {
  178.     if (char_ptrs[cno] == 0L)
  179.         return(0);
  180.     fseek(ifile,char_ptrs[cno],0);
  181.     get_char_header();
  182.     return(1);
  183. }
  184.  
  185. get_char_header()
  186. {
  187.     int tmp;
  188.     int c,cno;
  189.     do {
  190.         while ((c = getc(ifile)) != EOF && c != ESC)
  191.             ;
  192.         if (c == EOF)        return EOF;
  193.         tmp = getw(ifile);
  194.     } while (tmp != '*c');
  195.     cno = get_number();
  196.     do {
  197.         while ((c = getc(ifile)) != EOF && c != ESC)
  198.             ;
  199.         if (c == EOF)        return EOF;
  200.         tmp = getw(ifile);
  201.     } while (tmp != '(s');
  202.     tmp = get_number();
  203.  
  204.     fread(&char_header,sizeof(char_header),1,ifile);
  205.     fread(bmap,tmp-sizeof(char_header),1,ifile);
  206.  
  207.     char_header.width = iswap(char_header.width);
  208.     char_header.height = iswap(char_header.height);
  209.     char_header.top_offset = iswap(char_header.top_offset);
  210.     char_header.left_offset = iswap(char_header.left_offset);
  211.     char_header.delta_x = iswap(char_header.delta_x)>>2;
  212.  
  213.     ibuf[0] = char_header.width-1 | 0x07;    
  214.     ibuf[1] = char_header.height-1;
  215.     return cno;
  216. }
  217.  
  218. show_bmaps(ch)
  219. {
  220.     int xf,yf,c;
  221.  
  222.     cleardevice();
  223.     setcolor(7);
  224.  
  225.     xf = 16; yf = 0;                            /* Start indented */
  226.     for (c = ch; c <= max_c; ++c)
  227.     {
  228.         if (get_char(c) == 0)    continue;
  229.         if (xf + (mbytes << 3) > maxx)
  230.             {
  231.             yf += font_header.height;
  232.             xf = 16;
  233.             if (yf + font_header.height > maxy)    return;
  234.             }
  235.  
  236.         if (g_driver==CGA || g_driver==HERCMONO)    /* if one color, draw before */
  237.             putimage(xf,yf+font_header.baseline-char_header.top_offset,ibuf,0);
  238.  
  239.         xf -= char_header.left_offset;
  240.         line(xf,yf+font_header.height-1,                      /* Draw bottom */
  241.                 xf+mbytes<<3,yf+font_header.height-1);
  242.         line(xf,yf,xf,yf+font_header.height-1);          /* Left character line */
  243.         line(xf+char_header.delta_x,yf,                      /* Right character line*/
  244.                 xf+char_header.delta_x,yf+font_header.height-1);
  245.         line(xf,yf+font_header.baseline,                      /* Character baseline */
  246.             xf+char_header.delta_x,yf+font_header.baseline);
  247.         xf += char_header.left_offset;
  248.  
  249.         if (g_driver!=CGA && g_driver!=HERCMONO)
  250.             putbits(xf,yf+font_header.baseline-char_header.top_offset,
  251.                         char_header.width,char_header.height,bmap,15);
  252.         xf += (mbytes << 3);
  253.     }
  254.     yf += font_header.height;
  255.     if (yf-1 <= maxy)    line(0,yf-1,maxx,yf-1);            /* I haven't bothered */
  256. }                                                                    /* to omit this bottom line */
  257.  
  258. get_number()
  259. {
  260.     char *p;
  261.     int c;
  262.     p = (char *) ibuf;
  263.     while ((c = getc(ifile)) != EOF && isdigit(c))
  264.         *p++ = c;
  265.     *p = 0;
  266.     return(atoi((char *) ibuf));
  267. }
  268.  
  269. iswap(n)
  270. unsigned int n;
  271. {
  272.     return((n >> 8) | (n << 8));
  273. }
  274.  
  275. fr_exit()
  276. {
  277.     #ifndef DEBUG
  278.     closegraph();
  279.     #endif
  280.  
  281.     exit();
  282. }
  283.  
  284. erxit(char *tmp,...)
  285. {
  286.     va_list arg_ptr;
  287.     va_start(arg_ptr,tmp);
  288.     vfprintf(stderr,tmp,arg_ptr);
  289.     va_end(arg_ptr);
  290.     exit(0);
  291. }
  292.