===================================================================
RCS file: /home/cvs/OpenXM_contrib2/asir2000/lib/glib,v
retrieving revision 1.8
retrieving revision 1.14
diff -u -p -r1.8 -r1.14
--- OpenXM_contrib2/asir2000/lib/glib	2002/08/03 23:59:14	1.8
+++ OpenXM_contrib2/asir2000/lib/glib	2004/03/11 12:25:20	1.14
@@ -1,7 +1,12 @@
-/* $OpenXM: OpenXM_contrib2/asir2000/lib/glib,v 1.7 2002/07/15 02:14:48 takayama Exp $ */
-/* $Id: glib,v 1.8 2002/08/03 23:59:14 takayama Exp $ */
+/* $OpenXM: OpenXM_contrib2/asir2000/lib/glib,v 1.13 2004/03/11 05:16:11 takayama Exp $ */
+/* $Id: glib,v 1.14 2004/03/11 12:25:20 takayama Exp $ */
 
+/* BUG: this library has not yet been adapted to the new automatic launcher
+        of ox_plot. 2002, Aug, 4.  Do not load glib more than twice. 
+*/
 
+#define DO_NOT_START_SERVER_EXPLICITLY
+
 /* #define CLIP  1 */
 /* #define DEBUG 1 */
 #define LIST  4
@@ -41,21 +46,19 @@ Glib_canvas_x = 400$
 Glib_canvas_y = 400$
 Glib_xmin=0$  Glib_xmax=Glib_canvas_x$
 Glib_ymin=0$  Glib_ymax=Glib_canvas_y$
-#define START_SERVER \
-extern Glib_server_started, Glib_process$ \
-if (type(Glib_server_started) == 0) { \
-  Glib_process = ox_launch_nox(0,"ox_plot")$ \
-  register_handler(reset_plot); \
-  Glib_server_started = 1$ \
-}$
+Glib_math_coordinate=0$
 
