mirror of https://github.com/koreader/koreader
Merge remote-tracking branch 'hwhw/master' into djvu
commit
eba1c2d5ef
@ -1,70 +1,19 @@
|
|||||||
require "unireader"
|
require "unireader"
|
||||||
|
|
||||||
DJVUReader = UniReader:new{}
|
DJVUReader = UniReader:new{
|
||||||
|
newDC = function()
|
||||||
|
print("djvu.newDC")
|
||||||
|
return djvu.newDC()
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
function DJVUReader:init()
|
function DJVUReader:init()
|
||||||
self.nulldc = djvu.newDC()
|
self.nulldc = self.newDC()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- open a DJVU file and its settings store
|
-- open a DJVU file and its settings store
|
||||||
|
-- DJVU does not support password yet
|
||||||
function DJVUReader:open(filename)
|
function DJVUReader:open(filename)
|
||||||
self.doc = djvu.openDocument(filename)
|
self.doc = djvu.openDocument(filename)
|
||||||
if self.doc ~= nil then
|
return self:loadSettings(filename)
|
||||||
self.settings = DocSettings:open(filename)
|
|
||||||
local gamma = self.settings:readsetting("gamma")
|
|
||||||
if gamma then
|
|
||||||
self.globalgamma = gamma
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- set viewer state according to zoom state
|
|
||||||
function DJVUReader:setzoom(page)
|
|
||||||
local dc = djvu.newDC()
|
|
||||||
local pwidth, pheight = page:getSize(self.nulldc)
|
|
||||||
|
|
||||||
if self.globalzoommode == self.ZOOM_FIT_TO_PAGE then
|
|
||||||
self.globalzoom = width / pwidth
|
|
||||||
self.offset_x = 0
|
|
||||||
self.offset_y = (height - (self.globalzoom * pheight)) / 2
|
|
||||||
if height / pheight < self.globalzoom then
|
|
||||||
self.globalzoom = height / pheight
|
|
||||||
print(width, (self.globalzoom * pwidth))
|
|
||||||
self.offset_x = (width - (self.globalzoom * pwidth)) / 2
|
|
||||||
self.offset_y = 0
|
|
||||||
end
|
|
||||||
elseif self.globalzoommode == self.ZOOM_FIT_TO_PAGE_WIDTH then
|
|
||||||
self.globalzoom = width / pwidth
|
|
||||||
self.offset_x = 0
|
|
||||||
self.offset_y = (height - (self.globalzoom * pheight)) / 2
|
|
||||||
elseif self.globalzoommode == self.ZOOM_FIT_TO_PAGE_HEIGHT then
|
|
||||||
self.globalzoom = height / pheight
|
|
||||||
self.offset_x = (width - (self.globalzoom * pwidth)) / 2
|
|
||||||
self.offset_y = 0
|
|
||||||
end
|
|
||||||
|
|
||||||
dc:setZoom(self.globalzoom)
|
|
||||||
-- record globalzoom for manual zoom in/out
|
|
||||||
self.globalzoom_orig = self.globalzoom
|
|
||||||
|
|
||||||
dc:setRotate(self.globalrotate);
|
|
||||||
dc:setOffset(self.offset_x, self.offset_y)
|
|
||||||
self.fullwidth, self.fullheight = page:getSize(dc)
|
|
||||||
self.min_offset_x = fb.bb:getWidth() - self.fullwidth
|
|
||||||
self.min_offset_y = fb.bb:getHeight() - self.fullheight
|
|
||||||
if(self.min_offset_x > 0) then
|
|
||||||
self.min_offset_x = 0
|
|
||||||
end
|
|
||||||
if(self.min_offset_y > 0) then
|
|
||||||
self.min_offset_y = 0
|
|
||||||
end
|
|
||||||
|
|
||||||
-- set gamma here, we don't have any other good place for this right now:
|
|
||||||
if self.globalgamma ~= self.GAMMA_NO_GAMMA then
|
|
||||||
print("gamma correction: "..self.globalgamma)
|
|
||||||
dc:setGamma(self.globalgamma)
|
|
||||||
end
|
|
||||||
return dc
|
|
||||||
end
|
end
|
||||||
|
@ -1,38 +1,66 @@
|
|||||||
DocSettings = {}
|
DocSettings = {}
|
||||||
|
|
||||||
function DocSettings:open(docfile)
|
function DocSettings:open(docfile)
|
||||||
local new = {}
|
local new = { file = docfile..".kpdfview.lua", data = {} }
|
||||||
new.docdb, errno, errstr = sqlite3.open(docfile..".kpdfview")
|
local ok, stored = pcall(dofile,new.file)
|
||||||
if new.docdb ~= nil then
|
if ok then
|
||||||
new.docdb:exec("CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT);")
|
new.data = stored
|
||||||
new.stmt_readsetting = new.docdb:prepare("SELECT value FROM settings WHERE key = ?;")
|
|
||||||
new.stmt_savesetting = new.docdb:prepare("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?);")
|
|
||||||
end
|
end
|
||||||
return setmetatable(new, { __index = DocSettings})
|
return setmetatable(new, { __index = DocSettings})
|
||||||
end
|
end
|
||||||
|
|
||||||
function DocSettings:readsetting(key)
|
function DocSettings:readsetting(key)
|
||||||
if self.docdb ~= nil then
|
return self.data[key]
|
||||||
self.stmt_readsetting:reset()
|
end
|
||||||
self.stmt_readsetting:bind_values(key)
|
|
||||||
local result = self.stmt_readsetting:step()
|
function DocSettings:savesetting(key, value)
|
||||||
if result == sqlite3.ROW then
|
self.data[key] = value
|
||||||
return self.stmt_readsetting:get_value(0)
|
end
|
||||||
|
|
||||||
|
-- simple serialization function, won't do uservalues, functions, loops
|
||||||
|
function DocSettings:_serialize(what, outt, indent)
|
||||||
|
if type(what) == "table" then
|
||||||
|
local didrun = false
|
||||||
|
table.insert(outt, "{")
|
||||||
|
for k, v in pairs(what) do
|
||||||
|
if didrun then
|
||||||
|
table.insert(outt, ",")
|
||||||
|
end
|
||||||
|
table.insert(outt, "\n")
|
||||||
|
table.insert(outt, string.rep("\t", indent+1))
|
||||||
|
table.insert(outt, "[")
|
||||||
|
self:_serialize(k, outt, indent+1)
|
||||||
|
table.insert(outt, "] = ")
|
||||||
|
self:_serialize(v, outt, indent+1)
|
||||||
|
didrun = true
|
||||||
end
|
end
|
||||||
|
if didrun then
|
||||||
|
table.insert(outt, "\n")
|
||||||
|
table.insert(outt, string.rep("\t", indent))
|
||||||
|
end
|
||||||
|
table.insert(outt, "}")
|
||||||
|
elseif type(what) == "string" then
|
||||||
|
table.insert(outt, string.format("%q", what))
|
||||||
|
elseif type(what) == "number" or type(what) == "boolean" then
|
||||||
|
table.insert(outt, tostring(what))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function DocSettings:savesetting(key, value)
|
function DocSettings:flush()
|
||||||
if self.docdb ~= nil then
|
-- write a serialized version of the data table
|
||||||
self.stmt_savesetting:reset()
|
if not self.file then
|
||||||
self.stmt_savesetting:bind_values(key, value)
|
return
|
||||||
self.stmt_savesetting:step()
|
end
|
||||||
|
local f_out = io.open(self.file, "w")
|
||||||
|
if f_out ~= nil then
|
||||||
|
local out = {"-- we can read Lua syntax here!\nreturn "}
|
||||||
|
self:_serialize(self.data, out, 0)
|
||||||
|
table.insert(out, "\n")
|
||||||
|
f_out:write(table.concat(out))
|
||||||
|
f_out:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function DocSettings:close()
|
function DocSettings:close()
|
||||||
if self.docdb ~= nil then
|
self:flush()
|
||||||
self.docdb:close()
|
|
||||||
self.docdb = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue