home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / viewwrld / viewwrld.lha / viewworld / gr / postscript.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-21  |  6.7 KB  |  225 lines

  1. /* $Id: postscript.c,v 2.3 89/09/20 17:01:44 mbp Exp $
  2.  *
  3.  * postscript.c: procedures for writing PostScript files
  4.  */
  5.  
  6. /************************************************************************
  7.  *        Copyright (C) 1989 by Mark B. Phillips                  *
  8.  *                                     *
  9.  * Permission to use, copy, modify, and distribute this software and    *
  10.  * its documentation for any purpose and without fee is hereby granted, *
  11.  * provided that the above copyright notice appear in all copies and    *
  12.  * that both that copyright notice and this permission notice appear in *
  13.  * supporting documentation, and that the name of Mark B. Phillips or   *
  14.  * the University of Maryland not be used in advertising or publicity   *
  15.  * pertaining to distribution of the software without specific, written *
  16.  * prior permission.  This software is provided "as is" without express *
  17.  * or implied warranty.                                                 *
  18.  ************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include "gr.h"
  23. #include "internal.h"
  24.  
  25. #define RADTODEG(a) ((a)*180/M_PI);
  26.  
  27. #define HEADER 1
  28. #define TRAILER 2
  29.  
  30. static int current_x=0;
  31. static int current_y=0;
  32.  
  33. static FILE *postscript_fp=NULL;
  34. static FILE *find_ps_file();
  35. char *getenv();
  36.  
  37. /*-----------------------------------------------------------------------
  38.  * Function:    GR_begin_postscript_file
  39.  * Description:    initialize PostScript output to a file
  40.  * Args  IN:    fp: pointer to already opened file
  41.  */
  42. GR_begin_postscript_file(fp, logo, label, time)
  43.      FILE *fp;
  44.      char *logo, *label, *time;
  45. {
  46.   FILE *header_fp;
  47.   int c;
  48.  
  49.   /* Make sure we're not already plotting */
  50.   if (postscript_fp != NULL) return(1);
  51.  
  52.   /* Save the file pointer */
  53.   postscript_fp = fp;
  54.  
  55.   /* Write initial lines of PostScript to file */
  56.   if (fprintf(postscript_fp, "%%!\n") == EOF) return(1);
  57.   if (fprintf(postscript_fp, "/CanvasWidth %1d def\n", GR_canvas_width)
  58.       == EOF) return(1);
  59.   if (fprintf(postscript_fp, "/CanvasHeight %1d def\n", GR_canvas_height)
  60.       == EOF) return(1);
  61.  
  62.   if ( (label != NULL) && (strlen(label)>0) ) {
  63.     if (fprintf(postscript_fp, "/LabelPresent 1 def\n") == EOF) return(1);
  64.     if (fprintf(postscript_fp, "/Label (%s) def\n",label) == EOF) return(1);
  65.   }
  66.   else
  67.     if (fprintf(postscript_fp, "/LabelPresent 0 def\n") == EOF) return(1);
  68.  
  69.   if ( (time != NULL) && (strlen(time)>0) ) {
  70.     if (fprintf(postscript_fp, "/TimePresent 1 def\n") == EOF) return(1);
  71.     if (fprintf(postscript_fp, "/Time (%s) def\n",time) == EOF) return(1);
  72.   }
  73.   else
  74.     if (fprintf(postscript_fp, "/TimePresent 0 def\n") == EOF) return(1);
  75.  
  76.   if ( (logo != NULL) && (strlen(logo)>0) ) {
  77.     if (fprintf(postscript_fp, "/LogoPresent 1 def\n") == EOF) return(1);
  78.     if (fprintf(postscript_fp, "/Logo (%s) def\n",logo) == EOF) return(1);
  79.   }
  80.   else
  81.     if (fprintf(postscript_fp, "/LogoPresent 0 def\n") == EOF) return(1);
  82.  
  83.   /* Copy PostScript header file into new file */
  84.   header_fp = find_ps_file( HEADER );
  85.   if (header_fp == NULL) {
  86.     GR_error("I can't find the PostScript header file !");
  87.     return(1);
  88.   }
  89.   else {
  90.     while ((c=getc(header_fp))!=EOF) putc(c,postscript_fp);
  91.     fclose(header_fp);
  92.   }
  93.   return(0);
  94. }
  95.  
  96. /*-----------------------------------------------------------------------
  97.  * Function:    GR_end_postscript_file
  98.  * Description:    terminate PostScript output to a file
  99.  * Args:    (none)
  100.  * Notes:    Should only be called after a call to
  101.  *        GR_begin_postscript_file.   This call does NOT close
  102.  *        the file.
  103.  */
  104. int
  105. GR_end_postscript_file()
  106. {
  107.   FILE *trailer_fp;
  108.   int c;
  109.  
  110.   /* Make sure we're plotting */
  111.   if (postscript_fp == NULL) return(1);
  112.   
  113.   /* Copy PostScript trailer file into the new file */
  114.   trailer_fp = find_ps_file( TRAILER );
  115.   if (trailer_fp == NULL) {
  116.     GR_error("I can't find the PostScript trailer file !");
  117.     return(1);
  118.   }
  119.   else {
  120.     while ((c=getc(trailer_fp))!=EOF) putc(c,postscript_fp);
  121.     fclose(trailer_fp);
  122.   }
  123.  
  124.   /* Reset the global file pointer so we know we're not plotting */
  125.   postscript_fp = NULL;
  126.  
  127.   return(0);
  128. }
  129.  
  130. int
  131. GR_postscript_move(x,y)
  132. int x,y;
  133. {
  134.   fprintf( postscript_fp, "%1d %1d m\n", x, y );
  135. }
  136.  
  137. int
  138. GR_postscript_draw(x,y)
  139. int x,y;
  140. {
  141.   fprintf( postscript_fp, "%1d %1d d\n", x, y );
  142. }
  143.  
  144. int
  145. GR_postscript_point()
  146. {
  147.   fprintf( postscript_fp, "p\n" );
  148. }
  149.  
  150. int
  151. GR_postscript_arc( xc,yc, x1,y1, x2,y2 )
  152.      int xc,yc,x1,y1,x2,y2;
  153. {
  154.   int xdiff, ydiff, r;
  155.   double ang1,ang2;
  156.   
  157.   /* Compute radius in device coords: */
  158.   xdiff = x1 - xc;
  159.   ydiff = y1 - yc;
  160.   r = (int) (sqrt( (double)(xdiff*xdiff + ydiff*ydiff) ) + .5);
  161.  
  162.   /* Check to see if the two points on the arc are equal: */
  163.   if ( (x1==x2) && (y1==y2) ) {
  164.  
  165.     /* If they are, draw a cicle through that point */
  166.     fprintf( postscript_fp, "%1d %1d %1d c\n", xc, yc, r );
  167.  
  168.   }
  169.   else {
  170.     /* Otherwise, draw the arc. The roles of x and y are switched in
  171.      * calculating the angles here, because in the PostScript file x
  172.      * is vertical and y is horizontal (the picture is drawn
  173.      * sideways).  The PostScript file automatically switches the x &
  174.      * y coordinates of points, however, which is why we DON'T
  175.      * interchange xc and yc.  This is terribly confusing, but it's
  176.      * the first combination I came up with which works! */
  177.     ang1 = RADTODEG( atan2( (double)xdiff, (double)ydiff ) );
  178.     ang2 = RADTODEG( atan2( (double)(x2 - xc), (double)(y2 - yc) ) );
  179.     fprintf( postscript_fp, "%1d %1d %1d %f %f a\n", xc, yc, r, ang1, ang2 );
  180.   }
  181. }
  182.  
  183. /*-----------------------------------------------------------------------
  184.  * Function:     find_ps_file
  185.  * Description:  return FILE ptr to .ps header or trailer file
  186.  * Arguments:    type: tells which file to look for:
  187.  *                 HEADER means look for header file
  188.  *                 TRAILER means look for trailer file
  189.  * Returns:      ptr to file opened for reading
  190.  * Notes:        This procedure looks for this files "gr_header.ps" nad
  191.  *               "gr_trailer.ps" in the following places, in this order:
  192.  *               1. In the directory given by the environment variable
  193.  *                  GR_LIB, if this variable is set
  194.  *               2. In the "hardwired" location, given by the strings
  195.  *                  HEADER_FILE, and TRAILER_FILE.
  196.  */
  197. static FILE *
  198. find_ps_file(c)
  199. int c;
  200. {
  201.   char fname[256], fbase[20], *gr_lib;
  202.   int found;
  203.   FILE *fp;
  204.  
  205.   sprintf(fbase, "%s", c==HEADER ? "gr_header.ps" : "gr_trailer.ps");
  206.  
  207.   /* First look in GR_LIB */
  208.   gr_lib = getenv("GR_LIB");
  209.   if (gr_lib != NULL) {
  210.     sprintf(fname,"%s%s%s",
  211.         gr_lib, (gr_lib[strlen(gr_lib)-1]=='/') ? "" : "/",
  212.         fbase);
  213.     fp = fopen(fname,"r");
  214.     if (fp != NULL) return(fp);
  215.   }
  216.  
  217.   /* Then in hardwired location */
  218.   sprintf(fname,"%s", c==HEADER ? HEADER_FILE : TRAILER_FILE );
  219.   fp = fopen(fname,"r");
  220.   if (fp != NULL) return(fp);
  221.  
  222.   /* If we got this far, it doesn't exist */
  223.   return(NULL);
  224. }
  225.