home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / stex2-18.zip / SeeTeX / Xtex / DviPageGS-2.c < prev    next >
C/C++ Source or Header  |  1992-06-25  |  8KB  |  289 lines

  1. /*
  2.  * Copyright 1989 Dirk Grunwald
  3.  * 
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without fee,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Dirk Grunwald or M.I.T.
  9.  * not be used in advertising or publicity pertaining to distribution of
  10.  * the software without specific, written prior permission.  Dirk
  11.  * Grunwald and M.I.T. makes no representations about the suitability of
  12.  * this software for any purpose.  It is provided "as is" without express
  13.  * or implied warranty.
  14.  * 
  15.  * DIRK GRUNWALD AND M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL M.I.T.  BE LIABLE FOR ANY SPECIAL, INDIRECT
  18.  * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  19.  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  20.  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  21.  * OR PERFORMANCE OF THIS SOFTWARE.
  22.  * 
  23.  * Author:
  24.  *     Dr. Dirk Grunwald
  25.  *     Dept. of Computer Science
  26.  *     Campus Box 430
  27.  *     Univ. of Colorado, Boulder
  28.  *     Boulder, CO 80309
  29.  * 
  30.  *     grunwald@colorado.edu
  31.  *     
  32.  */ 
  33.  
  34. #include <stdio.h>
  35. #include <sys/time.h>
  36. #include <X11/IntrinsicP.h>
  37. #include <X11/StringDefs.h>
  38. #include <stdio.h>
  39. #include <assert.h>
  40. #include <varargs.h>
  41. #include <math.h>
  42. #include <ctype.h>
  43. #include <sys/param.h>
  44. #include <unistd.h>        /* for access() */
  45.  
  46. #include "xtex.h"
  47. #include "dvi-simple.h"
  48. #include "DviPageP.h"
  49.  
  50. #include "DviPageGS-2.h"
  51.  
  52. static int rx, ry;
  53. static int width, height;
  54. static int psfigError;
  55. static double tx, ty;
  56. static double psfigWidth, psfigHeight;
  57. static char prevDir[MAXPATHLEN] = "", curDir[MAXPATHLEN];
  58.  
  59. #ifdef __hpux
  60. #define getwd(d) getcwd(d, MAXPATHLEN)
  61. #endif
  62.  
  63. void
  64. psfigBeginGHOST (w, cp, fudge_height)
  65. DviPageWidget w;
  66. char *cp;
  67. int fudge_height;
  68.     {
  69. int bbllx, bblly;
  70. int bburx, bbury;
  71. int rawWidth, rawHeight;
  72. int rawPsfigWidth, rawPsfigHeight;
  73. double scaleXY, psfigScaleX, psfigScaleY, magScaleX, magScaleY;
  74. char ghostSetupStr[1024], ghostInputStr[1024];
  75.  
  76.     if (sscanf(cp, " %d %d %d %d %d %d ", &rawWidth, &rawHeight, 
  77.             &bbllx, &bblly, &bburx, &bbury) != 6)
  78.         {
  79.         DialogMessage ("Error reading info at ps::begin\n", False);
  80.         psfigError = True;
  81.         return;
  82.         }
  83.     
  84.     if (w->dviPage.ghostPixmap == None)
  85.         {
  86.         psfigError = True;
  87.         return;
  88.         }
  89.  
  90.     /* Set up pixmap for ghostscript to draw on */
  91.     /* Make sure pixmap is appropriate */
  92.     if ((w->dviPage.ghostPixmap != None) && 
  93.             ((w->dviPage.ghostPixWidth != w->dviPage.pixelsWide) || 
  94.             (w->dviPage.ghostPixHeight != w->dviPage.pixelsHigh)))
  95.         {
  96.         /* Pixmap exists, but something changed. 
  97.          * Kill the interpreter if the queue is empty.
  98.          */
  99. #ifdef DEBUG
  100.         fprintf (stderr, "Size changed : %d %d --> %d %d\n",
  101.             w->dviPage.ghostPixWidth, w->dviPage.ghostPixHeight,
  102.             w->dviPage.pixelsWide, w->dviPage.pixelsHigh);
  103.         fflush (stderr);
  104. #endif
  105.         gsintResize (w);
  106.         }
  107.  
  108.     if (w->dviPage.ghostPixmap == None)
  109.         {
  110.         psfigError = True;
  111.         return;
  112.         }
  113.  
  114.     /* Check if directory chnaged */
  115.     getwd (curDir);
  116.     if (strcmp (prevDir, curDir) != 0)
  117.         {
  118.         /* Yes; kill interpreter. */
  119.         StopInterpreter (w);
  120.         strcpy (prevDir, curDir);
  121.         }
  122.  
  123.     rx = fastFromSpHoriz(w, w -> dviPage.dviStackPointer -> h);
  124.     ry = fastFromSpVert(w, w -> dviPage.dviStackPointer -> v);
  125.     width = fastFromSpHoriz (w, rawWidth);
  126.     height = fastFromSpVert(w, rawHeight);
  127.  
  128.     if (w->dviPage.ghostPID == -1)
  129.         {
  130.         StartInterpreter (w);
  131.         if (w->dviPage.ghostPID == -1)
  132.             {
  133.             psfigError = True;
  134.             /* Couldn't start gs; just draw an outline */
  135.             XDrawRectangle (XtDisplay (w), XtWindow (w), w->dviPage.paintGC, 
  136.                 rx, ry, width, height);
  137.             return;
  138.             }
  139.         }
  140.  
  141. /* 
  142.  * \epsfbox{} apparently causes a situation where the image is 
  143.  * expected to grow upward, rather than down from the position on the 
  144.  * page, so we fake it here
  145.  */
  146.         if( fudge_height ) ry -= height;
  147.  
  148.     psfigError = False;
  149.     DialogMessage ("startTexFig\n", False);
  150.  
  151.     rawPsfigWidth = bburx - bbllx;
  152.     rawPsfigHeight = bbury - bblly;
  153.     psfigWidth = fastFromSpHoriz (w, (double)(rawPsfigWidth));
  154.     psfigHeight = fastFromSpVert (w, (double)(rawPsfigHeight));
  155.     psfigScaleX = (double) width / psfigWidth;
  156.     psfigScaleY = (double) height / psfigHeight;
  157.  
  158. #ifdef DEBUG
  159.     fprintf (stderr, "rx=%d ry=%d W = %d H = %d\n\
  160. (w, h) = (%d %d) :: (%d %d)\n\
  161. %d %d :: %f %f\n",
  162.         rx, ry, w->dviPage.pixelsWide, w->dviPage.pixelsHigh,
  163.         rawWidth, rawHeight, width, height, 
  164.         rawPsfigWidth, rawPsfigHeight, psfigWidth, psfigHeight);
  165.     fprintf (stderr, "ll = (%d %d) :: (%d %d)\nur = (%d %d) :: (%d %d)\n",
  166.         bbllx, bblly, fastFromSpHoriz (w, bbllx), fastFromSpVert (w, bblly), 
  167.         bburx, bbury, fastFromSpHoriz (w, bburx), fastFromSpVert (w, bbury));
  168. #endif
  169.  
  170.     /* Save state */
  171.     WriteBytes (w, "startTexFig\n");
  172. #ifdef DEBUG
  173.     fprintf (stderr, "startTexFig\n");
  174. #endif
  175.  
  176.     /* Scale per psfig instructions */
  177.     sprintf (ghostInputStr, "%f %f scale\n", psfigScaleX, psfigScaleY);
  178.     WriteBytes (w, ghostInputStr);
  179. #ifdef DEBUG
  180.     fprintf (stderr, "%s", ghostInputStr);
  181. #endif
  182.     
  183.     /* Scale for pixels per inch goodals + magnification */
  184.     magScaleX = (double) w->dviPage.pixelsPerInchHoriz / 72.0;
  185.     magScaleY = (double) w->dviPage.pixelsPerInchVert / 72.0;
  186.     sprintf (ghostInputStr, "%f %f scale\n", magScaleX, magScaleY);
  187.     WriteBytes (w, ghostInputStr);
  188. #ifdef DEBUG
  189.     fprintf (stderr, "%s", ghostInputStr);
  190. #endif
  191.  
  192.     /* Translate origin to lower-left-corner of figure on page */
  193.     tx = -1.0 * fastFromSpHoriz (w, bbllx) / magScaleX;
  194.     ty = -1.0 * fastFromSpVert (w, bblly) / magScaleY;
  195.     sprintf (ghostInputStr, "%f %f translate\n", tx, ty);
  196.     WriteBytes (w, ghostInputStr);
  197. #ifdef DEBUG
  198.     fprintf (stderr, "%s", ghostInputStr);
  199. #endif 
  200.  
  201.     /* Show that were busy */
  202.     busyCursor ();
  203.     /*
  204.     TeXPageMakeInsensitive (w);
  205.     */
  206.     }
  207.  
  208.  
  209. void
  210. psfigEndGHOST (w, cp)
  211. DviPageWidget w;
  212. char *cp;
  213.     {
  214. char ghostSetupStr[1024];
  215.  
  216.     if (psfigError || (w->dviPage.ghostPID == -1))
  217.         return;
  218.  
  219.     WriteBytes (w, "endTexFig\n");
  220. #ifdef DEBUG
  221.     fprintf (stderr, "endTexFig\n");
  222. #endif
  223.  
  224.  
  225.     waitCopyAndAcknowledge (w, True, 0, w->dviPage.pixelsHigh - height, 
  226.         width, height, rx, ry);
  227.  
  228.     DialogMessage ("endTexFig\n", False);
  229.     unbusyCursor ();
  230.     /*
  231.     TeXPageMakeSensitive (w);
  232.     */
  233.     }
  234.  
  235. void
  236. psfigPlotfileGHOST (w, cp)
  237. DviPageWidget w;
  238. char *cp;
  239. {
  240.     char *fileName, ghostInputStr[1024];
  241.  
  242.     if (psfigError || (w->dviPage.ghostPID == -1))
  243.         return;
  244.  
  245.     /* Skip white space */
  246.     while (cp && *cp && isspace(*cp)) 
  247.         cp++;
  248.     if (strncmp (cp, "plotfile", 8) != 0)
  249.         return;
  250.  
  251.     /* Skip 'plotfile' */
  252.     cp += 8;
  253.  
  254.     /* Skip white space */
  255.     while (cp && *cp && isspace(*cp)) 
  256.         cp++;
  257.     fileName = cp;
  258.     /* Skip non-white */
  259.     while (cp && *cp && !isspace(*cp)) 
  260.         cp++;
  261.     /* Terminate file name */
  262.     *cp = 0;
  263.  
  264.     if (access(fileName,R_OK) == 0) {
  265.         DialogMessage ("\t", False);
  266.         DialogMessage (fileName, False);
  267.         DialogMessage ("\n", False);
  268.         sprintf (ghostInputStr, "(%s) run\n", fileName);
  269.     } else {        /* error in reading file */
  270.         double lx = -tx;
  271.         double ly = -ty;
  272.         sprintf(ghostInputStr,"Cannot access file %s. Drawing box.\n",
  273.             fileName);
  274.         DialogMessage(ghostInputStr, False);
  275.         sprintf(ghostInputStr,
  276.             "newpath %lf %lf moveto %lf %lf lineto %lf %lf \
  277. lineto %lf %lf lineto closepath gsave 0.9 setgray fill grestore\n",
  278.             lx, ly,
  279.             lx + psfigWidth, ly,
  280.             lx + psfigWidth, ly + psfigHeight,
  281.             lx, ly + psfigHeight);
  282.     }
  283.  
  284.     WriteBytes (w, ghostInputStr);
  285. #ifdef DEBUG
  286.     fprintf (stderr, "%s", ghostInputStr);
  287. #endif
  288. }
  289.