home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 334_02 / post.trm < prev    next >
Text File  |  1991-02-05  |  9KB  |  329 lines

  1. /* GNUPLOT - post.trm */
  2. /*
  3.  * Copyright (C) 1990   
  4.  *
  5.  * Permission to use, copy, and distribute this software and its
  6.  * documentation for any purpose with or without fee is hereby granted, 
  7.  * provided that the above copyright notice appear in all copies and 
  8.  * that both that copyright notice and this permission notice appear 
  9.  * in supporting documentation.
  10.  *
  11.  * Permission to modify the software is granted, but not the right to
  12.  * distribute the modified code.  Modifications are to be distributed 
  13.  * as patches to released version.
  14.  *  
  15.  * This software  is provided "as is" without express or implied warranty.
  16.  * 
  17.  * This file is included by ../term.c.
  18.  *
  19.  * This terminal driver supports:
  20.  *     postscript, psbig, epsf1, epsf2
  21.  *
  22.  * AUTHORS
  23.  *  Russell Lang
  24.  * 
  25.  * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
  26.  * 
  27.  */
  28.  
  29.  
  30. /* PostScript driver by Russell Lang, rjl@monu1.cc.monash.edu.au */
  31.  
  32. int pspage=0;            /* page count */
  33. int ps_path_count=0;     /* count of lines in path */
  34. int ps_ang=0;            /* text angle */
  35. enum JUSTIFY ps_justify=LEFT;    /* text is flush left */
  36. char *ps_font;            /* name of font in use */
  37. BOOLEAN ps_big;            /* true if big font used */
  38.  
  39. char *PS_header[] = {
  40. "/vpt2 vpt 2 mul def\n",
  41. "/hpt2 hpt 2 mul def\n",
  42. /* flush left show */
  43. "/Lshow { currentpoint stroke moveto\n",
  44. "  0 vshift rmoveto show } def\n", 
  45. /* flush right show */
  46. "/Rshow { currentpoint stroke moveto\n",
  47. "  dup stringwidth pop neg vshift rmoveto show } def\n", 
  48. /* centred show */
  49. "/Cshow { currentpoint stroke moveto\n",
  50. "  dup stringwidth pop -2 div vshift rmoveto show } def\n", 
  51. /* Border Lines */
  52. "/BL { stroke gnulinewidth 2 mul setlinewidth } def\n",
  53. /* Axes Lines */
  54. "/AL { stroke gnulinewidth 2 div setlinewidth } def\n",
  55. /* Plot Lines */
  56. "/PL { stroke gnulinewidth setlinewidth } def\n",
  57. /* Line Types */
  58. "/LTb { BL [] 0 setdash } def\n", /* border */
  59. "/LTa { AL [1 dl 2 dl] 0 setdash } def\n", /* axes */
  60. "/LT0 { PL [] 0 setdash } def\n",
  61. "/LT1 { PL [4 dl 2 dl] 0 setdash } def\n",
  62. "/LT2 { PL [2 dl 3 dl] 0 setdash } def\n",
  63. "/LT3 { PL [1 dl 1.5 dl] 0 setdash } def\n",
  64. "/LT4 { PL [5 dl 2 dl 1 dl 2 dl] 0 setdash } def\n",
  65. "/LT5 { PL [4 dl 3 dl 1 dl 3 dl] 0 setdash } def\n",
  66. "/LT6 { PL [2 dl 2 dl 2 dl 4 dl] 0 setdash } def\n",
  67. "/LT7 { PL [2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 0 setdash } def\n",
  68. "/LT8 { PL [2 dl 2 dl 2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 0 setdash } def\n",
  69. "/M {moveto} def\n",
  70. "/L {lineto} def\n",
  71. "/P {  LT0\n", /* Point */
  72. "  currentlinewidth 2 div sub moveto\n",
  73. "  0 currentlinewidth rlineto  stroke } def\n",
  74. "/D {  LT0  2 copy  vpt add moveto\n", /* Diamond */
  75. "  hpt neg vpt neg rlineto  hpt vpt neg rlineto\n",
  76. "  hpt vpt rlineto  hpt neg vpt rlineto  closepath  stroke\n",
  77. "  P  } def\n",
  78. "/A {  LT0  vpt sub moveto  0 vpt2 rlineto\n", /* Plus (Add) */
  79. "  currentpoint stroke moveto\n",
  80. "  hpt neg vpt neg rmoveto  hpt2 0 rlineto stroke\n",
  81. "  } def\n",
  82. "/B {  LT0  2 copy  exch hpt sub exch vpt add moveto\n", /* Box */
  83. "  0 vpt2 neg rlineto  hpt2 0 rlineto  0 vpt2 rlineto\n",
  84. "  hpt2 neg 0 rlineto  closepath  stroke\n",
  85. "  P  } def\n",
  86. "/C {  LT0  exch hpt sub exch vpt add moveto\n", /* Cross */
  87. "  hpt2 vpt2 neg rlineto  currentpoint  stroke  moveto\n",
  88. "  hpt2 neg 0 rmoveto  hpt2 vpt2 rlineto stroke  } def\n",
  89. "/T {  LT0  2 copy  vpt 1.12 mul add moveto\n", /* Triangle */
  90. "  hpt neg vpt -1.62 mul rlineto\n",
  91. "  hpt 2 mul 0 rlineto\n",
  92. "  hpt neg vpt 1.62 mul rlineto  closepath  stroke\n",
  93. "  P  } def\n",
  94. "/S {  LT0  2 copy A C} def\n", /* Star */
  95. NULL
  96. };
  97.  
  98. #define PS_XMAX 3600
  99. #define PS_YMAX 2520
  100.  
  101. #define PS_XLAST (PS_XMAX - 1)
  102. #define PS_YLAST (PS_YMAX - 1)
  103.  
  104. #define PS_VTIC (PS_YMAX/80)
  105. #define PS_HTIC (PS_YMAX/80)
  106.  
  107.  
  108. #define PS_SC (PS_XMAX/360)        /* scale is 1pt = 10 units */
  109. #define    PS_LW (0.25*PS_SC)        /* linewidth = 0.25 pts */
  110.  
  111. #define PS_VCHAR1 (7*PS_SC)        /* 7 point characters */
  112. #define PS_HCHAR1 (7*PS_SC*6/10)
  113.  
  114. #define PS_VCHAR2 (11*PS_SC)        /* 11 point characters */
  115. #define PS_HCHAR2 (11*PS_SC*6/10)
  116.  
  117.  
  118. EPSF1_init()
  119. {
  120.     ps_big = FALSE;
  121.     ps_font = "Courier";
  122.     /* the font could also be "Courier-Bold" or "Times-Roman" etc. */
  123. }
  124.  
  125.  
  126. EPSF2_init()
  127. {
  128.     ps_big = TRUE;
  129.     ps_font = "Courier";
  130. }
  131.  
  132.  
  133. EPSF_graphics()
  134. {
  135. int i;
  136.     fprintf(outfile,"%%!PS-Adobe-2.0 EPSF-2.0\n");
  137.     fprintf(outfile,"%%%%Creator: gnuplot\n");
  138.     fprintf(outfile,"%%%%DocumentFonts: %s\n", ps_font);
  139.     fprintf(outfile,"%%%%DocumentNeededFonts: %s\n", ps_font);
  140.     fprintf(outfile,"%%%%BoundingBox: 0 0 %d %d\n",
  141.         (int)(xsize*(PS_XMAX)/PS_SC+0.5), 
  142.         (int)(ysize*(PS_YMAX)/PS_SC+0.5) );
  143.     fprintf(outfile,"%%%%EndComments\n");
  144.     fprintf(outfile,"40 dict begin\n");
  145.     fprintf(outfile,"/gnulinewidth %.3f def\n",PS_LW);
  146.     fprintf(outfile,"/vshift %d def\n", 
  147.         ps_big ? -PS_VCHAR2/3 : -PS_VCHAR1/3);
  148.     fprintf(outfile,"/dl {%d mul} def\n",PS_SC); /* dash length */
  149.     fprintf(outfile,"/hpt %.1f def\n",PS_HTIC/2.0);
  150.     fprintf(outfile,"/vpt %.1f def\n",PS_VTIC/2.0);
  151.     for ( i=0; PS_header[i] != NULL; i++)
  152.         fprintf(outfile,"%s",PS_header[i]);
  153.     fprintf(outfile,"%%%%EndProlog\n");
  154.     fprintf(outfile,"%%%%BeginSetup\n");
  155.     fprintf(outfile,"/Gnu_save save def\n");
  156.     fprintf(outfile,"%.3f %.3f scale\n",1.0/PS_SC,1.0/PS_SC);
  157.     fprintf(outfile,"0 setgray\n");
  158.     fprintf(outfile,"%%%%IncludeFont: %s\n", ps_font);
  159.     fprintf(outfile,"/%s findfont %d ", ps_font, 
  160.         ps_big ? PS_VCHAR2 : PS_VCHAR1 );
  161.     fprintf(outfile,"scalefont setfont\n");
  162.     fprintf(outfile,"newpath\n");
  163.     ps_path_count = 0;
  164.     fprintf(outfile,"%%%%EndSetup\n");
  165. }
  166.  
  167.  
  168. EPSF_text()
  169. {
  170.     fprintf(outfile,"stroke\nGnu_save restore\n");
  171.     fprintf(outfile,"showpage\n");
  172.     ps_path_count = 0;
  173.     fprintf(outfile,"%%%%Trailer\n");
  174.     fprintf(outfile,"end\n");
  175. }
  176.  
  177.  
  178. EPSF_reset()
  179. {
  180. }
  181.  
  182.  
  183. PS1_init()
  184. {
  185.     EPSF1_init();
  186.     PS_init();
  187. }
  188.  
  189.  
  190. PS2_init()
  191. {
  192.     EPSF2_init();
  193.     PS_init();
  194. }
  195.  
  196.  
  197. PS_init()
  198. {
  199.     fprintf(outfile,"%%!PS-Adobe-2.0\n");
  200.     fprintf(outfile,"%%%%Creator: gnuplot\n");
  201.     fprintf(outfile,"%%%%DocumentFonts: %s\n", ps_font);
  202.     fprintf(outfile,"%%%%DocumentNeededFonts: %s\n", ps_font);
  203.     fprintf(outfile,"%%%%Pages: (atend)\n");
  204.     fprintf(outfile,"%%%%BoundingBox: %d 50 550 %d\n",
  205.         (int)(550.5-ysize*(PS_YMAX)/PS_SC*2), 
  206.         (int)(50.5+xsize*(PS_XMAX)/PS_SC*2) );
  207.     fprintf(outfile,"%%%%EndComments\n");
  208.     fprintf(outfile,"/GnuTransform {\n");
  209.     fprintf(outfile,"  90 rotate\n  50 -550 translate\n  2 2 scale\n");
  210.     fprintf(outfile,"} def\n");
  211.     fprintf(outfile,"%%%%EndProlog\n");
  212.     pspage = 0;
  213. }
  214.  
  215.  
  216.  
  217. PS_graphics()
  218. {
  219.     fprintf(outfile,"%%%%Page: ? %d\n",++pspage);
  220.     fprintf(outfile,"gsave\nGnuTransform\n");
  221.     fprintf(outfile,"%%%%BeginDocument: EPSF\n");
  222.     fprintf(outfile,"1 dict begin\n/showpage {} def\n");
  223.     EPSF_graphics();
  224. }
  225.  
  226.  
  227. PS_text()
  228. {
  229.     EPSF_text();
  230.     fprintf(outfile,"%%%%EndDocument\n");
  231.     fprintf(outfile,"end\nshowpage\ngrestore\n");
  232. }
  233.  
  234.  
  235.  
  236. PS_reset()
  237. {
  238.     fprintf(outfile,"%%%%Trailer\n");
  239.     fprintf(outfile,"%%%%Pages: %d\n",pspage);
  240. }
  241.  
  242.  
  243. PS_linetype(linetype)
  244. int linetype;
  245. {
  246. char *line = "ba012345678"; 
  247.     fprintf(outfile,"LT%c\n", line[(linetype%9)+2]);
  248.     ps_path_count = 0;
  249. }
  250.  
  251.  
  252. PS_move(x,y)
  253. unsigned int x,y;
  254. {
  255.     fprintf(outfile,"%d %d M\n", x, y);
  256.     ps_path_count += 1;
  257. }
  258.  
  259.  
  260. PS_vector(x,y)
  261. unsigned int x,y;
  262. {
  263.     fprintf(outfile,"%d %d L\n", x, y);
  264.     ps_path_count += 1;
  265.     if (ps_path_count >= 400) {
  266.         fprintf(outfile,"currentpoint stroke moveto\n");
  267.         ps_path_count = 0;
  268.     }
  269. }
  270.  
  271.  
  272. PS_put_text(x,y,str)
  273. unsigned int x, y;
  274. char *str;
  275. {
  276. char ch;
  277.     PS_move(x,y);
  278.     if (ps_ang != 0)
  279.         fprintf(outfile,"currentpoint gsave translate %d rotate 0 0 moveto\n"
  280.             ,ps_ang*90);
  281.     putc('(',outfile);
  282.     ch = *str++;
  283.     while(ch!='\0') {
  284.         if ( (ch=='(') || (ch==')') || (ch=='\\') )
  285.             putc('\\',outfile);
  286.         putc(ch,outfile);
  287.         ch = *str++;
  288.     }
  289.     switch(ps_justify) {
  290.         case LEFT : fprintf(outfile,") Lshow\n");
  291.             break;
  292.         case CENTRE : fprintf(outfile,") Cshow\n");
  293.             break;
  294.         case RIGHT : fprintf(outfile,") Rshow\n");
  295.             break;
  296.     }
  297.     if (ps_ang != 0)
  298.         fprintf(outfile,"grestore\n");
  299.     ps_path_count = 0;
  300. }
  301.  
  302. int PS_text_angle(ang)
  303. int ang;
  304. {
  305.     ps_ang=ang;
  306.     return TRUE;
  307. }
  308.  
  309. int PS_justify_text(mode)
  310. enum JUSTIFY mode;
  311. {
  312.     ps_justify=mode;
  313.     return TRUE;
  314. }
  315.  
  316. /* postscript point routines */
  317. PS_point(x,y,number)
  318. int x,y;
  319. int number;
  320. {
  321. char *point = "PDABCTS";
  322.     number %= POINT_TYPES;
  323.      if (number < -1)
  324.         number = -1;        /* negative types are all 'dot' */
  325.     fprintf(outfile,"%d %d %c\n", x, y, point[number+1]);
  326.     ps_path_count = 0;
  327. }
  328.  
  329.