
/*
 *
 * by Noritsugu Nakamura
 * from 2001.04.05
 */

#include "ruby.h"
#include "rubyio.h"

#include "../tgifdefs.h"
#include "../color.e"
#include "../pattern.e"
#include "../raster.e"
#include "../text.e"

#include "tgif_lib.h"

static VALUE mTgif;

static VALUE
rb_ext_Tgif_new()
{
  if (!InitTgifInterface()) exit(-1);
}

static VALUE
/*rb_ext_Tgif_open(VALUE self, VALUE fullname, VALUE objfile, VALUE gzippedobjfile)*/
rb_ext_Tgif_open(VALUE self, VALUE filename)
{
  /*LoadFile(STR2CSTR(fullname), TRUE, FALSE);*/
  TgifLoadFile(STR2CSTR(filename));

  return Qnil;
}

static VALUE
rb_ext_Tgif_delete()
{
  CleanUp();
  CleanUpPaperSize();

  return Qnil;
}


static VALUE
rb_ext_Tgif_out(VALUE self, VALUE file, VALUE type)
{
  OpenFile *fptr;

  Check_Type(file, T_FILE); 
  GetOpenFile(file, fptr);
  rb_io_check_writable(fptr);
  /*TgifGenerate(stdout, STR2CSTR(type));*/
  TgifOutObj(fptr->f, STR2CSTR(type));

  return Qnil;
}

static VALUE
rb_ext_Tgif_dump(VALUE self, VALUE file, VALUE type)
{
  TgifOutPS(STR2CSTR(file), STR2CSTR(type));

  return Qnil;
}

static VALUE
rb_ext_Tgif_add_page_before(VALUE self)
{
  AddPageBefore();
  return Qnil;
}

static VALUE
rb_ext_Tgif_add_page_after(VALUE self)
{
  AddPageAfter();
  return Qnil;
}


static VALUE
rb_ext_Tgif_box(VALUE self, VALUE ltx, VALUE lty, VALUE rbx, VALUE rby)
{
  float cltx, clty, crbx, crby;

  cltx = (float)NUM2DBL(ltx); clty = (float)NUM2DBL(lty);
  crbx = (float)NUM2DBL(rbx); crby = (float)NUM2DBL(rby);
  TgifBox(&cltx, &clty, &crbx, &crby, 0);

  return Qnil;
}

static VALUE
rb_ext_Tgif_rcbox(VALUE self, VALUE x, VALUE y, VALUE rx, VALUE ry)
{
  float ltx, lty, rbx, rby;

  ltx = (float)NUM2DBL(x); lty = (float)NUM2DBL(y);
  rbx = (float)NUM2DBL(rx); rby = (float)NUM2DBL(ry);
  TgifRCBox(&ltx, &lty, &rbx, &rby, 0);

  return Qnil;
}

static VALUE
rb_ext_Tgif_oval(VALUE self, VALUE lx, VALUE ly, VALUE rx, VALUE ry)
{
  float ltx, lty, rbx, rby;

  ltx = (float)NUM2DBL(lx); lty = (float)NUM2DBL(ly);
  rbx = (float)NUM2DBL(rx); rby = (float)NUM2DBL(ry);
  TgifOval(&ltx, &lty, &rbx, &rby);

  return Qnil;
}

static VALUE
rb_ext_Tgif_start_vertex(VALUE self, VALUE x, VALUE y)
{
  float cx, cy;

  cx = (float)NUM2DBL(x); cy = (float)NUM2DBL(y);
  TgifStartVertex(&cx, &cy);

  return Qnil;
}

static VALUE
rb_ext_Tgif_add_vertex(VALUE self, VALUE x, VALUE y)
{
  float cx, cy;

  cx = (float)NUM2DBL(x); cy = (float)NUM2DBL(y);
  TgifAddVertex(&cx, &cy);

  return Qnil;
}

static VALUE
rb_ext_Tgif_poly(VALUE self)
{
  TgifPoly();

  return Qnil;
}

static VALUE
rb_ext_Tgif_polygon(VALUE self)
{
  TgifPolygon();

  return Qnil;
}

static VALUE
rb_ext_Tgif_arc(VALUE self, VALUE xc, VALUE yc, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE dir,
				VALUE ltx, VALUE lty, VALUE w, VALUE h, VALUE angle1, VALUE angle2)
{
  float cxc, cyc, cx1, cy1, cx2, cy2, cdir, cltx, clty, cw, ch, cangle1, cangle2;

  TgifArc(&cxc, &cyc, &cx1, &cy1, &cx2, &cy2, &cdir, &cltx, &clty, &cw, &ch, &cangle1, &cangle2);

  return Qnil;
}

