diff options
Diffstat (limited to 'Build/source/texk/web2c/luatexdir/lua/lpdflib.c')
-rw-r--r-- | Build/source/texk/web2c/luatexdir/lua/lpdflib.c | 331 |
1 files changed, 149 insertions, 182 deletions
diff --git a/Build/source/texk/web2c/luatexdir/lua/lpdflib.c b/Build/source/texk/web2c/luatexdir/lua/lpdflib.c index 00cffd398d7..a93f75a2aa0 100644 --- a/Build/source/texk/web2c/luatexdir/lua/lpdflib.c +++ b/Build/source/texk/web2c/luatexdir/lua/lpdflib.c @@ -18,14 +18,12 @@ with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */ static const char _svn_version[] = - "$Id: lpdflib.c 4877 2014-03-14 01:26:05Z luigi $ " + "$Id: lpdflib.c 4956 2014-03-28 12:12:17Z luigi $ " "$URL: https://foundry.supelec.fr/svn/luatex/trunk/source/texk/web2c/luatexdir/lua/lpdflib.c $"; #include "ptexlib.h" #include "lua/luatex-api.h" -#define PDF_ENV "pdf.env" - static int luapdfprint(lua_State * L) { int n; @@ -185,47 +183,10 @@ static int l_immediateobj(lua_State * L) return 1; } -/**********************************************************************/ -/* for lookup within table in LUA_REGISTRYINDEX (instead of repeated strcmp()) */ - -typedef enum { P__ZERO, - P_CATALOG, - P_H, - P_INFO, - P_NAMES, - P_PDFCATALOG, - P_PDFINFO, - P_PDFNAMES, - P_PDFTRAILER, - P_RAW, - P_STREAM, - P_TRAILER, - P_V, - P__SENTINEL -} parm_idx; - -static const parm_struct pdf_parms[] = { - {NULL, P__ZERO}, /* dummy; lua indices run from 1 */ - {"catalog", P_CATALOG}, - {"h", P_H}, - {"info", P_INFO}, - {"names", P_NAMES}, - {"pdfcatalog", P_PDFCATALOG}, /* obsolescent */ - {"pdfinfo", P_PDFINFO}, /* obsolescent */ - {"pdfnames", P_PDFNAMES}, /* obsolescent */ - {"pdftrailer", P_PDFTRAILER}, /* obsolescent */ - {"raw", P_RAW}, - {"stream", P_STREAM}, - {"trailer", P_TRAILER}, - {"v", P_V}, - {NULL, P__SENTINEL} -}; - -/**********************************************************************/ - static int table_obj(lua_State * L) { - int k, type, obj_compression; + const char *type; + int k, obj_compression; int compress_level = -1; /* unset */ int os_threshold = OBJSTM_ALWAYS; /* default: put non-stream objects into object streams */ int saved_compress_level = static_pdf->compress_level; @@ -238,33 +199,20 @@ static int table_obj(lua_State * L) /* get object "type" */ - /* lua_pushstring(L, "type"); /\* ks t *\/ */ - /* lua_gettable(L, -2); /\* vs? t *\/ */ lua_key_rawgeti(type); if (lua_isnil(L, -1)) /* !vs t */ luaL_error(L, "pdf.obj(): object \"type\" missing"); if (!lua_isstring(L, -1)) /* !vs t */ luaL_error(L, "pdf.obj(): object \"type\" must be string"); - lua_pushstring(L, PDF_ENV); /* s vs t */ - lua_gettable(L, LUA_REGISTRYINDEX); /* t vs t */ - lua_pushvalue(L, -2); /* vs t vs t */ - lua_gettable(L, -2); /* i? t vs t */ - if (!lua_isnumber(L, -1)) /* !i t vs t */ - luaL_error(L, "pdf.obj(): \"%s\" is not a valid object type", lua_tostring(L, -3)); - type = (int) lua_tointeger(L, -1); /* i t vs t */ - switch (type) { - case P_RAW: - case P_STREAM: - break; - default: - luaL_error(L, "pdf.obj(): \"%s\" is not a valid object type", lua_tostring(L, -3)); /* i vs t */ + type = lua_tostring(L, -1); + + if (! (lua_key_eq(type, raw) || lua_key_eq(type, stream))) { + luaL_error(L, "pdf.obj(): \"%s\" is not a valid object type", type); /* i vs t */ } - lua_pop(L, 3); /* t */ + lua_pop(L, 1); /* t */ /* get optional "immediate" */ - /* lua_pushstring(L, "immediate"); /\* ks t *\/ */ - /* lua_gettable(L, -2); /\* b? t *\/ */ lua_key_rawgeti(immediate); if (!lua_isnil(L, -1)) { /* b? t */ if (!lua_isboolean(L, -1)) /* !b t */ @@ -275,8 +223,6 @@ static int table_obj(lua_State * L) /* is a reserved object referenced by "objnum"? */ - /* lua_pushstring(L, "objnum"); /\* ks t *\/ */ - /* lua_gettable(L, -2); /\* vi? t *\/ */ lua_key_rawgeti(objnum); if (!lua_isnil(L, -1)) { /* vi? t */ if (!lua_isnumber(L, -1)) /* !vi t */ @@ -298,11 +244,9 @@ static int table_obj(lua_State * L) /* get optional "attr" (allowed only for stream case) */ - /* lua_pushstring(L, "attr"); /\* ks t *\/ */ - /* lua_gettable(L, -2); /\* attr-s? t *\/ */ lua_key_rawgeti(attr); if (!lua_isnil(L, -1)) { /* attr-s? t */ - if (type != P_STREAM) + if (! lua_key_eq(type, stream)) luaL_error(L, "pdf.obj(): \"attr\" key not allowed for non-stream object"); if (!lua_isstring(L, -1)) /* !attr-s t */ luaL_error(L, "pdf.obj(): object \"attr\" must be string"); @@ -311,16 +255,15 @@ static int table_obj(lua_State * L) lua_pop(L, 1); /* t */ } else obj_obj_stream_attr(static_pdf, k) = luaL_ref(Luas, LUA_REGISTRYINDEX); /* t */ - } else + } else { lua_pop(L, 1); /* t */ + } /* get optional "compresslevel" (allowed only for stream case) */ - /* lua_pushstring(L, "compresslevel"); /\* ks t *\/ */ - /* lua_gettable(L, -2); /\* vi? t *\/ */ lua_key_rawgeti(compresslevel); if (!lua_isnil(L, -1)) { /* vi? t */ - if (type == P_RAW) + if (lua_key_eq(type, raw)) luaL_error(L, "pdf.obj(): \"compresslevel\" key not allowed for raw object"); if (!lua_isnumber(L, -1)) /* !vi t */ luaL_error(L, "pdf.obj(): \"compresslevel\" must be integer"); @@ -336,11 +279,9 @@ static int table_obj(lua_State * L) /* get optional "objcompression" (allowed only for non-stream case) */ - /* lua_pushstring(L, "objcompression"); /\* ks t *\/ */ - /* lua_gettable(L, -2); /\* b? t *\/ */ lua_key_rawgeti(objcompression); if (!lua_isnil(L, -1)) { /* b? t */ - if (type == P_STREAM) + if (lua_key_eq(type, stream)) luaL_error(L, "pdf.obj(): \"objcompression\" key not allowed for stream object"); if (!lua_isboolean(L, -1)) /* !b t */ luaL_error(L, "pdf.obj(): \"objcompression\" must be boolean"); @@ -357,21 +298,15 @@ static int table_obj(lua_State * L) /* now the object contents for all cases are handled */ - /* lua_pushstring(L, "string"); /\* ks t *\/ */ - /* lua_gettable(L, -2); /\* string-s? t *\/ */ lua_key_rawgeti(string); - /* lua_pushstring(L, "file"); /\* ks string-s? t *\/ */ - /* lua_gettable(L, -3); /\* file-s? string-s? t *\/ */ - lua_rawgeti(L, LUA_REGISTRYINDEX, lua_key_index(file)); - lua_gettable(L, -3); + lua_key_rawgeti_n(file,-2); if (!lua_isnil(L, -1) && !lua_isnil(L, -2)) /* file-s? string-s? t */ luaL_error(L, "pdf.obj(): \"string\" and \"file\" must not be given together"); if (lua_isnil(L, -1) && lua_isnil(L, -2)) /* nil nil t */ luaL_error(L, "pdf.obj(): no \"string\" or \"file\" given"); - switch (type) { - case P_RAW: + if (lua_key_eq(type, raw)) { if (immediate == 1) pdf_begin_obj(static_pdf, k, os_threshold); if (!lua_isnil(L, -2)) { /* file-s? string-s? t */ @@ -408,8 +343,7 @@ static int table_obj(lua_State * L) } if (immediate == 1) pdf_end_obj(static_pdf); - break; - case P_STREAM: + } else { if (immediate == 1) { pdf_begin_obj(static_pdf, k, OBJSTM_NEVER); /* 0 = not an object stream candidate! */ pdf_begin_dict(static_pdf); @@ -456,9 +390,6 @@ static int table_obj(lua_State * L) pdf_end_stream(static_pdf); pdf_end_obj(static_pdf); } - break; - default: - assert(0); } static_pdf->compress_level = saved_compress_level; return k; @@ -511,7 +442,7 @@ static int orig_obj(lua_State * L) } if (lua_key_eq(st_s, stream)) { set_obj_obj_is_stream(static_pdf, k); - } else if (lua_key_eq(st_s, streamfile)) { + } else if (lua_key_eq(st_s, streamfile)) { set_obj_obj_is_stream(static_pdf, k); set_obj_obj_is_file(static_pdf, k); } else @@ -566,7 +497,7 @@ static int l_reserveobj(lua_State * L) case 1: if (!lua_isstring(L, -1)) luaL_error(L, "pdf.reserveobj() optional argument must be string"); - if (lua_key_eq(st_s, annot)) { + if (lua_key_eq(st_s, annot)) { pdf_last_annot = pdf_create_obj(static_pdf, obj_type_annot, 0); } else { luaL_error(L, "pdf.reserveobj() optional string must be \"annot\""); @@ -599,102 +530,135 @@ static int l_registerannot(lua_State * L) return 0; } +static int l_get_pdf_value(lua_State * L, int key) +{ + lua_rawgeti(L, LUA_REGISTRYINDEX, lua_key_index(pdf_data)); + lua_gettable(L, LUA_REGISTRYINDEX); + /* [table] */ + lua_rawgeti(L, LUA_REGISTRYINDEX, key); + /* [table] [key] */ + lua_rawget(L,-2); + return 1; +} +static int l_get_pageresources(lua_State * L) { + return l_get_pdf_value(L,lua_key_index(pageresources)); +} +static int l_get_pageattributes(lua_State * L) { + return l_get_pdf_value(L,lua_key_index(pageattributes)); +} +static int l_get_pagesattributes(lua_State * L) { + return l_get_pdf_value(L,lua_key_index(pagesattributes)); +} +static int l_get_catalog(lua_State * L) { + return l_get_pdf_value(L,lua_key_index(catalog)); +} +static int l_get_info(lua_State * L) { + return l_get_pdf_value(L,lua_key_index(info)); +} +static int l_get_names(lua_State * L) { + return l_get_pdf_value(L,lua_key_index(names)); +} + + +static int l_get_trailer(lua_State * L) { + return l_get_pdf_value(L,lua_key_index(trailer)); +} + + static int getpdf(lua_State * L) { - char *s; - int i, l; - if (lua_isstring(L, 2) && (lua_tostring(L, 2) != NULL)) { - lua_pushstring(L, PDF_ENV); /* s ... */ - lua_gettable(L, LUA_REGISTRYINDEX); /* t ... */ - lua_pushvalue(L, 2); /* st t ... */ - lua_gettable(L, -2); /* i? t ... */ - if (lua_isnumber(L, -1)) { /* i t ... */ - i = (int) lua_tointeger(L, -1); /* i t ... */ - lua_pop(L, 2); /* ... */ - switch (i) { - case P_PDFCATALOG: - case P_CATALOG: - s = tokenlist_to_cstring(pdf_catalog_toks, true, &l); - lua_pushlstring(L, s, (size_t) l); - break; - case P_PDFINFO: - case P_INFO: - s = tokenlist_to_cstring(pdf_info_toks, true, &l); - lua_pushlstring(L, s, (size_t) l); - break; - case P_PDFNAMES: - case P_NAMES: - s = tokenlist_to_cstring(pdf_names_toks, true, &l); - lua_pushlstring(L, s, (size_t) l); - break; - case P_PDFTRAILER: - case P_TRAILER: - s = tokenlist_to_cstring(pdf_trailer_toks, true, &l); - lua_pushlstring(L, s, (size_t) l); - break; - case P_H: - lua_pushnumber(L, static_pdf->posstruct->pos.h); - break; - case P_V: - lua_pushnumber(L, static_pdf->posstruct->pos.v); - break; - default: - lua_rawget(L, -2); - } - } else { - lua_pop(L, 2); /* ... */ - lua_rawget(L, -2); + /* [pdf table] [key] */ + const char *s ; + if (lua_gettop(L) != 2) { + return 0; + } + if (lua_isstring(L, -1)) { + s = lua_tostring(L, -1); + if (lua_key_eq(s,h)) { + lua_pushnumber(L, static_pdf->posstruct->pos.h); + } else if (lua_key_eq(s,v)) { + lua_pushnumber(L, static_pdf->posstruct->pos.v); + } else if ( + lua_key_eq(s,catalog) || lua_key_eq(s,info) || lua_key_eq(s,trailer) || lua_key_eq(s,names) || + lua_key_eq(s,pageattributes) || lua_key_eq(s,pagesattributes) || lua_key_eq(s,pageresources) + ) { + lua_rawgeti(L, LUA_REGISTRYINDEX, luaS_index(pdf_data)); + lua_gettable(L, LUA_REGISTRYINDEX); + /* [pdf table] [key] [pdf.data table] */ + lua_replace(L, -3); + /* [pdf.data table] [key] */ } - } else { - lua_pushnil(L); } - return 1; + lua_rawget(L, -2); + return 0; } + + + +static int l_set_pdf_value(lua_State * L, int key) +{ + if (lua_isstring(L, -1)) { + /* [value] */ + lua_rawgeti(L, LUA_REGISTRYINDEX, lua_key_index(pdf_data)); + lua_gettable(L, LUA_REGISTRYINDEX); + /* [value] [table] */ + lua_rawgeti(L, LUA_REGISTRYINDEX, key); + /* [value] [table] [key] */ + lua_pushvalue(L, -3); + /* [table] [key] [value] */ + lua_rawset(L,-3); + } + return 0; +} +static int l_set_pageresources(lua_State * L) { + return l_set_pdf_value(L,lua_key_index(pageresources)); +} +static int l_set_pageattributes(lua_State * L) { + return l_set_pdf_value(L,lua_key_index(pageattributes)); +} +static int l_set_pagesattributes(lua_State * L) { + return l_set_pdf_value(L,lua_key_index(pagesattributes)); +} +static int l_set_catalog(lua_State * L) { + return l_set_pdf_value(L,lua_key_index(catalog)); +} +static int l_set_info(lua_State * L) { + return l_set_pdf_value(L,lua_key_index(info)); +} +static int l_set_names(lua_State * L) { + return l_set_pdf_value(L,lua_key_index(names)); +} +static int l_set_trailer(lua_State * L) { + return l_set_pdf_value(L,lua_key_index(trailer)); +} + + static int setpdf(lua_State * L) { - int i; + /* [pdf table] [key] [value] */ + const char *s ; if (lua_gettop(L) != 3) { return 0; } - (void) luaL_checkstring(L, 2); /* ... */ - lua_pushstring(L, PDF_ENV); /* s ... */ - lua_gettable(L, LUA_REGISTRYINDEX); /* t ... */ - lua_pushvalue(L, 2); /* st t ... */ - lua_gettable(L, -2); /* i? t ... */ - if (lua_isnumber(L, -1)) { /* i t ... */ - i = (int) lua_tointeger(L, -1); /* i t ... */ - lua_pop(L, 2); /* ... */ - switch (i) { - case P_PDFCATALOG: - case P_CATALOG: - pdf_catalog_toks = tokenlist_from_lua(L); - break; - case P_PDFINFO: - case P_INFO: - pdf_info_toks = tokenlist_from_lua(L); - break; - case P_PDFNAMES: - case P_NAMES: - pdf_names_toks = tokenlist_from_lua(L); - break; - case P_PDFTRAILER: - case P_TRAILER: - pdf_trailer_toks = tokenlist_from_lua(L); - break; - case P_H: - case P_V: - /* can't set |h| and |v| yet */ - default: - lua_rawset(L, -3); + if (lua_isstring(L, -2)) { + s = lua_tostring(L, -1); + if ( + lua_key_eq(s,catalog) || lua_key_eq(s,info) || lua_key_eq(s,trailer) || lua_key_eq(s,names) || + lua_key_eq(s,pageattributes) || lua_key_eq(s,pagesattributes) || lua_key_eq(s,pageresources) + ) { + lua_rawgeti(L, LUA_REGISTRYINDEX, luaS_index(pdf_data)); + lua_gettable(L, LUA_REGISTRYINDEX); + /* [pdf table] [key] [value] [pdf.data table] */ + lua_replace(L, -4); + /* [pdf.data table] [key] [value] */ } - } else { - lua_pop(L, 2); /* ... */ - lua_rawset(L, -3); } + lua_rawset(L, -3); return 0; } + static int l_objtype(lua_State * L) { int n = lua_gettop(L); @@ -741,18 +705,6 @@ static int l_mapline(lua_State * L) return 0; } -static int l_pdfmapfile(lua_State * L) -{ - luaL_error(L, "pdf.pdfmapfile() is obsolete. Use pdf.mapfile() instead."); - return 0; -} - -static int l_pdfmapline(lua_State * L) -{ - luaL_error(L, "pdf.pdfmapline() is obsolete. Use pdf.mapline() instead."); - return 0; -} - static int l_pageref(lua_State * L) { int n = lua_gettop(L); @@ -821,8 +773,6 @@ static const struct luaL_Reg pdflib[] = { {"obj", l_obj}, {"objtype", l_objtype}, {"pageref", l_pageref}, - {"pdfmapfile", l_pdfmapfile}, /* obsolete */ - {"pdfmapline", l_pdfmapline}, /* obsolete */ {"print", luapdfprint}, {"refobj", l_refobj}, {"registerannot", l_registerannot}, @@ -832,6 +782,20 @@ static const struct luaL_Reg pdflib[] = { {"getvpos", l_getvpos}, {"getmatrix", l_getmatrix}, {"hasmatrix", l_hasmatrix}, + {"setcatalog", l_set_catalog}, + {"setinfo", l_set_info}, + {"setnames", l_set_names}, + {"settrailer", l_set_trailer}, + {"setpageresources", l_set_pageresources}, + {"setpageattributes", l_set_pageattributes}, + {"setpagesattributes", l_set_pagesattributes}, + {"getcatalog", l_get_catalog}, + {"getinfo", l_get_info}, + {"getnames", l_get_names}, + {"gettrailer", l_get_trailer}, + {"getpageresources", l_get_pageresources}, + {"getpageattributes", l_get_pageattributes}, + {"getpagesattributes", l_get_pagesattributes}, {NULL, NULL} /* sentinel */ }; @@ -839,7 +803,10 @@ static const struct luaL_Reg pdflib[] = { int luaopen_pdf(lua_State * L) { - preset_environment(L, pdf_parms, PDF_ENV); + lua_pushstring(L,"pdf.data"); + lua_newtable(L); + lua_settable(L,LUA_REGISTRYINDEX); + /* */ luaL_register(L, "pdf", pdflib); /* build meta table */ luaL_newmetatable(L, "pdf.meta"); |