2013-10-18 20:38:07 +00:00
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local CreOptions = require("ui/data/creoptions")
|
|
|
|
local Document = require("document/document")
|
|
|
|
local Configurable = require("ui/reader/configurable")
|
2013-10-22 15:11:31 +00:00
|
|
|
local Font = require("ui/font")
|
|
|
|
local Screen = require("ui/screen")
|
2013-10-22 18:51:29 +00:00
|
|
|
local DEBUG = require("dbg")
|
2013-10-18 20:38:07 +00:00
|
|
|
-- TBD: DrawContext
|
2012-06-05 07:23:36 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local CreDocument = Document:new{
|
2012-12-21 01:10:16 +00:00
|
|
|
-- this is defined in kpvcrlib/crengine/crengine/include/lvdocview.h
|
|
|
|
SCROLL_VIEW_MODE = 0,
|
|
|
|
PAGE_VIEW_MODE = 1,
|
|
|
|
|
2012-06-05 07:23:36 +00:00
|
|
|
_document = false,
|
2012-06-12 06:52:35 +00:00
|
|
|
engine_initilized = false,
|
|
|
|
|
2012-06-05 07:23:36 +00:00
|
|
|
line_space_percent = 100,
|
2013-10-16 15:21:20 +00:00
|
|
|
default_font = "FreeSerif",
|
|
|
|
header_font = "FreeSans",
|
|
|
|
fallback_font = "Droid Sans Fallback",
|
2013-01-17 22:12:20 +00:00
|
|
|
default_css = "./data/cr3.css",
|
2013-01-07 04:21:11 +00:00
|
|
|
options = CreOptions,
|
2012-06-05 07:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-- NuPogodi, 20.05.12: inspect the zipfile content
|
2013-10-22 15:11:31 +00:00
|
|
|
function CreDocument.zipContentExt(self, fname)
|
2012-06-05 07:23:36 +00:00
|
|
|
local outfile = "./data/zip_content"
|
|
|
|
local s = ""
|
|
|
|
os.execute("unzip ".."-l \""..fname.."\" > "..outfile)
|
|
|
|
local i = 1
|
|
|
|
if io.open(outfile,"r") then
|
|
|
|
for lines in io.lines(outfile) do
|
|
|
|
if i == 4 then s = lines break else i = i + 1 end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- return the extention
|
|
|
|
return string.lower(string.match(s, ".+%.([^.]+)"))
|
|
|
|
end
|
|
|
|
|
2012-06-12 06:52:35 +00:00
|
|
|
function CreDocument:engineInit()
|
|
|
|
if not engine_initilized then
|
2012-09-18 00:12:33 +00:00
|
|
|
-- initialize cache
|
|
|
|
cre.initCache(1024*1024*64)
|
2013-03-13 02:43:15 +00:00
|
|
|
|
2012-06-12 06:52:35 +00:00
|
|
|
-- we need to initialize the CRE font list
|
|
|
|
local fonts = Font:getFontList()
|
|
|
|
for _k, _v in ipairs(fonts) do
|
2012-10-03 17:08:27 +00:00
|
|
|
if _v ~= "Dingbats.cff" and _v ~= "StandardSymL.cff" then
|
|
|
|
local ok, err = pcall(cre.registerFont, Font.fontdir..'/'.._v)
|
|
|
|
if not ok then
|
2012-11-23 06:10:34 +00:00
|
|
|
DEBUG(err)
|
2012-10-03 17:08:27 +00:00
|
|
|
end
|
2012-06-12 06:52:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local default_font = G_reader_settings:readSetting("cre_font")
|
|
|
|
if default_font then
|
|
|
|
self.default_font = default_font
|
2012-06-05 07:23:36 +00:00
|
|
|
end
|
2012-06-12 06:52:35 +00:00
|
|
|
|
2013-01-13 03:23:30 +00:00
|
|
|
local header_font = G_reader_settings:readSetting("header_font")
|
|
|
|
if header_font then
|
|
|
|
self.header_font = header_font
|
|
|
|
end
|
|
|
|
|
2012-06-12 06:52:35 +00:00
|
|
|
engine_initilized = true
|
2012-06-05 07:23:36 +00:00
|
|
|
end
|
2012-06-12 06:52:35 +00:00
|
|
|
end
|
2012-06-05 07:23:36 +00:00
|
|
|
|
2012-06-12 06:52:35 +00:00
|
|
|
function CreDocument:init()
|
|
|
|
self:engineInit()
|
2013-01-07 04:21:11 +00:00
|
|
|
self.configurable:loadDefaults(self.options)
|
2012-06-05 07:23:36 +00:00
|
|
|
|
|
|
|
local ok
|
|
|
|
local file_type = string.lower(string.match(self.file, ".+%.([^.]+)"))
|
|
|
|
if file_type == "zip" then
|
|
|
|
-- NuPogodi, 20.05.12: read the content of zip-file
|
|
|
|
-- and return extention of the 1st file
|
2013-01-13 04:17:43 +00:00
|
|
|
file_type = self:zipContentExt(self.file)
|
2012-06-05 07:23:36 +00:00
|
|
|
end
|
|
|
|
-- these two format use the same css file
|
|
|
|
if file_type == "html" then
|
|
|
|
file_type = "htm"
|
|
|
|
end
|
|
|
|
-- if native css-file doesn't exist, one needs to use default cr3.css
|
|
|
|
if not io.open("./data/"..file_type..".css") then
|
|
|
|
file_type = "cr3"
|
|
|
|
end
|
2013-01-17 22:12:20 +00:00
|
|
|
self.default_css = "./data/"..file_type..".css"
|
2012-06-05 07:23:36 +00:00
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
-- @TODO check the default view_mode to a global user configurable
|
|
|
|
-- variable 22.12 2012 (houqp)
|
2013-01-21 22:27:22 +00:00
|
|
|
ok, self._document = pcall(cre.newDocView,
|
|
|
|
Screen:getWidth(), Screen:getHeight(), self.PAGE_VIEW_MODE
|
|
|
|
)
|
2012-06-05 07:23:36 +00:00
|
|
|
if not ok then
|
|
|
|
self.error_message = self.doc -- will contain error message
|
|
|
|
return
|
|
|
|
end
|
2013-10-16 15:21:20 +00:00
|
|
|
|
|
|
|
-- adjust font sizes according to screen dpi
|
|
|
|
self._document:adjustFontSizes(Screen:getDPI())
|
|
|
|
|
|
|
|
-- set fallback font face
|
|
|
|
self._document:setStringProperty("crengine.font.fallback.face", self.fallback_font)
|
|
|
|
|
2012-06-05 07:23:36 +00:00
|
|
|
self.is_open = true
|
|
|
|
self.info.has_pages = false
|
|
|
|
self:_readMetadata()
|
2013-01-07 04:21:11 +00:00
|
|
|
self.info.configurable = true
|
2012-06-05 07:23:36 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 02:22:33 +00:00
|
|
|
function CreDocument:loadDocument()
|
|
|
|
self._document:loadDocument(self.file)
|
2013-04-20 21:54:20 +00:00
|
|
|
if not self.info.has_pages then
|
|
|
|
self.info.doc_height = self._document:getFullHeight()
|
|
|
|
end
|
2013-01-13 02:22:33 +00:00
|
|
|
end
|
|
|
|
|
2013-10-16 15:21:20 +00:00
|
|
|
function CreDocument:close()
|
|
|
|
self._document:saveDefaults()
|
|
|
|
Document.close(self)
|
|
|
|
end
|
|
|
|
|
2013-10-17 15:53:29 +00:00
|
|
|
function CreDocument:getPageCount()
|
|
|
|
return self._document:getPages()
|
|
|
|
end
|
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
function CreDocument:drawCurrentView(target, x, y, rect, pos)
|
|
|
|
tile_bb = Blitbuffer.new(rect.w, rect.h)
|
|
|
|
self._document:drawCurrentPage(tile_bb)
|
|
|
|
target:blitFrom(tile_bb, x, y, 0, 0, rect.w, rect.h)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:drawCurrentViewByPos(target, x, y, rect, pos)
|
|
|
|
self._document:gotoPos(pos)
|
|
|
|
self:drawCurrentView(target, x, y, rect)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:drawCurrentViewByPage(target, x, y, rect, page)
|
|
|
|
self._document:gotoPage(page)
|
|
|
|
self:drawCurrentView(target, x, y, rect)
|
|
|
|
end
|
|
|
|
|
2012-06-05 07:23:36 +00:00
|
|
|
function CreDocument:hintPage(pageno, zoom, rotation)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:drawPage(target, x, y, rect, pageno, zoom, rotation)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:renderPage(pageno, rect, zoom, rotation)
|
|
|
|
end
|
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
function CreDocument:gotoXPointer(xpointer)
|
|
|
|
self._document:gotoXPointer(xpointer)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getXPointer()
|
|
|
|
return self._document:getXPointer()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPosFromXPointer(xp)
|
|
|
|
return self._document:getPosFromXPointer(xp)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageFromXPointer(xp)
|
|
|
|
return self._document:getPageFromXPointer(xp)
|
|
|
|
end
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function CreDocument:getFontFace()
|
|
|
|
return self._document:getFontFace()
|
|
|
|
end
|
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
function CreDocument:getCurrentPos()
|
|
|
|
return self._document:getCurrentPos()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Document:gotoPos(pos)
|
|
|
|
self._document:gotoPos(pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:gotoPage(page)
|
|
|
|
self._document:gotoPage(page)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getCurrentPage()
|
|
|
|
return self._document:getCurrentPage()
|
|
|
|
end
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function CreDocument:setFontFace(new_font_face)
|
2012-12-20 22:00:51 +00:00
|
|
|
if new_font_face then
|
2012-06-12 09:42:02 +00:00
|
|
|
self._document:setFontFace(new_font_face)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function CreDocument:getFontSize()
|
|
|
|
return self._document:getFontSize()
|
|
|
|
end
|
|
|
|
|
2012-12-17 06:50:50 +00:00
|
|
|
function CreDocument:setFontSize(new_font_size)
|
2012-12-20 22:00:51 +00:00
|
|
|
if new_font_size then
|
2012-12-17 06:50:50 +00:00
|
|
|
self._document:setFontSize(new_font_size)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-07 11:53:35 +00:00
|
|
|
function CreDocument:setViewMode(new_mode)
|
|
|
|
if new_mode then
|
|
|
|
if new_mode == "scroll" then
|
|
|
|
self._document:setViewMode(self.SCROLL_VIEW_MODE)
|
|
|
|
else
|
|
|
|
self._document:setViewMode(self.PAGE_VIEW_MODE)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-13 03:23:30 +00:00
|
|
|
function CreDocument:setHeaderFont(new_font)
|
|
|
|
if new_font then
|
|
|
|
self._document:setHeaderFont(new_font)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function CreDocument:zoomFont(delta)
|
|
|
|
self._document:zoomFont(delta)
|
|
|
|
end
|
2012-06-11 15:50:11 +00:00
|
|
|
|
2012-06-12 13:12:04 +00:00
|
|
|
function CreDocument:setInterlineSpacePercent(percent)
|
|
|
|
self._document:setDefaultInterlineSpace(percent)
|
|
|
|
end
|
|
|
|
|
2013-01-07 17:16:17 +00:00
|
|
|
function CreDocument:toggleFontBolder()
|
|
|
|
self._document:toggleFontBolder()
|
|
|
|
end
|
|
|
|
|
2013-01-13 03:49:01 +00:00
|
|
|
function CreDocument:setGammaIndex(index)
|
|
|
|
cre.setGammaIndex(index)
|
|
|
|
end
|
|
|
|
|
2013-01-17 21:59:40 +00:00
|
|
|
function CreDocument:setStyleSheet(new_css)
|
2013-01-21 22:27:22 +00:00
|
|
|
self._document:setStyleSheet(new_css)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setEmbeddedStyleSheet(toggle)
|
|
|
|
self._document:setEmbeddedStyleSheet(toggle)
|
2013-01-17 21:59:40 +00:00
|
|
|
end
|
|
|
|
|
2013-06-28 09:12:04 +00:00
|
|
|
function CreDocument:setPageMargins(left, top, right, bottom)
|
|
|
|
self._document:setPageMargins(left, top, right, bottom)
|
2013-10-16 15:21:20 +00:00
|
|
|
self._document:setIntProperty("crengine.page.margin.left", left)
|
|
|
|
self._document:setIntProperty("crengine.page.margin.top", top)
|
|
|
|
self._document:setIntProperty("crengine.page.margin.right", right)
|
|
|
|
self._document:setIntProperty("crengine.page.margin.bottom", bottom)
|
2013-06-28 09:12:04 +00:00
|
|
|
end
|
|
|
|
|
2013-07-28 06:35:46 +00:00
|
|
|
function CreDocument:setVisiblePageCount(new_count)
|
|
|
|
self._document:setVisiblePageCount(new_count)
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
function CreDocument:register(registry)
|
|
|
|
registry:addProvider("txt", "application/txt", self)
|
|
|
|
registry:addProvider("epub", "application/epub", self)
|
|
|
|
registry:addProvider("html", "application/html", self)
|
|
|
|
registry:addProvider("htm", "application/htm", self)
|
|
|
|
registry:addProvider("zip", "application/zip", self)
|
|
|
|
registry:addProvider("rtf", "application/rtf", self)
|
|
|
|
registry:addProvider("mobi", "application/mobi", self)
|
|
|
|
registry:addProvider("prc", "application/prc", self)
|
|
|
|
registry:addProvider("azw", "application/azw", self)
|
|
|
|
registry:addProvider("chm", "application/chm", self)
|
|
|
|
registry:addProvider("pdb", "application/pdb", self)
|
|
|
|
registry:addProvider("doc", "application/doc", self)
|
|
|
|
registry:addProvider("tcr", "application/tcr", self)
|
|
|
|
end
|
|
|
|
|
|
|
|
return CreDocument
|