home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuplapi.zip / gnuplot-api-os2 / examples / Gnuplot.pm next >
Encoding:
Perl POD Document  |  1999-11-05  |  32.9 KB  |  997 lines

  1. package Term::Gnuplot;
  2.  
  3. =head1 NAME
  4.  
  5. Term::Gnuplot - lowlevel graphics using gnuplot drawing routines.
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.   use Term::Gnuplot ':ALL';
  10.   list_terms();
  11.   change_term('dumb') or die "Cannot set terminal.\n";
  12.   term_init();                # init()
  13.   term_start_plot();            # graphics();
  14.   $xmax = scaled_xmax();
  15.   $ymax = scaled_ymax();
  16.   linetype(-2);
  17.   move(0,0);
  18.   vector($xmax-1,0);
  19.   vector($xmax-1,$ymax-1);
  20.   vector(0,$ymax-1);
  21.   vector(0,0);
  22.   justify_text(LEFT);
  23.   put_text(h_char()*5, $ymax - v_char()*3,"Terminal Test, Perl");
  24.   $x = $xmax/4;
  25.   $y = $ymax/4;
  26.   $xl = h_tic()*5;
  27.   $yl = v_tic()*5;
  28.   linetype(2);
  29.   arrow($x,$y,$x+$xl,$y,1);
  30.   arrow($x,$y,$x+$xl/2,$y+$yl,1);
  31.   arrow($x,$y,$x,$y+$yl,1);
  32.   arrow($x,$y,$x-$xl/2,$y+$yl,0);
  33.   arrow($x,$y,$x-$xl,$y,1);
  34.   arrow($x,$y,$x-$xl,$y-$yl,1);
  35.   arrow($x,$y,$x,$y-$yl,1);
  36.   arrow($x,$y,$x+$xl,$y-$yl,1);
  37.   term_end_plot();            # text();
  38.   Term::Gnuplot::reset();
  39.  
  40. =head1 EXPORTS
  41.  
  42. None by default.
  43.  
  44. =head2 Exportable and export tags:
  45.  
  46. =over
  47.  
  48. =item C<:SETUP>
  49.  
  50.   change_term test_term init_terminal list_terms  plot_outfile_set
  51.   term_init term_start_plot term_end_plot
  52.   term_start_multiplot term_end_multiplot plotsizes_scale
  53.   setcanvas
  54.  
  55. =item C<:JUSTIFY>
  56.  
  57.   LEFT CENTRE RIGHT
  58.  
  59. =item C<:FIELDS>
  60.  
  61.   name description xmax ymax v_char h_char v_tic h_tic
  62.   scaled_xmax scaled_ymax
  63.  
  64. =item C<:METHODS>
  65.  
  66.   init scale graphics linetype move vector point text_angle
  67.   justify_text put_text arrow text
  68.  
  69. =item %description
  70.  
  71. Hash of C<name =E<gt> description> pairs.  In particular, C<keys
  72. %description> contains names of supported terminals.
  73.  
  74. =item C<:ALL>
  75.  
  76. All of the above.
  77.  
  78. =back
  79.  
  80. =head1 DESCRIPTION
  81.  
  82. Below I include the contents of the file F<term/README> from gnuplot
  83. distribution (see L<gnuplot F<term/README>>). It explains
  84. the meaning of the methods of L<"SYNOPSIS">.
  85.  
  86. All methods are supported under Perl, the C<options> method is available as
  87. set_options(). The discription below includes underscores, that are
  88. deleted in the perl interface.
  89.  
  90. The functions which are not included in the description below:
  91.  
  92. =over
  93.  
  94. =item C<change_term($newname)>
  95.  
  96. =item test_term()
  97.  
  98. =item init_terminal()
  99.  
  100. self-explanatory. 
  101.  
  102. =item list_terms()
  103.  
  104. Print names/descriptions of supported terminals.
  105.  
  106. =item C<plot_outfile_set($filename)>
  107.  
  108. set the output file.  This should be done before setting the terminal type.
  109.  
  110. =item C<plotsizes_scale($xfactor, $yfactor)>
  111.  
  112. set the size of the output device (such as a graphic file) in fractions
  113. of the default size.
  114. Some output drivers may ignore this request.  The request is active until the
  115. next request.
  116.  
  117. Does not change sizes reported by xmax() and ymax()!  After such a call
  118. one should use the following calls:
  119.  
  120. =item scaled_xmax(), scaled_ymax()
  121.  
  122. Report xmax() and ymax() corrected by the scaling factors given to
  123. plotsizes_scale().
  124.  
  125. =item term_init(), term_start_plot(), term_end_plot()
  126.  
  127. higher-level functions variants of init(), graphics(), text().  Should
  128. be prefered over init(), graphics(), text() if the output
  129. file is changed, since they take into account resetting of output file
  130. mode (if needed).
  131.  
  132. =item term_start_multiplot(), term_end_multiplot()
  133.  
  134. Interfaces to C functions with the same names.  How to use them
  135. outside of C<gnuplot> proper is not clear.
  136.  
  137. =item _term_descrs()
  138.  
  139. Returns hash of names/descriptions of supported terminals (available as
  140. %Term::Gnuplot::description too).
  141.  
  142. =item setcanvas($ptk_canvas)
  143.  
  144. Sets the Tk widget for direct-draw operations (may be used with
  145. terminal 'tkcanvas' with the option 'tkperl_canvas').
  146.  
  147. =back
  148.  
  149. B<NOTE.> Some terminals I<require> calling set_options() before init()!
  150.  
  151. =head1 gnuplot F<term/README>
  152.  
  153. DOCUMENTATION FOR GNUPLOT TERMINAL DRIVER WRITERS
  154.  
  155. By Russell Lang 1/90
  156.  
  157. Updated for new file layout by drd 4/95
  158.  
  159. Paragraphs about inclusion of TERM_HELP added by rcc 1/96
  160.  
  161. No change to the interface between gnuplot and the terminal drivers,
  162. but we would like to make the terminal drivers standalone
  163.  
  164. 1) in order move the support for the terminal drivers outside of the
  165.    support for the main program, thereby encouraging a library of
  166.    contributed drivers
  167. 2) To make it easy for users to add contributed drivers, by adding
  168.    a single #include line to term.h
  169. 3) To allow individual compilation on DOS, to save the overlay
  170.    manager from having to load _all_ drivers together.
  171.  
  172. CORRECTION - scale() interface is no longer supported, since it
  173. is incompatible with multiplot.
  174.  
  175. Whole of terminal driver should be contained in one <driver>.trm file,
  176. with a fairly strict layout as detailed below - this allows the
  177. gnuplot maintainers to change the way the terminal drivers are
  178. compiled without having to change the drivers themselves.
  179.  
  180. term.h, and therefore each file.trm file, may be loaded more than once,
  181. with different sections selected by macros.
  182.  
  183. Each driver provides all the functions it needs, and a table of
  184. function pointers and other data to interface to gnuplot.
  185. The table entry is currently defined as follows in plot.h:
  186.  
  187.   struct TERMENTRY {
  188.  
  189.   /* required entries */
  190.  
  191.     char *name;
  192.     char *description;
  193.     unsigned int xmax,ymax,v_char,h_char,v_tic,h_tic;
  194.  
  195.     void (*options) __PROTO((void));
  196.     void (*init) __PROTO((void));
  197.     void (*reset) __PROTO((void));
  198.     void (*text) __PROTO((void));
  199.     int (*scale) __PROTO((double, double));
  200.     void (*graphics) __PROTO((void));
  201.     void (*move) __PROTO((unsigned int, unsigned int));
  202.     void (*vector) __PROTO((unsigned int, unsigned int));
  203.     void (*linetype) __PROTO((int));
  204.     void (*put_text) __PROTO((unsigned int, unsigned int,char*));
  205.  
  206.   /* optional entries */
  207.  
  208.     int (*text_angle) __PROTO((int));
  209.     int (*justify_text) __PROTO((enum JUSTIFY));
  210.     void (*point) __PROTO((unsigned int, unsigned int,int));
  211.     void (*arrow) __PROTO((unsigned int, unsigned int, unsigned int,
  212.                unsigned int, int));
  213.    int (*set_font) __PROTO((char *font));  /* "font,size" */
  214.     void (*set_pointsize) __PROTO((double size)); /* notification of set pointsize */
  215.     int flags; /* various flags */
  216.    void (*suspend) __PROTO((void)); /* after one plot of multiplot */
  217.    void (*resume) __PROTO((void));  /* before subsequent plot of multiplot */
  218.    void (*boxfill) __PROTO((int style, unsigned int x1, unsigned int y1, unsigned int width, unsigned int height)); /* clear part of multiplot */
  219.    void (*linewidth) __PROTO((double linewidth));
  220.    void (*pointsize) __PROTO((double pointsize));
  221.   };
  222.  
  223. One consequence of (1) is that we would like drivers to be backwards
  224. compatible - drivers in the correct form below should work in future
  225. versions of gnuplot without change. C compilers guarantee to fill
  226. unitialised members of a structure to zero, so gnuplot can detect old
  227. drivers, in which fields have not been initalised, and can point
  228. new interface entry pointers to dummy functions.
  229.  
  230. We can add fields to the terminal structure, but only at the end of the list.
  231. If you design a terminal that cant work without a new interface being defined,
  232. and consequent changes to the main gnuplot source, please contact
  233. bug-gnuplot@dartmouth.edu simply to ensure that you have the most
  234. up to date defn of the terminal structure. Also, please ensure that
  235. the 'set term' command checks for 0 values in added fields when an
  236. old driver is selected, and a pointer to a suitable 'cant do' function
  237. is provided. It is therefore not required (and in fact not possible)
  238. to add padding fields to the end of all drivers.
  239.  
  240. Similarly, if you add an optional field to an old driver, take care
  241. to ensure that all intervening fields are padded with zeros.
  242.  
  243. Some of the above fields are required - this should not be a problem,
  244. since they were all required in earlier releases of gnuplot.
  245. The later fields are interfaces to capabilities that not all devices
  246. can do, or for which the generic routines provided should be adequate.
  247. There are several null ('cant do') functions provided by term.c which
  248. a driver can reference in the table. Similarly, for bitmap devices, there
  249. are generic routines for lines and text provided by bitmap.c
  250.  
  251.  
  252.  
  253. Here's a brief description of each variable:
  254.  
  255. The char *name is a pointer to a string containing the name
  256. of the terminal.  This name is used by the 'set terminal' and 
  257. 'show terminal' commands.  
  258. The name must be unique and must not be confused with an abbreviation 
  259. of another name.  For example if the name "postscript" exists, it is not
  260. possible to have another name "postscript2".
  261. Keep the name under 15 characters.
  262.  
  263. The char *description is a pointer to a string containing a
  264. description of the terminal, which is displayed in response
  265. to the 'set terminal' command.  
  266. Keep the description under 60 characters.
  267.  
  268. xmax is the maximum number of points in the x direction.  
  269. The range of points used by gnuplot is 0 to xmax-1.
  270.  
  271. ymax is the maximum number of points in the y direction.  
  272. The range of points used by gnuplot is 0 to ymax-1.
  273.  
  274. v_char is the height of characters, in the same units as xmax and ymax.
  275. The border for labelling at the top and bottom of the plot is 
  276. calculated using v_char.  
  277. v_char is used as the vertical line spacing for characters.
  278.  
  279. h_char is the width of characters, in the same units as xmax and ymax.
  280. The border for labelling at the left and right of the plot is 
  281. calculated using h_char, for example.
  282. If the _justify_text function returns FALSE, h_char is used to justify 
  283. text right or centre.  If characters are not fixed width, then the 
  284. _justify_text function must correctly justify the text.
  285.  
  286. v_tic is the vertical size of tics along the x axis, 
  287. in the same units as ymax.
  288.  
  289. h_tic is the horizontal size of tics along the y axis, 
  290. in the same units as xmax.
  291.  
  292. v_tic and h_tic should give tics of the same physical size on the
  293. output. The ratio of these two quantities is used by gnuplot to set the
  294. aspect ratio to 1 so that circles appear circular when 'set size square'
  295. is active.
  296.  
  297. All the above values need not be static - values can be substituted
  298. into the table during terminal initialisation, based on options for
  299. example.
  300.  
  301.  
  302.  
  303. Here's a brief description of what each term.c function does:
  304.  
  305. _options()  Called when terminal type is selected.  
  306. This procedure should parse options on the command line.  A list of the 
  307. currently selected options should be stored in term_options[] in a form 
  308. suitable for use with the set term command.  term_options[] is used by 
  309. the save command.  Use options_null() if no options are available. 
  310.  
  311. _init()  Called once, when the device is first selected.  This procedure
  312. should set up things that only need to be set once, like handshaking and
  313. character sets etc...
  314. There is a global variable 'pointsize' which you might want to use here.
  315. If set pointsize is issued after init has been called, the set_pointsize()
  316. function is called.
  317.  
  318. _reset()  Called when gnuplot is exited, the output device changed or
  319. the terminal type changed.  This procedure should reset the device, 
  320. possibly flushing a buffer somewhere or generating a form feed.
  321.  
  322. _scale(xs,ys) Called just before _graphics(). This takes the x and y
  323. scaling factors as information. If the terminal would like to do its
  324. own scaling, it returns TRUE. Otherwise, it can ignore the information
  325. and return FALSE: do_plot will do the scaling for you. null_scale is
  326. provided to do just this, so most drivers can ignore this function
  327. entirely. The Latex driver is currently the only one providing its own
  328. scaling. PLEASE DO NOT USE THIS INTERFACE - IT IS NOT COMPATIBLE WITH
  329. MULTIPLOT.
  330.  
  331. _graphics()  Called just before a plot is going to be displayed.  This
  332. procedure should set the device into graphics mode.  Devices which can't
  333. be used as terminals (like plotters) will probably be in graphics mode 
  334. always and therefore won't need this.
  335.  
  336. _text()  Called immediately after a plot is displayed.  This procedure 
  337. should set the device back into text mode if it is also a terminal, so
  338. that commands can be seen as they're typed.  Again, this will probably
  339. do nothing if the device can't be used as a terminal. This call can
  340. be used to trigger conversion and output for bitmap devices.
  341.  
  342. _move(x,y)  Called at the start of a line.  The cursor should move to the
  343. (x,y) position without drawing.
  344.  
  345. _vector(x,y)  Called when a line is to be drawn.  This should display a line
  346. from the last (x,y) position given by _move() or _vector() to this new (x,y)
  347. position.
  348.  
  349. _linetype(lt)  Called to set the line type before text is displayed or
  350. line(s) plotted.  This procedure should select a pen color or line
  351. style if the device has these capabilities.  
  352. lt is an integer from -2 to 0 or greater.  
  353. An lt of -2 is used for the border of the plot.
  354. An lt of -1 is used for the X and Y axes.  
  355. lt 0 and upwards are used for plots 0 and upwards.
  356. If _linetype() is called with lt greater than the available line types, 
  357. it should map it to one of the available line types.
  358. Most drivers provide 9 different linetypes (lt is 0 to 8).
  359.  
  360. _put_text(x,y,str)  Called to display text at the (x,y) position, 
  361. while in graphics mode.   The text should be vertically (with respect 
  362. to the text) justified about (x,y).  The text is rotated according 
  363. to _text_angle and then horizontally (with respect to the text)
  364. justified according to _justify_text.
  365.  
  366.  
  367. The following are optional
  368.  
  369.  
  370. _text_angle(ang)  Called to rotate the text angle when placing the y label.
  371. If ang = 0 then text is horizontal.  If ang = 1 then text is vertically
  372. upwards.  Returns TRUE if text can be rotated, FALSE otherwise.
  373. [But you must return TRUE if called with ang=0]
  374.  
  375. _justify_text(mode)  Called to justify text left, right or centre.
  376. If mode = LEFT then text placed by _put_text is flushed left against (x,y).
  377. If mode = CENTRE then centre of text is at (x,y).  
  378. If mode = RIGHT then text is placed flushed right against (x,y).
  379. Returns TRUE if text can be justified
  380. Returns FALSE otherwise and then _put_text assumes text is flushed left;
  381. justification of text is then performed by calculating the text width
  382. using strlen(text) * h_char.
  383.  
  384. _point(x,y,point)  Called to place a point at position (x,y).
  385. point is -1 or an integer from 0 upwards.  
  386. At least 6 point types (numbered 0 to 5) are normally provided.  
  387. Point type -1 is a dot.
  388. If point is more than the available point types then it should 
  389. be mapped back to one of the available points.
  390. Two _point() functions called do_point() and line_and_point() are 
  391. provided in term.c and should be suitable for most drivers.  
  392. do_point() draws the points in the current line type.
  393. If your driver uses dotted line types (generally because it is
  394. monochrome), you should use line_and_point() which changes to 
  395. line type 0 before drawing the point.  line type 0 should be solid.
  396.  
  397. There is a global variable 'pointsize' which is controlled by the
  398. set pointsize command. If possible, use that. pointsize should be
  399. examined at terminal init. If it is subsequently changed, the
  400. set_pointsize() fn will be called.
  401.  
  402.  
  403. _arrow(sx,sy,ex,ey,head)  Called to draw an arrrow from (sx,sy) to (ex,ey).
  404. A head is drawn on the arrow if head = TRUE.
  405. An _arrow() function called do_arrow() is provided in term.c which will
  406. draw arrows using the _move() and _vector() functions.  
  407. Drivers should use do_arrow unless it causes problems.
  408.  
  409. _set_font() is called to set the font of labels, etc [new 3.7 feature]
  410. fonts are selected as strings "name,size"
  411.  
  412. _pointsize() is used to set the pointsize for subsequent points
  413.  
  414. _flags stores various flags describing driver capabilities. Currently
  415.  three bits are allocated
  416.   - TERM_CAN_MULTIPLOT - driver can do multiplot
  417.   fully-interactively when output is not redirected. ie text and graphics
  418.   go to different places, or driver can flip using suspend.
  419.   - TERM_CANT_MULTIPLOT - driver cannot multiplot, even if output
  420.   is redirected.
  421.   - TERM_BINARY - output file must be opened in binary mode
  422.   Another bit is earmarked for VMS_PASTHRU, but not yet implemented.
  423.  
  424. _suspend() - called before gnuplot issues a prompt in multiplot mode
  425.    linux vga driver uses this entry point to flip from graphics to
  426.    text mode. X11 driver will take this opportunity to paint the window
  427.    on the display.
  428.  
  429. _resume() - called after suspend(), before subsequent plots of a multiplot.
  430.  
  431. _boxfill() - fills a box in given style (currently unimplemented - always
  432.            background colour at present). used by 'clear' in multiplot for
  433.            support of inset graphs
  434.  
  435. _linewidth() - sets the linewidth
  436.  
  437. The following should illustrate the order in which calls to these
  438. routines are made:
  439.  
  440.  _options()
  441.   _init()
  442.     _scale(xs,ys)
  443.     _graphics()
  444.       _linewidth(lw)
  445.       _linetype(lt)
  446.       _move(x,y)
  447.       _vector(x,y)
  448.       _pointsize(size)
  449.       _point(x,y,point)
  450.       _text_angle(angle)
  451.       _justify(mode)
  452.       _set_font(font)
  453.       _put_text(x,y,text)
  454.       _arrow(sx,sy,ex,ey)
  455.     _text()
  456.     _graphics()
  457.       .
  458.     _suspend()
  459.     _set_pointsize()
  460.     _resume()
  461.       .
  462.     _text()
  463.   _reset()
  464.  
  465.  
  466. ------------------------------------
  467.  
  468. BITMAP DEVICES
  469.  
  470. A file bitmap.c is provided, implementing a generic set of bitmap
  471. routines. It provides all the routines required to generate a
  472. bitmap in memory, drawing lines and writing text. A simple driver
  473. need provide only a text() entry point, which converts and outputs
  474. the stored bitmap in the format required by the device.
  475.  
  476. Internally, the bitmap is built of one or more planes of 1
  477. bit per pixel. In fact, I think the library would be easier to
  478. use if it offered one or more planes of pixels with 1,2,4 or 8
  479. bits per pixel, since not all bitmap devices are based on planes,
  480. and the planes have to be recombined at the end at present.
  481. In general, a device would use either planes or bits-per-pixel,
  482. though I guess a 24-bit bitmap could use 3 planes of 8 bits
  483. per pixel..?
  484.  
  485.  
  486. The pixels are currently organised horizontally packed into bytes.
  487.  
  488. ie
  489.  
  490.   ********%%%%%%%%$$$$$$$$!!!!!!!! etc
  491.   ^^^^^^^^@@@@@@@@########++++++++ etc
  492.  
  493. where like symbols are stored in one byte. Vertical packing can be
  494. arranged by reversing x and y dimensions and setting the global
  495. b_rastermode to TRUE.  (eg epson 8-pin dot-matrix printer)
  496.  
  497.  
  498. Functions provided are
  499.  
  500. (internal fns ? - should probably be static, not external ?)
  501.  
  502.   b_setpixel(x,y,value)     
  503.   b_setmaskpixel(x,y,value)
  504.   b_putc(x,y,char,angle)
  505.   b_setvalue(size)
  506.  
  507. setting up stuff
  508.  
  509.   b_makebitmap(x,y,planes)  - make a bitmap of size x * y
  510.   b_freebitmap()            - free bitmap
  511.   b_charsize(size)
  512.  
  513.  
  514. gnuplot driver interface functions  (can go straight into gnuplot structure)
  515.  
  516.   b_setlinetype(linetype)
  517.   b_move(x,y)
  518.   b_vector(x,y)
  519.   b_put_text(x,y,*str)
  520.   b_text_angle(ang)
  521.  
  522.  
  523.  
  524. I think that the library could be made easier to use if we defined
  525. a structure which described the bitmap (raster mode, planes, bits-per-pixel,
  526. colours, etc) and then added to the gnuplot term struct a pointer to
  527. this structure. Then we could have b_graphics() routine which did all
  528. the initialisation that presently has to be done by the driver graphics()
  529. entry point.  Also, one day I would like to have parsing, including
  530. terminal driver options, table-driven, but I'm getting ahead of myself
  531. here.
  532.  
  533.  
  534. At present, bitmap.c is linked into gnuplot unconditionally. Perhaps
  535. it should be put into a library, so that it is linked in only if
  536. any of the user-selected drivers require bitmap support.
  537.  
  538. There may be scope to do similar things with some of the other
  539. stuff that is shared by several drivers. Rather than requiring,
  540. for example, that LATEX driver is required if EMTEX is to be used,
  541. the shared routines could be extracted to a library and linked
  542. if any of the drivers which use them are used.  Just a thought...
  543.  
  544. ------------------------------------
  545.  
  546. FILE LAYOUT
  547. -----------
  548.  
  549. I think a file layout like the following will leave most flexibility
  550. to the gnuplot maintainers. I use REGIS for example.
  551.  
  552.  
  553.   #include "driver.h"
  554.  
  555.  
  556.   #ifdef TERM_REGISTER
  557.   register_term(regis) /* no ; */
  558.   #endif
  559.  
  560.  
  561.   #ifdef TERM_PROTO
  562.   TERM_PUBLIC void REGISinit __PROTO((void));
  563.   TERM_PUBLIC void REGISgraphics __PROTO((void));
  564.   /* etc */
  565.   #define GOT_REGIS_PROTO
  566.   #endif
  567.  
  568.  
  569.   #ifndef TERM_PROTO_ONLY
  570.   #ifdef TERM_BODY
  571.  
  572.   TERM_PUBLIC void REGISinit()
  573.   {
  574.     /* etc */
  575.   }
  576.  
  577.   /* etc */
  578.  
  579.   #endif
  580.  
  581.  
  582.   #ifdef TERM_TABLE
  583.  
  584.   TERM_TABLE_START(regis_driver)
  585.     /* no { */
  586.     "regis", "REGIS graphics language",
  587.     REGISXMAX, /* etc */
  588.     /* no } */
  589.   TERM_TABLE_END(regis_driver)
  590.  
  591.   #undef LAST_TERM
  592.   #define LAST_TERM regis_driver
  593.  
  594.   #endif /* TERM_TABLE */
  595.   #endif /* TERM_PROTO_ONLY */
  596.  
  597.  
  598.  
  599.  
  600.   #ifdef TERM_HELP
  601.   START_HELP(regis)
  602.   "1 regis",
  603.   "?set terminal regis",
  604.   "?regis",
  605.   " The `regis` terminal device generates output in the REGIS graphics language.",
  606.   " It has the option of using 4 (the default) or 16 colors.",
  607.   "",
  608.   " Syntax:",
  609.   "         set term regis {4 | 16}"
  610.   END_HELP(regis)
  611.   #endif
  612.  
  613.  
  614. --------------
  615.  
  616. The first three lines in the TERM_HELP section must contain the same
  617. name as that specified by register_term, since this is the name that
  618. will be entered into the list of available terminals.  If more than
  619. one name is registered, the additional names should have their own
  620. two "?" lines, but not the "1" line.
  621.  
  622. Each record is enclosed in double-quotes and (except for the last
  623. record) followed by a comma.  The text is copied as a single string
  624. into gnuplot.doc, so the syntax must obey the rules of that entity.
  625. If the text includes double-quotes or backslashes, these must be
  626. escaped by preceding each occurence with a backslash.
  627.  
  628. --------------
  629.  
  630. Rationale:
  631.  
  632. We may want to compile all drivers into term.c or one driver at a time
  633. this layout should support both
  634. TERM_PUBLIC will be static  if all modules are in term.c, or blank
  635. otherwise.
  636. Please make private support functions static if possible.
  637.  
  638.  
  639. We may include term.h, and therefore all these files, one or more times.
  640. If just once (all modules compiled into term.c) putting the four
  641. parts in this order should make it work.
  642.  
  643. we may compile the table entries into either an array or a linked list
  644. This organisation should support both
  645.  
  646. For separate compilation, we may write a program which
  647. defines TERM_REGISTER and #include term.h to find out which drivers are
  648. selected in term.h and thereby generate a makefile.
  649.  
  650.  
  651.  
  652. For a driver which depends on another (eg enhpost and pslatex on post)
  653. the driver can do something like
  654.  
  655.   #ifndef GOT_POST_PROTO
  656.   #define TERM_PROTO_ONLY
  657.   #include "post.trm"
  658.   #undef TERM_PROTO_ONLY
  659.   #endif
  660.  
  661. this is probably needed only in the TERM_TABLE section only, but may
  662. also be used in the body. The TERM_PROTO_ONLY means that we pick up
  663. only the protos from post.trm, even if current driver is being compiled
  664. with TERM_BODY or TERM_TABLE
  665.  
  666. If we do it the linked-list way, the arg to TERM_TABLE_START will be
  667. the name of the variable, so any valid, unique name is fine.
  668. The TERM_TABLE_START macro will do all the work of linking the entries
  669. together, probably using LAST_TERM
  670.  
  671. The inclusion of the TERM_HELP section (and removal of terminal documentation
  672. from the master gnuplot.doc file) means that the online help will include
  673. discussions of only those terminals available to the user.  For generation
  674. of the printed manual, all can be included.
  675.  
  676.  
  677. Please make as many things as possible static, but do still try to use unique
  678. names since all drivers may all be compiled into term.o
  679.  
  680. The bit in the PROTO section is basically what you would put into a .h
  681. file if we had them - everything that is needed by the TABLE_ENTRY
  682. should be defined in this part. In particular, dont forget all the maxes
  683. and character sizes and things for the table entry.
  684.  
  685. Dont forget to put TERM_PUBLIC in the defns of the fns as well as the
  686. prototypes. It will probably always expand to 'static' except for pcs.
  687.  
  688. =head1 Using Term::Gnuplot from C libraries
  689.  
  690. The interface of this module to B<gnuplot> version 3.7 is going via a
  691. translation layer in F<Gnuplot.h>.  This layer isolates low-level drawing
  692. routines from B<gnuplot> program.  (In doing this unsupported job it does
  693. some nasty thing, in particular F<Gnuplot.h> cannot be included in more than
  694. one C compilation unit.)
  695.  
  696. In fact F<Gnuplot.h> can be used by any C program or library which
  697. wants to use device-independent plotting routines of B<gnuplot>.
  698.  
  699. C library should use the same syntax of calls as C<Term::Gnuplot>, with
  700. the only difference that to call low-level _init() method one calls
  701. gptable_init(), to set terminal one calls C<termset(name)>,
  702. and to get a property of terminal (say C<xmax>) one uses C<termprop(xmax)>.
  703.  
  704. To set options one can setup the array C<token> and data C<c_token>,
  705. C<num_tokens>, C<input_line>, then call options().  Alternately, one
  706. can define C<SET_OPTIONS_FROM_STRING>, then a call
  707. C<set_options_from(string)> is avalable.  This call will set up all
  708. these variables and will call options().  However, the logic of the
  709. parsing of the string is very primitive.
  710.  
  711. B<NOTES.>
  712.  
  713. =over
  714.  
  715. =item *
  716.  
  717. To initialize the facilities of F<Gnuplot.h> from C one needs to call
  718. setup_gpshim().  This call can be made as many times as wanted, only the
  719. first call will do anything.
  720.  
  721. =item *
  722.  
  723. C<NULL> argument to term_set_output() means "reset back to stdout".
  724.  
  725. =item *
  726.  
  727. F<Gnuplot.h> expects that the macro/function C<croak(...)> is defined.
  728. This function should have the same syntax as printf(), and should not return.
  729. It is used as an error-reporting function.
  730.  
  731. =item *
  732.  
  733. One should define functions C<int StartOutput()>, C<int EndOutput()> and
  734. C<int OutLine(char *s)> which will be used by B<gnuplot> for error messages
  735. and for terminal listing.  Alternatively, one can define
  736. C<GNUPLOT_OUTLINE_STDOUT>, and B<gnuplot> will put these messages to C<stdout>.
  737.  
  738. =back
  739.  
  740. =head2 Runtime link with B<gnuplot> DLL
  741.  
  742. There are two different ways to use the plotting calls via F<Gnuplot.h>.
  743. One can either establish the link to B<gnuplot> library at I<compile/link
  744. time>, or to postpone this link to I<run time>.  By default F<Gnuplot.h>
  745. provides the first way, to switch to the second way define C<DYNAMIC_GNUPLOT>
  746. before including F<Gnuplot.h>.
  747.  
  748. To establish a link at I<run time>, one needs to load a dynamic library
  749. which is compiled from F<Gnuplot.h> - but without C<DYNAMIC_GNUPLOT> defined.
  750. Feed the result of the call to get_term_ftable() as an argument to
  751. set_term_ftable(), as in (with error-condition checking disabled):
  752.  
  753.   typedef struct t_ftable *(*g_ftable)(void);
  754.   g_ftable g_ftable_addr;
  755.   void *handle = dlopen("gnuterm.dll", mode);
  756.  
  757.   g_ftable_addr = (g_ftable) dlsym(handle, "get_term_ftable");
  758.   set_term_ftable((*g_ftable_addr)());
  759.  
  760. This means that any C library/program which uses the API provided by
  761. F<Gnuplot.h> does not I<need> to be even linked with B<gnuplot>,
  762. neither it I<requires> include files of B<gnuplot>.  If plotting
  763. is done in some situations only, one will not need the overhead of gnuplot
  764. plotting library (F<gnuterm.dll> in the above example) unless plotting
  765. is requested.
  766.  
  767. =head2 Runtime link of a Perl module with C<Term::Gnuplot>
  768.  
  769. To facilitate I<run time> link the C<Term::Gnuplot> Perl module provides
  770. a Perl subroutine get_term_ftable() which is a variant of C get_term_ftable()
  771. which returns an integer instead of an address.  One can feed this integer
  772. to a C function C<v_set_term_ftable(void*)>, which will establish a runtime
  773. link.  Thus the external XS library which wants to use Term::Gnuplot
  774. at runtime can put this in F<.xs> file:
  775.  
  776.   #define int_set_term_ftable(a) (v_set_term_ftable((void*)a))
  777.   extern  void v_set_term_ftable(void *a);
  778.   ...
  779.  
  780.   void
  781.   int_set_term_ftable(a)
  782.     IV a
  783.  
  784. include F<Gnuplot.h> (with C<DYNAMIC_GNUPLOT> defined) into any C file,
  785. and define this Perl subroutine
  786.  
  787.   sub link_gnuplot {
  788.     eval 'use Term::Gnuplot 0.56; 1' or die;
  789.     int_set_term_ftable(Term::Gnuplot::get_term_ftable());
  790.   }
  791.  
  792. in F<.pm> file.  
  793.  
  794. Now if it needs to do plotting, it calls link_gnuplot(), then does the
  795. plotting - without a need to interact with B<gnuplot> at compile/link
  796. time, and having the additional burden of low-level plotting code
  797. loaded in the executable/DLL.
  798.  
  799. After a call link_gnuplot(), all the F<Gnuplot.h> API calls made from
  800. the above C file will be directed through the vtables of the Perl
  801. module C<Term::Gnuplot>.
  802.  
  803. =head2 Using different Plotting libraries
  804.  
  805. In fact F<Gnuplot.h> knows almost nothing about B<gnuplot>, with a notable
  806. exceptions that there is an API call C<term = change_term(name)>, which
  807. returns a table of methods.  Thus I<in principle> F<Gnuplot.h> can establish
  808. a runtime-link with any plotting library which supports (or can be
  809. coerced to support) this call.  (The table is assumed to
  810. be compatible with gnuplot layout of C<struct termentry>.)
  811.  
  812. F<Gnuplot.h> uses also a handful of other gnuplot APIs (such as
  813. changing output file and several initialization shortcuts), but they
  814. should be easy to be ignored if an interface to another plotting library
  815. is needed.
  816.  
  817. =head1 Examples
  818.  
  819. =head2 High-level plotting
  820.  
  821.   sub ploth {
  822.     my ($xmin, $xmax, $sub) = (shift, shift, shift);
  823.     my $wpoints = scaled_xmax() - 1;
  824.     my $hpoints = scaled_ymax() - 1;
  825.     my $delta = ($xmax - $xmin)/$wpoints;
  826.     my (@ys, $y);
  827.     my ($ymin, $ymax) = (1e300, -1e300);
  828.     my $x = $xmin;
  829.     for my $i (0 .. $wpoints) {
  830.       $y = $sub->($x);
  831.       push @ys, $y;
  832.       $ymin = $y if $ymin > $y;
  833.       $ymax = $y if $ymax < $y;
  834.       $x += $delta;
  835.     }
  836.     my $deltay = ($ymax - $ymin)/$hpoints;
  837.     $_ = ($_ - $ymin)/$deltay + $yfix for @ys;
  838.  
  839.     term_start_plot();
  840.  
  841.     linetype -2;
  842.     move 0, $yfix;
  843.     vector 0, $hpoints + $yfix;
  844.     vector $wpoints, $hpoints + $yfix;
  845.     vector $wpoints, $yfix;
  846.     vector 0, $yfix;
  847.   
  848.     linetype -1;
  849.     if ($xmin < 0 && $xmax > 0) {
  850.       my $xzero = -$xmin/$delta;
  851.       move $xzero, $yfix;
  852.       vector $xzero, $hpoints + $yfix;
  853.     }
  854.     if ($ymin < 0 && $ymax > 0) {
  855.       my $yzero = -$ymin/$deltay + $yfix;
  856.       move 0, $yzero;
  857.       vector $wpoints, $yzero;
  858.     }
  859.  
  860.     linetype 0;
  861.     move 0, int($ys[0] + 0.5);
  862.     linetype 0;
  863.     for my $i (1 .. $wpoints) {
  864.       $x += $delta;
  865.       vector $i, int($ys[$i] + 0.5);
  866.     }
  867.     term_end_plot();
  868.   }
  869.  
  870. This function should be used as in
  871.  
  872.   ploth(-1,3, sub { sin( (shift)**6 ) })
  873.  
  874. $yfix should be set to 1 for C<gif> terminal to compensate for off-by-one bug.
  875.  
  876. =head2 Multifile plotting
  877.  
  878.   use strict;
  879.   use Term::Gnuplot qw(:ALL);
  880.  
  881.   my ($yfix, $ext) = (1, 'gif');
  882.   plot_outfile_set "manypl1.$ext";        # Need to set before plotterm()
  883.  
  884.   change_term "gif";
  885.   set_options "size", 300, ',', 200;
  886.   term_init();
  887.  
  888.   for my $k (1..6) {
  889.     plot_outfile_set "manypl$k.$ext" unless $k == 1;
  890.     ploth( -1, 3, sub { sin((shift)**$k) } );
  891.   }
  892.  
  893. Here we use the function ploth() from L<High-level plotting>, it should
  894. be in the same scope so that $yfix is visible there.  Note that C<gif>
  895. terminal requires $yfix to be 1 to circumvent a bug, and requires a literal
  896. C<','> in the set_options call.
  897.  
  898. If a terminal does not support direct setting of the size of the output device,
  899. one may set global rescaling factors by calling plotsizes_scale():
  900.  
  901.   plotsizes_scale(300/xmax(), 200/ymax());
  902.  
  903. =head1 BUGS and LIMITATIONS
  904.  
  905. The following C macros are set to reasonable values, no peeking is
  906. performed to get correct values, this may break Gnuplot on some systems:
  907.  
  908.   NO_ATEXIT HAVE_ON_EXIT PIPES HAVE_LIBC_H
  909.  
  910. No testing for
  911.  
  912.   X11
  913.  
  914. macros is performed either, however, this may only increase size of
  915. the executable (since C<X11> module is not making any direct C<X> calls,
  916. but calls an external program to serve the requests).
  917.  
  918. Apparently C<gif> terminal has off-by-one error: yrange is C<1..ymax()>.
  919. All the bugs of B<gnuplot> low-level plotting show in this module as well.
  920.  
  921. =head1 SEE ALSO
  922.  
  923. L<Term::GnuplotTerminals>.
  924.  
  925. =cut
  926.  
  927. require Exporter;
  928. require DynaLoader;
  929.  
  930. $VERSION = '0.5701';
  931.  
  932. @ISA = qw(Exporter DynaLoader);
  933. # Items to export into callers namespace by default. Note: do not export
  934. # names by default without a very good reason. Use EXPORT_OK instead.
  935. # Do not simply export all your public functions/methods/constants.
  936. @EXPORT = qw(
  937.     
  938. );
  939. @EXPORT_OK = qw(
  940.         change_term test_term init_terminal list_terms
  941.         term_init term_start_plot term_end_plot
  942.         term_start_multiplot term_end_multiplot plotsizes_scale
  943.         LEFT CENTRE RIGHT 
  944.         name description xmax ymax v_char h_char v_tic h_tic
  945.         scaled_xmax scaled_ymax
  946.         init scale graphics linetype move vector point text_angle
  947.         justify_text put_text arrow text set_options
  948.         set_font pointsize suspend resume is_binary cannot_multiplot 
  949.         can_multiplot fillbox linewidth plot_outfile_set
  950.         %description);
  951. %EXPORT_TAGS = ('JUSTIFY' => [qw(LEFT CENTRE RIGHT)],
  952.         'SETUP' => [qw(change_term test_term init_terminal
  953.                    term_init term_start_plot term_end_plot
  954.                    term_start_multiplot term_end_multiplot
  955.                    list_terms plot_outfile_set plotsizes_scale
  956.                    setcanvas)],
  957.         'FIELDS'  => [qw(name description xmax ymax
  958.                  is_binary cannot_multiplot can_multiplot
  959.                  v_char h_char v_tic h_tic
  960.                  scaled_xmax scaled_ymax)],
  961.         'METHODS' => [qw(init scale graphics linetype move vector 
  962.                  point text_angle justify_text put_text arrow
  963.                  text set_options
  964.                  set_font pointsize suspend resume 
  965.                  fillbox linewidth)],
  966.         );
  967. $EXPORT_TAGS{'ALL'} = [@{$EXPORT_TAGS{'JUSTIFY'}},
  968.                @{$EXPORT_TAGS{'SETUP'}},
  969.                @{$EXPORT_TAGS{'FIELDS'}},
  970.                @{$EXPORT_TAGS{'METHODS'}}, '%description'];
  971.  
  972. *pointsize = \&setpointsize;    # To simplify C macros
  973.  
  974. bootstrap Term::Gnuplot;
  975.  
  976. %description = _term_descrs();
  977.  
  978. # Preloaded methods go here.
  979.  
  980. sub LEFT {0}
  981. sub CENTRE {1}
  982. sub RIGHT {2}
  983.  
  984. sub name {(getdata())[0]}
  985. sub description {(getdata())[1]}
  986. sub xmax {(getdata())[2]}
  987. sub ymax {(getdata())[3]}
  988. sub v_char {(getdata())[4]}
  989. sub h_char {(getdata())[5]}
  990. sub v_tic {(getdata())[6]}
  991. sub h_tic {(getdata())[7]}
  992.  
  993. # Autoload methods go after __END__, and are processed by the autosplit program.
  994.  
  995. 1;
  996. __END__
  997.