mod: call different reader in reader.lua

call PDFReader or DJVUReader according to filetype
pull/2/merge
Qingping Hou 12 years ago
parent e258371134
commit 516e32bc46

@ -44,7 +44,6 @@ typedef struct DrawContext {
double gamma; double gamma;
int offset_x; int offset_x;
int offset_y; int offset_y;
ddjvu_format_t *pixelformat;
} DrawContext; } DrawContext;
@ -132,11 +131,6 @@ static int newDrawContext(lua_State *L) {
dc->offset_y = offset_y; dc->offset_y = offset_y;
dc->gamma = gamma; dc->gamma = gamma;
/*dc->pixelformat = ddjvu_format_create(DDJVU_FORMAT_RGBMASK32, 4, format_mask);*/
dc->pixelformat = ddjvu_format_create(DDJVU_FORMAT_GREY8, 0, NULL);
ddjvu_format_set_row_order(dc->pixelformat, 1);
ddjvu_format_set_y_direction(dc->pixelformat, 1);
/*ddjvu_format_set_ditherbits(dc->pixelformat, 2);*/
luaL_getmetatable(L, "drawcontext"); luaL_getmetatable(L, "drawcontext");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
@ -185,7 +179,6 @@ static int dcGetZoom(lua_State *L) {
static int dcSetGamma(lua_State *L) { static int dcSetGamma(lua_State *L) {
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext"); DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
dc->gamma = luaL_checknumber(L, 2); dc->gamma = luaL_checknumber(L, 2);
ddjvu_format_set_gamma(dc->pixelformat, dc->gamma);
return 0; return 0;
} }
@ -277,6 +270,7 @@ static int drawPage(lua_State *L) {
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext"); DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer"); BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
ddjvu_format_t *pixelformat;
ddjvu_rect_t pagerect, renderrect; ddjvu_rect_t pagerect, renderrect;
uint8_t *imagebuffer = NULL; uint8_t *imagebuffer = NULL;
@ -284,6 +278,12 @@ static int drawPage(lua_State *L) {
/* fill pixel map with white color */ /* fill pixel map with white color */
memset(imagebuffer, 0xFF, (bb->w)*(bb->h)+1); memset(imagebuffer, 0xFF, (bb->w)*(bb->h)+1);
pixelformat = ddjvu_format_create(DDJVU_FORMAT_GREY8, 0, NULL);
ddjvu_format_set_row_order(pixelformat, 1);
ddjvu_format_set_y_direction(pixelformat, 1);
ddjvu_format_set_gamma(pixelformat, dc->gamma);
/*ddjvu_format_set_ditherbits(dc->pixelformat, 2);*/
/* render full page into rectangle specified by pagerect */ /* render full page into rectangle specified by pagerect */
/*pagerect.x = luaL_checkint(L, 4);*/ /*pagerect.x = luaL_checkint(L, 4);*/
/*pagerect.y = luaL_checkint(L, 5);*/ /*pagerect.y = luaL_checkint(L, 5);*/
@ -307,19 +307,14 @@ static int drawPage(lua_State *L) {
/*ctm = fz_concat(ctm, fz_rotate(dc->rotate));*/ /*ctm = fz_concat(ctm, fz_rotate(dc->rotate));*/
/*ctm = fz_concat(ctm, fz_translate(dc->offset_x, dc->offset_y));*/ /*ctm = fz_concat(ctm, fz_translate(dc->offset_x, dc->offset_y));*/
printf("%d, %d\n", bb->w, bb->h);
ddjvu_page_render(page->page_ref, ddjvu_page_render(page->page_ref,
DDJVU_RENDER_COLOR, DDJVU_RENDER_COLOR,
&pagerect, &pagerect,
&renderrect, &renderrect,
dc->pixelformat, pixelformat,
bb->w, bb->w,
imagebuffer); imagebuffer);
/*if(dc->gamma >= 0.0) {*/
/*fz_gamma_pixmap(page->doc->context, pix, dc->gamma);*/
/*}*/
uint8_t *bbptr = (uint8_t*)bb->data; uint8_t *bbptr = (uint8_t*)bb->data;
uint8_t *pmptr = (uint8_t*)imagebuffer; uint8_t *pmptr = (uint8_t*)imagebuffer;
int x, y; int x, y;
@ -342,6 +337,7 @@ static int drawPage(lua_State *L) {
free(imagebuffer); free(imagebuffer);
pmptr = imagebuffer = NULL; pmptr = imagebuffer = NULL;
ddjvu_format_release(pixelformat);
return 0; return 0;
} }

@ -40,10 +40,6 @@ DJVUReader = {
shift_y = 50, shift_y = 50,
pan_by_page = false, -- using shift_[xy] or width/height pan_by_page = false, -- using shift_[xy] or width/height
-- keep track of input state:
shiftmode = false, -- shift pressed
altmode = false, -- alt pressed
-- the djvu document: -- the djvu document:
doc = nil, doc = nil,
-- the document's setting store: -- the document's setting store:
@ -101,7 +97,7 @@ function DJVUReader:draworcache(no, zoom, offset_x, offset_y, width, height, gam
local page = self.doc:openPage(no) local page = self.doc:openPage(no)
local dc = self:setzoom(page, hash) local dc = self:setzoom(page, hash)
--print("--drawing page : "..no) --print("--drawing page : "..no)
page:draw(dc, self.cache[hash].bb, 0, 0) page:draw(dc, self.cache[hash].bb)
--print("--page draught: "..no) --print("--page draught: "..no)
page:close() page:close()
else else
@ -125,14 +121,14 @@ function DJVUReader:clearcache()
end end
-- open a DJVU file and its settings store -- open a DJVU file and its settings store
function DJVUReader:open(filename, password) function DJVUReader:open(filename)
self.doc = djvu.openDocument(filename, password or "") self.doc = djvu.openDocument(filename)
if self.doc ~= nil then if self.doc ~= nil then
--self.settings = DocSettings:open(filename) self.settings = DocSettings:open(filename)
--local gamma = self.settings:readsetting("gamma") local gamma = self.settings:readsetting("gamma")
--if gamma then if gamma then
--self.globalgamma = gamma self.globalgamma = gamma
--end end
return true return true
end end
return false return false
@ -219,44 +215,44 @@ function DJVUReader:goto(no)
return return
end end
--[[ -- for jump_stack]] -- for jump_stack
--if self.pageno and math.abs(self.pageno - no) > 1 then if self.pageno and math.abs(self.pageno - no) > 1 then
--local jump_item = nil local jump_item = nil
---- add current page to jump_stack if no in -- add current page to jump_stack if no in
--for _t,_v in ipairs(self.jump_stack) do for _t,_v in ipairs(self.jump_stack) do
--if _v.page == self.pageno then if _v.page == self.pageno then
--jump_item = _v jump_item = _v
--table.remove(self.jump_stack, _t) table.remove(self.jump_stack, _t)
--elseif _v.page == no then elseif _v.page == no then
---- the page we jumped to should not be show in stack -- the page we jumped to should not be show in stack
--table.remove(self.jump_stack, _t) table.remove(self.jump_stack, _t)
--end end
--end end
---- create a new one if not found -- create a new one if not found
--if not jump_item then if not jump_item then
--jump_item = { jump_item = {
--page = self.pageno, page = self.pageno,
--datetime = os.date("%Y-%m-%d %H:%M:%S"), datetime = os.date("%Y-%m-%d %H:%M:%S"),
--} }
--end end
---- insert at the start -- insert at the start
--table.insert(self.jump_stack, 1, jump_item) table.insert(self.jump_stack, 1, jump_item)
--if #self.jump_stack > 10 then if #self.jump_stack > 10 then
---- remove the last element to keep the size less than 10 -- remove the last element to keep the size less than 10
--table.remove(self.jump_stack) table.remove(self.jump_stack)
--end end
--[[end]] end
self.pageno = no self.pageno = no
self:show(no) self:show(no)
--[[ if no < self.doc:getPages() then]] if no < self.doc:getPages() then
--if self.globalzoommode ~= self.ZOOM_BY_VALUE then if self.globalzoommode ~= self.ZOOM_BY_VALUE then
---- pre-cache next page -- pre-cache next page
--self:draworcache(no+1,self.globalzoommode,self.offset_x,self.offset_y,width,height,self.globalgamma,self.globalrotate) self:draworcache(no+1,self.globalzoommode,self.offset_x,self.offset_y,width,height,self.globalgamma,self.globalrotate)
--else else
--self:draworcache(no,self.globalzoom,self.offset_x,self.offset_y,width,height,self.globalgamma,self.globalrotate) self:draworcache(no,self.globalzoom,self.offset_x,self.offset_y,width,height,self.globalgamma,self.globalrotate)
--end end
--[[end]] end
end end
-- adjust global gamma setting -- adjust global gamma setting
@ -283,11 +279,6 @@ function DJVUReader:setglobalzoom(zoom)
end end
end end
function DJVUReader:setrotate(rotate)
self.globalrotate = rotate
self:goto(self.pageno)
end
function DJVUReader:showTOC() function DJVUReader:showTOC()
toc = self.doc:getTOC() toc = self.doc:getTOC()
local menu_items = {} local menu_items = {}
@ -398,10 +389,6 @@ function DJVUReader:inputloop()
self:showTOC() self:showTOC()
elseif ev.code == KEY_B then elseif ev.code == KEY_B then
self:showJumpStack() self:showJumpStack()
elseif ev.code == KEY_J then
self:setrotate( self.globalrotate + 10 )
elseif ev.code == KEY_K then
self:setrotate( self.globalrotate - 10 )
end end
if self.globalzoommode == self.ZOOM_BY_VALUE then if self.globalzoommode == self.ZOOM_BY_VALUE then

@ -41,9 +41,7 @@ function FileChooser:readdir()
for f in lfs.dir(self.path) do for f in lfs.dir(self.path) do
if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not string.match(f, "^%.[^.]") then if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not string.match(f, "^%.[^.]") then
table.insert(self.dirs, f) table.insert(self.dirs, f)
--elseif string.match(f, ".+%.[pP][dD][fF]$") or string.match(f, ".+%.[dD][jJ][vV][uU]$")then elseif string.match(f, ".+%.[pP][dD][fF]$") or string.match(f, ".+%.[dD][jJ][vV][uU]$")then
--@TODO search for all files 03.03 2012
elseif string.match(f, ".+%.[dD][jJ][vV][uU]$")then
table.insert(self.files, f) table.insert(self.files, f)
end end
end end

@ -31,6 +31,22 @@ longopts = {
device = "d", device = "d",
help = "h" help = "h"
} }
function openFile(filename)
local file_type = string.lower(string.match(filename, ".+%.(.+)"))
if file_type == "djvu" then
if DJVUReader:open(filename) then
DJVUReader:goto(tonumber(DJVUReader.settings:readsetting("last_page")) or 1)
DJVUReader:inputloop()
end
elseif file_type == "pdf" then
if PDFReader:open(filename,"") then -- TODO: query for password
PDFReader:goto(tonumber(PDFReader.settings:readsetting("last_page") or 1))
PDFReader:inputloop()
end
end
end
optarg, optind = alt_getopt.get_opts(ARGV, "p:G:hg:d:", longopts) optarg, optind = alt_getopt.get_opts(ARGV, "p:G:hg:d:", longopts)
if optarg["h"] or ARGV[optind] == nil then if optarg["h"] or ARGV[optind] == nil then
print("usage: ./reader.lua [OPTION] ... DOCUMENT.PDF") print("usage: ./reader.lua [OPTION] ... DOCUMENT.PDF")
@ -93,50 +109,26 @@ if r_cfont ~=nil then
FontChooser.cfont = r_cfont FontChooser.cfont = r_cfont
end end
-- display directory or open file
if lfs.attributes(ARGV[optind], "mode") == "directory" then if lfs.attributes(ARGV[optind], "mode") == "directory" then
local running = true local running = true
FileChooser:setPath(ARGV[optind]) FileChooser:setPath(ARGV[optind])
while running do while running do
local pdffile = FileChooser:choose(0,height) local file = FileChooser:choose(0,height)
if pdffile ~= nil then if file ~= nil then
if DJVUReader:open(pdffile,"") then openFile(file)
DJVUReader:goto(1)
DJVUReader:inputloop()
end
else else
running = false running = false
end end
end end
else else
DJVUReader:open(ARGV[optind], optarg["p"]) openFile(ARGV[optind], optarg["p"])
DJVUReader:goto(tonumber(optarg["g"]) or tonumber(PDFReader.settings:readsetting("last_page") or 1))
DJVUReader:inputloop()
end end
--[[if lfs.attributes(ARGV[optind], "mode") == "directory" then]] -- save reader settings
--local running = true reader_settings:savesetting("cfont", FontChooser.cfont)
--FileChooser:setPath(ARGV[optind]) reader_settings:close()
--while running do
--local pdffile = FileChooser:choose(0,height)
--if pdffile ~= nil then
--if PDFReader:open(pdffile,"") then -- TODO: query for password
--PDFReader:goto(tonumber(PDFReader.settings:readsetting("last_page") or 1))
--PDFReader:inputloop()
--end
--else
--running = false
--end
--end
--else
----PDFReader:open(ARGV[optind], optarg["p"])
----PDFReader:goto(tonumber(optarg["g"]) or tonumber(PDFReader.settings:readsetting("last_page") or 1))
----PDFReader:inputloop()
--end
---- save reader settings
--reader_settings:savesetting("cfont", FontChooser.cfont)
--reader_settings:close()
input.closeAll() input.closeAll()
--os.execute('test -e /proc/keypad && echo "send '..KEY_HOME..'" > /proc/keypad ') --os.execute('test -e /proc/keypad && echo "send '..KEY_HOME..'" > /proc/keypad ')

Loading…
Cancel
Save