#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/StringDefs.h>
#include <math.h>

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/file.h>

#include "eview.h"
#include "char16.h"


struct eth_font {
   int bits[FONT_WIDTH][FONT_HEIGHT];
   int dwx0; 
   int bbh,bbw; 
   int bbxoff,my_yoff;                /* Store only what is needed later */

}  fidel[FIDELS];
 
int i,j,k,l,done,xloc,yloc,mail_xloc,mail_yloc,form_num,HEIGHT,WIDTH;
unsigned short tmp;
unsigned long foreground,fore_init, background;

extern struct flags {
                      char color[3][8]; 
                      unsigned long fg[3];
                      int y[4];
                    } flag;

int initx();

extern Display *mydisplay;
extern Drawable mywindow;
extern GC mygc;
extern Font font;
extern XFontStruct *ethfont,*latfont;
extern KeySym mykey;
extern int myscreen;
extern XEvent myevent;
extern void file_handler();
extern char getchr();
extern refresh();
void get_mail(),diqala(),alternate(),trans_eth(),trans_latin();

unsigned char NewCh;
char forms[] = "euiaE|oWUIAEO",LastCh,NextCh;
int  eth=0,eth0,file=0,kerning,nolat=0,un_line,last_un,ethspace=0;
int  hard_mode=0, first_k=1;           /* flags */
int vowel,punct,consonant,number,hohe,sadis=0,Enum,form=NIL;
unsigned new=NIL,last=NIL;

char bg_color[12],fg_color[12],fg_init[12],screen_color[12],font_name[80];
char eth_font_name[80],lat_font_name[80],Mail[7];
int  bandira=0,mail,mail_check=0,page=0;
int  no_eth_font=0,no_lat_font=0,num_lines;     /* file read flags */
FILE *inptr;

main(argc,argv)
int argc;
char **argv;
{

  input_handler(argc, argv); 

  initx();

  if(no_eth_font) {
     if ( read_fidel() ){
       fprintf(stderr,"\nERROR OPENING FONT FILE!!!\n");
       exit(1);
     }
  }

  printf("Move Pointer over Viewing Window and Hit any Key to Begin and Continue\n");

  done = 0;
  xloc = LEFT;
  yloc = TOP;
  j=0;

  while (!done) {
    if ( XEventsQueued ( mydisplay, QueuedAfterFlush ) > 0 )
    {
      XNextEvent ( mydisplay, &myevent );
      switch ( myevent.type ) {
        case Expose:
	/*  redraw_screen(xloc,yloc); */
  	  break;

        case MappingNotify:
          XRefreshKeyboardMapping ((XMappingEvent *)&myevent);
/*	  redraw_screen(xloc,yloc); */
          break;

        case ButtonPress:
  	  break;

        case KeyPress: 
          disp_text();  
          break;					/* end KeyPress */

        default:
          break;
      }						/* end Event switch */
    }						/* end if Queued */
  }						/* end while */

  XFreeGC (mydisplay, mygc);
  XDestroyWindow (mydisplay, mywindow);
  XCloseDisplay (mydisplay);

  exit(0);

} 


/*  DISP_FIDEL  -  displays fidel (row,col) at position (xloc,yloc)  */

disp_fidel ( addr )
unsigned addr;
{
int i,j,k,l,jmax,old_xloc;
char ch,ch2;
XChar2b thechar;
jmax = 0;
/*printf("DISP_FIDEL - dispalying %c as %,%d\n",addr,xloc,yloc); */
  if ( addr <= FIDELS  ) {  
    if(no_eth_font) { 
      xloc += fidel[addr].bbxoff;
      for (i=0; i<fidel[addr].bbh; i++)
        for (j=0; j<fidel[addr].bbw; j++) 
            if ( fidel[addr].bits[j][i] ) 
              XDrawPoint(mydisplay,mywindow,mygc,(xloc)+j,(yloc+fidel[addr].my_yoff)+i);
    }   
    else {
           thechar.byte1 = (addr >> 8);
           thechar.byte2 = (addr & 255);
           XDrawString16(mydisplay,mywindow,mygc,xloc,(yloc+FIDEL_HEIGHT),&thechar,1);
         }

    old_xloc = xloc;

    if(kerning) {
       if(no_eth_font) xloc += fidel[addr].dwx0;
          else         xloc += XTextWidth16(ethfont,&thechar,1);
    }
    else if(addr == SPACE)     xloc += SPACE_SIZE;
           else                xloc += FIDEL_WIDTH;
                                                        
   if (un_line) 
      XDrawLine (mydisplay,mywindow,mygc,old_xloc,yloc+FIDEL_HEIGHT+2,xloc,yloc+FIDEL_HEIGHT+2);

      if ( xloc >= RIGHT ) {
         xloc = LEFT;
         num_lines++;
         yloc += FIDEL_HEIGHT;
      if ( yloc >= BOTTOM )
         yloc = TOP;
      }

    
  }
  else 					/* Else its a control character */
    switch ( addr ) {
      case RETURN:
        xloc = LEFT;
        yloc += FIDEL_HEIGHT;
        if ( yloc >= BOTTOM ) yloc = TOP;

 
       if(page) {
          if(last_un)  last_un = 0;
          if(yloc != TOP)
	      erase_line( yloc+FIDEL_HEIGHT ); 
       }
	break;

      default:					/* Nada */
        break;
    }						/* End switch */

}


