2014-08-14 11:49:42 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2019-03-03 02:50:05 +00:00
|
|
|
local CanvasContext = require("document/canvascontext")
|
2015-10-03 06:18:47 +00:00
|
|
|
local DataStorage = require("datastorage")
|
2017-04-01 15:55:15 +00:00
|
|
|
local Document = require("document/document")
|
2019-03-03 02:50:05 +00:00
|
|
|
local FontList = require("fontlist")
|
2014-01-17 19:05:17 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2018-04-21 20:03:04 +00:00
|
|
|
local RenderImage = require("ui/renderimage")
|
2019-04-18 21:26:53 +00:00
|
|
|
local Screen = require("device").screen
|
2021-05-05 20:48:10 +00:00
|
|
|
local buffer = require("string.buffer")
|
2014-08-22 10:24:49 +00:00
|
|
|
local ffi = require("ffi")
|
2018-06-02 16:10:55 +00:00
|
|
|
local C = ffi.C
|
2022-09-27 23:10:50 +00:00
|
|
|
local cre -- Delayed loading
|
2017-04-01 15:55:15 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
|
|
|
local logger = require("logger")
|
2021-05-05 20:48:10 +00:00
|
|
|
local lru = require("ffi/lru")
|
2022-05-05 19:00:22 +00:00
|
|
|
local time = require("ui/time")
|
2012-06-05 07:23:36 +00:00
|
|
|
|
2017-04-26 19:46:46 +00:00
|
|
|
-- engine can be initialized only once, on first document opened
|
|
|
|
local engine_initialized = false
|
|
|
|
|
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 CreDocument = Document:extend{
|
2014-03-13 13:52:43 +00:00
|
|
|
-- this is defined in kpvcrlib/crengine/crengine/include/lvdocview.h
|
|
|
|
SCROLL_VIEW_MODE = 0,
|
|
|
|
PAGE_VIEW_MODE = 1,
|
|
|
|
|
|
|
|
_document = false,
|
2014-08-28 12:59:42 +00:00
|
|
|
_loaded = false,
|
2019-02-03 08:58:48 +00:00
|
|
|
_view_mode = nil,
|
2019-04-18 21:26:53 +00:00
|
|
|
_smooth_scaling = false,
|
2019-06-25 22:23:41 +00:00
|
|
|
_nightmode_images = true,
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
line_space_percent = 100,
|
2018-05-11 15:48:26 +00:00
|
|
|
default_font = "Noto Serif",
|
2022-07-17 23:16:51 +00:00
|
|
|
monospace_font = "Droid Sans Mono",
|
2018-05-11 15:48:26 +00:00
|
|
|
header_font = "Noto Sans",
|
2020-04-25 18:20:41 +00:00
|
|
|
|
|
|
|
-- Reasons for the fallback font ordering:
|
|
|
|
-- - Noto Sans CJK SC before FreeSans/Serif, as it has nice and larger
|
|
|
|
-- symbol glyphs for Wikipedia EPUB headings than both Free fonts)
|
|
|
|
-- - FreeSerif after most, has it has good coverage but smaller glyphs
|
|
|
|
-- (and most other fonts are better looking)
|
|
|
|
-- - FreeSans covers areas that FreeSerif do not, and is usually
|
|
|
|
-- fine along other fonts (even serif fonts)
|
|
|
|
-- - Noto Serif & Sans at the end, just in case, and to have consistent
|
|
|
|
-- (and larger than FreeSerif) '?' glyphs for codepoints not found
|
|
|
|
-- in any fallback font. Also, we don't know if the user is using
|
|
|
|
-- a serif or a sans main font, so choosing to have one of these early
|
|
|
|
-- might not be the best decision (and moving them before FreeSans would
|
|
|
|
-- require one to set FreeSans as fallback to get its nicer glyphes, which
|
|
|
|
-- would override Noto Sans CJK good symbol glyphs with smaller ones
|
|
|
|
-- (Noto Sans & Serif do not have these symbol glyphs).
|
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
|
|
|
fallback_fonts = { -- const
|
2020-04-25 18:20:41 +00:00
|
|
|
"Noto Sans CJK SC",
|
2020-06-08 14:36:06 +00:00
|
|
|
"Noto Naskh Arabic",
|
2020-04-25 18:20:41 +00:00
|
|
|
"Noto Sans Devanagari UI",
|
2022-03-15 22:13:13 +00:00
|
|
|
"Noto Sans Bengali UI",
|
2020-04-25 18:20:41 +00:00
|
|
|
"FreeSans",
|
|
|
|
"FreeSerif",
|
|
|
|
"Noto Serif",
|
|
|
|
"Noto Sans",
|
|
|
|
},
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
default_css = "./data/cr3.css",
|
2018-02-02 20:21:52 +00:00
|
|
|
provider = "crengine",
|
[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 = "Cool Reader Engine",
|
2020-10-31 09:40:36 +00:00
|
|
|
|
|
|
|
hide_nonlinear_flows = false,
|
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
|
|
|
flows = nil, -- table
|
|
|
|
page_in_flow = nil, -- table
|
2020-10-31 09:40:36 +00:00
|
|
|
last_linear_page = nil,
|
2012-06-05 07:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-- NuPogodi, 20.05.12: inspect the zipfile content
|
2014-08-11 12:37:50 +00:00
|
|
|
function CreDocument:zipContentExt(fname)
|
|
|
|
local std_out = io.popen("unzip ".."-qql \""..fname.."\"")
|
|
|
|
if std_out then
|
2021-09-25 08:22:47 +00:00
|
|
|
local size, ext
|
2014-08-11 12:37:50 +00:00
|
|
|
for line in std_out:lines() do
|
2021-09-25 08:22:47 +00:00
|
|
|
size, ext = string.match(line, "%s+(%d+)%s+.+%.([^.]+)")
|
|
|
|
if size and ext then break end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2021-09-25 08:22:47 +00:00
|
|
|
std_out:close()
|
|
|
|
if ext then return string.lower(ext) end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2012-06-05 07:23:36 +00:00
|
|
|
end
|
|
|
|
|
2014-04-30 15:24:44 +00:00
|
|
|
function CreDocument:cacheInit()
|
|
|
|
-- remove legacy cr3cache directory
|
|
|
|
if lfs.attributes("./cr3cache", "mode") == "directory" then
|
|
|
|
os.execute("rm -r ./cr3cache")
|
|
|
|
end
|
2018-03-15 13:09:24 +00:00
|
|
|
-- crengine saves caches on disk for faster re-openings, and cleans
|
|
|
|
-- the less recently used ones when this limit is reached
|
|
|
|
local default_cre_disk_cache_max_size = 64 -- in MB units
|
2018-03-08 20:23:52 +00:00
|
|
|
-- crengine various in-memory caches max-sizes are rather small
|
2020-02-24 14:25:08 +00:00
|
|
|
-- (2.5 / 4.5 / 4.5 / 1 MB), and we can avoid some bugs if we
|
|
|
|
-- increase them. Let's multiply them by 40 (each cache would
|
2018-03-08 20:23:52 +00:00
|
|
|
-- grow only when needed, depending on book characteristics).
|
|
|
|
-- People who would get out of memory crashes with big books on
|
|
|
|
-- older devices can decrease that with setting:
|
|
|
|
-- "cre_storage_size_factor"=1 (or 2, or 5)
|
2020-02-24 14:25:08 +00:00
|
|
|
local default_cre_storage_size_factor = 40
|
2018-03-15 13:09:24 +00:00
|
|
|
cre.initCache(DataStorage:getDataDir() .. "/cache/cr3cache",
|
|
|
|
(G_reader_settings:readSetting("cre_disk_cache_max_size") or default_cre_disk_cache_max_size)*1024*1024,
|
2018-03-01 20:55:05 +00:00
|
|
|
G_reader_settings:nilOrTrue("cre_compress_cached_data"),
|
2018-03-08 20:23:52 +00:00
|
|
|
G_reader_settings:readSetting("cre_storage_size_factor") or default_cre_storage_size_factor)
|
2014-04-30 15:24:44 +00:00
|
|
|
end
|
|
|
|
|
2012-06-12 06:52:35 +00:00
|
|
|
function CreDocument:engineInit()
|
2017-04-26 19:46:46 +00:00
|
|
|
if not engine_initialized then
|
2022-09-27 23:10:50 +00:00
|
|
|
cre = require("libs/libkoreader-cre")
|
2014-03-13 13:52:43 +00:00
|
|
|
-- initialize cache
|
2014-04-30 15:24:44 +00:00
|
|
|
self:cacheInit()
|
|
|
|
|
|
|
|
-- initialize hyph dictionaries
|
2014-06-17 13:48:07 +00:00
|
|
|
cre.initHyphDict("./data/hyph/")
|
2013-03-13 02:43:15 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
-- we need to initialize the CRE font list
|
2019-03-03 02:50:05 +00:00
|
|
|
local fonts = FontList:getFontList()
|
2022-09-27 23:10:50 +00:00
|
|
|
for k, v in ipairs(fonts) do
|
|
|
|
if not v:find("/urw/") and not v:find("/nerdfonts/symbols.ttf") then
|
|
|
|
local ok, err = pcall(cre.registerFont, v)
|
2014-03-13 13:52:43 +00:00
|
|
|
if not ok then
|
2018-07-01 15:35:12 +00:00
|
|
|
logger.err("failed to register crengine font:", err)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-04-28 23:37:53 +00:00
|
|
|
-- Make sure registered fonts have a proper entry at weight 400 and 700 when
|
|
|
|
-- possible, to avoid having synthesized fonts for these normal and bold weights.
|
|
|
|
-- This allows restoring a bit of the previous behaviour of crengine when it
|
|
|
|
-- wasn't handling font styles, and associated for each typeface one single
|
|
|
|
-- font to regular (400) and one to bold (700).
|
|
|
|
-- It should ensure we use real fonts (and not synthesized ones) for normal text
|
|
|
|
-- and bold text with the font_base_weight setting set to its default value of 0 (=400).
|
|
|
|
cre.regularizeRegisteredFontsWeights(true) -- true to print what modifications were made
|
2012-06-12 06:52:35 +00:00
|
|
|
|
2022-07-17 23:16:51 +00:00
|
|
|
-- Set up bias for some specific fonts
|
|
|
|
self:setOtherFontBiases()
|
|
|
|
|
2017-04-26 19:46:46 +00:00
|
|
|
engine_initialized = true
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2022-09-27 23:10:50 +00:00
|
|
|
|
|
|
|
return cre
|
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()
|
2017-10-01 17:23:06 +00:00
|
|
|
self:updateColorRendering()
|
2014-03-13 13:52:43 +00:00
|
|
|
self:engineInit()
|
|
|
|
|
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
|
|
|
self.flows = {}
|
|
|
|
self.page_in_flow = {}
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2014-08-11 12:37:50 +00:00
|
|
|
file_type = self:zipContentExt(self.file) or "unknown"
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2018-06-06 11:18:29 +00:00
|
|
|
|
|
|
|
-- June 2018: epub.css has been cleaned to be more conforming to HTML specs
|
|
|
|
-- and to not include class name based styles (with conditional compatiblity
|
|
|
|
-- styles for previously opened documents). It should be usable on all
|
|
|
|
-- HTML based documents, except FB2 which has some incompatible specs.
|
|
|
|
-- The other css files (htm.css, rtf.css...) have not been updated in the
|
|
|
|
-- same way, and are kept as-is for when a previously opened document
|
|
|
|
-- requests one of them.
|
|
|
|
self.default_css = "./data/epub.css"
|
2019-10-17 13:17:35 +00:00
|
|
|
if file_type == "fb2" or file_type == "fb3" then
|
2018-06-06 11:18:29 +00:00
|
|
|
self.default_css = "./data/fb2.css"
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
|
2019-02-03 08:58:48 +00:00
|
|
|
-- This mode must be the same as the default one set as ReaderView.view_mode
|
2022-09-27 23:10:50 +00:00
|
|
|
self._view_mode = G_defaults:readSetting("DCREREADER_VIEW_MODE") == "scroll" and self.SCROLL_VIEW_MODE or self.PAGE_VIEW_MODE
|
2019-02-03 08:58:48 +00:00
|
|
|
|
2014-08-29 09:17:08 +00:00
|
|
|
local ok
|
2019-02-18 16:01:00 +00:00
|
|
|
ok, self._document = pcall(cre.newDocView, CanvasContext:getWidth(), CanvasContext:getHeight(), self._view_mode)
|
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
|
2014-04-02 20:59:17 +00:00
|
|
|
|
2018-01-03 08:43:49 +00:00
|
|
|
-- We would have liked to call self._document:loadDocument(self.file)
|
|
|
|
-- here, to detect early if file is a supported document, but we
|
|
|
|
-- need to delay it till after some crengine settings are set for a
|
|
|
|
-- consistent behaviour.
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
self.is_open = true
|
|
|
|
self.info.has_pages = false
|
|
|
|
self:_readMetadata()
|
|
|
|
self.info.configurable = true
|
2020-01-16 11:48:34 +00:00
|
|
|
|
|
|
|
-- Setup crengine library calls caching
|
|
|
|
self:setupCallCache()
|
2012-06-05 07:23:36 +00:00
|
|
|
end
|
|
|
|
|
2020-02-24 14:25:08 +00:00
|
|
|
function CreDocument:getDomVersionWithNormalizedXPointers()
|
|
|
|
return cre.getDomVersionWithNormalizedXPointers()
|
|
|
|
end
|
|
|
|
|
2018-05-04 22:38:50 +00:00
|
|
|
function CreDocument:getLatestDomVersion()
|
2018-05-24 22:22:08 +00:00
|
|
|
return cre.getLatestDomVersion()
|
2018-05-04 22:38:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getOldestDomVersion()
|
|
|
|
return 20171225 -- arbitrary day in the past
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:requestDomVersion(version)
|
2018-05-24 22:22:08 +00:00
|
|
|
logger.dbg("CreDocument: requesting DOM version:", version)
|
2020-10-03 13:07:07 +00:00
|
|
|
self._document:setIntProperty("crengine.render.requested_dom_version", version)
|
2018-05-04 22:38:50 +00:00
|
|
|
end
|
|
|
|
|
2020-08-25 13:45:05 +00:00
|
|
|
function CreDocument:getDocumentFormat()
|
|
|
|
return self._document:getDocumentFormat()
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:07:59 +00:00
|
|
|
function CreDocument:setupDefaultView()
|
|
|
|
if self.loaded then
|
|
|
|
-- Don't apply defaults if the document has already been loaded
|
|
|
|
-- as this must be done before calling loadDocument()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- have crengine load defaults from cr3.ini
|
|
|
|
self._document:readDefaults()
|
|
|
|
logger.dbg("CreDocument: applied cr3.ini default settings.")
|
|
|
|
|
2021-01-29 00:02:13 +00:00
|
|
|
-- Disable crengine image scaling options (we prefer scaling them via crengine.render.dpi)
|
|
|
|
self._document:setIntProperty("crengine.image.scaling.zoomin.block.mode", 0)
|
|
|
|
self._document:setIntProperty("crengine.image.scaling.zoomin.block.scale", 1)
|
|
|
|
self._document:setIntProperty("crengine.image.scaling.zoomin.inline.mode", 0)
|
|
|
|
self._document:setIntProperty("crengine.image.scaling.zoomin.inline.scale", 1)
|
|
|
|
self._document:setIntProperty("crengine.image.scaling.zoomout.block.mode", 0)
|
|
|
|
self._document:setIntProperty("crengine.image.scaling.zoomout.block.scale", 1)
|
|
|
|
self._document:setIntProperty("crengine.image.scaling.zoomout.inline.mode", 0)
|
|
|
|
self._document:setIntProperty("crengine.image.scaling.zoomout.inline.scale", 1)
|
|
|
|
|
2023-02-16 22:30:54 +00:00
|
|
|
-- crengine won't create a cache for small documents, which could actually
|
|
|
|
-- makes re-opening small files slower than big files!
|
|
|
|
-- Also, we need a cache for ReaderRolling's partial rerenderings handling.
|
|
|
|
-- In crengine code:
|
|
|
|
-- A cache is created if the file size is larger than:
|
|
|
|
-- crengine.cache.filesize.min = PROP_MIN_FILE_SIZE_TO_CACHE: default 300000
|
|
|
|
-- (fallback: DOCUMENT_CACHING_SIZE_THRESHOLD=1048576)
|
|
|
|
-- A cache is searched for (to be used) only if the file size is larger than 65535:
|
|
|
|
-- hardcoded not overridable: DOCUMENT_CACHING_MIN_SIZE=65535
|
|
|
|
-- Other related variables:
|
|
|
|
-- PROP_FORCED_MIN_FILE_SIZE_TO_CACHE: not used (a hardcoded value of 30000 is used instead)
|
|
|
|
-- (if filesize < 30000: swapToCache simulated but not really done)
|
|
|
|
-- DOCUMENT_CACHING_MAX_RAM_USAGE 8388608: not used
|
|
|
|
-- Force having a cache with small documents (but at least > 65K)
|
|
|
|
self._document:setIntProperty("crengine.cache.filesize.min", 65536)
|
|
|
|
|
2021-01-29 00:02:19 +00:00
|
|
|
-- If switching to two pages on view, we want it to behave like two columns
|
|
|
|
-- and each view to be a single page number (instead of the default of two).
|
|
|
|
-- This ensures that page number and count are consistent between top and
|
|
|
|
-- bottom status bars, that SkimTo -1/+1 don't do nothing every other tap,
|
|
|
|
-- and that reading statistics do not see half of the pages never read.
|
|
|
|
self._document:setIntProperty("window.pages.two.visible.as.one.page.number", 1)
|
|
|
|
|
2020-04-25 18:20:41 +00:00
|
|
|
-- set fallback font faces (this was formerly done in :init(), but it
|
2018-11-20 20:07:59 +00:00
|
|
|
-- affects crengine calcGlobalSettingsHash() and would invalidate the
|
|
|
|
-- cache from the main currently being read document when we just
|
2020-04-25 18:20:41 +00:00
|
|
|
-- loadDocument(only_metadata) another document to get its metadata
|
2018-11-20 20:07:59 +00:00
|
|
|
-- or cover image, eg. from History hold menu).
|
2020-04-25 18:20:41 +00:00
|
|
|
self:setupFallbackFontFaces()
|
2018-11-20 20:07:59 +00:00
|
|
|
|
2022-07-17 23:16:51 +00:00
|
|
|
-- Adjust or not fallback font sizes
|
|
|
|
self:setAdjustedFallbackFontSizes(G_reader_settings:nilOrTrue("cre_adjusted_fallback_font_sizes"))
|
|
|
|
|
|
|
|
-- set monospace fonts size scaling
|
|
|
|
self:setMonospaceFontScaling(G_reader_settings:readSetting("cre_monospace_scaling") or 100)
|
|
|
|
|
2019-02-18 16:01:00 +00:00
|
|
|
-- adjust font sizes according to dpi set in canvas context
|
|
|
|
self._document:adjustFontSizes(CanvasContext:getDPI())
|
2018-11-20 20:07:59 +00:00
|
|
|
|
|
|
|
-- set top status bar font size
|
2021-03-06 21:44:18 +00:00
|
|
|
if G_reader_settings:has("cre_header_status_font_size") then
|
2018-11-20 20:07:59 +00:00
|
|
|
self._document:setIntProperty("crengine.page.header.font.size",
|
|
|
|
G_reader_settings:readSetting("cre_header_status_font_size"))
|
|
|
|
end
|
2020-05-25 20:39:59 +00:00
|
|
|
|
|
|
|
-- One can set these to change from white background
|
2021-03-06 21:44:18 +00:00
|
|
|
if G_reader_settings:has("cre_background_color") then
|
2020-05-25 20:39:59 +00:00
|
|
|
self:setBackgroundColor(G_reader_settings:readSetting("cre_background_color"))
|
|
|
|
end
|
2021-03-06 21:44:18 +00:00
|
|
|
if G_reader_settings:has("cre_background_image") then
|
2020-05-25 20:39:59 +00:00
|
|
|
self:setBackgroundImage(G_reader_settings:readSetting("cre_background_image"))
|
|
|
|
end
|
2018-11-20 20:07:59 +00:00
|
|
|
end
|
|
|
|
|
2018-03-10 21:20:54 +00:00
|
|
|
function CreDocument:loadDocument(full_document)
|
2014-08-28 12:59:42 +00:00
|
|
|
if not self._loaded then
|
2018-03-10 21:20:54 +00:00
|
|
|
local only_metadata = full_document == false
|
2018-08-07 16:37:52 +00:00
|
|
|
logger.dbg("CreDocument: loading document...")
|
2019-03-14 12:59:32 +00:00
|
|
|
if only_metadata then
|
|
|
|
-- Setting a default font before loading document
|
|
|
|
-- actually do prevent some crashes
|
|
|
|
self:setFontFace(self.default_font)
|
|
|
|
end
|
2018-03-10 21:20:54 +00:00
|
|
|
if self._document:loadDocument(self.file, only_metadata) then
|
2018-01-03 08:43:49 +00:00
|
|
|
self._loaded = true
|
2018-08-07 16:37:52 +00:00
|
|
|
logger.dbg("CreDocument: loading done.")
|
|
|
|
else
|
|
|
|
logger.dbg("CreDocument: loading failed.")
|
2018-01-03 08:43:49 +00:00
|
|
|
end
|
2014-08-28 12:59:42 +00:00
|
|
|
end
|
2018-01-03 08:43:49 +00:00
|
|
|
return self._loaded
|
2014-08-28 12:59:42 +00:00
|
|
|
end
|
|
|
|
|
2014-08-28 05:22:31 +00:00
|
|
|
function CreDocument:render()
|
|
|
|
-- load document before rendering
|
2014-08-28 12:59:42 +00:00
|
|
|
self:loadDocument()
|
2019-03-13 12:07:30 +00:00
|
|
|
-- This is now configurable and done by ReaderRolling:
|
|
|
|
-- -- set visible page count in landscape
|
|
|
|
-- if math.max(CanvasContext:getWidth(), CanvasContext:getHeight()) / CanvasContext:getDPI()
|
2022-09-27 23:10:50 +00:00
|
|
|
-- < G_defaults:readSetting("DCREREADER_TWO_PAGE_THRESHOLD") then
|
2019-03-13 12:07:30 +00:00
|
|
|
-- self:setVisiblePageCount(1)
|
|
|
|
-- end
|
2018-08-07 16:37:52 +00:00
|
|
|
logger.dbg("CreDocument: rendering document...")
|
2014-08-28 05:22:31 +00:00
|
|
|
self._document:renderDocument()
|
2019-02-26 06:19:55 +00:00
|
|
|
self.info.doc_height = self._document:getFullHeight()
|
|
|
|
self.been_rendered = true
|
|
|
|
logger.dbg("CreDocument: rendering done.")
|
|
|
|
end
|
|
|
|
|
ReaderRolling: quicker partial rerenderings with EPUBs
Only available with EPUBs containing 2 or more fragments,
and a file size large enough to ensure a cache file is used.
The idea is simply, on any rendering setting change, to
skip the rerendering of the full book and to defer any
rerendering to the moment we draw a DocFragment, and
render only it.
So, on a setting change, only the fragment containing the
current page will be rerendered, and the new fragments we
may cross while turning pages.
When having done so, KOReader is in a degraded state (the
full page count is incorrect, the ToC is invalid...).
So, a full rerendering is needed, and one will happen
in the background, and when the user is idle, we reload
seamlessly and quickly from the cache file it has made.
ReaderFlipping will show some icons in the top left
corner to let it know at which steps in this procress
we are.
2023-02-16 22:30:58 +00:00
|
|
|
function CreDocument:getDocumentRenderingHash(extended)
|
2021-01-29 00:02:17 +00:00
|
|
|
if self.been_rendered then
|
ReaderRolling: quicker partial rerenderings with EPUBs
Only available with EPUBs containing 2 or more fragments,
and a file size large enough to ensure a cache file is used.
The idea is simply, on any rendering setting change, to
skip the rerendering of the full book and to defer any
rerendering to the moment we draw a DocFragment, and
render only it.
So, on a setting change, only the fragment containing the
current page will be rerendered, and the new fragments we
may cross while turning pages.
When having done so, KOReader is in a degraded state (the
full page count is incorrect, the ToC is invalid...).
So, a full rerendering is needed, and one will happen
in the background, and when the user is idle, we reload
seamlessly and quickly from the cache file it has made.
ReaderFlipping will show some icons in the top left
corner to let it know at which steps in this procress
we are.
2023-02-16 22:30:58 +00:00
|
|
|
return self._document:getDocumentRenderingHash(extended)
|
2021-01-29 00:02:17 +00:00
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2019-02-26 06:19:55 +00:00
|
|
|
function CreDocument:_readMetadata()
|
|
|
|
Document._readMetadata(self) -- will grab/update self.info.number_of_pages
|
|
|
|
if self.been_rendered then
|
|
|
|
-- getFullHeight() would crash if the document is not
|
|
|
|
-- yet rendered
|
2014-03-13 13:52:43 +00:00
|
|
|
self.info.doc_height = self._document:getFullHeight()
|
|
|
|
end
|
2019-02-26 06:19:55 +00:00
|
|
|
return true
|
2013-01-13 02:22:33 +00:00
|
|
|
end
|
|
|
|
|
2013-10-16 15:21:20 +00:00
|
|
|
function CreDocument:close()
|
2021-05-20 17:09:54 +00:00
|
|
|
-- Let Document do the refcount check, and tell us if we actually need to tear down the instance.
|
|
|
|
if Document.close(self) then
|
|
|
|
-- Yup, final Document instance, we can safely destroy internal data.
|
|
|
|
-- (Document already took care of our self._document userdata).
|
|
|
|
if self.buffer then
|
|
|
|
self.buffer:free()
|
|
|
|
self.buffer = nil
|
|
|
|
end
|
2021-05-05 20:48:10 +00:00
|
|
|
|
2021-05-20 17:09:54 +00:00
|
|
|
-- Only exists if the call cache is enabled
|
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
|
|
|
--[[
|
2021-05-20 17:09:54 +00:00
|
|
|
if self._callCacheDestroy then
|
|
|
|
self._callCacheDestroy()
|
|
|
|
end
|
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
|
|
|
--]]
|
2021-05-20 17:09:54 +00:00
|
|
|
end
|
2017-10-01 17:23:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:updateColorRendering()
|
|
|
|
Document.updateColorRendering(self) -- will set self.render_color
|
|
|
|
-- Delete current buffer, a new one will be created according
|
|
|
|
-- to self.render_color
|
|
|
|
if self.buffer then
|
|
|
|
self.buffer:free()
|
|
|
|
self.buffer = nil
|
|
|
|
end
|
2013-10-16 15:21:20 +00:00
|
|
|
end
|
|
|
|
|
2020-10-31 09:40:36 +00:00
|
|
|
function CreDocument:setHideNonlinearFlows(hide_nonlinear_flows)
|
|
|
|
if hide_nonlinear_flows ~= self.hide_nonlinear_flows then
|
|
|
|
self.hide_nonlinear_flows = hide_nonlinear_flows
|
|
|
|
self._document:setIntProperty("crengine.doc.nonlinear.pagebreak.force", self.hide_nonlinear_flows and 1 or 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:02:19 +00:00
|
|
|
function CreDocument:getPageCount(internal)
|
|
|
|
return self._document:getPages(internal)
|
2013-10-17 15:53:29 +00:00
|
|
|
end
|
|
|
|
|
2020-10-31 09:40:36 +00:00
|
|
|
-- Whether the document has any non-linear flow to care about
|
|
|
|
function CreDocument:hasNonLinearFlows()
|
|
|
|
return self._document:hasNonLinearFlows()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Whether non-linear flows (if any) will be hidden
|
|
|
|
function CreDocument:hasHiddenFlows()
|
|
|
|
return self.flows[1] ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the next/prev page number, skipping non-linear flows,
|
|
|
|
-- i.e. the next/prev page that is either in the current
|
|
|
|
-- flow or in the linear flow (flow 0)
|
|
|
|
-- If "page" is 0, these give the initial and final linear pages
|
|
|
|
function CreDocument:getNextPage(page)
|
|
|
|
if self:hasHiddenFlows() then
|
|
|
|
if page < 0 or page >= self:getPageCount() then
|
|
|
|
return 0
|
|
|
|
elseif page == 0 then
|
|
|
|
return self:getFirstPageInFlow(0)
|
|
|
|
end
|
|
|
|
local flow = self:getPageFlow(page)
|
|
|
|
local start_page = page + 1
|
|
|
|
local end_page = self:getLastLinearPage()
|
|
|
|
local test_page = start_page
|
|
|
|
-- max to ensure at least one iteration
|
|
|
|
-- (in case the current flow goes after all linear pages)
|
|
|
|
while test_page <= math.max(end_page, start_page) do
|
|
|
|
local test_page_flow = self:getPageFlow(test_page)
|
|
|
|
if test_page_flow == flow or test_page_flow == 0 then
|
|
|
|
-- same flow as current, or linear flow, this is a "good" page
|
|
|
|
return test_page
|
|
|
|
elseif test_page_flow > 0 then
|
|
|
|
-- some other non-linear flow, skip all pages in this flow
|
|
|
|
test_page = test_page + self:getTotalPagesInFlow(test_page_flow)
|
|
|
|
else
|
|
|
|
-- went beyond the last page
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return Document.getNextPage(self, page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPrevPage(page)
|
|
|
|
if self:hasHiddenFlows() then
|
|
|
|
if page < 0 or page > self:getPageCount() then
|
|
|
|
return 0
|
|
|
|
elseif page == 0 then
|
|
|
|
return self:getLastLinearPage()
|
|
|
|
end
|
|
|
|
local flow = self:getPageFlow(page)
|
|
|
|
local start_page = page - 1
|
|
|
|
local end_page = self:getFirstPageInFlow(0)
|
|
|
|
local test_page = start_page
|
|
|
|
-- min to ensure at least one iteration
|
|
|
|
-- (in case the current flow goes before all linear pages)
|
|
|
|
while test_page >= math.min(end_page, start_page) do
|
|
|
|
local test_page_flow = self:getPageFlow(test_page)
|
|
|
|
if test_page_flow == flow or test_page_flow == 0 then
|
|
|
|
-- same flow as current, or linear flow, this is a "good" page
|
|
|
|
return test_page
|
|
|
|
elseif test_page_flow > 0 then
|
|
|
|
-- some other non-linear flow, skip all pages in this flow
|
|
|
|
test_page = self:getFirstPageInFlow(test_page_flow) - 1
|
|
|
|
else
|
|
|
|
-- went beyond the first page
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return Document.getPrevPage(self, page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageFlow(page)
|
|
|
|
-- Only report non-linear pages if "hide_nonlinear_flows" is enabled, and in 1-page mode,
|
|
|
|
-- otherwise all pages are linear (flow 0)
|
|
|
|
if self.hide_nonlinear_flows and self._view_mode == self.PAGE_VIEW_MODE and self:getVisiblePageCount() == 1 then
|
|
|
|
return self._document:getPageFlow(page)
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getLastLinearPage()
|
|
|
|
return self.last_linear_page
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getFirstPageInFlow(flow)
|
|
|
|
return self.flows[flow][1]
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getTotalPagesInFlow(flow)
|
|
|
|
return self.flows[flow][2]
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageNumberInFlow(page)
|
|
|
|
if self:hasHiddenFlows() then
|
|
|
|
return self.page_in_flow[page]
|
|
|
|
else
|
|
|
|
return page
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:cacheFlows()
|
|
|
|
-- Build the cache tables "flows" and "page_in_flow", if there are
|
|
|
|
-- any non-linear flows in the source document. Also set the value
|
|
|
|
-- of "last_linear_page", to possibly speed up counting in documents
|
|
|
|
-- with many non-linear pages at the end.
|
|
|
|
-- flows[i] contains {ini, num}, where ini is the first page in flow i,
|
|
|
|
-- and num is the total number of pages in the flow.
|
|
|
|
-- page_in_flow[i] contains the number of page i with its flow.
|
|
|
|
--
|
|
|
|
-- So, flows[0][1] is the first page in the linear flow,
|
|
|
|
-- and page_in_flow[flows[0][1]] must be 1, because it is the first
|
|
|
|
self.flows = {}
|
|
|
|
self.page_in_flow = {}
|
|
|
|
if self:hasNonLinearFlows() and self.hide_nonlinear_flows then
|
|
|
|
for i=1,self:getPageCount() do
|
|
|
|
local flow = self:getPageFlow(i)
|
|
|
|
if self.flows[flow] ~= nil then
|
|
|
|
self.flows[flow][2] = self.flows[flow][2]+1
|
|
|
|
else
|
|
|
|
self.flows[flow] = {i, 1}
|
|
|
|
end
|
|
|
|
self.page_in_flow[i] = self.flows[flow][2]
|
|
|
|
if flow == 0 then
|
|
|
|
self.last_linear_page = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.last_linear_page = self:getPageCount()
|
|
|
|
self.flows[0] = {1, self.last_linear_page}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getTotalPagesLeft(page)
|
|
|
|
if self:hasHiddenFlows() then
|
|
|
|
local pages_left
|
|
|
|
local last_linear = self:getLastLinearPage()
|
|
|
|
if page > last_linear then
|
|
|
|
-- If beyond the last linear page, count only the pages in the current flow
|
|
|
|
local flow = self:getPageFlow(page)
|
|
|
|
pages_left = self:getTotalPagesInFlow(flow) - self:getPageNumberInFlow(page)
|
|
|
|
else
|
|
|
|
-- Otherwise, count all pages until the last linear,
|
|
|
|
-- except the flows that start (and end) between
|
|
|
|
-- the current page and the last linear
|
|
|
|
pages_left = last_linear - page
|
|
|
|
for flow, tab in ipairs(self.flows) do
|
|
|
|
-- tab[1] is the initial page of the flow
|
|
|
|
-- tab[2] is the total number of pages in the flow
|
|
|
|
if tab[1] > last_linear then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
-- strict >, to make sure we include pages in the current flow
|
|
|
|
if tab[1] > page then
|
|
|
|
pages_left = pages_left - tab[2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return pages_left
|
|
|
|
else
|
|
|
|
return Document.getTotalPagesLeft(self, page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-22 10:24:49 +00:00
|
|
|
function CreDocument:getCoverPageImage()
|
2018-04-21 20:03:04 +00:00
|
|
|
-- no need to render document in order to get cover image
|
2018-01-03 08:43:49 +00:00
|
|
|
if not self:loadDocument() then
|
|
|
|
return nil -- not recognized by crengine
|
|
|
|
end
|
2014-08-22 10:24:49 +00:00
|
|
|
local data, size = self._document:getCoverPageImageData()
|
|
|
|
if data and size then
|
2018-04-21 20:03:04 +00:00
|
|
|
local image = RenderImage:renderImageData(data, size)
|
2018-06-02 16:10:55 +00:00
|
|
|
C.free(data) -- free the userdata we got from crengine
|
2018-04-21 20:03:04 +00:00
|
|
|
return image
|
2017-01-15 20:42:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-11 17:55:28 +00:00
|
|
|
function CreDocument:getImageFromPosition(pos, want_frames, accept_cre_scalable_image)
|
|
|
|
local data, size, cre_img = self._document:getImageDataFromPosition(pos.x, pos.y, accept_cre_scalable_image)
|
2017-01-15 20:42:04 +00:00
|
|
|
if data and size then
|
|
|
|
logger.dbg("CreDocument: got image data from position", data, size)
|
2018-04-21 20:03:04 +00:00
|
|
|
local image = RenderImage:renderImageData(data, size, want_frames)
|
2018-06-02 16:10:55 +00:00
|
|
|
C.free(data) -- free the userdata we got from crengine
|
2018-04-21 20:03:04 +00:00
|
|
|
return image
|
2014-08-22 10:24:49 +00:00
|
|
|
end
|
2022-09-11 17:55:28 +00:00
|
|
|
if cre_img then
|
|
|
|
-- The image is a scalable image (SVG), and we got an image object from crengine, that
|
|
|
|
-- can draw itself at any requested scale factor: returns a function, that will be used
|
|
|
|
-- by ImageViewer to get the perfect bb.
|
|
|
|
return function(scale, w, h)
|
|
|
|
logger.dbg("CreImage: scaling for", scale, w, h)
|
|
|
|
if not cre_img then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if scale == false then -- used to signal we are done with the object
|
|
|
|
cre_img:free()
|
|
|
|
cre_img = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- scale will be used if non-0, otherwise the bb will be made to fit in w/h,
|
|
|
|
-- keeping the original aspect ratio
|
|
|
|
local image_data, image_w, image_h, image_scale = cre_img:renderScaled(scale, w, h)
|
|
|
|
if image_data then
|
|
|
|
-- This data is held in the cre_img object, so this bb is only
|
|
|
|
-- valid as long as this object is alive, and until the next
|
|
|
|
-- call to this function that will replace this data.
|
|
|
|
local bb = Blitbuffer.new(image_w, image_h, Blitbuffer.TYPE_BBRGB32, image_data)
|
|
|
|
return bb, image_scale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-22 10:24:49 +00:00
|
|
|
end
|
|
|
|
|
2013-12-24 17:42:57 +00:00
|
|
|
function CreDocument:getWordFromPosition(pos)
|
2014-07-02 06:47:05 +00:00
|
|
|
local wordbox = {
|
|
|
|
page = self._document:getCurrentPage(),
|
|
|
|
}
|
2020-12-22 17:45:27 +00:00
|
|
|
-- We use getTextFromPositions() which is more accurate.
|
|
|
|
-- In case some stuff is missing, we could fallback to use
|
|
|
|
-- the less accurate getWordFromPosition().
|
|
|
|
-- But it looks like getTextFromPositions() is just fine, and
|
|
|
|
-- when it fails, it's because there's no word at position.
|
|
|
|
-- So, we'll return nil below in case not all is found
|
|
|
|
local word_found = false
|
|
|
|
local box_found = false
|
|
|
|
|
|
|
|
local text_range = self._document:getTextFromPositions(pos.x, pos.y, pos.x, pos.y)
|
|
|
|
logger.dbg("CreDocument: get text range", text_range)
|
2019-03-13 12:10:32 +00:00
|
|
|
if text_range then
|
2020-12-22 17:45:27 +00:00
|
|
|
if text_range.text and text_range.text ~= "" then
|
|
|
|
wordbox.word = text_range.text
|
|
|
|
word_found = true
|
|
|
|
end
|
|
|
|
if text_range.pos0 and text_range.pos1 then
|
|
|
|
-- get segments from these pos, to build the overall box
|
|
|
|
local word_boxes = self._document:getWordBoxesFromPositions(text_range.pos0, text_range.pos1, true)
|
2021-10-23 10:12:43 +00:00
|
|
|
-- convert to Geom so we can use Geom.boundingBox
|
|
|
|
for i=1, #word_boxes do
|
|
|
|
local v = word_boxes[i]
|
|
|
|
word_boxes[i] = { x = v.x0, y = v.y0,
|
|
|
|
w = v.x1 - v.x0, h = v.y1 - v.y0 }
|
|
|
|
end
|
|
|
|
wordbox.sbox = Geom.boundingBox(word_boxes)
|
|
|
|
if wordbox.sbox then
|
2020-12-22 17:45:27 +00:00
|
|
|
box_found = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- add xpointers if any, might be useful for across pages highlighting
|
2019-03-13 12:10:32 +00:00
|
|
|
wordbox.pos0 = text_range.pos0
|
|
|
|
wordbox.pos1 = text_range.pos1
|
|
|
|
end
|
2020-12-22 17:45:27 +00:00
|
|
|
|
|
|
|
-- Fully trust getTextFromPositions()
|
|
|
|
if word_found and box_found then
|
|
|
|
return wordbox
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If we ever want to fallback to getWordFromPositions()
|
|
|
|
--[[
|
|
|
|
local word = self._document:getWordFromPosition(pos.x, pos.y)
|
|
|
|
logger.warn("CreDocument: get word box", word)
|
|
|
|
if not word_found then
|
|
|
|
wordbox.word = word.word
|
|
|
|
end
|
|
|
|
if not box_found then
|
|
|
|
if word.word then
|
|
|
|
wordbox.sbox = Geom:new{
|
|
|
|
x = word.x0,
|
|
|
|
y = word.y0,
|
|
|
|
w = word.x1 - word.x0,
|
|
|
|
h = word.y1 - word.y0,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
-- dummy box
|
|
|
|
wordbox.sbox = Geom:new{
|
|
|
|
x = pos.x, y = pos.y,
|
|
|
|
w = 20, h = 20,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2014-07-02 06:47:05 +00:00
|
|
|
return wordbox
|
2020-12-22 17:45:27 +00:00
|
|
|
]]--
|
2013-12-24 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
2023-05-17 04:34:37 +00:00
|
|
|
function CreDocument:getTextFromPositions(pos0, pos1, do_not_draw_selection)
|
|
|
|
local drawSelection, drawSegmentedSelection
|
|
|
|
if do_not_draw_selection then
|
|
|
|
drawSelection, drawSegmentedSelection = false, false
|
|
|
|
end
|
|
|
|
local text_range = self._document:getTextFromPositions(pos0.x, pos0.y, pos1.x, pos1.y,
|
|
|
|
drawSelection, drawSegmentedSelection)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: get text range", text_range)
|
2014-11-06 03:18:04 +00:00
|
|
|
if text_range then
|
2016-02-15 09:33:48 +00:00
|
|
|
-- local line_boxes = self:getScreenBoxesFromPositions(text_range.pos0, text_range.pos1)
|
2014-11-06 03:18:04 +00:00
|
|
|
return {
|
|
|
|
text = text_range.text,
|
|
|
|
pos0 = text_range.pos0,
|
|
|
|
pos1 = text_range.pos1,
|
|
|
|
--sboxes = line_boxes, -- boxes on screen
|
|
|
|
}
|
|
|
|
end
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2018-10-08 16:57:59 +00:00
|
|
|
function CreDocument:getScreenBoxesFromPositions(pos0, pos1, get_segments)
|
2014-03-13 13:52:43 +00:00
|
|
|
local line_boxes = {}
|
|
|
|
if pos0 and pos1 then
|
2018-10-08 16:57:59 +00:00
|
|
|
local word_boxes = self._document:getWordBoxesFromPositions(pos0, pos1, get_segments)
|
2014-03-13 13:52:43 +00:00
|
|
|
for i = 1, #word_boxes do
|
|
|
|
local line_box = word_boxes[i]
|
|
|
|
table.insert(line_boxes, Geom:new{
|
|
|
|
x = line_box.x0, y = line_box.y0,
|
|
|
|
w = line_box.x1 - line_box.x0,
|
|
|
|
h = line_box.y1 - line_box.y0,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return line_boxes
|
2013-12-24 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
2019-02-17 15:15:32 +00:00
|
|
|
function CreDocument:compareXPointers(xp1, xp2)
|
|
|
|
-- Returns 1 if XPointers are ordered (if xp2 is after xp1), -1 if not, 0 if same
|
2022-01-19 13:18:03 +00:00
|
|
|
-- Returns nil if any of XPointers are invalid
|
2019-02-17 15:15:32 +00:00
|
|
|
return self._document:compareXPointers(xp1, xp2)
|
|
|
|
end
|
|
|
|
|
2019-02-15 23:42:27 +00:00
|
|
|
function CreDocument:getNextVisibleWordStart(xp)
|
|
|
|
return self._document:getNextVisibleWordStart(xp)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getNextVisibleWordEnd(xp)
|
|
|
|
return self._document:getNextVisibleWordEnd(xp)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPrevVisibleWordStart(xp)
|
|
|
|
return self._document:getPrevVisibleWordStart(xp)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPrevVisibleWordEnd(xp)
|
|
|
|
return self._document:getPrevVisibleWordEnd(xp)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPrevVisibleChar(xp)
|
|
|
|
return self._document:getPrevVisibleChar(xp)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getNextVisibleChar(xp)
|
|
|
|
return self._document:getNextVisibleChar(xp)
|
|
|
|
end
|
|
|
|
|
2022-10-25 10:23:18 +00:00
|
|
|
function CreDocument:getSelectedWordContext(word, nb_words, pos0, pos1)
|
|
|
|
local pos_start = pos0
|
|
|
|
local pos_end = pos1
|
|
|
|
|
|
|
|
for i=0, nb_words do
|
|
|
|
local start = self:getPrevVisibleWordStart(pos_start)
|
|
|
|
if start then pos_start = start
|
|
|
|
else break end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=0, nb_words do
|
|
|
|
local ending = self:getNextVisibleWordEnd(pos_end)
|
|
|
|
if ending then pos_end = ending
|
|
|
|
else break end
|
|
|
|
end
|
|
|
|
|
|
|
|
local prev = self:getTextFromXPointers(pos_start, pos0)
|
|
|
|
local next = self:getTextFromXPointers(pos1, pos_end)
|
|
|
|
|
|
|
|
return prev, next
|
|
|
|
end
|
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
function CreDocument:drawCurrentView(target, x, y, rect, pos)
|
2014-10-27 14:03:20 +00:00
|
|
|
if self.buffer and (self.buffer.w ~= rect.w or self.buffer.h ~= rect.h) then
|
|
|
|
self.buffer:free()
|
|
|
|
self.buffer = nil
|
|
|
|
end
|
|
|
|
if not self.buffer then
|
2017-10-01 17:23:06 +00:00
|
|
|
-- Note about color rendering:
|
2019-03-27 21:50:44 +00:00
|
|
|
-- We use TYPE_BBRGB32 (and LVColorDrawBuf drawBuf(..., 32) in cre.cpp),
|
|
|
|
-- to match the screen's BB type, allowing us to take shortcuts when blitting.
|
|
|
|
self.buffer = Blitbuffer.new(rect.w, rect.h, self.render_color and Blitbuffer.TYPE_BBRGB32 or nil)
|
2014-10-27 14:03:20 +00:00
|
|
|
end
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @todo self.buffer could be re-used when no page/layout/highlights
|
2018-10-08 16:57:59 +00:00
|
|
|
-- change has been made, to avoid having crengine redraw the exact
|
2019-03-29 23:04:38 +00:00
|
|
|
-- same buffer. And it could only change when some other methods
|
|
|
|
-- from here are called
|
|
|
|
|
2019-04-18 21:26:53 +00:00
|
|
|
-- If in night mode, we ask crengine to invert all images, so they
|
2019-03-29 23:04:38 +00:00
|
|
|
-- get displayed in their original colors when the whole screen
|
|
|
|
-- is inverted by night mode
|
2019-04-18 21:26:53 +00:00
|
|
|
-- We also honor the current smooth scaling setting,
|
|
|
|
-- as well as the global SW dithering setting.
|
2019-03-29 23:04:38 +00:00
|
|
|
|
2022-05-05 19:00:22 +00:00
|
|
|
--local start_time = time.now()
|
2019-03-29 23:04:38 +00:00
|
|
|
self._drawn_images_count, self._drawn_images_surface_ratio =
|
2020-11-08 21:55:11 +00:00
|
|
|
self._document:drawCurrentPage(self.buffer, self.render_color, Screen.night_mode and self._nightmode_images, self._smooth_scaling, Screen.sw_dithering)
|
2022-05-05 19:00:22 +00:00
|
|
|
--local end_time = time.now()
|
|
|
|
--print(string.format("CreDocument:drawCurrentView: Rendering took %9.3f ms", time.to_ms(end_time - start_time))
|
2019-03-29 23:04:38 +00:00
|
|
|
|
2022-05-05 19:00:22 +00:00
|
|
|
--start = time.now()
|
2014-10-27 14:03:20 +00:00
|
|
|
target:blitFrom(self.buffer, x, y, 0, 0, rect.w, rect.h)
|
2022-05-05 19:00:22 +00:00
|
|
|
--end_time = time.now()
|
|
|
|
--print(string.format("CreDocument:drawCurrentView: Blitting took %9.3f ms", time.to_ms(end_time - start_time))
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:drawCurrentViewByPos(target, x, y, rect, pos)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:gotoPos(pos)
|
|
|
|
self:drawCurrentView(target, x, y, rect)
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:drawCurrentViewByPage(target, x, y, rect, page)
|
2021-01-29 00:02:19 +00:00
|
|
|
if not self.no_page_sync then
|
|
|
|
-- Avoid syncing page when this flag is set, when selecting text
|
|
|
|
-- across pages in 2-page mode and flipping half the screen
|
|
|
|
-- (currently only set by ReaderHighlight:onHoldPan())
|
|
|
|
-- self._document:gotoPage(page)
|
|
|
|
-- This allows this method to not be cached by cre call cache
|
|
|
|
self:gotoPage(page)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
self:drawCurrentView(target, x, y, rect)
|
2012-12-22 05:27:46 +00:00
|
|
|
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
|
|
|
|
|
2018-10-12 15:51:58 +00:00
|
|
|
function CreDocument:getPageMargins()
|
|
|
|
return self._document:getPageMargins()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getHeaderHeight()
|
|
|
|
return self._document:getHeaderHeight()
|
|
|
|
end
|
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
function CreDocument:gotoXPointer(xpointer)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: goto xpointer", xpointer)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:gotoXPointer(xpointer)
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getXPointer()
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:getXPointer()
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 17:29:20 +00:00
|
|
|
function CreDocument:getPageXPointer(page)
|
|
|
|
return self._document:getPageXPointer(page)
|
|
|
|
end
|
|
|
|
|
2017-01-15 20:42:04 +00:00
|
|
|
function CreDocument:isXPointerInDocument(xp)
|
|
|
|
return self._document:isXPointerInDocument(xp)
|
|
|
|
end
|
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
function CreDocument:getPosFromXPointer(xp)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:getPosFromXPointer(xp)
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageFromXPointer(xp)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:getPageFromXPointer(xp)
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
2019-09-21 16:59:05 +00:00
|
|
|
function CreDocument:getPageOffsetX(page)
|
|
|
|
return self._document:getPageOffsetX(page)
|
|
|
|
end
|
|
|
|
|
2019-02-03 08:58:48 +00:00
|
|
|
function CreDocument:getScreenPositionFromXPointer(xp)
|
|
|
|
-- We do not ensure xp is in the current page: we may return
|
|
|
|
-- a negative screen_y, which could be useful in some contexts
|
|
|
|
local doc_margins = self:getPageMargins()
|
|
|
|
local doc_y, doc_x = self:getPosFromXPointer(xp)
|
|
|
|
local top_y = self:getCurrentPos()
|
|
|
|
local screen_y = doc_y - top_y
|
2019-03-13 12:09:44 +00:00
|
|
|
local screen_x = doc_x + doc_margins["left"]
|
2019-02-03 08:58:48 +00:00
|
|
|
if self._view_mode == self.PAGE_VIEW_MODE then
|
2019-03-13 12:09:44 +00:00
|
|
|
if self:getVisiblePageCount() > 1 then
|
|
|
|
-- Correct coordinates if on the 2nd page in 2-pages mode
|
2021-01-29 00:02:19 +00:00
|
|
|
-- getPageStartY() and getPageOffsetX() expects internal page numbers
|
|
|
|
local next_page = self:getCurrentPage(true) + 1
|
|
|
|
if next_page <= self:getPageCount(true) then
|
2019-03-13 12:09:44 +00:00
|
|
|
local next_top_y = self._document:getPageStartY(next_page)
|
|
|
|
if doc_y >= next_top_y then
|
|
|
|
screen_y = doc_y - next_top_y
|
|
|
|
screen_x = screen_x + self._document:getPageOffsetX(next_page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-03 08:58:48 +00:00
|
|
|
screen_y = screen_y + doc_margins["top"] + self:getHeaderHeight()
|
|
|
|
end
|
|
|
|
-- Just as getPosFromXPointer() does, we return y first and x second,
|
|
|
|
-- as callers most often just need the y
|
|
|
|
return screen_y, screen_x
|
|
|
|
end
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function CreDocument:getFontFace()
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:getFontFace()
|
2012-06-12 12:58:15 +00:00
|
|
|
end
|
|
|
|
|
2022-06-11 09:08:42 +00:00
|
|
|
function CreDocument:getEmbeddedFontList()
|
|
|
|
return self._document:getEmbeddedFontList()
|
|
|
|
end
|
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
function CreDocument:getCurrentPos()
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:getCurrentPos()
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
2018-10-10 16:50:24 +00:00
|
|
|
function CreDocument:getPageLinks(internal_links_only)
|
|
|
|
return self._document:getPageLinks(internal_links_only)
|
2014-01-15 15:34:37 +00:00
|
|
|
end
|
|
|
|
|
2014-01-20 12:41:01 +00:00
|
|
|
function CreDocument:getLinkFromPosition(pos)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:getLinkFromPosition(pos.x, pos.y)
|
2014-01-20 12:41:01 +00:00
|
|
|
end
|
|
|
|
|
2018-10-10 16:50:24 +00:00
|
|
|
function CreDocument:isLinkToFootnote(source_xpointer, target_xpointer, flags, max_text_size)
|
|
|
|
return self._document:isLinkToFootnote(source_xpointer, target_xpointer, flags, max_text_size)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:highlightXPointer(xp)
|
|
|
|
-- with xp=nil, clears previous highlight(s)
|
|
|
|
return self._document:highlightXPointer(xp)
|
|
|
|
end
|
|
|
|
|
2018-10-08 16:58:43 +00:00
|
|
|
function CreDocument:getDocumentFileContent(filepath)
|
|
|
|
if filepath then
|
|
|
|
return self._document:getDocumentFileContent(filepath)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getTextFromXPointer(xp)
|
|
|
|
if xp then
|
|
|
|
return self._document:getTextFromXPointer(xp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-23 10:12:47 +00:00
|
|
|
function CreDocument:getTextFromXPointers(pos0, pos1, draw_selection)
|
|
|
|
local draw_segmented_selection = draw_selection -- always use segmented selections
|
|
|
|
return self._document:getTextFromXPointers(pos0, pos1, draw_selection, draw_segmented_selection)
|
2019-02-15 23:42:27 +00:00
|
|
|
end
|
|
|
|
|
2022-06-11 09:08:40 +00:00
|
|
|
function CreDocument:extendXPointersToSentenceSegment(pos0, pos1)
|
|
|
|
return self._document:extendXPointersToSentenceSegment(pos0, pos1)
|
|
|
|
end
|
|
|
|
|
2018-10-08 16:58:43 +00:00
|
|
|
function CreDocument:getHTMLFromXPointer(xp, flags, from_final_parent)
|
|
|
|
if xp then
|
|
|
|
return self._document:getHTMLFromXPointer(xp, flags, from_final_parent)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getHTMLFromXPointers(xp0, xp1, flags, from_root_node)
|
|
|
|
if xp0 and xp1 then
|
|
|
|
return self._document:getHTMLFromXPointers(xp0, xp1, flags, from_root_node)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-05 12:54:25 +00:00
|
|
|
function CreDocument:getStylesheetsMatchingRulesets(node_dataindex)
|
|
|
|
return self._document:getStylesheetsMatchingRulesets(node_dataindex)
|
|
|
|
end
|
|
|
|
|
2020-02-24 14:25:08 +00:00
|
|
|
function CreDocument:getNormalizedXPointer(xp)
|
|
|
|
-- Returns false when xpointer is not found in the DOM.
|
|
|
|
-- When requested DOM version >= getDomVersionWithNormalizedXPointers,
|
|
|
|
-- should return xp unmodified when found.
|
|
|
|
return self._document:getNormalizedXPointer(xp)
|
|
|
|
end
|
|
|
|
|
2017-10-01 17:23:06 +00:00
|
|
|
function CreDocument:gotoPos(pos)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: goto position", pos)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:gotoPos(pos)
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
2021-01-29 00:02:19 +00:00
|
|
|
function CreDocument:gotoPage(page, internal)
|
2020-10-31 09:40:36 +00:00
|
|
|
logger.dbg("CreDocument: goto page", page, "flow", self:getPageFlow(page))
|
2021-01-29 00:02:19 +00:00
|
|
|
self._document:gotoPage(page, internal)
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
2014-01-15 15:34:37 +00:00
|
|
|
function CreDocument:gotoLink(link)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: goto link", link)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:gotoLink(link)
|
2014-01-15 15:34:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:goBack()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: go back")
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:goBack()
|
2014-01-15 15:34:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:goForward(link)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: go forward")
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:goForward()
|
2014-01-15 15:34:37 +00:00
|
|
|
end
|
|
|
|
|
2021-01-29 00:02:19 +00:00
|
|
|
function CreDocument:getCurrentPage(internal)
|
|
|
|
return self._document:getCurrentPage(internal)
|
2012-12-22 05:27:46 +00:00
|
|
|
end
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function CreDocument:setFontFace(new_font_face)
|
2014-03-13 13:52:43 +00:00
|
|
|
if new_font_face then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set font face", new_font_face)
|
2014-05-11 12:57:18 +00:00
|
|
|
self._document:setStringProperty("font.face.default", new_font_face)
|
2017-09-01 19:49:10 +00:00
|
|
|
|
|
|
|
-- The following makes FontManager prefer this font in its match
|
|
|
|
-- algorithm, with the bias given (applies only to rendering of
|
|
|
|
-- elements with css font-family)
|
|
|
|
-- See: crengine/src/lvfntman.cpp LVFontDef::CalcMatch():
|
|
|
|
-- it will compute a score for each font, where it adds:
|
|
|
|
-- + 25600 if standard font family matches (inherit serif sans-serif
|
2022-07-17 23:16:51 +00:00
|
|
|
-- cursive fantasy monospace) (note that crengine registers all fonts
|
|
|
|
-- as "sans-serif", except monospace fonts)
|
2017-09-01 19:49:10 +00:00
|
|
|
-- + 6400 if they don't and none are monospace (ie:serif vs sans-serif,
|
|
|
|
-- prefer a sans-serif to a monospace if looking for a serif)
|
|
|
|
-- +256000 if font names match
|
|
|
|
-- So, here, we can bump the score of our default font, and we could use:
|
|
|
|
-- +1: uses existing real font-family, but use our font for
|
|
|
|
-- font-family: serif, sans-serif..., and fonts not found (or
|
|
|
|
-- embedded fonts disabled)
|
|
|
|
-- +25601: uses existing real font-family, but use our font even
|
|
|
|
-- for font-family: monospace
|
|
|
|
-- +256001: prefer our font to any existing font-family font
|
2022-07-17 23:16:51 +00:00
|
|
|
-- cre.setAsPreferredFontWithBias(new_font_face, 1)
|
|
|
|
-- Rather +1 +128x5 +256x5: we want our main font, even if it has no italic
|
|
|
|
-- nor bold variant (eg FreeSerif), to win over all other fonts that have
|
|
|
|
-- an italic or bold variant:
|
2020-02-27 11:12:49 +00:00
|
|
|
-- italic_match = 5 * (256 for real italic, or 128 for fake italic
|
|
|
|
-- weight_match = 5 * (256 - weight_diff * 256 / 800)
|
|
|
|
-- so give our font a bias enough to win over real italic or bold fonts
|
|
|
|
-- (all others params (size, family, name), used for computing the match
|
|
|
|
-- score, have a factor of 100 or 1000 vs the 5 used for italic & weight,
|
|
|
|
-- so it shouldn't hurt much).
|
2022-07-17 23:16:51 +00:00
|
|
|
-- Note that this is mostly necessary when a font name is given and we
|
|
|
|
-- don't have the font.
|
|
|
|
cre.setAsPreferredFontWithBias(new_font_face, 1 + 128*5 + 256*5)
|
|
|
|
|
|
|
|
-- The above call has resetted all other biases, so re-set our other ones
|
|
|
|
self:setOtherFontBiases()
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2012-06-12 09:42:02 +00:00
|
|
|
end
|
|
|
|
|
2022-07-17 23:16:51 +00:00
|
|
|
function CreDocument:setOtherFontBiases()
|
|
|
|
-- Make sure the user selected (or the default) monospace font is used even
|
|
|
|
-- if other monospace fonts were registered (same factor as above so its
|
|
|
|
-- synthetic bold or italic are used, in case some other monospace font
|
|
|
|
-- has real bold or italic variants)
|
|
|
|
local monospace_font = G_reader_settings:readSetting("monospace_font") or self.monospace_font
|
|
|
|
cre.setAsPreferredFontWithBias(monospace_font, 1 + 128*5 + 256*5, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setMonospaceFontScaling(value)
|
|
|
|
logger.dbg("CreDocument: set monospace font scaling", value)
|
|
|
|
self._document:setIntProperty("font.monospace.size.scale.percent", value or 100)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setAdjustedFallbackFontSizes(toggle)
|
|
|
|
logger.dbg("CreDocument: set adjusted fallback font sizes", toggle)
|
|
|
|
self._document:setIntProperty("crengine.font.fallback.sizes.adjusted", toggle and 1 or 0)
|
|
|
|
end
|
|
|
|
|
2020-04-25 18:20:41 +00:00
|
|
|
function CreDocument:setupFallbackFontFaces()
|
|
|
|
local fallbacks = {}
|
|
|
|
local seen_fonts = {}
|
|
|
|
local user_fallback = G_reader_settings:readSetting("fallback_font")
|
|
|
|
if user_fallback then
|
|
|
|
table.insert(fallbacks, user_fallback)
|
|
|
|
seen_fonts[user_fallback] = true
|
|
|
|
end
|
|
|
|
for _, font_name in pairs(self.fallback_fonts) do
|
|
|
|
if not seen_fonts[font_name] then
|
|
|
|
table.insert(fallbacks, font_name)
|
|
|
|
seen_fonts[font_name] = true
|
2018-05-11 15:48:26 +00:00
|
|
|
end
|
|
|
|
end
|
2020-04-28 21:57:10 +00:00
|
|
|
if G_reader_settings:isFalse("additional_fallback_fonts") then
|
|
|
|
-- Keep the first fallback font (user set or first from self.fallback_fonts),
|
|
|
|
-- as crengine won't reset its current set when provided with an empty string
|
|
|
|
for i=#fallbacks, 2, -1 do
|
|
|
|
table.remove(fallbacks, i)
|
|
|
|
end
|
|
|
|
end
|
2020-04-25 18:20:41 +00:00
|
|
|
-- We use '|' as the delimiter (which is less likely to be found in font
|
|
|
|
-- names than ',' or ';', without the need to have to use quotes.
|
|
|
|
local s_fallbacks = table.concat(fallbacks, "|")
|
|
|
|
logger.dbg("CreDocument: set fallback font faces:", s_fallbacks)
|
2021-03-30 15:25:13 +00:00
|
|
|
self._document:setStringProperty("crengine.font.fallback.faces", s_fallbacks)
|
2018-05-11 15:48:26 +00:00
|
|
|
end
|
|
|
|
|
2022-10-03 15:28:07 +00:00
|
|
|
function CreDocument:setFontFamilyFontFaces(font_family_fonts, ignore_font_names)
|
|
|
|
if not font_family_fonts then
|
|
|
|
font_family_fonts = {}
|
|
|
|
end
|
|
|
|
-- crengine expects font names concatenated in the order they appear in the
|
|
|
|
-- enum css_font_family_t (include/cssdef.h) (with css_ff_inherit ignored,
|
|
|
|
-- the first slot carries ignore_font_names if not empty
|
|
|
|
local fonts = {}
|
|
|
|
table.insert(fonts, ignore_font_names and "not-empty" or "")
|
|
|
|
table.insert(fonts, font_family_fonts["serif"] or "")
|
|
|
|
table.insert(fonts, font_family_fonts["sans-serif"] or "")
|
|
|
|
table.insert(fonts, font_family_fonts["cursive"] or "")
|
|
|
|
table.insert(fonts, font_family_fonts["fantasy"] or "")
|
|
|
|
table.insert(fonts, font_family_fonts["monospace"] or "")
|
|
|
|
table.insert(fonts, font_family_fonts["math"] or "")
|
|
|
|
table.insert(fonts, font_family_fonts["emoji"] or "")
|
|
|
|
table.insert(fonts, font_family_fonts["fangsong"] or "")
|
|
|
|
local s_font_family_faces = table.concat(fonts, "|")
|
|
|
|
logger.dbg("CreDocument: set font-family font faces:", s_font_family_faces)
|
|
|
|
self._document:setStringProperty("crengine.font.family.faces", s_font_family_faces)
|
|
|
|
end
|
|
|
|
|
2020-04-21 19:30:16 +00:00
|
|
|
-- To use the new crengine language typography facilities (hyphenation, line breaking,
|
|
|
|
-- OpenType fonts locl letter forms...)
|
|
|
|
function CreDocument:setTextMainLang(lang)
|
|
|
|
if lang then
|
|
|
|
logger.dbg("CreDocument: set textlang main lang", lang)
|
|
|
|
self._document:setStringProperty("crengine.textlang.main.lang", lang)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setTextEmbeddedLangs(toggle)
|
|
|
|
logger.dbg("CreDocument: set textlang embedded langs", toggle)
|
|
|
|
self._document:setStringProperty("crengine.textlang.embedded.langs.enabled", toggle and 1 or 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setTextHyphenation(toggle)
|
|
|
|
logger.dbg("CreDocument: set textlang hyphenation enabled", toggle)
|
|
|
|
self._document:setStringProperty("crengine.textlang.hyphenation.enabled", toggle and 1 or 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setTextHyphenationSoftHyphensOnly(toggle)
|
|
|
|
logger.dbg("CreDocument: set textlang hyphenation soft hyphens only", toggle)
|
|
|
|
self._document:setStringProperty("crengine.textlang.hyphenation.soft.hyphens.only", toggle and 1 or 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setTextHyphenationForceAlgorithmic(toggle)
|
|
|
|
logger.dbg("CreDocument: set textlang hyphenation force algorithmic", toggle)
|
|
|
|
self._document:setStringProperty("crengine.textlang.hyphenation.force.algorithmic", toggle and 1 or 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getTextMainLangDefaultHyphDictionary()
|
|
|
|
local main_lang_tag, main_lang_active_hyph_dict, loaded_lang_infos = cre.getTextLangStatus() -- luacheck: no unused
|
|
|
|
return loaded_lang_infos[main_lang_tag] and loaded_lang_infos[main_lang_tag].hyph_dict_name
|
|
|
|
end
|
|
|
|
|
|
|
|
-- To use the old crengine hyphenation manager (only one global hyphenation method)
|
2014-11-15 11:22:47 +00:00
|
|
|
function CreDocument:setHyphDictionary(new_hyph_dictionary)
|
|
|
|
if new_hyph_dictionary then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set hyphenation dictionary", new_hyph_dictionary)
|
2014-11-15 11:22:47 +00:00
|
|
|
self._document:setStringProperty("crengine.hyphenation.directory", new_hyph_dictionary)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-19 12:24:04 +00:00
|
|
|
function CreDocument:setHyphLeftHyphenMin(value)
|
|
|
|
-- default crengine value is 2: reset it if no value provided
|
|
|
|
logger.dbg("CreDocument: set hyphenation left hyphen min", value or 2)
|
|
|
|
self._document:setIntProperty("crengine.hyphenation.left.hyphen.min", value or 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setHyphRightHyphenMin(value)
|
|
|
|
logger.dbg("CreDocument: set hyphenation right hyphen min", value or 2)
|
|
|
|
self._document:setIntProperty("crengine.hyphenation.right.hyphen.min", value or 2)
|
|
|
|
end
|
|
|
|
|
2018-06-28 21:27:03 +00:00
|
|
|
function CreDocument:setTrustSoftHyphens(toggle)
|
|
|
|
logger.dbg("CreDocument: set hyphenation trust soft hyphens", toggle and 1 or 0)
|
|
|
|
self._document:setIntProperty("crengine.hyphenation.trust.soft.hyphens", toggle and 1 or 0)
|
|
|
|
end
|
|
|
|
|
2018-07-03 05:05:57 +00:00
|
|
|
function CreDocument:setRenderDPI(value)
|
|
|
|
-- set DPI used for scaling css units (with 96, 1 css px = 1 screen px)
|
|
|
|
-- it can be different from KOReader screen DPI
|
|
|
|
-- it has no relation to the default fontsize (which is already
|
|
|
|
-- scaleBySize()'d when provided to crengine)
|
|
|
|
logger.dbg("CreDocument: set render dpi", value or 96)
|
|
|
|
self._document:setIntProperty("crengine.render.dpi", value or 96)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setRenderScaleFontWithDPI(toggle)
|
|
|
|
-- wheter to scale font with DPI, or keep the current size
|
|
|
|
logger.dbg("CreDocument: set render scale font with dpi", toggle)
|
|
|
|
self._document:setIntProperty("crengine.render.scale.font.with.dpi", toggle)
|
|
|
|
end
|
|
|
|
|
2014-01-22 09:16:37 +00:00
|
|
|
function CreDocument:clearSelection()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("clear selection")
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:clearSelection()
|
2014-01-22 09:16:37 +00:00
|
|
|
end
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function CreDocument:getFontSize()
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:getFontSize()
|
2012-06-12 12:58:15 +00:00
|
|
|
end
|
|
|
|
|
2012-12-17 06:50:50 +00:00
|
|
|
function CreDocument:setFontSize(new_font_size)
|
2014-03-13 13:52:43 +00:00
|
|
|
if new_font_size then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set font size", new_font_size)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:setFontSize(new_font_size)
|
|
|
|
end
|
2012-12-17 06:50:50 +00:00
|
|
|
end
|
|
|
|
|
2013-01-07 11:53:35 +00:00
|
|
|
function CreDocument:setViewMode(new_mode)
|
2014-03-13 13:52:43 +00:00
|
|
|
if new_mode then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set view mode", new_mode)
|
2014-03-13 13:52:43 +00:00
|
|
|
if new_mode == "scroll" then
|
2019-02-03 08:58:48 +00:00
|
|
|
self._view_mode = self.SCROLL_VIEW_MODE
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
2019-02-03 08:58:48 +00:00
|
|
|
self._view_mode = self.PAGE_VIEW_MODE
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2019-02-03 08:58:48 +00:00
|
|
|
self._document:setViewMode(self._view_mode)
|
2020-10-31 09:40:36 +00:00
|
|
|
if self.hide_nonlinear_flows then
|
|
|
|
self:cacheFlows()
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-01-07 11:53:35 +00:00
|
|
|
end
|
|
|
|
|
2014-10-10 10:12:25 +00:00
|
|
|
function CreDocument:setViewDimen(dimen)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set view dimen", dimen)
|
2014-10-10 10:12:25 +00:00
|
|
|
self._document:setViewDimen(dimen.w, dimen.h)
|
|
|
|
end
|
|
|
|
|
2020-06-05 08:09:44 +00:00
|
|
|
function CreDocument:setHeaderProgressMarks(pages, ticks)
|
|
|
|
self._document:setHeaderProgressMarks(pages, ticks)
|
|
|
|
end
|
|
|
|
|
2013-01-13 03:23:30 +00:00
|
|
|
function CreDocument:setHeaderFont(new_font)
|
2014-03-13 13:52:43 +00:00
|
|
|
if new_font then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set header font", new_font)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:setHeaderFont(new_font)
|
|
|
|
end
|
2013-01-13 03:23:30 +00:00
|
|
|
end
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function CreDocument:zoomFont(delta)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: zoom font", delta)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:zoomFont(delta)
|
2012-06-12 12:58:15 +00:00
|
|
|
end
|
2012-06-11 15:50:11 +00:00
|
|
|
|
2012-06-12 13:12:04 +00:00
|
|
|
function CreDocument:setInterlineSpacePercent(percent)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set interline space", percent)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:setDefaultInterlineSpace(percent)
|
2012-06-12 13:12:04 +00:00
|
|
|
end
|
|
|
|
|
2021-04-28 23:37:53 +00:00
|
|
|
function CreDocument:setFontBaseWeight(weight)
|
|
|
|
-- In frontend, we use: 0, 1, -0.5, a delta from the regular weight of 400.
|
|
|
|
-- crengine expects for these: 400, 500, 350
|
|
|
|
local cre_weight = math.floor(400 + weight*100)
|
|
|
|
logger.dbg("CreDocument: set font base weight", weight, "=", cre_weight)
|
|
|
|
self._document:setIntProperty("font.face.base.weight", cre_weight)
|
2013-01-07 17:16:17 +00:00
|
|
|
end
|
|
|
|
|
2018-03-11 13:54:17 +00:00
|
|
|
function CreDocument:getGammaLevel()
|
|
|
|
return cre.getGammaLevel()
|
|
|
|
end
|
|
|
|
|
2013-01-13 03:49:01 +00:00
|
|
|
function CreDocument:setGammaIndex(index)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set gamma index", index)
|
2014-03-13 13:52:43 +00:00
|
|
|
cre.setGammaIndex(index)
|
2013-01-13 03:49:01 +00:00
|
|
|
end
|
|
|
|
|
2017-09-08 15:38:51 +00:00
|
|
|
function CreDocument:setFontHinting(mode)
|
|
|
|
logger.dbg("CreDocument: set font hinting mode", mode)
|
|
|
|
self._document:setIntProperty("font.hinting.mode", mode)
|
|
|
|
end
|
|
|
|
|
2018-08-17 21:08:21 +00:00
|
|
|
function CreDocument:setFontKerning(mode)
|
|
|
|
logger.dbg("CreDocument: set font kerning mode", mode)
|
|
|
|
self._document:setIntProperty("font.kerning.mode", mode)
|
|
|
|
end
|
|
|
|
|
2019-11-28 21:39:06 +00:00
|
|
|
function CreDocument:setWordSpacing(values)
|
|
|
|
-- values should be a table of 2 numbers (e.g.: { 90, 75 })
|
|
|
|
-- - space width scale percent (hard scale the width of each space char in
|
|
|
|
-- all fonts - 100 to use the normal font space glyph width unchanged).
|
|
|
|
-- - min space condensing percent (how much we can additionally decrease
|
|
|
|
-- a space width to make text fit on a line).
|
|
|
|
logger.dbg("CreDocument: set space width scale", values[1])
|
|
|
|
self._document:setIntProperty("crengine.style.space.width.scale.percent", values[1])
|
|
|
|
logger.dbg("CreDocument: set space condensing", values[2])
|
|
|
|
self._document:setIntProperty("crengine.style.space.condensing.percent", values[2])
|
2018-06-30 14:55:43 +00:00
|
|
|
end
|
|
|
|
|
2020-05-03 16:06:58 +00:00
|
|
|
function CreDocument:setWordExpansion(value)
|
|
|
|
logger.dbg("CreDocument: set word expansion", value)
|
|
|
|
self._document:setIntProperty("crengine.style.max.added.letter.spacing.percent", value or 0)
|
|
|
|
end
|
|
|
|
|
2022-06-05 10:36:33 +00:00
|
|
|
function CreDocument:setCJKWidthScaling(value)
|
|
|
|
logger.dbg("CreDocument: set cjk width scaling", value)
|
|
|
|
self._document:setIntProperty("crengine.style.cjk.width.scale.percent", value or 100)
|
|
|
|
end
|
|
|
|
|
2018-05-12 21:24:43 +00:00
|
|
|
function CreDocument:setStyleSheet(new_css_file, appended_css_content )
|
|
|
|
logger.dbg("CreDocument: set style sheet:",
|
|
|
|
new_css_file and new_css_file or "no file",
|
|
|
|
appended_css_content and "and appended content ("..#appended_css_content.." bytes)" or "(no appended content)")
|
|
|
|
self._document:setStyleSheet(new_css_file, appended_css_content)
|
2013-01-21 22:27:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:setEmbeddedStyleSheet(toggle)
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @fixme occasional segmentation fault when switching embedded style sheet
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set embedded style sheet", toggle)
|
2014-05-12 07:47:11 +00:00
|
|
|
self._document:setIntProperty("crengine.doc.embedded.styles.enabled", toggle)
|
2013-01-17 21:59:40 +00:00
|
|
|
end
|
|
|
|
|
2017-08-30 07:04:04 +00:00
|
|
|
function CreDocument:setEmbeddedFonts(toggle)
|
|
|
|
logger.dbg("CreDocument: set embedded fonts", toggle)
|
|
|
|
self._document:setIntProperty("crengine.doc.embedded.fonts.enabled", toggle)
|
|
|
|
end
|
|
|
|
|
2013-06-28 09:12:04 +00:00
|
|
|
function CreDocument:setPageMargins(left, top, right, bottom)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set page margins", left, top, right, bottom)
|
2014-05-12 07:47:11 +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
|
|
|
|
|
2019-07-14 20:13:05 +00:00
|
|
|
function CreDocument:setBlockRenderingFlags(flags)
|
|
|
|
logger.dbg("CreDocument: set block rendering flags", string.format("0x%x", flags))
|
|
|
|
self._document:setIntProperty("crengine.render.block.rendering.flags", flags)
|
|
|
|
end
|
|
|
|
|
2019-04-18 21:26:53 +00:00
|
|
|
function CreDocument:setImageScaling(toggle)
|
|
|
|
logger.dbg("CreDocument: set smooth scaling", toggle)
|
|
|
|
self._smooth_scaling = toggle
|
|
|
|
end
|
|
|
|
|
2019-06-25 22:23:41 +00:00
|
|
|
function CreDocument:setNightmodeImages(toggle)
|
|
|
|
logger.dbg("CreDocument: set nightmode images", toggle)
|
|
|
|
self._nightmode_images = toggle
|
|
|
|
end
|
|
|
|
|
2014-01-02 17:59:40 +00:00
|
|
|
function CreDocument:setFloatingPunctuation(enabled)
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @fixme occasional segmentation fault when toggling floating punctuation
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set floating punctuation", enabled)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:setIntProperty("crengine.style.floating.punctuation.enabled", enabled)
|
2014-01-02 17:59:40 +00:00
|
|
|
end
|
|
|
|
|
2017-04-01 14:34:00 +00:00
|
|
|
function CreDocument:setTxtPreFormatted(enabled)
|
|
|
|
logger.dbg("CreDocument: set txt preformatted", enabled)
|
|
|
|
self._document:setIntProperty("crengine.file.txt.preformatted", enabled)
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:02:19 +00:00
|
|
|
-- get crengine internal visible page count (to be used when doing specific
|
|
|
|
-- screen position handling)
|
2014-07-03 09:54:30 +00:00
|
|
|
function CreDocument:getVisiblePageCount()
|
|
|
|
return self._document:getVisiblePageCount()
|
|
|
|
end
|
|
|
|
|
2013-07-28 06:35:46 +00:00
|
|
|
function CreDocument:setVisiblePageCount(new_count)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set visible page count", new_count)
|
2021-01-13 19:26:14 +00:00
|
|
|
self._document:setVisiblePageCount(new_count, false)
|
2013-07-28 06:35:46 +00:00
|
|
|
end
|
|
|
|
|
2021-01-29 00:02:19 +00:00
|
|
|
-- get visible page number count (to be used when only interested in page numbers)
|
|
|
|
function CreDocument:getVisiblePageNumberCount()
|
|
|
|
return self._document:getVisiblePageNumberCount()
|
|
|
|
end
|
|
|
|
|
2014-01-04 14:30:36 +00:00
|
|
|
function CreDocument:setBatteryState(state)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set battery state", state)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:setBatteryState(state)
|
2014-01-04 14:30:36 +00:00
|
|
|
end
|
|
|
|
|
2014-01-18 11:18:12 +00:00
|
|
|
function CreDocument:isXPointerInCurrentPage(xp)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: check xpointer in current page", xp)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self._document:isXPointerInCurrentPage(xp)
|
2014-01-18 11:18:12 +00:00
|
|
|
end
|
|
|
|
|
2014-02-12 07:26:56 +00:00
|
|
|
function CreDocument:setStatusLineProp(prop)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("CreDocument: set status line property", prop)
|
2014-03-13 13:52:43 +00:00
|
|
|
self._document:setStringProperty("window.status.line", prop)
|
2014-02-12 07:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-05-25 20:39:59 +00:00
|
|
|
function CreDocument:setBackgroundColor(bgcolor) -- use nil to set to white
|
|
|
|
logger.dbg("CreDocument: set background color", bgcolor)
|
|
|
|
self._document:setBackgroundColor(bgcolor)
|
|
|
|
end
|
|
|
|
|
2019-08-17 17:10:54 +00:00
|
|
|
function CreDocument:setBackgroundImage(img_path) -- use nil to unset
|
|
|
|
logger.dbg("CreDocument: set background image", img_path)
|
|
|
|
self._document:setBackgroundImage(img_path)
|
|
|
|
end
|
|
|
|
|
2021-07-08 17:30:16 +00:00
|
|
|
function CreDocument:checkRegex(pattern)
|
|
|
|
logger.dbg("CreDocument: check regex ", pattern)
|
|
|
|
return self._document:checkRegex(pattern)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getAndClearRegexSearchError()
|
2021-07-08 21:30:37 +00:00
|
|
|
local retval = self._document:getAndClearRegexSearchError()
|
2021-07-08 17:30:16 +00:00
|
|
|
logger.dbg("CreDocument: getAndClearRegexSearchError", retval)
|
|
|
|
return retval
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:findText(pattern, origin, reverse, caseInsensitive, page, regex, max_hits)
|
|
|
|
logger.dbg("CreDocument: find text", pattern, origin, reverse, caseInsensitive, regex, max_hits)
|
2016-04-19 06:50:36 +00:00
|
|
|
return self._document:findText(
|
2021-07-08 17:30:16 +00:00
|
|
|
pattern, origin, reverse, caseInsensitive and 1 or 0, regex and 1 or 0, max_hits or 200)
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
2018-03-12 19:46:40 +00:00
|
|
|
function CreDocument:enableInternalHistory(toggle)
|
|
|
|
-- Setting this to 0 unsets crengine internal bookmarks highlighting,
|
|
|
|
-- and as a side effect, disable internal history and the need to build
|
|
|
|
-- a bookmark at each page turn: this speeds up a lot page turning
|
|
|
|
-- and menu opening on big books.
|
|
|
|
-- It has to be called late in the document opening process, and setting
|
|
|
|
-- it to false needs to be followed by a redraw.
|
|
|
|
-- It needs to be temporarily re-enabled on page resize for crengine to
|
|
|
|
-- keep track of position in page and restore it after resize.
|
|
|
|
logger.dbg("CreDocument: set bookmarks highlight and internal history", toggle)
|
|
|
|
self._document:setIntProperty("crengine.highlight.bookmarks", toggle and 2 or 0)
|
|
|
|
end
|
|
|
|
|
2019-09-21 16:59:05 +00:00
|
|
|
function CreDocument:setCallback(func)
|
|
|
|
return self._document:setCallback(func)
|
|
|
|
end
|
|
|
|
|
ReaderRolling: quicker partial rerenderings with EPUBs
Only available with EPUBs containing 2 or more fragments,
and a file size large enough to ensure a cache file is used.
The idea is simply, on any rendering setting change, to
skip the rerendering of the full book and to defer any
rerendering to the moment we draw a DocFragment, and
render only it.
So, on a setting change, only the fragment containing the
current page will be rerendered, and the new fragments we
may cross while turning pages.
When having done so, KOReader is in a degraded state (the
full page count is incorrect, the ToC is invalid...).
So, a full rerendering is needed, and one will happen
in the background, and when the user is idle, we reload
seamlessly and quickly from the cache file it has made.
ReaderFlipping will show some icons in the top left
corner to let it know at which steps in this procress
we are.
2023-02-16 22:30:58 +00:00
|
|
|
function CreDocument:canBePartiallyRerendered()
|
|
|
|
return self._document:canBePartiallyRerendered()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:isPartialRerenderingEnabled()
|
|
|
|
return self._document:isPartialRerenderingEnabled()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:enablePartialRerendering(enable)
|
|
|
|
return self._document:enablePartialRerendering(enable)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPartialRerenderingsCount()
|
|
|
|
return self._document:getPartialRerenderingsCount()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:isRerenderingDelayed()
|
|
|
|
return self._document:isRerenderingDelayed()
|
|
|
|
end
|
|
|
|
|
2018-05-20 23:19:36 +00:00
|
|
|
function CreDocument:isBuiltDomStale()
|
|
|
|
return self._document:isBuiltDomStale()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:hasCacheFile()
|
|
|
|
return self._document:hasCacheFile()
|
|
|
|
end
|
|
|
|
|
ReaderRolling: quicker partial rerenderings with EPUBs
Only available with EPUBs containing 2 or more fragments,
and a file size large enough to ensure a cache file is used.
The idea is simply, on any rendering setting change, to
skip the rerendering of the full book and to defer any
rerendering to the moment we draw a DocFragment, and
render only it.
So, on a setting change, only the fragment containing the
current page will be rerendered, and the new fragments we
may cross while turning pages.
When having done so, KOReader is in a degraded state (the
full page count is incorrect, the ToC is invalid...).
So, a full rerendering is needed, and one will happen
in the background, and when the user is idle, we reload
seamlessly and quickly from the cache file it has made.
ReaderFlipping will show some icons in the top left
corner to let it know at which steps in this procress
we are.
2023-02-16 22:30:58 +00:00
|
|
|
function CreDocument:isCacheFileStale()
|
|
|
|
return self._document:isCacheFileStale()
|
|
|
|
end
|
|
|
|
|
2018-05-20 23:19:36 +00:00
|
|
|
function CreDocument:invalidateCacheFile()
|
|
|
|
self._document:invalidateCacheFile()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getCacheFilePath()
|
|
|
|
return self._document:getCacheFilePath()
|
|
|
|
end
|
|
|
|
|
2019-09-21 16:59:05 +00:00
|
|
|
function CreDocument:getStatistics()
|
|
|
|
return self._document:getStatistics()
|
|
|
|
end
|
|
|
|
|
2020-05-25 20:39:59 +00:00
|
|
|
function CreDocument:getUnknownEntities()
|
|
|
|
return self._document:getUnknownEntities()
|
|
|
|
end
|
|
|
|
|
2018-06-13 22:46:52 +00:00
|
|
|
function CreDocument:canHaveAlternativeToc()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:isTocAlternativeToc()
|
|
|
|
return self._document:isTocAlternativeToc()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:buildAlternativeToc()
|
|
|
|
self._document:buildAlternativeToc()
|
|
|
|
end
|
|
|
|
|
2022-05-06 12:38:04 +00:00
|
|
|
function CreDocument:buildSyntheticPageMapIfNoneDocumentProvided(chars_per_synthetic_page)
|
|
|
|
self._document:buildSyntheticPageMapIfNoneDocumentProvided(chars_per_synthetic_page or 1024)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:isPageMapSynthetic()
|
|
|
|
return self._document:isPageMapSynthetic()
|
|
|
|
end
|
|
|
|
|
2020-03-26 13:04:59 +00:00
|
|
|
function CreDocument:hasPageMap()
|
|
|
|
return self._document:hasPageMap()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageMap()
|
|
|
|
return self._document:getPageMap()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageMapSource()
|
|
|
|
return self._document:getPageMapSource()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageMapCurrentPageLabel()
|
|
|
|
return self._document:getPageMapCurrentPageLabel()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageMapFirstPageLabel()
|
|
|
|
return self._document:getPageMapFirstPageLabel()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageMapLastPageLabel()
|
|
|
|
return self._document:getPageMapLastPageLabel()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageMapXPointerPageLabel(xp)
|
|
|
|
return self._document:getPageMapXPointerPageLabel(xp)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CreDocument:getPageMapVisiblePageLabels()
|
|
|
|
return self._document:getPageMapVisiblePageLabels()
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
function CreDocument: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("azw", "application/vnd.amazon.mobi8-ebook", self, 90)
|
2021-02-07 18:11:45 +00:00
|
|
|
registry:addProvider("azw", "application/x-mobi8-ebook", self, 90) -- Alternative mimetype for OPDS.
|
[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("chm", "application/vnd.ms-htmlhelp", self, 90)
|
|
|
|
registry:addProvider("doc", "application/msword", self, 90)
|
2019-10-17 13:17:35 +00:00
|
|
|
registry:addProvider("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", self, 90)
|
[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("epub", "application/epub+zip", self, 100)
|
2023-06-13 10:16:36 +00:00
|
|
|
registry:addProvider("epub", "application/epub", self, 100) -- Alternative mimetype for OPDS.
|
2020-03-26 13:04:59 +00:00
|
|
|
registry:addProvider("epub3", "application/epub+zip", self, 100)
|
[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("fb2", "application/fb2", self, 90)
|
2021-07-04 20:03:20 +00:00
|
|
|
registry:addProvider("fb2", "text/fb2+xml", self, 90) -- Alternative mimetype for OPDS.
|
[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("fb2.zip", "application/zip", self, 90)
|
2021-02-07 18:11:45 +00:00
|
|
|
registry:addProvider("fb2.zip", "application/fb2+zip", self, 90) -- Alternative mimetype for OPDS.
|
2019-10-17 13:17:35 +00:00
|
|
|
registry:addProvider("fb3", "application/fb3", self, 90)
|
[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("htm", "text/html", self, 100)
|
|
|
|
registry:addProvider("html", "text/html", self, 100)
|
|
|
|
registry:addProvider("htm.zip", "application/zip", self, 100)
|
|
|
|
registry:addProvider("html.zip", "application/zip", self, 100)
|
2021-12-07 13:06:37 +00:00
|
|
|
registry:addProvider("html.zip", "application/html+zip", self, 100) -- Alternative mimetype for OPDS.
|
2017-04-02 13:11:16 +00:00
|
|
|
registry:addProvider("log", "text/plain", self)
|
|
|
|
registry:addProvider("log.zip", "application/zip", self)
|
|
|
|
registry:addProvider("md", "text/plain", self)
|
|
|
|
registry:addProvider("md.zip", "application/zip", self)
|
[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("mobi", "application/x-mobipocket-ebook", self, 90)
|
bump crengine: ODT support, more typography languages (#6675)
Includes:
- Update German hyphenation patterns
- (Upstream) Adds ODT (ODF) format support
- TextLang, hyphenation: add Basque, Croatian, Esperanto,
Estonian, Georgian, Latvian, Lithuanian, Macedonian,
Occitan, Welsh; update Bulgarian, Irish, Portuguese,
Slovak, Dutch, Norwegian, Spanish; update hyphen min
for Czech, English, Greek; fix Romanian and Ukrainian
pattern file names
- HyphMan: adds HyphMethod::getLeft/RightHyphenMin()
- epub.css: update HR default style
- fb2.css: keep <date> in main text left-aligned
- getRenderedWidths(): inline-block and table fixes
- CSS: avoid style hash mismatch when serializing content:''
- Tables: re-order row groups when necessary
- XML parsing: don't drop trailing text
- HTML parser: tweak implicit head/body insertion code
- Fix text search failure when blank at start or end
readertypography.lua: some cleanup by removing the first
table listing hyph dicts, and including their filenames
in the second table so we can build the first table
dynamically. Also fetch hyph left/right min limits from
crengine to not have to duplicate them here and keep them
in sync.
css_tweaks.lua: for in-page footnotes, switched from
"-cr-only-if: epub-document" to ": -fb2-document"
("not" FB2 documents), so these tweaks also work with
DocX and ODT footnotes.
Also included in base bump:
- KOPTContext: add comic panel detection function/additional
leptonica declarations
- Forcibly disable docs when building giflib
2020-09-18 17:34:46 +00:00
|
|
|
registry:addProvider("odt", "application/vnd.oasis.opendocument.text ", self, 90)
|
[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
|
|
|
-- Palmpilot Document File
|
|
|
|
registry:addProvider("pdb", "application/vnd.palm", self, 90)
|
|
|
|
-- Palmpilot Resource File
|
|
|
|
registry:addProvider("prc", "application/vnd.palm", self)
|
2021-12-07 13:06:37 +00:00
|
|
|
registry:addProvider("rtf", "application/rtf", self, 90)
|
|
|
|
registry:addProvider("rtf.zip", "application/rtf+zip", self, 90) -- Alternative mimetype for OPDS.
|
2022-09-11 17:53:07 +00:00
|
|
|
registry:addProvider("svg", "image/svg+xml", self, 90)
|
2014-03-13 13:52:43 +00:00
|
|
|
registry:addProvider("tcr", "application/tcr", self)
|
[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("txt", "text/plain", self, 90)
|
|
|
|
registry:addProvider("txt.zip", "application/zip", self, 90)
|
2021-12-07 13:06:37 +00:00
|
|
|
registry:addProvider("txt.zip", "application/txt+zip", self, 90) -- Alternative mimetype for OPDS.
|
2020-10-03 13:07:07 +00:00
|
|
|
registry:addProvider("xhtml", "application/xhtml+xml", self, 100)
|
|
|
|
registry:addProvider("xml", "application/xml", self, 90)
|
[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("zip", "application/zip", self, 10)
|
2020-07-09 16:11:56 +00:00
|
|
|
-- Scripts that we allow running in the FM (c.f., Device:canExecuteScript)
|
2020-02-03 19:08:18 +00:00
|
|
|
registry:addProvider("sh", "application/x-shellscript", self, 90)
|
|
|
|
registry:addProvider("py", "text/x-python", self, 90)
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
2020-11-08 21:55:11 +00:00
|
|
|
-- no-op that will be wrapped by setupCallCache
|
|
|
|
function CreDocument:resetCallCache() end
|
|
|
|
|
2021-01-29 17:39:08 +00:00
|
|
|
-- no-op that will be wrapped by setupCallCache
|
|
|
|
function CreDocument:resetBufferCache() end
|
|
|
|
|
2020-01-16 11:48:34 +00:00
|
|
|
-- Optimise usage of some of the above methods by caching their results,
|
|
|
|
-- either globally, or per page/pos for those whose result may depend on
|
|
|
|
-- current page number or y-position.
|
|
|
|
function CreDocument:setupCallCache()
|
2020-01-30 11:25:50 +00:00
|
|
|
if not G_reader_settings:nilOrTrue("use_cre_call_cache") then
|
|
|
|
logger.dbg("CreDocument: not using cre call cache")
|
|
|
|
return
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
2020-01-30 11:25:50 +00:00
|
|
|
logger.dbg("CreDocument: using cre call cache")
|
2020-01-16 11:48:34 +00:00
|
|
|
local do_stats = G_reader_settings:isTrue("use_cre_call_cache_log_stats")
|
|
|
|
-- Tune these when debugging
|
|
|
|
local do_stats_include_not_cached = false
|
|
|
|
local do_log = false
|
|
|
|
|
|
|
|
-- Beware below for luacheck warnings "shadowing upvalue argument 'self'":
|
|
|
|
-- the 'self' we got and use here, and the one we may get implicitely
|
|
|
|
-- as first parameter of the methods we define or redefine, are actually
|
|
|
|
-- the same, but luacheck doesn't know that and would logically complain.
|
|
|
|
-- So, we define our helpers (self._callCache*) as functions and not methods:
|
|
|
|
-- no 'self' as first argument, use 'self.' and not 'self:' when calling them.
|
|
|
|
|
|
|
|
-- reset full cache
|
|
|
|
self._callCacheReset = function()
|
2021-05-05 20:48:10 +00:00
|
|
|
-- "Global" cache, a simple key, value store for *this* document
|
|
|
|
self._global_call_cache = {}
|
|
|
|
-- "Tags" cache, an LRU cache of per-tag simple key, value stores. 10 slots.
|
|
|
|
if self._tag_list_call_cache then
|
|
|
|
self._tag_list_call_cache:clear()
|
|
|
|
else
|
2021-05-07 01:59:27 +00:00
|
|
|
self._tag_list_call_cache = lru.new(10, nil, false)
|
2021-05-05 20:48:10 +00:00
|
|
|
end
|
|
|
|
-- i.e., the only thing that follows any sort of LRU eviction logic is the *list* of tag caches.
|
|
|
|
-- Each individual cache itself is just a simple key, value store (i.e., a hash map).
|
|
|
|
-- Points to said per-tag cache for the current tag.
|
|
|
|
self._tag_call_cache = nil
|
|
|
|
-- Stores the key for said current tag
|
|
|
|
self._current_call_cache_tag = nil
|
|
|
|
end
|
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
|
|
|
--[[
|
|
|
|
--- @note: If explicitly destroying the references to the caches is necessary to get their content collected by the GC,
|
|
|
|
--- then you have a ref leak to this Document instance somewhere,
|
|
|
|
--- c.f., https://github.com/koreader/koreader/pull/7634#discussion_r627820424
|
|
|
|
--- Keep in mind that a *lot* of ReaderUI modules keep a ref to document, so it may be fairly remote!
|
|
|
|
--- A good contender for issues like these would be references being stored in the class object instead
|
|
|
|
--- of in instance objects because of inheritance rules. c.f., https://github.com/koreader/koreader/pull/9586,
|
|
|
|
--- which got rid of a giant leak of every ReaderUI module via the active_widgets array...
|
|
|
|
--- A simple testing methodology is to create a dummy buffer alongside self.buffer in drawCurrentView,
|
|
|
|
--- push it to the global cache via _callCacheSet, and trace the BlitBuffer gc method in ffi/blitbuffer.lua.
|
|
|
|
--- You'll probably want to stick a pair of collectgarbage() calls somewhere in the UI loop
|
|
|
|
--- (e.g., at the coda of UIManager:close) to trip a GC pass earlier than the one in DocumentRegistry:openDocument.
|
|
|
|
--- (Keep in mind that, in UIManager:close, widget is still in scope inside the function,
|
|
|
|
--- so you'll have to close another widget to truly collect it (here, ReaderUI)).
|
2021-05-05 20:48:10 +00:00
|
|
|
self._callCacheDestroy = function()
|
|
|
|
self._global_call_cache = nil
|
|
|
|
self._tag_list_call_cache = nil
|
|
|
|
self._tag_call_cache = nil
|
|
|
|
self._current_call_cache_tag = nil
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
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
|
|
|
--]]
|
2020-01-16 11:48:34 +00:00
|
|
|
-- global cache
|
|
|
|
self._callCacheGet = function(key)
|
2021-05-05 20:48:10 +00:00
|
|
|
return self._global_call_cache[key]
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
|
|
|
self._callCacheSet = function(key, value)
|
2021-05-05 20:48:10 +00:00
|
|
|
self._global_call_cache[key] = value
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- current tag (page, pos) sub-cache
|
|
|
|
self._callCacheSetCurrentTag = function(tag)
|
2021-05-05 20:48:10 +00:00
|
|
|
-- If it already exists, return it and make it the MRU
|
|
|
|
self._tag_call_cache = self._tag_list_call_cache:get(tag)
|
|
|
|
if not self._tag_call_cache then
|
|
|
|
-- Otherwise, create it and insert it in the list cache, evicting the LRU tag cache if necessary.
|
2021-05-07 01:59:27 +00:00
|
|
|
self._tag_call_cache = {}
|
2021-05-05 20:48:10 +00:00
|
|
|
self._tag_list_call_cache:set(tag, self._tag_call_cache)
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
2021-05-05 20:48:10 +00:00
|
|
|
self._current_call_cache_tag = tag
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
|
|
|
self._callCacheGetCurrentTag = function(tag)
|
2021-05-05 20:48:10 +00:00
|
|
|
return self._current_call_cache_tag
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
|
|
|
-- per current tag cache
|
|
|
|
self._callCacheTagGet = function(key)
|
2021-05-05 20:48:10 +00:00
|
|
|
if self._tag_call_cache then
|
|
|
|
return self._tag_call_cache[key]
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
self._callCacheTagSet = function(key, value)
|
2021-05-05 20:48:10 +00:00
|
|
|
if self._tag_call_cache then
|
|
|
|
self._tag_call_cache[key] = value
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
self._callCacheReset()
|
|
|
|
|
|
|
|
local no_op = function() end
|
|
|
|
local addStatMiss = no_op
|
|
|
|
local addStatHit = no_op
|
|
|
|
local dumpStats = no_op
|
2021-05-05 20:48:10 +00:00
|
|
|
local now = no_op
|
2020-01-16 11:48:34 +00:00
|
|
|
if do_stats then
|
|
|
|
-- cache statistics
|
|
|
|
self._call_cache_stats = {}
|
2021-05-05 20:48:10 +00:00
|
|
|
now = function()
|
2022-05-05 19:00:22 +00:00
|
|
|
return time.now()
|
2021-05-05 20:48:10 +00:00
|
|
|
end
|
2020-01-16 11:48:34 +00:00
|
|
|
addStatMiss = function(name, starttime, not_cached)
|
2022-05-05 19:00:22 +00:00
|
|
|
local duration = time.since(starttime)
|
2020-01-16 11:48:34 +00:00
|
|
|
if not self._call_cache_stats[name] then
|
|
|
|
self._call_cache_stats[name] = {0, 0.0, 1, duration, not_cached}
|
|
|
|
else
|
|
|
|
local stat = self._call_cache_stats[name]
|
|
|
|
stat[3] = stat[3] + 1
|
|
|
|
stat[4] = stat[4] + duration
|
|
|
|
end
|
|
|
|
end
|
|
|
|
addStatHit = function(name, starttime)
|
2022-05-05 19:00:22 +00:00
|
|
|
local duration = time.since(starttime)
|
2020-01-16 11:48:34 +00:00
|
|
|
if not self._call_cache_stats[name] then
|
|
|
|
self._call_cache_stats[name] = {1, duration, 0, 0.0}
|
|
|
|
else
|
|
|
|
local stat = self._call_cache_stats[name]
|
|
|
|
stat[1] = stat[1] + 1
|
|
|
|
stat[2] = stat[2] + duration
|
|
|
|
end
|
|
|
|
end
|
|
|
|
dumpStats = function()
|
|
|
|
logger.info("cre call cache statistics:\n" .. self.getCallCacheStatistics())
|
|
|
|
end
|
|
|
|
-- Make this one non-local, in case we want to have it shown via a menu item
|
|
|
|
self.getCallCacheStatistics = function()
|
|
|
|
local util = require("util")
|
|
|
|
local res = {}
|
|
|
|
table.insert(res, "CRE call cache content:")
|
2021-05-05 20:48:10 +00:00
|
|
|
table.insert(res, string.format(" all: %d items", util.tableSize(self._global_call_cache) + self._tag_list_call_cache:used_slots()))
|
|
|
|
table.insert(res, string.format(" global: %d items", util.tableSize(self._global_call_cache)))
|
|
|
|
table.insert(res, string.format(" tags: %d items", self._tag_list_call_cache:used_slots()))
|
|
|
|
for tag, tag_cache in self._tag_list_call_cache:pairs() do
|
|
|
|
table.insert(res, string.format(" '%s': %d items", tag,
|
|
|
|
util.tableSize(tag_cache)))
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
|
|
|
local hit_keys = {}
|
|
|
|
local nohit_keys = {}
|
|
|
|
local notcached_keys = {}
|
|
|
|
for k, v in pairs(self._call_cache_stats) do
|
|
|
|
if self._call_cache_stats[k][1] > 0 then
|
|
|
|
table.insert(hit_keys, k)
|
|
|
|
else
|
|
|
|
if #v > 4 then
|
|
|
|
table.insert(notcached_keys, k)
|
|
|
|
else
|
|
|
|
table.insert(nohit_keys, k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(hit_keys)
|
|
|
|
table.sort(nohit_keys)
|
|
|
|
table.sort(notcached_keys)
|
|
|
|
table.insert(res, "CRE call cache hits statistics:")
|
|
|
|
local total_duration = 0
|
|
|
|
local total_duration_saved = 0
|
|
|
|
for _, k in ipairs(hit_keys) do
|
|
|
|
local hits, hits_duration, misses, missed_duration = unpack(self._call_cache_stats[k])
|
|
|
|
local total = hits + misses
|
|
|
|
local pct_hit = 100.0 * hits / total
|
|
|
|
local duration_avoided = 1.0 * hits * missed_duration / misses
|
|
|
|
local duration_added_s = ""
|
|
|
|
if hits_duration >= 0.001 then
|
|
|
|
duration_added_s = string.format(" (-%.3fs)", hits_duration)
|
|
|
|
end
|
|
|
|
local pct_duration_avoided = 100.0 * duration_avoided / (missed_duration + hits_duration + duration_avoided)
|
|
|
|
table.insert(res, string.format(" %s: %d/%d hits (%d%%) %.3fs%s saved, %.3fs used (%d%% saved)", k, hits, total,
|
|
|
|
pct_hit, duration_avoided, duration_added_s, missed_duration, pct_duration_avoided))
|
|
|
|
total_duration = total_duration + missed_duration + hits_duration
|
|
|
|
total_duration_saved = total_duration_saved + duration_avoided - hits_duration
|
|
|
|
end
|
|
|
|
table.insert(res, " By call times (hits | misses):")
|
|
|
|
for _, k in ipairs(hit_keys) do
|
|
|
|
local hits, hits_duration, misses, missed_duration = unpack(self._call_cache_stats[k])
|
|
|
|
table.insert(res, string.format(" %s: (%d) %.3f ms | %.3f ms (%d)", k, hits, 1000*hits_duration/hits, 1000*missed_duration/misses, misses))
|
|
|
|
end
|
|
|
|
table.insert(res, " No hit for:")
|
|
|
|
for _, k in ipairs(nohit_keys) do
|
|
|
|
local hits, hits_duration, misses, missed_duration = unpack(self._call_cache_stats[k]) -- luacheck: no unused
|
|
|
|
table.insert(res, string.format(" %s: %d misses %.3fs",
|
|
|
|
k, misses, missed_duration))
|
|
|
|
total_duration = total_duration + missed_duration + hits_duration
|
|
|
|
end
|
|
|
|
if #notcached_keys > 0 then
|
|
|
|
table.insert(res, " No cache for:")
|
|
|
|
for _, k in ipairs(notcached_keys) do
|
|
|
|
local hits, hits_duration, misses, missed_duration = unpack(self._call_cache_stats[k]) -- luacheck: no unused
|
|
|
|
table.insert(res, string.format(" %s: %d calls %.3fs",
|
|
|
|
k, misses, missed_duration))
|
|
|
|
total_duration = total_duration + missed_duration + hits_duration
|
|
|
|
end
|
|
|
|
end
|
2021-05-05 02:05:09 +00:00
|
|
|
local pct_duration_saved = 100.0 * total_duration_saved / (total_duration + total_duration_saved)
|
2020-01-16 11:48:34 +00:00
|
|
|
table.insert(res, string.format(" cpu time used: %.3fs, saved: %.3fs (%d%% saved)", total_duration, total_duration_saved, pct_duration_saved))
|
|
|
|
return table.concat(res, "\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Tweak CreDocument functions for cache interaction
|
|
|
|
-- No need to tweak metatable and play with __index, we just put
|
|
|
|
-- in self wrapped copies of the original CreDocument functions.
|
|
|
|
for name, func in pairs(CreDocument) do
|
|
|
|
if type(func) == "function" then
|
|
|
|
-- Various type of wrap
|
|
|
|
local no_wrap = false -- luacheck: no unused
|
|
|
|
local add_reset = false
|
|
|
|
local add_buffer_trash = false
|
|
|
|
local cache_by_tag = false
|
|
|
|
local cache_global = false
|
|
|
|
local set_tag = nil
|
|
|
|
local set_arg = nil
|
2021-01-29 00:02:19 +00:00
|
|
|
local set_arg2 = nil
|
2020-01-16 11:48:34 +00:00
|
|
|
local is_cached = false
|
|
|
|
|
|
|
|
-- Assume all set* may change rendering
|
|
|
|
if name == "setBatteryState" then no_wrap = true -- except this one
|
|
|
|
elseif name:sub(1,3) == "set" then add_reset = true
|
|
|
|
elseif name:sub(1,6) == "toggle" then add_reset = true
|
|
|
|
elseif name:sub(1,6) == "update" then add_reset = true
|
2020-01-30 11:25:50 +00:00
|
|
|
elseif name:sub(1,6) == "enable" then add_reset = true
|
2020-01-16 11:48:34 +00:00
|
|
|
elseif name == "zoomFont" then add_reset = true -- not used by koreader
|
2020-11-08 21:55:11 +00:00
|
|
|
elseif name == "resetCallCache" then add_reset = true
|
2020-10-31 09:40:36 +00:00
|
|
|
elseif name == "cacheFlows" then add_reset = true
|
2020-01-16 11:48:34 +00:00
|
|
|
|
|
|
|
-- These may have crengine do native highlight or unhighlight
|
|
|
|
-- (we could keep the original buffer and use a scratch buffer while
|
|
|
|
-- these are used, but not worth bothering)
|
|
|
|
elseif name == "clearSelection" then add_buffer_trash = true
|
|
|
|
elseif name == "highlightXPointer" then add_buffer_trash = true
|
|
|
|
elseif name == "getWordFromPosition" then add_buffer_trash = true
|
|
|
|
elseif name == "getTextFromPositions" then add_buffer_trash = true
|
2021-10-23 10:12:47 +00:00
|
|
|
elseif name == "getTextFromXPointers" then add_buffer_trash = true
|
2020-01-16 11:48:34 +00:00
|
|
|
elseif name == "findText" then add_buffer_trash = true
|
2021-01-29 17:39:08 +00:00
|
|
|
elseif name == "resetBufferCache" then add_buffer_trash = true
|
2020-01-16 11:48:34 +00:00
|
|
|
|
|
|
|
-- These may change page/pos
|
2021-01-29 00:02:19 +00:00
|
|
|
elseif name == "gotoPage" then set_tag = "page" ; set_arg = 2 ; set_arg2 = 3
|
2020-01-16 11:48:34 +00:00
|
|
|
elseif name == "gotoPos" then set_tag = "pos" ; set_arg = 2
|
|
|
|
elseif name == "drawCurrentViewByPos" then set_tag = "pos" ; set_arg = 6
|
2021-01-29 00:02:19 +00:00
|
|
|
-- elseif name == "drawCurrentViewByPage" then set_tag = "page" ; set_arg = 6
|
|
|
|
-- drawCurrentViewByPage() has some tweaks when browsing half-pages for
|
|
|
|
-- text selection in two-pages mode: no need to wrap it, as it uses
|
|
|
|
-- internally 2 other functions that are themselves wrapped
|
|
|
|
|
2020-01-16 11:48:34 +00:00
|
|
|
-- gotoXPointer() is for cre internal fixup, we always use gotoPage/Pos
|
|
|
|
-- (goBack, goForward, gotoLink are not used)
|
|
|
|
|
2020-10-31 09:20:02 +00:00
|
|
|
-- For some, we prefer no cache (if they cost nothing, return some huge
|
2020-01-16 11:48:34 +00:00
|
|
|
-- data that we'd rather not cache, are called with many different args,
|
|
|
|
-- or we'd rather have up to date crengine state)
|
|
|
|
elseif name == "getCurrentPage" then no_wrap = true
|
|
|
|
elseif name == "getCurrentPos" then no_wrap = true
|
|
|
|
elseif name == "getVisiblePageCount" then no_wrap = true
|
2021-01-29 00:02:19 +00:00
|
|
|
elseif name == "getVisiblePageNumberCount" then no_wrap = true
|
2020-01-16 11:48:34 +00:00
|
|
|
elseif name == "getCoverPageImage" then no_wrap = true
|
|
|
|
elseif name == "getDocumentFileContent" then no_wrap = true
|
|
|
|
elseif name == "getHTMLFromXPointer" then no_wrap = true
|
|
|
|
elseif name == "getHTMLFromXPointers" then no_wrap = true
|
|
|
|
elseif name == "getImageFromPosition" then no_wrap = true
|
|
|
|
elseif name == "getTextFromXPointer" then no_wrap = true
|
|
|
|
elseif name == "getPageOffsetX" then no_wrap = true
|
|
|
|
elseif name == "getNextVisibleWordStart" then no_wrap = true
|
|
|
|
elseif name == "getNextVisibleWordEnd" then no_wrap = true
|
|
|
|
elseif name == "getPrevVisibleWordStart" then no_wrap = true
|
|
|
|
elseif name == "getPrevVisibleWordEnd" then no_wrap = true
|
|
|
|
elseif name == "getPrevVisibleChar" then no_wrap = true
|
|
|
|
elseif name == "getNextVisibleChar" then no_wrap = true
|
|
|
|
elseif name == "getCacheFilePath" then no_wrap = true
|
|
|
|
elseif name == "getStatistics" then no_wrap = true
|
2020-02-24 14:25:08 +00:00
|
|
|
elseif name == "getNormalizedXPointer" then no_wrap = true
|
2020-10-31 09:40:36 +00:00
|
|
|
elseif name == "getNextPage" then no_wrap = true
|
|
|
|
elseif name == "getPrevPage" then no_wrap = true
|
|
|
|
elseif name == "getPageFlow" then no_wrap = true
|
|
|
|
elseif name == "getPageNumberInFlow" then no_wrap = true
|
|
|
|
elseif name == "getTotalPagesLeft" then no_wrap = true
|
ReaderRolling: quicker partial rerenderings with EPUBs
Only available with EPUBs containing 2 or more fragments,
and a file size large enough to ensure a cache file is used.
The idea is simply, on any rendering setting change, to
skip the rerendering of the full book and to defer any
rerendering to the moment we draw a DocFragment, and
render only it.
So, on a setting change, only the fragment containing the
current page will be rerendered, and the new fragments we
may cross while turning pages.
When having done so, KOReader is in a degraded state (the
full page count is incorrect, the ToC is invalid...).
So, a full rerendering is needed, and one will happen
in the background, and when the user is idle, we reload
seamlessly and quickly from the cache file it has made.
ReaderFlipping will show some icons in the top left
corner to let it know at which steps in this procress
we are.
2023-02-16 22:30:58 +00:00
|
|
|
elseif name == "getDocumentRenderingHash" then no_wrap = true
|
|
|
|
elseif name == "getPartialRerenderingsCount" then no_wrap = true
|
2020-01-16 11:48:34 +00:00
|
|
|
|
|
|
|
-- Some get* have different results by page/pos
|
|
|
|
elseif name == "getLinkFromPosition" then cache_by_tag = true
|
|
|
|
elseif name == "getPageLinks" then cache_by_tag = true
|
|
|
|
elseif name == "getScreenBoxesFromPositions" then cache_by_tag = true
|
|
|
|
elseif name == "getScreenPositionFromXPointer" then cache_by_tag = true
|
|
|
|
elseif name == "getXPointer" then cache_by_tag = true
|
|
|
|
elseif name == "isXPointerInCurrentPage" then cache_by_tag = true
|
2020-03-26 13:04:59 +00:00
|
|
|
elseif name == "getPageMapCurrentPageLabel" then cache_by_tag = true
|
|
|
|
elseif name == "getPageMapVisiblePageLabels" then cache_by_tag = true
|
2020-01-16 11:48:34 +00:00
|
|
|
|
|
|
|
-- Assume all remaining get* can have their results
|
|
|
|
-- cached globally by function arguments
|
|
|
|
elseif name:sub(1,3) == "get" then cache_global = true
|
|
|
|
|
|
|
|
-- All others don't need to be wrapped
|
|
|
|
end
|
|
|
|
|
|
|
|
if add_reset then
|
|
|
|
self[name] = function(...)
|
|
|
|
-- logger.dbg("callCache:", name, "called with", select(2,...))
|
|
|
|
if do_log then logger.dbg("callCache:", name, "reseting cache") end
|
|
|
|
self._callCacheReset()
|
|
|
|
return func(...)
|
|
|
|
end
|
|
|
|
elseif add_buffer_trash then
|
|
|
|
self[name] = function(...)
|
|
|
|
if do_log then logger.dbg("callCache:", name, "reseting buffer") end
|
|
|
|
self._callCacheSet("current_buffer_tag", nil)
|
|
|
|
return func(...)
|
|
|
|
end
|
|
|
|
elseif set_tag then
|
|
|
|
self[name] = function(...)
|
|
|
|
if do_log then logger.dbg("callCache:", name, "setting tag") end
|
|
|
|
local val = select(set_arg, ...)
|
2021-01-29 00:02:19 +00:00
|
|
|
if set_arg2 then
|
|
|
|
local val2 = select(set_arg2, ...)
|
|
|
|
if val2 ~= nil then
|
|
|
|
val = val .. tostring(val2)
|
|
|
|
end
|
|
|
|
end
|
2020-01-16 11:48:34 +00:00
|
|
|
self._callCacheSetCurrentTag(set_tag .. val)
|
|
|
|
return func(...)
|
|
|
|
end
|
|
|
|
elseif cache_by_tag then
|
|
|
|
is_cached = true
|
|
|
|
self[name] = function(...)
|
2021-05-05 20:48:10 +00:00
|
|
|
local starttime = now()
|
|
|
|
local cache_key = name .. buffer.encode({select(2, ...)})
|
2020-01-16 11:48:34 +00:00
|
|
|
local results = self._callCacheTagGet(cache_key)
|
|
|
|
if results then
|
|
|
|
if do_log then logger.dbg("callCache:", name, "cache hit:", cache_key) end
|
|
|
|
addStatHit(name, starttime)
|
|
|
|
-- We might want to return a deep-copy of results, in case callers
|
|
|
|
-- play at modifying values. But it looks like none currently do.
|
|
|
|
-- So, better to keep calling code not modifying returned results.
|
|
|
|
return unpack(results)
|
|
|
|
else
|
|
|
|
if do_log then logger.dbg("callCache:", name, "cache miss:", cache_key) end
|
|
|
|
results = { func(...) }
|
|
|
|
self._callCacheTagSet(cache_key, results)
|
|
|
|
addStatMiss(name, starttime)
|
|
|
|
return unpack(results)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif cache_global then
|
|
|
|
is_cached = true
|
|
|
|
self[name] = function(...)
|
2021-05-05 20:48:10 +00:00
|
|
|
local starttime = now()
|
|
|
|
local cache_key = name .. buffer.encode({select(2, ...)})
|
2020-01-16 11:48:34 +00:00
|
|
|
local results = self._callCacheGet(cache_key)
|
|
|
|
if results then
|
|
|
|
if do_log then logger.dbg("callCache:", name, "cache hit:", cache_key) end
|
|
|
|
addStatHit(name, starttime)
|
|
|
|
-- See comment above
|
|
|
|
return unpack(results)
|
|
|
|
else
|
|
|
|
if do_log then logger.dbg("callCache:", name, "cache miss:", cache_key) end
|
|
|
|
results = { func(...) }
|
|
|
|
self._callCacheSet(cache_key, results)
|
|
|
|
addStatMiss(name, starttime)
|
|
|
|
return unpack(results)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if do_stats_include_not_cached and not is_cached then
|
|
|
|
local func2 = self[name] -- might already be wrapped
|
|
|
|
self[name] = function(...)
|
2021-05-05 20:48:10 +00:00
|
|
|
local starttime = now()
|
2020-01-16 11:48:34 +00:00
|
|
|
local results = { func2(...) }
|
|
|
|
addStatMiss(name, starttime, true) -- not_cached = true
|
|
|
|
return unpack(results)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- We override a bit more specifically the one responsible for drawing page
|
2022-06-11 17:06:06 +00:00
|
|
|
self.drawCurrentView = function(this, target, x, y, rect, pos)
|
2020-01-16 11:48:34 +00:00
|
|
|
local do_draw = false
|
2022-06-11 17:06:06 +00:00
|
|
|
local current_tag = this._callCacheGetCurrentTag()
|
|
|
|
local current_buffer_tag = this._callCacheGet("current_buffer_tag")
|
|
|
|
if this.buffer and (this.buffer.w ~= rect.w or this.buffer.h ~= rect.h) then
|
2020-01-16 11:48:34 +00:00
|
|
|
do_draw = true
|
2022-06-11 17:06:06 +00:00
|
|
|
elseif not this.buffer then
|
2020-01-16 11:48:34 +00:00
|
|
|
do_draw = true
|
|
|
|
elseif not current_buffer_tag then
|
|
|
|
do_draw = true
|
|
|
|
elseif current_buffer_tag ~= current_tag then
|
|
|
|
do_draw = true
|
|
|
|
end
|
2021-05-05 20:48:10 +00:00
|
|
|
local starttime = now()
|
2020-01-16 11:48:34 +00:00
|
|
|
if do_draw then
|
|
|
|
if do_log then logger.dbg("callCache: ########## drawCurrentView: full draw") end
|
2022-06-11 17:06:06 +00:00
|
|
|
CreDocument.drawCurrentView(this, target, x, y, rect, pos)
|
2020-01-16 11:48:34 +00:00
|
|
|
addStatMiss("drawCurrentView", starttime)
|
2022-06-11 17:06:06 +00:00
|
|
|
this._callCacheSet("current_buffer_tag", current_tag)
|
2020-01-16 11:48:34 +00:00
|
|
|
else
|
|
|
|
if do_log then logger.dbg("callCache: ---------- drawCurrentView: light draw") end
|
2022-06-11 17:06:06 +00:00
|
|
|
target:blitFrom(this.buffer, x, y, 0, 0, rect.w, rect.h)
|
2020-01-16 11:48:34 +00:00
|
|
|
addStatHit("drawCurrentView", starttime)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Dump statistics on close
|
|
|
|
if do_stats then
|
2022-06-11 17:06:06 +00:00
|
|
|
self.close = function(this)
|
2020-01-16 11:48:34 +00:00
|
|
|
dumpStats()
|
2022-06-11 17:06:06 +00:00
|
|
|
CreDocument.close(this)
|
2020-01-16 11:48:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return CreDocument
|