2013-02-19 03:57:14 +00:00
|
|
|
require "../math"
|
|
|
|
|
2012-05-18 22:49:01 +00:00
|
|
|
--[[
|
|
|
|
This is a registry for document providers
|
|
|
|
]]--
|
|
|
|
DocumentRegistry = {
|
|
|
|
providers = { }
|
|
|
|
}
|
|
|
|
|
|
|
|
function DocumentRegistry:addProvider(extension, mimetype, provider)
|
|
|
|
table.insert(self.providers, { extension = extension, mimetype = mimetype, provider = provider })
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocumentRegistry:getProvider(file)
|
|
|
|
-- TODO: some implementation based on mime types?
|
2012-09-18 00:36:39 +00:00
|
|
|
local extension = string.lower(string.match(file, ".+%.([^.]+)") or "")
|
2012-05-18 22:49:01 +00:00
|
|
|
for _, provider in ipairs(self.providers) do
|
|
|
|
if extension == provider.extension then
|
2012-06-12 06:38:00 +00:00
|
|
|
return provider.provider
|
2012-05-18 22:49:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-12 06:52:35 +00:00
|
|
|
function DocumentRegistry:openDocument(file)
|
2013-07-09 06:17:06 +00:00
|
|
|
local provider = self:getProvider(file)
|
|
|
|
if provider ~= nil then
|
|
|
|
return provider:new{file = file}
|
|
|
|
end
|
2012-06-12 06:52:35 +00:00
|
|
|
end
|
|
|
|
|
2012-06-12 06:38:00 +00:00
|
|
|
|
2012-05-18 22:49:01 +00:00
|
|
|
--[[
|
|
|
|
This is an abstract interface to a document
|
|
|
|
]]--
|
|
|
|
Document = {
|
|
|
|
-- file name
|
|
|
|
file = nil,
|
|
|
|
|
|
|
|
info = {
|
|
|
|
-- whether the document is pageable
|
|
|
|
has_pages = false,
|
|
|
|
-- whether words can be provided
|
|
|
|
has_words = false,
|
|
|
|
-- whether hyperlinks can be provided
|
|
|
|
has_hyperlinks = false,
|
|
|
|
-- whether (native to format) annotations can be provided
|
|
|
|
has_annotations = false,
|
|
|
|
|
|
|
|
-- whether pages can be rotated
|
|
|
|
is_rotatable = false,
|
|
|
|
|
|
|
|
number_of_pages = 0,
|
|
|
|
-- if not pageable, length of the document in pixels
|
2012-06-12 09:42:02 +00:00
|
|
|
doc_height = 0,
|
2013-02-02 20:42:59 +00:00
|
|
|
|
2012-05-18 22:49:01 +00:00
|
|
|
-- other metadata
|
|
|
|
title = "",
|
|
|
|
author = "",
|
|
|
|
date = ""
|
|
|
|
},
|
2013-02-02 20:42:59 +00:00
|
|
|
|
2013-02-20 06:32:51 +00:00
|
|
|
GAMMA_NO_GAMMA = 1.0,
|
|
|
|
|
2013-02-02 20:42:59 +00:00
|
|
|
-- override bbox from orignal page's getUsedBBox
|
|
|
|
bbox = {},
|
|
|
|
|
2012-05-18 22:49:01 +00:00
|
|
|
-- flag to show whether the document was opened successfully
|
|
|
|
is_open = false,
|
|
|
|
error_message = nil,
|
|
|
|
|
|
|
|
-- flag to show that the document needs to be unlocked by a password
|
|
|
|
is_locked = false,
|
|
|
|
}
|
|
|
|
|
|
|
|
function Document:new(o)
|
|
|
|
local o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
if o.init then o:init() end
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2012-08-25 14:52:12 +00:00
|
|
|
-- override this method to open a document
|
|
|
|
function Document:init()
|
|
|
|
end
|
|
|
|
|
2012-05-18 22:49:01 +00:00
|
|
|
-- this might be overridden by a document implementation
|
|
|
|
function Document:unlock(password)
|
|
|
|
-- return true instead when the password provided unlocked the document
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- this might be overridden by a document implementation
|
|
|
|
function Document:close()
|
2012-06-03 14:26:16 +00:00
|
|
|
if self.is_open then
|
|
|
|
self.is_open = false
|
|
|
|
self._document:close()
|
|
|
|
end
|
2012-05-18 22:49:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- this might be overridden by a document implementation
|
|
|
|
function Document:getNativePageDimensions(pageno)
|
2012-06-03 14:26:16 +00:00
|
|
|
local hash = "pgdim|"..self.file.."|"..pageno
|
|
|
|
local cached = Cache:check(hash)
|
|
|
|
if cached then
|
|
|
|
return cached[1]
|
|
|
|
end
|
|
|
|
local page = self._document:openPage(pageno)
|
|
|
|
local page_size_w, page_size_h = page:getSize(self.dc_null)
|
|
|
|
local page_size = Geom:new{ w = page_size_w, h = page_size_h }
|
|
|
|
Cache:insert(hash, CacheItem:new{ page_size })
|
|
|
|
page:close()
|
|
|
|
return page_size
|
|
|
|
end
|
|
|
|
|
|
|
|
function Document:_readMetadata()
|
2013-01-09 08:41:42 +00:00
|
|
|
self.info.number_of_pages = self._document:getPages()
|
2012-06-03 14:26:16 +00:00
|
|
|
return true
|
2012-05-18 22:49:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- calculates page dimensions
|
|
|
|
function Document:getPageDimensions(pageno, zoom, rotation)
|
2012-06-10 15:36:19 +00:00
|
|
|
local native_dimen = self:getNativePageDimensions(pageno):copy()
|
2012-05-18 22:49:01 +00:00
|
|
|
if rotation == 90 or rotation == 270 then
|
|
|
|
-- switch orientation
|
|
|
|
native_dimen.w, native_dimen.h = native_dimen.h, native_dimen.w
|
|
|
|
end
|
|
|
|
native_dimen:scaleBy(zoom)
|
2013-02-19 05:28:04 +00:00
|
|
|
--DEBUG("dimen for pageno", pageno, "zoom", zoom, "rotation", rotation, "is", native_dimen)
|
2012-05-18 22:49:01 +00:00
|
|
|
return native_dimen
|
|
|
|
end
|
|
|
|
|
2013-02-02 20:42:59 +00:00
|
|
|
function Document:getPageBBox(pageno)
|
|
|
|
local bbox = self.bbox[pageno] -- exact
|
|
|
|
if bbox ~= nil then
|
2013-02-19 05:28:04 +00:00
|
|
|
--DEBUG("bbox from", pageno)
|
2013-03-10 05:15:00 +00:00
|
|
|
return bbox
|
2013-02-02 20:42:59 +00:00
|
|
|
else
|
2013-03-10 05:15:00 +00:00
|
|
|
local oddEven = math.oddEven(pageno)
|
2013-02-02 20:42:59 +00:00
|
|
|
bbox = self.bbox[oddEven] -- odd/even
|
|
|
|
end
|
|
|
|
if bbox ~= nil then -- last used up to this page
|
2013-02-19 05:28:04 +00:00
|
|
|
--DEBUG("bbox from", oddEven)
|
2013-03-10 05:15:00 +00:00
|
|
|
return bbox
|
2013-02-02 20:42:59 +00:00
|
|
|
else
|
|
|
|
for i = 0,pageno do
|
|
|
|
bbox = self.bbox[ pageno - i ]
|
|
|
|
if bbox ~= nil then
|
2013-02-19 05:28:04 +00:00
|
|
|
--DEBUG("bbox from", pageno - i)
|
2013-03-10 05:15:00 +00:00
|
|
|
return bbox
|
2013-02-02 20:42:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if bbox == nil then -- fallback bbox
|
|
|
|
bbox = self:getUsedBBox(pageno)
|
2013-02-19 05:28:04 +00:00
|
|
|
--DEBUG("bbox from ORIGINAL page")
|
2013-02-02 20:42:59 +00:00
|
|
|
end
|
2013-02-19 05:28:04 +00:00
|
|
|
--DEBUG("final bbox", bbox)
|
2013-02-02 20:42:59 +00:00
|
|
|
return bbox
|
|
|
|
end
|
|
|
|
|
2012-12-07 21:42:27 +00:00
|
|
|
--[[
|
|
|
|
This method returns pagesize if bbox is corrupted
|
|
|
|
--]]
|
2012-12-02 09:09:32 +00:00
|
|
|
function Document:getUsedBBoxDimensions(pageno, zoom, rotation)
|
2013-02-02 20:42:59 +00:00
|
|
|
local bbox = self:getPageBBox(pageno)
|
|
|
|
local ubbox_dimen = nil
|
2013-04-03 05:03:55 +00:00
|
|
|
if (bbox.x0 < 0) or (bbox.y0 < 0) or (bbox.x1 < 0) or (bbox.y1 < 0)
|
|
|
|
or (bbox.x0 == bbox.x1) or (bbox.y0 == bbox.y1) then
|
2012-12-07 21:42:27 +00:00
|
|
|
-- if document's bbox info is corrupted, we use the page size
|
|
|
|
ubbox_dimen = self:getPageDimensions(pageno, zoom, rotation)
|
|
|
|
else
|
|
|
|
ubbox_dimen = Geom:new{
|
2013-02-02 20:42:59 +00:00
|
|
|
x = bbox.x0,
|
|
|
|
y = bbox.y0,
|
|
|
|
w = bbox.x1 - bbox.x0,
|
|
|
|
h = bbox.y1 - bbox.y0,
|
2012-12-07 21:42:27 +00:00
|
|
|
}
|
|
|
|
if zoom ~= 1 then
|
|
|
|
ubbox_dimen:transformByScale(zoom)
|
|
|
|
end
|
2012-12-02 09:09:32 +00:00
|
|
|
end
|
|
|
|
return ubbox_dimen
|
|
|
|
end
|
|
|
|
|
2012-05-27 11:47:22 +00:00
|
|
|
function Document:getToc()
|
|
|
|
return self._document:getToc()
|
|
|
|
end
|
|
|
|
|
2013-05-01 15:43:53 +00:00
|
|
|
function Document:getTextBoxes(pageno)
|
2013-04-23 22:59:52 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function Document:getOCRWord(pageno, rect)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2013-02-20 06:32:51 +00:00
|
|
|
function Document:renderPage(pageno, rect, zoom, rotation, gamma, render_mode)
|
|
|
|
local hash = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..gamma.."|"..render_mode
|
2012-06-03 14:26:16 +00:00
|
|
|
local page_size = self:getPageDimensions(pageno, zoom, rotation)
|
|
|
|
-- this will be the size we actually render
|
|
|
|
local size = page_size
|
|
|
|
-- we prefer to render the full page, if it fits into cache
|
|
|
|
if not Cache:willAccept(size.w * size.h / 2) then
|
|
|
|
-- whole page won't fit into cache
|
|
|
|
DEBUG("rendering only part of the page")
|
|
|
|
-- TODO: figure out how to better segment the page
|
|
|
|
if not rect then
|
|
|
|
DEBUG("aborting, since we do not have a specification for that part")
|
|
|
|
-- required part not given, so abort
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- only render required part
|
2013-02-20 06:32:51 +00:00
|
|
|
hash = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..gamma.."|"..render_mode.."|"..tostring(rect)
|
2012-06-03 14:26:16 +00:00
|
|
|
size = rect
|
|
|
|
end
|
|
|
|
|
|
|
|
-- prepare cache item with contained blitbuffer
|
|
|
|
local tile = CacheItem:new{
|
|
|
|
size = size.w * size.h / 2 + 64, -- estimation
|
|
|
|
excerpt = size,
|
|
|
|
pageno = pageno,
|
|
|
|
bb = Blitbuffer.new(size.w, size.h)
|
|
|
|
}
|
|
|
|
|
|
|
|
-- create a draw context
|
|
|
|
local dc = DrawContext.new()
|
|
|
|
|
|
|
|
dc:setRotate(rotation)
|
|
|
|
-- correction of rotation
|
|
|
|
if rotation == 90 then
|
|
|
|
dc:setOffset(page_size.w, 0)
|
|
|
|
elseif rotation == 180 then
|
|
|
|
dc:setOffset(page_size.w, page_size.h)
|
|
|
|
elseif rotation == 270 then
|
|
|
|
dc:setOffset(0, page_size.h)
|
|
|
|
end
|
|
|
|
dc:setZoom(zoom)
|
2013-02-20 06:32:51 +00:00
|
|
|
|
|
|
|
if gamma ~= self.GAMMA_NO_GAMMA then
|
|
|
|
--DEBUG("gamma correction: ", gamma)
|
|
|
|
dc:setGamma(gamma)
|
|
|
|
end
|
2012-06-03 14:26:16 +00:00
|
|
|
|
|
|
|
-- render
|
|
|
|
local page = self._document:openPage(pageno)
|
2012-10-09 22:26:01 +00:00
|
|
|
page:draw(dc, tile.bb, size.x, size.y, render_mode)
|
2012-06-03 14:26:16 +00:00
|
|
|
page:close()
|
|
|
|
Cache:insert(hash, tile)
|
|
|
|
|
|
|
|
return tile
|
|
|
|
end
|
|
|
|
|
|
|
|
-- a hint for the cache engine to paint a full page to the cache
|
|
|
|
-- TODO: this should trigger a background operation
|
2013-02-20 06:32:51 +00:00
|
|
|
function Document:hintPage(pageno, zoom, rotation, gamma, render_mode)
|
|
|
|
local hash_full_page = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..gamma.."|"..render_mode
|
2013-01-09 03:38:49 +00:00
|
|
|
if not Cache:check(hash_full_page) then
|
2013-02-25 13:29:41 +00:00
|
|
|
DEBUG("hinting page", pageno)
|
2013-02-20 06:32:51 +00:00
|
|
|
self:renderPage(pageno, nil, zoom, rotation, gamma, render_mode)
|
2013-01-08 08:54:34 +00:00
|
|
|
end
|
2012-06-03 14:26:16 +00:00
|
|
|
end
|
|
|
|
|
2012-12-02 09:09:32 +00:00
|
|
|
--[[
|
|
|
|
Draw page content to blitbuffer.
|
|
|
|
1. find tile in cache
|
|
|
|
2. if not found, call renderPage
|
|
|
|
|
|
|
|
@target: target blitbuffer
|
|
|
|
@rect: visible_area inside document page
|
|
|
|
--]]
|
2013-02-20 06:32:51 +00:00
|
|
|
function Document:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, render_mode)
|
|
|
|
local hash_full_page = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..gamma.."|"..render_mode
|
2013-01-09 03:38:49 +00:00
|
|
|
local hash_excerpt = hash_full_page.."|"..tostring(rect)
|
2012-06-03 14:26:16 +00:00
|
|
|
local tile = Cache:check(hash_full_page)
|
|
|
|
if not tile then
|
|
|
|
tile = Cache:check(hash_excerpt)
|
|
|
|
if not tile then
|
|
|
|
DEBUG("rendering")
|
2013-02-20 06:32:51 +00:00
|
|
|
tile = self:renderPage(pageno, rect, zoom, rotation, gamma, render_mode)
|
2012-06-03 14:26:16 +00:00
|
|
|
end
|
|
|
|
end
|
2012-12-02 09:09:32 +00:00
|
|
|
DEBUG("now painting", tile, rect)
|
|
|
|
target:blitFrom(tile.bb,
|
|
|
|
x, y,
|
|
|
|
rect.x - tile.excerpt.x,
|
|
|
|
rect.y - tile.excerpt.y,
|
|
|
|
rect.w, rect.h)
|
2012-06-03 14:26:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Document:getPageText(pageno)
|
|
|
|
-- is this worth caching? not done yet.
|
|
|
|
local page = self._document:openPage(pageno)
|
|
|
|
local text = page:getPageText()
|
|
|
|
page:close()
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-05-18 23:13:53 +00:00
|
|
|
-- load implementations:
|
2012-05-18 22:49:01 +00:00
|
|
|
|
2012-05-18 23:13:53 +00:00
|
|
|
require "document/pdfdocument"
|
2012-06-03 02:59:35 +00:00
|
|
|
require "document/djvudocument"
|
2012-06-05 07:23:36 +00:00
|
|
|
require "document/credocument"
|