get_color_pixel(mydisplay,colorname,ground)
Display *mydisplay;
char *colorname;
int ground;
{
 int      scr = DefaultScreen(mydisplay);
 Colormap cmap = DefaultColormap(mydisplay, scr);
 XColor   color, ignore;

 if(XAllocNamedColor(mydisplay, cmap, colorname, colorname, &color, &ignore))
   return (color.pixel);
 else{
  printf("Warning:  Couldn't allocate color %s\n", colorname);
  if (ground) return (BlackPixel(mydisplay, scr));
     else return (WhitePixel(mydisplay,scr));

 } 
}

int get_ban_color(locy)
int locy;
{
int ban;
      if ( locy <  flag.y[1]) ban = 0;
      if ( locy >= flag.y[1] && locy < flag.y[2]) ban = 1;
      if ( locy >= flag.y[2]) ban = 2;
      return ban;
}
/*  ERASE_LINE  -  erases the current line + 1 (just draw a white rectangle)  */

erase_line ( locy )
int locy; 
{
int screen,ban;
XPoint rect[4];
char screen_color[12],ch,ch2;
unsigned long new_ground;

  rect[0].x = LEFT;
  rect[0].y = locy;
  rect[1].x = WIDTH;
  rect[1].y = locy;
  rect[2].x = WIDTH;
  rect[2].y = locy + 2*FIDEL_HEIGHT +3;
  rect[3].x = LEFT;
  rect[3].y = locy + 2*FIDEL_HEIGHT +3;  /* +3 for latin under hangs like g */

  if(last_un) rect[0].y = rect[1].y = locy + 4; 
 
  if(bandira) {
       rect[2].y -= FIDEL_HEIGHT;
       rect[3].y -= FIDEL_HEIGHT;
       ban = get_ban_color(locy);
       XSetForeground ( mydisplay, mygc, flag.fg[ban]);
       XSetBackground ( mydisplay, mygc, flag.fg[ban]);
       XFillPolygon ( mydisplay, mywindow, mygc, rect, 4, Convex, CoordModeOrigin ); 

       rect[0].y = rect[1].y = locy + FIDEL_HEIGHT;
       rect[2].y += FIDEL_HEIGHT +3;
       rect[3].y += FIDEL_HEIGHT +3;
       ban = get_ban_color(locy+FIDEL_HEIGHT);
       XSetForeground ( mydisplay, mygc, flag.fg[ban]);
       XSetBackground ( mydisplay, mygc, flag.fg[ban]);
       XFillPolygon ( mydisplay, mywindow, mygc, rect, 4, Convex, CoordModeOrigin ); 
  }
  else {
       XSetForeground ( mydisplay, mygc, background );
       XFillPolygon ( mydisplay, mywindow, mygc, rect, 4, Convex, CoordModeOrigin ); 
  }

  XSetForeground ( mydisplay, mygc, foreground );
}


/*  READ_FIDEL  -  reads from file gez.asc and constructs FIDEL bit array */