static VALUE
rb_ext_Tgif_text(VALUE self, VALUE x, VALUE y, VALUE str, VALUE font_name, VALUE font_size)
{
  float cx, cy;

  cx = (float)NUM2DBL(x);
  cy = (float)NUM2DBL(y);
  TgifText(&cx, &cy, STR2CSTR(font_name), NUM2INT(font_size), STR2CSTR(str));

  return Qnil;
}

static VALUE
rb_ext_Tgif_import_gif(VALUE self, VALUE path, VALUE x, VALUE y, VALUE w, VALUE h, VALUE gamma, VALUE fit)
{
  float cx, cy, cw, ch;

  cx = (float)NUM2DBL(x);
  cy = (float)NUM2DBL(y);
  cw = (float)NUM2DBL(w);
  ch = (float)NUM2DBL(h);
  TgifImportGIF(STR2CSTR(path), &cx, &cy, &cw, &ch, STR2CSTR(gamma), NUM2INT(fit));

  return Qnil;
}

static VALUE
rb_ext_Tgif_save_state(VALUE self)
{
  TgifSaveState();

  return Qnil;
}

static VALUE
rb_ext_Tgif_restore_state(VALUE self)
{
  TgifRestoreState();

  return Qnil;
}


static VALUE
rb_ext_Tgif_import_jpeg(VALUE self, VALUE path, VALUE x, VALUE y, VALUE w, VALUE h, VALUE gamma, VALUE fit)
{
  float cx, cy, cw, ch;

  cx = (float)NUM2DBL(x);
  cy = (float)NUM2DBL(y);
  cw = (float)NUM2DBL(w);
  ch = (float)NUM2DBL(h);
  TgifImportJPEG(STR2CSTR(path), &cx, &cy, &cw, &ch, STR2CSTR(gamma), NUM2INT(fit));

  return Qnil;
}

/* attribute */
static VALUE
rb_ext_Tgif_text_just(VALUE self, VALUE just)
{
  int cjust;

  cjust = NUM2INT(just);
  if (cjust > 2) {
  } else {
	textJust = cjust;
  }
  return Qnil;
}

static VALUE
rb_ext_Tgif_line_dash(VALUE self, VALUE dash)
{

  curDash = NUM2INT(dash);
  return Qnil;
}

static VALUE
rb_ext_Tgif_line_style(VALUE self, VALUE style)
{

  lineStyle = NUM2INT(style);
  return Qnil;
}

static VALUE
rb_ext_Tgif_line_type(VALUE self, VALUE type)
{
  int ctype;

  ctype = NUM2INT(type);
  if (ctype >= MAXLINETYPES) {
	
  } else {
	curSpline = ctype;
  }
  return Qnil;
}

static VALUE
rb_ext_Tgif_line_width(VALUE self, VALUE width)
{
  int cwidth;

  cwidth = NUM2INT(width);
  if ((cwidth >= 0) && (cwidth < maxLineWidths)) {
	lineWidth = cwidth;
  } else {
	rb_raise(rb_eArgError, "line width is out of range");
  }

  return Qnil;
}

static VALUE
rb_ext_Tgif_arrow_width(VALUE self, VALUE width)
{
  int cwidth;

  cwidth = NUM2INT(width);
#if 0
  curWidthOfLine[lineWidth] = cwidth;
#endif

  return Qnil;
}

static VALUE
rb_ext_Tgif_fill(VALUE self, VALUE fill)
{
  int cfill;

  cfill = NUM2INT(fill);
  if (cfill >= MAXPATTERNS) {
	
  } else {
	objFill = cfill;
  }

  return Qnil;
}

static VALUE
rb_ext_Tgif_pen(VALUE self, VALUE pen)
{
  int cpen;

  cpen = NUM2INT(pen);
  if (cpen >= MAXPATTERNS) {
	
  } else {
	penPat = cpen;
  }

  return Qnil;
}

static VALUE
rb_ext_Tgif_color(VALUE self, VALUE color)
{
  int new_alloc = FALSE;

  colorIndex = QuickFindColorIndex(NULL, STR2CSTR(color), &new_alloc, TRUE);

  return Qnil;
}



