diff --git a/pdf.c b/pdf.c index 48d7608a9..faf813a9b 100644 --- a/pdf.c +++ b/pdf.c @@ -577,6 +577,59 @@ static int cleanCache(lua_State *L) { return 0; } + +static int getPageLinks(lua_State *L) { + fz_link *page_links; + fz_link *link; + + int link_count; + + PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage"); + + page_links = fz_load_links(page->doc->xref, page->page); // page->doc->xref? + + lua_newtable(L); // all links + + link_count = 0; + + for (link = page_links; link; link = link->next) { + lua_newtable(L); // new link + + lua_pushstring(L, "x0"); + lua_pushinteger(L, link->rect.x0); + lua_settable(L, -3); + lua_pushstring(L, "y0"); + lua_pushinteger(L, link->rect.y0); + lua_settable(L, -3); + lua_pushstring(L, "x1"); + lua_pushinteger(L, link->rect.x1); + lua_settable(L, -3); + lua_pushstring(L, "y1"); + lua_pushinteger(L, link->rect.y1); + lua_settable(L, -3); + + if (link->dest.kind == FZ_LINK_URI) { + lua_pushstring(L, "uri"); + lua_pushstring(L, link->dest.ld.uri.uri); + lua_settable(L, -3); + } else if (link->dest.kind == FZ_LINK_GOTO) { + lua_pushstring(L, "page"); + lua_pushinteger(L, link->dest.ld.gotor.page); // FIXME page+1? + lua_settable(L, -3); + } else { + printf("ERROR: unkown link kind: %x", link->dest.kind); + } + + lua_rawseti(L, -2, ++link_count); + } + + printf("## getPageLinks found %d links in document", link_count); + + fz_drop_link(page->doc->context, page_links); + + return 1; +} + static const struct luaL_Reg pdf_func[] = { {"openDocument", openDocument}, {NULL, NULL} @@ -599,6 +652,7 @@ static const struct luaL_Reg pdfpage_meth[] = { {"getSize", getPageSize}, {"getUsedBBox", getUsedBBox}, {"getPageText", getPageText}, + {"getPageLinks", getPageLinks}, {"close", closePage}, {"__gc", closePage}, {"draw", drawPage}, diff --git a/pdfreader.lua b/pdfreader.lua index b14d0a94a..12961d438 100644 --- a/pdfreader.lua +++ b/pdfreader.lua @@ -45,3 +45,16 @@ function PDFReader:getText(pageno) page:close() return text end + +function PDFReader:getPageLinks(pageno) + local ok, page = pcall(self.doc.openPage, self.doc, pageno) + if not ok then + -- TODO: error handling + return nil + end + local links = page:getPageLinks() + Debug("## page:getPageLinks ", links) + page:close() + return links +end + diff --git a/unireader.lua b/unireader.lua index 5e4263714..a9a73f22a 100644 --- a/unireader.lua +++ b/unireader.lua @@ -2062,6 +2062,11 @@ function UniReader:searchHighLight(search) end +function UniReader:getPageLinks(pageno) + Debug("getPageLinks not supported in this format") + return nil +end + -- used in UniReader:showMenu() function UniReader:_drawReadingInfo() local width, height = G_width, G_height @@ -2855,6 +2860,13 @@ function UniReader:addAllCommands() end end ) + self.commands:add(KEY_L, MOD_SHIFT, "L", + "page links", + function(unireader) + Debug("unireader", unireader) + unireader:getPageLinks( unireader.pageno ) + end + ) self.commands:add(KEY_BACK,MOD_ALT,"Back", "close document", function(unireader)