add: toc menu

pull/2/merge
Qingping Hou 12 years ago
parent e60242d3f6
commit ea498e7a5a

41
pdf.c

@ -88,28 +88,20 @@ static int getNumberOfPages(lua_State *L) {
} }
/* /*
* Return a table with (title,page) pair as entry: * helper function for getTableOfContent()
* {"chapter1"=12, "chapter2"=20}
*/ */
static int getTableOfContent(lua_State *L) { static int walkTableOfContent(lua_State *L, fz_outline* ol, int *count, int depth) {
fz_outline *ol; depth++;
int i;
PdfDocument *doc = (PdfDocument*) luaL_checkudata(L, 1, "pdfdocument");
ol = pdf_load_outline(doc->xref);
lua_newtable(L);
i = 1;
while(ol) { while(ol) {
lua_pushnumber(L, i); lua_pushnumber(L, *count);
/* set subtable */ /* set subtable */
lua_newtable(L); lua_newtable(L);
lua_pushstring(L, "page"); lua_pushstring(L, "page");
lua_pushnumber(L, ol->dest.ld.gotor.page + 1); lua_pushnumber(L, ol->dest.ld.gotor.page + 1);
lua_settable(L, -3); lua_settable(L, -3);
lua_pushstring(L, "level"); lua_pushstring(L, "depth");
lua_pushnumber(L, 1); // level 1 lua_pushnumber(L, depth);
lua_settable(L, -3); lua_settable(L, -3);
lua_pushstring(L, "title"); lua_pushstring(L, "title");
lua_pushstring(L, ol->title); lua_pushstring(L, ol->title);
@ -117,9 +109,28 @@ static int getTableOfContent(lua_State *L) {
lua_settable(L, -3); lua_settable(L, -3);
i++; (*count)++;
if (ol->down) {
walkTableOfContent(L, ol->down, count, depth);
}
ol = ol->next; ol = ol->next;
} }
return 0;
}
/*
* Return a table with (title,page) pair as entry:
* {"chapter1"=12, "chapter2"=20}
*/
static int getTableOfContent(lua_State *L) {
fz_outline *ol;
int count = 1;
PdfDocument *doc = (PdfDocument*) luaL_checkudata(L, 1, "pdfdocument");
ol = pdf_load_outline(doc->xref);
lua_newtable(L);
walkTableOfContent(L, ol, &count, 0);
return 1; return 1;
} }

@ -344,6 +344,8 @@ function PDFReader:inputloop()
no = toc_menu:choose(0, fb.bb:getHeight()) no = toc_menu:choose(0, fb.bb:getHeight())
if no then if no then
self:goto(no) self:goto(no)
else
self:goto(self.pageno)
end end
elseif ev.code == KEY_J then elseif ev.code == KEY_J then

@ -4,8 +4,8 @@ require "graphics"
TOCMenu = { TOCMenu = {
-- font for displaying file/dir names -- font for displaying file/dir names
face = freetype.newBuiltinFace("cjk", 25), face = freetype.newBuiltinFace("cjk", 22),
fhash = "s25", fhash = "s22",
-- font for page title -- font for page title
tface = freetype.newBuiltinFace("Helvetica-BoldOblique", 25), tface = freetype.newBuiltinFace("Helvetica-BoldOblique", 25),
tfhash = "hbo25", tfhash = "hbo25",
@ -15,7 +15,7 @@ TOCMenu = {
-- title height -- title height
title_H = 40, title_H = 40,
-- spacing between lines -- spacing between lines
spacing = 40, spacing = 36,
-- foot height -- foot height
foot_H = 27, foot_H = 27,
@ -97,8 +97,8 @@ function TOCMenu:choose(ypos, height)
local i = (self.page - 1) * perpage + c local i = (self.page - 1) * perpage + c
if i <= self.items then if i <= self.items then
y = ypos + self.title_H + (self.spacing * c) y = ypos + self.title_H + (self.spacing * c)
renderUtf8Text(fb.bb, 50, y, self.face, self.fhash, renderUtf8Text(fb.bb, 30, y, self.face, self.fhash,
self.toc[i]["title"], true) (" "):rep(self.toc[i]["depth"]-1)..self.toc[i]["title"], true)
end end
end end

Loading…
Cancel
Save