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

Generated by: LCOV version 1.14