/* VGA2MF.C is Copyright 1990 Humanities and Arts Computing Center */
/* usage: vga2mf [vgafontname] [metafont_sourcecode_output_file] */
/* suggestions to [email protected] */

/* history: */
/* v. 1.0 released 4/25/1990 */
/* this is a minimally modified version of Ega2mf */
/* modifications to handle larger VGA character cell */
/* one might also modify egamf to accept a -v switch */
/* to permit one program to do both */
/* but _we_ didn't do it that way because . . . */
/* "So many programs, so little time . . . */

#include <stdio.h>
#include <string.h>



/* the charblock structure is used for processing each character */
typedef struct {
              unsigned char column[8];
              } videorow;
typedef struct {
               videorow row[16];
              } charblock;

/* the vga font is a naked 8x16 bitmap 256 chars deep */
typedef struct {
               unsigned char bitmap[16];
               } vgachar;

typedef struct {
              vgachar charset[256];
              } vgafont;

/* globally accessible area */
charblock editchar;
/* global vars  and strings */
char infilename[128];
char outfilename[128];

jumpout() { exit(1); }

void copyright()
{
 puts("VGA2Mf  Screen font --> METAFONT code generator\n");
 puts("HUMANITIES AND ARTS COMPUTING CENTER, DR-10");
 puts("UNIVERSITY OF WASHINGTON, SEATTLE WA 98195 USA\n");
 puts("Copyright 1990 Humanities and Arts Computing Center\n");
 puts("This software may be used, modified, and redistributed free of any");
 puts("fee or royalty, provided that this copyright and 'free software' notice");
 puts("remains intact in any copies redistributed, and that you may charge no fee");
 puts("of any sort whatsoever without the written permission of the Humanities");
 puts("and Arts Computing Center.");
 return;
}


void exists (char *filename)
 {
   unsigned char response;
   FILE *handle;
   handle = fopen ( filename , "rb" ) ;
   if (handle != (FILE *) NULL)
      {  fclose(handle);
         printf("File %s already exists: O.K. to replace? (Y/N): ", filename);
         response = getchar();
         if ( ! ( (response=='y') || (response=='Y') ) )
           {
           printf("\nPlease rename files as necessary and try again.\n");
           jumpout();
           }
      }
   return;
   }

FILE *getopen(char *filename, char *access)
{
       int     temp;
       FILE    *handle;
       gets(filename);
       if (access[0]=='w' || access[0]=='W') exists(filename);
       handle=fopen(filename,access);
       if (handle == NULL) {
         puts("\nCan't open file ");
         puts(filename);
         puts("\n");
         }
       return handle;
}

FILE *get_file(char *access,char *prompt,char *filename)
{
       printf("\n %s",prompt);
       return getopen(filename,access);
}

void makedisplay(unsigned char thischar, vgafont *thisfont)
{
 int temp,mask,i,k,hold;
 for (i=0; i<16; i++) {
   temp=thisfont->charset[thischar].bitmap[i];
   mask=0x80;
   for (k=0; k<8; k++) {
     hold=temp & mask;
     if (hold==0) editchar.row[i].column[k]=0;
     else editchar.row[i].column[k]=1;
     mask=mask / 2;
   } /* endfor k */
 } /* endfor i */
 return;
}


main(int argc, char *argv[])
{
 FILE *f1=(FILE *) NULL;
 FILE *f2=(FILE *) NULL;
 int done,ch,i,k,codeout;
 char quote,percent;
 vgafont *thisvga;
 quote=34;
 percent=37;
 /* yes, yes! we are assuming the ASCII character set */
 copyright();
 thisvga = (vgafont *) malloc(sizeof(vgafont));
 if (argc > 1) {
   f1 = fopen(argv[1],"rb");
   (void) strcpy(infilename,argv[1]);
   }
 if (f1 == (FILE *) NULL) {
       /* prompt for and open input file */
       for ( ; f1 == (FILE *) NULL; f1=get_file("rb","Enter name of input vga font file: ",infilename)) ;
       }
 fread(thisvga,sizeof(vgafont),1,f1);
 fclose(f1);
 if (argc > 2) {
   (void) strcpy(outfilename,argv[2]);
   exists(outfilename);
   f2 = fopen(argv[2],"w");
   }
 if (f2 == (FILE *) NULL) {
       /* prompt for and open output file */
       for ( ; f2 == (FILE *) NULL; f2=get_file("w","Enter name of output Metafont source file: ",outfilename)) ;
       }
  fprintf(f2,"%c This is %s generated by VGA2MF from %s.\n",percent,outfilename,infilename);
  fprintf(f2,"if unknown cmbase: input cmbase fi\n");
  fprintf(f2,"mode_setup;\n");
  fprintf(f2,"def generate suffix t= enddef;\n");
  fprintf(f2,"input cmtt10; font_setup;\n");
  fprintf(f2,"if ligs>1: font_coding_scheme:=%cTeX text%c;\n",quote,quote);
  fprintf(f2,"else: font_coding_scheme:=if ligs=0: %cTeX typewriter text%c\n",quote,quote);
  fprintf(f2,"  else: %cTeX text without f-ligatures%c fi; fi \n",quote,quote);
  fprintf(f2,"numeric cheight; cheight=body_height+desc_depth;\n");
  fprintf(f2,"numeric hhalf; numeric vhalf; hhalf=.4u; vhalf=(cheight)/36;\n");
  fprintf(f2,"numeric vcent; vcent=cheight/32; numeric hcent; hcent=9/16u;\n");
  fprintf(f2,"def crt(expr row,col) =\n");
  fprintf(f2,"y.c:=body_height-(row * cheight / 16)-vcent; x.c:=hcent+(col * 9/8u);\n");
  fprintf(f2,"y.a:=y.c+vhalf; y.b:=y.c-vhalf;\n");
  fprintf(f2,"x.a:=x.c-hhalf; x.b:=x.c+hhalf;\n");
  fprintf(f2,"filldraw (x.a,y.b)..(x.a,y.a)..(x.b,y.a)..(x.b,y.b)..cycle;\n");
  fprintf(f2,"enddef;\n");
  printf("Now processing character ");
  for (ch=0; ch<256; ch++) {
    /* generate code for character ch */
    if ( (ch < 32) || (ch > 126) ) printf("[%d] ",ch);
    else printf("[%c] ",ch);
    fprintf(f2,"\nbeginchar(%d,9u#,body_height#,desc_depth#);",ch);
    fprintf(f2,"adjust_fit(0,0);\npickup pencircle scaled 0.2pt;\n");
    makedisplay(ch,thisvga);
    for(i=0; i < 16; i++) {
       codeout=0;
       for(k=0; k<8; k++) {
           if (editchar.row[i].column[k] != 0) {
               fprintf(f2,"crt(%d,%d); ",i,k);
               codeout=1;
               } /* endif set dot on */
       } /* end k */
       if (codeout > 0) fprintf(f2,"\n");
    } /* endfor i */
    fprintf(f2,"endchar;\n");
    if ( ( ch % 16 ) == 0 ) printf("\n");
  } /* endfor ch */
  fprintf(f2,"\n");
  fprintf(f2,"font_slant slant; font_x_height x_height#;\n");
  fprintf(f2," font_normal_space 9u#;\n");
  fprintf(f2," font_quad 18u#;\n");
  fprintf(f2," font_extra_space 9u#;\n");
  fprintf(f2,"bye.\n");
  printf("\nVGA2MF is done.  Good luck with your new font.\n");
  fclose(f2);
}