void
Init_tgif(){
  mTgif = rb_define_module("Tgif");

  rb_define_module_function(mTgif, "new",    rb_ext_Tgif_new, 0);
  rb_define_module_function(mTgif, "open",   rb_ext_Tgif_open, 1);
  rb_define_module_function(mTgif, "delete", rb_ext_Tgif_delete, 0);
  rb_define_module_function(mTgif, "out",    rb_ext_Tgif_out, 2);
  rb_define_module_function(mTgif, "dump",   rb_ext_Tgif_dump, 2);

  /* page */
  rb_define_module_function(mTgif, "add_page_before", rb_ext_Tgif_add_page_before, 0);
  rb_define_module_function(mTgif, "add_page_after",  rb_ext_Tgif_add_page_after, 0);

  /* object */
  rb_define_module_function(mTgif, "box",          rb_ext_Tgif_box, 4);
  rb_define_module_function(mTgif, "rcbox",        rb_ext_Tgif_rcbox, 4);
  rb_define_module_function(mTgif, "oval",         rb_ext_Tgif_oval, 4);
  rb_define_module_function(mTgif, "start_vertex", rb_ext_Tgif_start_vertex, 2);
  rb_define_module_function(mTgif, "add_vertex",   rb_ext_Tgif_add_vertex, 2);
  rb_define_module_function(mTgif, "poly",         rb_ext_Tgif_poly, 0);
  rb_define_module_function(mTgif, "polygon",      rb_ext_Tgif_polygon, 0);
  rb_define_module_function(mTgif, "arc",          rb_ext_Tgif_arc, 13);
  rb_define_module_function(mTgif, "text",         rb_ext_Tgif_text, 5);

  rb_define_module_function(mTgif, "import_gif",   rb_ext_Tgif_import_gif, 7);
  rb_define_module_function(mTgif, "import_jpeg",  rb_ext_Tgif_import_jpeg, 7);

  rb_define_module_function(mTgif, "save_state",    rb_ext_Tgif_save_state, 0);
  rb_define_module_function(mTgif, "restore_state", rb_ext_Tgif_restore_state, 0);

  /* attribute */
  rb_define_module_function(mTgif, "text_just",  rb_ext_Tgif_text_just, 1);

  rb_define_module_function(mTgif, "line_dash",  rb_ext_Tgif_line_dash, 1);
  rb_define_module_function(mTgif, "line_style", rb_ext_Tgif_line_style, 1);
  rb_define_module_function(mTgif, "line_type",  rb_ext_Tgif_line_type, 1);
  rb_define_module_function(mTgif, "line_width", rb_ext_Tgif_line_width, 1);

  rb_define_module_function(mTgif, "arrow_width", rb_ext_Tgif_arrow_width, 1);

  rb_define_module_function(mTgif, "fill", rb_ext_Tgif_fill, 1);
  rb_define_module_function(mTgif, "pen",  rb_ext_Tgif_pen, 1);

  rb_define_module_function(mTgif, "color", rb_ext_Tgif_color, 1);

  /* constant */
  rb_define_const(mTgif, "LS_PLAIN",  INT2FIX(LS_PLAIN));
  rb_define_const(mTgif, "LS_RIGHT",  INT2FIX(LS_RIGHT));
  rb_define_const(mTgif, "LS_LEFT",   INT2FIX(LS_LEFT));
  rb_define_const(mTgif, "LS_DOUBLE", INT2FIX(LS_DOUBLE));

  rb_define_const(mTgif, "LT_STRAIGHT",  INT2FIX(LT_STRAIGHT));
  rb_define_const(mTgif, "LT_SPLINE",    INT2FIX(LT_SPLINE));
  rb_define_const(mTgif, "LT_INTSPLINE", INT2FIX(LT_INTSPLINE));

  rb_define_const(mTgif, "NONEPAT",  INT2FIX(NONEPAT));
  rb_define_const(mTgif, "SOLIDPAT",  INT2FIX(SOLIDPAT));
  rb_define_const(mTgif, "BACKPAT",  INT2FIX(BACKPAT));
  rb_define_const(mTgif, "SCROLLPAT",  INT2FIX(SCROLLPAT));

  rb_define_const(mTgif, "JUST_L", INT2FIX(JUST_L));
  rb_define_const(mTgif, "JUST_C", INT2FIX(JUST_C));
  rb_define_const(mTgif, "JUST_R", INT2FIX(JUST_R));
}
