2012-05-18 23:13:53 +00:00
|
|
|
require "cache"
|
|
|
|
require "ui/geometry"
|
2013-01-03 14:24:38 +00:00
|
|
|
require "ui/screen"
|
|
|
|
require "ui/reader/readerconfig"
|
|
|
|
require "document/koptinterface"
|
2012-05-18 23:13:53 +00:00
|
|
|
|
|
|
|
PdfDocument = Document:new{
|
|
|
|
_document = false,
|
|
|
|
-- muPDF manages its own additional cache
|
|
|
|
mupdf_cache_size = 5 * 1024 * 1024,
|
2013-01-03 14:24:38 +00:00
|
|
|
dc_null = DrawContext.new(),
|
|
|
|
screen_size = Screen:getSize(),
|
2013-02-02 21:16:19 +00:00
|
|
|
screen_dpi = Screen:getDPI(),
|
2013-01-06 05:53:36 +00:00
|
|
|
options = KoptOptions,
|
2013-01-03 14:24:38 +00:00
|
|
|
configurable = Configurable,
|
|
|
|
koptinterface = KoptInterface,
|
2012-05-18 23:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function PdfDocument:init()
|
2013-01-06 05:53:36 +00:00
|
|
|
self.configurable:loadDefaults(self.options)
|
2012-05-18 23:13:53 +00:00
|
|
|
local ok
|
|
|
|
ok, self._document = pcall(pdf.openDocument, self.file, self.mupdf_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-05-18 23:13:53 +00:00
|
|
|
if self._document:needsPassword() then
|
|
|
|
self.is_locked = true
|
|
|
|
else
|
|
|
|
self:_readMetadata()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function PdfDocument:unlock(password)
|
|
|
|
if not self._document:authenticatePassword(password) then
|
|
|
|
self._document:close()
|
|
|
|
return false, "wrong password"
|
|
|
|
end
|
|
|
|
self.is_locked = false
|
|
|
|
return self:_readMetadata()
|
|
|
|
end
|
|
|
|
|
|
|
|
function PdfDocument:getUsedBBox(pageno)
|
|
|
|
local hash = "pgubbox|"..self.file.."|"..pageno
|
|
|
|
local cached = Cache:check(hash)
|
|
|
|
if cached then
|
2012-12-02 07:14:02 +00:00
|
|
|
return cached.ubbox
|
2012-05-18 23:13:53 +00:00
|
|
|
end
|
|
|
|
local page = self._document:openPage(pageno)
|
|
|
|
local used = {}
|
2012-12-02 09:09:32 +00:00
|
|
|
used.x0, used.y0, used.x1, used.y1 = page:getUsedBBox()
|
2013-03-05 08:43:19 +00:00
|
|
|
local pwidth, pheight = page:getSize(self.dc_null)
|
|
|
|
-- clamp to page BBox
|
|
|
|
if used.x0 < 0 then used.x0 = 0 end
|
|
|
|
if used.x1 > pwidth then used.x1 = pwidth end
|
|
|
|
if used.y0 < 0 then used.y0 = 0 end
|
|
|
|
if used.y1 > pheight then used.y1 = pheight end
|
2012-12-02 07:14:02 +00:00
|
|
|
--@TODO give size for cacheitem? 02.12 2012 (houqp)
|
|
|
|
Cache:insert(hash, CacheItem:new{
|
|
|
|
ubbox = used,
|
|
|
|
})
|
2012-05-18 23:13:53 +00:00
|
|
|
page:close()
|
|
|
|
return used
|
|
|
|
end
|
|
|
|
|
2013-01-03 14:24:38 +00:00
|
|
|
function PdfDocument:getPageDimensions(pageno, zoom, rotation)
|
|
|
|
if self.configurable.text_wrap == 1 then
|
|
|
|
return self.koptinterface:getPageDimensions(self, pageno, zoom, rotation)
|
|
|
|
else
|
|
|
|
return Document.getPageDimensions(self, pageno, zoom, rotation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-20 06:32:51 +00:00
|
|
|
function PdfDocument:renderPage(pageno, rect, zoom, rotation, gamma, render_mode)
|
2013-01-03 14:24:38 +00:00
|
|
|
if self.configurable.text_wrap == 1 then
|
|
|
|
return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, render_mode)
|
|
|
|
else
|
2013-02-20 06:32:51 +00:00
|
|
|
return Document.renderPage(self, pageno, rect, zoom, rotation, gamma, render_mode)
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-20 06:32:51 +00:00
|
|
|
function PdfDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, render_mode)
|
2013-01-03 14:24:38 +00:00
|
|
|
if self.configurable.text_wrap == 1 then
|
|
|
|
self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, render_mode)
|
|
|
|
else
|
2013-02-20 06:32:51 +00:00
|
|
|
Document.drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma, render_mode)
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-18 23:13:53 +00:00
|
|
|
DocumentRegistry:addProvider("pdf", "application/pdf", PdfDocument)
|