home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / TO_EPS.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  9KB  |  371 lines

  1. /*
  2.    Copyright (c) 1991 - 1993 Heinz W. Werntges.  All rights reserved.
  3.    Distributed by Free Software Foundation, Inc.
  4.  
  5. This file is part of HP2xx.
  6.  
  7. HP2xx is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  9. to anyone for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.  Refer
  11. to the GNU General Public License, Version 2 or later, for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. HP2xx, but only under the conditions described in the GNU General Public
  15. License.  A copy of this license is supposed to have been
  16. given to you along with HP2xx so you can know your rights and
  17. responsibilities.  It should be in a file named COPYING.  Among other
  18. things, the copyright notice and this notice must be preserved on all
  19. copies.
  20.  
  21. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23.  
  24. /** to_eps.c:     Converter to Encapsulated PostScript format;
  25.  **        (derived from PLPLOT driver  "postscript.c")
  26.  **
  27.  ** 91/06/29  V 1.00  HWW  Derived from postscript.c
  28.  ** 91/10/15  V 1.01  HWW  ANSI_C
  29.  ** 91/11/20  V 1.02  HWW  Changes due to "SPn;"
  30.  ** 91/12/22  V 1.02a HWW  Many small changes
  31.  ** 92/05/17  V 1.02b HWW  Output to stdout if outfile == '-'
  32.  ** 92/05/24  V 1.10a HWW  Color supported (RGB)
  33.  ** 92/10/20  V 1.10b HWW  Bug fix: Setting color implies a new path
  34.  ** 92/12/12  V 1.10c HWW  Info line now interprets outfile=='-' as "stdout"
  35.  ** 93/04/12  V 1.10d HWW  Explicit prototypes for time() and ctime() removed;
  36.  **               VMstatus reports only if !p->quiet
  37.  **/
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <time.h>
  43. #include <math.h>
  44. #include "bresnham.h"
  45. #include "hp2xx.h"
  46.  
  47.  
  48. #define    A4_height    297    /* in [mm]    */
  49.  
  50.  
  51.  
  52. extern    float    xmin, xmax, ymin, ymax;
  53. static        linecount = 0;
  54. static    float    coord2mm;
  55.  
  56.  
  57.  
  58.  
  59. /**
  60.  ** Close graphics file
  61.  **/
  62. void    ps_end (FILE *fd)
  63. {
  64.   fprintf(fd," S\neop\n");
  65.   fprintf(fd, "@end\n");
  66.   fprintf(fd, "%%Trailer\n");
  67.   fclose (fd);
  68.   linecount = 0;
  69. }
  70.  
  71.  
  72.  
  73. /**
  74.  ** Flush old path and move
  75.  **/
  76. void    ps_stroke_and_move_to (HPGL_Pt *ppt, FILE *fd)
  77. {
  78.   fprintf(fd, " S\n%6.2f %6.2f M",    /* S: Start a new path    */
  79.         (ppt->x-xmin) * coord2mm, (ppt->y-ymin) * coord2mm);
  80.   linecount = 0;
  81. }
  82.  
  83.  
  84.  
  85.  
  86. /**
  87.  ** Set line width
  88.  **/
  89. void    ps_set_linewidth (double width, HPGL_Pt *ppt, FILE *fd)
  90. {
  91.   ps_stroke_and_move_to (ppt, fd);    /* MUST start a new path!    */
  92.   fprintf  (fd," %6.3f W\n", width);
  93. }
  94.  
  95.  
  96.  
  97. /**
  98.  ** Set RGB color
  99.  **/
  100. void    ps_set_color (double red, double green, double blue,
  101.             HPGL_Pt *ppt, FILE *fd)
  102. {
  103.   ps_stroke_and_move_to (ppt, fd);    /* MUST start a new path!    */
  104.   fprintf  (fd," %6.3f %6.3f %6.3f C\n", red, green, blue);
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. void    ps_line_to (HPGL_Pt *ppt, char mode, FILE *fd)
  112. {
  113.   if (linecount > 3)
  114.   {
  115.       putc('\n', fd);
  116.       linecount = 0;
  117.   }
  118.   else
  119.       putc(' ', fd);
  120.  
  121.   fprintf(fd, "%6.2f %6.2f %c",
  122.         (ppt->x-xmin) * coord2mm, (ppt->y-ymin) * coord2mm, mode);
  123.   linecount++;
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. /**
  131.  ** Get the date and time: This is optional, since its result only
  132.  ** appeares in the PS header.
  133.  **/
  134.  
  135. char    *Getdate (void)
  136. {
  137. int len;
  138. long t;
  139. char *p;
  140.  
  141.   t = time((long *) 0);
  142.   p = ctime(&t);
  143.   len = strlen(p);
  144.   *(p + len - 1) = '\0';  /* zap the newline character */
  145.   return p;
  146. }
  147.  
  148.  
  149.  
  150. /**
  151.  ** PostScript definitions
  152.  **/
  153.  
  154. void    ps_init (PAR *p, FILE *fd)
  155. {
  156. long    left, right, low, high;
  157. int    pensize;
  158.  
  159.   pensize = p->pensize[p->pen];
  160.  
  161. /**
  162.  ** Header comments into PostScript file
  163.  **/
  164.  
  165.   fprintf(fd,"%%!PS-Adobe-2.0 EPSF-2.0\n");
  166.   fprintf(fd,"%%%%Title: %s\n", p->outfile);
  167.   fprintf(fd,"%%%%Creator: hp2xx (c) 1991, 1992 by H. Werntges\n");
  168.   fprintf(fd,"%%%%CreationDate: %s\n", Getdate());
  169.   fprintf(fd,"%%%%Pages: 1\n");
  170.  
  171. /**
  172.  ** Bounding Box limits: Conversion factor: 2.834646 * 1/72" = 1 mm
  173.  **/
  174.  
  175.   left  = (long) floor( p->xoff                * 2.834646);
  176.   low   = (long) floor((A4_height-p->yoff-p->height)* 2.834646);
  177.   right = (long) ceil ((p->xoff   + p->width)        * 2.834646);
  178.   high  = (long) ceil ((A4_height - p->yoff)        * 2.834646);
  179.   fprintf(fd,"%%%%BoundingBox: %ld %ld %ld %ld\n", left, low, right, high);
  180.   if (!p->quiet)
  181.     fprintf(stderr,"Bounding Box: [%ld %ld %ld %ld]\n",
  182.             left, low, right, high);
  183.  
  184.   fprintf(fd,"%%%%EndComments\n\n");
  185.  
  186. /**
  187.  ** Definitions
  188.  **/
  189.  
  190.   fprintf(fd,"%%%%BeginProcSet\n");
  191.   fprintf(fd,"/PSSave save def\n");      /* save VM state */
  192.   fprintf(fd,"/PSDict 200 dict def\n");  /* define a dictionary */
  193.   fprintf(fd,"PSDict begin\n");          /* start using it */
  194.   fprintf(fd,"/@restore /restore load def\n");
  195.   fprintf(fd,"/restore\n");
  196.   fprintf(fd,"   {vmstatus pop\n");
  197.   fprintf(fd,"    dup @VMused lt {pop @VMused} if\n");
  198.   fprintf(fd,"    exch pop exch @restore /@VMused exch def\n");
  199.   fprintf(fd,"   } def\n");
  200.  
  201.   fprintf(fd,"/@pri\n");
  202.   fprintf(fd,"   {\n");
  203.   fprintf(fd,"    ( ) print\n");
  204.   fprintf(fd,"    (                                       ) cvs print\n");
  205.   fprintf(fd,"   } def\n");
  206.  
  207.   fprintf(fd,"/@start\n");    /* - @start -  -- start everything */
  208.   fprintf(fd,"   {\n");
  209.   fprintf(fd,"    vmstatus pop /@VMused exch def pop\n");
  210.   fprintf(fd,"   } def\n");
  211.  
  212.   fprintf(fd,"/@end\n");      /* - @end -  -- finished */
  213.   fprintf(fd,"   {");
  214.   if (!p->quiet)
  215.   {
  216.     fprintf(fd,    "(VM Used: ) print @VMused @pri\n");
  217.     fprintf(fd,"    (. Unused: ) print vmstatus @VMused sub @pri pop pop\n");
  218.     fprintf(fd,"    (\\n) print flush\n");
  219.  
  220.   }
  221.   fprintf(fd,"    end\n");
  222.   fprintf(fd,"    PSSave restore\n");
  223.   fprintf(fd,"   } def\n");
  224.  
  225.   fprintf(fd,"/bop\n");       /* bop -  -- begin a new page */
  226.   fprintf(fd,"   {\n");
  227.   fprintf(fd,"    /SaveImage save def\n");
  228.   fprintf(fd,"   } def\n");
  229.  
  230.   fprintf(fd,"/eop\n");       /* - eop -  -- end a page */
  231.   fprintf(fd,"   {\n");
  232.   fprintf(fd,"    showpage\n");
  233.   fprintf(fd,"    SaveImage restore\n");
  234.   fprintf(fd,"   } def\n");
  235.  
  236.   fprintf(fd,"/@line\n");     /* set line parameters */
  237.   fprintf(fd,"   {1 setlinecap  %%%% Replace 1 by 0 for cut-off lines\n");
  238.   fprintf(fd,"    1 setlinejoin %%%% Replace 1 by 0 for cut-off lines\n");
  239.   fprintf(fd,"%%%%    1 setmiterlimit    %%%%  Uncomment this for cut-off lines\n");
  240.   fprintf(fd,"   } def\n");
  241.  
  242.   fprintf(fd,"/@SetPlot\n");
  243.   fprintf(fd,"   {\n");
  244.   fprintf(fd,"    2.834646 2.834646 scale\n");    /* 1/72"--> mm */
  245.   fprintf(fd,"    %7.3f %7.3f translate\n", p->xoff,
  246.             A4_height - p->yoff - p->height);
  247.   fprintf(fd,"    %6.3f setlinewidth\n", pensize/10.0);
  248.   fprintf(fd,"   } def\n");
  249.   fprintf(fd,"/C {setrgbcolor} def\n");
  250.   fprintf(fd,"/D {lineto} def\n");
  251.   fprintf(fd,"/M {moveto} def\n");
  252.   fprintf(fd,"/S {stroke} def\n");
  253.   fprintf(fd,"/W {setlinewidth} def\n");
  254.   fprintf(fd,"/Z {stroke newpath} def\n");
  255.   fprintf(fd,"%%%%EndProcSet\n");
  256.   fprintf(fd,"end\n\n");      /* end of dictionary definition */
  257.  
  258. /**
  259.  ** Set up the plots
  260.  **/
  261.  
  262.   fprintf(fd,"%%%%BeginSetup\n");
  263.   fprintf(fd,"PSDict begin\n");
  264.   fprintf(fd,"@start\n");
  265.   fprintf(fd,"@line\n");
  266.   fprintf(fd,"@SetPlot\n\n");
  267.   fprintf(fd,"bop\n");
  268.   fprintf(fd,"%%%%EndSetup\n");
  269. }
  270.  
  271.  
  272.  
  273. /**
  274.  ** Higher-level interface: Output Encapsulated PostScript format
  275.  **/
  276.  
  277. void    to_eps (PAR *p, FILE *td)
  278. {
  279. PlotCmd    cmd;
  280. HPGL_Pt    pt1 = {0};
  281. FILE    *md;
  282. int    pensize, pencolor;
  283.  
  284.   if (!p->quiet)
  285.     fprintf(stderr,"\n\n- Writing EPS code to \"%s\"\n",
  286.         *p->outfile == '-' ? "stdout" : p->outfile);
  287.  
  288.   /* Init. of PostScript file: */
  289.   if (*p->outfile != '-')
  290.   {
  291.     if ((md = fopen(p->outfile, "w")) == NULL)
  292.     {
  293.         perror("hp2xx (eps)");
  294.         exit(ERROR);
  295.     }
  296.   }
  297.   else
  298.     md = stdout;
  299.  
  300.   /* PS header */
  301.   ps_init (p, md);
  302.  
  303.   /* Factor for transformation of HP coordinates to mm    */
  304.  
  305.   coord2mm = p->height / (ymax-ymin);
  306.  
  307.  
  308.   pensize = p->pensize[p->pen];
  309.   if (pensize != 0)
  310.     fprintf(md," %6.3f W\n", pensize/10.0);
  311.  
  312. /**
  313.  ** Command loop: While temporaty file not empty process command.
  314.  **/
  315.  
  316.   while ((cmd = PlotCmd_from_tmpfile()) != EOF)
  317.   {
  318.     switch (cmd)
  319.     {
  320.       case NOP:
  321.         break;
  322.       case SET_PEN:
  323.         if ((p->pen = fgetc(td)) == EOF)
  324.         {
  325.             perror("Unexpected end of temp. file: ");
  326.             exit (ERROR);
  327.         }
  328.         pensize = p->pensize[p->pen];
  329.         if (pensize != 0)
  330.             ps_set_linewidth ((double) pensize/10.0, &pt1, md);
  331.         pencolor = p->pencolor[p->pen];
  332.         ps_set_color (  p->Clut[pencolor][0]/255.0,
  333.                 p->Clut[pencolor][1]/255.0,
  334.                 p->Clut[pencolor][2]/255.0,
  335.                 &pt1, md);
  336.         break;
  337.       case MOVE_TO:
  338.         HPGL_Pt_from_tmpfile (&pt1);
  339.         if (pensize != 0)
  340.             ps_stroke_and_move_to (&pt1, md);
  341.         break;
  342.       case DRAW_TO:
  343.         HPGL_Pt_from_tmpfile (&pt1);
  344.         if (pensize != 0)
  345.             ps_line_to (&pt1, 'D', md);
  346.         break;
  347.       case PLOT_AT:
  348.         HPGL_Pt_from_tmpfile (&pt1);
  349.         if (pensize != 0)
  350.         {
  351.             ps_line_to (&pt1, 'M', md);
  352.             ps_line_to (&pt1, 'D', md);
  353.         }
  354.         break;
  355.       default:
  356.         fprintf(stderr,"Illegal cmd in temp. file!");
  357.         exit (ERROR);
  358.     }
  359.   }
  360.  
  361.   /* Finish up */
  362.  
  363.   ps_end (md);
  364.   if (md != stdout)
  365.     fclose (md);
  366.  
  367.   if (!p->quiet)
  368.     fputc ('\n', stderr);
  369. }
  370.  
  371.