Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - graph - plotport.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21348-d75f58f) Lines: 1020 1206 84.6 %
Date: 2017-11-20 06:21:05 Functions: 83 102 81.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                         PLOT ROUTINES                           */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : #include "rect.h"
      22             : 
      23             : static void (*pari_get_plot)(PARI_plot *);
      24             : 
      25             : /* no need for THREAD: OK to share this */
      26             : static hashtable *rgb_colors = NULL;
      27             : 
      28             : THREAD PariRect rectgraph[18]; /*NUMRECT*/
      29             : static THREAD long current_color[18]; /*NUMRECT*/
      30             : 
      31             : static long plotpoint_itype = 0, rectline_itype  = 0;
      32             : 
      33             : const long NUMRECT = 18;
      34             : const long RECUR_MAXDEPTH = 10;
      35             : const double RECUR_PREC = 0.001;
      36             : const long DEFAULT_COLOR = 1, AXIS_COLOR = 2;
      37             : 
      38             : enum {
      39             :   ROt_MV = 1, /* Move */
      40             :   ROt_PT,  /* Point */
      41             :   ROt_LN,  /* Line */
      42             :   ROt_BX,  /* Box */
      43             :   ROt_MP,  /* Multiple point */
      44             :   ROt_ML,  /* Multiple lines */
      45             :   ROt_ST,  /* String */
      46             :   ROt_PTT,  /* Point type change */
      47             :   ROt_LNT,  /* Line type change */
      48             :   ROt_PTS,  /* Point size change */
      49             :   ROt_NULL, /* To be the start of the chain */
      50             : };
      51             : 
      52             : INLINE long
      53       47450 : DTOL(double t) { return (long)(t + 0.5); }
      54             : 
      55             : static const long PS_WIDTH = 1120 - 60; /* 1400 - 60 for hi-res */
      56             : static const long PS_HEIGH = 800 - 40; /* 1120 - 60 for hi-res */
      57             : 
      58             : static void
      59          58 : _psdraw_scale(PARI_plot *T, GEN w, GEN x, GEN y)
      60             : {
      61          58 :   pari_sp av = avma;
      62          58 :   FILE *F = fopen(current_psfile, "a");
      63          58 :   if (!F) pari_err_FILE("postscript file",current_psfile);
      64          58 :   fputs(rect2ps(w,x,y,T), F);
      65          58 :   fclose(F); avma = av;
      66          58 : }
      67             : static void
      68           0 : _psdraw(PARI_plot *T, GEN w, GEN x, GEN y)
      69           0 : { (void)T; _psdraw_scale(NULL,w,x,y); }
      70             : static void
      71          58 : pari_get_psplot(PARI_plot *T, long scale)
      72             : {
      73          58 :   T->width = PS_WIDTH;
      74          58 :   T->height= PS_HEIGH;
      75          58 :   T->fheight= 15;
      76          58 :   T->fwidth = 6;
      77          58 :   T->hunit = 5;
      78          58 :   T->vunit = 5;
      79          58 :   T->draw = scale? &_psdraw: &_psdraw_scale;
      80          58 : }
      81             : 
      82             : /********************************************************************/
      83             : /**                                                                **/
      84             : /**                      RECTPLOT FUNCTIONS                        **/
      85             : /**                                                                **/
      86             : /********************************************************************/
      87             : static void
      88           0 : get_plot_null(PARI_plot *T)
      89           0 : { (void)T; pari_err(e_MISC,"high resolution graphics disabled"); }
      90             : 
      91             : void
      92        1538 : pari_init_graphics(void) { pari_get_plot = &get_plot_null; }
      93             : 
      94             : void
      95        1536 : pari_set_plot_engine(void (*plot)(PARI_plot *))
      96             : {
      97             :   long n;
      98        1536 :   pari_get_plot = plot;
      99       29184 :   for (n = 0; n < NUMRECT; n++)
     100             :   {
     101       27648 :     PariRect *e = &rectgraph[n];
     102       27648 :     e->head = e->tail = NULL;
     103       27648 :     e->sizex = e->sizey = 0;
     104       27648 :     current_color[n] = DEFAULT_COLOR;
     105             :   }
     106        1536 : }
     107             : 
     108             : void
     109        1536 : pari_kill_plot_engine(void)
     110             : {
     111             :   int i;
     112       29184 :   for (i=0; i<NUMRECT; i++)
     113             :   {
     114       27648 :     PariRect *e = &rectgraph[i];
     115       27648 :     if (RHead(e)) plotkill(i);
     116             :   }
     117        1536 :   if (rgb_colors)
     118             :   {
     119          11 :     pari_free((void*)rgb_colors->table);
     120          11 :     pari_free((void*)rgb_colors);
     121             :   }
     122        1536 :   if (GP_DATA->colormap) pari_free(GP_DATA->colormap);
     123        1536 :   if (GP_DATA->graphcolors) pari_free(GP_DATA->graphcolors);
     124        1536 : }
     125             : 
     126             : static PariRect *
     127       22128 : check_rect(long ne)
     128             : {
     129       22128 :   const char *f = "graphic function";
     130       22128 :   const long m = NUMRECT-1;
     131       22128 :   if (ne < 0) pari_err_DOMAIN(f, "rectwindow", "<", gen_0, stoi(ne));
     132       22120 :   if (ne > m) pari_err_DOMAIN(f, "rectwindow", ">", stoi(m), stoi(ne));
     133       22116 :   return &rectgraph[ne];
     134             : }
     135             : 
     136             : static PariRect *
     137       21949 : check_rect_init(long ne)
     138             : {
     139       21949 :   PariRect *e = check_rect(ne);
     140       21941 :   if (!RHead(e)) pari_err_TYPE("graphic function [use plotinit()]", stoi(ne));
     141       21941 :   return e;
     142             : }
     143             : static void
     144       27961 : Rchain(PariRect *e, RectObj *z)
     145             : {
     146       27961 :   if (!RHead(e)) RHead(e) = z; else RoNext(RTail(e)) = z;
     147       27961 :   RTail(e) = z;
     148       27961 :   RoNext(z) = NULL;
     149       27961 : }
     150             : 
     151             : static void
     152         171 : initrect_i(long ne, long x, long y)
     153             : {
     154             :   PariRect *e;
     155             :   RectObj *z;
     156             : 
     157         171 :   if (x <= 1) pari_err_DOMAIN("plotinit", "x", "<=", gen_1, stoi(x));
     158         171 :   if (y <= 1) pari_err_DOMAIN("plotinit", "y", "<=", gen_1, stoi(y));
     159         171 :   e = check_rect(ne); if (RHead(e)) plotkill(ne);
     160             : 
     161         167 :   z = (RectObj*) pari_malloc(sizeof(RectObj));
     162         167 :   RoType(z) = ROt_NULL;
     163         167 :   Rchain(e, z);
     164         167 :   RXsize(e) = x; RXcursor(e) = 0; RXscale(e) = 1; RXshift(e) = 0;
     165         167 :   RYsize(e) = y; RYcursor(e) = 0; RYscale(e) = 1; RYshift(e) = 0;
     166         167 : }
     167             : static long
     168         106 : initrect_get_arg(GEN x, long dft)
     169             : {
     170         106 :   if (!x) return dft;
     171          50 :   if (typ(x) != t_INT) pari_err_TYPE("plotinit",x);
     172          50 :   return itos(x);
     173             : }
     174             : void
     175          53 : plotinit(long ne, GEN x, GEN y, long flag)
     176             : {
     177          53 :   const long m = NUMRECT-3;
     178             :   long xi, yi;
     179             :   PARI_plot T;
     180             : 
     181          53 :   if (flag)
     182             :   {
     183           0 :     pari_get_plot(&T);
     184           0 :     xi = T.width -1; if (x) xi = DTOL(xi * gtodouble(x));
     185           0 :     yi = T.height-1; if (y) yi = DTOL(yi * gtodouble(y));
     186             :   }
     187             :   else
     188             :   {
     189          53 :     if (!x || !y) pari_get_plot(&T);
     190          53 :     xi = initrect_get_arg(x, T.width -1);
     191          53 :     yi = initrect_get_arg(y, T.height-1);
     192             :   }
     193          53 :   if (ne > m) pari_err_DOMAIN("plotinit", "rectwindow", ">", stoi(m), stoi(ne));
     194          49 :   initrect_i(ne, xi, yi);
     195          45 : }
     196             : 
     197             : GEN
     198          47 : plotcursor(long ne)
     199             : {
     200          47 :   PariRect *e = check_rect_init(ne);
     201          47 :   return mkvec2s((long)RXcursor(e), (long)RYcursor(e));
     202             : }
     203             : 
     204             : static void
     205          72 : plotscale0(long ne, double x1, double x2, double y1, double y2)
     206             : {
     207          72 :   PariRect *e = check_rect_init(ne);
     208             :   double x, y;
     209             : 
     210          72 :   x = RXshift(e) + RXscale(e) * RXcursor(e);
     211          72 :   y = RYshift(e) + RYscale(e) * RYcursor(e);
     212          72 :   RXscale(e) = RXsize(e)/(x2-x1); RXshift(e) = -x1*RXscale(e);
     213          72 :   RYscale(e) = RYsize(e)/(y1-y2); RYshift(e) = -y2*RYscale(e);
     214          72 :   RXcursor(e) = (x - RXshift(e)) / RXscale(e);
     215          72 :   RYcursor(e) = (y - RYshift(e)) / RYscale(e);
     216          72 : }
     217             : void
     218          11 : plotscale(long ne, GEN x1, GEN x2, GEN y1, GEN y2)
     219          11 : { plotscale0(ne, gtodouble(x1), gtodouble(x2), gtodouble(y1), gtodouble(y2)); }
     220             : 
     221             : static void
     222         501 : plotmove0(long ne, double x, double y, long relative)
     223             : {
     224         501 :   PariRect *e = check_rect_init(ne);
     225         493 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj1P));
     226             : 
     227         493 :   if (relative) { RXcursor(e) += x; RYcursor(e) += y; }
     228         482 :   else          { RXcursor(e) = x; RYcursor(e) = y; }
     229         493 :   RoType(z) = ROt_MV;
     230         493 :   RoMVx(z) = RXcursor(e) * RXscale(e) + RXshift(e);
     231         493 :   RoMVy(z) = RYcursor(e) * RYscale(e) + RYshift(e);
     232         493 :   Rchain(e, z);
     233         493 : }
     234             : void
     235          77 : plotmove(long ne, GEN x, GEN y)
     236          77 : { plotmove0(ne,gtodouble(x),gtodouble(y),0); }
     237             : void
     238          11 : plotrmove(long ne, GEN x, GEN y)
     239          11 : { plotmove0(ne,gtodouble(x),gtodouble(y),1); }
     240             : 
     241             : /* ROt_MV/ROt_PT */
     242             : static void
     243          22 : plotpoint0(long ne, double x, double y,long relative)
     244             : {
     245          22 :   PariRect *e = check_rect_init(ne);
     246          22 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj1P));
     247             : 
     248          22 :   if (relative) { RXcursor(e) += x; RYcursor(e) += y; }
     249          11 :   else          { RXcursor(e) = x; RYcursor(e) = y; }
     250          22 :   RoPTx(z) = RXcursor(e)*RXscale(e) + RXshift(e);
     251          22 :   RoPTy(z) = RYcursor(e)*RYscale(e) + RYshift(e);
     252          22 :   RoType(z) = ( DTOL(RoPTx(z)) < 0
     253          22 :                 || DTOL(RoPTy(z)) < 0 || DTOL(RoPTx(z)) > RXsize(e)
     254          22 :                 || DTOL(RoPTy(z)) > RYsize(e) ) ? ROt_MV : ROt_PT;
     255          22 :   Rchain(e, z);
     256          22 :   RoCol(z) = current_color[ne];
     257          22 : }
     258             : static void
     259          11 : plotpoint(long ne, GEN x, GEN y)
     260          11 : { plotpoint0(ne,gtodouble(x),gtodouble(y),0); }
     261             : void
     262          11 : plotrpoint(long ne, GEN x, GEN y)
     263          11 : { plotpoint0(ne,gtodouble(x),gtodouble(y),1); }
     264             : 
     265             : void
     266           8 : plotcolor(long ne, long c)
     267             : {
     268           8 :   long n = lg(GP_DATA->colormap)-2;
     269           8 :   check_rect(ne);
     270           8 :   if (c < 1) pari_err_DOMAIN("plotcolor", "color", "<", gen_1, stoi(c));
     271           8 :   if (c > n) pari_err_DOMAIN("plotcolor", "color", ">", stoi(n), stoi(c));
     272           8 :   current_color[ne] = c;
     273           8 : }
     274             : 
     275             : /* ROt_MV/ROt_LN */
     276             : static void
     277         130 : rectline0(long ne, double gx2, double gy2, long relative)
     278             : {
     279             :   double dx, dy, dxy, xmin, xmax, ymin, ymax, x1, y1, x2, y2;
     280         130 :   PariRect *e = check_rect_init(ne);
     281         130 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     282         130 :   const double c = 1 + 1e-10;
     283             : 
     284         130 :   x1 = RXcursor(e)*RXscale(e) + RXshift(e);
     285         130 :   y1 = RYcursor(e)*RYscale(e) + RYshift(e);
     286         130 :   if (relative)
     287          11 :     { RXcursor(e)+=gx2; RYcursor(e)+=gy2; }
     288             :   else
     289         119 :     { RXcursor(e)=gx2; RYcursor(e)=gy2; }
     290         130 :   x2 = RXcursor(e)*RXscale(e) + RXshift(e);
     291         130 :   y2 = RYcursor(e)*RYscale(e) + RYshift(e);
     292         130 :   xmin = maxdd(mindd(x1,x2),0); xmax = mindd(maxdd(x1,x2),RXsize(e));
     293         130 :   ymin = maxdd(mindd(y1,y2),0); ymax = mindd(maxdd(y1,y2),RYsize(e));
     294         130 :   dxy = x1*y2 - y1*x2; dx = x2-x1; dy = y2-y1;
     295         130 :   if (dy)
     296             :   {
     297          76 :     double a = (dxy + RYsize(e)*dx) / dy, b = dxy / dy;
     298          76 :     if (dx*dy < 0)
     299          11 :     { xmin=maxdd(xmin,a); xmax=mindd(xmax,b); }
     300             :     else
     301          65 :     { xmin=maxdd(xmin,b); xmax=mindd(xmax,a); }
     302             :   }
     303         130 :   if (dx)
     304             :   {
     305          76 :     double a = (RXsize(e)*dy - dxy) / dx, b = -dxy / dx;
     306          76 :     if (dx*dy < 0)
     307          11 :     { ymin=maxdd(ymin,a); ymax=mindd(ymax,b); }
     308             :     else
     309          65 :     { ymin=maxdd(ymin,b); ymax=mindd(ymax,a); }
     310             :   }
     311         130 :   RoLNx1(z) = xmin;
     312         130 :   RoLNx2(z) = xmax;
     313         130 :   if (dx*dy < 0) { RoLNy1(z) = ymax; RoLNy2(z) = ymin; }
     314         119 :   else         { RoLNy1(z) = ymin; RoLNy2(z) = ymax; }
     315         130 :   RoType(z) = (xmin>xmax*c || ymin>ymax*c) ? ROt_MV : ROt_LN;
     316         130 :   Rchain(e, z);
     317         130 :   RoCol(z) = current_color[ne];
     318         130 : }
     319             : void
     320          11 : plotline(long ne, GEN gx2, GEN gy2)
     321          11 : { rectline0(ne, gtodouble(gx2), gtodouble(gy2),0); }
     322             : void
     323          11 : plotrline(long ne, GEN gx2, GEN gy2)
     324          11 : { rectline0(ne, gtodouble(gx2), gtodouble(gy2),1); }
     325             : 
     326             : enum {
     327             :   TICKS_CLOCKW   = 1, /* Draw in clockwise direction */
     328             :   TICKS_ACLOCKW  = 2, /* Draw in anticlockwise direction */
     329             :   TICKS_ENDSTOO  = 4, /* Draw at endspoints if needed */
     330             :   TICKS_NODOUBLE = 8  /* Do not draw double-length ticks */
     331             : };
     332             : 
     333             : /* Given coordinates of ends of a line, and labels l1 l2 attached to the
     334             :  * ends, plot ticks where the label coordinate takes "round" values */
     335             : static void
     336         244 : rectticks(PARI_plot *WW, long ne, double dx1, double dy1, double dx2,
     337             :           double dy2, double l1, double l2, long flags)
     338             : {
     339             :   long ddx, ddy, dx, dy, dxy, dxy1, x1, y1, x2, y2, nticks, n, n1, dn;
     340             :   double minstep, maxstep, step, l_min, l_max, minl, maxl, dl, dtx, dty, x, y;
     341         244 :   const double mult[3] = { 2./1., 5./2., 10./5. };
     342         244 :   PariRect *e = check_rect_init(ne);
     343         244 :   int do_double = !(flags & TICKS_NODOUBLE);
     344             : 
     345         244 :   x1 = DTOL(dx1*RXscale(e) + RXshift(e));
     346         244 :   y1 = DTOL(dy1*RYscale(e) + RYshift(e));
     347         244 :   x2 = DTOL(dx2*RXscale(e) + RXshift(e));
     348         244 :   y2 = DTOL(dy2*RYscale(e) + RYshift(e));
     349         244 :   dx = x2 - x1; if (dx < 0) dx = -dx;
     350         244 :   dy = y2 - y1; if (dy < 0) dy = -dy;
     351         244 :   dxy1 = maxss(dx, dy);
     352         244 :   dx /= WW->hunit;
     353         244 :   dy /= WW->vunit;
     354         244 :   if (dx > 1000 || dy > 1000)
     355           0 :     dxy = 1000; /* avoid overflow */
     356             :   else
     357         244 :     dxy = usqrt(dx*dx + dy*dy);
     358         244 :   nticks = (long) ((dxy + 2.5)/4);
     359         244 :   if (!nticks) return;
     360             : 
     361             :   /* Find nticks (or less) "round" numbers between l1 and l2. For our purpose
     362             :    * round numbers have "last significant" decimal digit either
     363             :    *    - any;
     364             :    *    - even;
     365             :    *    - divisible by 5.
     366             :    * We need to choose which alternative is better. */
     367         244 :   if (l1 < l2)
     368         122 :     l_min = l1, l_max = l2;
     369             :   else
     370         122 :     l_min = l2, l_max = l1;
     371         244 :   minstep = (l_max - l_min)/(nticks + 1);
     372         244 :   maxstep = 2.5*(l_max - l_min);
     373         244 :   step = exp(log(10.) * floor(log10(minstep)));
     374         244 :   if (!(flags & TICKS_ENDSTOO)) {
     375         244 :     double d = 2*(l_max - l_min)/dxy1; /* Two pixels off */
     376         244 :     l_min += d;
     377         244 :     l_max -= d;
     378             :   }
     379         740 :   for (n = 0; ; n++)
     380             :   {
     381         740 :     if (step >= maxstep) return;
     382             : 
     383         740 :     if (step >= minstep) {
     384         244 :       minl = ceil(l_min/step);
     385         244 :       maxl = floor(l_max/step);
     386         244 :       if (minl <= maxl && maxl - minl + 1 <= nticks) {
     387         244 :         nticks = (long) (maxl - minl + 1);
     388         244 :         l_min = minl * step;
     389         244 :         l_max = maxl * step; break;
     390             :       }
     391             :     }
     392         496 :     step *= mult[ n % 3 ];
     393         496 :   }
     394             :   /* Where to position doubleticks. Variants:
     395             :    * small: each 5, double: each 10  ; n=2 mod 3
     396             :    * small: each 2, double: each 10  ; n=1 mod 3
     397             :    * small: each 1, double: each  5 */
     398         244 :   dn = (n % 3 == 2)? 2: 5;
     399         244 :   n1 = ((long)minl) % dn; /* unused if do_double = FALSE */
     400             : 
     401             :   /* now l_min and l_max keep min/max values of l with ticks, and nticks is
     402             :      the number of ticks to draw. */
     403         244 :   if (nticks == 1) ddx = ddy = 0; /* -Wall */
     404             :   else {
     405         244 :     dl = (l_max - l_min)/(nticks - 1);
     406         244 :     ddx = (dx2 - dx1) * dl / (l2 - l1);
     407         244 :     ddy = (dy2 - dy1) * dl / (l2 - l1);
     408             :   }
     409         244 :   x = dx1 + (dx2 - dx1) * (l_min - l1) / (l2 - l1);
     410         244 :   y = dy1 + (dy2 - dy1) * (l_min - l1) / (l2 - l1);
     411             :   /* assume hunit and vunit form a square. For clockwise ticks: */
     412         244 :   dtx = WW->hunit * dy/dxy * (y2 > y1 ? 1 : -1); /* y-coord runs down */
     413         244 :   dty = WW->vunit * dx/dxy * (x2 > x1 ? 1 : -1);
     414        6776 :   for (n = 0; n < nticks; n++, x += ddx, y += ddy) {
     415        6532 :     RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     416        6532 :     double lunit = WW->hunit > 1 ? 1.5 : 2;
     417        6532 :     double l = (do_double && (n + n1) % dn == 0) ? lunit: 1;
     418             :     double x1, x2, y1, y2;
     419        6532 :     x1 = x2 = x*RXscale(e) + RXshift(e);
     420        6532 :     y1 = y2 = y*RYscale(e) + RYshift(e);
     421        6532 :     if (flags & TICKS_CLOCKW)  { x1 += dtx*l; y1 -= dty*l; }
     422        6532 :     if (flags & TICKS_ACLOCKW) { x2 -= dtx*l; y2 += dty*l; }
     423        6532 :     RoLNx1(z) = x1; RoLNy1(z) = y1;
     424        6532 :     RoLNx2(z) = x2; RoLNy2(z) = y2;
     425        6532 :     RoType(z) = ROt_LN;
     426        6532 :     Rchain(e, z);
     427        6532 :     RoCol(z) = current_color[ne];
     428             :   }
     429             : }
     430             : 
     431             : static void
     432          83 : rectbox0(long ne, double gx2, double gy2, long relative)
     433             : {
     434             :   double xx, yy, x1, y1, x2, y2, xmin, ymin, xmax, ymax;
     435          83 :   PariRect *e = check_rect_init(ne);
     436          83 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     437             : 
     438          83 :   x1 = RXcursor(e)*RXscale(e) + RXshift(e);
     439          83 :   y1 = RYcursor(e)*RYscale(e) + RYshift(e);
     440          83 :   if (relative)
     441          11 :   { xx = RXcursor(e)+gx2; yy = RYcursor(e)+gy2; }
     442             :   else
     443          72 :   {  xx = gx2; yy = gy2; }
     444          83 :   x2 = xx*RXscale(e) + RXshift(e);
     445          83 :   y2 = yy*RYscale(e) + RYshift(e);
     446          83 :   xmin = maxdd(mindd(x1,x2),0); xmax = mindd(maxdd(x1,x2),RXsize(e));
     447          83 :   ymin = maxdd(mindd(y1,y2),0); ymax = mindd(maxdd(y1,y2),RYsize(e));
     448             : 
     449          83 :   RoType(z) = ROt_BX;
     450          83 :   RoBXx1(z) = xmin; RoBXy1(z) = ymin;
     451          83 :   RoBXx2(z) = xmax; RoBXy2(z) = ymax;
     452          83 :   Rchain(e, z);
     453          83 :   RoCol(z) = current_color[ne];
     454          83 : }
     455             : void
     456          11 : plotbox(long ne, GEN gx2, GEN gy2)
     457          11 : { rectbox0(ne, gtodouble(gx2), gtodouble(gy2), 0); }
     458             : void
     459          11 : plotrbox(long ne, GEN gx2, GEN gy2)
     460          11 : { rectbox0(ne, gtodouble(gx2), gtodouble(gy2), 1); }
     461             : 
     462             : static void
     463       28281 : freeobj(RectObj *z) {
     464       28281 :   switch(RoType(z)) {
     465             :     case ROt_MP: case ROt_ML:
     466       10095 :       pari_free(RoMPxs(z));
     467       10095 :       pari_free(RoMPys(z)); break;
     468             :     case ROt_ST:
     469         306 :       pari_free(RoSTs(z)); break;
     470             :   }
     471       28281 :   pari_free(z);
     472       28281 : }
     473             : 
     474             : void
     475         167 : plotkill(long ne)
     476             : {
     477             :   RectObj *z, *t;
     478         167 :   PariRect *e = check_rect_init(ne);
     479             : 
     480         167 :   current_color[ne]=DEFAULT_COLOR;
     481         167 :   z=RHead(e);
     482         167 :   RHead(e) = RTail(e) = NULL;
     483         167 :   RXsize(e) = RYsize(e) = 0;
     484         167 :   RXcursor(e) = RYcursor(e) = 0;
     485         167 :   RXscale(e) = RYscale(e) = 1;
     486         167 :   RXshift(e) = RYshift(e) = 0;
     487         167 :   while (z) { t = RoNext(z); freeobj(z); z = t; }
     488         167 : }
     489             : 
     490             : /* ROt_MP */
     491             : static void
     492          22 : plotpoints0(long ne, double *X, double *Y, long lx)
     493             : {
     494             :   double *px, *py;
     495          22 :   long i, cp=0;
     496          22 :   PariRect *e = check_rect_init(ne);
     497          22 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjMP));
     498             : 
     499          22 :   RoMPxs(z) = px = (double*) pari_malloc(lx*sizeof(double));
     500          22 :   RoMPys(z) = py = (double*) pari_malloc(lx*sizeof(double));
     501        2836 :   for (i=0; i<lx; i++)
     502             :   {
     503        2814 :     double x = RXscale(e)*X[i] + RXshift(e);
     504        2814 :     double y = RYscale(e)*Y[i] + RYshift(e);
     505        2814 :     if (x >= 0 && y >= 0 && x <= RXsize(e) && y <= RYsize(e))
     506             :     {
     507        2813 :       px[cp] = x; py[cp] = y; cp++;
     508             :     }
     509             :   }
     510          22 :   RoType(z) = ROt_MP;
     511          22 :   RoMPcnt(z) = cp;
     512          22 :   Rchain(e, z);
     513          22 :   RoCol(z) = current_color[ne];
     514          22 : }
     515             : void
     516          22 : plotpoints(long ne, GEN X, GEN Y)
     517             : {
     518          22 :   pari_sp av = avma;
     519             :   double *px, *py;
     520             :   long i, lx;
     521             : 
     522          22 :   if (!is_vec_t(typ(X)) || !is_vec_t(typ(Y))) { plotpoint(ne, X, Y); return; }
     523          11 :   lx = lg(X); if (lg(Y) != lx) pari_err_DIM("plotpoints");
     524          11 :   lx--; if (!lx) return;
     525             : 
     526          11 :   px = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); X++;
     527          11 :   py = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); Y++;
     528         121 :   for (i=0; i<lx; i++)
     529             :   {
     530         110 :     px[i] = gtodouble(gel(X,i));
     531         110 :     py[i] = gtodouble(gel(Y,i));
     532             :   }
     533          11 :   plotpoints0(ne,px,py,lx); avma = av;
     534             : }
     535             : 
     536             : /* ROt_ML */
     537             : static void
     538       10033 : rectlines0(long ne, double *x, double *y, long lx, long flag)
     539             : {
     540             :   long i,I;
     541             :   double *ptx,*pty;
     542       10033 :   PariRect *e = check_rect_init(ne);
     543       10033 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     544             : 
     545       10033 :   I = flag ? lx+1 : lx;
     546       10033 :   ptx = (double*) pari_malloc(I*sizeof(double));
     547       10033 :   pty = (double*) pari_malloc(I*sizeof(double));
     548      168116 :   for (i=0; i<lx; i++)
     549             :   {
     550      158083 :     ptx[i] = RXscale(e)*x[i] + RXshift(e);
     551      158083 :     pty[i] = RYscale(e)*y[i] + RYshift(e);
     552             :   }
     553       10033 :   if (flag)
     554             :   {
     555           0 :     ptx[i] = RXscale(e)*x[0] + RXshift(e);
     556           0 :     pty[i] = RYscale(e)*y[0] + RYshift(e);
     557             :   }
     558       10033 :   Rchain(e, z);
     559       10033 :   RoType(z) = ROt_ML;
     560       10033 :   RoMLcnt(z) = lx;
     561       10033 :   RoMLxs(z) = ptx;
     562       10033 :   RoMLys(z) = pty;
     563       10033 :   RoCol(z) = current_color[ne];
     564       10033 : }
     565             : void
     566          22 : plotlines(long ne, GEN X, GEN Y, long flag)
     567             : {
     568          22 :   pari_sp av = avma;
     569             :   double *x, *y;
     570             :   long i, lx;
     571             : 
     572          22 :   if (!is_vec_t(typ(X)) || !is_vec_t(typ(Y))) { plotline(ne, X, Y); return; }
     573          11 :   lx = lg(X); if (lg(Y) != lx) pari_err_DIM("plotlines");
     574          11 :   lx--; if (!lx) return;
     575             : 
     576          11 :   x = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); X++;
     577          11 :   y = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); Y++;
     578          66 :   for (i=0; i<lx; i++)
     579             :   {
     580          55 :     x[i] = gtodouble(gel(X,i));
     581          55 :     y[i] = gtodouble(gel(Y,i));
     582             :   }
     583          11 :   rectlines0(ne,x,y,lx,flag); avma = av;
     584             : }
     585             : 
     586             : /* ROt_ST */
     587             : void
     588         266 : plotstring(long ne, char *str, long dir)
     589             : {
     590         266 :   PariRect *e = check_rect_init(ne);
     591         266 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjST));
     592         266 :   long l = strlen(str);
     593         266 :   char *s = (char *) pari_malloc(l+1);
     594             : 
     595         266 :   memcpy(s,str,l+1);
     596         266 :   RoType(z) = ROt_ST;
     597         266 :   RoSTl(z) = l;
     598         266 :   RoSTs(z) = s;
     599         266 :   RoSTx(z) = RXscale(e)*RXcursor(e)+RXshift(e);
     600         266 :   RoSTy(z) = RYscale(e)*RYcursor(e)+RYshift(e);
     601         266 :   RoSTdir(z) = dir;
     602         266 :   Rchain(e, z);
     603         266 :   RoCol(z) = current_color[ne];
     604         266 : }
     605             : 
     606             : /* ROt_PTT */
     607             : void
     608          11 : plotpointtype(long ne, long type)
     609             : {
     610          11 :  if (ne == -1) plotpoint_itype = type;
     611             :  else {
     612          11 :    PariRect *e = check_rect_init(ne);
     613          11 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPN));
     614          11 :    RoType(z) = ROt_PTT;
     615          11 :    RoPTTpen(z) = type;
     616          11 :    Rchain(e, z);
     617             :  }
     618          11 : }
     619             : 
     620             : /* ROt_PTS. FIXME: this function is a noop, since no graphic driver implement
     621             :  * this code. ne == -1 (change globally). */
     622             : void
     623           0 : plotpointsize(long ne, GEN size)
     624             : {
     625           0 :  if (ne == -1) { /*do nothing*/ }
     626             :  else {
     627           0 :    PariRect *e = check_rect_init(ne);
     628           0 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPS));
     629           0 :    RoType(z) = ROt_PTS;
     630           0 :    RoPTSsize(z) = gtodouble(size);
     631           0 :    Rchain(e, z);
     632             :  }
     633           0 : }
     634             : 
     635             : void
     636       10202 : plotlinetype(long ne, long type)
     637             : {
     638       10202 :  if (ne == -1) rectline_itype = type;
     639             :  else {
     640       10202 :    PariRect *e = check_rect_init(ne);
     641       10202 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPN));
     642       10202 :    RoType(z) = ROt_LNT;
     643       10202 :    RoLNTpen(z) = type;
     644       10202 :    Rchain(e, z);
     645             :  }
     646       10202 : }
     647             : 
     648             : static double*
     649           0 : cpd(double* R, size_t t)
     650           0 : { void *o = pari_malloc(t * sizeof(double)); memcpy(o,R,t); return (double*)o; }
     651             : static void*
     652         460 : cp(void* R, size_t t)
     653         460 : { void *o = pari_malloc(t); memcpy(o,R,t); return o; }
     654             : void
     655          36 : plotcopy(long source, long dest, GEN xoff, GEN yoff, long flag)
     656             : {
     657          36 :   PariRect *s = check_rect_init(source), *d = check_rect_init(dest);
     658          36 :   RectObj *R, *tail = RTail(d);
     659             :   long i, x, y;
     660          36 :   if (flag & RECT_CP_RELATIVE) {
     661          32 :     double xd = gtodouble(xoff), yd = gtodouble(yoff);
     662             :     PARI_plot T;
     663          32 :     if (xd > 1) pari_err_DOMAIN("plotcopy","dx",">",gen_1,xoff);
     664          28 :     if (xd < 0) pari_err_DOMAIN("plotcopy","dx","<",gen_0,xoff);
     665          24 :     if (yd > 1) pari_err_DOMAIN("plotcopy","dy",">",gen_1,yoff);
     666          20 :     if (yd < 0) pari_err_DOMAIN("plotcopy","dy","<",gen_0,yoff);
     667          16 :     pari_get_plot(&T);
     668          16 :     x = DTOL(xd * (T.width-1));
     669          16 :     y = DTOL(yd * (T.height-1));
     670             :   } else {
     671           4 :     if (typ(xoff) != t_INT) pari_err_TYPE("plotcopy",xoff);
     672           4 :     if (typ(yoff) != t_INT) pari_err_TYPE("plotcopy",yoff);
     673           4 :     x = itos(xoff);
     674           4 :     y = itos(yoff);
     675             :   }
     676          20 :   switch (flag & ~RECT_CP_RELATIVE)
     677             :   {
     678           8 :     case RECT_CP_NW: break;
     679           4 :     case RECT_CP_SW: y = RYsize(d) - RYsize(s) - y; break;
     680           4 :     case RECT_CP_SE: y = RYsize(d) - RYsize(s) - y; /* fall through */
     681           8 :     case RECT_CP_NE: x = RXsize(d) - RXsize(s) - x; break;
     682             :   }
     683         360 :   for (R = RHead(s); R; R = RoNext(R))
     684             :   {
     685             :     RectObj *o;
     686         340 :     switch(RoType(R))
     687             :     {
     688             :       case ROt_PT:
     689          40 :         o = (RectObj*)cp(R, sizeof(RectObj1P));
     690          40 :         RoPTx(o) += x; RoPTy(o) += y;
     691          40 :         break;
     692             :       case ROt_LN: case ROt_BX:
     693          80 :         o = (RectObj*)cp(R, sizeof(RectObj2P));
     694          80 :         RoLNx1(o) += x; RoLNy1(o) += y;
     695          80 :         RoLNx2(o) += x; RoLNy2(o) += y;
     696          80 :         break;
     697             :       case ROt_MP: case ROt_ML:
     698          40 :         o = (RectObj*)cp(R, sizeof(RectObjMP));
     699          40 :         RoMPxs(o) = (double*)cp(RoMPxs(R), sizeof(double)*RoMPcnt(o));
     700          40 :         RoMPys(o) = (double*)cp(RoMPys(R), sizeof(double)*RoMPcnt(o));
     701          40 :         for (i=0; i<RoMPcnt(o); i++) { RoMPxs(o)[i] += x; RoMPys(o)[i] += y; }
     702          40 :         break;
     703             :       case ROt_ST:
     704          40 :         o = (RectObj*)cp(R, sizeof(RectObjST));
     705          40 :         RoSTs(o) = (char*)cp(RoSTs(R),RoSTl(R)+1);
     706          40 :         RoSTx(o) += x; RoSTy(o) += y;
     707          40 :         break;
     708             :       default: /* ROt_PTT, ROt_LNT, ROt_PTS */
     709         140 :         o = (RectObj*)cp(R, sizeof(RectObjPN));
     710         140 :         break;
     711             :     }
     712         340 :     RoNext(tail) = o; tail = o;
     713             :   }
     714          20 :   RoNext(tail) = NULL; RTail(d) = tail;
     715          20 : }
     716             : 
     717             : enum {CLIPLINE_NONEMPTY = 1, CLIPLINE_CLIP_1 = 2, CLIPLINE_CLIP_2 = 4};
     718             : /* A simpler way is to clip by 4 half-planes */
     719             : static int
     720         120 : clipline(double xmin, double xmax, double ymin, double ymax,
     721             :          double *x1p, double *y1p, double *x2p, double *y2p)
     722             : {
     723         120 :   int xy_exch = 0, rc = CLIPLINE_NONEMPTY;
     724             :   double t, sl;
     725             :   double xi, xmn, xmx;
     726             :   double yi, ymn, ymx;
     727             :   int x1_is_ymn, x1_is_xmn;
     728         120 :   double x1 = *x1p, x2 = *x2p, y1 = *y1p, y2 = *y2p;
     729             : 
     730         120 :   if ((x1 < xmin &&  x2 < xmin) || (x1 > xmax && x2 > xmax))
     731          40 :     return 0;
     732          80 :   if (fabs(x1 - x2) < fabs(y1 - y2)) { /* Exchange x and y */
     733          24 :     xy_exch = 1;
     734          24 :     dswap(xmin, ymin); dswap(x1, y1);
     735          24 :     dswap(xmax, ymax); dswap(x2, y2);
     736             :   }
     737             : 
     738             :   /* Build y as a function of x */
     739          80 :   xi = x1;
     740          80 :   yi = y1;
     741          80 :   sl = x1==x2? 0: (y2 - yi)/(x2 - xi);
     742             : 
     743          80 :   if (x1 > x2) {
     744          24 :     x1_is_xmn = 0;
     745          24 :     xmn = x2;
     746          24 :     xmx = x1;
     747             :   } else {
     748          56 :     x1_is_xmn = 1;
     749          56 :     xmn = x1;
     750          56 :     xmx = x2;
     751             :   }
     752             : 
     753          80 :   if (xmn < xmin) {
     754           8 :     xmn = xmin;
     755           8 :     rc |= x1_is_xmn? CLIPLINE_CLIP_1: CLIPLINE_CLIP_2;
     756             :   }
     757          80 :   if (xmx > xmax) {
     758          12 :     xmx = xmax;
     759          12 :     rc |= x1_is_xmn? CLIPLINE_CLIP_2: CLIPLINE_CLIP_1;
     760             :   }
     761          80 :   if (xmn > xmx) return 0;
     762             : 
     763          80 :   ymn = yi + (xmn - xi)*sl;
     764          80 :   ymx = yi + (xmx - xi)*sl;
     765             : 
     766          80 :   if (sl < 0) t = ymn, ymn = ymx, ymx = t;
     767          80 :   if (ymn > ymax || ymx < ymin) return 0;
     768             : 
     769          80 :   if (rc & CLIPLINE_CLIP_1) x1 = x1_is_xmn? xmn: xmx;
     770          80 :   if (rc & CLIPLINE_CLIP_2) x2 = x1_is_xmn? xmx: xmn;
     771             : 
     772             :   /* Now we know there is an intersection, need to move x1 and x2 */
     773          80 :   x1_is_ymn = ((sl >= 0) == (x1 < x2));
     774          80 :   if (ymn < ymin) {
     775           4 :     double x = (ymin - yi)/sl + xi; /* slope != 0  ! */
     776           4 :     if (x1_is_ymn) x1 = x, rc |= CLIPLINE_CLIP_1;
     777           0 :     else           x2 = x, rc |= CLIPLINE_CLIP_2;
     778             :   }
     779          80 :   if (ymx > ymax) {
     780           4 :     double x = (ymax - yi)/sl + xi; /* slope != 0  ! */
     781           4 :     if (x1_is_ymn) x2 = x, rc |= CLIPLINE_CLIP_2;
     782           0 :     else           x1 = x, rc |= CLIPLINE_CLIP_1;
     783             :   }
     784          80 :   if (rc & CLIPLINE_CLIP_1) y1 = yi + (x1 - xi)*sl;
     785          80 :   if (rc & CLIPLINE_CLIP_2) y2 = yi + (x2 - xi)*sl;
     786          80 :   if (xy_exch) /* Exchange x and y */
     787          24 :     *x1p = y1, *x2p = y2, *y1p = x1, *y2p = x2;
     788             :   else
     789          56 :     *x1p = x1, *x2p = x2, *y1p = y1, *y2p = y2;
     790          80 :   return rc;
     791             : }
     792             : 
     793             : void
     794          20 : plotclip(long rect)
     795             : {
     796          20 :   PariRect *s = check_rect_init(rect);
     797          20 :   RectObj *next, *R = RHead(s), **prevp = &RHead(s);
     798          20 :   double xmin = 0, xmax = RXsize(s);
     799          20 :   double ymin = 0, ymax = RYsize(s);
     800             : 
     801         380 :   for (; R; R = next) {
     802         360 :     int did_clip = 0;
     803             : #define REMOVE() { *prevp = next; freeobj(R); break; }
     804             : #define NEXT() { prevp = &RoNext(R); break; }
     805             : 
     806         360 :     next = RoNext(R);
     807         360 :     switch(RoType(R)) {
     808             :       case ROt_PT:
     809          40 :         if ( DTOL(RoPTx(R)) < xmin || DTOL(RoPTx(R)) > xmax
     810          24 :           || DTOL(RoPTy(R)) < ymin || DTOL(RoPTy(R)) > ymax) REMOVE();
     811          24 :         NEXT();
     812             :       case ROt_BX:
     813          40 :         if (RoLNx1(R) < xmin) RoLNx1(R) = xmin, did_clip = 1;
     814          40 :         if (RoLNx2(R) < xmin) RoLNx2(R) = xmin, did_clip = 1;
     815          40 :         if (RoLNy1(R) < ymin) RoLNy1(R) = ymin, did_clip = 1;
     816          40 :         if (RoLNy2(R) < ymin) RoLNy2(R) = ymin, did_clip = 1;
     817          40 :         if (RoLNx1(R) > xmax) RoLNx1(R) = xmax, did_clip = 1;
     818          40 :         if (RoLNx2(R) > xmax) RoLNx2(R) = xmax, did_clip = 1;
     819          40 :         if (RoLNy1(R) > ymax) RoLNy1(R) = ymax, did_clip = 1;
     820          40 :         if (RoLNy2(R) > ymax) RoLNy2(R) = ymax, did_clip = 1;
     821             :         /* Remove zero-size clipped boxes */
     822          40 :         if (did_clip && RoLNx1(R) == RoLNx2(R)
     823           8 :                      && RoLNy1(R) == RoLNy2(R)) REMOVE();
     824          36 :         NEXT();
     825             :       case ROt_LN:
     826          40 :         if (!clipline(xmin, xmax, ymin, ymax,
     827             :                       &RoLNx1(R), &RoLNy1(R),
     828           8 :                       &RoLNx2(R), &RoLNy2(R))) REMOVE();
     829          32 :         NEXT();
     830             :       case ROt_MP: {
     831          20 :         int c = RoMPcnt(R), f = 0, t = 0;
     832             : 
     833         240 :         while (f < c) {
     834         200 :           if ( DTOL(RoMPxs(R)[f]) >= xmin && DTOL(RoMPxs(R)[f]) <= xmax
     835         120 :             && DTOL(RoMPys(R)[f]) >= ymin && DTOL(RoMPys(R)[f]) <= ymax) {
     836         120 :             if (t != f) {
     837           0 :               RoMPxs(R)[t] = RoMPxs(R)[f];
     838           0 :               RoMPys(R)[t] = RoMPys(R)[f];
     839             :             }
     840         120 :             t++;
     841             :           }
     842         200 :           f++;
     843             :         }
     844          20 :         if (t == 0) REMOVE();
     845          12 :         RoMPcnt(R) = t;
     846          12 :         NEXT();
     847             :       }
     848             :       case ROt_ML: {
     849             :         /* Hard case. Break a multiline into several pieces
     850             :          * if some part is clipped. */
     851          20 :         int c = RoMPcnt(R) - 1;
     852          20 :         int f = 0, t = 0, had_lines = 0, had_hole = 0, rc;
     853          20 :         double ox = RoMLxs(R)[0], oy = RoMLys(R)[0], oxn, oyn;
     854             : 
     855         120 :         while (f < c) {
     856             :         /* Endpoint of this segment is startpoint of next one: need to
     857             :          * preserve it if it is clipped. */
     858          80 :           oxn = RoMLxs(R)[f+1];
     859          80 :           oyn = RoMLys(R)[f+1];
     860         160 :           rc = clipline(xmin, xmax, ymin, ymax,
     861             :                   &ox, &oy, /* &RoMLxs(R)[f], &RoMLys(R)[f], */
     862         160 :                   &RoMLxs(R)[f+1], &RoMLys(R)[f+1]);
     863          80 :           RoMLxs(R)[f] = ox; ox = oxn;
     864          80 :           RoMLys(R)[f] = oy; oy = oyn;
     865          80 :           if (!rc) {
     866          32 :             if (had_lines) had_hole = 1;
     867          32 :             f++; continue;
     868             :           }
     869             : 
     870          48 :           if (!had_lines || (!(rc & CLIPLINE_CLIP_1) && !had_hole) ) {
     871             :             /* Continuous */
     872          48 :             had_lines = 1;
     873          48 :             if (t != f) {
     874           0 :               if (t == 0) {
     875           0 :                 RoMPxs(R)[t] = RoMPxs(R)[f];
     876           0 :                 RoMPys(R)[t] = RoMPys(R)[f];
     877             :               }
     878           0 :               RoMPxs(R)[t+1] = RoMPxs(R)[f+1];
     879           0 :               RoMPys(R)[t+1] = RoMPys(R)[f+1];
     880             :             }
     881          48 :             t++;
     882          48 :             f++;
     883          48 :             if (rc & CLIPLINE_CLIP_2) had_hole = 1, RoMLcnt(R) = t+1;
     884          48 :             continue;
     885             :           }
     886             :           /* Is not continuous, automatically R is not pari_free()ed.  */
     887           0 :           t++;
     888           0 :           RoMLcnt(R) = t;
     889           0 :           if (rc & CLIPLINE_CLIP_2) { /* Needs separate entry */
     890           0 :             RectObj *n = (RectObj*) pari_malloc(sizeof(RectObj2P));
     891           0 :             RoType(n) = ROt_LN;
     892           0 :             RoCol(n) = RoCol(R);
     893           0 :             RoLNx1(n) = RoMLxs(R)[f];   RoLNy1(n) = RoMLys(R)[f];
     894           0 :             RoLNx2(n) = RoMLxs(R)[f+1]; RoLNy2(n) = RoMLys(R)[f+1];
     895           0 :             RoNext(n) = next;
     896           0 :             RoNext(R) = n;
     897             :             /* Restore the unclipped value: */
     898           0 :             RoMLxs(R)[f+1] = oxn; RoMLys(R)[f+1] = oyn;
     899           0 :             f++;
     900           0 :             prevp = &RoNext(n);
     901             :           }
     902           0 :           if (f + 1 < c) { /* Are other lines */
     903           0 :             RectObj *n = (RectObj*) pari_malloc(sizeof(RectObjMP));
     904           0 :             RoType(n) = ROt_ML;
     905           0 :             RoCol(n) = RoCol(R);
     906           0 :             RoMLcnt(n) = c - f;
     907           0 :             RoMLxs(n) = cpd(RoMPxs(R) + f, c-f);
     908           0 :             RoMLys(n) = cpd(RoMPys(R) + f, c-f);
     909           0 :             RoMPxs(n)[0] = oxn;
     910           0 :             RoMPys(n)[0] = oyn;
     911           0 :             RoNext(n) = next;
     912           0 :             RoNext(R) = n;
     913           0 :             next = n;
     914             :           }
     915           0 :           break;
     916             :         }
     917          20 :         if (t == 0) REMOVE();
     918          12 :         NEXT();
     919             :       }
     920             :     }
     921             : #undef REMOVE
     922             : #undef NEXT
     923             :   }
     924          20 : }
     925             : 
     926             : /********************************************************************/
     927             : /**                                                                **/
     928             : /**                        HI-RES PLOT                             **/
     929             : /**                                                                **/
     930             : /********************************************************************/
     931             : static void
     932      137976 : Appendx(dblPointList *f, dblPointList *l,double x)
     933             : {
     934      137976 :   (l->d)[l->nb++]=x;
     935      137976 :   if (x < f->xsml) f->xsml = x;
     936      137976 :   if (x > f->xbig) f->xbig = x;
     937      137976 : }
     938             : 
     939             : static void
     940      164976 : Appendy(dblPointList *f, dblPointList *l,double y)
     941             : {
     942      164976 :   (l->d)[l->nb++]=y;
     943      164976 :   if (y < f->ysml) f->ysml = y;
     944      164976 :   if (y > f->ybig) f->ybig = y;
     945      164976 : }
     946             : 
     947             : static void
     948      138192 : get_xy(long cplx, GEN t, double *x, double *y)
     949             : {
     950      138192 :   if (cplx)
     951             :   {
     952           0 :     if (typ(t) == t_VEC)
     953             :     {
     954           0 :       if (lg(t) != 2) pari_err_DIM("get_xy");
     955           0 :       t = gel(t,1);
     956             :     }
     957           0 :     *x = gtodouble( real_i(t) );
     958           0 :     *y = gtodouble( imag_i(t) );
     959             :   }
     960             :   else
     961             :   {
     962      138192 :     if (typ(t) != t_VEC || lg(t) != 3) pari_err_DIM("get_xy");
     963      138192 :     *x = gtodouble( gel(t,1) );
     964      138192 :     *y = gtodouble( gel(t,2) );
     965             :   }
     966      138192 : }
     967             : /* t a t_VEC (possibly a scalar if cplx), get next (x,y) coordinate starting
     968             :  * at index *i [update i] */
     969             : static void
     970        8056 : get_xy_from_vec(long cplx, GEN t, long *i, double *x, double *y)
     971             : {
     972        8056 :   if (cplx)
     973             :   {
     974             :     GEN z;
     975           0 :     if (typ(t) == t_VEC) z = gel(t,(*i)++); else { z = t; (*i)++; }
     976           0 :     *x = gtodouble( real_i(z) );
     977           0 :     *y = gtodouble( imag_i(z) );
     978             :   }
     979             :   else
     980             :   {
     981        8056 :     *x = gtodouble( gel(t, (*i)++) );
     982        8056 :     *y = gtodouble( gel(t, (*i)++) );
     983             :   }
     984        8056 : }
     985             : /* X,Y t_VEC, get next (x,y) coordinate starting at index i
     986             :  * Y ignored if (cplx) */
     987             : static void
     988        6712 : get_xy_from_vec2(long cplx, GEN X, GEN Y, long i, double *x, double *y)
     989             : {
     990        6712 :   if (cplx)
     991             :   {
     992           0 :     GEN z = gel(X,i);
     993           0 :     *x = gtodouble( real_i(z) );
     994           0 :     *y = gtodouble( imag_i(z) );
     995             :   }
     996             :   else
     997             :   {
     998        6712 :     *x = gtodouble( gel(X, i) );
     999        6712 :     *y = gtodouble( gel(Y, i) );
    1000             :   }
    1001        6712 : }
    1002             : 
    1003             : /* Convert data from GEN to double before we call plotrecthrawin. */
    1004             : static dblPointList*
    1005          19 : gtodblList(GEN data, long flags)
    1006             : {
    1007             :   dblPointList *l;
    1008             :   double xsml, xbig, ysml, ybig;
    1009          19 :   long nl=lg(data)-1, lx1, i, j;
    1010          19 :   const long param = (flags & (PLOT_PARAMETRIC|PLOT_COMPLEX));
    1011          19 :   const long cplx = (flags & PLOT_COMPLEX);
    1012             : 
    1013          19 :   if (! is_vec_t(typ(data))) pari_err_TYPE("gtodblList",data);
    1014          19 :   if (!nl) return NULL;
    1015          19 :   lx1 = lg(gel(data,1));
    1016          19 :   if (!param && lx1 == 1) return NULL;
    1017             : 
    1018          19 :   if (nl == 1 && !cplx) pari_err_DIM("gtodblList");
    1019             :   /* Allocate memory, then convert coord. to double */
    1020          19 :   l = (dblPointList*)pari_malloc((cplx? 2*nl: nl)*sizeof(dblPointList));
    1021          38 :   for (i=0; i<nl; i += (cplx? 1: 2))
    1022             :   {
    1023          19 :     dblPointList *LX = l + i, *LY = l + (i+1);
    1024          19 :     GEN x = gel(data,i+1), y;
    1025          19 :     long lx = lg(x);
    1026          19 :     if (!is_vec_t(typ(x))) pari_err_TYPE("gtodblList",x);
    1027          19 :     if (cplx) y = NULL;
    1028             :     else
    1029             :     {
    1030          19 :       y = gel(data,i+2);
    1031          19 :       if (!is_vec_t(typ(y))) pari_err_TYPE("gtodblList",y);
    1032          19 :       if (lg(y) != lx || (!param && lx != lx1)) pari_err_DIM("gtodblList");
    1033             :     }
    1034             : 
    1035          19 :     lx--;
    1036          19 :     LX->d = (double*)pari_malloc(lx*sizeof(double));
    1037          19 :     LY->d = (double*)pari_malloc(lx*sizeof(double));
    1038        6731 :     for (j=1; j<=lx; j++)
    1039             :     {
    1040             :       double xx, yy;
    1041        6712 :       get_xy_from_vec2(cplx, x,y, j, &xx,&yy);
    1042        6712 :       LX->d[j-1] = xx;
    1043        6712 :       LY->d[j-1] = yy;
    1044             :     }
    1045          19 :     LX->nb = LY->nb = lx;
    1046             :   }
    1047             : 
    1048             :   /* Now compute extremas */
    1049          19 :   if (param)
    1050             :   {
    1051          19 :     l[0].nb = cplx? nl: nl/2;
    1052          19 :     for (i=0; i < l[0].nb; i+=2)
    1053          19 :       if (l[i+1].nb) break;
    1054          19 :     if (i >= l[0].nb) { pari_free(l); return NULL; }
    1055          19 :     xsml = xbig = l[i  ].d[0];
    1056          19 :     ysml = ybig = l[i+1].d[0];
    1057          38 :     for (; i < l[0].nb; i+=2)
    1058             :     {
    1059          19 :       dblPointList *LX = l + i, *LY = l + (i+1);
    1060        6731 :       for (j=0; j < LY->nb; j++)
    1061             :       {
    1062        6712 :         double x = LX->d[j], y = LY->d[j];
    1063        6712 :         if (x < xsml) xsml = x; else if (x > xbig) xbig = x;
    1064        6712 :         if (y < ysml) ysml = y; else if (y > ybig) ybig = y;
    1065             :       }
    1066             :     }
    1067             :   }
    1068             :   else
    1069             :   {
    1070           0 :     l[0].nb = nl-1;
    1071           0 :     xsml = xbig = l[0].d[0];
    1072           0 :     ysml = ybig = l[1].d[0];
    1073           0 :     for (j=0; j < l[1].nb; j++)
    1074             :     {
    1075           0 :       double x = l[0].d[j];
    1076           0 :       if (x < xsml) xsml = x; else if (x > xbig) xbig = x;
    1077             :     }
    1078           0 :     for (i=1; i <= l[0].nb; i++)
    1079           0 :       for (j=0; j < l[i].nb; j++)
    1080             :       {
    1081           0 :         double y = l[i].d[j];
    1082           0 :         if (y < ysml) ysml = y; else if (y > ybig) ybig = y;
    1083             :       }
    1084             :   }
    1085          19 :   l[0].xsml = xsml; l[0].xbig = xbig;
    1086          19 :   l[0].ysml = ysml; l[0].ybig = ybig; return l;
    1087             : }
    1088             : 
    1089             : /* (x+y)/2 */
    1090             : static GEN
    1091      208940 : rmiddle(GEN x, GEN y) { GEN z = addrr(x,y); shiftr_inplace(z,-1); return z; }
    1092             : 
    1093             : static void
    1094       91404 : single_recursion(void *E, GEN(*eval)(void*,GEN), dblPointList *pl,
    1095             :                  GEN xleft,double yleft, GEN xright,double yright,long depth)
    1096             : {
    1097             :   GEN xx;
    1098       91404 :   pari_sp av = avma;
    1099       91404 :   double yy, dy=pl[0].ybig - pl[0].ysml;
    1100             : 
    1101       91404 :   if (depth==RECUR_MAXDEPTH) return;
    1102             : 
    1103       83124 :   xx = rmiddle(xleft,xright);
    1104       83124 :   yy = gtodouble(eval(E,xx));
    1105             : 
    1106       83124 :   if (dy && fabs(yleft+yright-2*yy)< dy*RECUR_PREC) return;
    1107       43708 :   single_recursion(E,eval, pl,xleft,yleft, xx,yy, depth+1);
    1108             : 
    1109       43708 :   Appendx(&pl[0],&pl[0],rtodbl(xx));
    1110       43708 :   Appendy(&pl[0],&pl[1],yy);
    1111             : 
    1112       43708 :   single_recursion(E,eval, pl,xx,yy, xright,yright, depth+1);
    1113       43708 :   avma = av;
    1114             : }
    1115             : 
    1116             : static void
    1117      138104 : param_recursion(void *E,GEN(*eval)(void*,GEN), long cplx, dblPointList *pl,
    1118             :   GEN tleft,double xleft, double yleft,
    1119             :   GEN tright,double xright,double yright, long depth)
    1120             : {
    1121             :   GEN tt, p1;
    1122      138104 :   pari_sp av=avma;
    1123      138104 :   double xx, dy=pl[0].ybig - pl[0].ysml;
    1124      138104 :   double yy, dx=pl[0].xbig - pl[0].xsml;
    1125             : 
    1126      210348 :   if (depth==RECUR_MAXDEPTH) return;
    1127             : 
    1128      125816 :   tt = rmiddle(tleft,tright);
    1129      125816 :   p1 = eval(E,tt);
    1130      125816 :   get_xy(cplx, p1, &xx,&yy);
    1131             : 
    1132      125816 :   if (dx && dy && fabs(xleft+xright-2*xx) < dx*RECUR_PREC
    1133       62848 :                && fabs(yleft+yright-2*yy) < dy*RECUR_PREC) return;
    1134       65860 :   param_recursion(E,eval, cplx, pl, tleft,xleft,yleft, tt,xx,yy, depth+1);
    1135             : 
    1136       65860 :   Appendx(&pl[0],&pl[0],xx);
    1137       65860 :   Appendy(&pl[0],&pl[1],yy);
    1138             : 
    1139       65860 :   param_recursion(E,eval,cplx, pl, tt,xx,yy, tright,xright,yright, depth+1);
    1140       65860 :   avma = av;
    1141             : }
    1142             : 
    1143             : /* Graph 'code' for parameter values in [a,b], using 'N' sample points
    1144             :  * (0 = use a default value); code is either a t_CLOSURE or a t_POL or a
    1145             :  * t_VEC of two t_POLs from rectsplines. Returns a dblPointList of
    1146             :  * (absolute) coordinates. */
    1147             : static dblPointList *
    1148       10034 : plotrecthin(void *E, GEN(*eval)(void*, GEN), GEN a, GEN b, ulong flags,
    1149             :             long N, long prec)
    1150             : {
    1151       10034 :   const double INF = 1.0/0.0;
    1152       10034 :   const long param = flags & (PLOT_PARAMETRIC|PLOT_COMPLEX);
    1153       10034 :   const long recur = flags & PLOT_RECURSIVE;
    1154       10034 :   const long cplx = flags & PLOT_COMPLEX;
    1155             :   GEN t,dx,x;
    1156             :   dblPointList *pl;
    1157       10034 :   long tx, i, j, sig, nc, nl, ncoords, nbpoints, non_vec = 0;
    1158       10034 :   pari_sp av = avma;
    1159             : 
    1160       10034 :   sig = gcmp(b,a); if (!sig) return NULL;
    1161       10034 :   if (sig < 0) swap(a, b);
    1162       10034 :   if (N == 1)
    1163           4 :     pari_err_DOMAIN("ploth", "#points", "<", gen_2, stoi(N));
    1164       10030 :   if (!N) N = recur? 8: (param? 1500: 1000);
    1165             :   /* compute F(a) to determine nc = #curves; nl = #coord. lists */
    1166       10030 :   x = gtofp(a, prec);
    1167       10030 :   t = eval(E, x); tx = typ(t);
    1168       10030 :   if (param)
    1169             :   {
    1170        6015 :     if (cplx) nc = nl = (tx == t_VEC)? lg(t)-1: 1;
    1171             :     else
    1172             :     {
    1173        6015 :       if (tx != t_VEC)
    1174           4 :         pari_err_TYPE("ploth [not a t_VEC with PLOT_PARAMETRIC]", t);
    1175        6011 :       nl = lg(t)-1;
    1176        6011 :       nc = nl/2;
    1177        6011 :       if (odd(nl))
    1178           0 :         pari_err_TYPE("ploth [parametric plot with odd # of components]",t);
    1179             :     }
    1180             :   }
    1181             :   else
    1182             :   {
    1183        4015 :     if (!is_matvec_t(tx)) { nl = 2; non_vec = 1; }
    1184             :     else
    1185             :     {
    1186           8 :       if (tx != t_VEC) pari_err_TYPE("ploth [not a t_VEC]",t);
    1187           8 :       nl = lg(t);
    1188             :     }
    1189        4015 :     nc = nl-1;
    1190             :   }
    1191       10026 :   if (!nc) { avma = av; return NULL; }
    1192       10026 :   if (recur && nc > 1)
    1193           4 :     pari_err_TYPE("ploth [multi-curves cannot be plot recursively]",t);
    1194             : 
    1195       10022 :   ncoords = cplx? 2*nl: nl;
    1196       10022 :   nbpoints = recur? N << RECUR_MAXDEPTH: N;
    1197       10022 :   pl=(dblPointList*) pari_malloc(ncoords*sizeof(dblPointList));
    1198             :   /* set [xy]sml,[xy]big to default values */
    1199       10022 :   if (param)
    1200             :   {
    1201        6011 :     pl[0].xsml = INF;
    1202        6011 :     pl[0].xbig =-INF;
    1203             :   } else {
    1204        4011 :     pl[0].xsml = gtodouble(a);
    1205        4011 :     pl[0].xbig = gtodouble(b);
    1206             :   }
    1207       10022 :   pl[0].ysml = INF;
    1208       10022 :   pl[0].ybig =-INF;
    1209       30070 :   for (i = 0; i < ncoords; i++)
    1210             :   {
    1211       20048 :     pl[i].d = (double*)pari_malloc((nbpoints+1)*sizeof(double));
    1212       20048 :     pl[i].nb=0;
    1213             :   }
    1214       10022 :   dx = divru(gtofp(gsub(b,a),prec), N-1);
    1215       10022 :   if (recur) /* recursive plot */
    1216             :   {
    1217        9980 :     double yleft, yright = 0;
    1218        9980 :     if (param)
    1219             :     {
    1220        5992 :       GEN tleft = cgetr(prec), tright = cgetr(prec);
    1221        5992 :       double xleft, xright = 0;
    1222        5992 :       pari_sp av2 = avma;
    1223        5992 :       affgr(a,tleft);
    1224        5992 :       t = eval(E, tleft);
    1225        5992 :       get_xy(cplx,t, &xleft,&yleft);
    1226       12376 :       for (i=0; i<N-1; i++, avma = av2)
    1227             :       {
    1228        6384 :         if (i) { affrr(tright,tleft); xleft = xright; yleft = yright; }
    1229        6384 :         addrrz(tleft,dx,tright);
    1230        6384 :         t = eval(E, tright);
    1231        6384 :         get_xy(cplx,t, &xright,&yright);
    1232        6384 :         Appendx(&pl[0],&pl[0],xleft);
    1233        6384 :         Appendy(&pl[0],&pl[1],yleft);
    1234        6384 :         param_recursion(E,eval, cplx, pl, tleft,xleft,yleft, tright,xright,yright, 0);
    1235             :       }
    1236        5992 :       Appendx(&pl[0],&pl[0],xright);
    1237        5992 :       Appendy(&pl[0],&pl[1],yright);
    1238             :     }
    1239             :     else /* single curve */
    1240             :     {
    1241        3988 :       GEN xleft = cgetr(prec), xright = cgetr(prec);
    1242        3988 :       pari_sp av2 = avma;
    1243        3988 :       affgr(a,xleft);
    1244        3988 :       yleft = gtodouble(eval(E,xleft));
    1245        7976 :       for (i=0; i<N-1; i++, avma = av2)
    1246             :       {
    1247        3988 :         addrrz(xleft,dx,xright);
    1248        3988 :         yright = gtodouble(eval(E,xright));
    1249             : 
    1250        3988 :         Appendx(&pl[0],&pl[0],rtodbl(xleft));
    1251        3988 :         Appendy(&pl[0],&pl[1],yleft);
    1252             : 
    1253        3988 :         single_recursion(E,eval, pl,xleft,yleft,xright,yright,0);
    1254        3988 :         affrr(xright,xleft); yleft = yright;
    1255             :       }
    1256        3988 :       Appendx(&pl[0],&pl[0],rtodbl(xright));
    1257        3988 :       Appendy(&pl[0],&pl[1],yright);
    1258             :     }
    1259             :   }
    1260             :   else /* non-recursive plot */
    1261             :   {
    1262          42 :     pari_sp av2 = avma;
    1263          42 :     if (param)
    1264             :     {
    1265        8075 :       for (i=0; i<N; i++, affrr(addrr(x,dx), x), avma = av2)
    1266             :       {
    1267             :         long k, nt;
    1268        8060 :         t = eval(E,x);
    1269        8060 :         if (typ(t) != t_VEC)
    1270             :         {
    1271           4 :           if (cplx) nt = 1;
    1272           4 :           else nt = 0; /* trigger error */
    1273             :         }
    1274             :         else
    1275        8056 :           nt = lg(t)-1;
    1276        8060 :         if (nt != nl) pari_err_DIM("plotrecth");
    1277        8056 :         k = 0; j = 1;
    1278       24168 :         while (j <= nl)
    1279             :         {
    1280             :           double xx, yy;
    1281        8056 :           get_xy_from_vec(cplx, t, &j, &xx, &yy);
    1282        8056 :           Appendx(&pl[0], &pl[k++], xx);
    1283        8056 :           Appendy(&pl[0], &pl[k++], yy);
    1284             :         }
    1285             :       }
    1286             :     }
    1287          23 :     else if (non_vec)
    1288       19019 :       for (i=0; i<N; i++, affrr(addrr(x,dx), x), avma = av2)
    1289             :       {
    1290       19000 :         t = eval(E,x);
    1291       19000 :         pl[0].d[i] = gtodouble(x);
    1292       19000 :         Appendy(&pl[0],&pl[1], gtodouble(t));
    1293             :       }
    1294             :     else /* vector of non-parametric curves */
    1295        4004 :       for (i=0; i<N; i++, affrr(addrr(x,dx), x), avma = av2)
    1296             :       {
    1297        4000 :         t = eval(E,x);
    1298        4000 :         if (typ(t) != t_VEC || lg(t) != nl) pari_err_DIM("plotrecth");
    1299        4000 :         pl[0].d[i] = gtodouble(x);
    1300        4000 :         for (j=1; j<nl; j++) Appendy(&pl[0],&pl[j], gtodouble(gel(t,j)));
    1301             :       }
    1302             :   }
    1303       10018 :   pl[0].nb = nc; avma = av; return pl;
    1304             : }
    1305             : 
    1306             : static GEN
    1307      234176 : spline_eval(void* E, GEN x) { return gsubst((GEN)E,0,x); }
    1308             : 
    1309             : /* Uses highlevel plotting functions to implement splines as
    1310             :    a low-level plotting function. */
    1311             : static void
    1312           8 : rectsplines(long ne, double *x, double *y, long lx, long flag)
    1313             : {
    1314             :   long i, j;
    1315           8 :   pari_sp av0 = avma;
    1316           8 :   GEN X = pol_x(0), xa = cgetg(lx+1, t_VEC), ya = cgetg(lx+1, t_VEC);
    1317             :   GEN tas, pol3;
    1318           8 :   long param = flag & PLOT_PARAMETRIC;
    1319           8 :   const long fl = param | PLOT_RECURSIVE | PLOT_NO_RESCALE | PLOT_NO_FRAME
    1320             :                         | PLOT_NO_AXE_Y | PLOT_NO_AXE_X;
    1321             : 
    1322           8 :   if (lx < 4) pari_err(e_MISC, "Too few points (%ld) for spline plot", lx);
    1323       10008 :   for (i = 1; i <= lx; i++) {
    1324       10000 :     gel(xa,i) = dbltor(x[i-1]);
    1325       10000 :     gel(ya,i) = dbltor(y[i-1]);
    1326             :   }
    1327           8 :   if (param) {
    1328           4 :     tas = new_chunk(4);
    1329           4 :     for (j = 1; j <= 4; j++) gel(tas,j-1) = utoipos(j);
    1330           4 :     pol3 = cgetg(3, t_VEC);
    1331             :   }
    1332             :   else
    1333           4 :     tas = pol3 = NULL; /* gcc -Wall */
    1334        9984 :   for (i = 0; i <= lx - 4; i++) {
    1335        9976 :     pari_sp av = avma;
    1336             : 
    1337        9976 :     xa++; ya++;
    1338        9976 :     if (param) {
    1339        5988 :       gel(pol3,1) = polint_i(tas, xa, X, 4, NULL);
    1340        5988 :       gel(pol3,2) = polint_i(tas, ya, X, 4, NULL);
    1341             :     } else {
    1342        3988 :       pol3 = polint_i(xa, ya, X, 4, NULL);
    1343        3988 :       tas = xa;
    1344             :     }
    1345             :     /* Start with 3 points */
    1346       19952 :     plotrecth((void*)pol3, &spline_eval, ne,
    1347             :                i== 0 ? gel(tas,0) : gel(tas,1),
    1348        9976 :                i==lx-4 ? gel(tas,3) : gel(tas,2),
    1349             :                fl, 2, DEFAULTPREC);
    1350        9976 :     avma = av;
    1351             :   }
    1352           8 :   avma = av0;
    1353           8 : }
    1354             : 
    1355             : static void
    1356         244 : put_label(long ne, long x, long y, double d, long dir)
    1357             : {
    1358             :   char c[16];
    1359         244 :   sprintf(c,"%.5g", d);
    1360         244 :   plotmove0(ne,(double)x,(double)y,0);
    1361         244 :   plotstring(ne, c, dir);
    1362         244 : }
    1363             : static void
    1364       20074 : set_range(double m, double M, double *sml, double *big)
    1365             : {
    1366       20074 :   if (M - m < 1.e-9)
    1367             :   {
    1368           0 :     double d = fabs(m)/10; if (!d) d = 0.1;
    1369           0 :     M += d; m -= d;
    1370             :   }
    1371       20074 :   *sml = m; *big = M;
    1372       20074 : }
    1373             : /* Plot a dblPointList. Complete with axes, bounding box, etc.
    1374             :  *
    1375             :  * data is an array of structs. Its meaning depends on flags :
    1376             :  *
    1377             :  * + data[0] contains global extremas, the number of curves to plot
    1378             :  *   (data[0].nb) and a list of doubles (first set of x-coordinates).
    1379             :  *
    1380             :  * + data[i].nb (i>0) contains the number of points in the list
    1381             :  *   data[i].d (hopefully, data[2i].nb=data[2i+1].nb when i>0...)
    1382             :  *
    1383             :  * + If flags contain PLOT_PARAMETRIC, the array length should be
    1384             :  *   even, and successive pairs (data[2i].d, data[2i+1].d) represent
    1385             :  *   curves to plot.
    1386             :  *
    1387             :  * + If there is no such flag, the first element is an array with
    1388             :  *   x-coordinates and the following ones contain y-coordinates.
    1389             :  * If grect >= 0, output to this rectwindow. Otherwise draw immediately to
    1390             :  * screen (grect=-1) or to PS file (grect=-2), using two drawing rectangles:
    1391             :  * one for labels, another for graphs.*/
    1392             : static GEN
    1393       10037 : plotrecthrawin(PARI_plot *W, long grect, dblPointList *data, long flags)
    1394             : {
    1395       10037 :   const long param = flags & (PLOT_PARAMETRIC|PLOT_COMPLEX);
    1396       10037 :   const long max_graphcolors = lg(GP_DATA->graphcolors)-1;
    1397       10037 :   const pari_sp av = avma;
    1398             :   dblPointList x, y;
    1399             :   double xsml, xbig, ysml, ybig;
    1400             :   long ltype, i, nc, w[3], wx[3], wy[3];
    1401             : 
    1402       10037 :   if (!data) return cgetg(1,t_VEC);
    1403       10037 :   x = data[0]; nc = x.nb;
    1404       10037 :   set_range(x.xsml, x.xbig, &xsml, &xbig);
    1405       10037 :   set_range(x.ysml, x.ybig, &ysml, &ybig);
    1406       10037 :   if (W)
    1407             :   { /* actual output; else output to rectwindow: no labels */
    1408          61 :     const long srect = NUMRECT-2;
    1409             :     long lm, rm, tm, bm;
    1410             :     /* left/right/top/bottom margin */
    1411          61 :     lm = W->fwidth*10;
    1412          61 :     rm = W->hunit-1;
    1413          61 :     tm = W->vunit-1;
    1414          61 :     bm = W->vunit+W->fheight-1;
    1415          61 :     w[0] = wx[0] = wy[0] = evaltyp(t_VECSMALL) | evallg(3);
    1416          61 :     w[1] = srect; wx[1] = 0;  wy[1] = 0;
    1417          61 :     w[2] = grect; wx[2] = lm; wy[2] = tm;
    1418             :    /* Window (width x height) is given in pixels, correct pixels are 0..n-1,
    1419             :     * whereas rect functions work with windows whose pixel range is [0,n] */
    1420          61 :     initrect_i(srect, W->width - 1, W->height - 1);
    1421          61 :     current_color[srect] = DEFAULT_COLOR;
    1422          61 :     initrect_i(grect, W->width - (lm+rm) - 1, W->height - (tm+bm) - 1);
    1423             :     /* draw labels on srect */
    1424          61 :     put_label(srect, lm, 0, ybig, RoSTdirRIGHT|RoSTdirHGAP|RoSTdirTOP);
    1425          61 :     put_label(srect, lm, W->height-bm, ysml, RoSTdirRIGHT|RoSTdirHGAP|RoSTdirVGAP);
    1426          61 :     put_label(srect, lm, W->height - bm, xsml, RoSTdirLEFT|RoSTdirTOP);
    1427          61 :     put_label(srect, W->width-rm-1, W->height-bm, xbig, RoSTdirRIGHT|RoSTdirTOP);
    1428             :   }
    1429       10037 :   if (!(flags & PLOT_NO_RESCALE))
    1430          61 :     plotscale0(grect, xsml, xbig, ysml, ybig);
    1431             : 
    1432       10037 :   if (!(flags & PLOT_NO_FRAME))
    1433             :   {
    1434          61 :     int do_double = (flags & PLOT_NODOUBLETICK) ? TICKS_NODOUBLE : 0;
    1435             :     PARI_plot T, *pl;
    1436          61 :     if (W) pl = W; else { pl = &T; pari_get_plot(pl); }
    1437             : 
    1438          61 :     plotlinetype(grect, -2); /* Frame. */
    1439          61 :     current_color[grect] = DEFAULT_COLOR;
    1440          61 :     plotmove0(grect,xsml,ysml,0);
    1441          61 :     rectbox0(grect,xbig,ybig,0);
    1442          61 :     if (!(flags & PLOT_NO_TICK_X)) {
    1443          61 :       rectticks(pl, grect, xsml, ysml, xbig, ysml, xsml, xbig,
    1444          61 :         TICKS_CLOCKW | do_double);
    1445          61 :       rectticks(pl, grect, xbig, ybig, xsml, ybig, xbig, xsml,
    1446          61 :         TICKS_CLOCKW | do_double);
    1447             :     }
    1448          61 :     if (!(flags & PLOT_NO_TICK_Y)) {
    1449          61 :       rectticks(pl, grect, xbig, ysml, xbig, ybig, ysml, ybig,
    1450          61 :         TICKS_CLOCKW | do_double);
    1451          61 :       rectticks(pl, grect, xsml, ybig, xsml, ysml, ybig, ysml,
    1452          61 :         TICKS_CLOCKW | do_double);
    1453             :     }
    1454             :   }
    1455             : 
    1456       10037 :   if (!(flags & PLOT_NO_AXE_Y) && (xsml<=0 && xbig >=0))
    1457             :   {
    1458          54 :     plotlinetype(grect, -1); /* Axes. */
    1459          54 :     current_color[grect] = AXIS_COLOR;
    1460          54 :     plotmove0(grect,0.0,ysml,0);
    1461          54 :     rectline0(grect,0.0,ybig,0);
    1462             :   }
    1463             : 
    1464       10037 :   if (!(flags & PLOT_NO_AXE_X) && (ysml<=0 && ybig >=0))
    1465             :   {
    1466          54 :     plotlinetype(grect, -1); /* Axes. */
    1467          54 :     current_color[grect] = AXIS_COLOR;
    1468          54 :     plotmove0(grect,xsml,0.0,0);
    1469          54 :     rectline0(grect,xbig,0.0,0);
    1470             :   }
    1471             : 
    1472       10037 :   if (param) {
    1473        6026 :     i = 0;
    1474        6026 :     flags |= PLOT_PARAMETRIC;
    1475        6026 :     flags &= (~PLOT_COMPLEX); /* turn COMPLEX to PARAMETRIC*/
    1476        4011 :   } else i = 1;
    1477       20078 :   for (ltype = 0; ltype < nc; ltype++)
    1478             :   {
    1479       10041 :     current_color[grect] = GP_DATA->graphcolors[1+(ltype%max_graphcolors)];
    1480       10041 :     if (param) x = data[i++];
    1481             : 
    1482       10041 :     y = data[i++];
    1483       10041 :     if (flags & (PLOT_POINTS_LINES|PLOT_POINTS)) {
    1484          11 :       plotlinetype(grect, plotpoint_itype + ltype); /* Graphs */
    1485          11 :       plotpointtype(grect,plotpoint_itype + ltype); /* Graphs */
    1486          11 :       plotpoints0(grect, x.d, y.d, y.nb);
    1487          11 :       if (!(flags & PLOT_POINTS_LINES)) continue;
    1488             :     }
    1489             : 
    1490       10030 :     if (flags & PLOT_SPLINES) {
    1491             :       /* rectsplines will call us back with ltype == 0 */
    1492           8 :       int old = rectline_itype;
    1493           8 :       rectline_itype = rectline_itype + ltype;
    1494           8 :       rectsplines(grect, x.d, y.d, y.nb, flags);
    1495           8 :       rectline_itype = old;
    1496             :     } else {
    1497       10022 :       plotlinetype(grect, rectline_itype + ltype); /* Graphs */
    1498       10022 :       rectlines0(grect, x.d, y.d, y.nb, 0);
    1499             :     }
    1500             :   }
    1501       10037 :   for (i--; i>=0; i--) pari_free(data[i].d);
    1502       10037 :   pari_free(data);
    1503             : 
    1504       10037 :   if (W)
    1505             :   {
    1506          61 :     W->draw(W, w,wx,wy);
    1507          61 :     plotkill(w[1]);
    1508          61 :     plotkill(w[2]);
    1509             :   }
    1510       10037 :   avma = av;
    1511       10037 :   retmkvec4(dbltor(xsml), dbltor(xbig), dbltor(ysml), dbltor(ybig));
    1512             : }
    1513             : 
    1514             : /*************************************************************************/
    1515             : /*                                                                       */
    1516             : /*                          HI-RES FUNCTIONS                             */
    1517             : /*                                                                       */
    1518             : /*************************************************************************/
    1519             : /* If T != NULL, draw using the attached graphic (using rectwindow ne as a temp)
    1520             :  * Else write to rectwindow 'ne'.
    1521             :  * Graph y=f(x), x=a..b, use n points */
    1522             : static GEN
    1523       10034 : plotrecth_i(void *E, GEN(*f)(void*,GEN), PARI_plot *T, long ne, GEN a,GEN b,
    1524             :             ulong flags,long n, long prec)
    1525             : {
    1526       10034 :   dblPointList *pl = plotrecthin(E,f, a,b, flags, n, prec);
    1527       10018 :   return plotrecthrawin(T, ne, pl, flags);
    1528             : }
    1529             : GEN
    1530        9976 : plotrecth(void *E, GEN(*f)(void*,GEN), long ne, GEN a,GEN b,
    1531             :           ulong flags, long n, long prec)
    1532        9976 : { return plotrecth_i(E,f, NULL, ne, a,b, flags, n, prec); }
    1533             : GEN
    1534           0 : plotrecth0(long ne, GEN a,GEN b,GEN code,ulong flags,long n, long prec)
    1535           0 : { EXPR_WRAP(code, plotrecth(EXPR_ARG, ne, a,b, flags, n, prec)); }
    1536             : GEN
    1537          40 : ploth(void *E, GEN(*f)(void*,GEN), GEN a, GEN b,long flags, long n, long prec)
    1538             : {
    1539          40 :   PARI_plot T; pari_get_plot(&T);
    1540          40 :   return plotrecth_i(E,f, &T, NUMRECT-1, a,b, flags,n, prec);
    1541             : }
    1542             : GEN
    1543          40 : ploth0(GEN a, GEN b, GEN code, long flags,long n, long prec)
    1544          40 : { EXPR_WRAP(code, ploth(EXPR_ARG, a,b,flags,n, prec)); }
    1545             : GEN
    1546          18 : psploth(void *E, GEN(*f)(void*,GEN), GEN a,GEN b, long flags, long n, long prec)
    1547             : {
    1548          18 :   PARI_plot T; pari_get_psplot(&T,0);
    1549          18 :   return plotrecth_i(E,f, &T, NUMRECT-1, a,b, flags,n, prec);
    1550             : }
    1551             : GEN
    1552          18 : psploth0(GEN a, GEN b, GEN code, long flags, long n, long prec)
    1553          18 : { EXPR_WRAP(code, psploth(EXPR_ARG, a, b, flags, n, prec)); }
    1554             : 
    1555             : /* Draw list of points */
    1556             : static GEN
    1557          19 : plotrecthraw_i(PARI_plot *T, long ne, GEN data, long flags)
    1558             : {
    1559          19 :   dblPointList *pl = gtodblList(data,flags);
    1560          19 :   return plotrecthrawin(T, ne, pl, flags);
    1561             : }
    1562             : static GEN
    1563          19 : plothraw_i(PARI_plot *T, GEN X, GEN Y, long flag)
    1564             : {
    1565          19 :   pari_sp av = avma;
    1566          19 :   switch (flag) {
    1567          11 :     case 0: flag = PLOT_PARAMETRIC|PLOT_POINTS; break;
    1568           8 :     case 1: flag = PLOT_PARAMETRIC; break;
    1569           0 :     default: flag |= PLOT_PARAMETRIC; break;
    1570             :   }
    1571          19 :   return gerepileupto(av, plotrecthraw_i(T, NUMRECT-1, mkvec2(X,Y), flag));
    1572             : }
    1573             : GEN
    1574           8 : plothraw(GEN X, GEN Y, long flags)
    1575           8 : { PARI_plot T; pari_get_plot(&T); return plothraw_i(&T, X, Y, flags); }
    1576             : GEN
    1577          11 : psplothraw(GEN X, GEN Y, long flags)
    1578          11 : { PARI_plot T; pari_get_psplot(&T,0); return plothraw_i(&T, X, Y, flags); }
    1579             : GEN
    1580           0 : plotrecthraw(long ne, GEN data, long flags)
    1581           0 : { return plotrecthraw_i(NULL, ne, data, flags); }
    1582             : 
    1583             : GEN
    1584           4 : plothsizes(long flag)
    1585             : {
    1586           4 :   GEN vect = cgetg(1+6,t_VEC);
    1587             :   PARI_plot T;
    1588             : 
    1589           4 :   pari_get_plot(&T);
    1590           4 :   gel(vect,1) = stoi(T.width);
    1591           4 :   gel(vect,2) = stoi(T.height);
    1592           4 :   if (flag) {
    1593           0 :     gel(vect,3) = dbltor(T.hunit*1.0/T.width);
    1594           0 :     gel(vect,4) = dbltor(T.vunit*1.0/T.height);
    1595           0 :     gel(vect,5) = dbltor(T.fwidth*1.0/T.width);
    1596           0 :     gel(vect,6) = dbltor(T.fheight*1.0/T.height);
    1597             :   } else {
    1598           4 :     gel(vect,3) = stoi(T.hunit);
    1599           4 :     gel(vect,4) = stoi(T.vunit);
    1600           4 :     gel(vect,5) = stoi(T.fwidth);
    1601           4 :     gel(vect,6) = stoi(T.fheight);
    1602             :   }
    1603           4 :   return vect;
    1604             : }
    1605             : 
    1606             : /*************************************************************************/
    1607             : /*                                                                       */
    1608             : /*                         POSTSCRIPT OUTPUT                             */
    1609             : /*                                                                       */
    1610             : /*************************************************************************/
    1611             : /* if flag is set, rescale wrt T */
    1612             : static void
    1613          57 : gendraw(PARI_plot *T, GEN wxy, long flag)
    1614             : {
    1615             :   long i, n;
    1616             :   GEN W, X, Y;
    1617             : 
    1618          57 :   if (typ(wxy) != t_VEC) pari_err_TYPE("plotdraw",wxy);
    1619         114 :   n = lg(wxy)-1; if (!n) return;
    1620          57 :   if (n%3) pari_err_DIM("plotdraw");
    1621          57 :   n = n/3;
    1622             :   /* malloc mandatory in case T->draw forks then pari_close() */
    1623          57 :   W = cgetalloc(t_VECSMALL, n+1); /* win number */
    1624          57 :   X = cgetalloc(t_VECSMALL, n+1);
    1625          57 :   Y = cgetalloc(t_VECSMALL, n+1); /* (x,y)-offset */
    1626         114 :   for (i=1; i<=n; i++)
    1627             :   {
    1628          57 :     GEN w = gel(wxy,3*i-2), x = gel(wxy,3*i-1), y = gel(wxy,3*i);
    1629          57 :     if (typ(w)!=t_INT) pari_err_TYPE("plotdraw",w);
    1630          57 :     if (flag) {
    1631           0 :       X[i] = DTOL(gtodouble(x)*(T->width - 1));
    1632           0 :       Y[i] = DTOL(gtodouble(y)*(T->height - 1));
    1633             :     } else {
    1634          57 :       X[i] = gtos(x);
    1635          57 :       Y[i] = gtos(y);
    1636             :     }
    1637          57 :     W[i] = itos(w); check_rect_init(W[i]);
    1638             :   }
    1639          57 :   T->draw(T,W,X,Y);
    1640          57 :   pari_free(W);
    1641          57 :   pari_free(X);
    1642          57 :   pari_free(Y);
    1643             : 
    1644             : }
    1645             : void
    1646          29 : psdraw(GEN wxy, long flag)
    1647          29 : { PARI_plot T; pari_get_psplot(&T,flag); gendraw(&T, wxy, flag); }
    1648             : void
    1649          28 : plotdraw(GEN wxy, long flag)
    1650          28 : { PARI_plot T; pari_get_plot(&T); gendraw(&T, wxy, flag); }
    1651             : 
    1652             : /* may be called after pari_close(): don't use the PARI stack */
    1653             : void
    1654          58 : gen_draw(struct plot_eng *eng, GEN w, GEN x, GEN y, double xs, double ys)
    1655             : {
    1656          58 :   void *data = eng->data;
    1657          58 :   long i, j, lw = lg(w);
    1658          58 :   long hgapsize = eng->pl->hunit, fheight = eng->pl->fheight;
    1659          58 :   long vgapsize = eng->pl->vunit,  fwidth = eng->pl->fwidth;
    1660          58 :   long numcolors = lg(GP_DATA->colormap)-1;
    1661         145 :   for(i = 1; i < lw; i++)
    1662             :   {
    1663          87 :     PariRect *e = &rectgraph[w[i]];
    1664             :     RectObj *R;
    1665          87 :     long x0 = x[i], y0 = y[i];
    1666        4812 :     for (R = RHead(e); R; R = RoNext(R))
    1667             :     {
    1668        4725 :       long col = minss(numcolors, RoCol(R));
    1669        4725 :       switch(RoType(R))
    1670             :       {
    1671             :       case ROt_PT:
    1672          29 :         eng->sc(data,col);
    1673          29 :         eng->pt(data, DTOL((RoPTx(R)+x0)*xs), DTOL((RoPTy(R)+y0)*ys));
    1674          29 :         break;
    1675             :       case ROt_LN:
    1676        3917 :         eng->sc(data,col);
    1677       11751 :         eng->ln(data, DTOL((RoLNx1(R)+x0)*xs), DTOL((RoLNy1(R)+y0)*ys),
    1678        7834 :                       DTOL((RoLNx2(R)+x0)*xs), DTOL((RoLNy2(R)+y0)*ys));
    1679        3917 :         break;
    1680             :       case ROt_BX:
    1681          69 :         eng->sc(data,col);
    1682         345 :         eng->bx(data,
    1683          69 :                 DTOL((RoBXx1(R)+x0)*xs),
    1684          69 :                 DTOL((RoBXy1(R)+y0)*ys),
    1685          69 :                 DTOL((RoBXx2(R)-RoBXx1(R))*xs),
    1686          69 :                 DTOL((RoBXy2(R)-RoBXy1(R))*ys));
    1687          69 :         break;
    1688             :       case ROt_MP:
    1689             :         {
    1690          25 :           double *ptx = RoMPxs(R);
    1691          25 :           double *pty = RoMPys(R);
    1692          25 :           long     nb = RoMPcnt(R);
    1693          25 :           struct plot_points *points =
    1694          25 :             (struct plot_points *) pari_malloc(sizeof(*points)*nb);
    1695         904 :           for(j=0;j<nb;j++)
    1696             :           {
    1697         879 :             points[j].x = DTOL((ptx[j]+x0)*xs);
    1698         879 :             points[j].y = DTOL((pty[j]+y0)*ys);
    1699             :           }
    1700          25 :           eng->sc(data,col);
    1701          25 :           eng->mp(data, nb, points);
    1702          25 :           pari_free(points);
    1703          25 :           break;
    1704             :         }
    1705             :       case ROt_ML:
    1706             :         {
    1707          40 :           double *ptx = RoMLxs(R);
    1708          40 :           double *pty = RoMLys(R);
    1709          40 :           long     nb = RoMLcnt(R);
    1710          40 :           struct plot_points *points =
    1711          40 :             (struct plot_points *) pari_malloc(sizeof(*points)*nb);
    1712       13834 :           for(j=0;j<nb;j++)
    1713             :           {
    1714       13794 :             points[j].x = DTOL((ptx[j]+x0)*xs);
    1715       13794 :             points[j].y = DTOL((pty[j]+y0)*ys);
    1716             :           }
    1717          40 :           eng->sc(data,col);
    1718          40 :           eng->ml(data, nb, points);
    1719          40 :           pari_free(points);
    1720          40 :           break;
    1721             :         }
    1722             :       case ROt_ST:
    1723             :         {
    1724         138 :           long dir = RoSTdir(R);
    1725         138 :           long hjust = dir & RoSTdirHPOS_mask, hgap  = dir & RoSTdirHGAP;
    1726         138 :           long vjust = dir & RoSTdirVPOS_mask, vgap  = dir & RoSTdirVGAP;
    1727         138 :           char *text = RoSTs(R);
    1728         138 :           long x, y, l = RoSTl(R);
    1729         138 :           long shift = (hjust == RoSTdirLEFT ? 0 :
    1730             :               (hjust == RoSTdirRIGHT ? 2 : 1));
    1731         138 :           if (hgap) hgap = (hjust == RoSTdirLEFT) ? hgapsize : -hgapsize;
    1732         138 :           if (vgap) vgap = (vjust == RoSTdirBOTTOM) ? 2*vgapsize : -2*vgapsize;
    1733         138 :           if (vjust != RoSTdirBOTTOM)
    1734          87 :             vgap -= ((vjust == RoSTdirTOP) ? 2 : 1)*(fheight - 1);
    1735         138 :           x = DTOL((RoSTx(R) + x0 + hgap - (l * fwidth * shift)/2)*xs);
    1736         138 :           y = DTOL((RoSTy(R) + y0 - vgap/2)*ys);
    1737         138 :           eng->sc(data,col);
    1738         138 :           eng->st(data, x, y, text, l);
    1739         138 :           break;
    1740             :         }
    1741             :       default:
    1742         507 :         break;
    1743             :       }
    1744             :     }
    1745             :   }
    1746          58 : }
    1747             : 
    1748             : /*************************************************************************/
    1749             : /*                               SVG                                     */
    1750             : /*************************************************************************/
    1751             : 
    1752             : struct svg_data {
    1753             :   pari_str str;
    1754             :   char hexcolor[8];  /* "#rrggbb\0" */
    1755             : };
    1756             : #define data_str(d) (&((struct svg_data*)(d))->str)
    1757             : #define data_hexcolor(d) (((struct svg_data*)(d))->hexcolor)
    1758             : 
    1759             : static const char hexdigit[] = "0123456789abcdef";
    1760             : 
    1761             : /* Work with precision 1/scale */
    1762             : static const float SVG_SCALE = 1024.0;
    1763             : 
    1764             : static float
    1765           0 : svg_rescale(float x) { return x / SVG_SCALE; }
    1766             : 
    1767             : static void
    1768           0 : svg_point(void *data, long x, long y)
    1769             : {
    1770           0 :   pari_str *S = data_str(data);
    1771             : 
    1772           0 :   str_printf(S, "<circle cx='%.2f' cy='%.2f' r='0.5' ",
    1773           0 :     svg_rescale(x), svg_rescale(y));
    1774           0 :   str_printf(S, "style='fill:%s;stroke:none;'/>", data_hexcolor(data));
    1775           0 : }
    1776             : 
    1777             : static void
    1778           0 : svg_line(void *data, long x1, long y1, long x2, long y2)
    1779             : {
    1780           0 :   pari_str *S = data_str(data);
    1781             : 
    1782           0 :   str_printf(S, "<line x1='%.2f' y1='%.2f' x2='%.2f' y2='%.2f' ",
    1783           0 :     svg_rescale(x1), svg_rescale(y1), svg_rescale(x2), svg_rescale(y2));
    1784           0 :   str_printf(S, "style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    1785           0 : }
    1786             : 
    1787             : static void
    1788           0 : svg_rect(void *data, long x, long y, long w, long h)
    1789             : {
    1790           0 :   pari_str *S = data_str(data);
    1791             : 
    1792           0 :   str_printf(S, "<rect x='%.2f' y='%.2f' width='%.2f' height='%.2f' ",
    1793           0 :     svg_rescale(x), svg_rescale(y), svg_rescale(w), svg_rescale(h));
    1794           0 :   str_printf(S, "style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    1795           0 : }
    1796             : 
    1797             : static void
    1798           0 : svg_points(void *data, long nb, struct plot_points *p)
    1799             : {
    1800             :   long i;
    1801           0 :   for (i = 0; i < nb; i++)
    1802           0 :     svg_point(data, p[i].x, p[i].y);
    1803           0 : }
    1804             : 
    1805             : static void
    1806           0 : svg_color(void *data, long col)
    1807             : {
    1808             :   int r, g, b;
    1809           0 :   char *hexcolor = data_hexcolor(data);
    1810           0 :   color_to_rgb(gel(GP_DATA->colormap, col+1), &r, &g, &b);
    1811           0 :   hexcolor[0] = '#';
    1812           0 :   hexcolor[1] = hexdigit[r / 16];
    1813           0 :   hexcolor[2] = hexdigit[r & 15];
    1814           0 :   hexcolor[3] = hexdigit[g / 16];
    1815           0 :   hexcolor[4] = hexdigit[g & 15];
    1816           0 :   hexcolor[5] = hexdigit[b / 16];
    1817           0 :   hexcolor[6] = hexdigit[b & 15];
    1818           0 :   hexcolor[7] = '\0';
    1819           0 : }
    1820             : 
    1821             : static void
    1822           0 : svg_lines(void *data, long nb, struct plot_points *p)
    1823             : {
    1824             :   long i;
    1825           0 :   pari_str *S = data_str(data);
    1826             : 
    1827           0 :   str_printf(S, "<polyline points='");
    1828           0 :   for (i = 0; i < nb; i++)
    1829             :   {
    1830           0 :     if (i > 0) str_printf(S, " ");
    1831           0 :     str_printf(S, "%.2f,%.2f", svg_rescale(p[i].x), svg_rescale(p[i].y));
    1832             :   }
    1833           0 :   str_printf(S, "' style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    1834           0 : }
    1835             : 
    1836             : static void
    1837           0 : svg_text(void *data, long x, long y, char *text, long numtext)
    1838             : {
    1839           0 :   pari_str *S = data_str(data);
    1840             :   (void)numtext;
    1841           0 :   str_printf(S, "<text x='%.5f' y='%.5f' font-size='%ld' style='fill:%s;'>%s</text>",
    1842           0 :     svg_rescale(x),svg_rescale(y), 12, data_hexcolor(data), text);
    1843           0 : }
    1844             : 
    1845             : static void
    1846           0 : svg_head(PARI_plot *T, pari_str *S)
    1847             : {
    1848           0 :   str_printf(S, "<svg width='%ld' height='%ld' version='1.1' xmlns='http://www.w3.org/2000/svg'>", T->width, T->height);
    1849           0 : }
    1850             : 
    1851             : static void
    1852           0 : svg_tail(pari_str *S)
    1853             : {
    1854           0 :   str_printf(S, "</svg>");
    1855           0 : }
    1856             : 
    1857             : static void
    1858           0 : pari_get_svgplot(PARI_plot *T)
    1859             : {
    1860           0 :   T->width   = 480;
    1861           0 :   T->height  = 320;
    1862           0 :   T->hunit   = 3;
    1863           0 :   T->vunit   = 3;
    1864           0 :   T->fwidth  = 9;
    1865           0 :   T->fheight = 12;
    1866           0 : }
    1867             : 
    1868             : char *
    1869           0 : rect2svg(GEN w, GEN x, GEN y, PARI_plot *T)
    1870             : {
    1871             :   struct plot_eng pl;
    1872             :   struct svg_data data;
    1873             :   PARI_plot U;
    1874             : 
    1875           0 :   str_init(&data.str, 1);
    1876           0 :   svg_color(&data, 0);
    1877           0 :   if (!T) { T = &U; pari_get_svgplot(T); }
    1878           0 :   pl.data = &data;
    1879           0 :   pl.sc = &svg_color;
    1880           0 :   pl.pt = &svg_point;
    1881           0 :   pl.ln = &svg_line;
    1882           0 :   pl.bx = &svg_rect;
    1883           0 :   pl.mp = &svg_points;
    1884           0 :   pl.ml = &svg_lines;
    1885           0 :   pl.st = &svg_text;
    1886           0 :   pl.pl = T;
    1887             : 
    1888           0 :   svg_head(T, &data.str);
    1889           0 :   gen_draw(&pl, w, x, y, SVG_SCALE, SVG_SCALE);
    1890           0 :   svg_tail(&data.str);
    1891             : 
    1892           0 :   return data.str.string;
    1893             : }
    1894             : 
    1895             : /*************************************************************************/
    1896             : /*                            POSTSCRIPT                                 */
    1897             : /*************************************************************************/
    1898             : static void
    1899        4218 : ps_sc(void *data, long col)
    1900             : {
    1901        4218 :   pari_str *S = (pari_str*)data;
    1902        4218 :   long l = lg(GP_DATA->colormap)-1;
    1903             :   int r, g, b;
    1904        4218 :   if (col >= l)
    1905             :   {
    1906           0 :     pari_warn(warner,"non-existent color: %ld", col);
    1907           0 :     col = l-1;
    1908             :   }
    1909        4218 :   color_to_rgb(gel(GP_DATA->colormap,col+1), &r, &g, &b);
    1910        4218 :   str_printf(S,"%.6f %.6f %.6f setrgbcolor\n", r/255., g/255., b/255.);
    1911        4218 : }
    1912             : 
    1913             : static void
    1914         908 : ps_point(void *data, long x, long y)
    1915             : {
    1916         908 :   pari_str *S = (pari_str*)data;
    1917         908 :   str_printf(S,"%ld %ld p\n",y,x);
    1918         908 : }
    1919             : 
    1920             : static void
    1921        3917 : ps_line(void *data, long x1, long y1, long x2, long y2)
    1922             : {
    1923        3917 :   pari_str *S = (pari_str*)data;
    1924        3917 :   str_printf(S,"%ld %ld m %ld %ld l\n",y1,x1,y2,x2);
    1925        3917 :   str_printf(S,"stroke\n");
    1926        3917 : }
    1927             : 
    1928             : static void
    1929          69 : ps_rect(void *data, long x, long y, long w, long h)
    1930             : {
    1931          69 :   pari_str *S = (pari_str*)data;
    1932          69 :   str_printf(S,"%ld %ld m %ld %ld l %ld %ld l %ld %ld l closepath\n",
    1933             :              y,x, y,x+w, y+h,x+w, y+h,x);
    1934          69 : }
    1935             : 
    1936             : static void
    1937          25 : ps_points(void *data, long nb, struct plot_points *p)
    1938             : {
    1939             :   long i;
    1940          25 :   for (i=0; i<nb; i++) ps_point(data, p[i].x, p[i].y);
    1941          25 : }
    1942             : 
    1943             : static void
    1944          40 : ps_lines(void *data, long nb, struct plot_points *p)
    1945             : {
    1946          40 :   pari_str *S = (pari_str*)data;
    1947             :   long i;
    1948          40 :   str_printf(S,"%ld %ld m\n",p[0].y,p[0].x);
    1949          40 :   for (i=1; i<nb; i++) str_printf(S, "%ld %ld l\n", p[i].y, p[i].x);
    1950          40 :   str_printf(S,"stroke\n");
    1951          40 : }
    1952             : 
    1953             : static void
    1954         138 : ps_string(void *data, long x, long y, char *s, long length)
    1955             : {
    1956         138 :   pari_str *S = (pari_str*)data;
    1957             :   (void)length;
    1958         138 :   if (strpbrk(s, "(\\)")) {
    1959          11 :     str_printf(S,"(");
    1960          77 :     while (*s) {
    1961          55 :       if ( *s=='(' || *s==')' || *s=='\\' ) str_putc(S,'\\');
    1962          55 :       str_putc(S, *s);
    1963          55 :       s++;
    1964             :     }
    1965             :   } else
    1966         127 :     str_printf(S,"(%s", s);
    1967         138 :   str_printf(S,") %ld %ld m 90 rotate show -90 rotate\n", y, x);
    1968         138 : }
    1969             : 
    1970             : char *
    1971          58 : rect2ps_i(GEN w, GEN x, GEN y, PARI_plot *T, int plotps)
    1972             : {
    1973             :   struct plot_eng pl;
    1974             :   PARI_plot U;
    1975             :   pari_str S;
    1976          58 :   double xs = 0.65, ys = 0.65;
    1977          58 :   if (T) /* res wrt T dimens */
    1978             :   {
    1979          58 :     if (plotps)
    1980           0 :       xs = ys = 1;
    1981             :     else
    1982             :     {
    1983          58 :       xs *= ((double)PS_WIDTH) / T->width;
    1984          58 :       ys *= ((double)PS_HEIGH) / T->height;
    1985             :     }
    1986             :   }
    1987             :   else
    1988             :   {
    1989           0 :     T = &U; pari_get_psplot(T,0);
    1990             :   }
    1991          58 :   str_init(&S, 1);
    1992             :   /* Definitions taken from post terminal of Gnuplot. */
    1993          58 :   str_printf(&S, "%%!\n\
    1994             : 50 50 translate\n\
    1995             : /p {moveto 0 2 rlineto 2 0 rlineto 0 -2 rlineto closepath fill} def\n\
    1996             : /l {lineto} def\n\
    1997             : /m {moveto} def\n"
    1998          58 : "/Times-Roman findfont %ld scalefont setfont\n", DTOL(T->fheight * xs));
    1999             : 
    2000          58 :   pl.sc = &ps_sc;
    2001          58 :   pl.pt = &ps_point;
    2002          58 :   pl.ln = &ps_line;
    2003          58 :   pl.bx = &ps_rect;
    2004          58 :   pl.mp = &ps_points;
    2005          58 :   pl.ml = &ps_lines;
    2006          58 :   pl.st = &ps_string;
    2007          58 :   pl.pl = T;
    2008          58 :   pl.data = (void*)&S;
    2009             : 
    2010          58 :   if (plotps) str_printf(&S,"0 %ld translate -90 rotate\n", T->height - 50);
    2011          58 :   gen_draw(&pl, w, x, y, xs, ys);
    2012          58 :   str_puts(&S,"stroke showpage\n");
    2013          58 :   *S.cur = 0; return S.string;
    2014             : }
    2015             : char *
    2016          58 : rect2ps(GEN w, GEN x, GEN y, PARI_plot *T)
    2017          58 : { return rect2ps_i(w,x,y,T,0); }
    2018             : 
    2019             : void
    2020           0 : pari_plot_by_file(const char *env, const char *suf, const char *img)
    2021             : {
    2022           0 :   char *cmd, *s = pari_unique_filename_suffix("plotfile", suf);
    2023           0 :   FILE *f = fopen(s, "w");
    2024           0 :   if (!f) pari_err_FILE("image file", s);
    2025           0 :   fputs(img, f); (void)fclose(f);
    2026           0 :   cmd = os_getenv(env); if (!cmd) cmd = (char*)"open -W";
    2027           0 :   cmd = pari_sprintf("%s \"%s\" 2>/dev/null", cmd, s);
    2028           0 :   gpsystem(cmd);
    2029           0 :   pari_unlink(s);
    2030           0 :   pari_free(s);
    2031           0 : }
    2032             : 
    2033             : /*************************************************************************/
    2034             : /*                                                                       */
    2035             : /*                           RGB COLORS                                  */
    2036             : /*                                                                       */
    2037             : /*************************************************************************/
    2038             : /* generated from /etc/X11/rgb.txt by the following perl script
    2039             : #!/usr/bin/perl
    2040             : while(<>)
    2041             : {
    2042             :   ($hex, $name) = split(/\t\t/, $_);
    2043             :   $hex =~ s/^ +//; chomp($name); $name =~ s, *,,g;
    2044             :   $hex = sprintf("0x%02x%02x%02x", split(/\s+/, $hex));
    2045             :   $name = lc($name); next if ($done{$name});
    2046             :   $done{$name} = 1;
    2047             :   print "COL(\"$name\", $hex),\n";
    2048             : }
    2049             : */
    2050             : 
    2051             : #define COL(x,y) {(void*)x,(void*)y,0,NULL}
    2052             : static hashentry col_list[] = {
    2053             : COL("", 0x000000),
    2054             : COL("snow", 0xfffafa),
    2055             : COL("ghostwhite", 0xf8f8ff),
    2056             : COL("whitesmoke", 0xf5f5f5),
    2057             : COL("gainsboro", 0xdcdcdc),
    2058             : COL("floralwhite", 0xfffaf0),
    2059             : COL("oldlace", 0xfdf5e6),
    2060             : COL("linen", 0xfaf0e6),
    2061             : COL("antiquewhite", 0xfaebd7),
    2062             : COL("papayawhip", 0xffefd5),
    2063             : COL("blanchedalmond", 0xffebcd),
    2064             : COL("bisque", 0xffe4c4),
    2065             : COL("peachpuff", 0xffdab9),
    2066             : COL("navajowhite", 0xffdead),
    2067             : COL("moccasin", 0xffe4b5),
    2068             : COL("cornsilk", 0xfff8dc),
    2069             : COL("ivory", 0xfffff0),
    2070             : COL("lemonchiffon", 0xfffacd),
    2071             : COL("seashell", 0xfff5ee),
    2072             : COL("honeydew", 0xf0fff0),
    2073             : COL("mintcream", 0xf5fffa),
    2074             : COL("azure", 0xf0ffff),
    2075             : COL("aliceblue", 0xf0f8ff),
    2076             : COL("lavender", 0xe6e6fa),
    2077             : COL("lavenderblush", 0xfff0f5),
    2078             : COL("mistyrose", 0xffe4e1),
    2079             : COL("white", 0xffffff),
    2080             : COL("black", 0x000000),
    2081             : COL("darkslategray", 0x2f4f4f),
    2082             : COL("darkslategrey", 0x2f4f4f),
    2083             : COL("dimgray", 0x696969),
    2084             : COL("dimgrey", 0x696969),
    2085             : COL("slategray", 0x708090),
    2086             : COL("slategrey", 0x708090),
    2087             : COL("lightslategray", 0x778899),
    2088             : COL("lightslategrey", 0x778899),
    2089             : COL("gray", 0xbebebe),
    2090             : COL("grey", 0xbebebe),
    2091             : COL("lightgrey", 0xd3d3d3),
    2092             : COL("lightgray", 0xd3d3d3),
    2093             : COL("midnightblue", 0x191970),
    2094             : COL("navy", 0x000080),
    2095             : COL("navyblue", 0x000080),
    2096             : COL("cornflowerblue", 0x6495ed),
    2097             : COL("darkslateblue", 0x483d8b),
    2098             : COL("slateblue", 0x6a5acd),
    2099             : COL("mediumslateblue", 0x7b68ee),
    2100             : COL("lightslateblue", 0x8470ff),
    2101             : COL("mediumblue", 0x0000cd),
    2102             : COL("royalblue", 0x4169e1),
    2103             : COL("blue", 0x0000ff),
    2104             : COL("dodgerblue", 0x1e90ff),
    2105             : COL("deepskyblue", 0x00bfff),
    2106             : COL("skyblue", 0x87ceeb),
    2107             : COL("lightskyblue", 0x87cefa),
    2108             : COL("steelblue", 0x4682b4),
    2109             : COL("lightsteelblue", 0xb0c4de),
    2110             : COL("lightblue", 0xadd8e6),
    2111             : COL("powderblue", 0xb0e0e6),
    2112             : COL("paleturquoise", 0xafeeee),
    2113             : COL("darkturquoise", 0x00ced1),
    2114             : COL("mediumturquoise", 0x48d1cc),
    2115             : COL("turquoise", 0x40e0d0),
    2116             : COL("cyan", 0x00ffff),
    2117             : COL("lightcyan", 0xe0ffff),
    2118             : COL("cadetblue", 0x5f9ea0),
    2119             : COL("mediumaquamarine", 0x66cdaa),
    2120             : COL("aquamarine", 0x7fffd4),
    2121             : COL("darkgreen", 0x006400),
    2122             : COL("darkolivegreen", 0x556b2f),
    2123             : COL("darkseagreen", 0x8fbc8f),
    2124             : COL("seagreen", 0x2e8b57),
    2125             : COL("mediumseagreen", 0x3cb371),
    2126             : COL("lightseagreen", 0x20b2aa),
    2127             : COL("palegreen", 0x98fb98),
    2128             : COL("springgreen", 0x00ff7f),
    2129             : COL("lawngreen", 0x7cfc00),
    2130             : COL("green", 0x00ff00),
    2131             : COL("chartreuse", 0x7fff00),
    2132             : COL("mediumspringgreen", 0x00fa9a),
    2133             : COL("greenyellow", 0xadff2f),
    2134             : COL("limegreen", 0x32cd32),
    2135             : COL("yellowgreen", 0x9acd32),
    2136             : COL("forestgreen", 0x228b22),
    2137             : COL("olivedrab", 0x6b8e23),
    2138             : COL("darkkhaki", 0xbdb76b),
    2139             : COL("khaki", 0xf0e68c),
    2140             : COL("palegoldenrod", 0xeee8aa),
    2141             : COL("lightgoldenrodyellow", 0xfafad2),
    2142             : COL("lightyellow", 0xffffe0),
    2143             : COL("yellow", 0xffff00),
    2144             : COL("gold", 0xffd700),
    2145             : COL("lightgoldenrod", 0xeedd82),
    2146             : COL("goldenrod", 0xdaa520),
    2147             : COL("darkgoldenrod", 0xb8860b),
    2148             : COL("rosybrown", 0xbc8f8f),
    2149             : COL("indianred", 0xcd5c5c),
    2150             : COL("saddlebrown", 0x8b4513),
    2151             : COL("sienna", 0xa0522d),
    2152             : COL("peru", 0xcd853f),
    2153             : COL("burlywood", 0xdeb887),
    2154             : COL("beige", 0xf5f5dc),
    2155             : COL("wheat", 0xf5deb3),
    2156             : COL("sandybrown", 0xf4a460),
    2157             : COL("tan", 0xd2b48c),
    2158             : COL("chocolate", 0xd2691e),
    2159             : COL("firebrick", 0xb22222),
    2160             : COL("brown", 0xa52a2a),
    2161             : COL("darksalmon", 0xe9967a),
    2162             : COL("salmon", 0xfa8072),
    2163             : COL("lightsalmon", 0xffa07a),
    2164             : COL("orange", 0xffa500),
    2165             : COL("darkorange", 0xff8c00),
    2166             : COL("coral", 0xff7f50),
    2167             : COL("lightcoral", 0xf08080),
    2168             : COL("tomato", 0xff6347),
    2169             : COL("orangered", 0xff4500),
    2170             : COL("red", 0xff0000),
    2171             : COL("hotpink", 0xff69b4),
    2172             : COL("deeppink", 0xff1493),
    2173             : COL("pink", 0xffc0cb),
    2174             : COL("lightpink", 0xffb6c1),
    2175             : COL("palevioletred", 0xdb7093),
    2176             : COL("maroon", 0xb03060),
    2177             : COL("mediumvioletred", 0xc71585),
    2178             : COL("violetred", 0xd02090),
    2179             : COL("magenta", 0xff00ff),
    2180             : COL("violet", 0xee82ee),
    2181             : COL("plum", 0xdda0dd),
    2182             : COL("orchid", 0xda70d6),
    2183             : COL("mediumorchid", 0xba55d3),
    2184             : COL("darkorchid", 0x9932cc),
    2185             : COL("darkviolet", 0x9400d3),
    2186             : COL("blueviolet", 0x8a2be2),
    2187             : COL("purple", 0xa020f0),
    2188             : COL("mediumpurple", 0x9370db),
    2189             : COL("thistle", 0xd8bfd8),
    2190             : COL("snow1", 0xfffafa),
    2191             : COL("snow2", 0xeee9e9),
    2192             : COL("snow3", 0xcdc9c9),
    2193             : COL("snow4", 0x8b8989),
    2194             : COL("seashell1", 0xfff5ee),
    2195             : COL("seashell2", 0xeee5de),
    2196             : COL("seashell3", 0xcdc5bf),
    2197             : COL("seashell4", 0x8b8682),
    2198             : COL("antiquewhite1", 0xffefdb),
    2199             : COL("antiquewhite2", 0xeedfcc),
    2200             : COL("antiquewhite3", 0xcdc0b0),
    2201             : COL("antiquewhite4", 0x8b8378),
    2202             : COL("bisque1", 0xffe4c4),
    2203             : COL("bisque2", 0xeed5b7),
    2204             : COL("bisque3", 0xcdb79e),
    2205             : COL("bisque4", 0x8b7d6b),
    2206             : COL("peachpuff1", 0xffdab9),
    2207             : COL("peachpuff2", 0xeecbad),
    2208             : COL("peachpuff3", 0xcdaf95),
    2209             : COL("peachpuff4", 0x8b7765),
    2210             : COL("navajowhite1", 0xffdead),
    2211             : COL("navajowhite2", 0xeecfa1),
    2212             : COL("navajowhite3", 0xcdb38b),
    2213             : COL("navajowhite4", 0x8b795e),
    2214             : COL("lemonchiffon1", 0xfffacd),
    2215             : COL("lemonchiffon2", 0xeee9bf),
    2216             : COL("lemonchiffon3", 0xcdc9a5),
    2217             : COL("lemonchiffon4", 0x8b8970),
    2218             : COL("cornsilk1", 0xfff8dc),
    2219             : COL("cornsilk2", 0xeee8cd),
    2220             : COL("cornsilk3", 0xcdc8b1),
    2221             : COL("cornsilk4", 0x8b8878),
    2222             : COL("ivory1", 0xfffff0),
    2223             : COL("ivory2", 0xeeeee0),
    2224             : COL("ivory3", 0xcdcdc1),
    2225             : COL("ivory4", 0x8b8b83),
    2226             : COL("honeydew1", 0xf0fff0),
    2227             : COL("honeydew2", 0xe0eee0),
    2228             : COL("honeydew3", 0xc1cdc1),
    2229             : COL("honeydew4", 0x838b83),
    2230             : COL("lavenderblush1", 0xfff0f5),
    2231             : COL("lavenderblush2", 0xeee0e5),
    2232             : COL("lavenderblush3", 0xcdc1c5),
    2233             : COL("lavenderblush4", 0x8b8386),
    2234             : COL("mistyrose1", 0xffe4e1),
    2235             : COL("mistyrose2", 0xeed5d2),
    2236             : COL("mistyrose3", 0xcdb7b5),
    2237             : COL("mistyrose4", 0x8b7d7b),
    2238             : COL("azure1", 0xf0ffff),
    2239             : COL("azure2", 0xe0eeee),
    2240             : COL("azure3", 0xc1cdcd),
    2241             : COL("azure4", 0x838b8b),
    2242             : COL("slateblue1", 0x836fff),
    2243             : COL("slateblue2", 0x7a67ee),
    2244             : COL("slateblue3", 0x6959cd),
    2245             : COL("slateblue4", 0x473c8b),
    2246             : COL("royalblue1", 0x4876ff),
    2247             : COL("royalblue2", 0x436eee),
    2248             : COL("royalblue3", 0x3a5fcd),
    2249             : COL("royalblue4", 0x27408b),
    2250             : COL("blue1", 0x0000ff),
    2251             : COL("blue2", 0x0000ee),
    2252             : COL("blue3", 0x0000cd),
    2253             : COL("blue4", 0x00008b),
    2254             : COL("dodgerblue1", 0x1e90ff),
    2255             : COL("dodgerblue2", 0x1c86ee),
    2256             : COL("dodgerblue3", 0x1874cd),
    2257             : COL("dodgerblue4", 0x104e8b),
    2258             : COL("steelblue1", 0x63b8ff),
    2259             : COL("steelblue2", 0x5cacee),
    2260             : COL("steelblue3", 0x4f94cd),
    2261             : COL("steelblue4", 0x36648b),
    2262             : COL("deepskyblue1", 0x00bfff),
    2263             : COL("deepskyblue2", 0x00b2ee),
    2264             : COL("deepskyblue3", 0x009acd),
    2265             : COL("deepskyblue4", 0x00688b),
    2266             : COL("skyblue1", 0x87ceff),
    2267             : COL("skyblue2", 0x7ec0ee),
    2268             : COL("skyblue3", 0x6ca6cd),
    2269             : COL("skyblue4", 0x4a708b),
    2270             : COL("lightskyblue1", 0xb0e2ff),
    2271             : COL("lightskyblue2", 0xa4d3ee),
    2272             : COL("lightskyblue3", 0x8db6cd),
    2273             : COL("lightskyblue4", 0x607b8b),
    2274             : COL("slategray1", 0xc6e2ff),
    2275             : COL("slategray2", 0xb9d3ee),
    2276             : COL("slategray3", 0x9fb6cd),
    2277             : COL("slategray4", 0x6c7b8b),
    2278             : COL("lightsteelblue1", 0xcae1ff),
    2279             : COL("lightsteelblue2", 0xbcd2ee),
    2280             : COL("lightsteelblue3", 0xa2b5cd),
    2281             : COL("lightsteelblue4", 0x6e7b8b),
    2282             : COL("lightblue1", 0xbfefff),
    2283             : COL("lightblue2", 0xb2dfee),
    2284             : COL("lightblue3", 0x9ac0cd),
    2285             : COL("lightblue4", 0x68838b),
    2286             : COL("lightcyan1", 0xe0ffff),
    2287             : COL("lightcyan2", 0xd1eeee),
    2288             : COL("lightcyan3", 0xb4cdcd),
    2289             : COL("lightcyan4", 0x7a8b8b),
    2290             : COL("paleturquoise1", 0xbbffff),
    2291             : COL("paleturquoise2", 0xaeeeee),
    2292             : COL("paleturquoise3", 0x96cdcd),
    2293             : COL("paleturquoise4", 0x668b8b),
    2294             : COL("cadetblue1", 0x98f5ff),
    2295             : COL("cadetblue2", 0x8ee5ee),
    2296             : COL("cadetblue3", 0x7ac5cd),
    2297             : COL("cadetblue4", 0x53868b),
    2298             : COL("turquoise1", 0x00f5ff),
    2299             : COL("turquoise2", 0x00e5ee),
    2300             : COL("turquoise3", 0x00c5cd),
    2301             : COL("turquoise4", 0x00868b),
    2302             : COL("cyan1", 0x00ffff),
    2303             : COL("cyan2", 0x00eeee),
    2304             : COL("cyan3", 0x00cdcd),
    2305             : COL("cyan4", 0x008b8b),
    2306             : COL("darkslategray1", 0x97ffff),
    2307             : COL("darkslategray2", 0x8deeee),
    2308             : COL("darkslategray3", 0x79cdcd),
    2309             : COL("darkslategray4", 0x528b8b),
    2310             : COL("aquamarine1", 0x7fffd4),
    2311             : COL("aquamarine2", 0x76eec6),
    2312             : COL("aquamarine3", 0x66cdaa),
    2313             : COL("aquamarine4", 0x458b74),
    2314             : COL("darkseagreen1", 0xc1ffc1),
    2315             : COL("darkseagreen2", 0xb4eeb4),
    2316             : COL("darkseagreen3", 0x9bcd9b),
    2317             : COL("darkseagreen4", 0x698b69),
    2318             : COL("seagreen1", 0x54ff9f),
    2319             : COL("seagreen2", 0x4eee94),
    2320             : COL("seagreen3", 0x43cd80),
    2321             : COL("seagreen4", 0x2e8b57),
    2322             : COL("palegreen1", 0x9aff9a),
    2323             : COL("palegreen2", 0x90ee90),
    2324             : COL("palegreen3", 0x7ccd7c),
    2325             : COL("palegreen4", 0x548b54),
    2326             : COL("springgreen1", 0x00ff7f),
    2327             : COL("springgreen2", 0x00ee76),
    2328             : COL("springgreen3", 0x00cd66),
    2329             : COL("springgreen4", 0x008b45),
    2330             : COL("green1", 0x00ff00),
    2331             : COL("green2", 0x00ee00),
    2332             : COL("green3", 0x00cd00),
    2333             : COL("green4", 0x008b00),
    2334             : COL("chartreuse1", 0x7fff00),
    2335             : COL("chartreuse2", 0x76ee00),
    2336             : COL("chartreuse3", 0x66cd00),
    2337             : COL("chartreuse4", 0x458b00),
    2338             : COL("olivedrab1", 0xc0ff3e),
    2339             : COL("olivedrab2", 0xb3ee3a),
    2340             : COL("olivedrab3", 0x9acd32),
    2341             : COL("olivedrab4", 0x698b22),
    2342             : COL("darkolivegreen1", 0xcaff70),
    2343             : COL("darkolivegreen2", 0xbcee68),
    2344             : COL("darkolivegreen3", 0xa2cd5a),
    2345             : COL("darkolivegreen4", 0x6e8b3d),
    2346             : COL("khaki1", 0xfff68f),
    2347             : COL("khaki2", 0xeee685),
    2348             : COL("khaki3", 0xcdc673),
    2349             : COL("khaki4", 0x8b864e),
    2350             : COL("lightgoldenrod1", 0xffec8b),
    2351             : COL("lightgoldenrod2", 0xeedc82),
    2352             : COL("lightgoldenrod3", 0xcdbe70),
    2353             : COL("lightgoldenrod4", 0x8b814c),
    2354             : COL("lightyellow1", 0xffffe0),
    2355             : COL("lightyellow2", 0xeeeed1),
    2356             : COL("lightyellow3", 0xcdcdb4),
    2357             : COL("lightyellow4", 0x8b8b7a),
    2358             : COL("yellow1", 0xffff00),
    2359             : COL("yellow2", 0xeeee00),
    2360             : COL("yellow3", 0xcdcd00),
    2361             : COL("yellow4", 0x8b8b00),
    2362             : COL("gold1", 0xffd700),
    2363             : COL("gold2", 0xeec900),
    2364             : COL("gold3", 0xcdad00),
    2365             : COL("gold4", 0x8b7500),
    2366             : COL("goldenrod1", 0xffc125),
    2367             : COL("goldenrod2", 0xeeb422),
    2368             : COL("goldenrod3", 0xcd9b1d),
    2369             : COL("goldenrod4", 0x8b6914),
    2370             : COL("darkgoldenrod1", 0xffb90f),
    2371             : COL("darkgoldenrod2", 0xeead0e),
    2372             : COL("darkgoldenrod3", 0xcd950c),
    2373             : COL("darkgoldenrod4", 0x8b6508),
    2374             : COL("rosybrown1", 0xffc1c1),
    2375             : COL("rosybrown2", 0xeeb4b4),
    2376             : COL("rosybrown3", 0xcd9b9b),
    2377             : COL("rosybrown4", 0x8b6969),
    2378             : COL("indianred1", 0xff6a6a),
    2379             : COL("indianred2", 0xee6363),
    2380             : COL("indianred3", 0xcd5555),
    2381             : COL("indianred4", 0x8b3a3a),
    2382             : COL("sienna1", 0xff8247),
    2383             : COL("sienna2", 0xee7942),
    2384             : COL("sienna3", 0xcd6839),
    2385             : COL("sienna4", 0x8b4726),
    2386             : COL("burlywood1", 0xffd39b),
    2387             : COL("burlywood2", 0xeec591),
    2388             : COL("burlywood3", 0xcdaa7d),
    2389             : COL("burlywood4", 0x8b7355),
    2390             : COL("wheat1", 0xffe7ba),
    2391             : COL("wheat2", 0xeed8ae),
    2392             : COL("wheat3", 0xcdba96),
    2393             : COL("wheat4", 0x8b7e66),
    2394             : COL("tan1", 0xffa54f),
    2395             : COL("tan2", 0xee9a49),
    2396             : COL("tan3", 0xcd853f),
    2397             : COL("tan4", 0x8b5a2b),
    2398             : COL("chocolate1", 0xff7f24),
    2399             : COL("chocolate2", 0xee7621),
    2400             : COL("chocolate3", 0xcd661d),
    2401             : COL("chocolate4", 0x8b4513),
    2402             : COL("firebrick1", 0xff3030),
    2403             : COL("firebrick2", 0xee2c2c),
    2404             : COL("firebrick3", 0xcd2626),
    2405             : COL("firebrick4", 0x8b1a1a),
    2406             : COL("brown1", 0xff4040),
    2407             : COL("brown2", 0xee3b3b),
    2408             : COL("brown3", 0xcd3333),
    2409             : COL("brown4", 0x8b2323),
    2410             : COL("salmon1", 0xff8c69),
    2411             : COL("salmon2", 0xee8262),
    2412             : COL("salmon3", 0xcd7054),
    2413             : COL("salmon4", 0x8b4c39),
    2414             : COL("lightsalmon1", 0xffa07a),
    2415             : COL("lightsalmon2", 0xee9572),
    2416             : COL("lightsalmon3", 0xcd8162),
    2417             : COL("lightsalmon4", 0x8b5742),
    2418             : COL("orange1", 0xffa500),
    2419             : COL("orange2", 0xee9a00),
    2420             : COL("orange3", 0xcd8500),
    2421             : COL("orange4", 0x8b5a00),
    2422             : COL("darkorange1", 0xff7f00),
    2423             : COL("darkorange2", 0xee7600),
    2424             : COL("darkorange3", 0xcd6600),
    2425             : COL("darkorange4", 0x8b4500),
    2426             : COL("coral1", 0xff7256),
    2427             : COL("coral2", 0xee6a50),
    2428             : COL("coral3", 0xcd5b45),
    2429             : COL("coral4", 0x8b3e2f),
    2430             : COL("tomato1", 0xff6347),
    2431             : COL("tomato2", 0xee5c42),
    2432             : COL("tomato3", 0xcd4f39),
    2433             : COL("tomato4", 0x8b3626),
    2434             : COL("orangered1", 0xff4500),
    2435             : COL("orangered2", 0xee4000),
    2436             : COL("orangered3", 0xcd3700),
    2437             : COL("orangered4", 0x8b2500),
    2438             : COL("red1", 0xff0000),
    2439             : COL("red2", 0xee0000),
    2440             : COL("red3", 0xcd0000),
    2441             : COL("red4", 0x8b0000),
    2442             : COL("debianred", 0xd70751),
    2443             : COL("deeppink1", 0xff1493),
    2444             : COL("deeppink2", 0xee1289),
    2445             : COL("deeppink3", 0xcd1076),
    2446             : COL("deeppink4", 0x8b0a50),
    2447             : COL("hotpink1", 0xff6eb4),
    2448             : COL("hotpink2", 0xee6aa7),
    2449             : COL("hotpink3", 0xcd6090),
    2450             : COL("hotpink4", 0x8b3a62),
    2451             : COL("pink1", 0xffb5c5),
    2452             : COL("pink2", 0xeea9b8),
    2453             : COL("pink3", 0xcd919e),
    2454             : COL("pink4", 0x8b636c),
    2455             : COL("lightpink1", 0xffaeb9),
    2456             : COL("lightpink2", 0xeea2ad),
    2457             : COL("lightpink3", 0xcd8c95),
    2458             : COL("lightpink4", 0x8b5f65),
    2459             : COL("palevioletred1", 0xff82ab),
    2460             : COL("palevioletred2", 0xee799f),
    2461             : COL("palevioletred3", 0xcd6889),
    2462             : COL("palevioletred4", 0x8b475d),
    2463             : COL("maroon1", 0xff34b3),
    2464             : COL("maroon2", 0xee30a7),
    2465             : COL("maroon3", 0xcd2990),
    2466             : COL("maroon4", 0x8b1c62),
    2467             : COL("violetred1", 0xff3e96),
    2468             : COL("violetred2", 0xee3a8c),
    2469             : COL("violetred3", 0xcd3278),
    2470             : COL("violetred4", 0x8b2252),
    2471             : COL("magenta1", 0xff00ff),
    2472             : COL("magenta2", 0xee00ee),
    2473             : COL("magenta3", 0xcd00cd),
    2474             : COL("magenta4", 0x8b008b),
    2475             : COL("orchid1", 0xff83fa),
    2476             : COL("orchid2", 0xee7ae9),
    2477             : COL("orchid3", 0xcd69c9),
    2478             : COL("orchid4", 0x8b4789),
    2479             : COL("plum1", 0xffbbff),
    2480             : COL("plum2", 0xeeaeee),
    2481             : COL("plum3", 0xcd96cd),
    2482             : COL("plum4", 0x8b668b),
    2483             : COL("mediumorchid1", 0xe066ff),
    2484             : COL("mediumorchid2", 0xd15fee),
    2485             : COL("mediumorchid3", 0xb452cd),
    2486             : COL("mediumorchid4", 0x7a378b),
    2487             : COL("darkorchid1", 0xbf3eff),
    2488             : COL("darkorchid2", 0xb23aee),
    2489             : COL("darkorchid3", 0x9a32cd),
    2490             : COL("darkorchid4", 0x68228b),
    2491             : COL("purple1", 0x9b30ff),
    2492             : COL("purple2", 0x912cee),
    2493             : COL("purple3", 0x7d26cd),
    2494             : COL("purple4", 0x551a8b),
    2495             : COL("mediumpurple1", 0xab82ff),
    2496             : COL("mediumpurple2", 0x9f79ee),
    2497             : COL("mediumpurple3", 0x8968cd),
    2498             : COL("mediumpurple4", 0x5d478b),
    2499             : COL("thistle1", 0xffe1ff),
    2500             : COL("thistle2", 0xeed2ee),
    2501             : COL("thistle3", 0xcdb5cd),
    2502             : COL("thistle4", 0x8b7b8b),
    2503             : COL("gray0", 0x000000),
    2504             : COL("grey0", 0x000000),
    2505             : COL("gray1", 0x030303),
    2506             : COL("grey1", 0x030303),
    2507             : COL("gray2", 0x050505),
    2508             : COL("grey2", 0x050505),
    2509             : COL("gray3", 0x080808),
    2510             : COL("grey3", 0x080808),
    2511             : COL("gray4", 0x0a0a0a),
    2512             : COL("grey4", 0x0a0a0a),
    2513             : COL("gray5", 0x0d0d0d),
    2514             : COL("grey5", 0x0d0d0d),
    2515             : COL("gray6", 0x0f0f0f),
    2516             : COL("grey6", 0x0f0f0f),
    2517             : COL("gray7", 0x121212),
    2518             : COL("grey7", 0x121212),
    2519             : COL("gray8", 0x141414),
    2520             : COL("grey8", 0x141414),
    2521             : COL("gray9", 0x171717),
    2522             : COL("grey9", 0x171717),
    2523             : COL("gray10", 0x1a1a1a),
    2524             : COL("grey10", 0x1a1a1a),
    2525             : COL("gray11", 0x1c1c1c),
    2526             : COL("grey11", 0x1c1c1c),
    2527             : COL("gray12", 0x1f1f1f),
    2528             : COL("grey12", 0x1f1f1f),
    2529             : COL("gray13", 0x212121),
    2530             : COL("grey13", 0x212121),
    2531             : COL("gray14", 0x242424),
    2532             : COL("grey14", 0x242424),
    2533             : COL("gray15", 0x262626),
    2534             : COL("grey15", 0x262626),
    2535             : COL("gray16", 0x292929),
    2536             : COL("grey16", 0x292929),
    2537             : COL("gray17", 0x2b2b2b),
    2538             : COL("grey17", 0x2b2b2b),
    2539             : COL("gray18", 0x2e2e2e),
    2540             : COL("grey18", 0x2e2e2e),
    2541             : COL("gray19", 0x303030),
    2542             : COL("grey19", 0x303030),
    2543             : COL("gray20", 0x333333),
    2544             : COL("grey20", 0x333333),
    2545             : COL("gray21", 0x363636),
    2546             : COL("grey21", 0x363636),
    2547             : COL("gray22", 0x383838),
    2548             : COL("grey22", 0x383838),
    2549             : COL("gray23", 0x3b3b3b),
    2550             : COL("grey23", 0x3b3b3b),
    2551             : COL("gray24", 0x3d3d3d),
    2552             : COL("grey24", 0x3d3d3d),
    2553             : COL("gray25", 0x404040),
    2554             : COL("grey25", 0x404040),
    2555             : COL("gray26", 0x424242),
    2556             : COL("grey26", 0x424242),
    2557             : COL("gray27", 0x454545),
    2558             : COL("grey27", 0x454545),
    2559             : COL("gray28", 0x474747),
    2560             : COL("grey28", 0x474747),
    2561             : COL("gray29", 0x4a4a4a),
    2562             : COL("grey29", 0x4a4a4a),
    2563             : COL("gray30", 0x4d4d4d),
    2564             : COL("grey30", 0x4d4d4d),
    2565             : COL("gray31", 0x4f4f4f),
    2566             : COL("grey31", 0x4f4f4f),
    2567             : COL("gray32", 0x525252),
    2568             : COL("grey32", 0x525252),
    2569             : COL("gray33", 0x545454),
    2570             : COL("grey33", 0x545454),
    2571             : COL("gray34", 0x575757),
    2572             : COL("grey34", 0x575757),
    2573             : COL("gray35", 0x595959),
    2574             : COL("grey35", 0x595959),
    2575             : COL("gray36", 0x5c5c5c),
    2576             : COL("grey36", 0x5c5c5c),
    2577             : COL("gray37", 0x5e5e5e),
    2578             : COL("grey37", 0x5e5e5e),
    2579             : COL("gray38", 0x616161),
    2580             : COL("grey38", 0x616161),
    2581             : COL("gray39", 0x636363),
    2582             : COL("grey39", 0x636363),
    2583             : COL("gray40", 0x666666),
    2584             : COL("grey40", 0x666666),
    2585             : COL("gray41", 0x696969),
    2586             : COL("grey41", 0x696969),
    2587             : COL("gray42", 0x6b6b6b),
    2588             : COL("grey42", 0x6b6b6b),
    2589             : COL("gray43", 0x6e6e6e),
    2590             : COL("grey43", 0x6e6e6e),
    2591             : COL("gray44", 0x707070),
    2592             : COL("grey44", 0x707070),
    2593             : COL("gray45", 0x737373),
    2594             : COL("grey45", 0x737373),
    2595             : COL("gray46", 0x757575),
    2596             : COL("grey46", 0x757575),
    2597             : COL("gray47", 0x787878),
    2598             : COL("grey47", 0x787878),
    2599             : COL("gray48", 0x7a7a7a),
    2600             : COL("grey48", 0x7a7a7a),
    2601             : COL("gray49", 0x7d7d7d),
    2602             : COL("grey49", 0x7d7d7d),
    2603             : COL("gray50", 0x7f7f7f),
    2604             : COL("grey50", 0x7f7f7f),
    2605             : COL("gray51", 0x828282),
    2606             : COL("grey51", 0x828282),
    2607             : COL("gray52", 0x858585),
    2608             : COL("grey52", 0x858585),
    2609             : COL("gray53", 0x878787),
    2610             : COL("grey53", 0x878787),
    2611             : COL("gray54", 0x8a8a8a),
    2612             : COL("grey54", 0x8a8a8a),
    2613             : COL("gray55", 0x8c8c8c),
    2614             : COL("grey55", 0x8c8c8c),
    2615             : COL("gray56", 0x8f8f8f),
    2616             : COL("grey56", 0x8f8f8f),
    2617             : COL("gray57", 0x919191),
    2618             : COL("grey57", 0x919191),
    2619             : COL("gray58", 0x949494),
    2620             : COL("grey58", 0x949494),
    2621             : COL("gray59", 0x969696),
    2622             : COL("grey59", 0x969696),
    2623             : COL("gray60", 0x999999),
    2624             : COL("grey60", 0x999999),
    2625             : COL("gray61", 0x9c9c9c),
    2626             : COL("grey61", 0x9c9c9c),
    2627             : COL("gray62", 0x9e9e9e),
    2628             : COL("grey62", 0x9e9e9e),
    2629             : COL("gray63", 0xa1a1a1),
    2630             : COL("grey63", 0xa1a1a1),
    2631             : COL("gray64", 0xa3a3a3),
    2632             : COL("grey64", 0xa3a3a3),
    2633             : COL("gray65", 0xa6a6a6),
    2634             : COL("grey65", 0xa6a6a6),
    2635             : COL("gray66", 0xa8a8a8),
    2636             : COL("grey66", 0xa8a8a8),
    2637             : COL("gray67", 0xababab),
    2638             : COL("grey67", 0xababab),
    2639             : COL("gray68", 0xadadad),
    2640             : COL("grey68", 0xadadad),
    2641             : COL("gray69", 0xb0b0b0),
    2642             : COL("grey69", 0xb0b0b0),
    2643             : COL("gray70", 0xb3b3b3),
    2644             : COL("grey70", 0xb3b3b3),
    2645             : COL("gray71", 0xb5b5b5),
    2646             : COL("grey71", 0xb5b5b5),
    2647             : COL("gray72", 0xb8b8b8),
    2648             : COL("grey72", 0xb8b8b8),
    2649             : COL("gray73", 0xbababa),
    2650             : COL("grey73", 0xbababa),
    2651             : COL("gray74", 0xbdbdbd),
    2652             : COL("grey74", 0xbdbdbd),
    2653             : COL("gray75", 0xbfbfbf),
    2654             : COL("grey75", 0xbfbfbf),
    2655             : COL("gray76", 0xc2c2c2),
    2656             : COL("grey76", 0xc2c2c2),
    2657             : COL("gray77", 0xc4c4c4),
    2658             : COL("grey77", 0xc4c4c4),
    2659             : COL("gray78", 0xc7c7c7),
    2660             : COL("grey78", 0xc7c7c7),
    2661             : COL("gray79", 0xc9c9c9),
    2662             : COL("grey79", 0xc9c9c9),
    2663             : COL("gray80", 0xcccccc),
    2664             : COL("grey80", 0xcccccc),
    2665             : COL("gray81", 0xcfcfcf),
    2666             : COL("grey81", 0xcfcfcf),
    2667             : COL("gray82", 0xd1d1d1),
    2668             : COL("grey82", 0xd1d1d1),
    2669             : COL("gray83", 0xd4d4d4),
    2670             : COL("grey83", 0xd4d4d4),
    2671             : COL("gray84", 0xd6d6d6),
    2672             : COL("grey84", 0xd6d6d6),
    2673             : COL("gray85", 0xd9d9d9),
    2674             : COL("grey85", 0xd9d9d9),
    2675             : COL("gray86", 0xdbdbdb),
    2676             : COL("grey86", 0xdbdbdb),
    2677             : COL("gray87", 0xdedede),
    2678             : COL("grey87", 0xdedede),
    2679             : COL("gray88", 0xe0e0e0),
    2680             : COL("grey88", 0xe0e0e0),
    2681             : COL("gray89", 0xe3e3e3),
    2682             : COL("grey89", 0xe3e3e3),
    2683             : COL("gray90", 0xe5e5e5),
    2684             : COL("grey90", 0xe5e5e5),
    2685             : COL("gray91", 0xe8e8e8),
    2686             : COL("grey91", 0xe8e8e8),
    2687             : COL("gray92", 0xebebeb),
    2688             : COL("grey92", 0xebebeb),
    2689             : COL("gray93", 0xededed),
    2690             : COL("grey93", 0xededed),
    2691             : COL("gray94", 0xf0f0f0),
    2692             : COL("grey94", 0xf0f0f0),
    2693             : COL("gray95", 0xf2f2f2),
    2694             : COL("grey95", 0xf2f2f2),
    2695             : COL("gray96", 0xf5f5f5),
    2696             : COL("grey96", 0xf5f5f5),
    2697             : COL("gray97", 0xf7f7f7),
    2698             : COL("grey97", 0xf7f7f7),
    2699             : COL("gray98", 0xfafafa),
    2700             : COL("grey98", 0xfafafa),
    2701             : COL("gray99", 0xfcfcfc),
    2702             : COL("grey99", 0xfcfcfc),
    2703             : COL("gray100", 0xffffff),
    2704             : COL("grey100", 0xffffff),
    2705             : COL("darkgrey", 0xa9a9a9),
    2706             : COL("darkgray", 0xa9a9a9),
    2707             : COL("darkblue", 0x00008b),
    2708             : COL("darkcyan", 0x008b8b),
    2709             : COL("darkmagenta", 0x8b008b),
    2710             : COL("darkred", 0x8b0000),
    2711             : COL("lightgreen", 0x90ee90),
    2712             : COL(NULL,0) /* sentinel */
    2713             : };
    2714             : #undef COL
    2715             : 
    2716             : void
    2717        4218 : colorname_to_rgb(const char *s, int *r, int *g, int *b)
    2718             : {
    2719             :   hashentry *ep;
    2720             :   long rgb;
    2721             : 
    2722        4218 :   if (!rgb_colors) rgb_colors = hashstr_import_static(col_list, 1000);
    2723        4218 :   ep = hash_search(rgb_colors, (void*)s);
    2724        4218 :   if (!ep) pari_err(e_MISC, "unknown color %s", s);
    2725        4218 :   rgb = (long)ep->val;
    2726        4218 :   *b = rgb & 0xff; rgb >>= 8;
    2727        4218 :   *g = rgb & 0xff; rgb >>= 8;
    2728        4218 :   *r = rgb;
    2729        4218 : }
    2730             : 
    2731             : void
    2732        4218 : color_to_rgb(GEN c, int *r, int *g, int *b)
    2733             : {
    2734        4218 :   switch(typ(c))
    2735             :   {
    2736             :     case t_STR:
    2737        4218 :       colorname_to_rgb(GSTR(c), r,g,b);
    2738        4218 :       break;
    2739             :     default: /* t_VECSMALL: */
    2740           0 :       *r = c[1]; *g = c[2]; *b = c[3];
    2741           0 :       break;
    2742             :   }
    2743        4218 : }

Generated by: LCOV version 1.11