From d577d991b97ae2b5ee1af23641bcffc3f83af5b2 Mon Sep 17 00:00:00 2001 From: Pixel Date: Wed, 4 Nov 2009 11:56:41 -0800 Subject: Initial import. Contains the im, cd and iup librairies, and a "working" Makefile for them under linux. --- cd/src/lua5/cdlua5.c | 1823 ++++++++++++++++++++++++++++ cd/src/lua5/cdlua5.def | 13 + cd/src/lua5/cdlua5_active.c | 2163 +++++++++++++++++++++++++++++++++ cd/src/lua5/cdlua5_canvas.c | 2405 +++++++++++++++++++++++++++++++++++++ cd/src/lua5/cdlua5ctx.c | 802 +++++++++++++ cd/src/lua5/cdluacontextplus5.c | 44 + cd/src/lua5/cdluacontextplus5.def | 4 + cd/src/lua5/cdluaim5.c | 265 ++++ cd/src/lua5/cdluaim5.def | 4 + cd/src/lua5/cdluapdf5.c | 53 + cd/src/lua5/cdluapdf5.def | 4 + cd/src/lua5/cdvoid5.c | 130 ++ cd/src/lua5/cdvoid5.h | 18 + 13 files changed, 7728 insertions(+) create mode 100755 cd/src/lua5/cdlua5.c create mode 100755 cd/src/lua5/cdlua5.def create mode 100755 cd/src/lua5/cdlua5_active.c create mode 100755 cd/src/lua5/cdlua5_canvas.c create mode 100755 cd/src/lua5/cdlua5ctx.c create mode 100755 cd/src/lua5/cdluacontextplus5.c create mode 100755 cd/src/lua5/cdluacontextplus5.def create mode 100755 cd/src/lua5/cdluaim5.c create mode 100755 cd/src/lua5/cdluaim5.def create mode 100755 cd/src/lua5/cdluapdf5.c create mode 100755 cd/src/lua5/cdluapdf5.def create mode 100755 cd/src/lua5/cdvoid5.c create mode 100755 cd/src/lua5/cdvoid5.h (limited to 'cd/src/lua5') diff --git a/cd/src/lua5/cdlua5.c b/cd/src/lua5/cdlua5.c new file mode 100755 index 0000000..7e60fbe --- /dev/null +++ b/cd/src/lua5/cdlua5.c @@ -0,0 +1,1823 @@ +/** \file + * \brief Lua Binding + * + * See Copyright Notice in cd.h + */ + +#include +#include +#include + +#include "cd.h" +#include "cdgdiplus.h" +#include "cdnative.h" +#include "cdps.h" + +#include +#include + +#include "cdlua.h" +#include "cdlua5_private.h" + + +/***************************************************************************\ +* Initialization * +\***************************************************************************/ + +static const char* cdlua_key = "cdlua5"; + +static void cdlua_SetState(lua_State * L, cdluaLuaState* cdL) +{ + lua_pushlightuserdata(L, (void*)cdlua_key); + lua_pushlightuserdata(L, (void*)cdL); + lua_settable(L, LUA_REGISTRYINDEX); /* registry[address("cdlua5")]=cdL */ + lua_pop(L, 1); +} + +cdluaLuaState* cdlua_getstate(lua_State * L) +{ + cdluaLuaState* cdL; + lua_pushlightuserdata(L, (void*)cdlua_key); + lua_gettable(L, LUA_REGISTRYINDEX); + cdL = (cdluaLuaState*)lua_touserdata(L, -1); + lua_pop(L, 1); + return cdL; +} + +cdluaContext* cdlua_getcontext(lua_State * L, int param) +{ + cdluaLuaState* cdL = cdlua_getstate(L); + + int driver = luaL_checkint(L, param); + if ((driver < 0) || (driver >= cdL->numdrivers)) + luaL_argerror(L, param, "unknown driver"); + + return cdL->drivers[driver]; +} + +static lua_State* cdlua5_play_luaState = NULL; + +lua_State* cdlua_getplaystate(void) +{ + return cdlua5_play_luaState; +} + +void cdlua_setplaystate(lua_State* L) +{ + cdlua5_play_luaState = L; +} + +static cdluaPalette* cdlua_rawcheckpalette(lua_State *L, int param) +{ + void *p = lua_touserdata(L, param); + if (p != NULL) { /* value is a userdata? */ + if (lua_getmetatable(L, param)) { /* does it have a metatable? */ + lua_getfield(L, LUA_REGISTRYINDEX, "cdPalette"); /* get correct metatable */ + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ + lua_pop(L, 2); /* remove both metatables */ + return (cdluaPalette*)p; + } + lua_pop(L, 1); /* remove previous metatable */ + + /* check also for IM palette */ + lua_getfield(L, LUA_REGISTRYINDEX, "imPalette"); /* get correct metatable */ + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ + lua_pop(L, 2); /* remove both metatables */ + return (cdluaPalette*)p; + } + } + } + luaL_typerror(L, param, "cdPalette"); /* else error */ + return NULL; /* to avoid warnings */ +} + +cdluaPalette * cdlua_checkpalette(lua_State * L, int param) +{ + cdluaPalette * pal = cdlua_rawcheckpalette(L, param); + if (!pal->color) + luaL_argerror(L, param, "killed cdPalette"); + return pal; +} + +void cdlua_pushpalette(lua_State* L, long* palette, int size) +{ + cdluaPalette* pal = (cdluaPalette*)lua_newuserdata(L, sizeof(cdluaPalette)); + luaL_getmetatable(L, "cdPalette"); + lua_setmetatable(L, -2); + + pal->count = size; + pal->color = palette; +} + +cdState * cdlua_checkstate(lua_State* L, int param) +{ + cdState** state_p = (cdState**)luaL_checkudata(L, param, "cdState"); + if (!(*state_p)) + luaL_argerror(L, param, "released cdState"); + return *state_p; +} + +void cdlua_pushstate(lua_State* L, cdState* state) +{ + cdState** state_p = (cdState**)lua_newuserdata(L, sizeof(cdState*)); + luaL_getmetatable(L, "cdState"); + lua_setmetatable(L, -2); + + *state_p = state; +} + +cdluaPattern* cdlua_checkpattern(lua_State* L, int param) +{ + cdluaPattern* pattern_p = (cdluaPattern*) luaL_checkudata(L, param, "cdPattern"); + if (!pattern_p->pattern) + luaL_argerror(L, param, "killed cdPattern"); + return pattern_p; +} + +void cdlua_pushpattern(lua_State* L, long int* pattern, int width, int height) +{ + cdluaPattern* pattern_p = (cdluaPattern*)lua_newuserdata(L, sizeof(cdluaPattern)); + luaL_getmetatable(L, "cdPattern"); + lua_setmetatable(L, -2); + + pattern_p->pattern = pattern; + pattern_p->width = width; + pattern_p->height = height; + pattern_p->size = width * height; +} + +cdluaStipple* cdlua_checkstipple(lua_State* L, int param) +{ + cdluaStipple* stipple_p = (cdluaStipple*) luaL_checkudata(L, param, "cdStipple"); + if (!stipple_p->stipple) + luaL_argerror(L, param, "killed cdStipple"); + return stipple_p; +} + +void cdlua_pushstipple(lua_State* L, unsigned char* stipple, int width, int height) +{ + cdluaStipple* stipple_p = (cdluaStipple*)lua_newuserdata(L, sizeof(cdluaStipple)); + luaL_getmetatable(L, "cdStipple"); + lua_setmetatable(L, -2); + + stipple_p->stipple = stipple; + stipple_p->width = width; + stipple_p->height = height; + stipple_p->size = width * height; +} + +cdluaImageRGB* cdlua_checkimagergb(lua_State* L, int param) +{ + cdluaImageRGB* imagergb_p = (cdluaImageRGB*) luaL_checkudata(L, param, "cdImageRGB"); + if (!imagergb_p->red) + luaL_argerror(L, param, "killed cdImageRGB"); + return imagergb_p; +} + +void cdlua_pushimagergb(lua_State* L, unsigned char* red, unsigned char* green, unsigned char* blue, int width, int height) +{ + cdluaImageRGB* imagergb_p = (cdluaImageRGB*)lua_newuserdata(L, sizeof(cdluaImageRGB)); + luaL_getmetatable(L, "cdImageRGB"); + lua_setmetatable(L, -2); + + imagergb_p->red = red; + imagergb_p->green = green; + imagergb_p->blue = blue; + imagergb_p->width = width; + imagergb_p->height = height; + imagergb_p->size = width * height; + imagergb_p->free = 1; +} + +void cdlua_pushimagergb_ex(lua_State* L, unsigned char* red, unsigned char* green, unsigned char* blue, int width, int height) +{ + cdluaImageRGB* imagergb_p = (cdluaImageRGB*)lua_newuserdata(L, sizeof(cdluaImageRGB)); + luaL_getmetatable(L, "cdImageRGB"); + lua_setmetatable(L, -2); + + imagergb_p->red = red; + imagergb_p->green = green; + imagergb_p->blue = blue; + imagergb_p->width = width; + imagergb_p->height = height; + imagergb_p->size = width * height; + imagergb_p->free = 0; +} + +cdluaImageRGBA* cdlua_checkimagergba(lua_State* L, int param) +{ + cdluaImageRGBA* imagergba_p = (cdluaImageRGBA*) luaL_checkudata(L, param, "cdImageRGBA"); + if (!imagergba_p->red) + luaL_argerror(L, param, "killed cdImageRGBA"); + return imagergba_p; +} + +void cdlua_pushimagergba(lua_State* L, unsigned char* red, unsigned char* green, unsigned char* blue, unsigned char* alpha, int width, int height) +{ + cdluaImageRGBA* imagergba_p = (cdluaImageRGBA*)lua_newuserdata(L, sizeof(cdluaImageRGBA)); + luaL_getmetatable(L, "cdImageRGBA"); + lua_setmetatable(L, -2); + + imagergba_p->red = red; + imagergba_p->green = green; + imagergba_p->blue = blue; + imagergba_p->alpha = alpha; + imagergba_p->width = width; + imagergba_p->height = height; + imagergba_p->size = width * height; + imagergba_p->free = 1; +} + +void cdlua_pushimagergba_ex(lua_State* L, unsigned char* red, unsigned char* green, unsigned char* blue, unsigned char* alpha, int width, int height) +{ + cdluaImageRGBA* imagergba_p = (cdluaImageRGBA*)lua_newuserdata(L, sizeof(cdluaImageRGBA)); + luaL_getmetatable(L, "cdImageRGBA"); + lua_setmetatable(L, -2); + + imagergba_p->red = red; + imagergba_p->green = green; + imagergba_p->blue = blue; + imagergba_p->alpha = alpha; + imagergba_p->width = width; + imagergba_p->height = height; + imagergba_p->size = width * height; + imagergba_p->free = 0; +} + +cdluaImageMap* cdlua_checkimagemap(lua_State* L, int param) +{ + cdluaImageMap* imagemap_p = (cdluaImageMap*) luaL_checkudata(L, param, "cdImageMap"); + if (!imagemap_p->index) + luaL_argerror(L, param, "killed cdImageMap"); + return imagemap_p; +} + +void cdlua_pushimagemap(lua_State* L, unsigned char* index, int width, int height) +{ + cdluaImageMap* imagemap_p = (cdluaImageMap*)lua_newuserdata(L, sizeof(cdluaImageMap)); + luaL_getmetatable(L, "cdImageMap"); + lua_setmetatable(L, -2); + + imagemap_p->index = index; + imagemap_p->width = width; + imagemap_p->height = height; + imagemap_p->size = width * height; +} + +cdluaImageChannel* cdlua_checkchannel(lua_State* L, int param) +{ + cdluaImageChannel* channel_p = (cdluaImageChannel*) luaL_checkudata(L, param, "cdImageChannel"); + if (!channel_p->channel) + luaL_argerror(L, param, "killed cdImageChannel"); + return channel_p; +} + +void cdlua_pushchannel(lua_State* L, unsigned char* channel, int size) +{ + cdluaImageChannel* channel_p = (cdluaImageChannel*)lua_newuserdata(L, sizeof(cdluaImageChannel)); + luaL_getmetatable(L, "cdImageChannel"); + lua_setmetatable(L, -2); + + channel_p->channel = channel; + channel_p->size = size; +} + +long cdlua_checkcolor(lua_State* L, int param) +{ + if (!lua_islightuserdata(L, param)) + { + if (lua_isnumber(L, param) && (lua_tointeger(L, param) == CD_QUERY)) + return CD_QUERY; + + luaL_argerror(L, param, "invalid color, must be a light user data"); + } + + return (long int)lua_touserdata(L, param); +} + +cdImage* cdlua_checkimage(lua_State* L, int param) +{ + cdImage** image_p = (cdImage**)luaL_checkudata(L, param, "cdImage"); + if (!(*image_p)) + luaL_argerror(L, param, "killed cdImage"); + return *image_p; +} + +void cdlua_pushimage(lua_State* L, cdImage* image) +{ + cdImage** image_p = (cdImage**)lua_newuserdata(L, sizeof(cdImage*)); + luaL_getmetatable(L, "cdImage"); + lua_setmetatable(L, -2); + + *image_p = image; +} + +cdBitmap * cdlua_checkbitmap(lua_State* L, int param) +{ + cdBitmap** bitmap_p = (cdBitmap**)luaL_checkudata(L, param, "cdBitmap"); + if (!(*bitmap_p)) + luaL_argerror(L, param, "killed cdBitmap"); + return *bitmap_p; +} + +void cdlua_pushbitmap(lua_State* L, cdBitmap* bitmap) +{ + cdBitmap** bitmap_p = (cdBitmap**)lua_newuserdata(L, sizeof(cdBitmap*)); + luaL_getmetatable(L, "cdBitmap"); + lua_setmetatable(L, -2); + + *bitmap_p = bitmap; +} + +/***************************************************************************\ +* cd.ContextCaps(ctx: number) -> (caps: number) * +\***************************************************************************/ +static int cdlua5_contextcaps(lua_State * L) +{ + cdluaContext* cdlua_ctx = cdlua_getcontext(L, 1); + lua_pushnumber(L, cdContextCaps(cdlua_ctx->ctx())); + return 1; +} + +static int cdlua5_releasestate(lua_State * L) +{ + cdState* *state_p = (cdState* *) luaL_checkudata(L, 1, "cdState"); + if (*state_p) + { + cdReleaseState(*state_p); + *state_p = NULL; /* mark as released */ + } + + return 0; +} + +static int cdlua5_createstipple(lua_State *L) +{ + int size; + unsigned char* stipple; + + int width = luaL_checkint(L, 1); + int height = luaL_checkint(L, 2); + + if (width < 1 || height < 1) + luaL_argerror(L, 1, "stipple dimensions should be positive integers"); + + size = width * height; + stipple = (unsigned char *) malloc(size); + memset(stipple, '\0', size); + + cdlua_pushstipple(L, stipple, width, height); + + return 1; +} + +static int cdlua5_killstipple(lua_State *L) +{ + cdluaStipple *stipple_p = (cdluaStipple*)luaL_checkudata(L, 1, "cdStipple"); + if (stipple_p->stipple) + { + free(stipple_p->stipple); + stipple_p->stipple = NULL; /* mark as killed */ + } + + return 0; +} + +/***************************************************************************\ +* number = stipple[i] * +\***************************************************************************/ +static int cdlua5_indexstipple(lua_State *L) +{ + cdluaStipple* stipple_p = cdlua_checkstipple(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || index >= stipple_p->size) + luaL_argerror(L, 2, "index is out of bounds"); + + lua_pushnumber(L, stipple_p->stipple[index]); + return 1; +} + +/***************************************************************************\ +* stipple[i] = number . * +\***************************************************************************/ +static int cdlua5_newindexstipple(lua_State *L) +{ + unsigned char value; + + cdluaStipple* stipple_p = cdlua_checkstipple(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || index >= stipple_p->size) + luaL_argerror(L, 2, "index is out of bounds"); + + value = (unsigned char)luaL_checkint(L, 3); + if ((value != 0 && value != 1)) + luaL_argerror(L, 3, "value must be 0 or 1"); + + stipple_p->stipple[index] = value; + return 0; +} + +static int cdlua5_createpattern(lua_State *L) +{ + int size; + long int *pattern; + + int width = luaL_checkint(L, 1); + int height = luaL_checkint(L, 2); + + if (width < 1 || height < 1) + luaL_argerror(L, 1, "pattern dimensions should be positive integers"); + + size = width * height; + pattern = (long int *) malloc(size * sizeof(long int)); + memset(pattern, 255, size * sizeof(long int)); + + cdlua_pushpattern(L, pattern, width, height); + + return 1; +} + +static int cdlua5_killpattern(lua_State *L) +{ + cdluaPattern *pattern_p = (cdluaPattern *) luaL_checkudata(L, 1, "cdPattern"); + if (pattern_p->pattern) + { + free(pattern_p->pattern); + pattern_p->pattern = NULL; /* mark as killed */ + } + + return 0; +} + +/***************************************************************************\ +* color = pattern[i] * +\***************************************************************************/ +static int cdlua5_indexpattern(lua_State *L) +{ + cdluaPattern* pattern_p = cdlua_checkpattern(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || index >= pattern_p->size) + luaL_argerror(L, 2, "index is out of bounds"); + + lua_pushlightuserdata(L, (void *) pattern_p->pattern[index]); + return 1; +} + +/***************************************************************************\ +* pattern[i] = color * +\***************************************************************************/ +static int cdlua5_newindexpattern(lua_State *L) +{ + long int color; + + cdluaPattern* pattern_p = cdlua_checkpattern(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || index >= pattern_p->size) + luaL_argerror(L, 2, "index is out of bounds"); + + color = cdlua_checkcolor(L, 3); + + pattern_p->pattern[index] = color; + return 0; +} + +static int cdlua5_rgb2map(lua_State *L) +{ + cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 1); + cdluaImageMap* imagemap_p = cdlua_checkimagemap(L, 2); + cdluaPalette* pal = cdlua_checkpalette(L, 3); + cdRGB2Map(imagergb_p->width, imagergb_p->height, + imagergb_p->red, imagergb_p->green, imagergb_p->blue, + imagemap_p->index, pal->count, pal->color); + return 0; +} + +static int cdlua5_createbitmap(lua_State *L) +{ + cdBitmap *bitmap; + + int width = luaL_checkint(L, 1); + int height = luaL_checkint(L, 2); + int type = luaL_checkint(L, 3); + + if (width < 1 || height < 1) + luaL_argerror(L, 1, "bitmap dimensions should be positive integers"); + + bitmap = cdCreateBitmap(width, height, type); + if (bitmap) + cdlua_pushbitmap(L, bitmap); + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_killbitmap(lua_State *L) +{ + cdBitmap* *bitmap_p = (cdBitmap* *) luaL_checkudata(L, 1, "cdBitmap"); + if (*bitmap_p) + { + cdKillBitmap(*bitmap_p); + *bitmap_p = NULL; /* mark as killed */ + } + + return 0; +} + +static int cdlua5_bitmapgetdata(lua_State *L) +{ + cdBitmap* bitmap = cdlua_checkbitmap(L, 1); + int dataptr = luaL_checkint(L, 2); + + unsigned char *data = cdBitmapGetData(bitmap, dataptr); + if (data) + cdlua_pushchannel(L, data, bitmap->w * bitmap->h); + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_bitmapsetrect(lua_State *L) +{ + cdBitmap* bitmap = cdlua_checkbitmap(L, 1); + int xmin = (int) luaL_checkint(L, 2); + int xmax = (int) luaL_checkint(L, 3); + int ymin = (int) luaL_checkint(L, 4); + int ymax = (int) luaL_checkint(L, 5); + + cdBitmapSetRect(bitmap, xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_bitmaprgb2map(lua_State *L) +{ + cdBitmap* bitmaprgb = cdlua_checkbitmap(L, 1); + cdBitmap* bitmapmap = cdlua_checkbitmap(L, 2); + + if (bitmaprgb->type != CD_RGB) + luaL_argerror(L, 1, "invalid bitmap type, must be RGB"); + if (bitmapmap->type != CD_MAP) + luaL_argerror(L, 2, "invalid bitmap type, must be Map"); + + cdBitmapRGB2Map(bitmaprgb, bitmapmap); + return 0; +} + +static int cdlua5_createimagergb(lua_State * L) +{ + unsigned char *red, *green, *blue; + int size; + int width = luaL_checkint(L,1); + int height = luaL_checkint(L,2); + + if (width < 1 || height < 1) + luaL_argerror(L, 1, "image dimensions should be positive integers"); + + size = width*height; + red = (unsigned char*)malloc(3*size); + + if (red) + { + memset(red, 255, 3*size); /* white */ + green = red + size; + blue = red + 2*size; + cdlua_pushimagergb(L, red, green, blue, width, height); + } + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_killimagergb(lua_State *L) +{ + cdluaImageRGB* imagergb_p = (cdluaImageRGB*)luaL_checkudata(L, 1, "cdImageRGB"); + if (imagergb_p->red && imagergb_p->free) + { + free(imagergb_p->red); + imagergb_p->red = NULL; /* mark as killed */ + imagergb_p->green = NULL; + imagergb_p->blue = NULL; + } + + return 0; +} + +static int cdlua5_createimagergba(lua_State * L) +{ + unsigned char *red, *green, *blue, *alpha; + int size; + int width = luaL_checkint(L,1); + int height = luaL_checkint(L,2); + + if (width < 1 || height < 1) + luaL_argerror(L, 1, "image dimensions should be positive integers"); + + size = width*height; + red = (unsigned char*)malloc(4*size); + + if (red) + { + memset(red, 255, 3*size); /* white */ + green = red + size; + blue = red + 2*size; + alpha = red + 3*size; + memset(alpha, 0, size); /* transparent */ + cdlua_pushimagergba(L, red, green, blue, alpha, width, height); + } + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_killimagergba(lua_State *L) +{ + cdluaImageRGBA* imagergba_p = (cdluaImageRGBA*)luaL_checkudata(L, 1, "cdImageRGBA"); + if (imagergba_p->red && imagergba_p->free) + { + free(imagergba_p->red); + imagergba_p->red = NULL; /* mark as killed */ + imagergba_p->green = NULL; + imagergba_p->blue = NULL; + imagergba_p->alpha = NULL; + } + + return 0; +} + +static int cdlua5_createimagemap(lua_State *L) +{ + int size; + unsigned char *index; + int width = luaL_checkint(L,1); + int height = luaL_checkint(L,2); + + if (width < 1 || height < 1) + luaL_argerror(L, 1, "imagemap dimensions should be positive integers"); + + size = width * height; + index = (unsigned char *) malloc(size); + + if (index) + { + memset(index, 0, size); + cdlua_pushimagemap(L, index, width, height); + } + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_killimagemap(lua_State *L) +{ + cdluaImageMap *imagemap_p = (cdluaImageMap *) luaL_checkudata(L, 1, "cdImageMap"); + if (imagemap_p->index) + { + free(imagemap_p->index); + imagemap_p->index = NULL; /* mark as killed */ + } + + return 0; +} + +/***************************************************************************\ +* number = imagemap[i] * +\***************************************************************************/ +static int cdlua5_indeximagemap(lua_State *L) +{ + cdluaImageMap* imagemap_p = cdlua_checkimagemap(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || index >= imagemap_p->size) + luaL_argerror(L, 2, "index is out of bounds"); + + lua_pushnumber(L, imagemap_p->index[index]); + return 1; +} + +/***************************************************************************\ +* imagemap[i] = number * +\***************************************************************************/ +static int cdlua5_newindeximagemap(lua_State *L) +{ + int value; + + cdluaImageMap* imagemap_p = cdlua_checkimagemap(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || index >= imagemap_p->size) + luaL_argerror(L, 2, "index is out of bounds"); + + value = luaL_checkint(L, 3); + if ((value < 0 || value > 255)) + luaL_argerror(L, 3, "value should be in range [0, 255]"); + + imagemap_p->index[index] = (unsigned char) value; + return 0; +} + +/***************************************************************************\ +* channel "gettable" fallback. This fallback is called when a LUA line like * +* "c = imagergb.r[y*w + x]" is executed. The imagergb "gettable" fallback * +* fills and returns a channel structure with info about the buffer. This * +* structure is consulted and the appropriate value is returned. * +\***************************************************************************/ +static int cdlua5_indexchannel(lua_State *L) +{ + cdluaImageChannel* channel_p = cdlua_checkchannel(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || + (channel_p->size > 0 && index >= channel_p->size) || + (channel_p->size == -1 && index >= 256)) { + luaL_argerror(L, 2, "index is out of bounds"); + } + + if (channel_p->size == -1) /* COLORS */ + lua_pushlightuserdata(L, (void *)((long int*)channel_p->channel)[index]); + else + lua_pushnumber(L, channel_p->channel[index]); + + return 1; +} + +/***************************************************************************\ +* channel "settable" fallback. This fallback is called when a LUA line like * +* "imagergb.r[y*w + x] = c" is executed. The imagergb "gettable" fallback * +* fills and returns a channel structure with info about the buffer. This * +* structure is consulted and the value is assigned where it should. * +\***************************************************************************/ +static int cdlua5_newindexchannel(lua_State *L) +{ + int value; + + cdluaImageChannel* channel_p = cdlua_checkchannel(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || + (channel_p->size > 0 && index >= channel_p->size) || + (channel_p->size == -1 && index >= 256)) { + luaL_argerror(L, 2, "index is out of bounds"); + } + + if (channel_p->size > 0) + { + value = luaL_checkint(L, 3); + if ((value < 0 || value > 255)) + luaL_argerror(L, 3, "value should be in range [0, 255]"); + channel_p->channel[index] = (unsigned char) value; + } + else /* COLORS */ + { + value = (long int) cdlua_checkcolor(L, 3); + ((long int*)channel_p->channel)[index] = value; + } + 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. * +* The following "gettable" or "settable" * +* then assigns or returns the appropriate value. * +\***************************************************************************/ +static int cdlua5_indeximagergb(lua_State *L) +{ + unsigned char* channel = NULL; + cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 1); + const char *index_s = luaL_checkstring(L, 2); + + if (*index_s == 'r' || *index_s == 'R') + channel = imagergb_p->red; + else if (*index_s == 'g' || *index_s == 'G') + channel = imagergb_p->green; + else if (*index_s == 'b' || *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); + + return 1; +} + +/***************************************************************************\ +* imagergba "gettable" fallback. This fallback is called when a LUA line * +* like "c = imagergba.r[y*w + x]" or "imagergba.r[y*w + x] = c" is executed.* +* The following "gettable" or "settable" * +* then assigns or returns the appropriate value. * +\***************************************************************************/ +static int cdlua5_indeximagergba(lua_State *L) +{ + unsigned char* channel = NULL; + cdluaImageRGBA* imagergba_p = cdlua_checkimagergba(L, 1); + const char *index_s = luaL_checkstring(L, 2); + + if (*index_s == 'r' || *index_s == 'R') + channel = imagergba_p->red; + else if (*index_s == 'g' || *index_s == 'G') + channel = imagergba_p->green; + else if (*index_s == 'b' || *index_s == 'B') + channel = imagergba_p->blue; + else if (*index_s == 'a' || *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); + + return 1; +} + +/***************************************************************************\ +* bitmap "gettable" fallback. This fallback is called when a LUA line * +* like "c = bitmap.r[y*w + x]" or "bitmap.r[y*w + x] = c" is executed. * +* The following "gettable" or "settable" * +* then assigns or returns the appropriate value. * +\***************************************************************************/ +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') + channel = cdBitmapGetData(bitmap, CD_IRED); + else if (*index_s == 'g' || *index_s == 'G') + channel = cdBitmapGetData(bitmap, CD_IGREEN); + else if (*index_s == 'b' || *index_s == 'B') + channel = cdBitmapGetData(bitmap, CD_IBLUE); + else if (*index_s == 'a' || *index_s == 'A') + channel = cdBitmapGetData(bitmap, CD_IALPHA); + else if (*index_s == 'i' || *index_s == 'I') + channel = cdBitmapGetData(bitmap, CD_INDEX); + else if (*index_s == 'c' || *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); + + return 1; +} + +static int cdlua5_killimage(lua_State *L) +{ + cdImage* *image_p = (cdImage* *) luaL_checkudata(L, 1, "cdImage"); + if (*image_p) + { + cdKillImage(*image_p); + *image_p = NULL; /* mark as killed */ + } + return 0; +} + +/***************************************************************************\ +* cd.Version() -> (version: string) * +\***************************************************************************/ +static int cdlua5_version(lua_State *L) +{ + lua_pushstring(L, cdVersion()); + return 1; +} + +/***************************************************************************\ +* Register callback functions. * +* cd.ContextRegisterCallback(ctx, cb: number, func: function) -> (status: number) * +\***************************************************************************/ +static int cdlua5_registercallback(lua_State *L) +{ + int cb_i, func_lock; + cdluaCallback* cdCB; + cdluaContext* cdlua_ctx; + + cdlua_ctx = cdlua_getcontext(L, 1); + + cb_i = luaL_checkint(L, 2); + if (cb_i >= cdlua_ctx->cb_n) + luaL_argerror(L, 2, "invalid callback parameter"); + + if (lua_isnil(L, 3)) + func_lock = -1; + else if (!lua_isfunction(L, 3)) + luaL_argerror(L, 3, "invalid function parameter"); + else + lua_pushvalue(L, 3); + func_lock = lua_ref(L, 1); + + cdCB = &cdlua_ctx->cb_list[cb_i]; + + if (cdCB->lock != -1) + { + lua_unref(L,cdCB->lock); + cdCB->lock = func_lock; + if (func_lock == -1) + { + cdContextRegisterCallback(cdlua_ctx->ctx(), cb_i, NULL); + } + } + else + { + if (func_lock != -1) + { + cdContextRegisterCallback(cdlua_ctx->ctx(), cb_i, (cdCallback)cdCB->func); + cdCB->lock = func_lock; + } + } + return 0; +} + + + +/***************************************************************************\ +* Color Coding * +\***************************************************************************/ + +/***************************************************************************\ +* Creates a color as a light userdata. The color value is * +* placed in the (void *) value. Not beautiful, but works best. * +* cd.EncodeColor(r, g, b: number) -> (old_color: color) * +\***************************************************************************/ +static int cdlua5_encodecolor(lua_State *L) +{ + int red_f, green_f, blue_f; + unsigned char red_i, green_i, blue_i; + long int color; + + red_f = luaL_checkint(L, 1); + green_f = luaL_checkint(L, 2); + blue_f = luaL_checkint(L, 3); + + if (red_f < 0 || red_f > 255) + luaL_argerror(L, 1, "color components values should be in range [0, 255]"); + if (green_f < 0 || green_f > 255) + luaL_argerror(L, 2, "color components values should be in range [0, 255]"); + if (blue_f < 0 || blue_f > 255) + luaL_argerror(L, 3, "color components values should be in range [0, 255]"); + + red_i = (unsigned char) (red_f); + green_i = (unsigned char) (green_f); + blue_i = (unsigned char) (blue_f); + + color = cdEncodeColor(red_i, green_i, blue_i); + lua_pushlightuserdata(L, (void *)color); + + return 1; +} + +/***************************************************************************\ +* Decodes a color previously created. * +* cd.DecodeColor(color: color) -> (r, g, b: number) * +\***************************************************************************/ +static int cdlua5_decodecolor(lua_State *L) +{ + unsigned char red_i, green_i, blue_i; + long int color = cdlua_checkcolor(L, 1); + cdDecodeColor(color, &red_i, &green_i, &blue_i); + lua_pushnumber(L, red_i); + lua_pushnumber(L, green_i); + lua_pushnumber(L, blue_i); + + return 3; +} + +/***************************************************************************\ +* cd.EncodeAlpha(color: color_tag, alpha: number) -> (color: color) * +\***************************************************************************/ +static int cdlua5_encodealpha(lua_State *L) +{ + float alpha_f; + unsigned char alpha_i; + long int color; + + color = cdlua_checkcolor(L, 1); + + if (!lua_isnumber(L, 2)) + luaL_argerror(L, 2, "invalid alpha parameter"); + + alpha_f = (float) lua_tonumber(L, 2); + + if (alpha_f < 0 || alpha_f > 255) + luaL_argerror(L, 2, "alpha components values should be in range [0, 255]"); + + alpha_i = (unsigned char) (alpha_f); + + color = cdEncodeAlpha(color, alpha_i); + lua_pushlightuserdata(L, (void *) color); + return 1; +} + +/***************************************************************************\ +* cd.DecodeAlpha(color: color) -> (a: number) * +\***************************************************************************/ +static int cdlua5_decodealpha(lua_State* L) +{ + long int color = cdlua_checkcolor(L, 1); + unsigned char alpha_i = cdDecodeAlpha(color); + lua_pushnumber(L, alpha_i); + return 1; +} + +/***************************************************************************\ +* cd.Alpha(color: color) -> (r: number) * +\***************************************************************************/ +static int cdlua5_alpha(lua_State* L) +{ + long int color = cdlua_checkcolor(L, 1); + lua_pushnumber(L, cdAlpha(color)); + return 1; +} + +/***************************************************************************\ +* cd.Red(color: color) -> (r: number) * +\***************************************************************************/ +static int cdlua5_red(lua_State* L) +{ + long int color = cdlua_checkcolor(L, 1); + lua_pushnumber(L, cdRed(color)); + return 1; +} + +/***************************************************************************\ +* cd.Blue(color: color) -> (r: number) * +\***************************************************************************/ +static int cdlua5_blue(lua_State *L) +{ + long int color = cdlua_checkcolor(L, 1); + lua_pushnumber(L, cdBlue(color)); + return 1; +} + +/***************************************************************************\ +* cd.Green(color: color) -> (r: number) * +\***************************************************************************/ +static int cdlua5_green(lua_State *L) +{ + long int color = cdlua_checkcolor(L, 1); + lua_pushnumber(L, cdGreen(color)); + return 1; +} + +static int cdlua5_createpalette(lua_State *L) +{ + int size_i; + long int *palette; + + size_i = luaL_checkint(L, 1); + if (size_i < 1) + luaL_argerror(L, 1, "palette size should be a positive integer"); + + palette = (long int *) malloc(256 * sizeof(long int)); + memset(palette, 0, 256 * sizeof(long int)); + + cdlua_pushpalette(L, palette, size_i); + + return 1; +} + +static int cdlua5_killpalette(lua_State *L) +{ + cdluaPalette* pal = (cdluaPalette *)luaL_checkudata(L, 1, "cdPalette"); + if (pal->color) + { + free(pal->color); + pal->color = NULL; /* mark as killed */ + } + + return 0; +} + +/***************************************************************************\ +* color = palette[i] * +\***************************************************************************/ +static int cdlua5_indexpalette(lua_State *L) +{ + cdluaPalette* pal = cdlua_checkpalette(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || index >= pal->count) + luaL_argerror(L, 2, "index is out of bounds"); + + lua_pushlightuserdata(L, (void*) pal->color[index]); + return 1; +} + +/***************************************************************************\ +* palette[i] = color * +\***************************************************************************/ +static int cdlua5_newindexpalette(lua_State *L) +{ + long int color; + cdluaPalette* pal = cdlua_checkpalette(L, 1); + + int index = luaL_checkint(L, 2); + if (index < 0 || index >= pal->count) + luaL_argerror(L, 2, "index is out of bounds"); + + color = cdlua_checkcolor(L, 3); + + pal->color[index] = color; + return 0; +} + +/*****************************************************************************\ + len +\*****************************************************************************/ +static int cdluaPalette_len(lua_State *L) +{ + cdluaPalette *pal = (cdluaPalette*)lua_touserdata(L, 1); + lua_pushinteger(L, pal->count); + return 1; +} + +/*****************************************************************************\ + tostring +\*****************************************************************************/ +static int cdlua5_tostringpalette (lua_State *L) +{ + cdluaPalette *pal = (cdluaPalette*)lua_touserdata(L, 1); + lua_pushfstring(L, "cdPalette(%p)%s", pal, (pal->color)? "": "-killed"); + return 1; +} + +static int cdlua5_tostringimage (lua_State *L) +{ + cdImage* *image_p = (cdImage**)lua_touserdata(L, 1); + lua_pushfstring(L, "cdImage(%p)%s", image_p, (*image_p)? "": "-killed"); + return 1; +} + +static int cdlua5_tostringbitmap (lua_State *L) +{ + cdBitmap* *bitmap_p = (cdBitmap**)lua_touserdata(L, 1); + lua_pushfstring(L, "cdBitmap(%p)%s", bitmap_p, (*bitmap_p)? "": "-killed"); + return 1; +} + +static int cdlua5_tostringchannel (lua_State *L) +{ + cdluaImageChannel *imagechannel_p = (cdluaImageChannel*)lua_touserdata(L, 1); + lua_pushfstring(L, "cdImageChannel(%p)%s", imagechannel_p, (imagechannel_p->channel)? "": "-killed"); + return 1; +} + +static int cdlua5_tostringstate (lua_State *L) +{ + cdState* *state_p = (cdState**)lua_touserdata(L, 1); + lua_pushfstring(L, "cdState(%p)%s", state_p, (*state_p)? "": "-released"); + return 1; +} + +static int cdlua5_tostringpattern (lua_State *L) +{ + cdluaPattern *pattern_p = (cdluaPattern*)lua_touserdata(L, 1); + lua_pushfstring(L, "cdPattern(%p)%s", pattern_p, (pattern_p->pattern)? "": "-killed"); + return 1; +} + +static int cdlua5_tostringstipple (lua_State *L) +{ + cdluaStipple *stipple_p = (cdluaStipple*)lua_touserdata(L, 1); + lua_pushfstring(L, "cdStipple(%p)%s", stipple_p, (stipple_p->stipple)? "": "-killed"); + return 1; +} + +static int cdlua5_tostringimagergba (lua_State *L) +{ + cdluaImageRGBA *imagergba_p = (cdluaImageRGBA*)lua_touserdata(L, 1); + lua_pushfstring(L, "cdImageRGBA(%p)%s", imagergba_p, (imagergba_p->red)? "": "-killed"); + return 1; +} + +static int cdlua5_tostringimagergb (lua_State *L) +{ + cdluaImageRGB *imagergb_p = (cdluaImageRGB*)lua_touserdata(L, 1); + lua_pushfstring(L, "cdImageRGB(%p)%s", imagergb_p, (imagergb_p->red)? "": "-killed"); + return 1; +} + +static int cdlua5_tostringimagemap (lua_State *L) +{ + cdluaImageMap *imagemap_p = (cdluaImageMap*)lua_touserdata(L, 1); + lua_pushfstring(L, "cdImageMap(%p)%s", imagemap_p, (imagemap_p->index)? "": "-killed"); + return 1; +} + +/***************************************************************************\ +* cd.Reserved * +\***************************************************************************/ +static int cdlua5_reserved(lua_State *L) +{ + long int color = cdlua_checkcolor(L, 1); + lua_pushnumber(L, cdReserved(color)); + return 1; +} + +/***************************************************************************\ +* cd.GetScreenColorPlanes * +\***************************************************************************/ +static int cdlua5_getscreencolorplanes(lua_State *L) +{ + lua_pushnumber(L, cdGetScreenColorPlanes()); + return 1; +} + +/***************************************************************************\ +* cd.GetScreenSize * +\***************************************************************************/ +static int cdlua5_getscreensize(lua_State *L) +{ + int width; + int height; + double mm_width; + double mm_height; + cdGetScreenSize(&width, &height, &mm_width, &mm_height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + lua_pushnumber(L, mm_width); + lua_pushnumber(L, mm_height); + return 4; +} + +/***************************************************************************\ +* cd.UseContextPlus * +\***************************************************************************/ +static int cdlua5_usecontextplus(lua_State *L) +{ + lua_pushnumber(L, cdUseContextPlus(luaL_checkint(L, 1))); + return 1; +} + + +/********************************************************************************\ +* CDLua Exported functions * +\********************************************************************************/ +static const struct luaL_reg cdlib[] = { + + /* Initialization */ + {"ContextCaps" , cdlua5_contextcaps}, + + /* Control */ + {"ReleaseState" , cdlua5_releasestate}, + + /* Stipple */ + {"CreateStipple", cdlua5_createstipple}, + {"KillStipple" , cdlua5_killstipple}, + + /* Pattern */ + {"CreatePattern", cdlua5_createpattern}, + {"KillPattern" , cdlua5_killpattern}, + + /* Client Images */ + {"RGB2Map" , cdlua5_rgb2map}, + {"CreateBitmap" , cdlua5_createbitmap}, + {"KillBitmap" , cdlua5_killbitmap}, + {"BitmapGetData" , cdlua5_bitmapgetdata}, + {"BitmapSetRect" , cdlua5_bitmapsetrect}, + {"BitmapRGB2Map" , cdlua5_bitmaprgb2map}, + + {"CreateImageRGB" , cdlua5_createimagergb}, + {"KillImageRGB" , cdlua5_killimagergb}, + {"CreateImageRGBA" , cdlua5_createimagergba}, + {"KillImageRGBA" , cdlua5_killimagergba}, + {"CreateImageMap" , cdlua5_createimagemap}, + {"KillImageMap" , cdlua5_killimagemap}, + + /* Server Images */ + {"KillImage" , cdlua5_killimage}, + + /* Other */ + {"Version" , cdlua5_version}, + {"RegisterCallback" , cdlua5_registercallback}, + {"ContextRegisterCallback" , cdlua5_registercallback}, + + /* Color Coding */ + {"EncodeColor" , cdlua5_encodecolor}, + {"DecodeColor" , cdlua5_decodecolor}, + {"EncodeAlpha" , cdlua5_encodealpha}, + {"DecodeAlpha" , cdlua5_decodealpha}, + {"Alpha" , cdlua5_alpha}, + {"Red" , cdlua5_red}, + {"Blue" , cdlua5_blue}, + {"Green" , cdlua5_green}, + {"Reserved" , cdlua5_reserved}, + + /* Palette */ + {"CreatePalette", cdlua5_createpalette}, + {"KillPalette" , cdlua5_killpalette}, + + /* native window functions */ + {"GetScreenColorPlanes" , cdlua5_getscreencolorplanes}, + {"GetScreenSize" , cdlua5_getscreensize}, + + /* gdi+ functions */ + {"UseContextPlus" , cdlua5_usecontextplus}, + + {NULL, NULL}, +}; + +void cdlua_addcontext(lua_State *L, cdluaLuaState* cdL, cdluaContext *cdlua_ctx) +{ + int i; + cdlua_ctx->id = cdL->numdrivers; + cdL->drivers[cdL->numdrivers] = cdlua_ctx; + + lua_pushstring(L, cdlua_ctx->name); + lua_pushnumber(L, cdL->numdrivers); + lua_settable(L, -3); + + /* skip CD_SIZECB, register other callbacks */ + for (i=1; i < cdlua_ctx->cb_n; i++) + { + lua_pushstring(L, cdlua_ctx->cb_list[i].name); + lua_pushnumber(L, i); + lua_settable(L, -3); + } + + cdL->numdrivers++; +} + + +/********************************************************************************\ +* Exports all CD constants * +\********************************************************************************/ +typedef struct cdlua5_constant { + const char *name; + lua_Number value; +} cdlua5_constant; + +typedef struct cdlua5_color { + const char *name; + long int value; +} cdlua5_color; + +static const struct cdlua5_constant cdlibconstant[] = { + /* query value */ + {"QUERY", CD_QUERY}, + + /* these definitions are compatible with the IM library */ + {"RGB" , CD_RGB}, + {"MAP" , CD_MAP}, + {"RGBA", CD_RGBA}, + + {"IRED" , CD_IRED}, + {"IGREEN", CD_IGREEN}, + {"IBLUE" , CD_IBLUE}, + {"IALPHA", CD_IALPHA}, + {"INDEX" , CD_INDEX}, + {"COLORS", CD_COLORS}, + + /* status report */ + {"ERROR", CD_ERROR}, + {"OK" , CD_OK}, + + /* clip mode */ + {"CLIPOFF" , CD_CLIPOFF}, + {"CLIPAREA" , CD_CLIPAREA}, + {"CLIPPOLYGON", CD_CLIPPOLYGON}, + {"CLIPREGION" , CD_CLIPREGION}, + + /* region combine mode */ + {"UNION" , CD_UNION}, + {"INTERSECT" , CD_INTERSECT}, + {"DIFFERENCE" , CD_DIFFERENCE}, + {"NOTINTERSECT", CD_NOTINTERSECT}, + + /* polygon mode (begin...end) */ + {"FILL" , CD_FILL}, + {"OPEN_LINES" , CD_OPEN_LINES}, + {"CLOSED_LINES", CD_CLOSED_LINES}, + {"CLIP" , CD_CLIP}, + {"BEZIER" , CD_BEZIER}, + {"REGION" , CD_REGION}, + {"POLYCUSTOM" , CD_POLYCUSTOM}, + + /* fill mode */ + {"EVENODD", CD_EVENODD}, + {"WINDING", CD_WINDING}, + + /* line join */ + {"MITER", CD_MITER}, + {"BEVEL", CD_BEVEL}, + {"ROUND", CD_ROUND}, + + /* line cap */ + {"CAPFLAT" , CD_CAPFLAT}, + {"CAPSQUARE", CD_CAPSQUARE}, + {"CAPROUND" , CD_CAPROUND}, + + /* background opacity mode */ + {"OPAQUE" , CD_OPAQUE}, + {"TRANSPARENT", CD_TRANSPARENT}, + + /* write mode */ + {"REPLACE", CD_REPLACE}, + {"XOR" , CD_XOR}, + {"NOT_XOR", CD_NOT_XOR}, + + /* color allocation mode (palette) */ + {"POLITE", CD_POLITE}, + {"FORCE" , CD_FORCE}, + + /* line style */ + {"CONTINUOUS" , CD_CONTINUOUS}, + {"DASHED" , CD_DASHED}, + {"DOTTED" , CD_DOTTED}, + {"DASH_DOT" , CD_DASH_DOT}, + {"DASH_DOT_DOT", CD_DASH_DOT_DOT}, + {"CUSTOM" , CD_CUSTOM}, + + /* marker type */ + {"PLUS" , CD_PLUS}, + {"STAR" , CD_STAR}, + {"CIRCLE" , CD_CIRCLE}, + {"X" , CD_X}, + {"BOX" , CD_BOX}, + {"DIAMOND" , CD_DIAMOND}, + {"HOLLOW_CIRCLE" , CD_HOLLOW_CIRCLE}, + {"HOLLOW_BOX" , CD_HOLLOW_BOX}, + {"HOLLOW_DIAMOND", CD_HOLLOW_DIAMOND}, + + /* hatch type */ + {"HORIZONTAL", CD_HORIZONTAL}, + {"VERTICAL" , CD_VERTICAL}, + {"FDIAGONAL" , CD_FDIAGONAL}, + {"BDIAGONAL" , CD_BDIAGONAL}, + {"CROSS" , CD_CROSS}, + {"DIAGCROSS" , CD_DIAGCROSS}, + + /* interior style */ + {"SOLID" , CD_SOLID}, + {"HATCH" , CD_HATCH}, + {"STIPPLE", CD_STIPPLE}, + {"PATTERN", CD_PATTERN}, + {"HOLLOW" , CD_HOLLOW}, + + /* text alignment */ + {"NORTH" , CD_NORTH}, + {"SOUTH" , CD_SOUTH}, + {"EAST" , CD_EAST}, + {"WEST" , CD_WEST}, + {"NORTH_EAST" , CD_NORTH_EAST}, + {"NORTH_WEST" , CD_NORTH_WEST}, + {"SOUTH_EAST" , CD_SOUTH_EAST}, + {"SOUTH_WEST" , CD_SOUTH_WEST}, + {"CENTER" , CD_CENTER}, + {"BASE_LEFT" , CD_BASE_LEFT}, + {"BASE_CENTER", CD_BASE_CENTER}, + {"BASE_RIGHT" , CD_BASE_RIGHT}, + + /* style */ + {"PLAIN" , CD_PLAIN}, + {"BOLD" , CD_BOLD}, + {"ITALIC" , CD_ITALIC}, + {"BOLD_ITALIC", CD_BOLD_ITALIC}, + {"UNDERLINE" , CD_UNDERLINE}, + {"STRIKEOUT" , CD_STRIKEOUT}, + + /* font size */ + {"SMALL" , CD_SMALL}, + {"STANDARD", CD_STANDARD}, + {"LARGE" , CD_LARGE}, + + /* Canvas Capabilities */ + {"CAP_NONE" , CD_CAP_NONE}, + {"CAP_FLUSH" , CD_CAP_FLUSH}, + {"CAP_CLEAR" , CD_CAP_CLEAR}, + {"CAP_PLAY" , CD_CAP_PLAY}, + {"CAP_YAXIS" , CD_CAP_YAXIS}, + {"CAP_CLIPAREA" , CD_CAP_CLIPAREA}, + {"CAP_CLIPPOLY" , CD_CAP_CLIPPOLY}, + {"CAP_RECT" , CD_CAP_RECT}, + {"CAP_IMAGERGB" , CD_CAP_IMAGERGB}, + {"CAP_IMAGERGBA" , CD_CAP_IMAGERGBA}, + {"CAP_IMAGEMAP" , CD_CAP_IMAGEMAP}, + {"CAP_GETIMAGERGB" , CD_CAP_GETIMAGERGB}, + {"CAP_IMAGESRV" , CD_CAP_IMAGESRV}, + {"CAP_BACKGROUND" , CD_CAP_BACKGROUND}, + {"CAP_BACKOPACITY" , CD_CAP_BACKOPACITY}, + {"CAP_WRITEMODE" , CD_CAP_WRITEMODE}, + {"CAP_LINESTYLE" , CD_CAP_LINESTYLE}, + {"CAP_LINEWITH" , CD_CAP_LINEWITH}, + {"CAP_WD" , CD_CAP_FPRIMTIVES}, + {"CAP_HATCH" , CD_CAP_HATCH}, + {"CAP_STIPPLE" , CD_CAP_STIPPLE}, + {"CAP_PATTERN" , CD_CAP_PATTERN}, + {"CAP_FONT" , CD_CAP_FONT}, + {"CAP_FONTDIM" , CD_CAP_FONTDIM}, + {"CAP_TEXTSIZE" , CD_CAP_TEXTSIZE}, + {"CAP_TEXTORIENTATION", CD_CAP_TEXTORIENTATION}, + {"CAP_PALETTE" , CD_CAP_PALETTE}, + {"CAP_LINECAP" , CD_CAP_LINECAP}, + {"CAP_LINEJOIN" , CD_CAP_LINEJOIN}, + {"CAP_REGION" , CD_CAP_REGION}, + {"CAP_CHORD" , CD_CAP_CHORD}, + {"CAP_ALL" , CD_CAP_ALL}, + + /* cdPlay definitions */ + {"SIZECB", CD_SIZECB}, + {"ABORT", CD_ABORT}, + {"CONTINUE", CD_CONTINUE}, + + /* simulation flags */ + {"SIM_NONE" , CD_SIM_NONE}, + {"SIM_LINE" , CD_SIM_LINE}, + {"SIM_RECT" , CD_SIM_RECT}, + {"SIM_ARC" , CD_SIM_ARC}, + {"SIM_POLYLINE" , CD_SIM_POLYLINE}, + {"SIM_BOX" , CD_SIM_BOX}, + {"SIM_SECTOR" , CD_SIM_SECTOR}, + {"SIM_POLYGON" , CD_SIM_POLYGON}, + {"SIM_CHORD" , CD_SIM_CHORD}, + {"SIM_ALL" , CD_SIM_ALL}, + {"SIM_LINES" , CD_SIM_LINES}, + {"SIM_FILLS" , CD_SIM_FILLS}, + + /* some conversion factors */ + {"MM2PT" , CD_MM2PT}, + {"RAD2DEG", CD_RAD2DEG}, + + /* cdcgm.h (the callback names are registered in cdlua_addcontext) */ + + /* cdgdiplus.h */ + {"SPLINE" , CD_SPLINE}, + {"FILLSPLINE" , CD_FILLSPLINE}, + {"FILLGRADIENT", CD_FILLGRADIENT}, + + /* cdps.h */ + {"A0" , CD_A0}, + {"A1" , CD_A1}, + {"A2" , CD_A2}, + {"A3" , CD_A3}, + {"A4" , CD_A4}, + {"A5" , CD_A5}, + {"LETTER", CD_LETTER}, + {"LEGAL" , CD_LEGAL}, + + {NULL, -1}, +}; + +static void initconst(lua_State *L) +{ + const cdlua5_constant *l = cdlibconstant; + for (; l->name; l++) { + lua_pushstring(L, l->name); + lua_pushnumber(L, l->value); + lua_settable(L, -3); + } +} + +/* some predefined colors for convenience */ +static const struct cdlua5_color cdlibcolor[] = { + {"RED" , CD_RED}, + {"DARK_RED" , CD_DARK_RED}, + {"GREEN" , CD_GREEN}, + {"DARK_GREEN" , CD_DARK_GREEN}, + {"BLUE" , CD_BLUE}, + {"DARK_BLUE" , CD_DARK_BLUE}, + {"YELLOW" , CD_YELLOW}, + {"DARK_YELLOW" , CD_DARK_YELLOW}, + {"MAGENTA" , CD_MAGENTA}, + {"DARK_MAGENTA", CD_DARK_MAGENTA}, + {"CYAN" , CD_CYAN}, + {"DARK_CYAN" , CD_DARK_CYAN}, + {"WHITE" , CD_WHITE}, + {"BLACK" , CD_BLACK}, + {"DARK_GRAY" , CD_DARK_GRAY}, + {"GRAY" , CD_GRAY}, + {NULL, -1}, +}; + +static void initcolor(lua_State *L) +{ + const cdlua5_color *l = cdlibcolor; + for (; l->name; l++) + { + lua_pushstring(L, l->name); + lua_pushlightuserdata(L, (void*) l->value); + lua_settable(L, -3); + } +} + +static void initmetatables(lua_State *L) +{ + /* there is no object orientation for these metatables, + only gc and optionaly array access */ + + luaL_newmetatable(L, "cdState"); /* create new metatable for cdState handles */ + lua_pushliteral (L, "__gc"); + lua_pushcfunction (L, cdlua5_releasestate); /* register the method */ + lua_settable (L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringstate); + lua_settable(L, -3); + lua_pop(L, 1); /* removes the metatable from the top of the stack */ + + luaL_newmetatable(L, "cdImage"); + lua_pushliteral (L, "__gc"); + lua_pushcfunction (L, cdlua5_killimage); + lua_settable (L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringimage); + lua_settable(L, -3); + lua_pop(L, 1); + + luaL_newmetatable(L, "cdBitmap"); + lua_pushliteral (L, "__gc"); + lua_pushcfunction (L, cdlua5_killbitmap); + lua_settable (L, -3); + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, cdlua5_indexbitmap); + lua_settable(L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringbitmap); + lua_settable(L, -3); + lua_pop(L, 1); + + luaL_newmetatable(L, "cdImageRGB"); + lua_pushliteral (L, "__gc"); + lua_pushcfunction (L, cdlua5_killimagergb); + lua_settable (L, -3); + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, cdlua5_indeximagergb); + lua_settable(L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringimagergb); + lua_settable(L, -3); + lua_pop(L, 1); + + luaL_newmetatable(L, "cdImageRGBA"); + lua_pushliteral (L, "__gc"); + lua_pushcfunction (L, cdlua5_killimagergba); + lua_settable (L, -3); + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, cdlua5_indeximagergba); + lua_settable(L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringimagergba); + lua_settable(L, -3); + lua_pop(L, 1); + + luaL_newmetatable(L, "cdImageChannel"); + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, cdlua5_indexchannel); + lua_settable(L, -3); + lua_pushliteral(L, "__newindex"); + lua_pushcfunction(L, cdlua5_newindexchannel); + lua_settable(L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringchannel); + lua_settable(L, -3); + lua_pop(L, 1); + + luaL_newmetatable(L, "cdStipple"); + lua_pushliteral (L, "__gc"); + lua_pushcfunction (L, cdlua5_killstipple); + lua_settable (L, -3); + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, cdlua5_indexstipple); + lua_settable(L, -3); + lua_pushliteral(L, "__newindex"); + lua_pushcfunction(L, cdlua5_newindexstipple); + lua_settable(L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringstipple); + lua_settable(L, -3); + lua_pop(L, 1); + + luaL_newmetatable(L, "cdPattern"); + lua_pushliteral (L, "__gc"); + lua_pushcfunction (L, cdlua5_killpattern); + lua_settable (L, -3); + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, cdlua5_indexpattern); + lua_settable(L, -3); + lua_pushliteral(L, "__newindex"); + lua_pushcfunction(L, cdlua5_newindexpattern); + lua_settable(L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringpattern); + lua_settable(L, -3); + lua_pop(L, 1); + + luaL_newmetatable(L, "cdPalette"); + lua_pushliteral(L, "__gc"); + lua_pushcfunction(L, cdlua5_killpalette); + lua_settable(L, -3); + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, cdlua5_indexpalette); + lua_settable(L, -3); + lua_pushliteral(L, "__newindex"); + lua_pushcfunction(L, cdlua5_newindexpalette); + lua_settable(L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringpalette); + lua_settable(L, -3); + lua_pushliteral(L, "__len"); + lua_pushcfunction(L, cdluaPalette_len); + lua_settable(L, -3); + lua_pop(L, 1); + + luaL_newmetatable(L, "cdImageMap"); + lua_pushliteral(L, "__gc"); + lua_pushcfunction(L, cdlua5_killimagemap); + lua_settable(L, -3); + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, cdlua5_indeximagemap); + lua_settable(L, -3); + lua_pushliteral(L, "__newindex"); + lua_pushcfunction(L, cdlua5_newindeximagemap); + lua_settable(L, -3); + lua_pushliteral(L, "__tostring"); + lua_pushcfunction(L, cdlua5_tostringimagemap); + lua_settable(L, -3); + lua_pop(L, 1); +} + +static void setinfo (lua_State *L) +{ + lua_pushliteral (L, "_COPYRIGHT"); + lua_pushliteral (L, CD_COPYRIGHT); + lua_settable (L, -3); + + lua_pushliteral (L, "_DESCRIPTION"); + lua_pushliteral (L, CD_DESCRIPTION); + lua_settable (L, -3); + + lua_pushliteral (L, "_NAME"); + lua_pushliteral (L, CD_NAME); + lua_settable (L, -3); + + lua_pushliteral (L, "_VERSION"); + lua_pushstring (L, cdVersion()); + lua_settable (L, -3); + + lua_pushliteral (L, "_VERSION_DATE"); + lua_pushliteral(L, CD_VERSION_DATE); + lua_settable (L, -3); + + lua_pushliteral (L, "_VERSION_NUMBER"); + lua_pushinteger(L, cdVersionNumber()); + lua_settable (L, -3); +} + + +/********************************************************************************\ +* CDLua OpenLib * +\********************************************************************************/ + + +int cdlua_open (lua_State *L) +{ + cdluaLuaState* cdL = malloc(sizeof(cdluaLuaState)); + memset(cdL, 0, sizeof(cdluaLuaState)); + cdlua_SetState(L, cdL); + + initmetatables(L); + + luaL_register(L, "cd", cdlib); /* leave "cd" table at the top of the stack */ + setinfo(L); + + cdlua_open_active(L, cdL); + cdlua_open_canvas(L); + + cdlua_initdrivers(L, cdL); + initconst(L); + initcolor(L); + + return 1; +} + +int cdlua_close(lua_State *L) +{ + cdluaLuaState* cdL = cdlua_getstate(L); + if (cdL) + { + cdKillCanvas(cdL->void_canvas); + free(cdL); + } + return 0; +} + +int luaopen_cdlua(lua_State* L) +{ + return cdlua_open(L); +} + +int luaopen_cdlua51(lua_State* L) +{ + return cdlua_open(L); +} diff --git a/cd/src/lua5/cdlua5.def b/cd/src/lua5/cdlua5.def new file mode 100755 index 0000000..b4811b2 --- /dev/null +++ b/cd/src/lua5/cdlua5.def @@ -0,0 +1,13 @@ +EXPORTS + cdlua_open + cdlua_close + cdlua_getcanvas + cdlua_addcontext + cdlua_getplaystate + cdlua_getstate + cdlua_checkcanvas + cdlua_pushcanvas + luaopen_cdlua + luaopen_cdlua51 + cdlua_pushbitmap + cdlua_checkbitmap \ No newline at end of file diff --git a/cd/src/lua5/cdlua5_active.c b/cd/src/lua5/cdlua5_active.c new file mode 100755 index 0000000..ad4398e --- /dev/null +++ b/cd/src/lua5/cdlua5_active.c @@ -0,0 +1,2163 @@ +/** \file + * \brief Lua Binding of the OLD API that needs an active canvas + * + * See Copyright Notice in cd.h + */ + + +#include +#include +#include + +#ifdef CD_NO_OLD_INTERFACE +#undef CD_NO_OLD_INTERFACE +#endif + +#include "cd.h" +#include "wd.h" +#include "cdirgb.h" + +#include +#include + +#include "cdlua.h" +#include "cdlua5_private.h" +#include "cdvoid5.h" + + +void cdlua_kill_active(lua_State * L, cdCanvas* canvas) +{ + cdluaLuaState* cdL = cdlua_getstate(L); + cdCanvas* void_canvas = cdL->void_canvas; + + /* find out about the currently active canvas */ + cdCanvas *current_canvas = cdActiveCanvas(); + + /* this should never happen, unless the user did it on purpouse! */ + if (canvas == void_canvas) + luaL_error(L, "trying to kill the void canvas"); + + /* if the user killed the currently active canvas, activate void canvas */ + if (canvas == current_canvas) + cdActivate(void_canvas); +} + +/***************************************************************************\ +* Activates a cd canvas. * +\***************************************************************************/ +static int cdlua5_activate(lua_State * L) +{ + cdCanvas* canvas; + + /* if canvas is nil, activate a void canvas */ + if (lua_isnil(L, 1)) + { + cdluaLuaState* cdL = cdlua_getstate(L); + cdCanvas* void_canvas = cdL->void_canvas; + lua_pushnumber(L, cdActivate(void_canvas)); + return 1; + } + + canvas = cdlua_checkcanvas(L, 1); + lua_pushnumber(L, cdActivate(canvas)); + return 1; +} + +/***************************************************************************\ +* Returns the active canvas. * +\***************************************************************************/ +static int cdlua5_activecanvas(lua_State* L) +{ + cdCanvas* canvas = cdActiveCanvas(); + if (canvas) + cdlua_pushcanvas(L, canvas); + else + lua_pushnil(L); + + return 1; +} + +/***************************************************************************\ +* cd.Simulate(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_simulate(lua_State *L) +{ + lua_pushnumber(L, cdSimulate(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* Control * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Clear() * +\***************************************************************************/ +static int cdlua5_clear(lua_State *L) +{ + (void)L; + cdClear(); + return 0; +} + +/***************************************************************************\ +* cd.Flush() * +\***************************************************************************/ +static int cdlua5_flush(lua_State *L) +{ + (void)L; + cdFlush(); + return 0; +} + +static int cdlua5_savestate(lua_State *L) +{ + cdState* state = cdSaveState(); + if (state) + cdlua_pushstate(L, state); + else + lua_pushnil(L); + return 1; +} + +static int cdlua5_restorestate(lua_State * L) +{ + cdRestoreState(cdlua_checkstate(L, 1)); + return 0; +} + +/***************************************************************************\ +* cd.SetAttribute(name, data: string) * +\***************************************************************************/ + +static int cdlua_isuserdata(const char* name) +{ + if (strcmp(name, "HDC")==0) return 1; + if (strcmp(name, "GC")==0) return 1; + return 0; +} + +static int cdlua5_setattribute(lua_State *L) +{ + const char* name = luaL_checkstring(L, 1); + + if (lua_isnil(L, 2)) + { + cdSetAttribute(name, NULL); + } + else + { + char* data; + if (cdlua_isuserdata(name)) + data = (char*) lua_touserdata(L, 2); + else + data = (char*) luaL_checkstring(L, 2); + cdSetAttribute(name, data); + } + return 0; +} + + +/***************************************************************************\ +* cd.SetAttribute(name: string) -> (data: string) * +\***************************************************************************/ +static int cdlua5_getattribute(lua_State *L) +{ + char* name = (char *)luaL_checkstring(L, 1); + char* data = cdGetAttribute(name); + if (data) + { + if (cdlua_isuserdata(name)) + lua_pushlightuserdata(L, data); + else + lua_pushstring(L, data); + } + else + lua_pushnil(L); + return 1; +} + + + +/***************************************************************************\ +* Coordinate System * +\***************************************************************************/ + +/***************************************************************************\ +* cd.GetCanvasSize() -> (width, heigth, mm_width, mm_height: number) * +\***************************************************************************/ +static int cdlua5_getcanvassize(lua_State *L) +{ + int width; + int height; + double mm_width; + double mm_height; + + cdGetCanvasSize(&width, &height, &mm_width, &mm_height); + + lua_pushnumber(L, width); + lua_pushnumber(L, height); + lua_pushnumber(L, mm_width); + lua_pushnumber(L, mm_height); + return 4; +} + +/***************************************************************************\ +* cd.UpdateYAxis(yc: number) -> (yr: number) * +\***************************************************************************/ +static int cdlua5_updateyaxis(lua_State *L) +{ + int y = luaL_checkint(L, 1); + cdUpdateYAxis(&y); + lua_pushnumber(L, y); + return 1; +} + +/***************************************************************************\ +* cd.MM2Pixel(mm_dx, mm_dy: number) -> (dx, dy: number) * +\***************************************************************************/ +static int cdlua5_mm2pixel(lua_State *L) +{ + double mm_dx_d, mm_dy_d; + int dx, dy; + + mm_dx_d = luaL_checknumber(L,1); + mm_dy_d = luaL_checknumber(L,2); + + cdMM2Pixel(mm_dx_d, mm_dy_d, &dx, &dy); + lua_pushnumber(L, dx); + lua_pushnumber(L, dy); + return 2; +} + +/***************************************************************************\ +* cd.Pixel2MM(dx, dy: number) -> (mm_dx, mm_dy: number) * +\***************************************************************************/ +static int cdlua5_pixel2mm(lua_State *L) +{ + double mm_dx_d, mm_dy_d; + int dx, dy; + + dx = luaL_checkint(L,1); + dy = luaL_checkint(L,2); + + cdPixel2MM(dx, dy, &mm_dx_d, &mm_dy_d); + lua_pushnumber(L, mm_dx_d); + lua_pushnumber(L, mm_dy_d); + return 2; +} + +/***************************************************************************\ +* cd.Origin(x, y: number) * +\***************************************************************************/ +static int cdlua5_origin(lua_State *L) +{ + cdOrigin(luaL_checkint(L,1), luaL_checkint(L,2)); + return 0; +} + + + +/***************************************************************************\ +* World Coordinates * +\***************************************************************************/ + +/***************************************************************************\ +* wd.Window(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_window(lua_State *L) +{ + double xmin = luaL_checknumber(L, 1); + double xmax = luaL_checknumber(L, 2); + double ymin = luaL_checknumber(L, 3); + double ymax = luaL_checknumber(L, 4); + wdWindow(xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* wd.GetWindow() -> (xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_getwindow(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + + wdGetWindow(&xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/***************************************************************************\ +* wd.Viewport(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_viewport(lua_State *L) +{ + int xmin = luaL_checkint(L, 1); + int xmax = luaL_checkint(L, 2); + int ymin = luaL_checkint(L, 3); + int ymax = luaL_checkint(L, 4); + wdViewport(xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* wd.GetViewport() -> (xmin, xmax, ymin, ymax: number * +\***************************************************************************/ +static int wdlua5_getviewport(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + + wdGetViewport(&xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/***************************************************************************\ +* wd.World2Canvas(xw, yw: number) -> (xv, yv: number) * +\***************************************************************************/ +static int wdlua5_world2canvas(lua_State *L) +{ + double xw_d, yw_d; + int xv_i, yv_i; + + xw_d = luaL_checknumber(L, 1); + yw_d = luaL_checknumber(L, 2); + + wdWorld2Canvas(xw_d, yw_d, &xv_i, &yv_i); + lua_pushnumber(L, xv_i); + lua_pushnumber(L, yv_i); + return 2; +} + +/***************************************************************************\ +* wd.Canvas2World(xv, yv: number) -> (xw, yw: number) * +\***************************************************************************/ +static int wdlua5_canvas2world(lua_State *L) +{ + int xv_i, yv_i; + double xw_d, yw_d; + + xv_i = luaL_checkint(L, 1); + yv_i = luaL_checkint(L, 2); + + wdCanvas2World(xv_i, yv_i, &xw_d, &yw_d); + lua_pushnumber(L, xw_d); + lua_pushnumber(L, yw_d); + return 2; +} + + + +/***************************************************************************\ +* General Attributes * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Foreground(color) -> color * +\***************************************************************************/ +static int cdlua5_foreground(lua_State *L) +{ + long int color_i = cdlua_checkcolor(L, 1); + color_i = cdForeground(color_i); + lua_pushlightuserdata(L, (void*)color_i); + return 1; +} + +/***************************************************************************\ +* cd.Background(color) -> color * +\***************************************************************************/ +static int cdlua5_background(lua_State *L) +{ + long int color_i = cdlua_checkcolor(L, 1); + color_i = cdBackground(color_i); + lua_pushlightuserdata(L, (void*) color_i); + return 1; +} + +/***************************************************************************\ +* cd.WriteMode(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_writemode(lua_State *L) +{ + lua_pushnumber(L, cdWriteMode(luaL_checkint(L, 1))); + return 1; +} + + + +/***************************************************************************\ +* Clipping * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Clip(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_clip(lua_State *L) +{ + lua_pushnumber(L, cdClip(luaL_checkint(L,1))); + return 1; +} + +/***************************************************************************\ +* cd.ClipArea(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int cdlua5_cliparea(lua_State *L) +{ + int xmin = luaL_checkint(L, 1); + int xmax = luaL_checkint(L, 2); + int ymin = luaL_checkint(L, 3); + int ymax = luaL_checkint(L, 4); + + cdClipArea(xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* cd.wClipArea(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_cliparea(lua_State *L) +{ + double xmin = luaL_checknumber(L, 1); + double xmax = luaL_checknumber(L, 2); + double ymin = luaL_checknumber(L, 3); + double ymax = luaL_checknumber(L, 4); + + wdClipArea(xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* cd.GetClipArea() -> (xmin, xmax, ymin, ymax, status: number) * +\***************************************************************************/ +static int cdlua5_getcliparea(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + int status; + + status = cdGetClipArea(&xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + lua_pushnumber(L, status); + return 5; +} + +/***************************************************************************\ +* cd.wGetClipArea() -> (xmin, xmax, ymin, ymax, status: number) * +\***************************************************************************/ +static int wdlua5_getcliparea(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + int status; + + status = wdGetClipArea(&xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + lua_pushnumber(L, status); + return 5; +} + +/***************************************************************************\ +* cd.GetClipPoly() -> (n: number, points: table) * +\***************************************************************************/ +static int cdlua5_getclippoly(lua_State *L) +{ + int n, i; + int *pts; + + pts = cdGetClipPoly(&n); + if (pts) + { + lua_pushnumber(L, n); + + lua_newtable(L); + for (i=0; i < 2*n; i++) + { + lua_pushnumber(L, i+1); + lua_pushnumber(L, pts[i]); + lua_settable(L, -3); + } + + return 2; + } + else + { + lua_pushnil(L); + return 1; + } +} + +/***************************************************************************\ +* cd.wGetClipPoly() -> (n: number, points: table) * +\***************************************************************************/ +static int wdlua5_getclippoly(lua_State *L) +{ + int n, i; + double *pts; + + pts = wdGetClipPoly(&n); + if (pts) + { + lua_pushnumber(L, n); + + lua_newtable(L); + for (i=0; i < 2*n; i++) + { + lua_pushnumber(L, i+1); + lua_pushnumber(L, pts[i]); + lua_settable(L,-3); + } + + return 2; + } + else + { + lua_pushnil(L); + return 1; + } +} + + +/***************************************************************************\ +* Regions * +\***************************************************************************/ + +/***************************************************************************\ +* cd.RegionCombineMode(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_regioncombinemode(lua_State *L) +{ + lua_pushnumber(L, cdRegionCombineMode(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.PointInRegion(x, y: number) -> (status: number) * +\***************************************************************************/ +static int cdlua5_pointinregion(lua_State *L) +{ + lua_pushnumber(L, cdPointInRegion(luaL_checkint(L, 1),luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.wPointInRegion(x, y: number) -> (status: number) * +\***************************************************************************/ +static int wdlua5_pointinregion(lua_State *L) +{ + lua_pushnumber(L, wdPointInRegion(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.OffsetRegion(dx, dy: number) * +\***************************************************************************/ +static int cdlua5_offsetregion(lua_State *L) +{ + cdOffsetRegion(luaL_checkint(L, 1), luaL_checkint(L, 2)); + return 0; +} + +/***************************************************************************\ +* cd.wOffsetRegion(dx, dy: number) * +\***************************************************************************/ +static int wdlua5_offsetregion(lua_State *L) +{ + wdOffsetRegion(luaL_checknumber(L, 1), luaL_checknumber(L, 2)); + return 0; +} + +/***************************************************************************\ +* cd.RegionBox() -> (xmin, xmax, ymin, ymax, status: number) * +\***************************************************************************/ +static int cdlua5_regionbox(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + + cdRegionBox(&xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/***************************************************************************\ +* cd.wRegionBox() -> (xmin, xmax, ymin, ymax, status: number) * +\***************************************************************************/ +static int wdlua5_regionbox(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + + wdRegionBox(&xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + + + +/***************************************************************************\ +* Primitives * +\***************************************************************************/ + + + +/***************************************************************************\ +* Marks * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Pixel(x, y: number, color) * +\***************************************************************************/ +static int cdlua5_pixel (lua_State *L) +{ + cdPixel(luaL_checkint(L, 1), luaL_checkint(L, 2), cdlua_checkcolor(L, 3)); + return 0 ; +} + +/***************************************************************************\ +* cd.wPixel(x, y: number, color) * +\***************************************************************************/ +static int wdlua5_pixel (lua_State *L) +{ + wdPixel(luaL_checknumber(L, 1), luaL_checknumber(L, 2), cdlua_checkcolor(L, 3)); + return 0; +} + +/***************************************************************************\ +* cd.Mark(x, y: number) * +\***************************************************************************/ +static int cdlua5_mark(lua_State *L) +{ + cdMark(luaL_checkint(L,1), luaL_checkint(L,2)); + return 0; +} + +/***************************************************************************\ +* cd.wMark(x, y: number) * +\***************************************************************************/ +static int wdlua5_mark(lua_State *L) +{ + wdMark(luaL_checknumber(L, 1), luaL_checknumber(L, 2)); + return 0; +} + +/***************************************************************************\ +* cd.MarkType(type: number) -> (old_type: number) * +\***************************************************************************/ +static int cdlua5_marktype(lua_State *L) +{ + lua_pushnumber(L, cdMarkType(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.MarkSize(size: number) -> (old_size: number) * +\***************************************************************************/ +static int cdlua5_marksize(lua_State *L) +{ + lua_pushnumber(L, cdMarkSize(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.wMarkSize(size: number) -> (old_size: number) * +\***************************************************************************/ +static int wdlua5_marksize(lua_State *L) +{ + lua_pushnumber(L, wdMarkSize(luaL_checknumber(L, 1))); + return 1; +} + + + +/***************************************************************************\ +* Lines * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Line(x1, y1, x2, y2: number) * +\***************************************************************************/ +static int cdlua5_line(lua_State *L) +{ + int x1 = luaL_checkint(L,1); + int y1 = luaL_checkint(L,2); + int x2 = luaL_checkint(L,3); + int y2 = luaL_checkint(L,4); + cdLine(x1, y1, x2, y2); + return 0; +} + +/***************************************************************************\ +* cd.wLine(x1, y1, x2, y2: number) * +\***************************************************************************/ +static int wdlua5_line(lua_State *L) +{ + double x1 = luaL_checknumber(L, 1); + double y1 = luaL_checknumber(L, 2); + double x2 = luaL_checknumber(L, 3); + double y2 = luaL_checknumber(L, 4); + wdLine(x1, y1, x2, y2); + return 0; +} + +/***************************************************************************\ +* cd.Rect(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int cdlua5_rect(lua_State *L) +{ + int xmin = luaL_checkint(L,1); + int xmax = luaL_checkint(L,2); + int ymin = luaL_checkint(L,3); + int ymax = luaL_checkint(L,4); + cdRect(xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* cd.wRect(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_rect(lua_State *L) +{ + double xmin = luaL_checknumber(L, 1); + double xmax = luaL_checknumber(L, 2); + double ymin = luaL_checknumber(L, 3); + double ymax = luaL_checknumber(L, 4); + wdRect(xmin,xmax,ymin,ymax); + return 0; +} + +/***************************************************************************\ +* cd.Arc(xc, yc, w, h, angle1, angle2: number) * +\***************************************************************************/ +static int cdlua5_arc(lua_State *L) +{ + int xc = luaL_checkint(L,1); + int yc = luaL_checkint(L,2); + int w = luaL_checkint(L,3); + int h = luaL_checkint(L,4); + double angle1 = luaL_checknumber(L,5); + double angle2 = luaL_checknumber(L,6); + cdArc(xc, yc, w, h, angle1, angle2); + return 0; +} + +/***************************************************************************\ +* cd.wArc(xc, yc, w, h, angle1, angle2: number) * +\***************************************************************************/ +static int wdlua5_arc(lua_State *L) +{ + double xc = luaL_checknumber(L, 1); + double yc = luaL_checknumber(L, 2); + double w = luaL_checknumber(L, 3); + double h = luaL_checknumber(L, 4); + double angle1 = luaL_checknumber(L, 5); + double angle2 = luaL_checknumber(L, 6); + wdArc(xc, yc, w, h, angle1, angle2); + return 0; +} + +/***************************************************************************\ +* cd.LineStyle(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_linestyle(lua_State *L) +{ + lua_pushnumber(L, cdLineStyle(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.LineStyleDashes(dashes: table, count: number) * +\***************************************************************************/ +static int cdlua5_linestyledashes(lua_State *L) +{ + int *dashes_int, dashes_count, i; + + if (!lua_istable(L, 1)) + luaL_argerror(L, 1, "invalid dashes, must be a table"); + + dashes_count = luaL_checkint(L, 2); + dashes_int = (int*) malloc(dashes_count * sizeof(int)); + + for (i=0; i < dashes_count; i++) + { + lua_pushnumber(L, i+1); + lua_gettable(L,1); + + dashes_int[i] = luaL_checkint(L,-1); + } + + cdLineStyleDashes(dashes_int, dashes_count); + free(dashes_int); + + return 0; +} + +/***************************************************************************\ +* cd.LineWidth(width: number) -> (old_width: number) * +\***************************************************************************/ +static int cdlua5_linewidth(lua_State *L) +{ + lua_pushnumber(L, cdLineWidth(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.wLineWidth(width: number) -> (old_width: number) * +\***************************************************************************/ +static int wdlua5_linewidth(lua_State *L) +{ + lua_pushnumber(L, wdLineWidth(luaL_checknumber(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.LineJoin(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_linejoin(lua_State *L) +{ + lua_pushnumber(L, cdLineJoin(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.LineCap(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_linecap(lua_State *L) +{ + lua_pushnumber(L, cdLineCap(luaL_checkint(L, 1))); + return 1; +} + + + +/***************************************************************************\ +* Filled Areas * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Box(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int cdlua5_box(lua_State *L) +{ + int xmin = luaL_checkint(L, 1); + int xmax = luaL_checkint(L, 2); + int ymin = luaL_checkint(L, 3); + int ymax = luaL_checkint(L, 4); + cdBox(xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* cd.wBox(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_box(lua_State *L) +{ + double xmin = luaL_checknumber(L, 1); + double xmax = luaL_checknumber(L, 2); + double ymin = luaL_checknumber(L, 3); + double ymax = luaL_checknumber(L, 4); + wdBox(xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* cd.Sector(xc, yc, w, h, angle1, angle2: number) * +\***************************************************************************/ +static int cdlua5_sector(lua_State *L) +{ + int xc = luaL_checkint(L,1); + int yc = luaL_checkint(L,2); + int w = luaL_checkint(L,3); + int h = luaL_checkint(L,4); + double angle1 = luaL_checknumber(L,5); + double angle2 = luaL_checknumber(L,6); + cdSector(xc, yc, w, h, angle1, angle2); + return 0; +} + +/***************************************************************************\ +* cd.wSector(xc, yc, w, h, angle1, angle2: number) * +\***************************************************************************/ +static int wdlua5_sector(lua_State *L) +{ + double xc = luaL_checknumber(L, 1); + double yc = luaL_checknumber(L, 2); + double w = luaL_checknumber(L, 3); + double h = luaL_checknumber(L, 4); + double angle1 = luaL_checknumber(L, 5); + double angle2 = luaL_checknumber(L, 6); + wdSector(xc, yc, w, h, angle1, angle2); + return 0; +} + +/***************************************************************************\ +* cd.Chord(xc, yc, w, h, angle1, angle2: number) * +\***************************************************************************/ +static int cdlua5_chord(lua_State *L) +{ + int xc = luaL_checkint(L,1); + int yc = luaL_checkint(L,2); + int w = luaL_checkint(L,3); + int h = luaL_checkint(L,4); + double angle1 = luaL_checknumber(L,5); + double angle2 = luaL_checknumber(L,6); + cdChord(xc, yc, w, h, angle1, angle2); + return 0; +} + +/***************************************************************************\ +* cd.wChord(xc, yc, w, h, angle1, angle2: number) * +\***************************************************************************/ +static int wdlua5_chord(lua_State *L) +{ + double xc = luaL_checknumber(L, 1); + double yc = luaL_checknumber(L, 2); + double w = luaL_checknumber(L, 3); + double h = luaL_checknumber(L, 4); + double angle1 = luaL_checknumber(L, 5); + double angle2 = luaL_checknumber(L, 6); + wdChord(xc, yc, w, h, angle1, angle2); + return 0; +} + +/***************************************************************************\ +* cd.BackOpacity(opacity: number) -> (old_opacity: number) * +\***************************************************************************/ +static int cdlua5_backopacity(lua_State *L) +{ + lua_pushnumber(L, cdBackOpacity(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.FillMode(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_fillmode(lua_State *L) +{ + lua_pushnumber(L, cdFillMode(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.InteriorStyle(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_interiorstyle(lua_State *L) +{ + lua_pushnumber(L, cdInteriorStyle(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.Hatch(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_hatch(lua_State *L) +{ + lua_pushnumber(L, cdHatch(luaL_checkint(L, 1))); + return 1; +} + +static int cdlua5_stipple(lua_State *L) +{ + cdluaStipple *stipple_p = cdlua_checkstipple(L, 1); + cdStipple(stipple_p->width, stipple_p->height, stipple_p->stipple); + return 0 ; +} + +static int wdlua5_stipple(lua_State *L) +{ + cdluaStipple *stipple_p = cdlua_checkstipple(L, 1); + double w_mm = luaL_checknumber(L, 2); + double h_mm = luaL_checknumber(L, 3); + wdStipple(stipple_p->width, stipple_p->height, stipple_p->stipple, w_mm, h_mm); + return 0; +} + +static int cdlua5_getstipple(lua_State *L) +{ + int width, height, size; + unsigned char *stipple, *new_stipple = NULL; + + stipple = cdGetStipple(&width, &height); + + size = width * height; + + if (stipple) + new_stipple = (unsigned char *)malloc(size); + + if (new_stipple) + { + memcpy(new_stipple, stipple, size); + cdlua_pushstipple(L, new_stipple, width, height); + } + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_pattern(lua_State *L) +{ + cdluaPattern* pattern_p = cdlua_checkpattern(L, 1); + cdPattern(pattern_p->width, pattern_p->height, pattern_p->pattern); + return 0; +} + +static int wdlua5_pattern(lua_State *L) +{ + cdluaPattern* pattern_p = cdlua_checkpattern(L, 1); + double w_mm = luaL_checknumber(L, 2); + double h_mm = luaL_checknumber(L, 3); + wdPattern(pattern_p->width, pattern_p->height, pattern_p->pattern, w_mm, h_mm); + return 0; +} + +static int cdlua5_getpattern(lua_State *L) +{ + int width, height, size; + long int *pattern, *new_pattern = NULL; + + pattern = cdGetPattern(&width, &height); + + size = width * height; + + if (pattern) + new_pattern = (long int *) malloc(size * sizeof(long int)); + + if (new_pattern) + { + memcpy(new_pattern, pattern, size * sizeof(long int)); + cdlua_pushpattern(L, new_pattern, width, height); + } + else + lua_pushnil(L); + + return 1; +} + +/***************************************************************************\ +* Text * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Text(x, y: number, text: string) * +\***************************************************************************/ +static int cdlua5_text(lua_State *L) +{ + const char* s = luaL_checkstring(L, 3); + cdText(luaL_checkint(L, 1), luaL_checkint(L, 2), s); + return 0; +} + +/***************************************************************************\ +* cd.wText(x, y: number, text: string) * +\***************************************************************************/ +static int wdlua5_text(lua_State *L) +{ + const char* s = luaL_checkstring(L, 3); + wdText(luaL_checknumber(L, 1), luaL_checknumber(L, 2), s); + return 0; +} + +/***************************************************************************\ +* cd.Font(typeface, style, size: number) * +\***************************************************************************/ +static int cdlua5_font(lua_State *L) +{ + cdFont(luaL_checkint(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3)); + return 0; +} + +/***************************************************************************\ +* cd.wFont(typeface, style, size: number) * +\***************************************************************************/ +static int wdlua5_font(lua_State *L) +{ + wdFont(luaL_checkint(L, 1), luaL_checkint(L, 2), luaL_checknumber(L, 3)); + return 0; +} + + +/***************************************************************************\ +* cd.GetFont() -> (typeface, style, size: number) * +\***************************************************************************/ +static int cdlua5_getfont(lua_State *L) +{ + int type_face, style, size; + + cdGetFont(&type_face, &style, &size); + lua_pushnumber(L, type_face); + lua_pushnumber(L, style); + lua_pushnumber(L, size); + return 3; +} + +/***************************************************************************\ +* cd.wGetFont() -> (typeface, style, size: number) * +\***************************************************************************/ +static int wdlua5_getfont(lua_State *L) +{ + int type_face, style; + double size; + + wdGetFont(&type_face, &style, &size); + lua_pushnumber(L, type_face); + lua_pushnumber(L, style); + lua_pushnumber(L, size); + return 3; +} + +/***************************************************************************\ +* cd.NativeFont(font: string) * +\***************************************************************************/ +static int cdlua5_nativefont(lua_State *L) +{ + lua_pushstring(L, cdNativeFont(luaL_checkstring(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.TextAlignment(alignment: number) -> (old_alignment: number) * +\***************************************************************************/ +static int cdlua5_textalignment(lua_State *L) +{ + lua_pushnumber(L, cdTextAlignment(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.TextOrientation(angle: number) -> (old_angle: number) * +\***************************************************************************/ +static int cdlua5_textorientation(lua_State *L) +{ + lua_pushnumber(L, cdTextOrientation(luaL_checknumber(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.FontDim() -> (max_width, max_height, ascent, descent: number) * +\***************************************************************************/ +static int cdlua5_fontdim(lua_State *L) +{ + int max_width; + int height; + int ascent; + int descent; + + cdFontDim(&max_width, &height, &ascent, &descent); + lua_pushnumber(L, max_width); + lua_pushnumber(L, height); + lua_pushnumber(L, ascent); + lua_pushnumber(L, descent); + return 4; +} + +/***************************************************************************\ +* cd.wFontDim() -> (max_width, max_height, ascent, descent: number) * +\***************************************************************************/ +static int wdlua5_fontdim(lua_State *L) +{ + double max_width; + double height; + double ascent; + double descent; + + wdFontDim(&max_width, &height, &ascent, &descent); + lua_pushnumber(L, max_width); + lua_pushnumber(L, height); + lua_pushnumber(L, ascent); + lua_pushnumber(L, descent); + return 4; +} + +/***************************************************************************\ +* cd.TextSize(text: string) -> (width, heigth: number) * +\***************************************************************************/ +static int cdlua5_textsize(lua_State *L) +{ + int width; + int height; + + const char* text_s = luaL_checkstring(L, 1); + + cdTextSize(text_s, &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/***************************************************************************\ +* cd.wTextSize(text: string) -> (width, heigth: number) * +\***************************************************************************/ +static int wdlua5_textsize(lua_State *L) +{ + double width; + double height; + + const char* text_s = luaL_checkstring(L, 1); + + wdTextSize(text_s, &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/****************************************************************************\ +* cd.TextBox(x, y: number, text: string) -> (xmin, xmax, ymin, ymax: number) * +\****************************************************************************/ +static int cdlua5_textbox(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + int x = luaL_checkint(L, 1); + int y = luaL_checkint(L, 2); + const char* s = luaL_checkstring(L, 3); + + cdTextBox(x, y, s, &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/*****************************************************************************\ +* cd.wTextBox(x, y: number, text: string) -> (xmin, xmax, ymin, ymax: number) * +\*****************************************************************************/ +static int wdlua5_textbox(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + double x = luaL_checknumber(L, 1); + double y = luaL_checknumber(L, 2); + const char* s = luaL_checkstring(L, 3); + + wdTextBox(x, y, s, &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/***************************************************************************************************************\ +* cd.TextBounds(x, y: number, text: string) -> (rect0, rect1, rect2, rect3, rect4, rect5, rect6, rect7: number) * +\***************************************************************************************************************/ +static int cdlua5_textbounds(lua_State *L) +{ + int rect[8]; + int x = luaL_checkint(L, 1); + int y = luaL_checkint(L, 2); + const char* s = luaL_checkstring(L, 3); + + cdTextBounds(x, y, s, rect); + lua_pushnumber(L, rect[0]); + lua_pushnumber(L, rect[1]); + lua_pushnumber(L, rect[2]); + lua_pushnumber(L, rect[3]); + lua_pushnumber(L, rect[4]); + lua_pushnumber(L, rect[5]); + lua_pushnumber(L, rect[6]); + lua_pushnumber(L, rect[7]); + return 4; +} + +/****************************************************************************************************************\ +* cd.wTextBounds(x, y: number, text: string) -> (rect0, rect1, rect2, rect3, rect4, rect5, rect6, rect7: number) * +\****************************************************************************************************************/ +static int wdlua5_textbounds(lua_State *L) +{ + double rect[8]; + double x = luaL_checknumber(L, 1); + double y = luaL_checknumber(L, 2); + const char* s = luaL_checkstring(L, 3); + + wdTextBounds(x, y, s, rect); + lua_pushnumber(L, rect[0]); + lua_pushnumber(L, rect[1]); + lua_pushnumber(L, rect[2]); + lua_pushnumber(L, rect[3]); + lua_pushnumber(L, rect[4]); + lua_pushnumber(L, rect[5]); + lua_pushnumber(L, rect[6]); + lua_pushnumber(L, rect[7]); + return 4; +} + + + +/***************************************************************************\ +* Text * +\***************************************************************************/ + +/***************************************************************************\ +* cd.VectorText(x, y: number, text: string) * +\***************************************************************************/ +static int cdlua5_vectortext(lua_State *L) +{ + const char* s = luaL_checkstring(L,3); + cdVectorText(luaL_checkint(L, 1), luaL_checkint(L, 2), s); + return 0; +} + +/***************************************************************************\ +* cd.wVectorText(x, y: number, text: string) * +\***************************************************************************/ +static int wdlua5_vectortext(lua_State *L) +{ + const char* s = luaL_checkstring(L, 3); + wdVectorText(luaL_checknumber(L, 1), luaL_checknumber(L, 2),s); + return 0; +} + +/***************************************************************************\ +* cd.MultiLineVectorText(x, y: number, text: string) * +\***************************************************************************/ +static int cdlua5_multilinevectortext(lua_State *L) +{ + const char* s = luaL_checkstring(L, 3); + cdMultiLineVectorText(luaL_checkint(L, 1), luaL_checkint(L, 2), s); + return 0; +} + +/***************************************************************************\ +* cd.wMultiLineVectorText(x, y: number, text: string) * +\***************************************************************************/ +static int wdlua5_multilinevectortext(lua_State *L) +{ + const char* s = luaL_checkstring(L, 3); + wdMultiLineVectorText(luaL_checknumber(L, 1), luaL_checknumber(L, 2), s); + return 0; +} + +/***************************************************************************\ +* cd.VectorTextDirection(x1, y1, x2, y2: number) * +\***************************************************************************/ +static int cdlua5_vectortextdirection(lua_State *L) +{ + int x1 = luaL_checkint(L,1); + int y1 = luaL_checkint(L,2); + int x2 = luaL_checkint(L,3); + int y2 = luaL_checkint(L,4); + cdVectorTextDirection(x1, y1, x2, y2); + return 0; +} + +/***************************************************************************\ +* cd.wVectorTextDirection(x1, y1, x2, y2: number) * +\***************************************************************************/ +static int wdlua5_vectortextdirection(lua_State *L) +{ + double x1 = luaL_checknumber(L, 1); + double y1 = luaL_checknumber(L, 2); + double x2 = luaL_checknumber(L, 3); + double y2 = luaL_checknumber(L, 4); + wdVectorTextDirection(x1, y1, x2, y2); + return 0; +} + + +/***************************************************************************\ +* cd.VectorTextTransform(matrix: table) -> (old_matrix: table) * +\***************************************************************************/ +static int cdlua5_vectortexttransform(lua_State *L) +{ + double matrix[6], *old_matrix; + int i; + + if (!lua_istable(L, 1)) + luaL_argerror(L, 1, "invalid matrix, must be a table"); + + for (i=0; i < 6; i++) + { + lua_rawgeti(L, 1, i+1); + + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 1, "invalid matrix value, must be a number"); + + matrix[i] = lua_tonumber(L, -1); + lua_pop(L, 1); + } + + old_matrix = cdVectorTextTransform(matrix); + lua_newtable(L); + for (i=0; i < 6; i++) + { + lua_pushnumber(L, old_matrix[i]); + lua_rawseti(L, 1, i+1); + } + return 1; +} + +/***************************************************************************\ +* cd.VectorTextSize(w, h: number, text: string) * +\***************************************************************************/ +static int cdlua5_vectortextsize(lua_State *L) +{ + const char* s = luaL_checkstring(L, 3); + cdVectorTextSize(luaL_checkint(L,1), luaL_checkint(L,2), s); + return 0; +} + +/***************************************************************************\ +* cd.wVectorTextSize(w, h: number, text: string) * +\***************************************************************************/ +static int wdlua5_vectortextsize(lua_State *L) +{ + const char* s = luaL_checkstring(L, 3); + wdVectorTextSize(luaL_checknumber(L, 1), luaL_checknumber(L, 2), s); + return 0; +} + +/***************************************************************************\ +* cd.VectorTextSize(w, h: number, text: string) * +\***************************************************************************/ +static int cdlua5_vectorcharsize(lua_State *L) +{ + lua_pushnumber(L, cdVectorCharSize(luaL_checkint(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.wVectorTextSize(w, h: number, text: string) * +\***************************************************************************/ +static int wdlua5_vectorcharsize(lua_State *L) +{ + lua_pushnumber(L, wdVectorCharSize(luaL_checknumber(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.VectorFont(filename: string) -> (font_name: string) * +\***************************************************************************/ +static int cdlua5_vectorfont(lua_State *L) +{ + lua_pushstring(L, cdVectorFont(luaL_checkstring(L, 1))); + return 1; +} + +/***************************************************************************\ +* cd.GetVectorTextSize(text: string) -> (w, h: number) * +\***************************************************************************/ +static int cdlua5_getvectortextsize(lua_State *L) +{ + int width; + int height; + + const char* text_s = luaL_checkstring(L, 1); + + cdGetVectorTextSize(text_s, &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/***************************************************************************\ +* cd.wGetVectorTextSize(text: string) -> (w, h: number) * +\***************************************************************************/ +static int wdlua5_getvectortextsize(lua_State *L) +{ + double width; + double height; + + const char* text_s = luaL_checkstring(L, 1); + + wdGetVectorTextSize(text_s, &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/***************************************************************************\ +* cd.GetVectorTextBounds(s: string, px,py: number) -> (rect: table) * +\***************************************************************************/ +static int cdlua5_vectortextbounds(lua_State *L) +{ + const char* s = luaL_checkstring(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int rect[8], i; + + cdGetVectorTextBounds(s, x, y, rect); + lua_newtable(L); + for (i=0; i < 8; i++) + { + lua_pushnumber(L, rect[i]); + lua_rawseti(L, -2, i+1); + } + return 1; +} + +/***************************************************************************\ +* cd.wGetVectorTextBounds(s: string, px,py: number) -> (rect: table) * +\***************************************************************************/ +static int wdlua5_vectortextbounds(lua_State *L) +{ + const char* s = luaL_checkstring(L, 1); + double x = luaL_checknumber(L, 2); + double y = luaL_checknumber(L, 3); + double rect[8]; + int i; + + wdGetVectorTextBounds(s, x, y, rect); + lua_newtable(L); + for (i=0; i < 8; i++) + { + lua_pushnumber(L, rect[i]); + lua_rawseti(L, -2, i+1); + } + return 1; +} + + + +/***************************************************************************\ +* Client Images. * +\***************************************************************************/ + +static int cdlua5_getimagergb(lua_State *L) +{ + cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + + cdGetImageRGB(imagergb_p->red, imagergb_p->green, imagergb_p->blue, + x, y, imagergb_p->width, imagergb_p->height); + return 0; +} + +static int cdlua5_putimagerectrgb(lua_State *L) +{ + cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int w = luaL_checkint(L, 4); + int h = luaL_checkint(L, 5); + int xmin = luaL_checkint(L, 6); + int xmax = luaL_checkint(L, 7); + int ymin = luaL_checkint(L, 8); + int ymax = luaL_checkint(L, 9); + + if (w < 0 || h < 0) + luaL_argerror(L, 4, "target region dimensions should be positive integers"); + + cdPutImageRectRGB(imagergb_p->width, imagergb_p->height, imagergb_p->red, + imagergb_p->green, imagergb_p->blue, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_putimagerectrgb(lua_State *L) +{ + cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 1); + double x = luaL_checknumber(L, 2); + double y = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + int xmin = luaL_checkint(L, 6); + int xmax = luaL_checkint(L, 7); + int ymin = luaL_checkint(L, 8); + int ymax = luaL_checkint(L, 9); + + if (w < 0 || h < 0) + luaL_argerror(L, 4, "target region dimensions should be positive integers"); + + wdPutImageRectRGB(imagergb_p->width, imagergb_p->height, imagergb_p->red, + imagergb_p->green, imagergb_p->blue, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_putimagerectrgba(lua_State *L) +{ + cdluaImageRGBA* imagergba_p = cdlua_checkimagergba(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int w = luaL_checkint(L, 4); + int h = luaL_checkint(L, 5); + int xmin = luaL_checkint(L, 6); + int xmax = luaL_checkint(L, 7); + int ymin = luaL_checkint(L, 8); + int ymax = luaL_checkint(L, 9); + + if (w < 0 || h < 0) + luaL_argerror(L, 4, "target region dimensions should be positive integers"); + + cdPutImageRectRGBA(imagergba_p->width, imagergba_p->height, imagergba_p->red, + imagergba_p->green, imagergba_p->blue, imagergba_p->alpha, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_putimagerectrgba(lua_State *L) +{ + cdluaImageRGBA* imagergba_p = cdlua_checkimagergba(L, 1); + double x = luaL_checknumber(L, 2); + double y = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + int xmin = luaL_checkint(L, 6); + int xmax = luaL_checkint(L, 7); + int ymin = luaL_checkint(L, 8); + int ymax = luaL_checkint(L, 9); + + if (w < 0 || h < 0) + luaL_argerror(L, 4, "target region dimensions should be positive integers"); + + wdPutImageRectRGBA(imagergba_p->width, imagergba_p->height, imagergba_p->red, + imagergba_p->green, imagergba_p->blue, imagergba_p->alpha, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_putimagerectmap(lua_State *L) +{ + cdluaImageMap* imagemap_p = cdlua_checkimagemap(L, 1); + cdluaPalette *pal = cdlua_checkpalette(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + int w = luaL_checkint(L, 5); + int h = luaL_checkint(L, 6); + int xmin = luaL_checkint(L, 7); + int xmax = luaL_checkint(L, 8); + int ymin = luaL_checkint(L, 9); + int ymax = luaL_checkint(L, 10); + + if (w < 0 || h < 0) + luaL_argerror(L, 5, "target region dimensions should be positive integers"); + + cdPutImageRectMap(imagemap_p->width, imagemap_p->height, imagemap_p->index, + pal->color, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_putimagerectmap(lua_State *L) +{ + cdluaImageMap* imagemap_p = cdlua_checkimagemap(L, 1); + cdluaPalette *pal = cdlua_checkpalette(L, 2); + double x = luaL_checknumber(L, 3); + double y = luaL_checknumber(L, 4); + double w = luaL_checknumber(L, 5); + double h = luaL_checknumber(L, 6); + int xmin = luaL_checkint(L, 7); + int xmax = luaL_checkint(L, 8); + int ymin = luaL_checkint(L, 9); + int ymax = luaL_checkint(L, 10); + + if (w < 0 || h < 0) + luaL_argerror(L, 5, "target region dimensions should be positive integers"); + + wdPutImageRectMap(imagemap_p->width, imagemap_p->height, imagemap_p->index, + pal->color, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_putbitmap(lua_State *L) +{ + cdBitmap *bitmap = cdlua_checkbitmap(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int w = luaL_checkint(L, 4); + int h = luaL_checkint(L, 5); + + if (w < 0 || h < 0) + luaL_argerror(L, 4, "target region dimensions should be positive integers"); + + cdPutBitmap(bitmap, x, y, w, h); + return 0; +} + +static int wdlua5_putbitmap(lua_State *L) +{ + cdBitmap *bitmap = cdlua_checkbitmap(L, 1); + double x = luaL_checknumber(L, 2); + double y = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + + if (w < 0 || h < 0) + luaL_argerror(L, 4, "target region dimensions should be positive integers"); + + wdPutBitmap(bitmap, x, y, w, h); + return 0; +} + +static int cdlua5_getbitmap(lua_State *L) +{ + cdBitmap *bitmap = cdlua_checkbitmap(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + cdGetBitmap(bitmap, x, y); + return 0; +} + +/***************************************************************************\ +* Server Images. * +\***************************************************************************/ + +static int cdlua5_createimage(lua_State *L) +{ + cdImage *image; + int width = luaL_checkint(L, 1); + int height = luaL_checkint(L, 2); + + if (width < 1 || height < 1) + luaL_argerror(L, 1, "image dimensions should be positive integers"); + + image = cdCreateImage(width, height); + if (image) + cdlua_pushimage(L, image); + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_getimage(lua_State *L) +{ + cdImage* image = cdlua_checkimage(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + cdGetImage(image, x, y); + return 0; +} + +static int cdlua5_putimagerect(lua_State *L) +{ + cdImage* image = cdlua_checkimage(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int xmin = luaL_checkint(L, 4); + int xmax = luaL_checkint(L, 5); + int ymin = luaL_checkint(L, 6); + int ymax = luaL_checkint(L, 7); + cdPutImageRect(image, x, y, xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_putimagerect(lua_State *L) +{ + cdImage* image = cdlua_checkimage(L, 1); + double x = luaL_checknumber(L, 2); + double y = luaL_checknumber(L, 3); + int xmin = luaL_checkint(L, 4); + int xmax = luaL_checkint(L, 5); + int ymin = luaL_checkint(L, 6); + int ymax = luaL_checkint(L, 7); + wdPutImageRect(image, x, y, xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* cd.ScrollArea(xmin, xmax, ymin, ymax, dx, dy: number) * +\***************************************************************************/ +static int cdlua5_scrollarea(lua_State *L) +{ + int xmin = luaL_checkint(L, 1); + int xmax = luaL_checkint(L, 2); + int ymin = luaL_checkint(L, 3); + int ymax = luaL_checkint(L, 4); + int dx = luaL_checkint(L, 5); + int dy = luaL_checkint(L, 6); + cdScrollArea(xmin, xmax, ymin, ymax, dx, dy); + return 0; +} + + + +/***************************************************************************\ +* Other * +\***************************************************************************/ + +/********************************************************************************\ +* cd.Play(ctx, xmin, xmax, ymin, ymax: number, data: string) -> (status: number) * +\********************************************************************************/ + +static int cdlua5_play(lua_State *L) +{ + cdluaContext* cdlua_ctx = cdlua_getcontext(L, 1); + int xmin = luaL_checkint(L,2); + int xmax = luaL_checkint(L,3); + int ymin = luaL_checkint(L,4); + int ymax = luaL_checkint(L,5); + const char *data_s = luaL_checkstring(L,6); + + cdlua_setplaystate(L); + cdPlay(cdlua_ctx->ctx(), xmin, xmax, ymin, ymax, (void*)data_s); + cdlua_setplaystate(NULL); + return 0; +} + +/***************************************************************************\ +* cd.GetColorPlanes() -> (bpp: number) * +\***************************************************************************/ +static int cdlua5_getcolorplanes(lua_State *L) +{ + lua_pushnumber(L, cdGetColorPlanes()); + return 1; +} + +static int cdlua5_palette(lua_State *L) +{ + cdluaPalette *pal = cdlua_checkpalette(L, 1); + int mode_i = luaL_checkint(L, 2); + cdPalette(pal->count, pal->color, mode_i); + return 0; +} + +/***************************************************************************\ +* cd.ImageRGB * +\***************************************************************************/ +static int cdlua5_imagergb(lua_State *L) +{ + cdCanvas *current_canvas; + int w, h, type = CD_RGB; + + cdCanvas* canvas = cdlua_checkcanvas(L, 1); + + if (cdCanvasGetContext(canvas) != CD_IMAGERGB) + luaL_argerror(L, 1, "invalid canvas, must be CD_IMAGERGB"); + + if (cdAlphaImage(canvas)) + type = CD_RGBA; + + current_canvas = cdActiveCanvas(); + cdActivate(canvas); + cdGetCanvasSize(&w, &h, NULL, NULL); + cdActivate(current_canvas); + + /* mark the image NOT to be freed */ + if (type == CD_RGBA) + cdlua_pushimagergba_ex(L, cdRedImage(canvas), cdGreenImage(canvas), cdBlueImage(canvas), cdAlphaImage(canvas), w, h); + else + cdlua_pushimagergb_ex(L, cdRedImage(canvas), cdGreenImage(canvas), cdBlueImage(canvas), w, h); + + return 1; +} + +/***************************************************************************\ +* cd.ImageRGBBitmap * +\***************************************************************************/ +static int cdlua5_imagergbbitmap(lua_State *L) +{ + cdCanvas *current_canvas; + int w, h, type = CD_RGB; + + cdCanvas* canvas = cdlua_checkcanvas(L, 1); + + if (cdCanvasGetContext(canvas) != CD_IMAGERGB) + luaL_argerror(L, 1, "invalid canvas, must be CD_IMAGERGB"); + + if (cdAlphaImage(canvas)) + type = CD_RGBA; + + current_canvas = cdActiveCanvas(); + cdActivate(canvas); + cdGetCanvasSize(&w, &h, NULL, NULL); + cdActivate(current_canvas); + + cdlua_pushbitmap(L, cdInitBitmap(w, h, type, + cdRedImage(canvas), + cdGreenImage(canvas), + cdBlueImage(canvas), + cdAlphaImage(canvas))); + + return 1; +} + +/***************************************************************************\ +* Hardcopy * +\***************************************************************************/ +static lua_State* wdlua5_hardcopy_luaState = NULL; + +static void wdlua5_hardcopy_func(void) +{ + lua_State* L = wdlua5_hardcopy_luaState; + lua_pushvalue(L, 4); /* push the function in the stack */ + if(lua_pcall(L, 0, 0, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); +} + +static int wdlua5_hardcopy(lua_State *L) +{ + cdluaContext* cdlua_ctx = cdlua_getcontext(L, 1); + void *data_p = cdlua_ctx->checkdata(L, 2); + cdCanvas* canvas = cdlua_checkcanvas(L, 3); + luaL_argcheck(L, !lua_isfunction(L, 4), 4, "invalid draw function"); + + wdlua5_hardcopy_luaState = L; + wdHardcopy(cdlua_ctx->ctx(), data_p, canvas, wdlua5_hardcopy_func); + + return 0; +} + + +/***************************************************************************\ +* Polygon functions * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Begin(mode: number) * +\***************************************************************************/ +static int cdlua5_begin(lua_State *L) +{ + cdBegin(luaL_checkint(L, 1)); + return 0; +} + +/***************************************************************************\ +* cd.Vertex(x, y: number) * +\***************************************************************************/ +static int cdlua5_vertex(lua_State *L) +{ + cdVertex(luaL_checkint(L, 1), luaL_checkint(L, 2)); + return 0; +} + +/***************************************************************************\ +* cd.wVertex(x, y: number) * +\***************************************************************************/ +static int wdlua5_vertex(lua_State *L) +{ + wdVertex(luaL_checknumber(L, 1), luaL_checknumber(L, 2)); + return 0; +} + +/***************************************************************************\ +* cd.End() * +\***************************************************************************/ +static int cdlua5_end(lua_State *L) +{ + (void)L; + cdEnd(); + return 0; +} + + +/********************************************************************************\ +* CDLua Exported functions * +\********************************************************************************/ +static const struct luaL_reg cdlib_active[] = { + + /* Initialization */ + {"Activate" , cdlua5_activate}, + {"ActiveCanvas" , cdlua5_activecanvas}, + {"Simulate" , cdlua5_simulate}, + + /* Control */ + {"Clear" , cdlua5_clear}, + {"Flush" , cdlua5_flush}, + {"SaveState" , cdlua5_savestate}, + {"RestoreState" , cdlua5_restorestate}, + {"SetAttribute" , cdlua5_setattribute}, + {"GetAttribute" , cdlua5_getattribute}, + + /* Coordinate System */ + {"GetCanvasSize" , cdlua5_getcanvassize}, + {"UpdateYAxis" , cdlua5_updateyaxis}, + {"MM2Pixel" , cdlua5_mm2pixel}, + {"Pixel2MM" , cdlua5_pixel2mm}, + {"Origin" , cdlua5_origin}, + + /* World Coordinates */ + {"wWindow" , wdlua5_window}, + {"wGetWindow" , wdlua5_getwindow}, + {"wViewport" , wdlua5_viewport}, + {"wGetViewport" , wdlua5_getviewport}, + {"wWorld2Canvas" , wdlua5_world2canvas}, + {"wCanvas2World" , wdlua5_canvas2world}, + + {"wHardcopy" , wdlua5_hardcopy}, + + /* General Attributes */ + {"Foreground" , cdlua5_foreground}, + {"Background" , cdlua5_background}, + {"WriteMode" , cdlua5_writemode}, + + /* Clipping */ + {"Clip" , cdlua5_clip}, + {"ClipArea" , cdlua5_cliparea}, + {"wClipArea" , wdlua5_cliparea}, + {"GetClipArea" , cdlua5_getcliparea}, + {"wGetClipArea" , wdlua5_getcliparea}, + {"GetClipPoly" , cdlua5_getclippoly}, + {"wGetClipPoly" , wdlua5_getclippoly}, + + /* Regions */ + {"RegionCombineMode" , cdlua5_regioncombinemode}, + {"PointInRegion" , cdlua5_pointinregion}, + {"wPointInRegion" , wdlua5_pointinregion}, + {"OffsetRegion" , cdlua5_offsetregion}, + {"wOffsetRegion" , wdlua5_offsetregion}, + {"RegionBox" , cdlua5_regionbox}, + {"wRegionBox" , wdlua5_regionbox}, + + /* Marks */ + {"Pixel" , cdlua5_pixel}, + {"wPixel" , wdlua5_pixel}, + {"Mark" , cdlua5_mark}, + {"wMark" , wdlua5_mark}, + {"MarkType" , cdlua5_marktype}, + {"MarkSize" , cdlua5_marksize}, + {"wMarkSize" , wdlua5_marksize}, + + /* Line */ + {"Line" , cdlua5_line}, + {"wLine" , wdlua5_line}, + {"Rect" , cdlua5_rect}, + {"wRect" , wdlua5_rect}, + {"Arc" , cdlua5_arc}, + {"wArc" , wdlua5_arc}, + {"LineStyle" , cdlua5_linestyle}, + {"LineStyleDashes" , cdlua5_linestyledashes}, + {"LineWidth" , cdlua5_linewidth}, + {"wLineWidth" , wdlua5_linewidth}, + {"LineJoin" , cdlua5_linejoin}, + {"LineCap" , cdlua5_linecap}, + + /* Filled Areas */ + {"Box" , cdlua5_box}, + {"wBox" , wdlua5_box}, + {"Sector" , cdlua5_sector}, + {"wSector" , wdlua5_sector}, + {"Chord" , cdlua5_chord}, + {"wChord" , wdlua5_chord}, + {"BackOpacity" , cdlua5_backopacity}, + {"FillMode" , cdlua5_fillmode}, + {"InteriorStyle" , cdlua5_interiorstyle}, + {"Hatch" , cdlua5_hatch}, + + /* Stipple */ + {"Stipple" , cdlua5_stipple}, + {"wStipple" , wdlua5_stipple}, + {"GetStipple" , cdlua5_getstipple}, + + /* Pattern */ + {"Pattern" , cdlua5_pattern}, + {"wPattern" , wdlua5_pattern}, + {"GetPattern" , cdlua5_getpattern}, + + /* Text */ + {"Text" , cdlua5_text}, + {"wText" , wdlua5_text}, + {"Font" , cdlua5_font}, + {"wFont" , wdlua5_font}, + {"GetFont" , cdlua5_getfont}, + {"wGetFont" , wdlua5_getfont}, + {"NativeFont" , cdlua5_nativefont}, + {"TextAlignment" , cdlua5_textalignment}, + {"TextOrientation" , cdlua5_textorientation}, + {"FontDim" , cdlua5_fontdim}, + {"wFontDim" , wdlua5_fontdim}, + {"TextSize" , cdlua5_textsize}, + {"wTextSize" , wdlua5_textsize}, + {"TextBox" , cdlua5_textbox}, + {"wTextBox" , wdlua5_textbox}, + {"TextBounds" , cdlua5_textbounds}, + {"wTextBounds" , wdlua5_textbounds}, + + /* Vector Text */ + {"VectorText" , cdlua5_vectortext}, + {"wVectorText" , wdlua5_vectortext}, + {"MultiLineVectorText" , cdlua5_multilinevectortext}, + {"wMultiLineVectorText" , wdlua5_multilinevectortext}, + {"VectorTextDirection" , cdlua5_vectortextdirection}, + {"wVectorTextDirection" , wdlua5_vectortextdirection}, + {"VectorTextTransform" , cdlua5_vectortexttransform}, + {"VectorTextSize" , cdlua5_vectortextsize}, + {"wVectorTextSize" , wdlua5_vectortextsize}, + {"VectorCharSize" , cdlua5_vectorcharsize}, + {"wVectorCharSize" , wdlua5_vectorcharsize}, + {"VectorFont" , cdlua5_vectorfont}, + {"GetVectorTextSize" , cdlua5_getvectortextsize}, + {"wGetVectorTextSize" , wdlua5_getvectortextsize}, + {"VectorTextBounds" , cdlua5_vectortextbounds}, + {"wVectorTextBounds" , wdlua5_vectortextbounds}, + + /* Client Images */ + {"GetImageRGB" , cdlua5_getimagergb}, + {"PutImageRectRGB" , cdlua5_putimagerectrgb}, + {"wPutImageRectRGB" , wdlua5_putimagerectrgb}, + {"PutImageRectRGBA" , cdlua5_putimagerectrgba}, + {"wPutImageRectRGBA", wdlua5_putimagerectrgba}, + {"PutImageRectMap" , cdlua5_putimagerectmap}, + {"wPutImageRectMap" , wdlua5_putimagerectmap}, + {"GetBitmap" , cdlua5_getbitmap}, + {"PutBitmap" , cdlua5_putbitmap}, + {"wPutBitmap" , wdlua5_putbitmap}, + + {"ImageRGB" , cdlua5_imagergb}, + {"ImageRGBBitmap" , cdlua5_imagergbbitmap}, + + /* Server Images */ + {"CreateImage" , cdlua5_createimage}, + {"GetImage" , cdlua5_getimage}, + {"PutImageRect" , cdlua5_putimagerect}, + {"wPutImageRect" , wdlua5_putimagerect}, + {"ScrollArea" , cdlua5_scrollarea}, + + /* Other */ + {"Play" , cdlua5_play}, + + /* Color Coding */ + {"GetColorPlanes" , cdlua5_getcolorplanes}, + + /* Palette */ + {"Palette" , cdlua5_palette}, + + /* Polygon */ + {"Begin" , cdlua5_begin}, + {"Vertex" , cdlua5_vertex}, + {"wVertex" , wdlua5_vertex}, + {"End" , cdlua5_end}, + + {NULL, NULL}, +}; + +typedef struct cdlua5_constant { + const char *name; + lua_Number value; +} cdlua5_constant; + +/* old backward compatible constants */ +static const struct cdlua5_constant cdlibconstant[] = { + {"SYSTEM", CD_SYSTEM}, + {"COURIER", CD_COURIER}, + {"TIMES_ROMAN", CD_TIMES_ROMAN}, + {"HELVETICA", CD_HELVETICA}, + {"NATIVE", CD_NATIVE}, + {"CLIPON", CD_CLIPON}, + {"CENTER_BASE", CD_CENTER_BASE}, + {"LEFT_BASE", CD_LEFT_BASE}, + {"RIGHT_BASE", CD_RIGHT_BASE}, + {"ITALIC_BOLD", CD_ITALIC_BOLD}, + + {NULL, -1} +}; + +static void initconst(lua_State *L) +{ + const cdlua5_constant *l = cdlibconstant; + for (; l->name; l++) { + lua_pushstring(L, l->name); + lua_pushnumber(L, l->value); + lua_settable(L, -3); + } +} + +void cdlua_open_active (lua_State *L, cdluaLuaState* cdL) +{ + /* "cd" table is at the top of the stack */ + luaL_register(L, NULL, cdlib_active); + initconst(L); + + cdL->void_canvas = cdCreateCanvas(CD_VOID, NULL); + cdlua_setvoidstate(cdL->void_canvas, L); + cdActivate(cdL->void_canvas); +} diff --git a/cd/src/lua5/cdlua5_canvas.c b/cd/src/lua5/cdlua5_canvas.c new file mode 100755 index 0000000..d04f76f --- /dev/null +++ b/cd/src/lua5/cdlua5_canvas.c @@ -0,0 +1,2405 @@ +/** \file + * \brief Lua Binding of the Canvas dependent API + * + * See Copyright Notice in cd.h + */ + + +#include +#include +#include + +#include "cd.h" +#include "wd.h" +#include "cdirgb.h" + +#include +#include + +#include "cdlua.h" +#include "cdlua5_private.h" + +#define _cdCheckCanvas(_canvas) (_canvas!=NULL && ((unsigned char*)_canvas)[0] == 'C' && ((unsigned char*)_canvas)[1] == 'D') + + +void cdlua_pushcanvas(lua_State * L, cdCanvas* canvas) +{ + cdCanvas* *canvas_p = (cdCanvas* *) lua_newuserdata(L, sizeof(cdCanvas*)); + *canvas_p = canvas; + luaL_getmetatable(L, "cdCanvas"); + lua_setmetatable(L, -2); +} + +cdCanvas* cdlua_checkcanvas(lua_State * L, int pos) +{ + cdCanvas* *canvas_p = (cdCanvas**)luaL_checkudata(L, pos, "cdCanvas"); + if (!(*canvas_p)) + luaL_argerror(L, pos, "killed cdCanvas"); + if (!_cdCheckCanvas(*canvas_p)) + luaL_argerror(L, pos, "invalid Lua object, killed cdCanvas in C but not in Lua"); + return *canvas_p; +} + +cdCanvas* cdlua_getcanvas(lua_State * L) +{ + return cdlua_checkcanvas(L, 1); +} + +static int cdlua5_createcanvas(lua_State * L) +{ + cdluaContext* cdlua_ctx = cdlua_getcontext(L, 1); + void *data_p = cdlua_ctx->checkdata(L, 2); + + cdCanvas* canvas = cdCreateCanvas(cdlua_ctx->ctx(), data_p); + + /* if creation failed, return nil so that the user can compare */ + /* the result with nil and know that it failed */ + if (canvas) + cdlua_pushcanvas(L, canvas); + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_killcanvas(lua_State *L) +{ + cdCanvas* *canvas_p = (cdCanvas**) luaL_checkudata(L, 1, "cdCanvas"); + if (!(*canvas_p)) + luaL_argerror(L, 1, "killed cdCanvas"); + if (!_cdCheckCanvas(*canvas_p)) + luaL_argerror(L, 1, "invalid Lua object, killed cdCanvas in C but not in Lua"); + + cdlua_kill_active(L, *canvas_p); + + cdKillCanvas(*canvas_p); + *canvas_p = NULL; /* mark as killed */ + + return 0; +} + +static int cdluaCanvas_eq (lua_State *L) +{ + cdCanvas* canvas1 = cdlua_checkcanvas(L, 1); + cdCanvas* canvas2 = cdlua_checkcanvas(L, 2); + lua_pushboolean(L, canvas1 == canvas2); + return 1; +} + +static int cdluaCanvas_tostring (lua_State *L) +{ + cdCanvas* *canvas_p = (cdCanvas**) luaL_checkudata(L, 1, "cdCanvas"); + if (!(*canvas_p)) + lua_pushfstring(L, "cdCanvas(%p - NULL)-killed", canvas_p); + else if (!_cdCheckCanvas(*canvas_p)) + lua_pushfstring(L, "cdCanvas(%p - INVALID)-killed in C but not in Lua", canvas_p); + else + lua_pushfstring(L, "cdCanvas(%p - %p)", canvas_p, *canvas_p); + return 1; +} + +static int cdlua5_getcontext(lua_State * L) +{ + cdluaLuaState* cdL; + cdContext* ctx; + int i; + int driver = -1; + cdCanvas* canvas = cdlua_checkcanvas(L, 1); + + ctx = cdCanvasGetContext(canvas); + cdL = cdlua_getstate(L); + + for (i=0; i < cdL->numdrivers; i++) + { + if (ctx == cdL->drivers[i]->ctx()) + { + driver = i; + break; + } + } + + if (i == cdL->numdrivers) + luaL_argerror(L, 1, "unknown driver"); + + lua_pushnumber(L, driver); + return 1; +} + +/***************************************************************************\ +* Activates a cd canvas. * +\***************************************************************************/ +static int cdlua5_activate(lua_State * L) +{ + lua_pushnumber(L, cdCanvasActivate(cdlua_checkcanvas(L, 1))); + return 1; +} + +static int cdlua5_deactivate(lua_State * L) +{ + cdCanvasDeactivate(cdlua_checkcanvas(L, 1)); + return 0; +} + +/***************************************************************************\ +* cd.Simulate(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_simulate(lua_State *L) +{ + lua_pushnumber(L, cdCanvasSimulate(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* Control * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Clear() * +\***************************************************************************/ +static int cdlua5_clear(lua_State *L) +{ + cdCanvasClear(cdlua_checkcanvas(L, 1)); + return 0; +} + +/***************************************************************************\ +* cd.Flush() * +\***************************************************************************/ +static int cdlua5_flush(lua_State *L) +{ + cdCanvasFlush(cdlua_checkcanvas(L, 1)); + return 0; +} + +static int cdlua5_savestate(lua_State *L) +{ + cdState* state = cdCanvasSaveState(cdlua_checkcanvas(L, 1)); + if (state) + cdlua_pushstate(L, state); + else + lua_pushnil(L); + return 1; +} + +static int cdlua5_restorestate(lua_State * L) +{ + cdCanvasRestoreState(cdlua_checkcanvas(L, 1), cdlua_checkstate(L, 2)); + return 0; +} + +static int cdlua_isuserdata(const char* name) +{ + if (strcmp(name, "HDC")==0) return 1; + if (strcmp(name, "GC")==0) return 1; + return 0; +} + +/***************************************************************************\ +* cd.SetAttribute(name, data: string) * +\***************************************************************************/ +static int cdlua5_setattribute(lua_State *L) +{ + const char* name = luaL_checkstring(L, 2); + + if (lua_isnil(L, 2)) + { + cdCanvasSetAttribute(cdlua_checkcanvas(L, 1), name, NULL); + } + else + { + char* data; + if (cdlua_isuserdata(name)) + data = (char*) lua_touserdata(L, 3); + else + data = (char*) luaL_checkstring(L, 3); + cdCanvasSetAttribute(cdlua_checkcanvas(L, 1), name, data); + } + return 0; +} + + +/***************************************************************************\ +* cd.SetAttribute(name: string) -> (data: string) * +\***************************************************************************/ +static int cdlua5_getattribute(lua_State *L) +{ + const char* name = luaL_checkstring(L, 2); + char* data = cdCanvasGetAttribute(cdlua_checkcanvas(L, 1), name); + if (data) + { + if (cdlua_isuserdata(name)) + lua_pushlightuserdata(L, data); + else + lua_pushstring(L, data); + } + else + lua_pushnil(L); + return 1; +} + + + +/***************************************************************************\ +* Coordinate System * +\***************************************************************************/ + +/***************************************************************************\ +* cd.GetCanvasSize() -> (width, heigth, mm_width, mm_height: number) * +\***************************************************************************/ +static int cdlua5_getcanvassize(lua_State *L) +{ + int width; + int height; + double mm_width; + double mm_height; + + cdCanvasGetSize(cdlua_checkcanvas(L, 1), &width, &height, &mm_width, &mm_height); + + lua_pushnumber(L, width); + lua_pushnumber(L, height); + lua_pushnumber(L, mm_width); + lua_pushnumber(L, mm_height); + return 4; +} + +/***************************************************************************\ +* cd.UpdateYAxis(yc: number) -> (yr: number) * +\***************************************************************************/ +static int cdlua5_updateyaxis(lua_State *L) +{ + double y = luaL_checknumber(L, 2); + cdfCanvasUpdateYAxis(cdlua_checkcanvas(L, 1), &y); + lua_pushnumber(L, y); + return 1; +} + +static int cdlua5_invertyaxis(lua_State *L) +{ + lua_pushnumber(L, cdfCanvasInvertYAxis(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +static int cdlua5_mm2pixel(lua_State *L) +{ + double mm_dx, mm_dy; + int dx, dy; + + mm_dx = luaL_checknumber(L,2); + mm_dy = luaL_checknumber(L,3); + + cdCanvasMM2Pixel(cdlua_checkcanvas(L, 1), mm_dx, mm_dy, &dx, &dy); + lua_pushnumber(L, dx); + lua_pushnumber(L, dy); + return 2; +} + +static int cdlua5_pixel2mm(lua_State *L) +{ + double mm_dx_d, mm_dy_d; + int dx, dy; + + dx = luaL_checkint(L,2); + dy = luaL_checkint(L,3); + + cdCanvasPixel2MM(cdlua_checkcanvas(L, 1), dx, dy, &mm_dx_d, &mm_dy_d); + lua_pushnumber(L, mm_dx_d); + lua_pushnumber(L, mm_dy_d); + return 2; +} + +static int cdlua5_fmm2pixel(lua_State *L) +{ + double mm_dx, mm_dy; + double dx, dy; + + mm_dx = luaL_checknumber(L,2); + mm_dy = luaL_checknumber(L,3); + + cdfCanvasMM2Pixel(cdlua_checkcanvas(L, 1), mm_dx, mm_dy, &dx, &dy); + lua_pushnumber(L, dx); + lua_pushnumber(L, dy); + return 2; +} + +static int cdlua5_fpixel2mm(lua_State *L) +{ + double mm_dx_d, mm_dy_d; + double dx, dy; + + dx = luaL_checknumber(L,2); + dy = luaL_checknumber(L,3); + + cdfCanvasPixel2MM(cdlua_checkcanvas(L, 1), dx, dy, &mm_dx_d, &mm_dy_d); + lua_pushnumber(L, mm_dx_d); + lua_pushnumber(L, mm_dy_d); + return 2; +} + +static int cdlua5_origin(lua_State *L) +{ + cdCanvasOrigin(cdlua_checkcanvas(L, 1), luaL_checkint(L,2), luaL_checkint(L,3)); + return 0; +} + +static int cdlua5_getorigin(lua_State *L) +{ + int x, y; + cdCanvasGetOrigin(cdlua_checkcanvas(L, 1), &x, &y); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + return 2; +} + +static int cdlua5_forigin(lua_State *L) +{ + cdfCanvasOrigin(cdlua_checkcanvas(L, 1), luaL_checknumber(L,2), luaL_checknumber(L,3)); + return 0; +} + +static int cdlua5_fgetorigin(lua_State *L) +{ + double x, y; + cdfCanvasGetOrigin(cdlua_checkcanvas(L, 1), &x, &y); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + return 2; +} + +static int cdlua5_transform(lua_State *L) +{ + double matrix[6]; + int i; + + if (!lua_istable(L, 2)) + luaL_argerror(L, 2, "invalid matrix, must be a table"); + + for (i=0; i < 6; i++) + { + lua_rawgeti(L, 2, i+1); + + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 2, "invalid matrix value, must be a number"); + + matrix[i] = lua_tonumber(L, -1); + lua_pop(L, 1); + } + + cdCanvasTransform(cdlua_checkcanvas(L, 1), matrix); + return 0; +} + +static int cdlua5_gettransform(lua_State *L) +{ + int i; + double* matrix = cdCanvasGetTransform(cdlua_checkcanvas(L, 1)); + lua_newtable(L); + for (i=0; i < 6; i++) + { + lua_pushnumber(L, matrix[i]); + lua_rawseti(L, 1, i+1); + } + return 1; +} + +static int cdlua5_transformmultiply(lua_State *L) +{ + double matrix[6]; + int i; + + if (!lua_istable(L, 2)) + luaL_argerror(L, 2, "invalid matrix, must be a table"); + + for (i=0; i < 6; i++) + { + lua_rawgeti(L, 2, i+1); + + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 1, "invalid matrix value, must be a number"); + + matrix[i] = lua_tonumber(L, -1); + lua_pop(L, 1); + } + + cdCanvasTransformMultiply(cdlua_checkcanvas(L, 1), matrix); + return 0; +} + +static int cdlua5_transformrotate(lua_State *L) +{ + cdCanvasTransformRotate(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2)); + return 0; +} + +static int cdlua5_transformscale(lua_State *L) +{ + cdCanvasTransformScale(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); + return 0; +} + +static int cdlua5_transformtranslate(lua_State *L) +{ + cdCanvasTransformTranslate(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); + return 0; +} + +static int cdlua5_transformpoint(lua_State *L) +{ + int x, y; + cdCanvasTransformPoint(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3), &x, &y); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + return 2; +} + +static int cdlua5_ftransformpoint(lua_State *L) +{ + double x, y; + cdfCanvasTransformPoint(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), &x, &y); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + return 2; +} + +/***************************************************************************\ +* World Coordinates * +\***************************************************************************/ + +/***************************************************************************\ +* wd.Window(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_window(lua_State *L) +{ + double xmin = luaL_checknumber(L, 2); + double xmax = luaL_checknumber(L, 3); + double ymin = luaL_checknumber(L, 4); + double ymax = luaL_checknumber(L, 5); + wdCanvasWindow(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* wd.GetWindow() -> (xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_getwindow(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + + wdCanvasGetWindow(cdlua_checkcanvas(L, 1), &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/***************************************************************************\ +* wd.Viewport(xmin, xmax, ymin, ymax: number) * +\***************************************************************************/ +static int wdlua5_viewport(lua_State *L) +{ + int xmin = luaL_checkint(L, 2); + int xmax = luaL_checkint(L, 3); + int ymin = luaL_checkint(L, 4); + int ymax = luaL_checkint(L, 5); + wdCanvasViewport(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* wd.GetViewport() -> (xmin, xmax, ymin, ymax: number * +\***************************************************************************/ +static int wdlua5_getviewport(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + + wdCanvasGetViewport(cdlua_checkcanvas(L, 1), &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/***************************************************************************\ +* wd.World2Canvas(xw, yw: number) -> (xv, yv: number) * +\***************************************************************************/ +static int wdlua5_world2canvas(lua_State *L) +{ + double xw_d, yw_d; + int xv_i, yv_i; + + xw_d = luaL_checknumber(L, 2); + yw_d = luaL_checknumber(L, 3); + + wdCanvasWorld2Canvas(cdlua_checkcanvas(L, 1), xw_d, yw_d, &xv_i, &yv_i); + lua_pushnumber(L, xv_i); + lua_pushnumber(L, yv_i); + return 2; +} + +/***************************************************************************\ +* wd.Canvas2World(xv, yv: number) -> (xw, yw: number) * +\***************************************************************************/ +static int wdlua5_canvas2world(lua_State *L) +{ + int xv_i, yv_i; + double xw_d, yw_d; + + xv_i = luaL_checkint(L, 2); + yv_i = luaL_checkint(L, 3); + + wdCanvasCanvas2World(cdlua_checkcanvas(L, 1), xv_i, yv_i, &xw_d, &yw_d); + lua_pushnumber(L, xw_d); + lua_pushnumber(L, yw_d); + return 2; +} + + + +/***************************************************************************\ +* General Attributes * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Foreground(color) -> color * +\***************************************************************************/ +static int cdlua5_foreground(lua_State *L) +{ + long int color_i = cdlua_checkcolor(L, 2); + color_i = cdCanvasForeground(cdlua_checkcanvas(L, 1), color_i); + lua_pushlightuserdata(L, (void*) color_i); + return 1; +} + +static int cdlua5_setforeground(lua_State *L) +{ + long int color_i = cdlua_checkcolor(L, 2); + cdCanvasSetForeground(cdlua_checkcanvas(L, 1), color_i); + return 0; +} + +/***************************************************************************\ +* cd.Background(color) -> color * +\***************************************************************************/ +static int cdlua5_background(lua_State *L) +{ + long int color_i = cdlua_checkcolor(L, 2); + color_i = cdCanvasBackground(cdlua_checkcanvas(L, 1), color_i); + lua_pushlightuserdata(L, (void*) color_i); + return 1; +} + +static int cdlua5_setbackground(lua_State *L) +{ + long int color_i = cdlua_checkcolor(L, 2); + cdCanvasSetBackground(cdlua_checkcanvas(L, 1), color_i); + return 0; +} +/***************************************************************************\ +* cd.WriteMode(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_writemode(lua_State *L) +{ + lua_pushnumber(L, cdCanvasWriteMode(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + + + +/***************************************************************************\ +* Clipping * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Clip(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_clip(lua_State *L) +{ + lua_pushnumber(L, cdCanvasClip(cdlua_checkcanvas(L, 1), luaL_checkint(L,2))); + return 1; +} + +static int cdlua5_cliparea(lua_State *L) +{ + int xmin = luaL_checkint(L, 2); + int xmax = luaL_checkint(L, 3); + int ymin = luaL_checkint(L, 4); + int ymax = luaL_checkint(L, 5); + + cdCanvasClipArea(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_cliparea(lua_State *L) +{ + double xmin = luaL_checknumber(L, 2); + double xmax = luaL_checknumber(L, 3); + double ymin = luaL_checknumber(L, 4); + double ymax = luaL_checknumber(L, 5); + + wdCanvasClipArea(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_fcliparea(lua_State *L) +{ + double xmin = luaL_checknumber(L, 2); + double xmax = luaL_checknumber(L, 3); + double ymin = luaL_checknumber(L, 4); + double ymax = luaL_checknumber(L, 5); + + cdfCanvasClipArea(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_getcliparea(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + int status; + + status = cdCanvasGetClipArea(cdlua_checkcanvas(L, 1), &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + lua_pushnumber(L, status); + return 5; +} + +static int wdlua5_getcliparea(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + int status; + + status = wdCanvasGetClipArea(cdlua_checkcanvas(L, 1), &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + lua_pushnumber(L, status); + return 5; +} + +static int cdlua5_fgetcliparea(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + int status; + + status = cdfCanvasGetClipArea(cdlua_checkcanvas(L, 1), &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + lua_pushnumber(L, status); + return 5; +} + +/***************************************************************************\ +* Regions * +\***************************************************************************/ + +/***************************************************************************\ +* cd.RegionCombineMode(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_regioncombinemode(lua_State *L) +{ + lua_pushnumber(L, cdCanvasRegionCombineMode(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.PointInRegion(x, y: number) -> (status: number) * +\***************************************************************************/ +static int cdlua5_pointinregion(lua_State *L) +{ + lua_pushnumber(L, cdCanvasIsPointInRegion(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3))); + return 1; +} + +/***************************************************************************\ +* cd.wPointInRegion(x, y: number) -> (status: number) * +\***************************************************************************/ +static int wdlua5_pointinregion(lua_State *L) +{ + lua_pushnumber(L, wdCanvasIsPointInRegion(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3))); + return 1; +} + +/***************************************************************************\ +* cd.OffsetRegion(dx, dy: number) * +\***************************************************************************/ +static int cdlua5_offsetregion(lua_State *L) +{ + cdCanvasOffsetRegion(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3)); + return 0; +} + +/***************************************************************************\ +* cd.wOffsetRegion(dx, dy: number) * +\***************************************************************************/ +static int wdlua5_offsetregion(lua_State *L) +{ + wdCanvasOffsetRegion(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); + return 0; +} + +/***************************************************************************\ +* cd.RegionBox() -> (xmin, xmax, ymin, ymax, status: number) * +\***************************************************************************/ +static int cdlua5_regionbox(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + + cdCanvasGetRegionBox(cdlua_checkcanvas(L, 1), &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/***************************************************************************\ +* cd.wRegionBox() -> (xmin, xmax, ymin, ymax, status: number) * +\***************************************************************************/ +static int wdlua5_regionbox(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + + wdCanvasGetRegionBox(cdlua_checkcanvas(L, 1), &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + + +/***************************************************************************\ +* Primitives * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Pixel(x, y: number, color) * +\***************************************************************************/ +static int cdlua5_pixel (lua_State *L) +{ + cdCanvasPixel(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3), cdlua_checkcolor(L, 4)); + return 0 ; +} + +/***************************************************************************\ +* cd.wPixel(x, y: number, color) * +\***************************************************************************/ +static int wdlua5_pixel (lua_State *L) +{ + wdCanvasPixel(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), cdlua_checkcolor(L, 4)); + return 0; +} + +/***************************************************************************\ +* cd.Mark(x, y: number) * +\***************************************************************************/ +static int cdlua5_mark(lua_State *L) +{ + cdCanvasMark(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3)); + return 0; +} + +/***************************************************************************\ +* cd.wMark(x, y: number) * +\***************************************************************************/ +static int wdlua5_mark(lua_State *L) +{ + wdCanvasMark(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); + return 0; +} + +/***************************************************************************\ +* cd.MarkType(type: number) -> (old_type: number) * +\***************************************************************************/ +static int cdlua5_marktype(lua_State *L) +{ + lua_pushnumber(L, cdCanvasMarkType(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.MarkSize(size: number) -> (old_size: number) * +\***************************************************************************/ +static int cdlua5_marksize(lua_State *L) +{ + lua_pushnumber(L, cdCanvasMarkSize(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.wMarkSize(size: number) -> (old_size: number) * +\***************************************************************************/ +static int wdlua5_marksize(lua_State *L) +{ + lua_pushnumber(L, wdCanvasMarkSize(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2))); + return 1; +} + + + +/***************************************************************************\ +* Lines * +\***************************************************************************/ + +static int cdlua5_line(lua_State *L) +{ + int x1 = luaL_checkint(L,2); + int y1 = luaL_checkint(L,3); + int x2 = luaL_checkint(L,4); + int y2 = luaL_checkint(L,5); + cdCanvasLine(cdlua_checkcanvas(L, 1), x1, y1, x2, y2); + return 0; +} + +static int wdlua5_line(lua_State *L) +{ + double x1 = luaL_checknumber(L, 2); + double y1 = luaL_checknumber(L, 3); + double x2 = luaL_checknumber(L, 4); + double y2 = luaL_checknumber(L, 5); + wdCanvasLine(cdlua_checkcanvas(L, 1), x1, y1, x2, y2); + return 0; +} + +static int cdlua5_fline(lua_State *L) +{ + double x1 = luaL_checknumber(L, 2); + double y1 = luaL_checknumber(L, 3); + double x2 = luaL_checknumber(L, 4); + double y2 = luaL_checknumber(L, 5); + cdfCanvasLine(cdlua_checkcanvas(L, 1), x1, y1, x2, y2); + return 0; +} + +static int cdlua5_rect(lua_State *L) +{ + int xmin = luaL_checkint(L,2); + int xmax = luaL_checkint(L,3); + int ymin = luaL_checkint(L,4); + int ymax = luaL_checkint(L,5); + cdCanvasRect(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_rect(lua_State *L) +{ + double xmin = luaL_checknumber(L, 2); + double xmax = luaL_checknumber(L, 3); + double ymin = luaL_checknumber(L, 4); + double ymax = luaL_checknumber(L, 5); + wdCanvasRect(cdlua_checkcanvas(L, 1), xmin,xmax,ymin,ymax); + return 0; +} + +static int cdlua5_frect(lua_State *L) +{ + double xmin = luaL_checknumber(L, 2); + double xmax = luaL_checknumber(L, 3); + double ymin = luaL_checknumber(L, 4); + double ymax = luaL_checknumber(L, 5); + cdfCanvasRect(cdlua_checkcanvas(L, 1), xmin,xmax,ymin,ymax); + return 0; +} + +static int cdlua5_arc(lua_State *L) +{ + int xc = luaL_checkint(L,2); + int yc = luaL_checkint(L,3); + int w = luaL_checkint(L,4); + int h = luaL_checkint(L,5); + double angle1 = luaL_checknumber(L,6); + double angle2 = luaL_checknumber(L,7); + cdCanvasArc(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +static int wdlua5_arc(lua_State *L) +{ + double xc = luaL_checknumber(L, 2); + double yc = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + double angle1 = luaL_checknumber(L, 6); + double angle2 = luaL_checknumber(L, 7); + wdCanvasArc(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +static int cdlua5_farc(lua_State *L) +{ + double xc = luaL_checknumber(L, 2); + double yc = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + double angle1 = luaL_checknumber(L, 6); + double angle2 = luaL_checknumber(L, 7); + cdfCanvasArc(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +/***************************************************************************\ +* cd.LineStyle(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_linestyle(lua_State *L) +{ + lua_pushnumber(L, cdCanvasLineStyle(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.LineStyleDashes(dashes: table, count: number) * +\***************************************************************************/ +static int cdlua5_linestyledashes(lua_State *L) +{ + int *dashes_int, dashes_count, i; + + if (!lua_istable(L, 2)) + luaL_argerror(L, 2, "invalid dashes, must be a table"); + + dashes_count = luaL_checkint(L, 3); + dashes_int = (int*) malloc(dashes_count * sizeof(int)); + + for (i=0; i < dashes_count; i++) + { + lua_pushnumber(L, i+1); + lua_gettable(L, 2); + + dashes_int[i] = luaL_checkint(L,-1); + } + + cdCanvasLineStyleDashes(cdlua_checkcanvas(L, 1), dashes_int, dashes_count); + free(dashes_int); + + return 0; +} + +/***************************************************************************\ +* cd.LineWidth(width: number) -> (old_width: number) * +\***************************************************************************/ +static int cdlua5_linewidth(lua_State *L) +{ + lua_pushnumber(L, cdCanvasLineWidth(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.wLineWidth(width: number) -> (old_width: number) * +\***************************************************************************/ +static int wdlua5_linewidth(lua_State *L) +{ + lua_pushnumber(L, wdCanvasLineWidth(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.LineJoin(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_linejoin(lua_State *L) +{ + lua_pushnumber(L, cdCanvasLineJoin(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.LineCap(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_linecap(lua_State *L) +{ + lua_pushnumber(L, cdCanvasLineCap(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + + + +/***************************************************************************\ +* Filled Areas * +\***************************************************************************/ + +static int cdlua5_box(lua_State *L) +{ + int xmin = luaL_checkint(L, 2); + int xmax = luaL_checkint(L, 3); + int ymin = luaL_checkint(L, 4); + int ymax = luaL_checkint(L, 5); + cdCanvasBox(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_box(lua_State *L) +{ + double xmin = luaL_checknumber(L, 2); + double xmax = luaL_checknumber(L, 3); + double ymin = luaL_checknumber(L, 4); + double ymax = luaL_checknumber(L, 5); + wdCanvasBox(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_fbox(lua_State *L) +{ + double xmin = luaL_checknumber(L, 2); + double xmax = luaL_checknumber(L, 3); + double ymin = luaL_checknumber(L, 4); + double ymax = luaL_checknumber(L, 5); + cdfCanvasBox(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_sector(lua_State *L) +{ + int xc = luaL_checkint(L,2); + int yc = luaL_checkint(L,3); + int w = luaL_checkint(L,4); + int h = luaL_checkint(L,5); + double angle1 = luaL_checknumber(L,6); + double angle2 = luaL_checknumber(L,7); + cdCanvasSector(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +static int wdlua5_sector(lua_State *L) +{ + double xc = luaL_checknumber(L, 2); + double yc = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + double angle1 = luaL_checknumber(L, 6); + double angle2 = luaL_checknumber(L, 7); + wdCanvasSector(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +static int cdlua5_fsector(lua_State *L) +{ + double xc = luaL_checknumber(L, 2); + double yc = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + double angle1 = luaL_checknumber(L, 6); + double angle2 = luaL_checknumber(L, 7); + cdfCanvasSector(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +static int cdlua5_chord(lua_State *L) +{ + int xc = luaL_checkint(L,2); + int yc = luaL_checkint(L,3); + int w = luaL_checkint(L,4); + int h = luaL_checkint(L,5); + double angle1 = luaL_checknumber(L,6); + double angle2 = luaL_checknumber(L,7); + cdCanvasChord(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +static int wdlua5_chord(lua_State *L) +{ + double xc = luaL_checknumber(L, 2); + double yc = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + double angle1 = luaL_checknumber(L, 6); + double angle2 = luaL_checknumber(L, 7); + wdCanvasChord(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +static int cdlua5_fchord(lua_State *L) +{ + double xc = luaL_checknumber(L, 2); + double yc = luaL_checknumber(L, 3); + double w = luaL_checknumber(L, 4); + double h = luaL_checknumber(L, 5); + double angle1 = luaL_checknumber(L, 6); + double angle2 = luaL_checknumber(L, 7); + cdfCanvasChord(cdlua_checkcanvas(L, 1), xc, yc, w, h, angle1, angle2); + return 0; +} + +/***************************************************************************\ +* cd.BackOpacity(opacity: number) -> (old_opacity: number) * +\***************************************************************************/ +static int cdlua5_backopacity(lua_State *L) +{ + lua_pushnumber(L, cdCanvasBackOpacity(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.FillMode(mode: number) -> (old_mode: number) * +\***************************************************************************/ +static int cdlua5_fillmode(lua_State *L) +{ + lua_pushnumber(L, cdCanvasFillMode(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.InteriorStyle(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_interiorstyle(lua_State *L) +{ + lua_pushnumber(L, cdCanvasInteriorStyle(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.Hatch(style: number) -> (old_style: number) * +\***************************************************************************/ +static int cdlua5_hatch(lua_State *L) +{ + lua_pushnumber(L, cdCanvasHatch(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +static int cdlua5_stipple(lua_State *L) +{ + cdluaStipple *stipple_p = cdlua_checkstipple(L, 2); + cdCanvasStipple(cdlua_checkcanvas(L, 1), stipple_p->width, stipple_p->height, stipple_p->stipple); + return 0 ; +} + +static int wdlua5_stipple(lua_State *L) +{ + cdluaStipple *stipple_p = cdlua_checkstipple(L, 2); + double w_mm = luaL_checknumber(L, 3); + double h_mm = luaL_checknumber(L, 4); + wdCanvasStipple(cdlua_checkcanvas(L, 1), stipple_p->width, stipple_p->height, stipple_p->stipple, w_mm, h_mm); + return 0; +} + +static int cdlua5_getstipple(lua_State *L) +{ + int width, height, size; + unsigned char *stipple, *new_stipple = NULL; + + stipple = cdCanvasGetStipple(cdlua_checkcanvas(L, 1), &width, &height); + + size = width * height; + + if (stipple) + new_stipple = (unsigned char *)malloc(size); + + if (new_stipple) + { + memcpy(new_stipple, stipple, size); + cdlua_pushstipple(L, new_stipple, width, height); + } + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_pattern(lua_State *L) +{ + cdluaPattern* pattern_p = cdlua_checkpattern(L, 2); + cdCanvasPattern(cdlua_checkcanvas(L, 1), pattern_p->width, pattern_p->height, pattern_p->pattern); + return 0; +} + +static int wdlua5_pattern(lua_State *L) +{ + cdluaPattern* pattern_p = cdlua_checkpattern(L, 2); + double w_mm = luaL_checknumber(L, 3); + double h_mm = luaL_checknumber(L, 4); + wdCanvasPattern(cdlua_checkcanvas(L, 1), pattern_p->width, pattern_p->height, pattern_p->pattern, w_mm, h_mm); + return 0; +} + +static int cdlua5_getpattern(lua_State *L) +{ + int width, height, size; + long int *pattern, *new_pattern = NULL; + + pattern = cdCanvasGetPattern(cdlua_checkcanvas(L, 1), &width, &height); + + size = width * height; + + if (pattern) + new_pattern = (long int *) malloc(size * sizeof(long int)); + + if (new_pattern) + { + memcpy(new_pattern, pattern, size * sizeof(long int)); + cdlua_pushpattern(L, new_pattern, width, height); + } + else + lua_pushnil(L); + + return 1; +} + +/***************************************************************************\ +* Text * +\***************************************************************************/ + +static int cdlua5_text(lua_State *L) +{ + const char* s = luaL_checkstring(L, 4); + cdCanvasText(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3), s); + return 0; +} + +static int wdlua5_text(lua_State *L) +{ + const char* s = luaL_checkstring(L, 4); + wdCanvasText(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), s); + return 0; +} + +static int cdlua5_ftext(lua_State *L) +{ + const char* s = luaL_checkstring(L, 4); + cdfCanvasText(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), s); + return 0; +} + +/***************************************************************************\ +* cd.Font(typeface, style, size: number) * +\***************************************************************************/ +static int cdlua5_font(lua_State *L) +{ + lua_pushnumber(L, cdCanvasFont(cdlua_checkcanvas(L, 1), luaL_checkstring(L, 2), luaL_checkint(L, 3), luaL_checkint(L, 4))); + return 1; +} + +/***************************************************************************\ +* cd.wFont(typeface, style, size: number) * +\***************************************************************************/ +static int wdlua5_font(lua_State *L) +{ + lua_pushnumber(L, wdCanvasFont(cdlua_checkcanvas(L, 1), luaL_checkstring(L, 2), luaL_checkint(L, 3), luaL_checknumber(L, 4))); + return 1; +} + + +/***************************************************************************\ +* cd.GetFont() -> (typeface, style, size: number) * +\***************************************************************************/ +static int cdlua5_getfont(lua_State *L) +{ + char type_face[1024]; + int style, size; + + cdCanvasGetFont(cdlua_checkcanvas(L, 1), type_face, &style, &size); + lua_pushstring(L, type_face); + lua_pushnumber(L, style); + lua_pushnumber(L, size); + return 3; +} + +/***************************************************************************\ +* cd.wGetFont() -> (typeface, style, size: number) * +\***************************************************************************/ +static int wdlua5_getfont(lua_State *L) +{ + char type_face[1024]; + int style; + double size; + + wdCanvasGetFont(cdlua_checkcanvas(L, 1), type_face, &style, &size); + lua_pushstring(L, type_face); + lua_pushnumber(L, style); + lua_pushnumber(L, size); + return 3; +} + +/***************************************************************************\ +* cd.NativeFont(font: string) * +\***************************************************************************/ +static int cdlua5_nativefont(lua_State *L) +{ + lua_pushstring(L, cdCanvasNativeFont(cdlua_checkcanvas(L, 1), luaL_checkstring(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.TextAlignment(alignment: number) -> (old_alignment: number) * +\***************************************************************************/ +static int cdlua5_textalignment(lua_State *L) +{ + lua_pushnumber(L, cdCanvasTextAlignment(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.TextOrientation(angle: number) -> (old_angle: number) * +\***************************************************************************/ +static int cdlua5_textorientation(lua_State *L) +{ + lua_pushnumber(L, cdCanvasTextOrientation(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.GetFontDim() -> (max_width, max_height, ascent, descent: number) * +\***************************************************************************/ +static int cdlua5_getfontdim(lua_State *L) +{ + int max_width; + int height; + int ascent; + int descent; + + cdCanvasGetFontDim(cdlua_checkcanvas(L, 1), &max_width, &height, &ascent, &descent); + lua_pushnumber(L, max_width); + lua_pushnumber(L, height); + lua_pushnumber(L, ascent); + lua_pushnumber(L, descent); + return 4; +} + +/***************************************************************************\ +* cd.wGetFontDim() -> (max_width, max_height, ascent, descent: number) * +\***************************************************************************/ +static int wdlua5_getfontdim(lua_State *L) +{ + double max_width; + double height; + double ascent; + double descent; + + wdCanvasGetFontDim(cdlua_checkcanvas(L, 1), &max_width, &height, &ascent, &descent); + lua_pushnumber(L, max_width); + lua_pushnumber(L, height); + lua_pushnumber(L, ascent); + lua_pushnumber(L, descent); + return 4; +} + +/***************************************************************************\ +* cd.GetTextSize(text: string) -> (width, heigth: number) * +\***************************************************************************/ +static int cdlua5_gettextsize(lua_State *L) +{ + int width; + int height; + cdCanvasGetTextSize(cdlua_checkcanvas(L, 1), luaL_checkstring(L, 2), &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/***************************************************************************\ +* cd.wGetTextSize(text: string) -> (width, heigth: number) * +\***************************************************************************/ +static int wdlua5_gettextsize(lua_State *L) +{ + double width; + double height; + wdCanvasGetTextSize(cdlua_checkcanvas(L, 1), luaL_checkstring(L, 2), &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/****************************************************************************\ +* cd.GetTextBox(x, y: number, text: string) -> (xmin, xmax, ymin, ymax: number) * +\****************************************************************************/ +static int cdlua5_gettextbox(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + const char* s = luaL_checkstring(L, 4); + + cdCanvasGetTextBox(cdlua_checkcanvas(L, 1), x, y, s, &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/*****************************************************************************\ +* cd.wGetTextBox(x, y: number, text: string) -> (xmin, xmax, ymin, ymax: number) * +\*****************************************************************************/ +static int wdlua5_gettextbox(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + double x = luaL_checknumber(L, 2); + double y = luaL_checknumber(L, 3); + const char* s = luaL_checkstring(L, 4); + + wdCanvasGetTextBox(cdlua_checkcanvas(L, 1), x, y, s, &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/***************************************************************************************************************\ +* cd.GetTextBounds(x, y: number, text: string) -> (rect0, rect1, rect2, rect3, rect4, rect5, rect6, rect7: number) * +\***************************************************************************************************************/ +static int cdlua5_gettextbounds(lua_State *L) +{ + int rect[8]; + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + const char* s = luaL_checkstring(L, 4); + + cdCanvasGetTextBounds(cdlua_checkcanvas(L, 1), x, y, s, rect); + lua_pushnumber(L, rect[0]); + lua_pushnumber(L, rect[1]); + lua_pushnumber(L, rect[2]); + lua_pushnumber(L, rect[3]); + lua_pushnumber(L, rect[4]); + lua_pushnumber(L, rect[5]); + lua_pushnumber(L, rect[6]); + lua_pushnumber(L, rect[7]); + return 4; +} + +/****************************************************************************************************************\ +* cd.wGetTextBounds(x, y: number, text: string) -> (rect0, rect1, rect2, rect3, rect4, rect5, rect6, rect7: number) * +\****************************************************************************************************************/ +static int wdlua5_gettextbounds(lua_State *L) +{ + double rect[8]; + double x = luaL_checknumber(L, 2); + double y = luaL_checknumber(L, 3); + const char* s = luaL_checkstring(L, 4); + + wdCanvasGetTextBounds(cdlua_checkcanvas(L, 1), x, y, s, rect); + lua_pushnumber(L, rect[0]); + lua_pushnumber(L, rect[1]); + lua_pushnumber(L, rect[2]); + lua_pushnumber(L, rect[3]); + lua_pushnumber(L, rect[4]); + lua_pushnumber(L, rect[5]); + lua_pushnumber(L, rect[6]); + lua_pushnumber(L, rect[7]); + return 4; +} + + + +/***************************************************************************\ +* Text * +\***************************************************************************/ + +/***************************************************************************\ +* cd.VectorText(x, y: number, text: string) * +\***************************************************************************/ +static int cdlua5_vectortext(lua_State *L) +{ + const char* s = luaL_checkstring(L,4); + cdCanvasVectorText(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3), s); + return 0; +} + +/***************************************************************************\ +* cd.wVectorText(x, y: number, text: string) * +\***************************************************************************/ +static int wdlua5_vectortext(lua_State *L) +{ + const char* s = luaL_checkstring(L, 4); + wdCanvasVectorText(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3),s); + return 0; +} + +/***************************************************************************\ +* cd.MultiLineVectorText(x, y: number, text: string) * +\***************************************************************************/ +static int cdlua5_multilinevectortext(lua_State *L) +{ + const char* s = luaL_checkstring(L, 4); + cdCanvasMultiLineVectorText(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3), s); + return 0; +} + +/***************************************************************************\ +* cd.wMultiLineVectorText(x, y: number, text: string) * +\***************************************************************************/ +static int wdlua5_multilinevectortext(lua_State *L) +{ + const char* s = luaL_checkstring(L, 4); + wdCanvasMultiLineVectorText(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), s); + return 0; +} + +/***************************************************************************\ +* cd.VectorTextDirection(x1, y1, x2, y2: number) * +\***************************************************************************/ +static int cdlua5_vectortextdirection(lua_State *L) +{ + int x1 = luaL_checkint(L,2); + int y1 = luaL_checkint(L,3); + int x2 = luaL_checkint(L,4); + int y2 = luaL_checkint(L,5); + cdCanvasVectorTextDirection(cdlua_checkcanvas(L, 1), x1, y1, x2, y2); + return 0; +} + +/***************************************************************************\ +* cd.wVectorTextDirection(x1, y1, x2, y2: number) * +\***************************************************************************/ +static int wdlua5_vectortextdirection(lua_State *L) +{ + double x1 = luaL_checknumber(L, 2); + double y1 = luaL_checknumber(L, 3); + double x2 = luaL_checknumber(L, 4); + double y2 = luaL_checknumber(L, 5); + wdCanvasVectorTextDirection(cdlua_checkcanvas(L, 1), x1, y1, x2, y2); + return 0; +} + + +/***************************************************************************\ +* cd.VectorTextTransform(matrix: table) -> (old_matrix: table) * +\***************************************************************************/ +static int cdlua5_vectortexttransform(lua_State *L) +{ + double matrix[6], *old_matrix; + int i; + + if (!lua_istable(L, 2)) + luaL_argerror(L, 2, "invalid matrix, must be a table"); + + for (i=0; i < 6; i++) + { + lua_rawgeti(L, 2, i+1); + + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 2, "invalid matrix value, must be a number"); + + matrix[i] = lua_tonumber(L, -1); + lua_pop(L, 1); + } + + old_matrix = cdCanvasVectorTextTransform(cdlua_checkcanvas(L, 1), matrix); + lua_newtable(L); + for (i=0; i < 6; i++) + { + lua_pushnumber(L, old_matrix[i]); + lua_rawseti(L, 1, i+1); + } + return 1; +} + +/***************************************************************************\ +* cd.VectorFontSize(w, h: number, text: string) * +\***************************************************************************/ +static int cdlua5_vectorfontsize(lua_State *L) +{ + cdCanvasVectorFontSize(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); + return 0; +} + +/***************************************************************************\ +* cd.GetVectorFontSize(text: string) -> (w, h: number) * +\***************************************************************************/ +static int cdlua5_getvectorfontsize(lua_State *L) +{ + double width; + double height; + cdCanvasGetVectorFontSize(cdlua_checkcanvas(L, 1), &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/***************************************************************************\ +* cd.VectorTextSize(w, h: number, text: string) * +\***************************************************************************/ +static int cdlua5_vectortextsize(lua_State *L) +{ + const char* s = luaL_checkstring(L, 4); + cdCanvasVectorTextSize(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3), s); + return 0; +} + +/***************************************************************************\ +* cd.wVectorTextSize(w, h: number, text: string) * +\***************************************************************************/ +static int wdlua5_vectortextsize(lua_State *L) +{ + const char* s = luaL_checkstring(L, 4); + wdCanvasVectorTextSize(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), s); + return 0; +} + +/***************************************************************************\ +* cd.VectorTextSize(w, h: number, text: string) * +\***************************************************************************/ +static int cdlua5_vectorcharsize(lua_State *L) +{ + lua_pushnumber(L, cdCanvasVectorCharSize(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.wVectorTextSize(w, h: number, text: string) * +\***************************************************************************/ +static int wdlua5_vectorcharsize(lua_State *L) +{ + lua_pushnumber(L, wdCanvasVectorCharSize(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.VectorFont(filename: string) -> (font_name: string) * +\***************************************************************************/ +static int cdlua5_vectorfont(lua_State *L) +{ + lua_pushstring(L, cdCanvasVectorFont(cdlua_checkcanvas(L, 1), luaL_checkstring(L, 2))); + return 1; +} + +/***************************************************************************\ +* cd.GetVectorTextSize(text: string) -> (w, h: number) * +\***************************************************************************/ +static int cdlua5_getvectortextsize(lua_State *L) +{ + int width; + int height; + cdCanvasGetVectorTextSize(cdlua_checkcanvas(L, 1), luaL_checkstring(L, 2), &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/***************************************************************************\ +* cd.wGetVectorTextSize(text: string) -> (w, h: number) * +\***************************************************************************/ +static int wdlua5_getvectortextsize(lua_State *L) +{ + double width; + double height; + wdCanvasGetVectorTextSize(cdlua_checkcanvas(L, 1), luaL_checkstring(L, 2), &width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +/***************************************************************************\ +* cd.GetVectorTextBounds(s: string, px,py: number) -> (rect: table) * +\***************************************************************************/ +static int cdlua5_getvectortextbounds(lua_State *L) +{ + const char* s = luaL_checkstring(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + int rect[8], i; + + cdCanvasGetVectorTextBounds(cdlua_checkcanvas(L, 1), s, x, y, rect); + lua_newtable(L); + for (i=0; i < 8; i++) + { + lua_pushnumber(L, rect[i]); + lua_rawseti(L, -2, i+1); + } + return 1; +} + +/***************************************************************************\ +* cd.wGetVectorTextBounds(s: string, px,py: number) -> (rect: table) * +\***************************************************************************/ +static int wdlua5_getvectortextbounds(lua_State *L) +{ + const char* s = luaL_checkstring(L, 2); + double x = luaL_checknumber(L, 3); + double y = luaL_checknumber(L, 4); + double rect[8]; + int i; + + wdCanvasGetVectorTextBounds(cdlua_checkcanvas(L, 1), s, x, y, rect); + lua_newtable(L); + for (i=0; i < 8; i++) + { + lua_pushnumber(L, rect[i]); + lua_rawseti(L, -2, i+1); + } + return 1; +} + +/****************************************************************************\ +* cd.GetVectorTextBox(x, y: number, text: string) -> (xmin, xmax, ymin, ymax: number) * +\****************************************************************************/ +static int cdlua5_getvectortextbox(lua_State *L) +{ + int xmin, xmax, ymin, ymax; + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + const char* s = luaL_checkstring(L, 4); + + cdCanvasGetVectorTextBox(cdlua_checkcanvas(L, 1), x, y, s, &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + +/*****************************************************************************\ +* cd.wGetVectorTextBox(x, y: number, text: string) -> (xmin, xmax, ymin, ymax: number) * +\*****************************************************************************/ +static int wdlua5_getvectortextbox(lua_State *L) +{ + double xmin, xmax, ymin, ymax; + double x = luaL_checknumber(L, 2); + double y = luaL_checknumber(L, 3); + const char* s = luaL_checkstring(L, 4); + + wdCanvasGetVectorTextBox(cdlua_checkcanvas(L, 1), x, y, s, &xmin, &xmax, &ymin, &ymax); + lua_pushnumber(L, xmin); + lua_pushnumber(L, xmax); + lua_pushnumber(L, ymin); + lua_pushnumber(L, ymax); + return 4; +} + + + +/***************************************************************************\ +* Client Images. * +\***************************************************************************/ + +static int cdlua5_getimagergb(lua_State *L) +{ + cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + cdCanvasGetImageRGB(cdlua_checkcanvas(L, 1), imagergb_p->red, imagergb_p->green, imagergb_p->blue, + x, y, imagergb_p->width, imagergb_p->height); + return 0; +} + +static int cdlua5_putimagerectrgb(lua_State *L) +{ + cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + int w = luaL_checkint(L, 5); + int h = luaL_checkint(L, 6); + int xmin = luaL_checkint(L, 7); + int xmax = luaL_checkint(L, 8); + int ymin = luaL_checkint(L, 9); + int ymax = luaL_checkint(L, 10); + + if (w < 0 || h < 0) + luaL_argerror(L, 5, "target region dimensions should be positive integers"); + + cdCanvasPutImageRectRGB(cdlua_checkcanvas(L, 1), imagergb_p->width, imagergb_p->height, imagergb_p->red, + imagergb_p->green, imagergb_p->blue, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_putimagerectrgb(lua_State *L) +{ + cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 2); + double x = luaL_checknumber(L, 3); + double y = luaL_checknumber(L, 4); + double w = luaL_checknumber(L, 5); + double h = luaL_checknumber(L, 6); + int xmin = luaL_checkint(L, 7); + int xmax = luaL_checkint(L, 8); + int ymin = luaL_checkint(L, 9); + int ymax = luaL_checkint(L, 10); + + if (w < 0 || h < 0) + luaL_argerror(L, 5, "target region dimensions should be positive integers"); + + wdCanvasPutImageRectRGB(cdlua_checkcanvas(L, 1), imagergb_p->width, imagergb_p->height, imagergb_p->red, + imagergb_p->green, imagergb_p->blue, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_putimagerectrgba(lua_State *L) +{ + cdluaImageRGBA* imagergba_p = cdlua_checkimagergba(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + int w = luaL_checkint(L, 5); + int h = luaL_checkint(L, 6); + int xmin = luaL_checkint(L, 7); + int xmax = luaL_checkint(L, 8); + int ymin = luaL_checkint(L, 9); + int ymax = luaL_checkint(L, 10); + + if (w < 0 || h < 0) + luaL_argerror(L, 5, "target region dimensions should be positive integers"); + + cdCanvasPutImageRectRGBA(cdlua_checkcanvas(L, 1), imagergba_p->width, imagergba_p->height, imagergba_p->red, + imagergba_p->green, imagergba_p->blue, imagergba_p->alpha, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_putimagerectrgba(lua_State *L) +{ + cdluaImageRGBA* imagergba_p = cdlua_checkimagergba(L, 2); + double x = luaL_checknumber(L, 3); + double y = luaL_checknumber(L, 4); + double w = luaL_checknumber(L, 5); + double h = luaL_checknumber(L, 6); + int xmin = luaL_checkint(L, 7); + int xmax = luaL_checkint(L, 8); + int ymin = luaL_checkint(L, 9); + int ymax = luaL_checkint(L, 10); + + if (w < 0 || h < 0) + luaL_argerror(L, 5, "target region dimensions should be positive integers"); + + wdCanvasPutImageRectRGBA(cdlua_checkcanvas(L, 1), imagergba_p->width, imagergba_p->height, imagergba_p->red, + imagergba_p->green, imagergba_p->blue, imagergba_p->alpha, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_putimagerectmap(lua_State *L) +{ + cdluaImageMap* imagemap_p = cdlua_checkimagemap(L, 2); + cdluaPalette *pal = cdlua_checkpalette(L, 3); + int x = luaL_checkint(L, 4); + int y = luaL_checkint(L, 5); + int w = luaL_checkint(L, 6); + int h = luaL_checkint(L, 7); + int xmin = luaL_checkint(L, 8); + int xmax = luaL_checkint(L, 9); + int ymin = luaL_checkint(L, 10); + int ymax = luaL_checkint(L, 11); + + if (w < 0 || h < 0) + luaL_argerror(L, 6, "target region dimensions should be positive integers"); + + cdCanvasPutImageRectMap(cdlua_checkcanvas(L, 1), imagemap_p->width, imagemap_p->height, imagemap_p->index, + pal->color, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_putimagerectmap(lua_State *L) +{ + cdluaImageMap* imagemap_p = cdlua_checkimagemap(L, 2); + cdluaPalette *pal = cdlua_checkpalette(L, 3); + double x = luaL_checknumber(L, 4); + double y = luaL_checknumber(L, 5); + double w = luaL_checknumber(L, 6); + double h = luaL_checknumber(L, 7); + int xmin = luaL_checkint(L, 8); + int xmax = luaL_checkint(L, 9); + int ymin = luaL_checkint(L, 10); + int ymax = luaL_checkint(L, 11); + + if (w < 0 || h < 0) + luaL_argerror(L, 6, "target region dimensions should be positive integers"); + + wdCanvasPutImageRectMap(cdlua_checkcanvas(L, 1), imagemap_p->width, imagemap_p->height, imagemap_p->index, + pal->color, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +static int cdlua5_putbitmap(lua_State *L) +{ + cdBitmap *bitmap = cdlua_checkbitmap(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + int w = luaL_checkint(L, 5); + int h = luaL_checkint(L, 6); + + if (w < 0 || h < 0) + luaL_argerror(L, 5, "target region dimensions should be positive integers"); + + cdCanvasPutBitmap(cdlua_checkcanvas(L, 1), bitmap, x, y, w, h); + return 0; +} + +static int wdlua5_putbitmap(lua_State *L) +{ + cdBitmap *bitmap = cdlua_checkbitmap(L, 2); + double x = luaL_checknumber(L, 3); + double y = luaL_checknumber(L, 4); + double w = luaL_checknumber(L, 5); + double h = luaL_checknumber(L, 6); + + if (w < 0 || h < 0) + luaL_argerror(L, 5, "target region dimensions should be positive integers"); + + wdCanvasPutBitmap(cdlua_checkcanvas(L, 1), bitmap, x, y, w, h); + return 0; +} + +static int cdlua5_getbitmap(lua_State *L) +{ + cdBitmap *bitmap = cdlua_checkbitmap(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + cdCanvasGetBitmap(cdlua_checkcanvas(L, 1), bitmap, x, y); + return 0; +} + +/***************************************************************************\ +* Server Images. * +\***************************************************************************/ + +static int cdlua5_createimage(lua_State *L) +{ + cdImage *image; + int width = luaL_checkint(L, 2); + int height = luaL_checkint(L, 3); + + if (width < 1 || height < 1) + luaL_argerror(L, 2, "image dimensions should be positive integers"); + + image = cdCanvasCreateImage(cdlua_checkcanvas(L, 1), width, height); + if (image) + cdlua_pushimage(L, image); + else + lua_pushnil(L); + + return 1; +} + +static int cdlua5_getimage(lua_State *L) +{ + cdImage* image = cdlua_checkimage(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + cdCanvasGetImage(cdlua_checkcanvas(L, 1), image, x, y); + return 0; +} + +static int cdlua5_putimagerect(lua_State *L) +{ + cdImage* image = cdlua_checkimage(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + int xmin = luaL_checkint(L, 5); + int xmax = luaL_checkint(L, 6); + int ymin = luaL_checkint(L, 7); + int ymax = luaL_checkint(L, 8); + cdCanvasPutImageRect(cdlua_checkcanvas(L, 1), image, x, y, xmin, xmax, ymin, ymax); + return 0; +} + +static int wdlua5_putimagerect(lua_State *L) +{ + cdImage* image = cdlua_checkimage(L, 2); + double x = luaL_checknumber(L, 3); + double y = luaL_checknumber(L, 4); + int xmin = luaL_checkint(L, 5); + int xmax = luaL_checkint(L, 6); + int ymin = luaL_checkint(L, 7); + int ymax = luaL_checkint(L, 8); + wdCanvasPutImageRect(cdlua_checkcanvas(L, 1), image, x, y, xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* cd.ScrollArea(xmin, xmax, ymin, ymax, dx, dy: number) * +\***************************************************************************/ +static int cdlua5_scrollarea(lua_State *L) +{ + int xmin = luaL_checkint(L, 2); + int xmax = luaL_checkint(L, 3); + int ymin = luaL_checkint(L, 4); + int ymax = luaL_checkint(L, 5); + int dx = luaL_checkint(L, 6); + int dy = luaL_checkint(L, 7); + cdCanvasScrollArea(cdlua_checkcanvas(L, 1), xmin, xmax, ymin, ymax, dx, dy); + return 0; +} + + + +/***************************************************************************\ +* Other * +\***************************************************************************/ + +/********************************************************************************\ +* cd.Play(ctx, xmin, xmax, ymin, ymax: number, data: string) -> (status: number) * +\********************************************************************************/ + +static int cdlua5_play(lua_State *L) +{ + cdluaContext* cdlua_ctx = cdlua_getcontext(L, 2); + int xmin = luaL_checkint(L,3); + int xmax = luaL_checkint(L,4); + int ymin = luaL_checkint(L,5); + int ymax = luaL_checkint(L,6); + const char *data_s = luaL_checkstring(L,7); + + cdlua_setplaystate(L); + cdCanvasPlay(cdlua_checkcanvas(L, 1), cdlua_ctx->ctx(), xmin, xmax, ymin, ymax, (void*)data_s); + cdlua_setplaystate(NULL); + return 0; +} + +/***************************************************************************\ +* cd.GetColorPlanes() -> (bpp: number) * +\***************************************************************************/ +static int cdlua5_getcolorplanes(lua_State *L) +{ + lua_pushnumber(L, cdCanvasGetColorPlanes(cdlua_checkcanvas(L, 1))); + return 1; +} + +static int cdlua5_palette(lua_State *L) +{ + cdluaPalette *pal = cdlua_checkpalette(L, 2); + int mode_i = luaL_checkint(L, 3); + cdCanvasPalette(cdlua_checkcanvas(L, 1), pal->count, pal->color, mode_i); + return 0; +} + +/***************************************************************************\ +* cd.ImageRGB * +\***************************************************************************/ +static int cdlua5_imagergb(lua_State *L) +{ + int w, h, type = CD_RGB; + + cdCanvas* canvas = cdlua_checkcanvas(L, 1); + + if (cdCanvasGetContext(canvas) != CD_IMAGERGB) + luaL_argerror(L, 1, "invalid canvas, must be CD_IMAGERGB"); + + if (cdAlphaImage(canvas)) + type = CD_RGBA; + + cdCanvasGetSize(canvas, &w, &h, NULL, NULL); + + /* mark the image NOT to be freed */ + if (type == CD_RGBA) + cdlua_pushimagergba_ex(L, cdRedImage(canvas), cdGreenImage(canvas), cdBlueImage(canvas), cdAlphaImage(canvas), w, h); + else + cdlua_pushimagergb_ex(L, cdRedImage(canvas), cdGreenImage(canvas), cdBlueImage(canvas), w, h); + + return 1; +} + +/***************************************************************************\ +* cd.ImageRGBBitmap * +\***************************************************************************/ +static int cdlua5_imagergbbitmap(lua_State *L) +{ + int w, h, type = CD_RGB; + + cdCanvas* canvas = cdlua_checkcanvas(L, 1); + + if (cdCanvasGetContext(canvas) != CD_IMAGERGB) + luaL_argerror(L, 1, "invalid canvas, must be CD_IMAGERGB"); + + if (cdAlphaImage(canvas)) + type = CD_RGBA; + + cdCanvasGetSize(canvas, &w, &h, NULL, NULL); + + cdlua_pushbitmap(L, cdInitBitmap(w, h, type, + cdRedImage(canvas), + cdGreenImage(canvas), + cdBlueImage(canvas), + cdAlphaImage(canvas))); + + return 1; +} + +/***************************************************************************\ +* Hardcopy * +\***************************************************************************/ +static lua_State* wdlua5_hardcopy_luaState = NULL; + +static void wdlua5_hardcopy_func(cdCanvas* canvas) +{ + lua_State* L = wdlua5_hardcopy_luaState; + lua_pushvalue(L, 4); /* push the function in the stack */ + cdlua_pushcanvas(L, canvas); + if(lua_pcall(L, 1, 0, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); +} + +static int wdlua5_hardcopy(lua_State *L) +{ + cdCanvas* canvas = cdlua_checkcanvas(L, 1); + cdluaContext* cdlua_ctx = cdlua_getcontext(L, 2); + void *data_p = cdlua_ctx->checkdata(L,3); + luaL_argcheck(L, !lua_isfunction(L, 4), 4, "invalid draw function"); + + wdlua5_hardcopy_luaState = L; + wdCanvasHardcopy(canvas, cdlua_ctx->ctx(), data_p, wdlua5_hardcopy_func); + wdlua5_hardcopy_luaState = NULL; + + return 0; +} + + +/***************************************************************************\ +* Polygon functions * +\***************************************************************************/ + +/***************************************************************************\ +* cd.Begin(mode: number) * +\***************************************************************************/ +static int cdlua5_begin(lua_State *L) +{ + cdCanvasBegin(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2)); + return 0; +} + +static int cdlua5_vertex(lua_State *L) +{ + cdCanvasVertex(cdlua_checkcanvas(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3)); + return 0; +} + +static int wdlua5_vertex(lua_State *L) +{ + wdCanvasVertex(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); + return 0; +} + +static int cdlua5_fvertex(lua_State *L) +{ + cdfCanvasVertex(cdlua_checkcanvas(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); + return 0; +} + +static int cdlua5_end(lua_State *L) +{ + cdCanvasEnd(cdlua_checkcanvas(L, 1)); + return 0; +} + + +/********************************************************************************\ +* Lua Exported functions * +\********************************************************************************/ + +static const struct luaL_reg cdlib_canvas_meta[] = { + + /* Initialization */ + {"GetContext" , cdlua5_getcontext}, + {"Kill" , cdlua5_killcanvas}, + {"Activate" , cdlua5_activate}, + {"Deactivate" , cdlua5_deactivate}, + {"Simulate" , cdlua5_simulate}, + + /* Control */ + {"Clear" , cdlua5_clear}, + {"Flush" , cdlua5_flush}, + {"SaveState" , cdlua5_savestate}, + {"RestoreState" , cdlua5_restorestate}, + {"SetAttribute" , cdlua5_setattribute}, + {"GetAttribute" , cdlua5_getattribute}, + + /* Coordinate System */ + {"GetSize" , cdlua5_getcanvassize}, + {"UpdateYAxis" , cdlua5_updateyaxis}, + {"InvertYAxis" , cdlua5_invertyaxis}, + {"MM2Pixel" , cdlua5_mm2pixel}, + {"Pixel2MM" , cdlua5_pixel2mm}, + {"Origin" , cdlua5_origin}, + {"GetOrigin" , cdlua5_getorigin}, + {"fMM2Pixel" , cdlua5_fmm2pixel}, + {"fPixel2MM" , cdlua5_fpixel2mm}, + {"fOrigin" , cdlua5_forigin}, + {"fGetOrigin" , cdlua5_fgetorigin}, + {"Transform" , cdlua5_transform}, + {"GetTransform" , cdlua5_gettransform}, + {"TransformMultiply" , cdlua5_transformmultiply}, + {"TransformRotate" , cdlua5_transformrotate}, + {"TransformScale" , cdlua5_transformscale}, + {"TransformTranslate" , cdlua5_transformtranslate}, + {"TransformPoint" , cdlua5_transformpoint}, + {"fTransformPoint" , cdlua5_ftransformpoint}, + + /* World Coordinates */ + {"wWindow" , wdlua5_window}, + {"wGetWindow" , wdlua5_getwindow}, + {"wViewport" , wdlua5_viewport}, + {"wGetViewport" , wdlua5_getviewport}, + {"wWorld2Canvas" , wdlua5_world2canvas}, + {"wCanvas2World" , wdlua5_canvas2world}, + + {"wHardcopy" , wdlua5_hardcopy}, + + /* General Attributes */ + {"Foreground" , cdlua5_foreground}, + {"Background" , cdlua5_background}, + {"SetForeground" , cdlua5_setforeground}, + {"SetBackground" , cdlua5_setbackground}, + {"WriteMode" , cdlua5_writemode}, + + /* Clipping */ + {"Clip" , cdlua5_clip}, + {"ClipArea" , cdlua5_cliparea}, + {"GetClipArea" , cdlua5_getcliparea}, + {"wClipArea" , wdlua5_cliparea}, + {"wGetClipArea" , wdlua5_getcliparea}, + {"fClipArea" , cdlua5_fcliparea}, + {"fGetClipArea" , cdlua5_fgetcliparea}, + + /* Regions */ + {"RegionCombineMode" , cdlua5_regioncombinemode}, + {"IsPointInRegion" , cdlua5_pointinregion}, + {"wIsPointInRegion" , wdlua5_pointinregion}, + {"OffsetRegion" , cdlua5_offsetregion}, + {"wOffsetRegion" , wdlua5_offsetregion}, + {"GetRegionBox" , cdlua5_regionbox}, + {"wGetRegionBox" , wdlua5_regionbox}, + + /* Marks */ + {"Pixel" , cdlua5_pixel}, + {"wPixel" , wdlua5_pixel}, + {"Mark" , cdlua5_mark}, + {"wMark" , wdlua5_mark}, + {"MarkType" , cdlua5_marktype}, + {"MarkSize" , cdlua5_marksize}, + {"wMarkSize" , wdlua5_marksize}, + + /* Line */ + {"Line" , cdlua5_line}, + {"wLine" , wdlua5_line}, + {"fLine" , cdlua5_fline}, + {"Rect" , cdlua5_rect}, + {"wRect" , wdlua5_rect}, + {"fRect" , cdlua5_frect}, + {"Arc" , cdlua5_arc}, + {"wArc" , wdlua5_arc}, + {"fArc" , cdlua5_farc}, + {"LineStyle" , cdlua5_linestyle}, + {"LineStyleDashes" , cdlua5_linestyledashes}, + {"LineWidth" , cdlua5_linewidth}, + {"wLineWidth" , wdlua5_linewidth}, + {"LineJoin" , cdlua5_linejoin}, + {"LineCap" , cdlua5_linecap}, + + /* Filled Areas */ + {"Box" , cdlua5_box}, + {"wBox" , wdlua5_box}, + {"fBox" , cdlua5_fbox}, + {"Sector" , cdlua5_sector}, + {"wSector" , wdlua5_sector}, + {"fSector" , cdlua5_fsector}, + {"Chord" , cdlua5_chord}, + {"wChord" , wdlua5_chord}, + {"fChord" , cdlua5_fchord}, + {"BackOpacity" , cdlua5_backopacity}, + {"FillMode" , cdlua5_fillmode}, + {"InteriorStyle" , cdlua5_interiorstyle}, + {"Hatch" , cdlua5_hatch}, + + /* Stipple */ + {"Stipple" , cdlua5_stipple}, + {"wStipple" , wdlua5_stipple}, + {"GetStipple" , cdlua5_getstipple}, + + /* Pattern */ + {"Pattern" , cdlua5_pattern}, + {"wPattern" , wdlua5_pattern}, + {"GetPattern" , cdlua5_getpattern}, + + /* Text */ + {"Text" , cdlua5_text}, + {"wText" , wdlua5_text}, + {"fText" , cdlua5_ftext}, + {"Font" , cdlua5_font}, + {"wFont" , wdlua5_font}, + {"GetFont" , cdlua5_getfont}, + {"wGetFont" , wdlua5_getfont}, + {"NativeFont" , cdlua5_nativefont}, + {"TextAlignment" , cdlua5_textalignment}, + {"TextOrientation" , cdlua5_textorientation}, + {"GetFontDim" , cdlua5_getfontdim}, + {"wGetFontDim" , wdlua5_getfontdim}, + {"GetTextSize" , cdlua5_gettextsize}, + {"wGetTextSize" , wdlua5_gettextsize}, + {"GetTextBox" , cdlua5_gettextbox}, + {"wGetTextBox" , wdlua5_gettextbox}, + {"GetTextBounds" , cdlua5_gettextbounds}, + {"wGetTextBounds" , wdlua5_gettextbounds}, + + /* Vector Text */ + {"VectorText" , cdlua5_vectortext}, + {"wVectorText" , wdlua5_vectortext}, + {"MultiLineVectorText" , cdlua5_multilinevectortext}, + {"wMultiLineVectorText" , wdlua5_multilinevectortext}, + {"VectorTextDirection" , cdlua5_vectortextdirection}, + {"wVectorTextDirection" , wdlua5_vectortextdirection}, + {"VectorTextTransform" , cdlua5_vectortexttransform}, + {"VectorFontSize" , cdlua5_vectorfontsize}, + {"GetVectorFontSize" , cdlua5_getvectorfontsize}, + {"VectorTextSize" , cdlua5_vectortextsize}, + {"wVectorTextSize" , wdlua5_vectortextsize}, + {"VectorCharSize" , cdlua5_vectorcharsize}, + {"wVectorCharSize" , wdlua5_vectorcharsize}, + {"VectorFont" , cdlua5_vectorfont}, + {"GetVectorTextSize" , cdlua5_getvectortextsize}, + {"wGetVectorTextSize" , wdlua5_getvectortextsize}, + {"GetVectorTextBounds" , cdlua5_getvectortextbounds}, + {"wGetVectorTextBounds" , wdlua5_getvectortextbounds}, + {"GetVectorTextBox" , cdlua5_getvectortextbox}, + {"wGetVectorTextBox" , wdlua5_getvectortextbox}, + + /* Client Images */ + {"GetImageRGB" , cdlua5_getimagergb}, + {"PutImageRectRGB" , cdlua5_putimagerectrgb}, + {"wPutImageRectRGB" , wdlua5_putimagerectrgb}, + {"PutImageRectRGBA" , cdlua5_putimagerectrgba}, + {"wPutImageRectRGBA", wdlua5_putimagerectrgba}, + {"PutImageRectMap" , cdlua5_putimagerectmap}, + {"wPutImageRectMap" , wdlua5_putimagerectmap}, + {"GetBitmap" , cdlua5_getbitmap}, + {"PutBitmap" , cdlua5_putbitmap}, + {"wPutBitmap" , wdlua5_putbitmap}, + + /* Server Images */ + {"CreateImage" , cdlua5_createimage}, + {"GetImage" , cdlua5_getimage}, + {"PutImageRect" , cdlua5_putimagerect}, + {"wPutImageRect" , wdlua5_putimagerect}, + {"ScrollArea" , cdlua5_scrollarea}, + + /* Other */ + {"Play" , cdlua5_play}, + + /* Color Coding */ + {"GetColorPlanes" , cdlua5_getcolorplanes}, + + /* Palette */ + {"Palette" , cdlua5_palette}, + + /* Polygon */ + {"Begin" , cdlua5_begin}, + {"Vertex" , cdlua5_vertex}, + {"wVertex" , wdlua5_vertex}, + {"fVertex" , cdlua5_fvertex}, + {"End" , cdlua5_end}, + + {"__eq", cdluaCanvas_eq}, + {"__tostring", cdluaCanvas_tostring}, + + {NULL, NULL}, +}; + +static const struct luaL_reg cdlib_canvas[] = { + /* Initialization */ + {"CreateCanvas" , cdlua5_createcanvas}, + {"KillCanvas" , cdlua5_killcanvas}, + {"GetContext" , cdlua5_getcontext}, /* compatibility name */ + {"ImageRGB" , cdlua5_imagergb}, + {"ImageRGBBitmap" , cdlua5_imagergbbitmap}, + {NULL, NULL}, +}; + +void cdlua_open_canvas (lua_State *L) +{ + /* "cd" table is at the top of the stack */ + + /* Object Oriented Access */ + luaL_newmetatable(L, "cdCanvas"); /* create new metatable for cdCanvas handles */ + lua_pushliteral(L, "__index"); + lua_pushvalue(L, -2); /* push metatable */ + lua_rawset(L, -3); /* metatable.__index = metatable */ + luaL_register(L, NULL, cdlib_canvas_meta); /* register methods */ + lua_pop(L, 1); /* removes the metatable from the top of the stack */ + + luaL_register(L, NULL, cdlib_canvas); +} diff --git a/cd/src/lua5/cdlua5ctx.c b/cd/src/lua5/cdlua5ctx.c new file mode 100755 index 0000000..e3bd19e --- /dev/null +++ b/cd/src/lua5/cdlua5ctx.c @@ -0,0 +1,802 @@ +/***************************************************************************\ +* $Id: cdlua5ctx.c,v 1.1 2008/10/17 06:10:42 scuri Exp $ +* * +\***************************************************************************/ + +#include +#include +#include +#include + +#include "cd.h" +#include "wd.h" + +#include "cdimage.h" +#include "cdirgb.h" +#include "cddxf.h" +#include "cddgn.h" +#include "cdcgm.h" +#include "cdwmf.h" +#include "cdemf.h" +#include "cdnative.h" +#include "cdprint.h" +#include "cdclipbd.h" +#include "cdmf.h" +#include "cdps.h" +#include "cddbuf.h" +#include "cdgdiplus.h" + +#include +#include + +#include "cdlua.h" +#include "cdlua5_private.h" + + +/***************************************************************************\ +* CD_CGM. * +\***************************************************************************/ +static void *cdcgm_checkdata(lua_State * L, int param) +{ + return (void *)luaL_checkstring(L,param); +} + +static int cgm_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h); +static int cgm_countercb(cdCanvas *canvas, double percent); +static int cgm_sclmdecb(cdCanvas *canvas, short scl_mde, short *draw_mode_i, double *factor_f); +static int cgm_vdcextcb(cdCanvas *canvas, short type, void *xmn, void *ymn, void *xmx, void *ymx); +static int cgm_begpictcb(cdCanvas *canvas, char *pict); +static int cgm_begpictbcb(cdCanvas *canvas); +static int cgm_begmtfcb(cdCanvas *canvas, int *xmn, int *ymn, int *xmx, int *ymx); + +static cdluaCallback cdluacgmcb[7] = { +{ + -1, + "SIZECB", + (cdCallback)cgm_sizecb +}, +{ + -1, + "CGMCOUNTERCB", + (cdCallback)cgm_countercb +}, +{ + -1, + "CGMSCLMDECB", + (cdCallback)cgm_sclmdecb +}, +{ + -1, + "CGMVDCEXTCB", + (cdCallback)cgm_vdcextcb +}, +{ + -1, + "CGMBEGPICTCB", + (cdCallback)cgm_begpictcb +}, +{ + -1, + "CGMBEGPICTBCB", + (cdCallback)cgm_begpictbcb +}, +{ + -1, + "CGMBEGMTFCB", + (cdCallback)cgm_begmtfcb +} +}; + +static cdluaContext cdluacgmctx = +{ + 0, + "CGM", + cdContextCGM, + cdcgm_checkdata, + cdluacgmcb, + 7 +}; + +/***************************************************************************\ +* CGM CD_COUNTERCB. * +\***************************************************************************/ +static int cgm_countercb(cdCanvas *canvas, double percent) +{ + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L, cdluacgmcb[CD_CGMCOUNTERCB].lock); + + cdlua_pushcanvas(L, canvas); + lua_pushnumber(L, percent); + if(lua_pcall(L, 2, 1, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L,-1)) + luaL_error(L, "invalid return value"); + + return luaL_checkint(L,-1); +} + +/***************************************************************************\ +* CGM CD_BEGPICTCB. * +\***************************************************************************/ +static int cgm_begpictcb(cdCanvas *canvas, char *pict) +{ + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L, cdluacgmcb[CD_CGMBEGPICTCB].lock); + + cdlua_pushcanvas(L, canvas); + lua_pushstring(L, pict); + if(lua_pcall(L, 2, 1, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L,-1)) + luaL_error(L,"invalid return value"); + + return luaL_checkint(L,-1); +} + +static int cgm_begmtfcb(cdCanvas *canvas, int *xmn, int *ymn, int *xmx, int *ymx) +{ + int result_i; + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L, cdluacgmcb[CD_CGMBEGMTFCB].lock); + + cdlua_pushcanvas(L, canvas); + if(lua_pcall(L, 1, 5, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L, -5)) + luaL_error(L, "invalid return value"); + + result_i = luaL_checkint(L, -5); + if (result_i == 1) + return 1; + + if (!lua_isnumber(L, -4)) + luaL_error(L, "invalid xmn return value"); + *xmn = luaL_checkint(L, -4); + + if (!lua_isnumber(L, -3)) + luaL_error(L, "invalid ymn return value"); + *ymn = luaL_checkint(L, -3); + + if (!lua_isnumber(L, -2)) + luaL_error(L, "invalid xmx return value"); + *xmx = luaL_checkint(L, -2); + + if (!lua_isnumber(L, -1)) + luaL_error(L, "invalid ymx return value"); + *ymx = luaL_checkint(L, -1); + + return result_i; +} + +/***************************************************************************\ +* CGM CD_BEGPICTBCB. * +\***************************************************************************/ +static int cgm_begpictbcb(cdCanvas *canvas) +{ + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L, cdluacgmcb[CD_CGMBEGPICTBCB].lock); + + cdlua_pushcanvas(L, canvas); + if(lua_pcall(L, 1, 1, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L, -1)) + luaL_error(L, "invalid return value"); + + return luaL_checkint(L,-1); +} + +/***************************************************************************\ +* CGM CD_SIZECB. * +\***************************************************************************/ +static int cgm_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h) +{ + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L,cdluacgmcb[CD_SIZECB].lock); + + cdlua_pushcanvas(L, canvas); + lua_pushnumber(L, w ); + lua_pushnumber(L, h ); + lua_pushnumber(L, mm_w ); + lua_pushnumber(L, mm_h ); + if(lua_pcall(L, 5, 1, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L, -1)) + luaL_error(L, "invalid return value"); + + return luaL_checkint(L,-1); +} + +/***************************************************************************\ +* CGM CD_SCLMDE. * +\***************************************************************************/ +static int cgm_sclmdecb(cdCanvas *canvas, short scl_mde, short *draw_mode_i, double *factor_f) +{ + int result_i; + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L,cdluacgmcb[CD_CGMSCLMDECB].lock); + + cdlua_pushcanvas(L, canvas); + lua_pushnumber(L, scl_mde); + if(lua_pcall(L, 2, 3, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L, -3)) + luaL_error(L, "invalid return value"); + + result_i = luaL_checkint(L, -3); + + if (result_i == 1) + return 1; + + if (!lua_isnumber(L, -2)) + luaL_error(L, "invalid draw mode return value"); + *draw_mode_i = (short) lua_tonumber(L,-2); + + if (!lua_isnumber(L, -1)) + luaL_error(L, "invalid factor return value"); + + *factor_f = (double) lua_tonumber(L, -1); + + return result_i; +} + +/***************************************************************************\ +* CGM CD_VDCEXTCB. * +\***************************************************************************/ +static int cgm_vdcextcb(cdCanvas *canvas, short type, void *xmn, void *ymn, void *xmx, void *ymx) +{ + int result_i; + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L, cdluacgmcb[CD_CGMVDCEXTCB].lock); + + cdlua_pushcanvas(L, canvas); + if(lua_pcall(L, 1, 5, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L, -5)) + luaL_error(L, "invalid return value"); + result_i = luaL_checkint(L,-5); + if (result_i == 1) + return 1; + + if (!lua_isnumber(L, -4)) + luaL_error(L, "invalid xmn return value"); + if (type == 1) *((float *) xmn) = (float) lua_tonumber(L, -4); + else *((int *) xmn) = luaL_checkint(L, -4); + + if (!lua_isnumber(L, -3)) + luaL_error(L, "invalid ymn return value"); + if (type == 1) *((float *) ymn) = (float) lua_tonumber(L, -3); + else *((int *) ymn) = luaL_checkint(L, -3); + + if (!lua_isnumber(L, -2)) + luaL_error(L,"invalid xmx return value"); + if (type == 1) *((float *) xmx) = (float) lua_tonumber(L, -2); + else *((int *) xmx) = luaL_checkint(L, -2); + + if (!lua_isnumber(L, -1)) + luaL_error(L,"invalid ymx return value"); + if (type == 1) *((float *) ymx) = (float) lua_tonumber(L, -1); + else *((int *) ymx) = (int) luaL_checkint(L, -1); + + return result_i; +} + +/***************************************************************************\ +* CD_DBUFFER. * +\***************************************************************************/ +static void *cddbuf_checkdata(lua_State * L, int param) +{ + return cdlua_checkcanvas(L, param); +} + +static cdluaContext cdluadbufctx = +{ + 0, + "DBUFFER", + cdContextDBuffer, + cddbuf_checkdata, + NULL, + 0 +}; + +/***************************************************************************\ +* CD_IMAGE. * +\***************************************************************************/ +static void *cdimage_checkdata(lua_State *L, int param) +{ + return cdlua_checkimage(L, param); +} + +static cdluaContext cdluaimagectx = +{ + 0, + "IMAGE", + cdContextImage, + cdimage_checkdata, + NULL, + 0 +}; + +static int cdlua_rawchecktype(lua_State *L, int param, const char* type) +{ + if (lua_isuserdata(L, param)) /* value is a userdata? */ + { + if (lua_getmetatable(L, param)) /* does it have a metatable? */ + { + lua_getfield(L, LUA_REGISTRYINDEX, type); /* get correct metatable */ + if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */ + { + lua_pop(L, 2); /* remove both metatables */ + return 1; + } + else + { + lua_pop(L, 2); /* remove both metatables */ + return -1; /* test for other metatables */ + } + } + } + return 0; /* do not continue */ +} + +/***************************************************************************\ +* CD_IMAGERGB. * +\***************************************************************************/ +static void *cdimagergb_checkdata(lua_State* L, int param) +{ + static char data_s[100] = ""; + + if (lua_isstring(L, param)) + { + const char* str = lua_tostring(L, param); + strcpy(data_s, str); + } + else + { + int ret = cdlua_rawchecktype(L, param, "cdBitmap"); + + if (ret == 0) + luaL_typerror(L, param, "cdBitmap"); /* not a user data and not a metatable */ + + if (ret == 1) + { + cdBitmap* *bitmap_p = (cdBitmap**)luaL_checkudata(L, param, "cdBitmap"); + if (!(*bitmap_p)) + luaL_argerror(L, param, "killed cdBitmap"); + + if ((*bitmap_p)->type != CD_RGB && (*bitmap_p)->type != CD_RGBA) + luaL_argerror(L, param, "bitmap should be of type rgb or rgba"); + + if (lua_isnoneornil(L, param+1)) + { + if ((*bitmap_p)->type == CD_RGBA) + sprintf(data_s, "%dx%d %p %p %p %p -a", (*bitmap_p)->w, (*bitmap_p)->h, + cdBitmapGetData(*bitmap_p, CD_IRED), + cdBitmapGetData(*bitmap_p, CD_IGREEN), + cdBitmapGetData(*bitmap_p, CD_IBLUE), + cdBitmapGetData(*bitmap_p, CD_IALPHA)); + else + sprintf(data_s, "%dx%d %p %p %p", (*bitmap_p)->w, (*bitmap_p)->h, + cdBitmapGetData(*bitmap_p, CD_IRED), + cdBitmapGetData(*bitmap_p, CD_IGREEN), + cdBitmapGetData(*bitmap_p, CD_IBLUE)); + } + else + { + double res_f = luaL_checknumber(L, param+1); + if ((*bitmap_p)->type == CD_RGBA) + sprintf(data_s, "%dx%d %p %p %p %p -r%g -a", (*bitmap_p)->w, (*bitmap_p)->h, + cdBitmapGetData(*bitmap_p, CD_IRED), + cdBitmapGetData(*bitmap_p, CD_IGREEN), + cdBitmapGetData(*bitmap_p, CD_IBLUE), + cdBitmapGetData(*bitmap_p, CD_IALPHA), + res_f); + else + sprintf(data_s, "%dx%d %p %p %p -r%g", (*bitmap_p)->w, (*bitmap_p)->h, + cdBitmapGetData(*bitmap_p, CD_IRED), + cdBitmapGetData(*bitmap_p, CD_IGREEN), + cdBitmapGetData(*bitmap_p, CD_IBLUE), + res_f); + } + + return data_s; + } + + ret = cdlua_rawchecktype(L, param, "cdImageRGB"); + if (ret == 1) + { + cdluaImageRGB *imagergb_p = (cdluaImageRGB*) luaL_checkudata(L, param, "cdImageRGB"); + if (!imagergb_p->red) + luaL_argerror(L, param, "killed cdImageRGB"); + + if (lua_isnoneornil(L, param+1)) + { + sprintf(data_s, "%dx%d %p %p %p", imagergb_p->width, imagergb_p->height, + imagergb_p->red, imagergb_p->green, imagergb_p->blue); + } + else + { + double res_f = luaL_checknumber(L, param+1); + sprintf(data_s, "%dx%d %p %p %p -r%g", imagergb_p->width, imagergb_p->height, + imagergb_p->red, imagergb_p->green, imagergb_p->blue, res_f); + } + + return data_s; + } + + ret = cdlua_rawchecktype(L, param, "cdImageRGBA"); + if (ret == 1) + { + cdluaImageRGBA *imagergba_p = (cdluaImageRGBA*) luaL_checkudata(L, param, "cdImageRGBA"); + if (!imagergba_p->red) + luaL_argerror(L, param, "killed cdImageRGBA"); + + if (lua_isnoneornil(L, param+1)) + { + sprintf(data_s, "%dx%d %p %p %p %p -a", imagergba_p->width, imagergba_p->height, + imagergba_p->red, imagergba_p->green, imagergba_p->blue, imagergba_p->alpha); + } + else + { + double res_f = luaL_checknumber(L, param+1); + sprintf(data_s, "%dx%d %p %p %p %p -r%g -a", imagergba_p->width, imagergba_p->height, + imagergba_p->red, imagergba_p->green, imagergba_p->blue, imagergba_p->alpha, res_f); + } + + return data_s; + } + + luaL_typerror(L, param, "cdBitmap"); /* is a metatable but it is not one of the accepted */ + } + + return data_s; +} + +static cdluaContext cdluaimagergbctx = +{ + 0, + "IMAGERGB", + cdContextImageRGB, + cdimagergb_checkdata, + NULL, + 0 +}; + +/***************************************************************************\ +* CD_DXF. * +\***************************************************************************/ +static void *cddxf_checkdata(lua_State * L, int param) +{ + return (void *)luaL_checkstring(L,param); +} + +static cdluaContext cdluadxfctx = +{ + 0, + "DXF", + cdContextDXF, + cddxf_checkdata +}; + +/***************************************************************************\ +* CD_DGN. * +\***************************************************************************/ +static void *cddgn_checkdata(lua_State * L, int param) +{ + return (void *)luaL_checkstring(L,param); +} + +static cdluaContext cdluadgnctx = +{ + 0, + "DGN", + cdContextDGN, + cddgn_checkdata, + NULL, + 0 +}; + +/***************************************************************************\ +* CD_WMF. * +\***************************************************************************/ +static void *cdwmf_checkdata(lua_State * L, int param) +{ + return (void *)luaL_checkstring(L,param); +} + +static int wmf_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h); + +static cdluaCallback cdluawmfcb[1] = +{{ + -1, + "SIZECB", + (cdCallback)wmf_sizecb +}}; + +static cdluaContext cdluawmfctx = +{ + 0, + "WMF", + cdContextWMF, + cdwmf_checkdata, + cdluawmfcb, + 1 +}; + +/***************************************************************************\ +* WMF CD_SIZECB. * +\***************************************************************************/ +static int wmf_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h) +{ + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L,cdluawmfcb[CD_SIZECB].lock); + + cdlua_pushcanvas(L, canvas); + lua_pushnumber(L, w); + lua_pushnumber(L, h); + lua_pushnumber(L, mm_w); + lua_pushnumber(L, mm_h); + if(lua_pcall(L, 5, 1, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L, -1)) + luaL_error(L,"invalid return value"); + + return luaL_checkint(L,-1); +} + +/***************************************************************************\ +* CD_EMF. * +\***************************************************************************/ +static void *cdemf_checkdata(lua_State *L, int param) +{ + return (void *)luaL_checkstring(L,param); +} + +static int emf_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h); + +static cdluaCallback cdluaemfcb[1] = +{{ + -1, + "SIZECB", + (cdCallback)emf_sizecb +}}; + +static cdluaContext cdluaemfctx = +{ + 0, + "EMF", + cdContextEMF, + cdemf_checkdata, + cdluaemfcb, + 1 +}; + +/***************************************************************************\ +* EMF CD_SIZECB. * +\***************************************************************************/ +static int emf_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h) +{ + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L,cdluaemfcb[CD_SIZECB].lock); + + cdlua_pushcanvas(L, canvas); + lua_pushnumber(L, w); + lua_pushnumber(L, h); + lua_pushnumber(L, mm_w); + lua_pushnumber(L, mm_h); + if(lua_pcall(L, 5, 1, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L,-1)) + luaL_error(L, "invalid return value"); + + return luaL_checkint(L,-1); +} + +/***************************************************************************\ +* CD_METAFILE. * +\***************************************************************************/ +static void *cdmetafile_checkdata(lua_State *L,int param) +{ + return (void *)luaL_checkstring(L,param); +} + +static int metafile_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h); + +static cdluaCallback cdluamfcb[1] = +{{ + -1, + "SIZECB", + (cdCallback)metafile_sizecb +}}; + +static cdluaContext cdluamfctx = +{ + 0, + "METAFILE", + cdContextMetafile, + cdmetafile_checkdata, + cdluamfcb, + 1 +}; + +/***************************************************************************\ +* METAFILE CD_SIZECB. * +\***************************************************************************/ +static int metafile_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h) +{ + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + + lua_getref(L, cdluamfcb[CD_SIZECB].lock); + + cdlua_pushcanvas(L, canvas); + lua_pushnumber(L, w); + lua_pushnumber(L, h); + lua_pushnumber(L, mm_w); + lua_pushnumber(L, mm_h); + if(lua_pcall(L, 5, 1, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L, -1)) + luaL_error(L, "invalid return value"); + + return luaL_checkint(L,-1); +} + +/***************************************************************************\ +* CD_PS. * +\***************************************************************************/ +static void *cdps_checkdata( lua_State *L, int param) +{ + return (void *)luaL_checkstring(L, param); +} + +static cdluaContext cdluapsctx = +{ + 0, + "PS", + cdContextPS, + cdps_checkdata, + NULL, + 0 +}; + +/***************************************************************************\ +* CD_PRINTER. * +\***************************************************************************/ +static void *cdprinter_checkdata(lua_State *L, int param) +{ + return (void *)luaL_checkstring(L,param); +} + +static cdluaContext cdluaprinterctx = +{ + 0, + "PRINTER", + cdContextPrinter, + cdprinter_checkdata, + NULL, + 0 +}; + +/***************************************************************************\ +* CD_CLIPBOARD. * +\***************************************************************************/ +static void *cdclipboard_checkdata(lua_State *L, int param) +{ + return (void *)luaL_checkstring(L,param); +} + +static int clipboard_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h); + +static cdluaCallback cdluaclipboardcb[1] = +{{ + -1, + "SIZECB", + (cdCallback)clipboard_sizecb +}}; + +static cdluaContext cdluaclipboardctx = +{ + 0, + "CLIPBOARD", + cdContextClipboard, + cdclipboard_checkdata, + cdluaclipboardcb, + 1 +}; + +/***************************************************************************\ +* CLIPBOARD CD_SIZECB. * +\***************************************************************************/ +static int clipboard_sizecb(cdCanvas *canvas, int w, int h, double mm_w, double mm_h) +{ + /* little Wrapper */ + lua_State * L = cdlua_getplaystate(); + lua_getref(L, cdluaclipboardcb[CD_SIZECB].lock); + + cdlua_pushcanvas(L, canvas); + lua_pushnumber(L, w); + lua_pushnumber(L, h); + lua_pushnumber(L, mm_w); + lua_pushnumber(L, mm_h); + if(lua_pcall(L, 5, 1, 0) != 0) + luaL_error(L, "error running function: %s", lua_tostring(L, -1)); + + if (!lua_isnumber(L,-1)) + luaL_error(L, "invalid return value"); + + return luaL_checkint(L,-1); +} + +/***************************************************************************\ +* CD_NATIVEWINDOW. * +\***************************************************************************/ +static void *cdnativewindow_checkdata(lua_State *L, int param) +{ +#ifdef WIN32 + if (!lua_isnil(L,param) && !lua_isuserdata(L,param)) + luaL_argerror(L, param, "data should be of type userdata"); + + return lua_touserdata(L,param); +#else + return (void *)luaL_checkstring(L,param); +#endif +} + +static cdluaContext cdluanativewindowctx = +{ + 0, + "NATIVEWINDOW", + cdContextNativeWindow, + cdnativewindow_checkdata, + NULL, + 0 +}; + + +/*******************************************************************************\ +* Init all CD Drivers * +*********************************************************************************/ +void cdlua_initdrivers(lua_State * L, cdluaLuaState* cdL) +{ + cdlua_addcontext(L, cdL, &cdluaimagectx); + cdlua_addcontext(L, cdL, &cdluaimagergbctx); + cdlua_addcontext(L, cdL, &cdluadxfctx); + cdlua_addcontext(L, cdL, &cdluadgnctx); + cdlua_addcontext(L, cdL, &cdluacgmctx); + cdlua_addcontext(L, cdL, &cdluamfctx); + cdlua_addcontext(L, cdL, &cdluapsctx); + cdlua_addcontext(L, cdL, &cdluaclipboardctx); + cdlua_addcontext(L, cdL, &cdluanativewindowctx); + cdlua_addcontext(L, cdL, &cdluaprinterctx); + cdlua_addcontext(L, cdL, &cdluawmfctx); + cdlua_addcontext(L, cdL, &cdluaemfctx); + cdlua_addcontext(L, cdL, &cdluadbufctx); +} diff --git a/cd/src/lua5/cdluacontextplus5.c b/cd/src/lua5/cdluacontextplus5.c new file mode 100755 index 0000000..de69167 --- /dev/null +++ b/cd/src/lua5/cdluacontextplus5.c @@ -0,0 +1,44 @@ +/** \file + * \brief Context Plus Lua 5 Binding + * + * See Copyright Notice in cd.h + */ + +#include +#include + +#include "cd.h" + +#include +#include + + +static int cdlua5_initcontextplus(lua_State *L) +{ + (void)L; + cdInitContextPlus(); + return 0; +} + +static const struct luaL_reg cdlib[] = { + {"InitContextPlus", cdlua5_initcontextplus}, + {NULL, NULL}, +}; + + +static int cdluacontextplus_open (lua_State *L) +{ + cdInitContextPlus(); + luaL_register(L, "cd", cdlib); /* leave "cd" table at the top of the stack */ + return 1; +} + +int luaopen_cdluacontextplus(lua_State* L) +{ + return cdluacontextplus_open(L); +} + +int luaopen_cdluacontextplus51(lua_State* L) +{ + return cdluacontextplus_open(L); +} diff --git a/cd/src/lua5/cdluacontextplus5.def b/cd/src/lua5/cdluacontextplus5.def new file mode 100755 index 0000000..55e478b --- /dev/null +++ b/cd/src/lua5/cdluacontextplus5.def @@ -0,0 +1,4 @@ +EXPORTS + luaopen_cdluacontextplus + luaopen_cdluacontextplus51 + \ No newline at end of file diff --git a/cd/src/lua5/cdluaim5.c b/cd/src/lua5/cdluaim5.c new file mode 100755 index 0000000..77ffc4f --- /dev/null +++ b/cd/src/lua5/cdluaim5.c @@ -0,0 +1,265 @@ +/** \file + * \brief CD+IM Lua 5 Binding + * + * See Copyright Notice in im_lib.h + */ + +#include +#include + +#define CD_NO_OLD_INTERFACE + +#include +#include + +#include "cd.h" +#include "cdirgb.h" +#include "wd.h" + +#include +#include + +#include + +#include "cdlua.h" +#include "cdlua5_private.h" + + +/*****************************************************************************\ + image:cdInitBitmap() -> cdBitmap +\*****************************************************************************/ +static int imlua_cdInitBitmap(lua_State *L) +{ + cdBitmap* bitmap; + imImage *image = imlua_checkimage(L, 1); + + if (!imImageIsBitmap(image)) + 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]); + else + bitmap = cdInitBitmap(image->width, image->height, CD_MAP, image->data[0], image->palette); + + if (!bitmap) + luaL_error(L, "insuficient memory to create bitmap"); + + cdlua_pushbitmap(L, bitmap); + return 1; +} + +/*****************************************************************************\ + image:cdCreateBitmap() -> cdBitmap +\*****************************************************************************/ +static int imlua_cdCreateBitmap(lua_State *L) +{ + cdBitmap* bitmap; + imImage *image = imlua_checkimage(L, 1); + + if (!imImageIsBitmap(image)) + luaL_argerror(L, 1, "image is not a bitmap"); + + if (image->color_space == IM_RGB) + bitmap = cdCreateBitmap(image->width, image->height, CD_RGB); + else + bitmap = cdCreateBitmap(image->width, image->height, CD_MAP); + + if (!bitmap) + luaL_error(L, "insuficient memory to create bitmap"); + + if (image->color_space == IM_RGB) + { + 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); + } + else + { + memcpy(cdBitmapGetData(bitmap, CD_INDEX), image->data[0], image->plane_size); + memcpy(cdBitmapGetData(bitmap, CD_COLORS), image->palette, image->palette_count*sizeof(long int)); + } + + cdlua_pushbitmap(L, bitmap); + return 1; +} + +/*****************************************************************************\ + cd:imImageCreate(bitmap: cdBitmap) -> imImage +\*****************************************************************************/ +static int cdlua_imImageCreate(lua_State *L) +{ + imImage *image; + cdBitmap* bitmap = cdlua_checkbitmap(L, 1); + + if (bitmap->type == CD_RGB) + image = imImageCreate(bitmap->w, bitmap->h, IM_RGB, IM_BYTE); + else + image = imImageCreate(bitmap->w, bitmap->h, IM_MAP, IM_BYTE); + + if (!image) + luaL_error(L, "insuficient memory to create image"); + + if (bitmap->type == CD_RGB) + { + 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); + } + else + { + memcpy(image->data[0], cdBitmapGetData(bitmap, CD_INDEX), image->plane_size); + memcpy(image->palette, cdBitmapGetData(bitmap, CD_COLORS), 256*sizeof(long int)); + } + + imlua_pushimage(L, image); + return 1; +} + +/*****************************************************************************\ + image:wdCanvasPutImageRect(_canvas, _x, _y, _w, _h, _xmin, _xmax, _ymin, _ymax) +\*****************************************************************************/ +static int imlua_wdCanvasPutImageRect(lua_State *L) +{ + int xr, yr, wr, hr; + imImage *image = imlua_checkimage(L, 1); + cdCanvas* canvas = cdlua_checkcanvas(L, 2); + double x = luaL_checknumber(L, 3); + double y = luaL_checknumber(L, 4); + double w = luaL_checknumber(L, 5); + double h = luaL_checknumber(L, 6); + int xmin = luaL_optint(L, 7, 0); + int xmax = luaL_optint(L, 8, 0); + int ymin = luaL_optint(L, 9, 0); + int ymax = luaL_optint(L, 10, 0); + + if (!imImageIsBitmap(image)) + luaL_argerror(L, 1, "image is not a bitmap"); + + wdCanvasWorld2Canvas(canvas, x, y, &xr, &yr); + wdCanvasWorld2CanvasSize(canvas, w, h, &wr, &hr); + + imcdCanvasPutImage(canvas, image, xr, yr, wr, hr, xmin, xmax, ymin, ymax); + return 0; +} + +/*****************************************************************************\ + image:cdCanvasPutImageRect(_canvas, _x, _y, _w, _h, _xmin, _xmax, _ymin, _ymax) +\*****************************************************************************/ +static int imlua_cdCanvasPutImageRect(lua_State *L) +{ + imImage *image = imlua_checkimage(L, 1); + cdCanvas* canvas = cdlua_checkcanvas(L, 2); + int x = luaL_checkint(L, 3); + int y = luaL_checkint(L, 4); + int w = luaL_checkint(L, 5); + int h = luaL_checkint(L, 6); + int xmin = luaL_optint(L, 7, 0); + int xmax = luaL_optint(L, 8, 0); + int ymin = luaL_optint(L, 9, 0); + int ymax = luaL_optint(L, 10, 0); + + if (!imImageIsBitmap(image)) + luaL_argerror(L, 1, "image is not a bitmap"); + + imcdCanvasPutImage(canvas, image, x, y, w, h, xmin, xmax, ymin, ymax); + return 0; +} + +/***************************************************************************\ +* image:cdCanvasGetImage(_canvas, x, y: number) * +\***************************************************************************/ +static int imlua_cdCanvasGetImage(lua_State *L) +{ + imImage *image = imlua_checkimage(L, 1); + cdCanvas* canvas = cdlua_checkcanvas(L, 2); + int x = luaL_optint(L, 3, 0); + int y = luaL_optint(L, 4, 0); + + if (image->color_space != IM_RGB || image->data_type != IM_BYTE) + luaL_argerror(L, 1, "image is not RGB/byte"); + + cdCanvasGetImageRGB(canvas, image->data[0], image->data[1], image->data[2], x, y, image->width, image->height); + return 0; +} + +/***************************************************************************\ +* image:cdCreateCanvas(res: number) -> cdCanvas * +\***************************************************************************/ +static int imlua_cdCreateCanvas(lua_State * L) +{ + cdCanvas**canvas_p, *canvas; + char data_s[100]; + + imImage *image = imlua_checkimage(L, 1); + + 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]); + } + 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); + } + + canvas = cdCreateCanvas(CD_IMAGERGB, data_s); + if (!canvas) + { + lua_pushnil(L); + return 1; + } + + canvas_p = (cdCanvas**) lua_newuserdata(L, sizeof(cdCanvas*)); + luaL_getmetatable(L, "cdCanvas"); + lua_setmetatable(L, -2); + *canvas_p = canvas; + + return 1; +} + +static const luaL_reg cdim_metalib[] = { + {"imImageCreate", cdlua_imImageCreate}, + {NULL, NULL} +}; + +static const luaL_reg imcd_metalib[] = { + {"cdCreateBitmap", imlua_cdCreateBitmap}, + {"cdInitBitmap", imlua_cdInitBitmap}, + {"cdCreateCanvas", imlua_cdCreateCanvas}, + {"wdCanvasPutImageRect", imlua_wdCanvasPutImageRect}, + {"cdCanvasPutImageRect", imlua_cdCanvasPutImageRect}, + {"cdCanvasGetImage", imlua_cdCanvasGetImage}, + + {NULL, NULL} +}; + +static void createmeta (lua_State *L) +{ + /* add methods to already created metatables */ + + luaL_getmetatable(L, "imImage"); + luaL_register(L, NULL, imcd_metalib); /* register methods */ + lua_pop(L, 1); /* removes the metatable from the top of the stack */ + + luaL_getmetatable(L, "cdBitmap"); + luaL_register(L, NULL, cdim_metalib); /* register methods */ + lua_pop(L, 1); /* removes the metatable from the top of the stack */ +} + +int cdluaim_open(lua_State *L) +{ + createmeta(L); + return 0; +} + +int luaopen_cdluaim(lua_State *L) +{ + return cdluaim_open(L); +} + +int luaopen_cdluaim51(lua_State *L) +{ + return cdluaim_open(L); +} diff --git a/cd/src/lua5/cdluaim5.def b/cd/src/lua5/cdluaim5.def new file mode 100755 index 0000000..0b26928 --- /dev/null +++ b/cd/src/lua5/cdluaim5.def @@ -0,0 +1,4 @@ +EXPORTS + cdluaim_open + luaopen_cdluaim + luaopen_cdluaim51 diff --git a/cd/src/lua5/cdluapdf5.c b/cd/src/lua5/cdluapdf5.c new file mode 100755 index 0000000..eb3f221 --- /dev/null +++ b/cd/src/lua5/cdluapdf5.c @@ -0,0 +1,53 @@ +/** \file + * \brief PDF Canvas Lua 5 Binding + * + * See Copyright Notice in cd.h + */ + +#include +#include + +#include "cd.h" +#include "cdpdf.h" + +#include +#include + +#include "cdlua.h" +#include "cdluapdf.h" +#include "cdlua5_private.h" + + +static void *cdpdf_checkdata(lua_State *L, int param) +{ + return (void *)luaL_checkstring(L, param); +} + +static cdluaContext cdluapdfctx = +{ + 0, + "PDF", + cdContextPDF, + cdpdf_checkdata, + NULL, + 0 +}; + +int cdluapdf_open (lua_State *L) +{ + cdluaLuaState* cdL = cdlua_getstate(L); + lua_pushliteral(L, "cd"); + lua_gettable(L, LUA_GLOBALSINDEX); /* leave "cd" table at the top of the stack */ + cdlua_addcontext(L, cdL, &cdluapdfctx); + return 1; +} + +int luaopen_cdluapdf(lua_State* L) +{ + return cdluapdf_open(L); +} + +int luaopen_cdluapdf51(lua_State* L) +{ + return cdluapdf_open(L); +} diff --git a/cd/src/lua5/cdluapdf5.def b/cd/src/lua5/cdluapdf5.def new file mode 100755 index 0000000..bfbc889 --- /dev/null +++ b/cd/src/lua5/cdluapdf5.def @@ -0,0 +1,4 @@ +EXPORTS + cdluapdf_open + luaopen_cdluapdf + luaopen_cdluapdf51 \ No newline at end of file diff --git a/cd/src/lua5/cdvoid5.c b/cd/src/lua5/cdvoid5.c new file mode 100755 index 0000000..2424e1d --- /dev/null +++ b/cd/src/lua5/cdvoid5.c @@ -0,0 +1,130 @@ +/** \file + * \brief CD Void driver for error checking while there is no active canvas + * + * See Copyright Notice in cd.h + */ + +#include +#include + +#include "cd.h" +#include "cd_private.h" + +#include +#include + +#include "cdlua5_private.h" + + +struct _cdCtxCanvas +{ + cdCanvas* canvas; + lua_State * L; +}; + +void cdlua_setvoidstate(cdCanvas* canvas, lua_State * L) +{ + canvas->ctxcanvas->L = L; +} + +static void cdcreatecanvas(cdCanvas *canvas, void *data) +{ + cdCtxCanvas *ctxcanvas = (cdCtxCanvas*) malloc(sizeof(cdCtxCanvas)); + ctxcanvas->canvas = canvas; + canvas->ctxcanvas = ctxcanvas; + (void)data; +} + +static void cdkillcanvas(cdCtxCanvas* ctxcanvas) +{ + free(ctxcanvas); +} + +/***************************************************************************\ +* Echos an error if called. * +\***************************************************************************/ +static void cdvoid_error(cdCtxCanvas* ctxcanvas) +{ + luaL_error(ctxcanvas->L, "cdlua: there is no active canvas"); +} + +/***************************************************************************\ +* Dummy. * +\***************************************************************************/ +static int cdvoid_dummy(void) +{ + return CD_OK; +} + +/***************************************************************************\ +* Driver function table. * +\***************************************************************************/ + +void cdinittable(cdCanvas* canvas) +{ + /* attribute functions can not be set, because of default attributes */ + + canvas->cxClip = (int (*)(cdCtxCanvas*, int))cdvoid_error; + canvas->cxClipArea = (void (*)(cdCtxCanvas*, int, int, int, int))cdvoid_error; + canvas->cxNewRegion = (void (*)(cdCtxCanvas*))cdvoid_error; + canvas->cxIsPointInRegion = (int (*)(cdCtxCanvas*, int, int))cdvoid_error; + canvas->cxOffsetRegion = (void (*)(cdCtxCanvas*, int, int))cdvoid_error; + canvas->cxGetRegionBox = (void (*)(cdCtxCanvas*, int *, int *, int *, int *))cdvoid_error; + canvas->cxFlush = (void ( *)(cdCtxCanvas*))cdvoid_error; + canvas->cxClear = (void ( *)(cdCtxCanvas*))cdvoid_error; + canvas->cxPixel = (void ( *)(cdCtxCanvas*, int ,int ,long ))cdvoid_error; + canvas->cxLine = (void ( *)(cdCtxCanvas*, int ,int ,int ,int ))cdvoid_error; + canvas->cxPoly = (void ( *)(cdCtxCanvas*, int ,struct _cdPoint *,int ))cdvoid_error; + canvas->cxRect = (void ( *)(cdCtxCanvas*, int ,int ,int ,int ))cdvoid_error; + canvas->cxBox = (void ( *)(cdCtxCanvas*, int ,int ,int ,int ))cdvoid_error; + canvas->cxArc = (void ( *)(cdCtxCanvas*, int ,int ,int ,int ,double ,double ))cdvoid_error; + canvas->cxSector = (void ( *)(cdCtxCanvas*, int ,int ,int ,int ,double ,double ))cdvoid_error; + canvas->cxChord = (void ( *)(cdCtxCanvas*, int ,int ,int ,int ,double ,double ))cdvoid_error; + canvas->cxText = (void (*)(cdCtxCanvas*, int ,int ,const char *))cdvoid_error; + canvas->cxGetFontDim = (void (*)(cdCtxCanvas*, int *,int *,int *,int *))cdvoid_error; + canvas->cxGetTextSize = (void (*)(cdCtxCanvas*, const char *,int *,int *))cdvoid_error; + canvas->cxPutImageRectRGB = (void (*)(cdCtxCanvas*, int ,int ,const unsigned char *,const unsigned char *,const unsigned char *,int ,int ,int ,int ,int ,int ,int ,int ))cdvoid_error; + canvas->cxPutImageRectRGBA = (void (*)(cdCtxCanvas*, int ,int ,const unsigned char *,const unsigned char *,const unsigned char *,const unsigned char *,int ,int ,int ,int ,int ,int ,int ,int ))cdvoid_error; + canvas->cxPutImageRectMap = (void (*)(cdCtxCanvas*, int ,int ,const unsigned char *,const long *,int ,int ,int ,int ,int ,int ,int ,int ))cdvoid_error; + canvas->cxScrollArea = (void (*)(cdCtxCanvas*, int ,int ,int ,int ,int ,int ))cdvoid_error; + canvas->cxFLine = (void (*)(cdCtxCanvas*, double ,double ,double ,double ))cdvoid_error; + canvas->cxFPoly = (void (*)(cdCtxCanvas*, int , cdfPoint*,int ))cdvoid_error; + canvas->cxFRect = (void (*)(cdCtxCanvas*, double ,double ,double ,double ))cdvoid_error; + canvas->cxFBox = (void (*)(cdCtxCanvas*, double ,double ,double ,double ))cdvoid_error; + canvas->cxFArc = (void (*)(cdCtxCanvas*, double ,double ,double ,double ,double ,double ))cdvoid_error; + canvas->cxFSector = (void (*)(cdCtxCanvas*, double ,double ,double ,double ,double ,double ))cdvoid_error; + canvas->cxFText = (void (*)(cdCtxCanvas*, double ,double ,const char *))cdvoid_error; + canvas->cxStipple = (void (*)(cdCtxCanvas*, int ,int ,const unsigned char *))cdvoid_error; + canvas->cxPattern = (void (*)(cdCtxCanvas*, int ,int , const long *))cdvoid_error; + canvas->cxNativeFont = (int (*)(cdCtxCanvas*, const char*))cdvoid_error; + canvas->cxPalette = (void (*)(cdCtxCanvas*, int ,const long *,int ))cdvoid_error; + canvas->cxGetImageRGB = (void (*)(cdCtxCanvas*, unsigned char *,unsigned char *,unsigned char *,int ,int ,int ,int ))cdvoid_error; + canvas->cxCreateImage = (cdCtxImage* (*)(cdCtxCanvas*, int ,int ))cdvoid_error; + canvas->cxGetImage = (void (*)(cdCtxCanvas*, cdCtxImage*, int ,int ))cdvoid_error; + canvas->cxPutImageRect = (void (*)(cdCtxCanvas*, cdCtxImage*,int ,int ,int ,int ,int ,int ))cdvoid_error; + canvas->cxKillImage = (void (*)(cdCtxImage*))cdvoid_error; + canvas->cxFClipArea = (void (*)(cdCtxCanvas*, double,double,double,double))cdvoid_error; + + /* must not be the error callback */ + canvas->cxActivate = (int (*)(cdCtxCanvas*))cdvoid_dummy; + canvas->cxDeactivate = (void (*)(cdCtxCanvas*))cdvoid_dummy; + canvas->cxFont = (int (*)(cdCtxCanvas*, const char *, int, int))cdvoid_dummy; + + canvas->cxKillCanvas = cdkillcanvas; +} + +static cdContext cdVoidContext = +{ + 0, + 0, + cdcreatecanvas, + cdinittable, + NULL, + NULL +}; + +cdContext* cdContextVoid(void) +{ + return &cdVoidContext; +} + diff --git a/cd/src/lua5/cdvoid5.h b/cd/src/lua5/cdvoid5.h new file mode 100755 index 0000000..75bf6e7 --- /dev/null +++ b/cd/src/lua5/cdvoid5.h @@ -0,0 +1,18 @@ +#ifndef _CD_VOID_ +#define _CD_VOID_ + +#ifdef __cplusplus +extern "C" { +#endif + +cdContext* cdContextVoid(void); +void cdlua_setvoidstate(cdCanvas* cnv, lua_State * L); + +#define CD_VOID cdContextVoid() + +#ifdef __cplusplus +} +#endif + +#endif /* ifndef _CD_VOID_ */ + -- cgit v1.2.3