read_fidel() {

FILE *infile;
unsigned short row_word,trow;
int i,j,k,l,m,done,bbxoff,bbyoff,bitmap,start;
char str[50];

  if ((infile=fopen(eth_font_name,"r")) == NULL){
    fprintf(stderr,"\n*** I cant read from %s ***\n",eth_font_name);
    return(1);
  }

  printf("\n\nReading...\n");

  done=0;
  while ( !done ) {
    fscanf(infile,"%s",str);
    if ( !strcmp("STARTCHAR",str) )
      done=1;
  }

  for (k=0; k<FIDELS; k++) {
    for (i=0; i<FONT_WIDTH; i++)
      for (j=0; j<FONT_HEIGHT; j++)
        fidel[k].bits[j][i] = 0;
    done=0;
    while ( !done ) {
      fscanf(infile,"%s",str);
      if ( !strcmp("DWIDTH",str) ) 
        fscanf(infile,"%d",&fidel[k].dwx0);
      if ( !strcmp("BBX",str) )
        done=1;
    }
    fscanf(infile,"%d%d%d%d",&fidel[k].bbw,&fidel[k].bbh,&fidel[k].bbxoff,&bbyoff);
    fidel[k].my_yoff = (FONT_HEIGHT - fidel[k].bbh) - bbyoff;
    fscanf(infile,"%s",str);
    for (i=0; i<fidel[k].bbh; i++){
      fscanf(infile,"%x",&bitmap);
      row_word = 0;
      for (j=0; j<fidel[k].bbw; j++) {
        trow = bitmap;
        trow >>= (15-j);
        trow &= 0x01;
        if( trow==1 ) 
          fidel[k].bits[j][i] = 1;
        else 
          fidel[k].bits[j][i] = 0;
      }
    }
  }

  fclose(infile);


  printf("... Done reading\n\n");
  return(0);
}

disp_text()
{
int max_num_lines;
char ch;

num_lines=0;
max_num_lines = HEIGHT/FIDEL_HEIGHT - 2;
if(page) {erase_line(yloc);erase_line(yloc+FIDEL_HEIGHT);}
if (!page) refresh();
  while (fscanf(inptr,"%c",&NewCh) != EOF) {
  if (num_lines > max_num_lines) {page += 1; ungetc(NewCh,inptr);return(1);}
        if(eth) trans_eth();
        if(!eth) trans_latin (0);
        if(mail_check) {
           Mail[mail_check-1] = NewCh;
           if(mail_check++ == 7) get_mail(1);
        }
  }

  if (feof(inptr)) {
      XFlush(mydisplay);
      if(file) {
            printf("Read Again or Quit? [A]/Q :");
            if (toupper(getchr()) == 'Q') exit(0);
          else {
                 rewind(inptr); 
                 page = 0; 
                 xloc = LEFT;
                 yloc = TOP;
                 eth  = eth0;
                 num_lines = 0;
                 printf("Move Pointer over Viewing Window and Hit any Key to Begin/Continue\n");
                 return(1);
               }
      } else printf("\a\tHit Control-C to Exit \n");
  } 
}

void get_mail(next)
int next;
{
int screen;
static char start[80],ch;
char Done[17];

strcpy(Done,"<End of Message>");
  mail_check = 0;
  if(next)
     if(strcmp(Mail,"</sera>")) return;
        else {
/*            XFlush ( mydisplay );   I Think this isn't needed */
              XSetForeground ( mydisplay, mygc, background ); 
              XFillRectangle ( mydisplay, mywindow, mygc,mail_xloc,mail_yloc,(xloc-mail_xloc),FIDEL_HEIGHT);

              xloc=mail_xloc;
              yloc=mail_yloc;
              XSetForeground ( mydisplay, mygc, foreground ); 
XDrawImageString(mydisplay,mywindow,mygc,xloc,(yloc+FIDEL_HEIGHT),&Done,17);
              XFlush ( mydisplay ); /* Flush Needed To Display Square */
        }
  do fscanf(inptr,"%s\n",start); while(strcmp(start,"<sera>") && !feof(inptr));


  if (!next && feof(inptr)) {
     printf("\aNo <sera> marker found.\n");
     printf("Restart File Read as Latin, Ethiopic, or Quit? [L]/E/Q : ");
     if ((ch =toupper(getchr())) == 'Q') exit(0);
     if (ch != 'E') eth = 0;
        else eth = 1;
     rewind(inptr);
     mail = 0;
     return;
  }

  if (next) {
     printf("Read Next or Quit? [N]/Q :");
     if (toupper(getchr()) == 'Q') exit(0);
        else if( feof(inptr) )  {
             printf("\t\aNo More Eth-Mail Found, Reread or Quit [R]/Q :");
             if (toupper(getchr()) == 'Q') exit(0);
                else {
                rewind(inptr); 
                do fscanf(inptr,"%s\n",start); while(strcmp(start,"<sera>") && !feof(inptr));

               }
       }
  fscanf(inptr,"%c",&NewCh);
  if (NewCh  != '\n') ungetc(NewCh,inptr); 
  page = 0; 
  xloc = LEFT;
  yloc = TOP;
  eth = 1;
  num_lines = 0;
  hard_mode = 0;
  un_line=last_un=0;
  
  refresh(); 
  }
  eth = 1;
}


