Back to index

radiance  4R0+20100331
mtext.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: mtext.c,v 1.6 2003/11/15 02:13:37 schorsch Exp $";
00003 #endif
00004 /*
00005  *  Program to convert ascii file to metafile
00006  *
00007  *    6/4/85
00008  *
00009  *  cc mtext.c mfio.o syscalls.o misc.o
00010  */
00011 
00012 #include <string.h>
00013 
00014 #include  "meta.h"
00015 #include  "plot.h"
00016 
00017 #define  MAXLINE  1024
00018 
00019 #define  CWIDTH  250
00020 
00021 #define  CTHICK  0
00022 
00023 #define  CDIR  0
00024 
00025 #define  CCOLOR  0
00026 
00027 #define  BYASPECT(w)  ((w)*3/2)
00028 
00029 static int  cwidth = CWIDTH,
00030            cheight = BYASPECT(CWIDTH),
00031            cthick = CTHICK,
00032            cdir = CDIR,
00033            ccolor = CCOLOR;
00034 
00035 char  *progname;
00036 
00037 static void execute(FILE  *fp);
00038 static void sectout(char  **sect, int  nlines, int  maxlen);
00039 static void plotstr(int  lino, char  *s);
00040 
00041 
00042 
00043 int
00044 main(
00045        int  argc,
00046        char  **argv
00047 )
00048 {
00049     FILE  *fp;
00050     
00051     progname = *argv++;
00052     argc--;
00053 
00054     while (argc && **argv == '-')  {
00055         switch (*(*argv+1))  {
00056             case 'w':
00057             case 'W':
00058                 cwidth = atoi(*argv+2);
00059               cheight = BYASPECT(cwidth);
00060               if (cheight < 0 || cheight > XYSIZE)
00061                   error(USER, "illegal character width");
00062                 break;
00063            case 't':
00064            case 'T':
00065               cthick = atoi(*argv+2);
00066               if (cthick < 0 || cthick > 3)
00067                   error(USER, "thickness values between 0 and 3 only");
00068               break;
00069            case 'r':
00070            case 'R':
00071                cdir = 0;
00072                break;
00073            case 'u':
00074            case 'U':
00075                cdir = 1;
00076                break;
00077            case 'l':
00078            case 'L':
00079                cdir = 2;
00080                break;
00081            case 'd':
00082            case 'D':
00083                cdir = 3;
00084                break;
00085             case 'c':
00086             case 'C':
00087                 ccolor = atoi(*argv+2);
00088                 if (ccolor < 0 || ccolor > 3)
00089                     error(USER, "color values between 0 and 3 only");
00090                 break;
00091             default:
00092                 sprintf(errmsg, "unknown option '%s'", *argv);
00093               error(WARNING, errmsg);
00094               break;
00095        }
00096         argv++;
00097         argc--;
00098     }
00099 
00100     if (argc)
00101         while (argc)  {
00102            fp = efopen(*argv, "r");
00103            execute(fp);
00104            fclose(fp);
00105            argv++;
00106            argc--;
00107        }
00108     else
00109         execute(stdin);
00110 
00111     pglob(PEOF, 0200, NULL);
00112 
00113 return(0);
00114 }
00115 
00116 
00117 void
00118 execute(             /* execute a file */
00119        FILE  *fp
00120 )
00121 {
00122     static char  linbuf[MAXLINE];
00123     int  nlines;
00124     char  **section;
00125     int  maxlen;
00126     int  done;
00127     int  j, k;
00128     
00129     nlines = XYSIZE/cheight;
00130     done = FALSE;
00131     
00132     if ((section = (char **)calloc(nlines, sizeof(char *))) == NULL)
00133         error(SYSTEM, "out of memory in execute");
00134     
00135     while (!done) {
00136         maxlen = 0;
00137         for (j = 0; j < nlines; j++) {
00138             if ((done = fgets(linbuf, MAXLINE, fp) == NULL))
00139                 break;
00140             k = strlen(linbuf);
00141             if (linbuf[k-1] == '\n')
00142                 linbuf[--k] = '\0';              /* get rid of newline */
00143             if (k > maxlen)
00144                 maxlen = k;
00145             if ((section[j] = malloc(k+1)) == NULL)
00146                 error(SYSTEM, "out of memory in execute");
00147             strcpy(section[j], linbuf);
00148         }
00149        if (maxlen > 0)
00150            sectout(section, j, maxlen);
00151         for (k = 0; k < j; k++) {
00152             free(section[k]);
00153             section[k] = NULL;
00154         }
00155     }
00156 
00157     free((char *)section);
00158     
00159 }
00160 
00161 
00162 void
00163 sectout(             /* write out a section */
00164        char  **sect,
00165        int  nlines,
00166        int  maxlen
00167 )
00168 {
00169     int  linwidt;
00170     char  *slin;
00171     int  i, j;
00172     
00173     linwidt = XYSIZE/cwidth;
00174     
00175     if ((slin = malloc(linwidt + 1)) == NULL)
00176         error(SYSTEM, "out of memory in sectout");
00177         
00178     for (i = 0; i < maxlen; i += linwidt) {
00179     
00180         if (i > 0)
00181             pglob(PEOP, cdir, NULL);
00182 
00183         for (j = 0; j < nlines; j++)
00184             if (i < strlen(sect[j])) {
00185                 strncpy(slin, sect[j] + i, linwidt);
00186                 slin[linwidt] = '\0';
00187                 plotstr(j, slin);
00188            }
00189 
00190     }
00191 
00192     pglob(PEOP, 0200, NULL);
00193     free(slin);
00194     
00195 }
00196 
00197 
00198 void
00199 plotstr(             /* plot string on line lino */
00200        int  lino,
00201        char  *s
00202 )
00203 {
00204     int  a0;
00205     register int  bottom, right;
00206     
00207     a0 = (cdir<<4) | (cthick<<2) | ccolor;
00208     bottom = XYSIZE-(lino+1)*cheight;
00209     right = strlen(s)*cwidth;
00210     
00211     switch (cdir) {
00212         case 0:             /* right */
00213             pprim(PVSTR, a0, 0, bottom, right, bottom+cheight-1, s);
00214             break;
00215         case 1:             /* up */
00216             pprim(PVSTR, a0, XYSIZE-bottom-cheight+1, 0,
00217                      XYSIZE-bottom, right, s);
00218             break;
00219         case 2:             /* left */
00220             pprim(PVSTR, a0, XYSIZE-right, XYSIZE-bottom-cheight+1,
00221                      XYSIZE-1, XYSIZE-bottom, s);
00222             break;
00223         case 3:             /* down */
00224             pprim(PVSTR, a0, bottom, XYSIZE-right,
00225                      bottom+cheight-1, XYSIZE-1, s);
00226             break;
00227     }
00228 
00229 }