+
 Glib_canvas = -1$
 Glib_xmag = 1$ Glib_ymag=1$ Glib_xoffset=0$ Glib_yoffset=0$
 Glib_safe_mode = 2 $
 Glib_counter = 100$
 
 def open_Canvas(P,S) {
-  open_canvas(P,S);
+  extern Glib_process;
+  if (P < 0) P=open_canvas(S); /* BUG, get process No. */
+  else open_canvas(P,S);
+  Glib_process = P; 
   R = ox_pop_cmo(P);
 /*
   if (glib_ox_get_errors(P) == []) {
@@ -77,9 +80,20 @@ end: */
 
 def glib_open() {
   extern Glib_canvas_x, Glib_canvas_y,
-         Glib_process, Glib_canvas;
-  START_SERVER  
+         Glib_process, Glib_canvas,
+         Glib_server_started, Glib_process$
+#ifndef DO_NOT_START_SERVER_EXPLICITLY
+if (type(Glib_server_started) == 0) { 
+    Glib_process = ox_launch_nox(0,"ox_plot")$ 
+    register_handler(reset_plot); 
+    Glib_server_started = 1$ 
+  }$
   Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
+#else
+  Glib_canvas = open_Canvas(-1,[Glib_canvas_x,Glib_canvas_y])$
+  register_handler(reset_plot); 
+  Glib_server_started = 1$ 
+#endif
   glib_check_strict();
   return(Glib_canvas);
 }
@@ -127,6 +141,9 @@ def glib_clear() {
 /*&usage begin: glib_window(Xmin,Ymin,Xmax,Ymax)
   It generates a window with the left top corner [{Xmin},{Ymin}] and
   the right bottom corner [{Xmax},{Ymax}].
+  If the global variable {Glib_math_coordinate} is set to 1, mathematical
+  coordinate system will be employed, i.e., the left top
+  corner will have the coordinate [{Xmin},{Ymax}].
   example: glib_window(-1,-1,10,10);
 end: */
 def glib_window(Xmin,Ymin,Xmax,Ymax) {
@@ -161,19 +178,29 @@ def glib_check_arg(X,Y) {
 end: */
 def glib_putpixel(X,Y) {
   extern Glib_process, Glib_canvas,
-         Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps;
+         Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps,
+         Glib_math_coordinate, Glib_canvas_y;
   if (Glib_canvas < 0) glib_open();
   glib_check();
   glib_check_arg(X,Y);
   C = getopt(color);
-  Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
-         glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+  if (Glib_math_coordinate) {
+    Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
+           Glib_canvas_y-glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+    Pos2= [glib_floor(Glib_xmag*(X+Glib_xoffset)),
+           glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+  }else{
+    Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
+           glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+    Pos2= [glib_floor(Glib_xmag*(X+Glib_xoffset)),
+           Glib_canvas_y-glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+  }
   if ( type(C) != -1 ) {
     draw_obj(Glib_process,Glib_canvas,Pos,C);
-    if (Glib_ps) glib_history(["putpixel",Pos,C]);
+    if (Glib_ps) glib_history(["putpixel",Pos2,C]);
   }else{
     draw_obj(Glib_process,Glib_canvas,Pos);
-    if (Glib_ps) glib_history(["putpixel",Pos,0]);
+    if (Glib_ps) glib_history(["putpixel",Pos2,0]);
   }
 }
 
@@ -196,7 +223,7 @@ def glib_clip_line(X0,Y0,X1,Y1,Color) {
   /* X0, Y0, X1, Y1 should be integers. 
      Coordinates are already translated. */
   extern Glib_process, Glib_canvas, Glib_canvas_x, Glib_canvas_y,
-         Glib_ps;
+         Glib_ps,  Glib_math_coordinate;
   if (Glib_canvas < 0) glib_open();
 
 if (Glib_ps) {
@@ -238,13 +265,21 @@ if (Glib_ps) {
 }
 
   glib_check();
-  Pos = [glib_floor(X0),glib_floor(Y0),glib_floor(X1),glib_floor(Y1)];
+  if (Glib_math_coordinate) {
+    Pos = [glib_floor(X0),Glib_canvas_y-glib_floor(Y0),
+           glib_floor(X1),Glib_canvas_y-glib_floor(Y1)];
+    Pos2= [glib_floor(X0),glib_floor(Y0),glib_floor(X1),glib_floor(Y1)];
+  }else{
+    Pos = [glib_floor(X0),glib_floor(Y0),glib_floor(X1),glib_floor(Y1)];
+    Pos2 = [glib_floor(X0),Glib_canvas_y-glib_floor(Y0),
+           glib_floor(X1),Glib_canvas_y-glib_floor(Y1)];
+  }
   if ( type(Color) != -1 ) {
     draw_obj(Glib_process,Glib_canvas,Pos,Color);
-    if (Glib_ps) glib_history(["line",Pos,Color]);
+    if (Glib_ps) glib_history(["line",Pos2,Color]);
   }else{
     draw_obj(Glib_process,Glib_canvas,Pos);
-    if (Glib_ps) glib_history(["line",Pos,0]);
+    if (Glib_ps) glib_history(["line",Pos2,0]);
   }
 }
 
@@ -295,25 +330,36 @@ def glib_clip1_y(X0,Y0,X1,Y1,Clip) {
 end: */
 def glib_print(X,Y,Text) {
   extern Glib_process, Glib_canvas,
-         Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps;
+         Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps,
+         Glib_math_coordinate, Glib_canvas_y;
   if (Glib_canvas < 0) glib_open();
   glib_check();
   glib_check_arg(X,Y);
   if (type(Text) != 7) error("glib_print(X,Y,Text): Text must be a string.");
   C = getopt(color);
-  Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
-         glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+  if (Glib_math_coordinate) {
+    Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
+           Glib_canvas_y-glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+    Pos2= [glib_floor(Glib_xmag*(X+Glib_xoffset)),
+           glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+  }else{
+    Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
+           glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+    Pos2= [glib_floor(Glib_xmag*(X+Glib_xoffset)),
+           Glib_canvas_y-glib_floor(Glib_ymag*(Y+Glib_yoffset))];
+  }
   if ( type(C) != -1 ) {
     draw_string(Glib_process,Glib_canvas,Pos,Text,C);
-    if (Glib_ps) glib_history(["print",Pos,Text,C]);
+    if (Glib_ps) glib_history(["print",Pos2,Text,C]);
   }else{
     draw_string(Glib_process,Glib_canvas,Pos,Text);
-    if (Glib_ps) glib_history(["print",Pos,Text,0]);
+    if (Glib_ps) glib_history(["print",Pos2,Text,0]);
   }
 }
 
 def glib_history(L) {
-  extern Glib_h, Glib_canvas_x, Glib_canvas_y;
+  extern Glib_h, Glib_canvas_x, Glib_canvas_y,
+         Glib_math_coodinate;
   if (L[0] == "putpixel" || L[0] == "print") {
     if (L[1][0] <= Glib_canvas_x && L[1][0] >= 0) {
       if (L[1][1] <= Glib_canvas_x && L[1][1] >= 0) 
@@ -335,23 +381,23 @@ def glib_tops() {
   return glib_ps(Glib_h);
 }
 def glib_ps(L) {
-  PS = "";
+  PS = string_to_tb("");
   Prev_color = 0;
   /* Prolog */
-  PS += "%%!PS-Adobe-1.0\n";
-  PS += "%%BoundingBox: 0 0 " +
-          rtostr(Glib_canvas_x) + " " + rtostr(Glib_canvas_y) + "\n";
-  PS += "%%Creator: This is generated by ifplot\n";
-  PS += "%%Title: ifplot\n";
-  PS += "%%EndComments: \n";
-  PS += "0.1 setlinewidth \n";
-  PS += "2 setlinecap \n";
-  PS += "2 setlinejoin \n";
-  PS += "/ifplot_putpixel {  \n";
-  PS += "    /yyy 2 1 roll def /xxx 2 1 roll def \n";
-  PS += "    gsave newpath xxx yyy .5 0 360 arc \n";
-  PS += "    fill grestore \n";
-  PS += "} def \n";
+  write_to_tb("%%!PS-Adobe-1.0\n",PS);
+  write_to_tb("%%BoundingBox: 0 0 " +
+          rtostr(Glib_canvas_x) + " " + rtostr(Glib_canvas_y) + "\n",PS);
+  write_to_tb("%%Creator: This is generated by ifplot\n",PS);
+  write_to_tb("%%Title: ifplot\n",PS);
+  write_to_tb("%%EndComments: \n",PS);
+  write_to_tb("0.1 setlinewidth \n",PS);
+  write_to_tb("2 setlinecap \n",PS);
+  write_to_tb("2 setlinejoin \n",PS);
+  write_to_tb("/ifplot_putpixel {  \n",PS);
+  write_to_tb("    /yyy 2 1 roll def /xxx 2 1 roll def \n",PS);
+  write_to_tb("    gsave newpath xxx yyy .5 0 360 arc \n",PS);
+  write_to_tb("    fill grestore \n",PS);
+  write_to_tb("} def \n",PS);
 
   L = reverse(L);
   N = length(L);
@@ -359,30 +405,64 @@ def glib_ps(L) {
     C = L[I];
     if (C[length(C)-1] != Prev_color) {
       Prev_color = C[length(C)-1];
-      PS += rtostr(deval(ishift(Prev_color,16)/256)) + " " +
+      write_to_tb(rtostr(deval(ishift(Prev_color,16)/256)) + " " +
             rtostr(deval(iand(ishift(Prev_color,8),0xff)/256)) + " " +
-            rtostr(deval(iand(Prev_color,0xff)/256)) + " setrgbcolor \n";
+            rtostr(deval(iand(Prev_color,0xff)/256)) + " setrgbcolor \n",PS);
     }
     if (C[0] == "putpixel") {
-       PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " ifplot_putpixel \n";
+       write_to_tb(rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " ifplot_putpixel \n",PS);
     }
     if (C[0] == "line") {
-       PS += " newpath ";
-       PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto " +
-             rtostr(C[1][2]) + " " + rtostr(C[1][3]) + " lineto stroke \n";
+       write_to_tb(" newpath ",PS);
+       write_to_tb(rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto " +
+             rtostr(C[1][2]) + " " + rtostr(C[1][3]) + " lineto stroke \n",PS);
     }
     if (C[0] == "print") {
-       PS += "/Times-Roman findfont 10 scalefont setfont \n";
-       PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto ";
-       PS += "(" + C[2] + ")  show \n";
+       write_to_tb("/Times-Roman findfont 10 scalefont setfont \n",PS);
+       write_to_tb(rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto ",PS);
+       write_to_tb("(" + C[2] + ")  show \n",PS);
     }
   }
 
   /* Epilog */
-  PS += "0 0 0 setrgbcolor \n";
-  PS += "showpage \n";
+  write_to_tb("0 0 0 setrgbcolor \n",PS);
+  write_to_tb("showpage \n",PS);
 
-  return PS;
+  return tb_to_string(PS);
 }
+
+/*&usage begin: glib_ps_form(S)
+  It returns the PS code generated by executing {S}
+  (experimental).
+  BUG. Only one function call is allowed as an argument. 
+  example: glib_ps_form(quote( glib_line(0,0,100,100) ));
+  ref: glib_tops
+end: */
+def glib_ps_form(F) {
+  extern Glib_h;
+  extern Glib_ps;
+  H = Glib_h; /* push Glib_h */
+  P = Glib_ps; /* push Glib_ps */
+
+  Glib_ps=1; Glib_h = [];
+  eval_quote(F);  
+  /* bug. eval_str causes seg fault for ccurve.rr main(8) */
+  R = glib_tops();
+  Glib_h = H; /* pop Glib_h */
+  Glib_ps = P; /* pop Glib_ps */
+  return R;
+}
+
+/*&usage begin: glib_plot(F)
+  It plots an object {F} on the glib canvas.
+  example: glib_plot([[0,1],[0.1,0.9],[0.2,0.7],[0.3,0.5],[0.4,0.8]]);
+  example: glib_plot(tan(x));
+end: */
+/* bug, xmin, xmax, color should be optional variables. */
+def glib_plot(F) {
+  Opt = getopt();
+  taka_glib_plot(F,Opt);
+}
+
 end$