/*-------------------------------------------------------------------------//
//                                                                         //
//  This is the primary transilteration body for SERA to fidel.  The other //
//  routines "get_enum", "alternate", and "diqala" are also used for       //
//  transliteration and are called by the trans_eth driver. The purpose    //
//  for the driver is to determine an address of a SERA string for the     //
//  font-map array "fidel.h".  The code is written now for clarity and not //
//  speed, the consequence should be minimal until VERY large documents    //
//  are transliterated.                                                    //
//                                                                         //
//-------------------------------------------------------------------------*/

void trans_eth()
{ 
static int i,ban,keep,qelem;
keep=qelem=0;

  if(strcmp(font_name,eth_font_name) && !no_eth_font) {
    strcpy(font_name,eth_font_name);
    font = ethfont->fid;
    XSetFont(mydisplay,mygc,font);
  }


/* special case characters */
  switch ( NewCh ) {
           case  9:
               disp_fidel( new );
               xloc += SPACE_SIZE*TABSTOP;
               new = NIL; 
               sadis = 0;
               break;
           case 10: 				 /* line feed */
           case 13: 				 /* carriage return */
            disp_fidel( new );
            disp_fidel ( RETURN );
            num_lines++;
            sadis = 0;
            new = NIL; 
            break;      /* careful, read next char to see if it is a vowel or 2 */

           case '_'  :  disp_fidel( new );
                        un_line = !un_line;  
                        if(!un_line) {
                          last_un = 1;
                          un_line = 0;
                        }
                          sadis = 0;
                          new = NIL;
                        break; 

           case '<'  :  if(mail) {
                           mail_check = 1; 
                           mail_xloc = xloc;
                           mail_yloc=yloc;
                        }
                        goto Default;

           case '\\' :
                  disp_fidel( new );
                  if(hard_mode)                  {
                     fscanf(inptr,"%c",&NextCh); 
                     if(NextCh == '!')         { 
                        fscanf(inptr,"%c",&NextCh); 
                        hard_mode = eth = 0;
                        NewCh=NextCh;
                        if(NewCh == ' ') fscanf(inptr,"%c",&NewCh);
                     }
                     else {  ungetc(NextCh,inptr);
                             trans_latin(1);
                          } 
                     goto End_Escapes;        /* "break" doesn't work here */
                  }
                  fscanf(inptr,"%c",&NewCh); 
                  switch( NewCh ) {
                      case  '1'  : case '2' : case '3' : case '4' : case  '5' :
                      case  '6'  : case '7' : case '8' : case '9' :
                                     while(NewCh > 48 && NewCh < 58){
                                           Enum = 336 + NewCh - 49;
                                           i = 0;
                                           do {fscanf(inptr,"%c",&NewCh);  i++;}
                                             while (NewCh == '0');
                                           get_enum(Enum,--i); 
                                     }
                                     ungetc(NewCh,inptr);
                                     break;
                     
                      case '.'   :   trans_latin(1);
                                     break;
                      case ','   :   trans_latin(1);
                                     break;
                      case ';'   :   trans_latin(1);
                                     break;
                      case ':'   :   trans_latin(1);
                                     break;
                      case '\''  :   trans_latin(1);
                                     break;
                      case '`'   :   trans_latin(1);
                                     break;
                      case '|'   :   trans_latin(1);
                                     break;
                      case '\\'  :   trans_latin(1);
                                     break;
                      case '_'   :   disp_fidel(328);
                                     break;
                      case '<'   :   disp_fidel(334);
                                     break;
                      case '>'   :   disp_fidel(335);
                                     break;
                      case '*'   :   disp_fidel(333);
                                     break;
                      case '!'   :   hard_mode =! hard_mode;
                                     fscanf(inptr,"%c",&NewCh); 
                                     if(NewCh == ' ') fscanf(inptr,"%c",&NewCh);
                                     eth = 0;
                                     break;
                      case '~'   :   fscanf(inptr,"%c",&NewCh); 
                                     switch( NewCh ) {

                                         case   'e'  :  disp_fidel(356);
                                                        break;
                                         case   'E'  :  disp_fidel(357);
                                                        break;
                                         case   'a'  :  disp_fidel(358);
                                                        break;
                                         case   'A'  :  disp_fidel(359);
                                                        break;
                                         case   'C'  :  if(first_k) 
                                                           kerning = !kerning;  
                                                           sadis = 0;
                                                           break;
                     case   'b'  :  strcpy(fg_color,"blue"); qelem=1; break;
                     case   'c'  :  strcpy(fg_color,"cyan3"); qelem=1; break;
                     case   'd'  :  strcpy(fg_color,fg_init); qelem=1; break;
                     case   'g'  :  strcpy(fg_color,"green"); qelem=1; break;
                     case   'o'  :  strcpy(fg_color,"orange"); qelem=1; break;
                     case   'p'  :  strcpy(fg_color,"purple3"); qelem=1; break;
                     case   'r'  :  strcpy(fg_color,"red");     qelem=1; break;
                     case   'w'  :  strcpy(fg_color,"white");   qelem=1; break;
                     case   'y'  :  strcpy(fg_color,"yellow"); qelem=1; break;
                                         default     :           break;
                                     }

                                     fscanf(inptr,"%c",&NextCh); 
                                     if (NextCh  != ' ') ungetc(NextCh,inptr);

                     if(qelem) {
                       qelem = 0; 
                        if (bandira) {
                           ban = get_ban_color(yloc);
                           if (!strcmp(fg_color,&flag.color[ban]))
                                 strcpy(screen_color,fg_init);
                              else
                                 strcpy(screen_color,fg_color);
                        }
                         else if (!strcmp(fg_color,bg_color))
                                 strcpy(screen_color,fg_init);
                              else
                                 strcpy(screen_color,fg_color);
                        foreground = get_color_pixel(mydisplay,screen_color,1);
                        XSetForeground (mydisplay, mygc, foreground);
                     }

                                     break;


                      default:
                        if (NewCh  == ' ') fscanf(inptr,"%c",&NewCh);
                        eth = 0;

                      } /*        end \ switches         */
End_Escapes:
                      new = NIL;
                      sadis = 0;
                      break;

       case 'e'  :  form = 0; goto vowels;
       case 'u'  :  form = 1; goto vowels;
       case 'i'  :  form = 2; goto vowels;
       case 'a'  :  form = 3; goto vowels;
       case 'E'  :  form = 4; goto vowels;
       case 'o'  :  form = 6; goto vowels;
       case 'W'  :  form = 7; sadis = 1; /*sadis=1 need for lone "Wx" forms*/
           vowels:  
                    LastCh = NewCh;
                    if (sadis) {                         /* edit last */
                       new +=  form - 5;
                       if (NewCh == 'W') diqala();
                       vowel=sadis=0;
                       break;
                    }
       case 'U' : 
       case 'A' : 
       case 'I' :  
       case 'O' :   
                    vowel=1;
                    goto Default;

       case '2'  :    last = new;
                      switch(LastCh) {
                        case 'k' :  last -=4;   /* correct for k -> k2 */
                        case 'h' :
                        case 's' :
                        case 'S' :
                                    new = last - (last-128)/8 + 108 ;
                                    break;


                        case 'a' : if(vowel) last += 3;/* correct for a -> a2 */
                        case 'e' : case 'u' : case 'i' : case 'E' : 
                        case 'I' : case 'o' : case 'U' : case 'A' : case 'O' :
                                    if(vowel)
                                       new = last - (last-128)/8 + 108 ;
                                    sadis = 0;
                        default  :  break;
                      }
                      if(last != new) break;
                      goto Default;

       case '3'  :
                     if ( LastCh == 'e' ) {
                          new = 151;
                          break;
                     }
       case '\'' :
       case '|'  :
                   if(LastCh == '`')  { 
                      NextCh = NewCh;
                      NewCh = LastCh;
                      trans_latin(1);
                      NewCh = NextCh;
                   }
                    else
                        disp_fidel(new);

                   if((!sadis && form==NIL) || LastCh == '`') trans_latin(1);
                   new = NIL;
                   sadis = 0;
                   LastCh = NewCh;
                   break;  


/* end special case evaluation */

default:
 vowel = 0;
 form=NIL;
Default: 
 consonant = number = punct = 0;

  hohe = NewCh - 32;
  last = new;                           /* new and last are present */
   new = con_id[hohe];                  /*  and previous addresses  */

/*  identify new character */

  if (!new) {                                     /* Latin Text */
     disp_fidel( last );
     trans_latin(1);
     new = NIL;
     sadis = 0;
     LastCh = NewCh;
     break;  
  }
  if (!vowel)
      if (hohe < 16 || (hohe == 27 || hohe == 26))       punct = 1;
          else if (hohe >15 && hohe < 26)               number = 1;
               else                                  consonant = 1;

/*   character identifed, now operate   */


    if(LastCh == '`' ) alternate();


/*  All Printing Done Here */

        disp_fidel( last );
        LastCh = NewCh;
        if(vowel && !sadis) break;
        if (consonant)  sadis = 1; 
           else         sadis = 0;
        if(number)  trans_latin(1);
        if(ethspace && NewCh == ' ') new++;   


            /*  end printing */

    }   /*  end switch   */

}   /*  end trans_eth  */


