home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuplapi.zip / gnuplot-api-os2 / examples / plotgnuplot.c < prev   
Encoding:
C/C++ Source or Header  |  2001-03-06  |  8.9 KB  |  333 lines

  1. /* $Id: plotgnuplot.c,v 1.7 2000/11/03 21:00:24 karim Exp $
  2.  
  3. Copyright (C) 2000  The PARI group.
  4.  
  5. This file is part of the PARI/GP package.
  6.  
  7. PARI/GP is free software; you can redistribute it and/or modify it under the
  8. terms of the GNU General Public License as published by the Free Software
  9. Foundation. It is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY WHATSOEVER.
  11.  
  12. Check the License for details. You should have received a copy of it, along
  13. with the package; see the file 'COPYING'. If not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15.  
  16. /*******************************************************************/
  17. /*                                                                 */
  18. /*                  HI-RES PLOT. GNUPLOT INTERFACE                 */
  19. /*                   written by Ilya Zakharevich                   */
  20. /*                                                                 */
  21. /*******************************************************************/
  22. # include "pari.h"
  23. #include "rect.h"
  24. #define croak(str) err(talker,str)
  25. #define SET_OPTIONS_FROM_STRING
  26. #define GNUPLOT_OUTLINE_STDOUT
  27. #define DONT_POLLUTE_INIT
  28. #include "Gnuplot.h"
  29.  
  30. #ifdef __EMX__
  31. #  define DEF_TERM "pm"
  32. #else
  33. #  define DEF_TERM (getenv("DISPLAY") ? "X11" : "dumb")
  34. #endif
  35.  
  36. void
  37. rectdraw0(long *w, long *x, long *y, long lw, long do_free)
  38. {
  39.   double *ptx,*pty;
  40.   long i,j,x0,y0, hjust, vjust, hgap, vgap, hgapsize, vgapsize;
  41.   long good, seen_graph = 0;
  42.   int point_type = -1, line_type = 0;
  43.   PariRect *e;
  44.   RectObj *p1;
  45.   int strdir = RoSTdirLEFT, can_justify = 1, shift = 0, xstart, xend;
  46.  
  47.   (void)do_free;
  48.   PARI_get_plot(0);
  49.  
  50.   hgapsize = h_unit;  vgapsize = v_unit;
  51.   if (hgapsize == 1)
  52.     hgapsize = 2;    /* Vertical direction is subjectively different! */
  53.   /* Find the info about the *actual* x and y-coords of the
  54.      rectangles.  Use the first rectangle with has_graph attribute. */
  55.  
  56.   for(i=0;i<lw;i++) {
  57.       e=rectgraph[w[i]];
  58.       if (RHasGraph(e)) {
  59.       set_mouse_feedback_rectangle(
  60.         x[i], x[i] + RXsize(e) - 1,
  61.         w_height - 1 - y[i] - (RYsize(e) - 1), w_height - 1 - y[i],
  62.         (0 - RXshift(e))/RXscale(e),
  63.         (RXsize(e) - 1 - RXshift(e))/RXscale(e),
  64.         (RYsize(e) - 1 - RYshift(e))/RYscale(e),
  65.         (0 - RYshift(e))/RYscale(e)
  66.       );
  67.       seen_graph = 1;
  68.       break;
  69.       }
  70.   }
  71.   if (!seen_graph)            /* Put some reasonable values */
  72.       set_mouse_feedback_rectangle(    0, w_width - 1,    0, w_height - 1,
  73.                     0, 0, 0, 0);
  74.  
  75. #if 0
  76.   graphics();                /* Switch on terminal. */
  77. #else
  78.   term_start_plot();            /* Switch on terminal. */
  79. #endif
  80.   linetype(line_type);            /* X does not work otherwise. */
  81.   setpointsize(pointsize);
  82.  
  83.   for(i=0;i<lw;i++)
  84.   {
  85.     e=rectgraph[w[i]]; p1=RHead(e); x0=x[i]; y0=y[i];
  86.     while(p1)
  87.     {
  88.       switch(RoType(p1))
  89.       {
  90.     case ROt_PT:
  91.       point(DTOL(RoPTx(p1)+x0), w_height - 1 - DTOL(RoPTy(p1) + y0),
  92.         point_type);
  93.       break;
  94.     case ROt_LN:
  95.       move(DTOL(RoLNx1(p1)+x0), w_height - 1 - DTOL(RoLNy1(p1) + y0));
  96.       vector(DTOL(RoLNx2(p1)+x0), w_height - 1 - DTOL(RoLNy2(p1) + y0));
  97.       break;
  98.     case ROt_BX:
  99.       move(DTOL(RoBXx1(p1)+x0), w_height - 1 - DTOL(RoBXy1(p1) + y0));
  100.       vector(DTOL(RoBXx2(p1)+x0), w_height - 1 - DTOL(RoBXy1(p1) + y0));
  101.       vector(DTOL(RoBXx2(p1)+x0), w_height - 1 - DTOL(RoBXy2(p1) + y0));
  102.       vector(DTOL(RoBXx1(p1)+x0), w_height - 1 - DTOL(RoBXy2(p1) + y0));
  103.       vector(DTOL(RoBXx1(p1)+x0), w_height - 1 - DTOL(RoBXy1(p1) + y0));
  104.       break;
  105.     case ROt_MP:
  106.       ptx=RoMPxs(p1);
  107.       pty=RoMPys(p1);
  108.       for(j=0;j<RoMPcnt(p1);j++)
  109.       {
  110.         point(DTOL(ptx[j] + x0),  w_height - 1 - DTOL(pty[j] + y0),
  111.           point_type);
  112.       }
  113.       break;
  114.     case ROt_ML:
  115.       ptx=RoMLxs(p1);
  116.       pty=RoMLys(p1);
  117.       j = 0;
  118.       if (DTOL(ptx[j]+x0) < 0 || DTOL(ptx[j]+x0) >= w_width
  119.           || DTOL(pty[j] + y0) < 0 || DTOL(pty[j] + y0) >= w_height) {
  120.         good = 0;
  121.       } else {
  122.         move(DTOL(ptx[j]+x0), w_height - 1 - DTOL(pty[j] + y0));
  123.         good = 1;
  124.       }
  125.       for(j=1;j<RoMLcnt(p1);j++)
  126.       {
  127.         if (good) {
  128.           if (DTOL(ptx[j] + x0) < 0 || DTOL(ptx[j]+x0) >= w_width
  129.           || DTOL(pty[j] + y0) < 0 || DTOL(pty[j] + y0) >= w_height) {
  130.         good = 0;
  131.           } else {
  132.         vector(DTOL(ptx[j] + x0), w_height - 1 - DTOL(pty[j] + y0));
  133.           }
  134.         } else {
  135.           if (DTOL(ptx[j] + x0) < 0 || DTOL(ptx[j] + x0) >= w_width
  136.           || DTOL(pty[j] + y0) < 0 || DTOL(pty[j] + y0) >= w_height) {
  137.           } else {
  138.         move(DTOL(ptx[j]+x0), w_height - 1 - DTOL(pty[j] + y0));
  139.         good = 1;
  140.           }
  141.         }
  142.       }
  143.       break;
  144.     case ROt_ST:
  145.       hjust = RoSTdir(p1) & RoSTdirHPOS_mask;
  146.       vjust = RoSTdir(p1) & RoSTdirVPOS_mask;
  147.       hgap = RoSTdir(p1) & RoSTdirHGAP;
  148.       if (hgap)
  149.         hgap = (hjust == RoSTdirLEFT) ? hgapsize : -hgapsize;
  150.       vgap = RoSTdir(p1) & RoSTdirVGAP;
  151.       if (vgap)
  152.         vgap = (vjust == RoSTdirBOTTOM) ? vgapsize : -vgapsize;
  153.       if (vjust != RoSTdirVCENTER)
  154.         vgap += ((vjust == RoSTdirTOP) ? -1 : 1) * (f_height - 1)/2;
  155.       if (strdir != hjust) {
  156.           shift = (hjust == RoSTdirLEFT ? 0 :
  157.                (hjust == RoSTdirRIGHT ? 2 : 1));
  158.           can_justify = justify_text(shift); /* 1 for LEFT */
  159.           strdir = RoSTdir(p1);
  160.       }
  161.       xstart = DTOL(RoSTx(p1) + x0) + hgap
  162.           - (can_justify ? 0 
  163.          : ((RoSTl(p1) * pari_plot.fwidth - 1) * shift / 2));
  164.       xend = xstart + (can_justify ? 0 : RoSTl(p1) * pari_plot.fwidth - 1);
  165.       if (xstart < 0 || xend >= w_width
  166.           || DTOL(RoSTy(p1) + y0) < 0 
  167.           || DTOL(RoSTy(p1) + y0) >= w_height) {
  168.       } else {
  169.           put_text(xstart,
  170.                w_height - 1 - DTOL(RoSTy(p1) + y0) + vgap,
  171.                RoSTs(p1));
  172.       }
  173.       break;
  174.     case ROt_PTT:
  175.       point_type = RoPTTpen(p1);
  176.       break;
  177.     case ROt_PTS:
  178.       pointsize = RoPTSsize(p1);
  179.       setpointsize(pointsize);
  180.       break;
  181.     case ROt_LNT:
  182.       linetype(RoLNTpen(p1));
  183.       break;
  184.     default: break;
  185.       }
  186.       p1=RoNext(p1);
  187.     }
  188.   }
  189. #if 0
  190.   text();                /* Reset terminal */
  191. #else
  192.   term_end_plot();            /* Reset terminal. */
  193. #endif
  194. }
  195.  
  196. void
  197. PARI_get_plot(long fatal)
  198. {
  199.   (void)fatal;
  200.   if (pari_plot.init) {
  201.     return;
  202.   }
  203.   term_set( DEF_TERM );
  204. }
  205.  
  206.  
  207. long
  208. term_set(char *s)
  209. {
  210.   char *t, *size = NULL;
  211.   double x, y;
  212.   static int had_error;
  213.  
  214.   setup_gpshim();
  215.   if (*s == 0)
  216.       s = pari_plot.name;
  217.   t = s;
  218.   if (t[1] == '\0' && t[0] == '?') {
  219.      list_terms();
  220.      return 1;
  221.   }
  222.   while (*t && !(*t == ' ' || *t == '\t' || *t == '\n' || *t == '='))
  223.     t++;
  224.   if ((t-s) > PLOT_NAME_LEN)
  225.       err(talker,"name \"%s\" for terminal too long", s);
  226.   if (*pari_plot.name && !had_error
  227.       && (strlen(pari_plot.name) != t - s /* As strcmp() without \0 at end */
  228.       || (strncmp(pari_plot.name, s, t-s) != 0)) )
  229.     reset();
  230.   strncpy(pari_plot.name,s,t-s);
  231.   pari_plot.name[t-s] = '\0';
  232.  
  233.   had_error = 1;
  234.   if (!termset( pari_plot.name ))
  235.       err(talker,"error setting terminal \"%s\"", pari_plot.name);
  236.   had_error = 0;
  237.  
  238.   if (*t == '=') {
  239.     size = ++t;
  240.     x = atof(size);
  241.     while (*t && !(*t == ' ' || *t == '\t' || *t == '\n' || *t == ','))
  242.       t++;
  243.     if (*t != ',')
  244.       err(talker, "Terminal size directive without ','");
  245.     y = atof(++t);
  246.     while (*t && !(*t == ' ' || *t == '\t' || *t == '\n'))
  247.       t++;
  248.     plotsizes_scale(x*(1 + 1e-6)/termprop(xmax),
  249.             y*(1 + 1e-6)/termprop(ymax)); /* Later - truncated! */
  250.   } else {
  251.     plotsizes_scale(1,1);
  252.   }
  253.  
  254.   /* *Needed*, say, by gif output: */
  255.   set_options_from(t);
  256.  
  257. #if 0
  258.   gptable_init();            /* Init terminal. */
  259. #else
  260.   term_init();
  261. #endif
  262.  
  263.   setpointsize(pointsize);
  264.  
  265.   w_width = scaled_xmax();
  266.   w_height = scaled_ymax();
  267.   f_height = termprop(v_char);
  268.   f_width = termprop(h_char);
  269.   h_unit = termprop(h_tic);
  270.   v_unit = termprop(v_tic);
  271.   pari_plot.init = 1;
  272.  
  273.   return 1;
  274. }
  275.  
  276. long
  277. plot_outfile_set(char *s) { 
  278.     int normal = (strcmp(s,"-") == 0);
  279.  
  280.     setup_gpshim();
  281.     /* Delegate all the hard work to term_set_output() */
  282.  
  283.     if (normal) 
  284.     term_set_output(NULL);
  285.     else {                /* term_set_output() needs
  286.                        a malloced string */
  287.     char *s1 = (char*) malloc(strlen(s) + 1);
  288.  
  289.     strcpy(s1,s);
  290.     term_set_output(s1);
  291.     }
  292.     return 1; 
  293. }
  294.  
  295. void
  296. set_pointsize(double d) 
  297. {
  298.     pointsize = d;
  299.     if (pari_plot.init)
  300.     setpointsize(d);
  301. }
  302.  
  303. #ifdef DYNAMIC_PLOTTING_RUNTIME_LINK
  304. #include <dlfcn.h>
  305.  
  306. get_term_ftable_t *
  307. get_term_ftable_get(void) /* Establish runtime link with gnuplot engine */
  308. {
  309.     char *s = getenv("GNUPLOT_DRAW_DLL"), buf[4096];
  310.     void *h, *f;
  311.     int mode = RTLD_LAZY;
  312.  
  313. #ifdef RTLD_GLOBAL
  314.     mode |= RTLD_GLOBAL;
  315. #endif
  316.  
  317.     if (!s)
  318.     s = DYNAMIC_PLOTTING_RUNTIME_LINK;
  319.     h = dlopen(s, mode);
  320.     if (!h) {
  321.     sprintf(buf,"Can't load Gnuplot drawing engine from '%s': %s", s, dlerror());
  322.     croak(buf);
  323.     return 0;
  324.     }
  325.     f = dlsym(h, "get_term_ftable");
  326.     if (f)
  327.     return (get_term_ftable_t *)f;
  328.     sprintf(buf, "Can't resolve 'get_term_ftable' function from Gnuplot drawing engine '%s': %s", s, dlerror());
  329.     croak(buf);
  330.     return 0;
  331. }
  332. #endif
  333.