From 25e85e1b809ec58ecac0f2e8fe48f74836f8e131 Mon Sep 17 00:00:00 2001
From: Pixel <pixel@nobis-crew.org>
Date: Tue, 15 Jun 2010 00:18:43 -0700
Subject: Upgrading to CD 5.3

---
 cd/src/lua5/cdlua5.c        | 81 +++++++++++++++++++++++++++++----------------
 cd/src/lua5/cdlua5_canvas.c | 65 ++++++++++++++++++++++++++++++++++--
 cd/src/lua5/cdlua5ctx.c     | 22 +++++++++++-
 cd/src/lua5/cdluaim5.c      | 43 +++++++++++++++++++-----
 4 files changed, 172 insertions(+), 39 deletions(-)

(limited to 'cd/src/lua5')

diff --git a/cd/src/lua5/cdlua5.c b/cd/src/lua5/cdlua5.c
index 7e60fbe..cd7554f 100755
--- a/cd/src/lua5/cdlua5.c
+++ b/cd/src/lua5/cdlua5.c
@@ -782,6 +782,13 @@ static int cdlua5_newindexchannel(lua_State *L)
   return 0;
 }
 
+static int cdl_ischar(const char* str, char c)
+{
+  if ((str[0] == c || str[0] == c-32) && str[1] == 0)
+    return 1;
+  return 0;
+}
+
 /***************************************************************************\
 * imagergb "gettable" fallback. This fallback is called when a LUA line     *
 * like "c = imagergb.r[y*w + x]" or "imagergb.r[y*w + x] = c" is executed.  *
@@ -794,16 +801,22 @@ static int cdlua5_indeximagergb(lua_State *L)
   cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 1);
   const char *index_s = luaL_checkstring(L, 2);
 
-  if (*index_s == 'r' || *index_s == 'R')
+  if (cdl_ischar(index_s, 'r'))
     channel = imagergb_p->red;
-  else if (*index_s == 'g' || *index_s == 'G')
+  else if (cdl_ischar(index_s, 'g'))
     channel = imagergb_p->green;
-  else if (*index_s == 'b' || *index_s == 'B')
+  else if (cdl_ischar(index_s, 'b'))
     channel = imagergb_p->blue;
-  else
-    luaL_argerror(L, 2, "index is an invalid channel name");
 
-  cdlua_pushchannel(L, channel, imagergb_p->size);
+  if (channel)
+    cdlua_pushchannel(L, channel, imagergb_p->size);
+  else
+  {
+    /* get raw method */
+    lua_getmetatable(L, 1);
+    lua_pushvalue(L, 2);
+    lua_rawget(L, -2);
+  }
 
   return 1;
 }
@@ -820,18 +833,24 @@ static int cdlua5_indeximagergba(lua_State *L)
   cdluaImageRGBA* imagergba_p = cdlua_checkimagergba(L, 1);
   const char *index_s = luaL_checkstring(L, 2);
 
-  if (*index_s == 'r' || *index_s == 'R')
+  if (cdl_ischar(index_s, 'r'))
     channel = imagergba_p->red;
-  else if (*index_s == 'g' || *index_s == 'G')
+  else if (cdl_ischar(index_s, 'g'))
     channel = imagergba_p->green;
-  else if (*index_s == 'b' || *index_s == 'B')
+  else if (cdl_ischar(index_s, 'b'))
     channel = imagergba_p->blue;
-  else if (*index_s == 'a' || *index_s == 'A')
+  else if (cdl_ischar(index_s, 'a'))
     channel = imagergba_p->alpha;
-  else
-    luaL_argerror(L, 2, "index is an invalid channel name");
 
-  cdlua_pushchannel(L, channel, imagergba_p->size);
+  if (channel)
+    cdlua_pushchannel(L, channel, imagergba_p->size);
+  else
+  {
+    /* get raw method */
+    lua_getmetatable(L, 1);
+    lua_pushvalue(L, 2);
+    lua_rawget(L, -2);
+  }
 
   return 1;
 }
