2013-10-18 20:38:07 +00:00
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local Cache = require("cache")
|
|
|
|
local CacheItem = require("cacheitem")
|
|
|
|
local KoptOptions = require("ui/data/koptoptions")
|
|
|
|
local KoptInterface = require("document/koptinterface")
|
|
|
|
local Document = require("document/document")
|
|
|
|
local Configurable = require("ui/reader/configurable")
|
|
|
|
-- TBD: DrawContext
|
|
|
|
|
|
|
|
local DjvuDocument = Document:new{
|
2012-06-03 02:59:35 +00:00
|
|
|
_document = false,
|
|
|
|
-- libdjvulibre manages its own additional cache, default value is hard written in c module.
|
|
|
|
djvulibre_cache_size = nil,
|
2013-01-03 14:24:38 +00:00
|
|
|
dc_null = DrawContext.new(),
|
2013-01-06 05:53:36 +00:00
|
|
|
options = KoptOptions,
|
2013-01-03 14:24:38 +00:00
|
|
|
koptinterface = KoptInterface,
|
2012-06-03 02:59:35 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
-- check DjVu magic string to validate
|
|
|
|
local 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-06-03 02:59:35 +00:00
|
|
|
function DjvuDocument:init()
|
2013-01-06 05:53:36 +00:00
|
|
|
self.configurable:loadDefaults(self.options)
|
2012-10-09 20:15:13 +00:00
|
|
|
if not validDjvuFile(self.file) then
|
|
|
|
self.error_message = "Not a valid DjVu file"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-06-03 02:59:35 +00:00
|
|
|
local ok
|
|
|
|
ok, self._document = pcall(djvu.openDocument, self.file, self.djvulibre_cache_size)
|
|
|
|
if not ok then
|
|
|
|
self.error_message = self.doc -- will contain error message
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.is_open = true
|
|
|
|
self.info.has_pages = true
|
2013-01-03 14:24:38 +00:00
|
|
|
self.info.configurable = true
|
2012-06-03 02:59:35 +00:00
|
|
|
self:_readMetadata()
|
|
|
|
end
|
|
|
|
|
2013-10-12 15:07:13 +00:00
|
|
|
function DjvuDocument: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)
|
2013-05-01 15:43:53 +00:00
|
|
|
end
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
2013-10-12 15:07:13 +00:00
|
|
|
return text_table
|
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:getPageTextBoxes(pageno)
|
|
|
|
return self._document:getPageText(pageno)
|
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:getWordFromPosition(spos)
|
|
|
|
return self.koptinterface:getWordFromPosition(self, spos)
|
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:getTextFromPositions(spos0, spos1)
|
|
|
|
return self.koptinterface:getTextFromPositions(self, spos0, spos1)
|
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:getPageBoxesFromPositions(pageno, ppos0, ppos1)
|
|
|
|
return self.koptinterface:getPageBoxesFromPositions(self, pageno, ppos0, ppos1)
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2013-10-14 15:05:58 +00:00
|
|
|
function DjvuDocument:getOCRWord(pageno, wbox)
|
|
|
|
return self.koptinterface:getOCRWord(self, pageno, wbox)
|
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:getOCRText(pageno, tboxes)
|
|
|
|
return self.koptinterface:getOCRText(self, pageno, tboxes)
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2012-06-03 02:59:35 +00:00
|
|
|
function DjvuDocument:getUsedBBox(pageno)
|
2012-06-03 14:26:16 +00:00
|
|
|
-- djvu does not support usedbbox, so fake it.
|
2012-06-03 02:59:35 +00:00
|
|
|
local used = {}
|
2013-01-03 14:24:38 +00:00
|
|
|
local native_dim = self:getNativePageDimensions(pageno)
|
|
|
|
used.x0, used.y0, used.x1, used.y1 = 0, 0, native_dim.w, native_dim.h
|
2012-06-03 02:59:35 +00:00
|
|
|
return used
|
|
|
|
end
|
|
|
|
|
2013-04-14 07:16:42 +00:00
|
|
|
function DjvuDocument:getPageBBox(pageno)
|
2013-10-12 15:07:13 +00:00
|
|
|
return self.koptinterface:getPageBBox(self, pageno)
|
2013-04-14 07:16:42 +00:00
|
|
|
end
|
|
|
|
|
2013-01-03 14:24:38 +00:00
|
|
|
function DjvuDocument:getPageDimensions(pageno, zoom, rotation)
|
2013-10-12 15:07:13 +00:00
|
|
|
return self.koptinterface:getPageDimensions(self, pageno, zoom, rotation)
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
|
|
|
|
2013-02-20 06:32:51 +00:00
|
|
|
function DjvuDocument:renderPage(pageno, rect, zoom, rotation, gamma, render_mode)
|
2013-10-12 15:07:13 +00:00
|
|
|
return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, render_mode)
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
|
|
|
|
2013-03-12 12:26:02 +00:00
|
|
|
function DjvuDocument:hintPage(pageno, zoom, rotation, gamma, render_mode)
|
2013-10-12 15:07:13 +00:00
|
|
|
return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma, render_mode)
|
2013-03-12 12:26:02 +00:00
|
|
|
end
|
|
|
|
|
2013-02-20 06:32:51 +00:00
|
|
|
function DjvuDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, render_mode)
|
2013-10-12 15:07:13 +00:00
|
|
|
return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma, render_mode)
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
function DjvuDocument:register(registry)
|
|
|
|
registry:addProvider("djvu", "application/djvu", self)
|
|
|
|
end
|
|
|
|
|
|
|
|
return DjvuDocument
|