/*-------------------------------------------------------------------------//
//                                                                         //
//  Hanndle all diqalawoc representations                                  //
//                                                                         //
//-------------------------------------------------------------------------*/

void diqala()
{
 int dform=NIL;
  last =  new - form + 5;
  fscanf(inptr,"%c",&NextCh);  /* read and discard */

  switch(NextCh) {

    case 'e'  : dform=0; goto diqalawoc;
    case 'u'  : dform=1; goto diqalawoc;
    case '\'' : dform=1; goto diqalawoc;
    case 'i'  : dform=2; goto diqalawoc;
    case 'E'  : dform=4; 
    diqalawoc :
                if(last == 262)                  /* h2Wx = hWx */
                  {new = 159 + dform; return;}
                if(last < 144 || last > 236)     /* mod 8 zone */
                  {disp_fidel(last); new = 231 + dform; return;}

    case 'a'  : if(dform == NIL) dform=3; 
                if(last > 151 && last < 236)     /* mod 12 zone */
                  {new += dform; return;}
                if(last == 262)                  /* h2Wa = hWa */
                  {new = 162; return;} 
                if(last > 263)               {   /* mode 7 and anything else */
                   disp_fidel(last); 
                   new = 231 + dform;
                }
                return;

    default   : /* character other than vowel follows "W" */

                ungetc(NextCh,inptr);

                if(last < 144 || last > 236) {   /* not mod 12 zone */
                   if(last > 263)      {         /* mode 7 and anything above */
                      disp_fidel(last); 
                      new = 234; 
                   }
                   if(last == 262)               /* h2W = hW */
                      new  = 162;
                   return;
                } 
                if(last > 151 && last < 236)     /* mod 12 zone */
                   new += 1;

  }
}


