2017-10-15 11:03:42 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Document = require("document/document")
|
2013-12-31 05:12:56 +00:00
|
|
|
local DrawContext = require("ffi/drawcontext")
|
2013-10-18 20:38:07 +00:00
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local DjvuDocument = Document:extend{
|
2014-03-13 13:52:43 +00:00
|
|
|
_document = false,
|
|
|
|
-- libdjvulibre manages its own additional cache, default value is hard written in c module.
|
2014-10-15 12:31:24 +00:00
|
|
|
is_djvu = true,
|
2014-03-13 13:52:43 +00:00
|
|
|
djvulibre_cache_size = nil,
|
|
|
|
dc_null = DrawContext.new(),
|
|
|
|
koptinterface = nil,
|
2017-10-15 11:03:42 +00:00
|
|
|
color_bb_type = Blitbuffer.TYPE_BBRGB24,
|
2018-02-02 20:21:52 +00:00
|
|
|
provider = "djvulibre",
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
provider_name = "DjVu Libre",
|
2012-06-03 02:59:35 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
-- check DjVu magic string to validate
|
|
|
|
local function validDjvuFile(filename)
|
2015-04-27 00:49:27 +00:00
|
|
|
local f = io.open(filename, "r")
|
2014-03-13 13:52:43 +00:00
|
|
|
if not f then return false end
|
|
|
|
local magic = f:read(8)
|
|
|
|
f:close()
|
|
|
|
if not magic or magic ~= "AT&TFORM" then return false end
|
|
|
|
return true
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
2012-06-03 02:59:35 +00:00
|
|
|
function DjvuDocument:init()
|
2014-03-13 13:52:43 +00:00
|
|
|
local djvu = require("libs/libkoreader-djvu")
|
|
|
|
self.koptinterface = require("document/koptinterface")
|
2018-10-26 15:27:43 +00:00
|
|
|
self.koptinterface:setDefaultConfigurable(self.configurable)
|
2014-03-13 13:52:43 +00:00
|
|
|
if not validDjvuFile(self.file) then
|
2014-08-29 09:17:08 +00:00
|
|
|
error("Not a valid DjVu file")
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local ok
|
2017-10-15 11:03:42 +00:00
|
|
|
ok, self._document = pcall(djvu.openDocument, self.file, self.render_color, self.djvulibre_cache_size)
|
2014-03-13 13:52:43 +00:00
|
|
|
if not ok then
|
2014-08-29 09:17:08 +00:00
|
|
|
error(self._document) -- will contain error message
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2024-07-14 20:00:31 +00:00
|
|
|
self:updateColorRendering()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.is_open = true
|
|
|
|
self.info.has_pages = true
|
|
|
|
self.info.configurable = true
|
2024-07-14 20:00:32 +00:00
|
|
|
self.render_mode = 0
|
2014-03-13 13:52:43 +00:00
|
|
|
self:_readMetadata()
|
2012-06-03 02:59:35 +00:00
|
|
|
end
|
|
|
|
|
2017-10-15 11:03:42 +00:00
|
|
|
function DjvuDocument:updateColorRendering()
|
|
|
|
Document.updateColorRendering(self) -- will set self.render_color
|
|
|
|
if self._document then
|
|
|
|
self._document:setColorRendering(self.render_color)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-06 19:54:33 +00:00
|
|
|
function DjvuDocument:comparePositions(pos1, pos2)
|
|
|
|
return self.koptinterface:comparePositions(self, pos1, pos2)
|
|
|
|
end
|
|
|
|
|
2013-10-12 15:07:13 +00:00
|
|
|
function DjvuDocument:getPageTextBoxes(pageno)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:getPageText(pageno)
|
2013-10-12 15:07:13 +00:00
|
|
|
end
|
|
|
|
|
2020-09-24 13:17:37 +00:00
|
|
|
function DjvuDocument:getPanelFromPage(pageno, pos)
|
|
|
|
return self.koptinterface:getPanelFromPage(self, pageno, pos)
|
|
|
|
end
|
|
|
|
|
2013-10-12 15:07:13 +00:00
|
|
|
function DjvuDocument:getWordFromPosition(spos)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.koptinterface:getWordFromPosition(self, spos)
|
2013-10-12 15:07:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:getTextFromPositions(spos0, spos1)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.koptinterface:getTextFromPositions(self, spos0, spos1)
|
2013-10-12 15:07:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:getPageBoxesFromPositions(pageno, ppos0, ppos1)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.koptinterface:getPageBoxesFromPositions(self, pageno, ppos0, ppos1)
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2014-05-02 08:50:43 +00:00
|
|
|
function DjvuDocument:nativeToPageRectTransform(pageno, rect)
|
|
|
|
return self.koptinterface:nativeToPageRectTransform(self, pageno, rect)
|
|
|
|
end
|
|
|
|
|
2022-10-25 10:23:18 +00:00
|
|
|
function DjvuDocument:getSelectedWordContext(word, nb_words, pos)
|
|
|
|
return self.koptinterface:getSelectedWordContext(word, nb_words, pos)
|
|
|
|
end
|
|
|
|
|
2013-10-14 15:05:58 +00:00
|
|
|
function DjvuDocument:getOCRWord(pageno, wbox)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.koptinterface:getOCRWord(self, pageno, wbox)
|
2013-10-14 15:05:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:getOCRText(pageno, tboxes)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.koptinterface:getOCRText(self, pageno, tboxes)
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2016-06-14 17:50:59 +00:00
|
|
|
function DjvuDocument:getPageBlock(pageno, x, y)
|
|
|
|
return self.koptinterface:getPageBlock(self, pageno, x, y)
|
2014-01-02 03:08:06 +00:00
|
|
|
end
|
|
|
|
|
2012-06-03 02:59:35 +00:00
|
|
|
function DjvuDocument:getUsedBBox(pageno)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- djvu does not support usedbbox, so fake it.
|
|
|
|
local used = {}
|
|
|
|
local native_dim = self:getNativePageDimensions(pageno)
|
|
|
|
used.x0, used.y0, used.x1, used.y1 = 0, 0, native_dim.w, native_dim.h
|
|
|
|
return used
|
2012-06-03 02:59:35 +00:00
|
|
|
end
|
|
|
|
|
2014-05-15 08:10:45 +00:00
|
|
|
function DjvuDocument:clipPagePNGFile(pos0, pos1, pboxes, drawer, filename)
|
|
|
|
return self.koptinterface:clipPagePNGFile(self, pos0, pos1, pboxes, drawer, filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:clipPagePNGString(pos0, pos1, pboxes, drawer)
|
|
|
|
return self.koptinterface:clipPagePNGString(self, pos0, pos1, pboxes, drawer)
|
|
|
|
end
|
|
|
|
|
2013-04-14 07:16:42 +00:00
|
|
|
function DjvuDocument:getPageBBox(pageno)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.koptinterface:getPageBBox(self, pageno)
|
2013-04-14 07:16:42 +00:00
|
|
|
end
|
|
|
|
|
2013-01-03 14:24:38 +00:00
|
|
|
function DjvuDocument:getPageDimensions(pageno, zoom, rotation)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.koptinterface:getPageDimensions(self, pageno, zoom, rotation)
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
|
|
|
|
2014-08-27 03:07:25 +00:00
|
|
|
function DjvuDocument:getCoverPageImage()
|
|
|
|
return self.koptinterface:getCoverPageImage(self)
|
|
|
|
end
|
|
|
|
|
2024-01-13 10:58:05 +00:00
|
|
|
function DjvuDocument:findText(pattern, origin, reverse, case_insensitive, page)
|
|
|
|
return self.koptinterface:findText(self, pattern, origin, reverse, case_insensitive, page)
|
|
|
|
end
|
|
|
|
|
|
|
|
function DjvuDocument:findAllText(pattern, case_insensitive, nb_context_words, max_hits)
|
|
|
|
return self.koptinterface:findAllText(self, pattern, case_insensitive, nb_context_words, max_hits)
|
2014-11-17 09:58:25 +00:00
|
|
|
end
|
|
|
|
|
2024-07-14 20:00:32 +00:00
|
|
|
function DjvuDocument:renderPage(pageno, rect, zoom, rotation, gamma, hinting)
|
|
|
|
return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, hinting)
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
|
|
|
|
2024-07-14 20:00:32 +00:00
|
|
|
function DjvuDocument:hintPage(pageno, zoom, rotation, gamma)
|
|
|
|
return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma)
|
2013-03-12 12:26:02 +00:00
|
|
|
end
|
|
|
|
|
2024-07-14 20:00:32 +00:00
|
|
|
function DjvuDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma)
|
|
|
|
return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma)
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
function DjvuDocument:register(registry)
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
registry:addProvider("djvu", "image/vnd.djvu", self, 100)
|
2021-02-07 18:11:45 +00:00
|
|
|
registry:addProvider("djvu", "application/djvu", self, 100) -- Alternative mimetype for OPDS.
|
|
|
|
registry:addProvider("djvu", "image/x-djvu", self, 100) -- Alternative mimetype for OPDS.
|
2021-01-17 08:22:48 +00:00
|
|
|
registry:addProvider("djv", "image/vnd.djvu", self, 100)
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return DjvuDocument
|