From 9e57e56f95aa84d3033d58f8d5c8c0085009efec Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Fri, 26 Oct 2018 08:27:43 -0700 Subject: [PATCH] decouple UI definitions from document modules --- frontend/apps/reader/modules/readerconfig.lua | 9 +++++++ frontend/apps/reader/readerui.lua | 4 +-- frontend/configurable.lua | 26 +++++++++++-------- frontend/document/credocument.lua | 3 --- frontend/document/djvudocument.lua | 4 +-- frontend/document/koptinterface.lua | 20 +++++++++++++- frontend/document/pdfdocument.lua | 4 +-- frontend/runtimectl.lua | 25 ++++++++++++++++++ frontend/ui/data/creoptions.lua | 2 +- 9 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 frontend/runtimectl.lua diff --git a/frontend/apps/reader/modules/readerconfig.lua b/frontend/apps/reader/modules/readerconfig.lua index c821d9d6a..caf35d22b 100644 --- a/frontend/apps/reader/modules/readerconfig.lua +++ b/frontend/apps/reader/modules/readerconfig.lua @@ -4,6 +4,8 @@ local Event = require("ui/event") local Geom = require("ui/geometry") local InputContainer = require("ui/widget/container/inputcontainer") local UIManager = require("ui/uimanager") +local CreOptions = require("ui/data/creoptions") +local KoptOptions = require("ui/data/koptoptions") local _ = require("gettext") local ReaderConfig = InputContainer:new{ @@ -11,6 +13,13 @@ local ReaderConfig = InputContainer:new{ } function ReaderConfig:init() + if self.document.koptinterface ~= nil then + self.options = KoptOptions + else + self.options = CreOptions + end + self.configurable:loadDefaults(self.options) + if not self.dimen then self.dimen = Geom:new{} end if Device:hasKeys() then self.key_events = { diff --git a/frontend/apps/reader/readerui.lua b/frontend/apps/reader/readerui.lua index d1d8893c1..dff788b27 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -197,10 +197,10 @@ function ReaderUI:init() -- config panel controller self:registerModule("config", ReaderConfig:new{ configurable = self.document.configurable, - options = self.document.options, dialog = self.dialog, view = self.view, - ui = self + ui = self, + document = self.document, }) if self.document.info.has_pages then -- kopt option controller diff --git a/frontend/configurable.lua b/frontend/configurable.lua index 5f1116110..97a4c1802 100644 --- a/frontend/configurable.lua +++ b/frontend/configurable.lua @@ -9,7 +9,8 @@ end function Configurable:reset() for key,value in pairs(self) do - if type(value) == "number" or type(value) == "string" then + local value_type = type(value) + if value_type == "number" or value_type == "string" then self[key] = nil end end @@ -18,7 +19,8 @@ end function Configurable:hash(sep) local hash = "" for key,value in pairs(self) do - if type(value) == "number" or type(value) == "string" then + local value_type = type(value) + if value_type == "number" or value_type == "string" then hash = hash..sep..value end end @@ -28,11 +30,12 @@ end function Configurable:loadDefaults(config_options) -- reset configurable before loading new options self:reset() + local prefix = config_options.prefix.."_" for i=1,#config_options do local options = config_options[i].options for j=1,#options do local key = options[j].name - local settings_key = config_options.prefix.."_"..key + local settings_key = prefix..key local default = G_reader_settings:readSetting(settings_key) self[key] = default or options[j].default_value if not self[key] then @@ -44,21 +47,22 @@ end function Configurable:loadSettings(settings, prefix) for key,value in pairs(self) do - if type(value) == "number" or type(value) == "string" - or type(value) == "table" then + local value_type = type(value) + if value_type == "number" or value_type == "string" + or value_type == "table" then local saved_value = settings:readSetting(prefix..key) - self[key] = (saved_value == nil) and self[key] or saved_value - --Debug("Configurable:loadSettings", "key", key, "saved value", - --saved_value,"Configurable.key", self[key]) + if saved_value ~= nil then + self[key] = saved_value + end end end - --Debug("loaded config:", dump(Configurable)) end function Configurable:saveSettings(settings, prefix) for key,value in pairs(self) do - if type(value) == "number" or type(value) == "string" - or type(value) == "table" then + local value_type = type(value) + if value_type == "number" or value_type == "string" + or value_type == "table" then settings:saveSetting(prefix..key, value) end end diff --git a/frontend/document/credocument.lua b/frontend/document/credocument.lua index 10b0072bc..25aea39e3 100644 --- a/frontend/document/credocument.lua +++ b/frontend/document/credocument.lua @@ -1,5 +1,4 @@ local Blitbuffer = require("ffi/blitbuffer") -local CreOptions = require("ui/data/creoptions") local DataStorage = require("datastorage") local Document = require("document/document") local Font = require("ui/font") @@ -28,7 +27,6 @@ local CreDocument = Document:new{ header_font = "Noto Sans", fallback_font = "Noto Sans CJK SC", default_css = "./data/cr3.css", - options = CreOptions, provider = "crengine", provider_name = "Cool Reader Engine", } @@ -94,7 +92,6 @@ end function CreDocument:init() self:updateColorRendering() self:engineInit() - self.configurable:loadDefaults(self.options) local file_type = string.lower(string.match(self.file, ".+%.([^.]+)")) if file_type == "zip" then diff --git a/frontend/document/djvudocument.lua b/frontend/document/djvudocument.lua index f4d8aacef..cf7e42efb 100644 --- a/frontend/document/djvudocument.lua +++ b/frontend/document/djvudocument.lua @@ -1,7 +1,6 @@ local Blitbuffer = require("ffi/blitbuffer") local Document = require("document/document") local DrawContext = require("ffi/drawcontext") -local KoptOptions = require("ui/data/koptoptions") local DjvuDocument = Document:new{ _document = false, @@ -9,7 +8,6 @@ local DjvuDocument = Document:new{ is_djvu = true, djvulibre_cache_size = nil, dc_null = DrawContext.new(), - options = KoptOptions, koptinterface = nil, color_bb_type = Blitbuffer.TYPE_BBRGB24, provider = "djvulibre", @@ -30,7 +28,7 @@ function DjvuDocument:init() self:updateColorRendering() local djvu = require("libs/libkoreader-djvu") self.koptinterface = require("document/koptinterface") - self.configurable:loadDefaults(self.options) + self.koptinterface:setDefaultConfigurable(self.configurable) if not validDjvuFile(self.file) then error("Not a valid DjVu file") end diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 0a7324bdb..aef111dbd 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -54,6 +54,24 @@ function OCREngine:onFree() end end +function KoptInterface:setDefaultConfigurable(configurable) + configurable.doc_language = DKOPTREADER_CONFIG_DOC_DEFAULT_LANG_CODE + configurable.trim_page = DKOPTREADER_CONFIG_TRIM_PAGE + configurable.text_wrap = DKOPTREADER_CONFIG_TEXT_WRAP + configurable.detect_indent = DKOPTREADER_CONFIG_DETECT_INDENT + configurable.max_columns = DKOPTREADER_CONFIG_MAX_COLUMNS + configurable.auto_straighten = DKOPTREADER_CONFIG_AUTO_STRAIGHTEN + configurable.justification = DKOPTREADER_CONFIG_JUSTIFICATION + configurable.writing_direction = 0 + configurable.font_size = DKOPTREADER_CONFIG_FONT_SIZE + configurable.page_margin = DKOPTREADER_CONFIG_PAGE_MARGIN + configurable.quality = DKOPTREADER_CONFIG_RENDER_QUALITY + configurable.contrast = DKOPTREADER_CONFIG_CONTRAST + configurable.defect_size = DKOPTREADER_CONFIG_DEFECT_SIZE + configurable.line_spacing = DKOPTREADER_CONFIG_LINE_SPACING + configurable.word_spacing = DKOPTREADER_CONFIG_DEFAULT_WORD_SPACING +end + function KoptInterface:waitForContext(kc) -- if koptcontext is being processed in background thread -- the isPreCache will return 1. @@ -421,7 +439,7 @@ returned boxes are in native page coordinates zoomed at 1.0 --]] function KoptInterface:getTextBoxes(doc, pageno) local text = doc:getPageTextBoxes(pageno) - if text and #text > 1 and doc.configurable.forced_ocr == 0 then + if text and #text > 1 and doc.configurable.forced_ocr ~= 1 then return text -- if we have no text in original page then we will reuse native word boxes -- in reflow mode and find text boxes from scratch in non-reflow mode diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index 8d2294269..e8f2b0697 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -3,7 +3,6 @@ local CacheItem = require("cacheitem") local Device = require("device") local Document = require("document/document") local DrawContext = require("ffi/drawcontext") -local KoptOptions = require("ui/data/koptoptions") local logger = require("logger") local util = require("util") local ffi = require("ffi") @@ -16,7 +15,6 @@ local PdfDocument = Document:new{ _document = false, is_pdf = true, dc_null = DrawContext.new(), - options = KoptOptions, epub_font_size = G_reader_settings:readSetting("copt_font_size") or DCREREADER_CONFIG_DEFAULT_FONT_SIZE or 22, koptinterface = nil, @@ -39,7 +37,7 @@ function PdfDocument:init() end end self.koptinterface = require("document/koptinterface") - self.configurable:loadDefaults(self.options) + self.koptinterface:setDefaultConfigurable(self.configurable) local ok ok, self._document = pcall(pdf.openDocument, self.file) if not ok then diff --git a/frontend/runtimectl.lua b/frontend/runtimectl.lua new file mode 100644 index 000000000..2d6b168da --- /dev/null +++ b/frontend/runtimectl.lua @@ -0,0 +1,25 @@ +local Runtimectl = { + should_restrict_JIT = false, + is_color_rendering_enabled = false, +} + +--[[ +Disable jit on some modules on android to make koreader on Android more stable. + +The strategy here is that we only use precious mcode memory (jitting) +on deep loops like the several blitting methods in blitbuffer.lua and +the pixel-copying methods in mupdf.lua. So that a small amount of mcode +memory (64KB) allocated when koreader is launched in the android.lua +is enough for the program and it won't need to jit other parts of lua +code and thus won't allocate mcode memory any more which by our +observation will be harder and harder as we run koreader. +]]-- +function Runtimectl:restrictJIT() + self.should_restrict_JIT = true +end + +function Runtimectl:setColorRenderingEnabled(val) + self.is_color_rendering_enabled = val +end + +return Runtimectl diff --git a/frontend/ui/data/creoptions.lua b/frontend/ui/data/creoptions.lua index 49960ca1c..0ef10d922 100644 --- a/frontend/ui/data/creoptions.lua +++ b/frontend/ui/data/creoptions.lua @@ -1,8 +1,8 @@ local Device = require("device") +local Screen = Device.screen local S = require("ui/data/strings") local optionsutil = require("ui/data/optionsutil") local _ = require("gettext") -local Screen = Device.screen -- add multiply operator to Aa dict local Aa = setmetatable({"Aa"}, {