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 / DviPageNEWS.c < prev    next >
C/C++ Source or Header  |  1992-06-25  |  9KB  |  312 lines

  1.  
  2. /*
  3.  * Copyright 1989 Dirk Grunwald
  4.  * 
  5.  * Permission to use, copy, modify, distribute, and sell this software
  6.  * and its documentation for any purpose is hereby granted without fee,
  7.  * provided that the above copyright notice appear in all copies and that
  8.  * both that copyright notice and this permission notice appear in
  9.  * supporting documentation, and that the name of Dirk Grunwald or M.I.T.
  10.  * not be used in advertising or publicity pertaining to distribution of
  11.  * the software without specific, written prior permission.  Dirk
  12.  * Grunwald and M.I.T. makes no representations about the suitability of
  13.  * this software for any purpose.  It is provided "as is" without express
  14.  * or implied warranty.
  15.  * 
  16.  * DIRK GRUNWALD AND M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  17.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18.  * FITNESS, IN NO EVENT SHALL M.I.T.  BE LIABLE FOR ANY SPECIAL, INDIRECT
  19.  * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  20.  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  21.  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  22.  * OR PERFORMANCE OF THIS SOFTWARE.
  23.  * 
  24.  * Author:
  25.  *     Dr. Dirk Grunwald
  26.  *     Dept. of Computer Science
  27.  *     Campus Box 430
  28.  *     Univ. of Colorado, Boulder
  29.  *     Boulder, CO 80309
  30.  * 
  31.  *     grunwald@colorado.edu
  32.  *     
  33.  */ 
  34.  
  35. #include <stdio.h>
  36. #include <sys/time.h>
  37. #include <X11/IntrinsicP.h>
  38. #include <X11/StringDefs.h>
  39. #include <stdio.h>
  40. #include <assert.h>
  41. #include <varargs.h>
  42. #include <math.h>
  43. #include <ctype.h>
  44. #include <sys/param.h>
  45.  
  46. #include "xtex.h"
  47. #include "dvi-simple.h"
  48. #include "DviPageP.h"
  49.  
  50. #include "DviPageNEWS.h"
  51.  
  52. void initNEWS()
  53. }
  54.  
  55. static Colormap xtex_colormap= (Colormap)0;
  56.  
  57. static void mangleColormap()
  58. {
  59.   Arg arg;
  60.   int xtex_screen;
  61.   int success;
  62.   int mincol = 10;
  63.   XColor xtex_color;
  64.   unsigned long planeMask;
  65.   unsigned int ncol;
  66.   unsigned long map[256];
  67.   int icol;
  68.  
  69.   
  70.   /* must modify the colormap so rasters plot in correct colors */
  71.   /* this is a big hack, arrived at by much trial and error */
  72.   xtex_screen = DefaultScreen(XtDisplay(TopLevelWidget));
  73.   xtex_colormap = DefaultColormap((XtDisplay(TopLevelWidget)),xtex_screen);
  74.  
  75.   /* first we allocate all available color cells */
  76.   ncol = 254;
  77.   do {
  78.     success = XAllocColorCells(XtDisplay(TopLevelWidget),xtex_colormap,
  79.                 0,&planeMask,0,map,ncol);
  80.     if (!success) ncol--;
  81.   } while (!success && ncol >= mincol);
  82.  
  83.   if( !success ) return;
  84.  
  85.   /* then we redefine those cells that NeWS seems to use for rasters */
  86.   /* and free up those that it does not */
  87.  
  88.   for (icol=0; icol<ncol; icol++) {
  89.      if (map[icol] >= 116 && map[icol] <= 129) {
  90.     /* a grey ramp */
  91.            xtex_color.red = (map[icol]-115)*65535/15;
  92.            xtex_color.green = (map[icol]-115)*65535/15;
  93.            xtex_color.blue = (map[icol]-115)*65535/15;
  94.            xtex_color.pixel = map[icol];
  95.     xtex_color.flags = DoRed | DoGreen | DoBlue;
  96.     XStoreColor(XtDisplay(TopLevelWidget),xtex_colormap,&xtex_color);
  97.      }else if (map[icol] == 192) {
  98.     /* a mid-grey */
  99.         xtex_color.red = 32768;
  100.         xtex_color.green = 32768;
  101.         xtex_color.blue = 32768;
  102.         xtex_color.pixel = map[icol];
  103.     xtex_color.flags = DoRed | DoGreen | DoBlue;
  104.     XStoreColor(XtDisplay(TopLevelWidget),xtex_colormap,&xtex_color);
  105.      }else if (map[icol] == 223) {
  106.     /* a lighter-grey */
  107.         xtex_color.red = 48000;
  108.         xtex_color.green = 48000;
  109.         xtex_color.blue = 48000;
  110.         xtex_color.pixel = map[icol];
  111.     xtex_color.flags = DoRed | DoGreen | DoBlue;
  112.     XStoreColor(XtDisplay(TopLevelWidget),xtex_colormap,&xtex_color);
  113.      }else if (map[icol] == 254) {
  114.     /* a white */
  115.         xtex_color.red = 65535;
  116.         xtex_color.green = 65535;
  117.         xtex_color.blue = 65535;
  118.         xtex_color.pixel = map[icol];
  119.     xtex_color.flags = DoRed | DoGreen | DoBlue;
  120.     XStoreColor(XtDisplay(TopLevelWidget),xtex_colormap,&xtex_color);
  121.      }else if (map[icol] == 255 || map[icol] == 130 || map[icol] == 131 || map[icol] == 135 || map[icol] == 136 || map[icol] == 155 || map[icol] == 156 || map[icol] == 160 || map[icol] == 161 ) {
  122.     /* lots of pixels seem to be black */
  123.         xtex_color.red = 0;
  124.         xtex_color.green = 0;
  125.         xtex_color.blue = 0;
  126.         xtex_color.pixel = map[icol];
  127.     xtex_color.flags = DoRed | DoGreen | DoBlue;
  128.     XStoreColor(XtDisplay(TopLevelWidget),xtex_colormap,&xtex_color);
  129.      }else if (map[icol] >= 0 && map[icol] <= 255) {
  130.     XFreeColors(XtDisplay(TopLevelWidget),xtex_colormap,&map[icol],1,0);
  131.      }else {
  132.     fprintf(stderr,"ignoring icol %d map %d\n",icol,map[icol]);
  133.      }
  134.   }
  135.  
  136.  
  137. }
  138.  
  139.  
  140. void
  141.   psfigBeginNEWS(w,cp,fudge_height)
  142. DviPageWidget w;
  143. char *cp;
  144. int fudge_height;
  145. {
  146.   int bbllx, bblly;
  147.   int bburx, bbury;
  148.   int width, height;
  149.   int rawWidth, rawHeight;
  150.   int haveExtensions;
  151.  
  152.   
  153.   int rx = fastFromSpHoriz(w, w -> dviPage.dviStackPointer -> h);
  154.   int ry = fastFromSpVert(w, w -> dviPage.dviStackPointer -> v);
  155.  
  156.   int maxWidth = XtScreen(w) -> width;
  157.   int mmWidth = WidthMMOfScreen( XtScreen(w) );
  158.   
  159.   double dpi = (w -> dviPage.dpiHoriz * w -> dviPage.userMag) / 1000;
  160.   double trueDpi;
  161.   double scale;
  162.   
  163.   if( xtex_colormap == (Colormap)0 ) mangleColormap();
  164.  
  165.   if ( w -> dviPage.trueDpi == 0.0 ) {
  166.     /* 72 dpi seems to be the correct value under OpenWindows */
  167.     /* also the values returned by maxWidth and mmWidth do not */
  168.     /* seem to be reliable.  So I've stuck with 72. */
  169.     /*    trueDpi = (maxWidth * 25.4) / mmWidth;*/
  170.     trueDpi = 72.;
  171.   }
  172.   else {
  173.     trueDpi = w -> dviPage.trueDpi;
  174.   }
  175.   scale = dpi / trueDpi;
  176.   
  177.   sscanf(cp, " %d %d %d %d %d %d ",
  178.      &rawWidth, &rawHeight,
  179.      &bbllx, &bblly,
  180.      &bburx, &bbury);
  181.   
  182.   width = fastFromSpHoriz(w, rawWidth);
  183.   height = fastFromSpVert(w, rawHeight);
  184.  
  185.   /*
  186.    * \epsfbox{} apparently causes a situation where the image is
  187.    * expected to grow upward, rather than down from the position on the
  188.    * page, so we fake it here
  189.    */
  190.   if( fudge_height ) ry -= height;
  191.   
  192.   /* catch up on the X side */
  193.  
  194.   XSync(XtDisplay(w),0);
  195.  
  196.   if (w -> dviPage.newsfile == NULL) {
  197.     w -> dviPage.newsfile = ps_open_PostScript();
  198.   }
  199.  
  200.   haveExtensions = w -> dviPage.newsfile != NULL;
  201.  
  202.   /* check if server has extension */
  203.   
  204.   if ( !haveExtensions ) {
  205.     XDrawLine(XtDisplay(w), XtWindow(w),
  206.           w -> dviPage.paintGC,
  207.           rx, ry, rx + width, ry );
  208.     XDrawLine(XtDisplay(w), XtWindow(w),
  209.           w -> dviPage.paintGC,
  210.           rx + width, ry, rx + width, ry + height);
  211.     XDrawLine(XtDisplay(w), XtWindow(w),
  212.           w -> dviPage.paintGC,
  213.           rx + width, ry + height, rx, ry + height);
  214.     XDrawLine(XtDisplay(w), XtWindow(w),
  215.           w -> dviPage.paintGC,
  216.           rx, ry + height, rx, ry);
  217.   }
  218.  
  219.   if ( haveExtensions ) {
  220.     /* get xid of window, then make this window the NeWS canvas */
  221.     XID xid = XtWindow(w);
  222.     NeWStoken newstoken;
  223.     int len;
  224.  
  225.     ps_token_from_xid(xid,&newstoken);
  226.     ps_setcanvas(newstoken);
  227.     
  228.     len = strlen(w -> dviPage.dpsPreamble);
  229.     psio_write(w -> dviPage.dpsPreamble, len, 1, PostScript);
  230.     psio_flush(PostScript);
  231.     
  232.     /* set origin, and flip y coordinates to agree with NeWS system */
  233.     psio_fprintf( PostScript,"\n %d %d @newsstart\n",rx,ry);
  234.     
  235.     psio_fprintf( PostScript,"\n%d %f @start\n",
  236.          w -> dviPage.pixelsPerInchHoriz,
  237.          scale);
  238.     
  239.     psio_fprintf( PostScript," %d %d %d %d %d %d  startTexFig\n",
  240.          rawWidth, rawHeight, bbllx, bblly, bburx, bbury);
  241.     
  242.     psio_flush(PostScript);
  243.   }
  244.  
  245. }
  246.  
  247. void
  248.   psfigPlotfileNEWS(w,cp)
  249. DviPageWidget w;
  250. char *cp;
  251. {
  252.   if ( w -> dviPage.newsfile )
  253.       {
  254.     while ( cp && *cp && isspace(*cp) ) cp++; /* skip white */
  255.     if (strncmp(cp,"plotfile",8) == 0 ) {
  256.       char *filename;
  257.       FILE *psfile;
  258.       
  259.       cp += 8; /* skip plotfile */
  260.       while ( cp && *cp && isspace(*cp) ) cp++; /* skip white */
  261.       filename = cp;
  262.       while ( cp && *cp && !isspace(*cp) ) cp++; /* skip non-white */
  263.       *cp = 0; /* terminate file name */
  264.       
  265.       if ( (psfile = fopen(filename, "r")) ) {
  266.         char buffer[1024];
  267.         int len;
  268.         
  269.         ps_flush_PostScript();
  270.         
  271.         for (;;) { 
  272.           len = fread(buffer, sizeof(char), 1024, psfile);
  273.           if  ( len == 0 ) break;
  274.           psio_write(buffer, len, 1, PostScript);
  275.         }      
  276.         fclose(psfile);
  277.         psio_flush(PostScript);
  278.       }
  279.     }
  280.     
  281.     psio_fprintf( PostScript,"\n endTexFig\n");
  282.     psio_flush(PostScript);
  283.     
  284.       }
  285. }
  286.  
  287. void
  288.   psfigEndNEWS(w,cp)
  289. DviPageWidget w;
  290. char *cp;
  291. {
  292.   if (w -> dviPage.newsfile ) {
  293.     psio_flush(PostScript);
  294.     ps_close_PostScript();
  295.     w -> dviPage.newsfile = NULL;
  296.   }
  297. }
  298.  
  299. int checkNEWS()
  300. {
  301.   PSFILE * newsfile;
  302.  
  303.   newsfile = ps_open_PostScript();
  304.   if( newsfile != NULL ){
  305.         ps_close_PostScript();
  306.         return 1;
  307.   }
  308.   return 0;
  309. }
  310.  
  311.