@@ -845,31 +864,36 @@ static int cdlua5_indeximagergba(lua_State *L)
 static int cdlua5_indexbitmap(lua_State *L)
 {
   unsigned char* channel = NULL;
-
   cdBitmap* bitmap = cdlua_checkbitmap(L, 1);
   const char *index_s = luaL_checkstring(L, 2);
 
   int size = bitmap->w * bitmap->h;
 
-  if (*index_s == 'r' || *index_s == 'R')
+  if (cdl_ischar(index_s, 'r'))
     channel = cdBitmapGetData(bitmap, CD_IRED);
-  else if (*index_s == 'g' || *index_s == 'G')
+  else if (cdl_ischar(index_s, 'g'))
     channel = cdBitmapGetData(bitmap, CD_IGREEN);
-  else if (*index_s == 'b' || *index_s == 'B')
+  else if (cdl_ischar(index_s, 'b'))
     channel = cdBitmapGetData(bitmap, CD_IBLUE);
-  else if (*index_s == 'a' || *index_s == 'A')
+  else if (cdl_ischar(index_s, 'a'))
     channel = cdBitmapGetData(bitmap, CD_IALPHA);
-  else if (*index_s == 'i' || *index_s == 'I')
+  else if (cdl_ischar(index_s, 'i'))
     channel = cdBitmapGetData(bitmap, CD_INDEX);
-  else if (*index_s == 'c' || *index_s == 'C') 
+  else if (cdl_ischar(index_s, 'c')) 
   {
     channel = cdBitmapGetData(bitmap, CD_COLORS);
     size = -1;
   }
-  else
-    luaL_argerror(L, 2, "index is an invalid channel name");
 
-  cdlua_pushchannel(L, channel, size);
+  if (channel)
+    cdlua_pushchannel(L, channel, size);
+  else
+  {
+    /* get raw method */
+    lua_getmetatable(L, 1);
+    lua_pushvalue(L, 2);
+    lua_rawget(L, -2);
+  }
 
   return 1;
 }
@@ -900,7 +924,7 @@ static int cdlua5_version(lua_State *L)
 \***************************************************************************/
 static int cdlua5_registercallback(lua_State *L)
 {
-  int cb_i, func_lock;
+  int cb_i, func_lock, ret = CD_ERROR;
   cdluaCallback* cdCB;
   cdluaContext* cdlua_ctx;
 
@@ -926,7 +950,7 @@ static int cdlua5_registercallback(lua_State *L)
     cdCB->lock = func_lock;
     if (func_lock == -1)
     {
-      cdContextRegisterCallback(cdlua_ctx->ctx(), cb_i, NULL);
+      ret = cdContextRegisterCallback(cdlua_ctx->ctx(), cb_i, NULL);
     }
   }
   else
@@ -934,10 +958,11 @@ static int cdlua5_registercallback(lua_State *L)
     if (func_lock != -1)
     {
       cdContextRegisterCallback(cdlua_ctx->ctx(), cb_i, (cdCallback)cdCB->func);
-      cdCB->lock = func_lock;
+      ret = cdCB->lock = func_lock;
     }
   }
-  return 0;
+  lua_pushnumber(L, ret);
+  return 1;
 }
 
 
@@ -1257,7 +1282,7 @@ static int cdlua5_getscreensize(lua_State *L)
 \***************************************************************************/
 static int cdlua5_usecontextplus(lua_State *L)
 {
-  lua_pushnumber(L, cdUseContextPlus(luaL_checkint(L, 1)));
+  lua_pushboolean(L, cdUseContextPlus(lua_toboolean(L, 1)));
   return 1;
 }
 
diff --git a/cd/src/lua5/cdlua5_canvas.c b/cd/src/lua5/cdlua5_canvas.c
index d04f76f..f3b929d 100755
--- a/cd/src/lua5/cdlua5_canvas.c
+++ b/cd/src/lua5/cdlua5_canvas.c
@@ -273,6 +273,12 @@ static int cdlua5_updateyaxis(lua_State *L)
   return 1;
 }
 
