take stipple map out as default and made it an option
[unix-history] / usr / src / local / ditroff / ditroff.old.okeeffe / grn / main.c
index 49a888f..b26b398 100644 (file)
@@ -1,4 +1,4 @@
-/*     main.c  1.16    (Berkeley) 84/04/17
+/*     main.c  1.24    (Berkeley)      86/04/14
  *
  *     This file contains the main and file system dependent routines
  * for processing gremlin files into troff input.  The program watches
  *
  *     This file contains the main and file system dependent routines
  * for processing gremlin files into troff input.  The program watches
@@ -21,8 +21,8 @@
  *         the picture.  At most one command may reside on each line, and
  *         each command is followed by a parameter separated by white space.
  *         The commands are as follows, and may be abbreviated down to one
  *         the picture.  At most one command may reside on each line, and
  *         each command is followed by a parameter separated by white space.
  *         The commands are as follows, and may be abbreviated down to one
- *         character (with exception of "scale" down to "sc") and may be
- *         upper or lower case.
+ *         character (with exception of "scale" and "stipple" down to "sc"
+ *         and "st") and may be upper or lower case.
  *
  *                           default  -  make all settings in the current
  *                                       .GS/.GE the global defaults.
  *
  *                           default  -  make all settings in the current
  *                                       .GS/.GE the global defaults.
  *                                       by an integer point size).
  *     roman, italics, bold, special  -  set gremlin's fonts to any other
  *                                       troff font (one or two characters)
  *                                       by an integer point size).
  *     roman, italics, bold, special  -  set gremlin's fonts to any other
  *                                       troff font (one or two characters)
+ *                        stipple, l  -  use a stipple font for polygons.  Arg
+ *                                       is troff font name.  No Default.  Can
+ *                                       use only one stipple font per picture.
+ *                                       (see below for stipple font index)
  *                          scale, x  -  scale is IN ADDITION to the global
  *                                       scale factor from the default.
  *                        pointscale  -  turn on scaling point sizes to
  *                          scale, x  -  scale is IN ADDITION to the global
  *                                       scale factor from the default.
  *                        pointscale  -  turn on scaling point sizes to
  *                                       The operand for these two commands is
  *                                       a floating-point number in units of
  *                                       inches
  *                                       The operand for these two commands is
  *                                       a floating-point number in units of
  *                                       inches
+ *                     oldstipplemap  -  use the old-style stipple mapping.
+ *                                       THE FOLLOWING COMMANDS ARE IGNORED
+ *                                       UNLESS OLDSTIPPLEMAP IS SPECIFIED.
+ *     l1, l2, l3, l4, l5, l6, l7, l8  -  set association between stipples
+ *                                       (1 - 8) and the stipple font file
+ *                                       index.  Valid cifplot indices are
+ *                                       1 - 32 (although 24 is not defined)
+ *                                       and valid unigrafix indices are
+ *                                       1 - 64.  Nonetheless, any number
+ *                                       between 0 and 255 is accepted since
+ *                                       new stipple fonts may be added.
+ *                                       An integer operand is required.
  *
  *     Troff number registers used:  g1 through g9.  g1 is the width of the
  *     picture, and g2 is the height.  g3, and g4, save information, g8
  *
  *     Troff number registers used:  g1 through g9.  g1 is the width of the
  *     picture, and g2 is the height.  g3, and g4, save information, g8
@@ -70,21 +86,32 @@ extern ELT *DBInit(), *DBRead();
 extern POINT *PTInit(), *PTMakePoint();
 
 
 extern POINT *PTInit(), *PTMakePoint();
 
 
+#ifndef GREMLIB
 #define GREMLIB                "/usr/local/gremlib/"
 #define GREMLIB                "/usr/local/gremlib/"
+#endif
+
+#define SUN_SCALEFACTOR 0.70
+
 #ifndef DEVDIR
 #define DEVDIR         "/usr/lib/font"
 #endif
 #define DEFAULTDEV     "va"
 #ifndef DEVDIR
 #define DEVDIR         "/usr/lib/font"
 #endif
 #define DEFAULTDEV     "va"
+#define DEFSTIPPLE     "cf"
 
 #define MAXINLINE      100             /* input line length */
 #define DEFTHICK       3               /* default thicknes */
 #define DEFSTYLE       SOLID           /* default line style */
 
 
 #define MAXINLINE      100             /* input line length */
 #define DEFTHICK       3               /* default thicknes */
 #define DEFSTYLE       SOLID           /* default line style */
 
