From b027efb11a3e6412ab2a6c0ba848764c82c36dc2 Mon Sep 17 00:00:00 2001 From: xelxebar Date: Sat, 10 Nov 2018 01:32:26 +0900 Subject: [PATCH] [Feature] Implement getMetadata() function for DjVu documents (#753) This implements the getMetadata() function for DjVu documents, following in the style of the same for the PDF implementation. This just dumbly copies all the metadata key-value pairs into a lua table. The intent is to give the frontend everything we possibly can and let it figure out what to do with the info, an example of which can be found in the companion pull request koreader/koreader#4314. --- djvu.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/djvu.c b/djvu.c index c8c72b3c0..fcb3ca327 100644 --- a/djvu.c +++ b/djvu.c @@ -162,6 +162,32 @@ static int setColorRendering(lua_State *L) { return 0; } +static int getMetadata(lua_State *L) { + DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); + miniexp_t anno = ddjvu_document_get_anno(doc->doc_ref, 1); + miniexp_t *keys = ddjvu_anno_get_metadata_keys(anno); + + // `keys` can be null if there's an error. In that case, + // we have the choice of returning either `nil` or + // the empty table back to lua. Here we prefer the latter. + lua_newtable(L); + if (!keys) return 1; + + int i; + for (i = 0; keys[i] != miniexp_nil; i++) { + const char *value = ddjvu_anno_get_metadata(anno, keys[i]); + + if (value) { + lua_pushstring(L, miniexp_to_name(keys[i])); + lua_pushstring(L, value); + lua_settable(L, -3); + } + } + + if (keys) free(keys); + return 1; +} + static int getNumberOfPages(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); lua_pushinteger(L, ddjvu_document_get_pagenum(doc->doc_ref)); @@ -716,6 +742,7 @@ static const struct luaL_Reg djvu_func[] = { static const struct luaL_Reg djvudocument_meth[] = { {"openPage", openPage}, + {"getMetadata", getMetadata}, {"getPages", getNumberOfPages}, {"getToc", getTableOfContent}, {"getPageText", getPageText},