home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / acad / c / hrsh2acd / hrsh2acd.c < prev    next >
Text File  |  1988-07-16  |  10KB  |  379 lines

  1. /* -- Hershey Font code format converter ------------------------------------
  2.  |
  3.  |    Author: Wayne C. Crawford, University of California, Berkeley.
  4.  |
  5.  |    This program is hereby released into the public domain.
  6.  |    No restriction is placed on the use of code, commercial or otherwise.
  7.  |
  8.  |    Converts Hershey Font in James Hurt format (see README.OLD) to
  9.  |    AutoCAD *.SHP format.
  10.  |
  11.  |    Usage: hrsh2acd fontfile.fnt <--, (where fontfile is in J. Hurt format)
  12.  |    produces output with suffix .SHP
  13.  |
  14.  *--------------------------------------------------------------------------*/
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. #ifndef TRUE
  20. #define TRUE   1
  21. #define FALSE  0
  22. #endif
  23.  
  24. FILE    *OUTfile, *fopen();        /* some files */
  25. int    aboveheight = 21;
  26. int    belowheight = 6;
  27. int    commandcount = 0;
  28. int    charnum;
  29. int    linelength = 0;
  30. int    lastlength = 0;
  31. int    last_was_cart = FALSE;
  32. int    already_nine = FALSE;
  33. int    skipflag = 1;         /* 1 if next draw is 'pen up' */
  34. int    oldx, oldy;
  35. char    char_string[1000];
  36. char    *char_name[]= {"spc","kexc","kdblqt","kns","kds","kpc","kand","kapos",
  37.               "klp","krp","kas","kpls","kcma","ksub","kper","kdiv","n0",
  38.         "n1","n2","n3","n4","n5","n6","n7","n8","n9","kcol","ksmc",
  39.         "klt","keq","kgt","kqm","kea","uca","ucb","ucc","ucd","uce",
  40.         "ucf","ucg","uch","uci","ucj","uck","ucl","ucm","ucn","uco",
  41.         "ucp","ucq","ucr","ucs","uct","ucu","ucv","ucw","ucx","ucy",
  42.         "ucz","klb","kbkslsh","krb","kcaret","kundrl","krvap","lca",
  43.         "lcb","lcc","lcd","lce","lcf","lcg","lch","lci","lcj","lck",
  44.         "lcl","lcm","lcn","lco","lcp","lcq","lcr","lcs","lct","lcu",
  45.         "lcv","lcw","lcx","lcy","lcz","klbr","kvbar","krbr","ktilde",
  46.         "kdeg","kpls-min","kdiam"};
  47.  
  48. /* ---- scanint: a function to scan an integer, using n characters of
  49.  *          the input file, ignoring newlines. (scanf won't work
  50.  *          because it also ignores blanks)
  51.  * ------------------------------------------------------------------- */
  52. int scanint(file,n)
  53. FILE *file;
  54. int n;
  55. {
  56. char buf[20];
  57. int i,c;
  58.  
  59.     for (i=0;i<n;i++)
  60.     {
  61.           while ((c = fgetc(file)) == '\n') ; /* discard spare newlines */
  62.         if (c == EOF) return(-1);
  63.         buf[i] = c;
  64.     }
  65.     buf[i] = 0;
  66.     return(atoi(buf));
  67. }
  68.  
  69. /****************************************************************************/
  70. /****************   The following routines translate from    ****************/
  71. /****************   skip/draw/move to AUTOCAD format:         ****************/
  72. /****************************************************************************/
  73.  
  74. static void skip()
  75. {
  76.     if (already_nine)
  77.     {
  78.         linelength += sprintf(char_string+linelength,"(0,0),");
  79.         commandcount+=2;
  80.         already_nine = FALSE;
  81.     }
  82.     linelength += sprintf(char_string+linelength,"2,");
  83.     commandcount++;
  84.     skipflag = TRUE;
  85.     last_was_cart = FALSE;
  86. }
  87.  
  88. static void unskip()
  89. {
  90.     if (already_nine)
  91.     {
  92.         linelength += sprintf(char_string+linelength,"(0,0),");
  93.         commandcount+=2;
  94.         already_nine = FALSE;
  95.     }
  96.     linelength += sprintf(char_string+linelength,"1,");
  97.     commandcount++;
  98.     skipflag = FALSE;
  99.     last_was_cart = FALSE;
  100. }
  101.  
  102. static int good_polar(x,y)
  103. int x,y;
  104. {
  105.     if (x<0) x*=-1;
  106.     if (y<0) y*=-1;
  107.     if ((x==0)&&(y==0)) return(-1);
  108.     if (x==0) return(1);
  109.     else if (y==0) return(2);
  110.     else if (x==y) return(3);
  111.     else if (x==2*y) return(4);
  112.     else if (y==2*x) return(5);
  113.     else return(0);
  114. }
  115.  
  116.  
  117. static void cart_coord(x,y)
  118. int x,y;
  119. {
  120.     char *ninespot;
  121.  
  122.     if (last_was_cart)
  123.     {
  124.         if (!already_nine)
  125.         {
  126.         ninespot = char_string+linelength-lastlength;
  127.         *ninespot = '9';
  128.         already_nine = TRUE;
  129.         }    
  130.         linelength += sprintf(char_string+linelength,"(%d,%d),",x,y);
  131.         commandcount+= 2;
  132.     }    
  133.     else
  134.     {        
  135.         lastlength = sprintf(char_string+linelength,"8,(%d,%d),",x,y);
  136.         linelength += lastlength;
  137.         commandcount+=3;
  138.         already_nine = FALSE;
  139.     }
  140.     last_was_cart = TRUE;
  141. }
  142.  
  143.  
  144. static void polar_coord(x,y,type)
  145. int x,y,type;
  146. {
  147.     if ( (x>15) || (y>15) || (x<-15) || (y<-15) )
  148.         cart_coord(x,y);
  149.     else
  150.     {         
  151.     if (already_nine)
  152.     {
  153.         linelength += sprintf(char_string+linelength,"(0,0),");
  154.         commandcount+=2;
  155.         already_nine = FALSE;
  156.     }
  157.     switch(type)
  158.     {
  159.         case 1: if (y > 0)
  160.                 linelength += sprintf(char_string+linelength,"0%X4,",y);
  161.             else linelength += sprintf(char_string+linelength,"0%XC,",-y);
  162.             break;
  163.         case 2: if (x > 0)
  164.                 linelength += sprintf(char_string+linelength,"0%X0,",x);
  165.             else linelength += sprintf(char_string+linelength,"0%X8,",-x);
  166.             break;
  167.         case 3: if (x > 0)
  168.             {
  169.             if (y > 0)
  170.                     linelength += sprintf(char_string+linelength,"0%X2,",x);
  171.                 else linelength += sprintf(char_string+linelength,"0%XE,",x);
  172.             }
  173.             else
  174.             {
  175.             if (y > 0)
  176.                     linelength += sprintf(char_string+linelength,"0%X6,",-x);
  177.                 else linelength += sprintf(char_string+linelength,"0%XA,",-x);
  178.             }
  179.             break;
  180.         case 4: if (x > 0)
  181.             {
  182.             if (y > 0)
  183.                     linelength += sprintf(char_string+linelength,"0%X1,",x);
  184.                 else linelength += sprintf(char_string+linelength,"0%XF,",x);
  185.             }
  186.             else
  187.             {
  188.             if (y > 0)
  189.                     linelength += sprintf(char_string+linelength,"0%X7,",-x);
  190.                 else linelength += sprintf(char_string+linelength,"0%X9,",-x);
  191.             }
  192.             break;
  193.         case 5: if (x > 0)
  194.             {
  195.             if (y > 0)
  196.                     linelength += sprintf(char_string+linelength,"0%X3,",y);
  197.                 else linelength += sprintf(char_string+linelength,"0%XD,",-y);
  198.             }
  199.             else
  200.             {
  201.             if (y > 0)
  202.                     linelength += sprintf(char_string+linelength,"0%X5,",y);
  203.                 else linelength += sprintf(char_string+linelength,"0%XB,",-y);
  204.             }
  205.             break;
  206.         default: printf("Error in polar_cord()\n");
  207.     }   
  208.     commandcount++;
  209.     last_was_cart = FALSE;
  210.     }        
  211. }
  212.  
  213.         
  214. static void out_pos(newx,newy)
  215. int newx,newy;
  216. {
  217.     int relx,rely,type;
  218.  
  219.     relx = newx-oldx;        
  220.     rely = newy-oldy;        
  221.     if ((type = good_polar(relx,rely)) > 0)
  222.     polar_coord(relx,rely,type);
  223.     else if (type == 0)
  224.     cart_coord(relx,rely);
  225.     else if (skipflag == TRUE)
  226.     {     
  227.         skipflag == FALSE;
  228.     commandcount--;        /* Get rid of old skip command */
  229.     linelength -= 2;
  230.     return;    
  231.     }    
  232.     if (skipflag == TRUE)    
  233.         unskip();
  234.     oldx = newx;    
  235.     oldy = newy;    
  236. }
  237.  
  238.  
  239. dump_fontinfo(name)    /* Put SHP file header */
  240. char *name;
  241. {
  242.     register int i;
  243.     int linelen;
  244.  
  245.     fprintf(OUTfile,"\*0,4,%s\n", name);
  246.     fprintf(OUTfile,"%d,%d,0,0\n",aboveheight,belowheight);
  247. }
  248.  
  249.  
  250. dump_charinfo()        /* For each character, ASCII code, byte count, name */
  251. {
  252.     register int i;
  253.     int linelen;
  254.  
  255.     fprintf(OUTfile,"\*%d,%d,%s\n",charnum,commandcount,char_name[charnum-32]);
  256.     i = -1;
  257.     linelen = 0;
  258. doagain:
  259.     if (linelen > 74)
  260.     {
  261.         linelen = 0;
  262.         fprintf(OUTfile,"\n");
  263.     }
  264.     do
  265.     {
  266.         putc(char_string[++i],OUTfile);
  267.         linelen++;
  268.     } while( (char_string[i] != ',') && (char_string[i+1] != '\0') );
  269.     if (char_string[i+1] != '\0') goto doagain;
  270.     fprintf(OUTfile,"\n");
  271. }
  272.  
  273.  
  274. /****************************************************************************/
  275. main(argc,argv)
  276. int argc;
  277. char **argv;
  278. {
  279. FILE    *INfile;
  280. char    line[2][256];
  281. int     x, y, startx, endx;
  282. int     ipnt, ich, nch, i;
  283. char    readfile[80], writefile[80];
  284. char    *rfname, *wfname;        
  285.  
  286.     charnum = 32;    
  287.     if (argc != 2) 
  288.     {
  289.         fprintf(stderr, "Usage: HRSH2ACD fontfile.fnt\n");
  290.     fprintf(stderr, "    Converts Hershey Font in J. Hurt format to\n");
  291.     fprintf(stderr, "    AutoCAD .SHP format.\n");    
  292.         exit(1);
  293.     }
  294.  
  295.     strcpy( readfile, strlwr(argv[1]) );
  296.     strcpy( writefile, readfile);
  297.     rfname = readfile;
  298.     while ( *(++rfname) != '\0' );    /* Check if input filename is OK */
  299.     --rfname; --rfname; --rfname;
  300.     if( strcmp( rfname, "fnt") != 0 )
  301.     {
  302.     printf("Input file suffix must be .fnt\n");
  303.     exit(2);
  304.     }    
  305.  
  306.     if ( (INfile = fopen(readfile,"r")) == NULL )    /* input file */
  307.     {
  308.         fprintf(stderr,"Can't open input font file: %s\n", readfile);
  309.         exit(1);
  310.     }
  311.     wfname = writefile;        /* make .SHP suffix for output file */
  312.     while(*wfname++ != '.');        
  313.     *wfname++ = 's';    
  314.     *wfname++ = 'h';    
  315.     *wfname = 'p';    
  316.     OUTfile = fopen(writefile,"w"); 
  317.     if( OUTfile == NULL )
  318.     {
  319.     fprintf(stderr,"Can't open output file: %s\n", writefile);
  320.     exit(1);
  321.     }
  322.  
  323.     linelength = 0;
  324.     dump_fontinfo( writefile );
  325.     fprintf(stderr, "Doing ASCII code....\n");
  326.  
  327.     while (TRUE) 
  328.     {
  329.       fprintf(stderr, "   %3d\r", charnum);   /* Show current ASCII code */
  330.           if ((ich = scanint(INfile,5)) < 1)     
  331.       {
  332.           fclose(OUTfile);    
  333.               exit(0);
  334.           }
  335.           nch = scanint(INfile,3);
  336.  
  337.           for (i=0; i<nch;i++) 
  338.       {
  339.               if ((i==32) ||(i==68) ||(i==104) ||(i==140)) fgetc(INfile); /* skip newlines */
  340.               line[0][i] = fgetc(INfile);
  341.               line[1][i] = fgetc(INfile);
  342.           }
  343.           fgetc(INfile);
  344.  
  345.           startx=(int)line[0][0] - (int)'R';
  346.           endx=(int)line[1][0] - (int)'R';
  347.  
  348.       oldx = startx;
  349.       oldy = -9;
  350.  
  351.             skip();
  352.       /*   .. loop per line of data */
  353.           for (ipnt=1;ipnt<nch;ipnt++) 
  354.       {
  355.                 if (line[0][ipnt] == ' ') 
  356.           {
  357.               /*  .. next data point is a move */
  358.                   skip();
  359.                 } 
  360.           else 
  361.           {
  362.               /*   .. draw (or move) to this data point */
  363.                x=(int)line[0][ipnt] -(int) 'R';
  364.                   y=(int)line[1][ipnt] -(int) 'R';
  365.           out_pos(x,-y);
  366.                 }
  367.           } /* for loop  .. end of this character */
  368.       skip();
  369.       out_pos(endx,-9);
  370.       sprintf(char_string+linelength-2,"0\0");
  371.       dump_charinfo();
  372.       charnum++; 
  373.       commandcount = 0; 
  374.       linelength = 0; 
  375.    } /* "infinite" loop */
  376.    fprintf(stderr, "\n");
  377.    exit();
  378. }
  379.