+static int cdlua5_yaxismode(lua_State *L)
+{
+  lua_pushnumber(L, cdCanvasYAxisMode(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2)));
+  return 1;
+}
+
 static int cdlua5_invertyaxis(lua_State *L)
 {
   lua_pushnumber(L, cdfCanvasInvertYAxis(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2)));
@@ -464,6 +470,56 @@ static int cdlua5_ftransformpoint(lua_State *L)
 * World Coordinates                                                         *
 \***************************************************************************/
 
+/***************************************************************************\
+* wd.GetTransform() -> (sx, sy, tx, ty: number)                        *
+\***************************************************************************/
+static int wdlua5_gettransform(lua_State *L)
+{
+  double sx, sy, tx, ty;
+
+  wdCanvasGetTransform(cdlua_checkcanvas(L, 1), &sx, &sy, &tx, &ty);
+  lua_pushnumber(L, sx);
+  lua_pushnumber(L, sy);
+  lua_pushnumber(L, tx);
+  lua_pushnumber(L, ty);
+  return 4;
+}
+
+/***************************************************************************\
+* wd.SetTransform(sx, sy, tx, ty: number)                                 *
+\***************************************************************************/
+static int wdlua5_settransform(lua_State *L)
+{
+  double sx = luaL_checknumber(L, 2);
+  double sy = luaL_checknumber(L, 3);
+  double tx = luaL_checknumber(L, 4);
+  double ty = luaL_checknumber(L, 5);
+  wdCanvasSetTransform(cdlua_checkcanvas(L, 1), sx, sy, tx, ty);
+  return 0;
+}
+
+/***************************************************************************\
+* wd.Translate(tx, ty: number)                                 *
+\***************************************************************************/
+static int wdlua5_translate(lua_State *L)
+{
+  double tx = luaL_checknumber(L, 2);
+  double ty = luaL_checknumber(L, 3);
+  wdCanvasTranslate(cdlua_checkcanvas(L, 1), tx, ty);
+  return 0;
+}
+
+/***************************************************************************\
+* wd.Scale(sx, sy: number)                                 *
+\***************************************************************************/
+static int wdlua5_scale(lua_State *L)
+{
+  double sx = luaL_checknumber(L, 2);
+  double sy = luaL_checknumber(L, 3);
+  wdCanvasScale(cdlua_checkcanvas(L, 1), sx, sy);
+  return 0;
+}
+
 /***************************************************************************\
 * wd.Window(xmin, xmax, ymin, ymax: number)                                 *
 \***************************************************************************/
@@ -712,7 +768,7 @@ static int cdlua5_regioncombinemode(lua_State *L)
 \***************************************************************************/
 static int  cdlua5_pointinregion(lua_State *L)
 {
-  lua_pushnumber(L, cdCanvasIsPointInRegion(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3)));
+  lua_pushboolean(L, cdCanvasIsPointInRegion(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3)));
   return 1;
 }
 
@@ -721,7 +777,7 @@ static int  cdlua5_pointinregion(lua_State *L)
 \***************************************************************************/
 static int wdlua5_pointinregion(lua_State *L)
 {
-  lua_pushnumber(L, wdCanvasIsPointInRegion(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)));
+  lua_pushboolean(L, wdCanvasIsPointInRegion(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)));
   return 1;
 }
 
