Skip to content

Commit

Permalink
[Feature] Implement getMetadata() function for DjVu documents (#753)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xelxebar authored and Frenzie committed Nov 9, 2018
1 parent bcd8ba4 commit b027efb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions djvu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit b027efb

Please sign in to comment.