/*-------------------------------------------------------------------------//
//                                                                         //
//  Hanndle the hohEt that start with `                                    //
//                                                                         //
//-------------------------------------------------------------------------*/

void alternate()
{
  switch (NewCh) {
  case 'U' : new  = 251;  break;
  case 'A' : new  = 253;  break;
  case 'O' : new  = 256;  break;

  case 'k' : new -= 4;     /* correct for k -> k2  */
  case 'h' :
  case 's' :
  case 'S' :
             new = new - (new-128)/8 + 108 ;
             break;

  default  : if (!vowel) {  NextCh = NewCh; 
                            NewCh = '`';
                            last = new;
                            trans_latin(1);
                            new = last;
                            NewCh = NextCh;
                            
             }
              else  new = NIL;
  }
  if(NewCh != 'I') last = NIL; 

}

/*-------------------------------------------------------------------------//
//                                                                         //
//  Quaint Little Routine For Ethiopic Numbers                             //
//                                                                         //
//-------------------------------------------------------------------------*/

get_enum(Enum,num0s)
int Enum,num0s;
{
int i,odd,num,ande=336,asr=9,mato=354,asrxi=355;


  asr +=  Enum;
  odd  =  num0s%2;
  
  if(Enum == ande)
     if(!num0s) {disp_fidel( ande );return;}
       else if(odd) disp_fidel( asr );
  
  if(Enum != ande)
     if(!odd) disp_fidel( Enum );
       else disp_fidel( asr );

  num = num0s%4;
  if(num > 1)
           disp_fidel( mato );

  num = num0s/4;
  for(i = 0; i < num; i++)
           disp_fidel( asrxi );
}


/*-------------------------------------------------------------------------//
//                                                                         //
//  Print/Convert the Latin text.                                          //
//                                                                         //
//-------------------------------------------------------------------------*/

