load("oxrfc103.rr");
module cfep;
localf initStatic$
localf setup$
localf test0$
localf test2$
localf t_encode$
localf t_encode1$
localf t_encode_str$
localf glib_send$
localf glib_send2$
localf glib_init_send$
localf open_canvas$
localf open_canvas2$
localf draw_obj$
localf draw_obj2$
localf draw_string$
localf draw_string2$
localf draw_flush$
localf getGlib_canvas$
localf x11toGLx$
localf x11toGLy$
localf glib_clear$
localf glib_remove_last$
localf glib_set_pixel_size$
localf png_send$
localf png_form$
static Glib_canvas;
static Glib_canvas_x;
static Glib_canvas_y;
def initStatic() {
ctrl("double_output",1);
Glib_canvas = 0; /* gid */
Glib_canvas_x = 400;
Glib_canvas_y = 400;
}
initStatic()$
def setup() {
ctrl("debug_window",0);
ctrl("no_debug_on_error",1)$
}
def getGlib_canvas() {
return Glib_canvas;
}
/* Example */
def test0() {
/* gid; commands */
glib_init_send("0; [glColor4f, 1.0,0.0,0.0,0.3]");
glib_init_send("0; [glRectf, -0.1,-0.1,0.1,0.1]");
glib_init_send("0; [glib_flush]");
glib_send("0; [glib_line,0.0,0.0,0.5,0.5,(int)255]");
glib_send("0; [glib_flush]");
}
def test2() {
glib_init_send("0;[glib3_ray_init]");
glib_init_send("0;[glib_flush]");
glib_send("0;[glib3_ray]");
glib_send("0;[glib_flush]");
}
def open_canvas(S) {
Glib_canvas++;
if (S[0] != 0) Glib_canvas_x = S[0];
else error("The fisrst argument of cfep.open_canvas is 0.");
if (S[1] != 0) Glib_canvas_y = S[1];
else error("The first argument of cfep.open_canvas is 0.");
return Glib_canvas;
}
def open_canvas2(P,S) {
return open_canvas(S);
}
def draw_obj(P,Canvas,S) {
draw_obj2(P,Canvas,S,0);
}
def x11toGLx(X) { X=deval(X); return (-1.0+2*X/Glib_canvas_x); }
def x11toGLy(Y) { Y=deval(Y); return (1.0-2*Y/Glib_canvas_y); }
def draw_obj2(P,Canvas,S,Color) {
if (length(S) == 4) { /* line */
X = rtostr(x11toGLx(S[0])); Y = rtostr(x11toGLy(S[1]));
X2 = rtostr(x11toGLx(S[2])); Y2 = rtostr(x11toGLy(S[3]));
CC="(int)"+rtostr(Color);
glib_send(rtostr(Canvas)+"; [glib_line,"+X+","+Y+","+X2+","+Y2+","+CC+"]");
}else if (length(S) == 2) { /* point */
X = rtostr(x11toGLx(S[0])); Y = rtostr(x11toGLy(S[1]));
CC="(int)"+rtostr(Color);
glib_send(rtostr(Canvas)+"; [glib_putpixel,"+X+","+Y+","+CC+"]");
}
} /* cfep.draw_obj2(0,0,[100,100],0xff0000); cfep.draw_flush(); */
def draw_string(P,C,Pos,Text) {
draw_string2(P,C,Pos,Text,0);
}
def draw_string2(P,C,Pos,Test,Color) {
print("cfep.draw_string2() has not been implemented.");
}
def draw_flush(Canvas) {
glib_send(rtostr(Canvas)+"; [glib_flush]");
}
def t_encode1(X) {
return [
ior(0xf8,iand(0x3,ishift(X,6))),
ior(0xf0,iand(0x7,ishift(X,3))),
ior(0xf0,iand(0x7,X))];
}
def t_encode_str(S) {
Ans = [ ];
Codes = strtoascii(S);
N = length(Codes);
for (I=0; I<N; I++) {
T = t_encode1(Codes[I]);
Ans = cons(T[0],Ans);
Ans = cons(T[1],Ans);
Ans = cons(T[2],Ans);
}
return reverse(Ans);
}
def t_encode(S) {
return asciitostr(t_encode_str(S));
}
def glib_send(S) {
S=S+"\n";
N=length(strtoascii(S))+1;
C=t_encode("{1<"+rtostr(N)+" "+S+" >}");
ctrl("hex",1);
output(); ctrl("hex",0);
print(C,0); output(); /* channel 1 is for OpenGL redraw. */
}
def glib_init_send(S) {
S=S+"\n";
N=length(strtoascii(S))+1;
C=t_encode("{2<"+rtostr(N)+" "+S+" >}");
ctrl("hex",1);
output(); ctrl("hex",0);
print(C,0); output(); /* channel 2 is for OpenGL init. */
}
def glib_send2(Channel,S) {
S=S+"\n";
if (Channel==1) Channel="1"; /* regular */
else Channel="2"; /* init */
N=length(strtoascii(S))+1;
C=t_encode("{"+Channel+"<"+rtostr(N)+" "+S+" >}");
ctrl("hex",1);
output(); ctrl("hex",0);
print(C,0); output(); /* channel 2 is for OpenGL init. */
}
/* glib_line(0,0,100,100); glib_flush();
cfep.glib_clear();
*/
def glib_clear() {
Glib_canvas = cfep.getGlib_canvas();
glib_send(rtostr(Glib_canvas)+";meta_removeAllInit");
glib_send(rtostr(Glib_canvas)+";meta_removeAll");
glib_init_send(rtostr(Glib_canvas)+"; [glClearColor,1.0,1.0,1.0,1.0]");
/* GL_COLOR_BUFFER_BIT 0x4000, = 16384 */
glib_init_send(rtostr(Glib_canvas)+"; [glClear,(int)16384]");
glib_flush();
}
def glib_remove_last() {
Glib_canvas = cfep.getGlib_canvas();
glib_send(rtostr(Glib_canvas)+";meta_removeLast");
}
def glib_set_pixel_size(P) {
Glib_canvas = cfep.getGlib_canvas();
P = deval(P);
glib_send(rtostr(Glib_canvas)+"; [glPointSize,"+rtostr(P)+"]");
}
/* png_send("showFile, /Users/nobuki/OpenXM_tmp/1/work0.png"); */
def png_send(S) {
/* S=S+"\n"; do not add \n */
N=length(strtoascii(S))+1;
C=cfep.t_encode("{10<"+rtostr(N)+" "+S+" >}");
ctrl("hex",1);
output(); ctrl("hex",0);
print(C); output(); /* channel 10 is for png. cf. MyDecoder.h */
}
def png_form(Obj) {
R = print_png_form2(Obj); /* asir-contrib must be loaded */
if (length(R) <2) error("The png file is not generated.");
S="showFile, "+R[1];
png_send(S);
}
endmodule;
Cfep_loaded = 1$
Cfep_use_opengl = 0$ /* disable OpenGL */
cfep.setup()$
/*
cfep.draw_obj2(0,0,[0,0,400,400],0x0000ff);
cfep.draw_obj(0,0,[0,0,200,100]);
cfep.draw_flush(0);
*/
/* Initialization file for cfep.
This file is loaded after ASIRRC and ./.asirrc is executed from ox_texmacs.
cf. ox_texmacs --asirInitFile filename, MyEnvironment.m of cfep.
module_definedp("cfep");
#define x11toGLx(x) (-1.0+2*x/Glib_canvas_x)
#define x11toGLy(y) (1.0-2*y/Glib_canvas_y)
*/
/* Should be moved to the buildin function. */
def radian(D) {
return (D/180)*@pi;
}
/* Add a path for asir_contrib_update library. */
ctrl("loadpath",cons(getenv("HOME")+"/.OpenXM_tmp/OpenXM/lib/asir-contrib",ctrl("loadpath")))$
/* $OpenXM: OpenXM/src/asir-contrib/packages/src/cfep-init.rr,v 1.9 2019/06/06 04:33:11 takayama Exp $ */
end$