static int setboxdim (lua_State *L, int whichdim) { int i,j,k,err; i = lua_gettop(L); if (!lua_isnumber(L,i)) { j = dimen_to_number(L,(char *)lua_tostring(L,i)); } else { j = (int)lua_tonumber(L,i); } k = get_box_id(L,(i-1)); lua_settop(L,(i-3)); /* table at -2 */ if (k<0 || k > 65535) { lua_pushstring(L, "incorrect index"); lua_error(L); } err = 0; switch (whichdim) { case width_offset: err = set_tex_box_width(k,j); break; case height_offset: err = set_tex_box_height(k,j); break; case depth_offset: err = set_tex_box_depth(k,j); } if (err) { lua_pushstring(L, "not a box"); lua_error(L); } return 0; }
int setdimen (lua_State *L) { int i,j; size_t k; int cur_cs; int texstr; char *s; i = lua_gettop(L); j = 0; /* find the value*/ if (!lua_isnumber(L,i)) if (lua_isstring(L,i)) { j = dimen_to_number(L,(char *)lua_tostring(L,i)); } else { lua_pushstring(L, "unsupported value type"); lua_error(L); } else j = (int)lua_tonumber(L,i); /* find the index*/ if (lua_type(L,i-1)==LUA_TSTRING) { s = (char *)lua_tolstring(L,i-1, &k); texstr = maketexlstring(s,k); cur_cs = string_lookup(texstr); flush_str(texstr); k = zget_equiv(cur_cs)-get_scaled_base(); } else { k = (int)luaL_checkinteger(L,i-1); } check_index_range(k); if(set_tex_dimen_register(k,j)) { lua_pushstring(L, "incorrect value"); lua_error(L); } return 0; }
int settex (lua_State *L) { char *st; int i,j,texstr; size_t k; int cur_cs, cur_cmd; j = 0; i = lua_gettop(L); if (lua_isstring(L,(i-1))) { st = (char *)lua_tolstring(L,(i-1), &k); texstr = maketexlstring(st,k); if (zis_primitive(texstr)) { cur_cs = string_lookup(texstr); flush_str(texstr); cur_cmd = zget_eq_type(cur_cs); if (is_int_assign(cur_cmd)) { if (lua_isnumber(L,i)) { assign_internal_int(zget_equiv(cur_cs),lua_tonumber(L,i)); } else { lua_pushstring(L, "unsupported value type"); lua_error(L); } } else if (is_dim_assign(cur_cmd)) { if (!lua_isnumber(L,i)) if (lua_isstring(L,i)) { j = dimen_to_number(L,(char *)lua_tostring(L,i)); } else { lua_pushstring(L, "unsupported value type"); lua_error(L); } else j = (int)lua_tonumber(L,i); assign_internal_dim(zget_equiv(cur_cs),j); } else { lua_pushstring(L, "unsupported tex internal assignment"); lua_error(L); } } else { lua_rawset(L,(i-2)); } } else { lua_rawset(L,(i-2)); } return 0; }
static void lua_to_image(lua_State * L, image * a) { /* value key table ... */ int i; image_dict *d = img_dict(a); assert(d != NULL); lua_pushstring(L, IMG_ENV); /* s v k t ... */ lua_gettable(L, LUA_REGISTRYINDEX); /* t v k t ... */ lua_pushvalue(L, -3); /* k t v k t ... */ lua_gettable(L, -2); /* i? t v k t ... */ if (!lua_isnumber(L, -1)) /* !i t v k t ... */ luaL_error(L, "lua_to_image(): %s is not a valid image key", lua_tostring(L, -4)); i = (int) lua_tointeger(L, -1); /* i t v k t ... */ lua_pop(L, 2); /* v k t ... */ switch (i) { case P_WIDTH: if (lua_isnil(L, -1)) set_wd_running(a); else if (lua_type(L, -1) == LUA_TNUMBER) img_width(a) = (int) lua_tointeger(L, -1); else if (lua_type(L, -1) == LUA_TSTRING) img_width(a) = dimen_to_number(L, lua_tostring(L, -1)); else luaL_error(L, "image.width needs integer or nil value or dimension string"); break; case P_HEIGHT: if (lua_isnil(L, -1)) set_ht_running(a); else if (lua_type(L, -1) == LUA_TNUMBER) img_height(a) = (int) lua_tointeger(L, -1); else if (lua_type(L, -1) == LUA_TSTRING) img_height(a) = dimen_to_number(L, lua_tostring(L, -1)); else luaL_error(L, "image.height needs integer or nil value or dimension string"); break; case P_DEPTH: if (lua_isnil(L, -1)) set_dp_running(a); else if (lua_type(L, -1) == LUA_TNUMBER) img_depth(a) = (int) lua_tointeger(L, -1); else if (lua_type(L, -1) == LUA_TSTRING) img_depth(a) = dimen_to_number(L, lua_tostring(L, -1)); else luaL_error(L, "image.depth needs integer or nil value or dimension string"); break; case P_TRANSFORM: if (lua_isnumber(L, -1)) img_transform(a) = (int) lua_tointeger(L, -1); else luaL_error(L, "image.transform needs integer value"); break; /* now follow all image_dict entries */ case P_FILENAME: if (img_state(d) >= DICT_FILESCANNED) luaL_error(L, "image.filename is now read-only"); if (img_type(d) == IMG_TYPE_PDFSTREAM) luaL_error(L, "image.filename can't be used with image.stream"); if (lua_isstring(L, -1)) { xfree(img_filename(d)); img_filename(d) = xstrdup(lua_tostring(L, -1)); } else luaL_error(L, "image.filename needs string value"); break; case P_VISIBLEFILENAME: if (img_state(d) >= DICT_FILESCANNED) luaL_error(L, "image.visiblefilename is now read-only"); if (img_type(d) == IMG_TYPE_PDFSTREAM) luaL_error(L, "image.visiblefilename can't be used with image.stream"); if (lua_isstring(L, -1)) { xfree(img_visiblefilename(d)); img_visiblefilename(d) = xstrdup(lua_tostring(L, -1)); } else luaL_error(L, "image.visiblefilename needs string value"); break; case P_ATTR: if (img_state(d) >= DICT_FILESCANNED) luaL_error(L, "image.attr is now read-only"); if (lua_isstring(L, -1) || lua_isnil(L, -1)) { xfree(img_attr(d)); if (lua_isstring(L, -1)) img_attr(d) = xstrdup(lua_tostring(L, -1)); } else luaL_error(L, "image.attr needs string or nil value"); break; case P_PAGE: if (img_state(d) >= DICT_FILESCANNED) luaL_error(L, "image.page is now read-only"); if (lua_type(L, -1) == LUA_TSTRING) { xfree(img_pagename(d)); img_pagename(d) = xstrdup(lua_tostring(L, -1)); img_pagenum(d) = 0; } else if (lua_type(L, -1) == LUA_TNUMBER) { img_pagenum(d) = (int) lua_tointeger(L, -1); xfree(img_pagename(d)); } else luaL_error(L, "image.page needs integer or string value"); break; case P_COLORSPACE: if (img_state(d) >= DICT_FILESCANNED) luaL_error(L, "image.colorspace is now read-only"); if (lua_isnil(L, -1)) img_colorspace(d) = 0; else if (lua_isnumber(L, -1)) img_colorspace(d) = (int) lua_tointeger(L, -1); else luaL_error(L, "image.colorspace needs integer or nil value"); break; case P_PAGEBOX: if (img_state(d) >= DICT_FILESCANNED) luaL_error(L, "image.pagebox is now read-only"); if (lua_isnil(L, -1)) img_pagebox(d) = PDF_BOX_SPEC_NONE; else if (lua_isstring(L, -1)) img_pagebox(d) = luaL_checkoption(L, -1, "none", pdfboxspec_s); else luaL_error(L, "image.pagebox needs string or nil value"); break; case P_BBOX: if (img_state(d) >= DICT_FILESCANNED) luaL_error(L, "image.bbox is now read-only"); if (!lua_istable(L, -1)) luaL_error(L, "image.bbox needs table value"); if (lua_objlen(L, -1) != 4) luaL_error(L, "image.bbox table must have exactly 4 elements"); for (i = 1; i <= 4; i++) { /* v k t ... */ lua_pushinteger(L, i); /* idx v k t ... */ lua_gettable(L, -2); /* int v k t ... */ if (lua_type(L, -1) == LUA_TNUMBER) img_bbox(d)[i - 1] = (int) lua_tointeger(L, -1); else if (lua_type(L, -1) == LUA_TSTRING) img_bbox(d)[i - 1] = dimen_to_number(L, lua_tostring(L, -1)); else luaL_error(L, "image.bbox table needs integer value or dimension string elements"); lua_pop(L, 1); /* v k t ... */ } img_set_bbox(d); break; case P_STREAM: if (img_filename(d) != NULL) luaL_error(L, "image.stream can't be used with image.filename"); if (img_state(d) >= DICT_FILESCANNED) luaL_error(L, "image.stream is now read-only"); if (img_pdfstream_ptr(d) == NULL) new_img_pdfstream_struct(d); xfree(img_pdfstream_stream(d)); img_pdfstream_stream(d) = xstrdup(lua_tostring(L, -1)); img_type(d) = IMG_TYPE_PDFSTREAM; break; case P_FILEPATH: case P_TOTALPAGES: case P_XSIZE: case P_YSIZE: case P_XRES: case P_YRES: case P_ROTATION: case P_IMAGETYPE: case P_OBJNUM: case P_INDEX: case P_COLORDEPTH: luaL_error(L, "image.%s is a read-only variable", img_parms[i].name); break; default: assert(0); } /* v k t ... */ }
static void lua_to_image(lua_State * L, image * a, image_dict * d) { int i, t; const char *s; s = lua_tostring(L,-2); t = lua_type(L, -1); if (lua_key_eq(s,width)) { if (t == LUA_TNIL) { set_wd_running(a); } else if (t == LUA_TNUMBER) { img_width(a) = (int) lua_roundnumber(L, -1); } else if (t == LUA_TSTRING) { img_width(a) = dimen_to_number(L, lua_tostring(L, -1)); } else { luaL_error(L, "image.width needs integer or nil value or dimension string"); } } else if (lua_key_eq(s,height)) { if (t == LUA_TNIL) { set_ht_running(a); } else if (t == LUA_TNUMBER) { img_height(a) = (int) lua_roundnumber(L, -1); } else if (t == LUA_TSTRING) { img_height(a) = dimen_to_number(L, lua_tostring(L, -1)); } else { luaL_error(L, "image.height needs integer or nil value or dimension string"); } } else if (lua_key_eq(s,depth)) { if (t == LUA_TNIL) { set_dp_running(a); } else if (t == LUA_TNUMBER) { img_depth(a) = (int) lua_roundnumber(L, -1); } else if (t == LUA_TSTRING) { img_depth(a) = dimen_to_number(L, lua_tostring(L, -1)); } else { luaL_error(L, "image.depth needs integer or nil value or dimension string"); } } else if (lua_key_eq(s,transform)) { if (t == LUA_TNUMBER) { img_transform(a) = (int) lua_tointeger(L, -1); } else { luaL_error(L, "image.transform needs integer value"); } } else if (lua_key_eq(s,filename)) { if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.filename is now read-only"); } else if (img_type(d) == IMG_TYPE_PDFSTREAM) { luaL_error(L, "image.filename can't be used with image.stream"); } else if (t == LUA_TSTRING) { xfree(img_filename(d)); img_filename(d) = xstrdup(lua_tostring(L, -1)); } else { luaL_error(L, "image.filename needs string value"); } } else if (lua_key_eq(s,visiblefilename)) { if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.visiblefilename is now read-only"); } else if (img_type(d) == IMG_TYPE_PDFSTREAM) { luaL_error(L, "image.visiblefilename can't be used with image.stream"); } else if (t == LUA_TSTRING) { xfree(img_visiblefilename(d)); img_visiblefilename(d) = xstrdup(lua_tostring(L, -1)); } else { luaL_error(L, "image.visiblefilename needs string value"); } } else if (lua_key_eq(s,attr)) { if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.attr is now read-only"); } else if (t == LUA_TSTRING) { xfree(img_attr(d)); img_attr(d) = xstrdup(lua_tostring(L, -1)); } else if (t == LUA_TNIL) { xfree(img_attr(d)); } else { luaL_error(L, "image.attr needs string or nil value"); } } else if (lua_key_eq(s,page)) { if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.page is now read-only"); } else if (t == LUA_TSTRING) { xfree(img_pagename(d)); img_pagename(d) = xstrdup(lua_tostring(L, -1)); img_pagenum(d) = 0; } else if (t == LUA_TNUMBER) { img_pagenum(d) = (int) lua_tointeger(L, -1); xfree(img_pagename(d)); } else { luaL_error(L, "image.page needs integer or string value"); } } else if (lua_key_eq(s,colorspace)) { if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.colorspace is now read-only"); } else if (t == LUA_TNIL) { img_colorspace(d) = 0; } else if (t == LUA_TNUMBER) { img_colorspace(d) = (int) lua_tointeger(L, -1); } else { luaL_error(L, "image.colorspace needs integer or nil value"); } } else if (lua_key_eq(s,pagebox)) { if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.pagebox is now read-only"); } else if (t == LUA_TNIL) { img_pagebox(d) = PDF_BOX_SPEC_MEDIA; } else if (t == LUA_TNUMBER) { i = lua_tointeger(L,-1); if (i < 0 || i >= img_pageboxes_max) { img_pagebox(d) = PDF_BOX_SPEC_MEDIA; } else { img_pagebox(d) = i; } } else if (t == LUA_TSTRING) { img_pagebox(d) = PDF_BOX_SPEC_MEDIA; for (i = 0; i < img_pageboxes_max; i++) { lua_rawgeti(L, LUA_REGISTRYINDEX, img_pageboxes[i]); if (lua_rawequal(L,-1,-2)) { img_pagebox(d) = i; lua_pop(L, 1); break; } else { lua_pop(L, 1); } } } else { luaL_error(L, "image.pagebox needs string, number or nil value"); } } else if (lua_key_eq(s,keepopen)) { if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.keepopen is now read-only"); } else if (t != LUA_TBOOLEAN) { luaL_error(L, "image.bbox needs boolean value"); } else { img_keepopen(d) = lua_toboolean(L, -1); } } else if (lua_key_eq(s,bbox)) { if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.bbox is now read-only"); } else if (t != LUA_TTABLE) { luaL_error(L, "image.bbox needs table value"); } else if (lua_rawlen(L, -1) != 4) { luaL_error(L, "image.bbox table must have exactly 4 elements"); } else { for (i = 1; i <= 4; i++) { /* v k t ... */ lua_pushinteger(L, i); /* idx v k t ... */ lua_gettable(L, -2); /* int v k t ... */ t = lua_type(L, -1); if (t == LUA_TNUMBER) { img_bbox(d)[i - 1] = (int) lua_roundnumber(L, -1); } else if (t == LUA_TSTRING) { img_bbox(d)[i - 1] = dimen_to_number(L, lua_tostring(L, -1)); } else { luaL_error(L, "image.bbox table needs integer value or dimension string elements"); } lua_pop(L, 1); /* v k t ... */ } img_set_bbox(d); } } else if (lua_key_eq(s,stream)) { if (img_filename(d) != NULL) { luaL_error(L, "image.stream can't be used with image.filename"); } else if (img_state(d) >= DICT_FILESCANNED) { luaL_error(L, "image.stream is now read-only"); } else { if (img_pdfstream_ptr(d) == NULL) { new_img_pdfstream_struct(d); } xfree(img_pdfstream_stream(d)); img_pdfstream_stream(d) = xstrdup(lua_tostring(L, -1)); img_type(d) = IMG_TYPE_PDFSTREAM; } } else { luaL_error(L, "image.%s can not be set", s); } }