void trans_latin ( one )
int  one;
{
int i,ban,qelem=0,old_xloc;

if(!no_lat_font)
  if(strcmp(font_name,lat_font_name)) {
    strcpy(font_name,lat_font_name);
    font = latfont->fid;
    XSetFont(mydisplay,mygc,font);
  }

    if(one) goto Print; 
    switch ( NewCh ) {
           case  9:
               if(!nolat) xloc += SPACE_SIZE*TABSTOP;
               break;
           case 10: 				 /* line feed */
           case 13: 				 /* carriage return */
              if(nolat) break;
              disp_fidel ( RETURN );
              num_lines++;
              break;  

           case '_'  : if(nolat) break;
                        un_line = !un_line;  
                        if(!un_line) {
                          last_un = 1;
                          un_line = 0;
                        }
                        break; 

           case '<'  :  if(mail) { 
                           mail_check = 1; 
                           mail_xloc = xloc;
                           mail_yloc=yloc;
                        }
                        goto Print;

           case '\\' :
                        fscanf(inptr,"%c",&NewCh); 
                        if(hard_mode) {
                                       if(NewCh == '!')              { 
                                          fscanf(inptr,"%c",&NextCh); 
                                          hard_mode =! hard_mode; 
                                          eth = 1;
                                          if(NextCh != ' ')
                                             ungetc(NextCh,inptr);
                                       }
                                        else { ungetc(NewCh,inptr);
                                               NewCh = '\\';
                                               goto Print; 
                                             }
                        }else
                          switch ( NewCh ) {
                               case '1' : case '2' : case '3' : case '4' : 
                               case '5' : case '6' : case '7' : case '8' : 
                               case '9' :
                                     while(NewCh > 48 && NewCh < 58){
                                           Enum = 336 + NewCh - 49;
                                           i = 0;
                                           do {fscanf(inptr,"%c",&NewCh);  i++;}
                                           while(NewCh == '0');
                                           get_enum(Enum,--i); 
                                     }
                                     ungetc(NewCh,inptr);
                                     break;

                               case '\\' :  goto Print;
                               case '_'  :  disp_fidel(328); 
                                            break;
                               case '.'  :  disp_fidel(329); 
                                            break;
                               case ','  :  disp_fidel(330); 
                                            break;
                               case ';'  :  disp_fidel(331); 
                                            break;
                               case ':'  :  disp_fidel(332); 
                                            break;
                               case '*'  :  disp_fidel(333); 
                                            break;
                               case '<'  :  disp_fidel(334); 
                                            break;
                               case '>'  :  disp_fidel(335); 
                                            break;
                               case '!'  :  hard_mode =! hard_mode;
                                            fscanf(inptr,"%c",&NextCh); 
                                            if (NextCh  != ' ') 
                                                ungetc(NextCh,inptr);
                                            eth = 1;
                                            break;
                               case '~'  :  fscanf(inptr,"%c",&NewCh); 
                                            switch( NewCh ) {

                                            case   'e'  :  disp_fidel(356);
                                                           break;
                                            case   'E'  :  disp_fidel(357);
                                                           break;
                                            case   'a'  :  disp_fidel(358);
                                                           break;
                                            case   'A'  :  disp_fidel(359);
                                                           break;
                                            case   'C'  :  if(first_k) 
                                                           kerning = !kerning;  
                                                           sadis = 0;
                                                           new = NIL;
                                                           break;
                     case   'b'  :  strcpy(fg_color,"blue"); qelem=1; break;
                     case   'c'  :  strcpy(fg_color,"cyan3"); qelem=1; break;
                     case   'd'  :  strcpy(fg_color,fg_init); qelem=1; break;
                     case   'g'  :  strcpy(fg_color,"green"); qelem=1; break;
                     case   'o'  :  strcpy(fg_color,"orange"); qelem=1; break;
                     case   'p'  :  strcpy(fg_color,"purple3"); qelem=1; break;
                     case   'r'  :  strcpy(fg_color,"red");     qelem=1; break;
                     case   'w'  :  strcpy(fg_color,"white");   qelem=1; break;
                     case   'y'  :  strcpy(fg_color,"yellow"); qelem=1; break;
                                         default     :           break;
                                     }

                                     fscanf(inptr,"%c",&NextCh); 
                                     if (NextCh != ' ') ungetc(NextCh,inptr);

                     if(qelem) {
                       qelem = 0; 
                        if (bandira) {
                           ban = get_ban_color(yloc);
                           if (!strcmp(fg_color,&flag.color[ban]))
                                 strcpy(screen_color,fg_init);
                              else
                                 strcpy(screen_color,fg_color);
                        }
                         else if (!strcmp(fg_color,bg_color))
                                 strcpy(screen_color,fg_init);
                              else
                                 strcpy(screen_color,fg_color);
                        foreground = get_color_pixel(mydisplay,screen_color,1);
                        XSetForeground (mydisplay, mygc, foreground);
                     }

                                      break; 

                               default   :  eth = 1;
                                            if (NewCh  != ' ')
                                                ungetc(NewCh,inptr);
                          }    /*  end switch \        */
                          break;
Print :     default:

  if((!nolat || one) && !no_lat_font){
        if (bandira) {
            ban = get_ban_color(yloc);
            XSetBackground ( mydisplay, mygc, flag.fg[ban]);
        }
XDrawImageString(mydisplay,mywindow,mygc,xloc,(yloc+FIDEL_HEIGHT),&NewCh,1);

    old_xloc  = xloc;
    if(kerning)                xloc +=  XTextWidth(latfont,&NewCh,1);
    else if(NewCh == ' ')      xloc += SPACE_SIZE;
           else                xloc += FIDEL_WIDTH;
                                                        
 if (un_line)
    XDrawLine (mydisplay,mywindow,mygc,old_xloc,yloc+FIDEL_HEIGHT+2,xloc,yloc+FIDEL_HEIGHT+2);

    if ( xloc >= RIGHT ) {
       xloc = LEFT;
       yloc += FIDEL_HEIGHT;
       num_lines++;
       if ( yloc >= BOTTOM )
        yloc = TOP;
    }
  }
 } /* end switch */

 if(one) {eth = 1; sadis = 0; new = NIL;}
}



