mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Merge pull request #867 from chrox/fix_863
register opened document in documentregistry
This commit is contained in:
@@ -17,6 +17,7 @@ local CreDocument = Document:new{
|
||||
PAGE_VIEW_MODE = 1,
|
||||
|
||||
_document = false,
|
||||
_loaded = false,
|
||||
engine_initilized = false,
|
||||
|
||||
line_space_percent = 100,
|
||||
@@ -120,9 +121,16 @@ function CreDocument:init()
|
||||
self.info.configurable = true
|
||||
end
|
||||
|
||||
function CreDocument:loadDocument()
|
||||
if not self._loaded then
|
||||
self._document:loadDocument(self.file)
|
||||
self._loaded = true
|
||||
end
|
||||
end
|
||||
|
||||
function CreDocument:render()
|
||||
-- load document before rendering
|
||||
self._document:loadDocument(self.file)
|
||||
self:loadDocument()
|
||||
self._document:renderDocument()
|
||||
if not self.info.has_pages then
|
||||
self.info.doc_height = self._document:getFullHeight()
|
||||
@@ -139,7 +147,7 @@ end
|
||||
|
||||
function CreDocument:getCoverPageImage()
|
||||
-- don't need to render document in order to get cover image
|
||||
self._document:loadDocument(self.file)
|
||||
self:loadDocument()
|
||||
local data, size = self._document:getCoverPageImageData()
|
||||
if data and size then
|
||||
local image = Image:fromData(data, size)
|
||||
|
||||
@@ -81,7 +81,8 @@ end
|
||||
|
||||
-- this might be overridden by a document implementation
|
||||
function Document:close()
|
||||
if self.is_open then
|
||||
local DocumentRegistry = require("document/documentregistry")
|
||||
if self.is_open and DocumentRegistry:closeDocument(self.file) == 0 then
|
||||
self.is_open = false
|
||||
self._document:close()
|
||||
end
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
This is a registry for document providers
|
||||
]]--
|
||||
local DocumentRegistry = {
|
||||
providers = { }
|
||||
registry = {},
|
||||
providers = {},
|
||||
}
|
||||
|
||||
function DocumentRegistry:addProvider(extension, mimetype, provider)
|
||||
@@ -20,9 +21,31 @@ function DocumentRegistry:getProvider(file)
|
||||
end
|
||||
|
||||
function DocumentRegistry:openDocument(file)
|
||||
local provider = self:getProvider(file)
|
||||
if provider ~= nil then
|
||||
return provider:new{file = file}
|
||||
if not self.registry[file] then
|
||||
local provider = self:getProvider(file)
|
||||
if provider ~= nil then
|
||||
self.registry[file] = {
|
||||
doc = provider:new{file = file},
|
||||
refs = 1,
|
||||
}
|
||||
end
|
||||
else
|
||||
self.registry[file].refs = self.registry[file].refs + 1
|
||||
end
|
||||
return self.registry[file].doc
|
||||
end
|
||||
|
||||
function DocumentRegistry:closeDocument(file)
|
||||
if self.registry[file] then
|
||||
self.registry[file].refs = self.registry[file].refs - 1
|
||||
if self.registry[file].refs == 0 then
|
||||
self.registry[file] = nil
|
||||
return 0
|
||||
else
|
||||
return self.registry[file].refs
|
||||
end
|
||||
else
|
||||
error("Try to close unregistered file.")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user