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;
int offset_x;
int offset_y;
ddjvu_format_t *pixelformat;
} DrawContext;
@ -132,11 +131,6 @@ static int newDrawContext(lua_State *L) {
dc->offset_y = offset_y;
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");
lua_setmetatable(L, -2);
@ -185,7 +179,6 @@ static int dcGetZoom(lua_State *L) {
static int dcSetGamma(lua_State *L) {
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
dc->gamma = luaL_checknumber(L, 2);
ddjvu_format_set_gamma(dc->pixelformat, dc->gamma);
return 0;
}
@ -277,6 +270,7 @@ static int drawPage(lua_State *L) {
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
ddjvu_format_t *pixelformat;
ddjvu_rect_t pagerect, renderrect;
uint8_t *imagebuffer = NULL;
@ -284,6 +278,12 @@ static int drawPage(lua_State *L) {
/* fill pixel map with white color */
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 */
/*pagerect.x = luaL_checkint(L, 4);*/
/*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_translate(dc->offset_x, dc->offset_y));*/
printf("%d, %d\n", bb->w, bb->h);
ddjvu_page_render(page->page_ref,
DDJVU_RENDER_COLOR,
&pagerect,
&renderrect,
dc->pixelformat,
pixelformat,
bb->w,
imagebuffer);
/*if(dc->gamma >= 0.0) {*/
/*fz_gamma_pixmap(page->doc->context, pix, dc->gamma);*/
/*}*/
uint8_t *bbptr = (uint8_t*)bb->data;
uint8_t *pmptr = (uint8_t*)imagebuffer;
int x, y;
@ -342,6 +337,7 @@ static int drawPage(lua_State *L) {
free(imagebuffer);
pmptr = imagebuffer = NULL;
ddjvu_format_release(pixelformat);
return 0;
}

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

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

@ -31,6 +31,22 @@ longopts = {
device = "d",
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)
if optarg["h"] or ARGV[optind] == nil then
print("usage: ./reader.lua [OPTION] ... DOCUMENT.PDF")
@ -93,50 +109,26 @@ if r_cfont ~=nil then
FontChooser.cfont = r_cfont
end
-- display directory or open file
if lfs.attributes(ARGV[optind], "mode") == "directory" then
local running = true
FileChooser:setPath(ARGV[optind])
while running do
local pdffile = FileChooser:choose(0,height)
if pdffile ~= nil then
if DJVUReader:open(pdffile,"") then
DJVUReader:goto(1)
DJVUReader:inputloop()
end
local file = FileChooser:choose(0,height)
if file ~= nil then
openFile(file)
else
running = false
end
end
else
DJVUReader:open(ARGV[optind], optarg["p"])
DJVUReader:goto(tonumber(optarg["g"]) or tonumber(PDFReader.settings:readsetting("last_page") or 1))
DJVUReader:inputloop()
openFile(ARGV[optind], optarg["p"])
end
--[[if lfs.attributes(ARGV[optind], "mode") == "directory" then]]
--local running = true
--FileChooser:setPath(ARGV[optind])
--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()
-- save reader settings
reader_settings:savesetting("cfont", FontChooser.cfont)
reader_settings:close()
input.closeAll()
--os.execute('test -e /proc/keypad && echo "send '..KEY_HOME..'" > /proc/keypad ')

Loading…
Cancel
Save