input_handler(argc, argv)
int argc;
char **argv;
{
char choice;
int latin=0;
/* $1 : -l, -e, -f, -m, -c, -fne, -fnl, -fg, -bg, -x, -y, filename :: $2 : NULL, filename */


  strcpy(fg_color,"black");
  strcpy(bg_color,"white");
  strcpy(eth_font_name,ETHFONT);

/* LATINFONT2 "-adobe-courier-medium-r-normal--14-100-100-100-m-90-iso8859-1"
**
**  If the font given below is unavailabe, try the above edit in the eview.h file.
**  The two fonts are for the most part identical.
*/
  strcpy(lat_font_name,LATINFONT1);
  HEIGHT = 48*FIDEL_HEIGHT;        /* 48 Lines   */
  WIDTH  = 60*FIDEL_WIDTH;         /* 60 Columns (ETHIOPIC) */

  i =  0;
  while (++i < argc) {
    if (argv[i][0] == '-') {
 
       switch(toupper(argv[i][1])) {

         case 'E' :   eth = 1;  break;
         case 'L' : latin = 1;  break;
         case 'C' : first_k = 0;  break;
         case 'M' :  mail = 1;  break;
         case 'B' :  strcpy(bg_color,argv[++i]);  break;
         case 'F' :  switch(toupper(argv[i][2]))  {
                        case 'G' :
                            strcpy(fg_color,argv[++i]);
                            break;
                        case 'N' : switch(toupper(argv[i][3]))  {
                            case 'E' :
                            strcpy(eth_font_name,argv[++i]);
                            break;
                            case 'L' :
                            strcpy(lat_font_name,argv[++i]);
                            break;
                        }   
                            break; 
                        default :
                            nolat = 1;
                        }
                     break;
                      
         case 'S' :   ethspace = 1;  break;
         case 'X' :   WIDTH  = FIDEL_WIDTH*atoi(argv[++i]);  break;
         case 'Y' :   HEIGHT = FIDEL_HEIGHT*atoi(argv[++i]);  break;
       }
    } else file = i;
  }

  if (file > 0) file_handler(argv[file]);
     else  inptr = stdin;

  if (eth+latin+mail+nolat == 0 && file > 0)  {
     printf("Select a File Treatment Type  :\a\n\n ");
     printf("[L]atin, Interpret Initial Text as Latin \n ");
     printf("[E]thiopic, Interpret Initial Text as Ethiopic \n ");
     printf("[M]ail, Read File as a Unix Mail File \n ");
     printf("[F]ilter, Assume File Starts as Latin Text and Filters\n\n ");
     printf("Your Choice Is :  ");
     choice = getchr();

     switch(toupper(choice)) {

       case 'L' :   eth = 0;  break;
       case 'E' :   eth = 1;  break;
       case 'M' :  mail = 1;  break;
       case 'F' : nolat = 1;  break;
    }
  }
  if(eth) strcpy(font_name,eth_font_name);
    else  strcpy(font_name,lat_font_name);
  eth0 = eth;
  kerning = first_k;
  strcpy(fg_init,fg_color);
  if(!strcmp(bg_color,"bandira")) bandira = 1;
  if (mail) get_mail(0); 
  return;
}