+#ifdef oldversion
 #define SCREENtoINCH   0.02            /* scaling factor, screen to inches */
 #define SCREENtoINCH   0.02            /* scaling factor, screen to inches */
+#endif
+
+double SCREENtoINCH;                   /* scaling factor, screen to inches */
+
 #define BIG    999999999999.0          /* unweildly large floating number */
 
 
 #define BIG    999999999999.0          /* unweildly large floating number */
 
 
-char   SccsId[] = "main.c      1.16    84/04/17";
+static char sccsid[] = "@(#) (Berkeley) %G%";
 
 char   *printer = DEFAULTDEV;  /* device to look up resolution of */
 char   *gremlib = GREMLIB;     /* place to find files after current dir. */
 
 char   *printer = DEFAULTDEV;  /* device to look up resolution of */
 char   *gremlib = GREMLIB;     /* place to find files after current dir. */
@@ -107,13 +134,17 @@ int       lastyline;              /* a line's vertical position is NOT the same */
 char * deffont[] = {  "R", "I", "B", "S"  };
 int    defsize[] = {  10, 16, 24, 36  };
 int    defthick[STYLES] = {  1, 1, 5, 1, 1, 3  };
 char * deffont[] = {  "R", "I", "B", "S"  };
 int    defsize[] = {  10, 16, 24, 36  };
 int    defthick[STYLES] = {  1, 1, 5, 1, 1, 3  };
+int    defstipple_index[NSTIPPLES] = { 1, 3, 12, 14, 16, 19, 21, 23 };
 int    style[STYLES] = {  DOTTED, DOTDASHED, SOLID, DASHED, SOLID, SOLID  };
 double scale = 1.0;            /* no scaling, default */
 int    defpoint = 0;           /* flag for pointsize scaling */
 int    style[STYLES] = {  DOTTED, DOTDASHED, SOLID, DASHED, SOLID, SOLID  };
 double scale = 1.0;            /* no scaling, default */
 int    defpoint = 0;           /* flag for pointsize scaling */
+char *  defstipple = (char *) 0;
 
 int    thick[STYLES];  /* thicknesses set by defaults, then by commands */
 char   *tfont[FONTS];  /* fonts originally set to deffont values, then */
 int    tsize[SIZES];   /*    optionally changed by commands inside grn */
 
 int    thick[STYLES];  /* thicknesses set by defaults, then by commands */
 char   *tfont[FONTS];  /* fonts originally set to deffont values, then */
 int    tsize[SIZES];   /*    optionally changed by commands inside grn */
+int    stipple_index[NSTIPPLES];       /* stipple font file indices */
+char *  stipple;
 
 double xscale;         /* scaling factor from individual pictures */
 double troffscale;     /* scaling factor at output time */ 
 
 double xscale;         /* scaling factor from individual pictures */
 double troffscale;     /* scaling factor at output time */ 
@@ -139,6 +170,8 @@ char        *c2 = inputline + 1;            /* hunt for lines that begin with */
 char   *c3 = inputline + 2;            /* ".GS" by looking individually */
 char   GScommand[MAXINLINE];           /* put user's ".GS" command line here */
 char   gremlinfile[MAXINLINE];         /* filename to use for a picture */
 char   *c3 = inputline + 2;            /* ".GS" by looking individually */
 char   GScommand[MAXINLINE];           /* put user's ".GS" command line here */
 char   gremlinfile[MAXINLINE];         /* filename to use for a picture */
+int    SUNFILE = FALSE;                /* TRUE if SUN gremlin file */
+int    oldstipmap = FALSE;             /* TRUE if old-style stipple mapping */
 
 char *doinput();
 
 
 char *doinput();
 
