2012-03-05 01:46:16 +00:00
|
|
|
require "unireader"
|
|
|
|
|
2012-10-12 22:37:38 +00:00
|
|
|
DJVUReader = UniReader:new{
|
|
|
|
show_links_enable = false
|
|
|
|
}
|
2012-03-05 01:46:16 +00:00
|
|
|
|
2012-10-08 18:46:45 +00:00
|
|
|
-- check DjVu magic string to validate
|
|
|
|
function validDJVUFile(filename)
|
|
|
|
f = io.open(filename, "r")
|
|
|
|
if not f then return false end
|
|
|
|
local magic = f:read(8)
|
|
|
|
f:close()
|
|
|
|
if not magic or magic ~= "AT&TFORM" then return false end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-03-05 01:46:16 +00:00
|
|
|
-- open a DJVU file and its settings store
|
2012-03-05 01:58:19 +00:00
|
|
|
-- DJVU does not support password yet
|
2012-03-05 01:46:16 +00:00
|
|
|
function DJVUReader:open(filename)
|
2012-10-08 18:46:45 +00:00
|
|
|
if not validDJVUFile(filename) then
|
|
|
|
return false, "Not a valid DjVu file"
|
|
|
|
end
|
|
|
|
|
2012-03-19 23:10:19 +00:00
|
|
|
local ok
|
2012-03-31 12:36:10 +00:00
|
|
|
ok, self.doc = pcall(djvu.openDocument, filename, self.cache_document_size)
|
2012-03-19 23:10:19 +00:00
|
|
|
if not ok then
|
|
|
|
return ok, self.doc -- this will be the error message instead
|
|
|
|
end
|
|
|
|
return ok
|
2012-03-05 01:46:16 +00:00
|
|
|
end
|
2012-03-19 03:05:23 +00:00
|
|
|
|
2012-04-06 12:13:46 +00:00
|
|
|
function DJVUReader:init()
|
|
|
|
self:addAllCommands()
|
|
|
|
self:adjustDjvuReaderCommand()
|
|
|
|
end
|
|
|
|
|
|
|
|
function DJVUReader:adjustDjvuReaderCommand()
|
2012-04-10 12:45:22 +00:00
|
|
|
self.commands:del(KEY_J, MOD_SHIFT, "J")
|
|
|
|
self.commands:del(KEY_K, MOD_SHIFT, "K")
|
2012-08-28 22:48:56 +00:00
|
|
|
self.commands:add(KEY_R, nil, "R",
|
2012-08-30 12:38:18 +00:00
|
|
|
"select djvu page rendering mode",
|
2012-08-28 22:48:56 +00:00
|
|
|
function(self)
|
2012-08-29 10:39:17 +00:00
|
|
|
self:select_render_mode()
|
2012-08-28 22:48:56 +00:00
|
|
|
end)
|
2012-04-06 12:13:46 +00:00
|
|
|
end
|
2012-03-23 09:20:51 +00:00
|
|
|
|
2012-08-29 10:39:17 +00:00
|
|
|
-- select the rendering mode from those supported by djvulibre.
|
|
|
|
-- Note that if the values in the definition of ddjvu_render_mode_t in djvulibre/libdjvu/ddjvuapi.h change,
|
|
|
|
-- then we should update our values here also. This is a bit risky, but these values never change, so it should be ok :)
|
|
|
|
function DJVUReader:select_render_mode()
|
|
|
|
local mode_menu = SelectMenu:new{
|
|
|
|
menu_title = "Select DjVu page rendering mode",
|
|
|
|
item_array = {
|
|
|
|
"COLOUR (works for both colour and b&w pages)", -- 0 (colour page or stencil)
|
|
|
|
"BLACK & WHITE (for b&w pages only, much faster)", -- 1 (stencil or colour page)
|
|
|
|
"COLOUR ONLY (slightly faster than COLOUR)", -- 2 (colour page or fail)
|
2012-09-01 21:21:29 +00:00
|
|
|
"MASK ONLY (for b&w pages only)", -- 3 (stencil or fail)
|
2012-08-29 10:39:17 +00:00
|
|
|
"COLOUR BACKGROUND (show only background)", -- 4 (colour background layer)
|
|
|
|
"COLOUR FOREGROUND (show only foreground)" -- 5 (colour foreground layer)
|
|
|
|
},
|
|
|
|
current_entry = self.render_mode,
|
|
|
|
}
|
|
|
|
local mode = mode_menu:choose(0, fb.bb:getHeight())
|
|
|
|
if mode then
|
|
|
|
self.render_mode = mode - 1
|
|
|
|
self:clearCache()
|
2012-08-28 22:48:56 +00:00
|
|
|
end
|
2012-08-29 11:09:41 +00:00
|
|
|
self:redrawCurrentPage()
|
2012-08-28 22:48:56 +00:00
|
|
|
end
|
|
|
|
|
2012-04-12 03:23:22 +00:00
|
|
|
----------------------------------------------------
|
|
|
|
-- highlight support
|
|
|
|
----------------------------------------------------
|
|
|
|
function DJVUReader:getText(pageno)
|
|
|
|
return self.doc:getPageText(pageno)
|
|
|
|
end
|
2012-03-23 09:20:51 +00:00
|
|
|
|
2012-05-23 05:04:05 +00:00
|
|
|
-- for incompatible API fixing
|
|
|
|
function DJVUReader:invertTextYAxel(pageno, text_table)
|
|
|
|
local _, height = self.doc:getOriginalPageSize(pageno)
|
|
|
|
for _,text in pairs(text_table) do
|
|
|
|
for _,line in ipairs(text) do
|
|
|
|
line.y0, line.y1 = (height - line.y1), (height - line.y0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return text_table
|
|
|
|
end
|
2012-10-04 20:08:39 +00:00
|
|
|
|
2012-10-06 13:43:21 +00:00
|
|
|
function render_mode_string(rm)
|
|
|
|
if (rm == 0) then
|
|
|
|
return "COLOUR"
|
|
|
|
elseif (rm == 1) then
|
|
|
|
return "B&W"
|
|
|
|
elseif (rm == 2) then
|
|
|
|
return "COLOUR ONLY"
|
|
|
|
elseif (rm == 3) then
|
|
|
|
return "MASK ONLY"
|
|
|
|
elseif (rm == 4) then
|
|
|
|
return "COLOUR BG"
|
|
|
|
elseif (rm == 5) then
|
|
|
|
return "COLOUR FG"
|
|
|
|
else
|
|
|
|
return "UNKNOWN"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-04 20:08:39 +00:00
|
|
|
function DJVUReader:_drawReadingInfo()
|
|
|
|
local width, height = G_width, G_height
|
|
|
|
local numpages = self.doc:getPages()
|
|
|
|
local load_percent = self.pageno/numpages
|
|
|
|
local face = Font:getFace("rifont", 20)
|
2012-10-04 21:08:52 +00:00
|
|
|
local page_width, page_height, page_dpi, page_gamma, page_type = self.doc:getPageInfo(self.pageno)
|
2012-10-04 20:08:39 +00:00
|
|
|
|
2012-10-04 21:08:52 +00:00
|
|
|
-- display memory, time, battery and DjVu info on top of page
|
2012-10-04 20:08:39 +00:00
|
|
|
fb.bb:paintRect(0, 0, width, 40+6*2, 0)
|
|
|
|
renderUtf8Text(fb.bb, 10, 15+6, face,
|
|
|
|
"M: "..
|
|
|
|
math.ceil( self.cache_current_memsize / 1024 ).."/"..math.ceil( self.cache_max_memsize / 1024 ).."k, "..
|
2012-10-06 14:00:02 +00:00
|
|
|
math.ceil( self.doc:getCacheSize() / 1024 ).."/"..math.ceil( self.cache_document_size / 1024 ).."k", true)
|
|
|
|
local txt = os.date("%a %d %b %Y %T").." ["..BatteryLevel().."]"
|
2012-10-10 12:57:28 +00:00
|
|
|
local w = sizeUtf8Text(0, width, face, txt, true).x
|
2012-10-06 14:00:02 +00:00
|
|
|
renderUtf8Text(fb.bb, width - w - 10, 15+6, face, txt, true)
|
2012-10-04 20:08:39 +00:00
|
|
|
renderUtf8Text(fb.bb, 10, 15+6+22, face,
|
2012-10-04 21:08:52 +00:00
|
|
|
"Gm:"..string.format("%.1f",self.globalgamma).." ["..tostring(page_gamma).."], "..
|
2012-10-04 20:08:39 +00:00
|
|
|
tostring(page_width).."x"..tostring(page_height)..", "..
|
2012-10-09 08:41:18 +00:00
|
|
|
string.format("%.1fx, ", self.globalzoom)..
|
2012-10-06 13:43:21 +00:00
|
|
|
tostring(page_dpi).."dpi, "..page_type..", "..
|
|
|
|
render_mode_string(self.render_mode), true)
|
2012-10-04 20:08:39 +00:00
|
|
|
|
|
|
|
-- display reading progress on bottom of page
|
|
|
|
local ypos = height - 50
|
|
|
|
fb.bb:paintRect(0, ypos, width, 50, 0)
|
|
|
|
ypos = ypos + 15
|
|
|
|
local cur_section = self:getTocTitleOfCurrentPage()
|
|
|
|
if cur_section ~= "" then
|
|
|
|
cur_section = "Sec: "..cur_section
|
|
|
|
end
|
|
|
|
renderUtf8Text(fb.bb, 10, ypos+6, face,
|
|
|
|
"Page: "..self.pageno.."/"..numpages.." "..cur_section, true)
|
|
|
|
|
|
|
|
ypos = ypos + 15
|
|
|
|
blitbuffer.progressBar(fb.bb, 10, ypos, width-20, 15,
|
|
|
|
5, 4, load_percent, 8)
|
|
|
|
end
|