home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / drawPS.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  19KB  |  687 lines

  1. /*
  2.  *                      D R A W P S . C
  3.  *
  4.  *  Changed to take magnification into account, 27 August 1990.
  5.  *
  6.  * (minor mods by don on 5 Jan 90 to accommodate highres PostScript)
  7.  *
  8.  * $Revision: 1.1 $
  9.  *
  10.  * $Log:    drawPS.c,v $
  11.  * Revision 1.1  90/03/10  20:32:48  grunwald
  12.  * Initial revision
  13.  *
  14.  *
  15.  * 89/04/24: decouty@irisa.fr, priol@irisa.fr
  16.  *              added three shading levels for fig,
  17.  *              removed '\n' in cmdout() calls
  18.  *
  19.  * Revision 1.4  87/05/07  15:06:24  dorab
  20.  * relinked back hh to h and vv to v undoing a previous change.
  21.  * this was causing subtle quantization problems. the main change
  22.  * is in the definition of hconvPS and vconvPS.
  23.  *
  24.  * changed the handling of the UC seal. the PS file should now be
  25.  * sent via the -i option.
  26.  *
  27.  * Revision 1.3  86/04/29  23:20:55  dorab
  28.  * first general release
  29.  *
  30.  * Revision 1.3  86/04/29  22:59:21  dorab
  31.  * first general release
  32.  *
  33.  * Revision 1.2  86/04/29  13:23:40  dorab
  34.  * Added distinctive RCS header
  35.  *
  36.  */
  37. #ifndef lint
  38. char RCSid[] =
  39.   "@(#)$Header: /usr/local/src/TeX/Dvips-5.0.2/RCS/drawPS.c,v 1.1 90/03/10 20:32:48 grunwald Exp $ (UCLA)";
  40. #endif
  41.  
  42. /*
  43.  the driver for handling the \special commands put out by
  44.  the tpic program as modified by Tim Morgan <morgan@uci.edu>
  45.  the co-ordinate system is with the origin at the top left
  46.  and the x-axis is to the right, and the y-axis is to the bottom.
  47.  when these routines are called, the origin is set at the last dvi position,
  48.  which has to be gotten from the dvi-driver (in this case, dvips) and will
  49.  have to be converted to device co-ordinates (in this case, by [hv]convPS).
  50.  all dimensions are given in milli-inches and must be converted to what
  51.  dvips has set up (i.e. there are convRESOLUTION units per inch).
  52.  
  53.  it handles the following \special commands
  54.     pn n                        set pen size to n
  55.     pa x y                      add path segment to (x,y)
  56.     fp                          flush path
  57.     ip                          flush invisible path, i.e. do shading only
  58.     da l                        flush dashed - each dash is l (inches)
  59.     dt l                        flush dotted - one dot per l (inches)
  60.     sp [l]                      flush spline - optional l is dot/dash length
  61.     ar x y xr yr sa ea          arc centered at (x,y) with x-radius xr
  62.                                 and y-radius yr, start angle sa (in rads),
  63.                                 end angle ea (in rads)
  64.     ia x y xr yr sa ea          invisible arc with same parameters as above
  65.     sh [s]                      shade last path (box, circle, ellipse)
  66.                 s = gray level (0 = white, 1 = black)
  67.     wh                          whiten last path (box, circle, ellipse)
  68.     bk                          blacken last path (box,circle, ellipse)
  69.     tx                          texture command - Added by T. Priol
  70.                 (priol@irisa.fr), enhanced by M. Jourdan:
  71.                 textures are translated into uniform gray levels
  72.  
  73.   this code is in the public domain
  74.  
  75.   written by dorab patel <dorab@cs.ucla.edu>
  76.   december 1985
  77.   released feb 1986
  78.   changes for dvips july 1987
  79.  
  80.   */
  81.  
  82. #ifdef TPIC                     /* rest of the file !! */
  83.  
  84. #include "dvips.h"
  85. #include <math.h>        /* used in function "arc" */
  86.  
  87. #ifdef DEBUG
  88. extern integer debug_flag;
  89. #endif  /* DEBUG */
  90.  
  91. /*
  92.  * external functions used here
  93.  */
  94. extern void cmdout();
  95. extern void floatout();
  96. extern void numout();
  97. extern void error();
  98.  
  99. /*
  100.  * external variables used here
  101.  */
  102. extern integer hh,vv;           /* the current x,y position in pixel units */
  103. extern int actualdpi ;
  104. extern int vactualdpi ;
  105. extern integer mag ;
  106.  
  107. #define convRESOLUTION DPI
  108. #define convVRESOLUTION VDPI
  109. #define TRUE 1
  110. #define FALSE 0
  111. #define tpicRESOLUTION 1000     /* number of tpic units per inch */
  112.  
  113. /* convert from tpic units to PS units */
  114. #define PixRound(a,b) zPixRound((integer)(a),(integer)(b))
  115. #define convPS(x) PixRound((x),convRESOLUTION)
  116. #define convVPS(x) PixRound((x),convVRESOLUTION)
  117.  
  118. /* convert from tpic locn to abs horiz PS locn */
  119. #define hconvPS(x) (integer)(hh + convPS(x))
  120. /* convert from tpic locn to abs vert PS locn */
  121. #define vconvPS(x) (integer)(vv + convVPS(x))
  122. /* convert to degrees */
  123. #define convDeg(x) (360*(x)/(2*3.14159265358))
  124.  
  125. /* if PostScript had splines, i wouldnt need to store the path */
  126. #define MAXPATHS 600            /* maximum number of path segments */
  127. #define NONE 0                  /* for shading */
  128. #define BLACK           1       /* for shading */
  129. #define GRAY        2       /* MJ; for shading */
  130. #define WHITE           3       /* for shading */
  131.  
  132. /* the following defines are used to make the PostScript shorter;
  133.   the corresponding defines are in the PostScript prolog special.lpro */
  134. #define MOVETO "a"
  135. #define LINETO "li"
  136. #define RCURVETO "rc"
  137. #define RLINETO "rl"
  138. #define STROKE "st"
  139. #define FILL "fil"
  140. #define NEWPATH "np"
  141. #define CLOSEPATH "closepath"
  142. /*
  143.  * STROKE and FILL must restore the current point to that
  144.  * saved by NEWPATH
  145.  */
  146.  
  147. static integer xx[MAXPATHS], yy[MAXPATHS]; /* the current path in milli-inches */
  148. static integer pathLen = 0;             /* the current path length */
  149. static integer shading = NONE;  /* what to shade the last figure */
  150. static integer penSize = 2;             /* pen size in PS units */
  151.  
  152. /* forward declarations */
  153. /* static void doShading(); */
  154. static integer zPixRound();             /* (int)(x/y)PixRound((int)x,(int)y) */
  155. static double  shadetp = 0.5;
  156.              /* shading level, initialized as requested by tpic 2.0 -- MJ */
  157.  
  158. void
  159. setPenSize(cp)
  160.      char *cp;
  161. {
  162.   long ps;
  163.  
  164.   if (sscanf(cp, " %ld ", &ps) != 1)
  165.     {
  166.       error("Illegal .ps command format");
  167.       return;
  168.     }
  169.  
  170.   penSize = convPS(ps);
  171.   numout((integer)penSize);
  172.   cmdout("setlinewidth");
  173. }                               /* end setPenSize */
  174.  
  175. void
  176. addPath(cp)
  177.      char *cp;
  178. {
  179.   long x,y;
  180.  
  181.   if (++pathLen >= MAXPATHS) error("! Too many points");
  182.   if (sscanf(cp, " %ld %ld ", &x, &y) != 2)
  183.     error("! Malformed path expression");
  184.   xx[pathLen] = x;
  185.   yy[pathLen] = y;
  186. }                               /* end of addPath */
  187.  
  188. void
  189. arc(cp, invis)
  190.      char *cp;
  191.     int invis;
  192. {
  193.   long xc, yc, xrad, yrad;
  194.   float startAngle, endAngle;
  195.  
  196.   if (sscanf(cp, " %ld %ld %ld %ld %f %f ", &xc, &yc, &xrad, &yrad,
  197.              &startAngle, &endAngle) != 6)
  198.     {
  199.       error("Illegal arc specification");
  200.       return;
  201.     }
  202.  
  203. /* To have filled ellipses/circles also have borders, we duplicate the object
  204.    specification; first time we emit a FILL command, second time we emit a
  205.    STROKE command.
  206.    There certainly exists a better way to do that in PostScript, by
  207.    remembering the object specification (path) across the fill command.
  208.    However I (MJ) am completely unproficient at PostScript...
  209. */
  210.  
  211. /* we need the newpath since STROKE doesnt do a newpath */
  212.  
  213.   if (shading) {
  214.       /* first time for shading */
  215.       cmdout(NEWPATH);
  216.       numout((integer)hconvPS(xc));
  217.       numout((integer)vconvPS(yc));
  218.       numout((integer)convPS(xrad));
  219.       if (xrad != yrad && VDPI == DPI)
  220.     numout((integer)convPS(yrad));
  221.       floatout(convDeg(startAngle));
  222.       floatout(convDeg(endAngle));
  223.  
  224.       if (xrad == yrad && VDPI == DPI)        /* for arcs and circles */
  225.     cmdout("arc");
  226.       else
  227.     cmdout("ellipse");
  228.  
  229.       cmdout(FILL);
  230.       shading = NONE;
  231.       cmdout("0 setgray");    /* default of black */
  232.   }
  233.  
  234.   if (!invis) {
  235.       /* There is a problem with tpic 2.0's handling of dotted ellipses.
  236.      In most (all?) cases, after conversion to degrees and rounding
  237.      to integers, startAngle and endAngle become the same, so nothing,
  238.      not even a dot (as requested), is printed in the output (at least
  239.      on my Apple LaserWriter).
  240.      So I (MJ) singled out this case.
  241.        */
  242.       float degStAng = convDeg(startAngle),
  243.             degEnAng = convDeg(endAngle);
  244.  
  245.       cmdout(NEWPATH);        /* save current point */
  246.  
  247.       if (degStAng != degEnAng) { /* normal case */
  248.       numout((integer)hconvPS(xc));
  249.       numout((integer)vconvPS(yc));
  250.       numout((integer)convPS(xrad));
  251.       if (xrad != yrad)
  252.           numout((integer)convPS(yrad));
  253.       floatout(degStAng);
  254.       floatout(degEnAng);
  255.  
  256.       if (xrad == yrad)    /* for arcs and circles */
  257.           cmdout("arc");
  258.       else
  259.           cmdout("ellipse");
  260.       }
  261.