@@ -156,21 +189,28 @@ main(argc, argv)
 int argc;
 char **argv;
 {
 int argc;
 char **argv;
 {
-       register FILE *fp = stdin;
+       register FILE *fp;
        register int k;
        register char c;
        register gfil = 0;
        char *file[50];
 
        char *operand();
        register int k;
        register char c;
        register gfil = 0;
        char *file[50];
 
        char *operand();
+       char *getenv();
 
 
 
 
+       if (fp = (FILE *) getenv("PRINTER")) printer = (char *) fp;
+       if (fp = (FILE *) getenv("TYPESETTER")) printer = (char *) fp;
        while (--argc) {
            if (**++argv != '-')
                file[gfil++] = *argv;
            else
              switch (c = (*argv)[1]) {
 
        while (--argc) {
            if (**++argv != '-')
                file[gfil++] = *argv;
            else
              switch (c = (*argv)[1]) {
 
+               case 0:
+                       file[gfil++] = NULL;
+                       break;
+
                case 'P':
                case 'T':       /* final output typesetter name */
                        printer = operand(&argc, &argv);
                case 'P':
                case 'T':       /* final output typesetter name */
                        printer = operand(&argc, &argv);
@@ -323,14 +363,18 @@ initpic()
     for (i = 0; i < SIZES; i++) {      /* font size defaults */
        tsize[i] = defsize[i];
     }
     for (i = 0; i < SIZES; i++) {      /* font size defaults */
        tsize[i] = defsize[i];
     }
+    for (i = 0; i < NSTIPPLES; i++) {  /* stipple font file default indices */
+       stipple_index[i] = defstipple_index[i];
+    }
+    stipple = defstipple;
 
     gremlinfile[0] = 0;                /* filename is "null" */
     setdefault = 0;            /* this is not the default settings (yet) */
 
     toppoint = BIG;            /* set the picture bounds out */
 
     gremlinfile[0] = 0;                /* filename is "null" */
     setdefault = 0;            /* this is not the default settings (yet) */
 
     toppoint = BIG;            /* set the picture bounds out */
-    bottompoint = 0.0;         /* of range so they'll be set */
+    bottompoint = -BIG;                /* of range so they'll be set */
     leftpoint = BIG;           /* by "savebounds" on input */
     leftpoint = BIG;           /* by "savebounds" on input */
-    rightpoint = 0.0;
+    rightpoint = -BIG;
 
     pointscale = defpoint;     /* Flag for scaling point sizes default. */
     xscale = scale;            /* default scale of individual pictures */
 
     pointscale = defpoint;     /* Flag for scaling point sizes default. */
     xscale = scale;            /* default scale of individual pictures */
@@ -395,6 +439,13 @@ int baseline;
                    return;             /* if a request is made to make the */
                                        /* picture fit into a specific area, */
                                        /* set the scale to do that. */
                    return;             /* if a request is made to make the */
                                        /* picture fit into a specific area, */
                                        /* set the scale to do that. */
+
+               SCREENtoINCH = (SUNFILE) ? 0.014 : 0.02;
+
+               if (stipple == (char *) NULL)   /* if user forgot stipple */
+                   if (has_polygon(PICTURE))   /* and picture has a polygon */
+                       stipple = DEFSTIPPLE;   /* then set the default */
+
                if ((temp = bottompoint - toppoint) < 0.1) temp = 0.1;
                temp = (height != 0.0) ? height / (temp * SCREENtoINCH)  : BIG;
                if ((troffscale = rightpoint - leftpoint) < 0.1) troffscale=0.1;
                if ((temp = bottompoint - toppoint) < 0.1) temp = 0.1;
                temp = (height != 0.0) ? height / (temp * SCREENtoINCH)  : BIG;
                if ((troffscale = rightpoint - leftpoint) < 0.1) troffscale=0.1;
@@ -409,8 +460,10 @@ int baseline;
                if (pointscale) {
                    register int i;             /* do pointscaling here, when */
                                             /* scale is known, before output */
                if (pointscale) {
                    register int i;             /* do pointscaling here, when */
                                             /* scale is known, before output */
+
                    for (i = 0; i < SIZES; i++)
                        tsize[i] = (int) (troffscale * (double) tsize[i] + 0.5);
                    for (i = 0; i < SIZES; i++)
                        tsize[i] = (int) (troffscale * (double) tsize[i] + 0.5);
+
                }
                                                   /* change to device units */
                troffscale *= SCREENtoINCH * res;       /* from screen units */
                }
                                                   /* change to device units */
                troffscale *= SCREENtoINCH * res;       /* from screen units */
@@ -430,12 +483,16 @@ int baseline;
 ".br\n.nr g1 %du\n.nr g2 %du\n%s.nr g3 \\n(.f\n.nr g4 \\n(.s\n\\0\n.sp -1\n",
                        xright-xleft, ybottom-ytop, GScommand);
 
 ".br\n.nr g1 %du\n.nr g2 %du\n%s.nr g3 \\n(.f\n.nr g4 \\n(.s\n\\0\n.sp -1\n",
                        xright-xleft, ybottom-ytop, GScommand);
 
+               if (stipple) {          /* stipple requested for this picture */
+                   printf(".st %s\n", stipple);
+               }
+
                lastx = xleft;          /* note where we are, (upper left */
                lastyline = lasty = ytop;       /* corner of the picture) */
 
                e = PICTURE;
                lastx = xleft;          /* note where we are, (upper left */
                lastyline = lasty = ytop;       /* corner of the picture) */
 
                e = PICTURE;
-               while (!DBNullelt(e)) {
-                   HGPrintElt(e);      /* traverse picture;  print elements */
+               while (!DBNullelt(e)) { /* traverse picture;  print elements */
+                   HGPrintElt(e, baseline);
                    e = DBNextElt(e);
                }
                                /* decide where to end picture */
                    e = DBNextElt(e);
                }
                                /* decide where to end picture */
@@ -450,9 +507,13 @@ int baseline;
                                        /* restore everything to the way */
                                        /* it was before the .GS, then put */
                                        /* out the ".GE" line from user */
                                        /* restore everything to the way */
                                        /* it was before the .GS, then put */
                                        /* out the ".GE" line from user */
-               if (flyback)
-                   printf(".sp -1\n");
                printf("\\D't %du'\\D's %du'\n", DEFTHICK, DEFSTYLE);
                printf("\\D't %du'\\D's %du'\n", DEFTHICK, DEFSTYLE);
+               if (flyback) {          /* make sure we end up at top of */
+                   printf(".sp -1\n");         /* picture if "flying back" */
+               }
+               if (stipple) {          /* restore stipple to previous */
+                   printf(".st\n");
+               }
                printf(".br\n.ft \\n(g3\n.ps \\n(g4\n%s", inputline);
            } else {
                interpret(inputline);   /* take commands from the input file */
                printf(".br\n.ft \\n(g3\n.ps \\n(g4\n%s", inputline);
            } else {
                interpret(inputline);   /* take commands from the input file */
@@ -469,7 +530,7 @@ int baseline;
  |             point sizes are NOT saved.  The scaling is done each time a
  |             new picture is started.
  |
  |             point sizes are NOT saved.  The scaling is done each time a
  |             new picture is started.
  |
- | Side Efct:  defpoint, scale, deffont, defsize and defthick are modified.
+ | Side Efct:  scale, and def* are modified.
  *----------------------------------------------------------------------------*/
 
 savestate()
  *----------------------------------------------------------------------------*/
 
 savestate()
@@ -485,6 +546,10 @@ savestate()
     for (i = 0; i < SIZES; i++) {      /* font size defaults */
        defsize[i] = tsize[i];
     }
     for (i = 0; i < SIZES; i++) {      /* font size defaults */
        defsize[i] = tsize[i];
     }
+    for (i = 0; i < NSTIPPLES; i++) {  /* stipple font file default indices */
+       defstipple_index[i] = stipple_index[i];
+    }
+    defstipple = stipple;      /* if stipple has been set, it's remembered */
 
     scale *= xscale;           /* default scale of individual pictures */
     defpoint = pointscale;     /* flag for scaling pointsizes from x factors */
 
     scale *= xscale;           /* default scale of individual pictures */
     defpoint = pointscale;     /* flag for scaling pointsizes from x factors */
@@ -576,10 +641,33 @@ char *line;
        nofont: error("no fontname specified in line %d", linenum);
                break;
            }
        nofont: error("no fontname specified in line %d", linenum);
                break;
            }
+           if (str1[1] == 't') goto stipplecommand;    /* or stipple */
+
            tfont[3] = malloc(strlen(str2) + 1);
            strcpy(tfont[3], str2);
            break;
 
            tfont[3] = malloc(strlen(str2) + 1);
            strcpy(tfont[3], str2);
            break;
 
+       case 'l':       /* l */
+           if ((str1[1] < '1') || (str1[1] > '8'))
+               goto stipplecommand;
+
+           /* else set stipple index */
+           i = atoi(str2);
+           if (i >= 0 && i < 256)
+               stipple_index[str1[1] - '1'] = i;
+           else
+               error("bad stipple index value at line %d", linenum);
+           break;
+
+       stipplecommand: /* stipple */
+           stipple = malloc(strlen(str2) + 1);
+           strcpy(stipple, str2);
+           break;
+
+       case 'o':       /* oldstipplemap */
+           oldstipmap = TRUE;
+           break;
+
        case 't':       /* thick */
            thick[2] = atoi(str2);
            break;
        case 't':       /* thick */
            thick[2] = atoi(str2);
            break;
@@ -632,3 +720,20 @@ char *line;
            break;
     };
 }
            break;
     };
 }
+
+
+/*
+ * return TRUE if picture contains a polygon
+ * otherwise FALSE
+ */
+has_polygon(elist)
+register ELT *elist;
+{
+    while (!DBNullelt(elist)) {
+       if (elist->type == POLYGON)
+           return(1);
+       elist = DBNextElt(elist);
+    }
+
+    return(0);
+}