@@ -2192,6 +2248,7 @@ static const struct luaL_reg cdlib_canvas_meta[] = {
   /* Coordinate System */
   {"GetSize"       , cdlua5_getcanvassize},
   {"UpdateYAxis"   , cdlua5_updateyaxis},
+  {"YAxisMode"     , cdlua5_yaxismode},
   {"InvertYAxis"   , cdlua5_invertyaxis},
   {"MM2Pixel"      , cdlua5_mm2pixel},
   {"Pixel2MM"      , cdlua5_pixel2mm},
@@ -2217,6 +2274,10 @@ static const struct luaL_reg cdlib_canvas_meta[] = {
   {"wGetViewport"   , wdlua5_getviewport},  
   {"wWorld2Canvas"  , wdlua5_world2canvas},
   {"wCanvas2World"  , wdlua5_canvas2world},
+  {"wGetTransform"  , wdlua5_gettransform},
+  {"wSetTransform"  , wdlua5_settransform},
+  {"wScale"         , wdlua5_scale},
+  {"wTranslate"     , wdlua5_translate},
 
   {"wHardcopy"      , wdlua5_hardcopy},
 
diff --git a/cd/src/lua5/cdlua5ctx.c b/cd/src/lua5/cdlua5ctx.c
index e3bd19e..4d97f3b 100755
--- a/cd/src/lua5/cdlua5ctx.c
+++ b/cd/src/lua5/cdlua5ctx.c
@@ -1,5 +1,5 @@
 /***************************************************************************\
-* $Id: cdlua5ctx.c,v 1.1 2008/10/17 06:10:42 scuri Exp $
+* $Id: cdlua5ctx.c,v 1.2 2009/12/02 20:31:02 scuri Exp $
 *                                                                           *
 \***************************************************************************/
 
@@ -23,6 +23,7 @@
 #include "cdclipbd.h"
 #include "cdmf.h"
 #include "cdps.h"
+#include "cdsvg.h"
 #include "cddbuf.h"
 #include "cdgdiplus.h"
 
@@ -687,6 +688,24 @@ static cdluaContext cdluapsctx =
   0
 };
 
+/***************************************************************************\
+* CD_SVG.                                                                    *
+\***************************************************************************/
+static void *cdsvg_checkdata( lua_State *L, int param)
+{
+  return (void *)luaL_checkstring(L, param);
+}
+
+static cdluaContext cdluasvgctx = 
+{
+  0,
+  "SVG",
+  cdContextSVG,
+  cdsvg_checkdata,
+  NULL,
+  0
+};
+
 /***************************************************************************\
 * CD_PRINTER.                                                               *
 \***************************************************************************/
@@ -793,6 +812,7 @@ void cdlua_initdrivers(lua_State * L, cdluaLuaState* cdL)
   cdlua_addcontext(L, cdL, &cdluacgmctx);
   cdlua_addcontext(L, cdL, &cdluamfctx);
   cdlua_addcontext(L, cdL, &cdluapsctx);
+  cdlua_addcontext(L, cdL, &cdluasvgctx);
   cdlua_addcontext(L, cdL, &cdluaclipboardctx);
   cdlua_addcontext(L, cdL, &cdluanativewindowctx);
   cdlua_addcontext(L, cdL, &cdluaprinterctx);
diff --git a/cd/src/lua5/cdluaim5.c b/cd/src/lua5/cdluaim5.c
index 77ffc4f..815cd0f 100755
--- a/cd/src/lua5/cdluaim5.c
+++ b/cd/src/lua5/cdluaim5.c
@@ -37,7 +37,12 @@ static int imlua_cdInitBitmap(lua_State *L)
     luaL_argerror(L, 1, "image is not a bitmap");
 
   if (image->color_space == IM_RGB)
-    bitmap = cdInitBitmap(image->width, image->height, CD_RGB, image->data[0], image->data[1], image->data[2]);
+  {
+    if (image->has_alpha)
+      bitmap = cdInitBitmap(image->width, image->height, CD_RGBA, image->data[0], image->data[1], image->data[2], image->data[3]);
+    else
+      bitmap = cdInitBitmap(image->width, image->height, CD_RGB, image->data[0], image->data[1], image->data[2]);
+  }
   else
     bitmap = cdInitBitmap(image->width, image->height, CD_MAP, image->data[0], image->palette);
 
@@ -60,7 +65,12 @@ static int imlua_cdCreateBitmap(lua_State *L)
     luaL_argerror(L, 1, "image is not a bitmap");
 
   if (image->color_space == IM_RGB)
-    bitmap = cdCreateBitmap(image->width, image->height, CD_RGB);
+  {
+    if (image->has_alpha)
+      bitmap = cdCreateBitmap(image->width, image->height, CD_RGBA);
+    else
+      bitmap = cdCreateBitmap(image->width, image->height, CD_RGB);
+  }
   else
     bitmap = cdCreateBitmap(image->width, image->height, CD_MAP);
 
@@ -72,6 +82,9 @@ static int imlua_cdCreateBitmap(lua_State *L)
     memcpy(cdBitmapGetData(bitmap, CD_IRED), image->data[0], image->plane_size);
     memcpy(cdBitmapGetData(bitmap, CD_IGREEN), image->data[1], image->plane_size);
     memcpy(cdBitmapGetData(bitmap, CD_IBLUE), image->data[2], image->plane_size);
+
+    if (image->has_alpha)
+      memcpy(cdBitmapGetData(bitmap, CD_IALPHA), image->data[3], image->plane_size);
   }
   else
   {
@@ -91,7 +104,7 @@ static int cdlua_imImageCreate(lua_State *L)
   imImage *image;
   cdBitmap* bitmap = cdlua_checkbitmap(L, 1);
 
-  if (bitmap->type == CD_RGB)
+  if (bitmap->type == CD_RGB || bitmap->type == CD_RGBA)
     image = imImageCreate(bitmap->w, bitmap->h, IM_RGB, IM_BYTE);
   else
     image = imImageCreate(bitmap->w, bitmap->h, IM_MAP, IM_BYTE);
@@ -99,11 +112,14 @@ static int cdlua_imImageCreate(lua_State *L)
   if (!image)
     luaL_error(L, "insuficient memory to create image");
 
-  if (bitmap->type == CD_RGB)
+  if (bitmap->type == CD_RGB || bitmap->type == CD_RGBA)
   {
     memcpy(image->data[0], cdBitmapGetData(bitmap, CD_IRED),   image->plane_size);
     memcpy(image->data[1], cdBitmapGetData(bitmap, CD_IGREEN), image->plane_size);
     memcpy(image->data[2], cdBitmapGetData(bitmap, CD_IBLUE),  image->plane_size);
+
+    if (bitmap->type == CD_RGBA)
+      memcpy(image->data[3], cdBitmapGetData(bitmap, CD_IALPHA),  image->plane_size);
   }
   else
   {
@@ -192,16 +208,27 @@ static int imlua_cdCreateCanvas(lua_State * L)
 
   imImage *image = imlua_checkimage(L, 1);
 
+  if (image->color_space != IM_RGB || image->data_type != IM_BYTE)
+    luaL_argerror(L, 1, "image is not RGB/byte");
+
   if (lua_isnoneornil(L, 2))
   {
-    sprintf(data_s, "%dx%d %p %p %p", image->width, image->height,
-      image->data[0], image->data[1], image->data[2]);
+    if (image->has_alpha)
+      sprintf(data_s, "%dx%d %p %p %p %p -a", image->width, image->height,
+                                              image->data[0], image->data[1], image->data[2], image->data[3]);
+    else
+      sprintf(data_s, "%dx%d %p %p %p", image->width, image->height,
+                                        image->data[0], image->data[1], image->data[2]);
   }
   else
   {
     double res_f = luaL_checknumber(L, 2);
-    sprintf(data_s, "%dx%d %p %p %p -r%g", image->width, image->height,
-      image->data[0], image->data[1], image->data[2], res_f);
+    if (image->has_alpha)
+      sprintf(data_s, "%dx%d %p %p %p %p -r%g -a", image->width, image->height,
+                                                   image->data[0], image->data[1], image->data[2], image->data[3], res_f);
+    else
+      sprintf(data_s, "%dx%d %p %p %p -r%g", image->width, image->height,
+                                             image->data[0], image->data[1], image->data[2], res_f);
   }
 
   canvas = cdCreateCanvas(CD_IMAGERGB, data_s);
-- 
cgit v1.2.3