decouple UI definitions from document modules

pull/4721/head
Qingping Hou 6 years ago committed by Frans de Jonge
parent 02eca23649
commit 9e57e56f95

@ -4,6 +4,8 @@ local Event = require("ui/event")
local Geom = require("ui/geometry") local Geom = require("ui/geometry")
local InputContainer = require("ui/widget/container/inputcontainer") local InputContainer = require("ui/widget/container/inputcontainer")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local CreOptions = require("ui/data/creoptions")
local KoptOptions = require("ui/data/koptoptions")
local _ = require("gettext") local _ = require("gettext")
local ReaderConfig = InputContainer:new{ local ReaderConfig = InputContainer:new{
@ -11,6 +13,13 @@ local ReaderConfig = InputContainer:new{
} }
function ReaderConfig:init() 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 not self.dimen then self.dimen = Geom:new{} end
if Device:hasKeys() then if Device:hasKeys() then
self.key_events = { self.key_events = {

@ -197,10 +197,10 @@ function ReaderUI:init()
-- config panel controller -- config panel controller
self:registerModule("config", ReaderConfig:new{ self:registerModule("config", ReaderConfig:new{
configurable = self.document.configurable, configurable = self.document.configurable,
options = self.document.options,
dialog = self.dialog, dialog = self.dialog,
view = self.view, view = self.view,
ui = self ui = self,
document = self.document,
}) })
if self.document.info.has_pages then if self.document.info.has_pages then
-- kopt option controller -- kopt option controller

@ -9,7 +9,8 @@ end
function Configurable:reset() function Configurable:reset()
for key,value in pairs(self) do 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 self[key] = nil
end end
end end
@ -18,7 +19,8 @@ end
function Configurable:hash(sep) function Configurable:hash(sep)
local hash = "" local hash = ""
for key,value in pairs(self) do 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 hash = hash..sep..value
end end
end end
@ -28,11 +30,12 @@ end
function Configurable:loadDefaults(config_options) function Configurable:loadDefaults(config_options)
-- reset configurable before loading new options -- reset configurable before loading new options
self:reset() self:reset()
local prefix = config_options.prefix.."_"
for i=1,#config_options do for i=1,#config_options do
local options = config_options[i].options local options = config_options[i].options
for j=1,#options do for j=1,#options do
local key = options[j].name 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) local default = G_reader_settings:readSetting(settings_key)
self[key] = default or options[j].default_value self[key] = default or options[j].default_value
if not self[key] then if not self[key] then
@ -44,21 +47,22 @@ end
function Configurable:loadSettings(settings, prefix) function Configurable:loadSettings(settings, prefix)
for key,value in pairs(self) do for key,value in pairs(self) do
if type(value) == "number" or type(value) == "string" local value_type = type(value)
or type(value) == "table" then if value_type == "number" or value_type == "string"
or value_type == "table" then
local saved_value = settings:readSetting(prefix..key) local saved_value = settings:readSetting(prefix..key)
self[key] = (saved_value == nil) and self[key] or saved_value if saved_value ~= nil then
--Debug("Configurable:loadSettings", "key", key, "saved value", self[key] = saved_value
--saved_value,"Configurable.key", self[key]) end
end end
end end
--Debug("loaded config:", dump(Configurable))
end end
function Configurable:saveSettings(settings, prefix) function Configurable:saveSettings(settings, prefix)
for key,value in pairs(self) do for key,value in pairs(self) do
if type(value) == "number" or type(value) == "string" local value_type = type(value)
or type(value) == "table" then if value_type == "number" or value_type == "string"
or value_type == "table" then
settings:saveSetting(prefix..key, value) settings:saveSetting(prefix..key, value)
end end
end end

@ -1,5 +1,4 @@
local Blitbuffer = require("ffi/blitbuffer") local Blitbuffer = require("ffi/blitbuffer")
local CreOptions = require("ui/data/creoptions")
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
local Document = require("document/document") local Document = require("document/document")
local Font = require("ui/font") local Font = require("ui/font")
@ -28,7 +27,6 @@ local CreDocument = Document:new{
header_font = "Noto Sans", header_font = "Noto Sans",
fallback_font = "Noto Sans CJK SC", fallback_font = "Noto Sans CJK SC",
default_css = "./data/cr3.css", default_css = "./data/cr3.css",
options = CreOptions,
provider = "crengine", provider = "crengine",
provider_name = "Cool Reader Engine", provider_name = "Cool Reader Engine",
} }
@ -94,7 +92,6 @@ end
function CreDocument:init() function CreDocument:init()
self:updateColorRendering() self:updateColorRendering()
self:engineInit() self:engineInit()
self.configurable:loadDefaults(self.options)
local file_type = string.lower(string.match(self.file, ".+%.([^.]+)")) local file_type = string.lower(string.match(self.file, ".+%.([^.]+)"))
if file_type == "zip" then if file_type == "zip" then

@ -1,7 +1,6 @@
local Blitbuffer = require("ffi/blitbuffer") local Blitbuffer = require("ffi/blitbuffer")
local Document = require("document/document") local Document = require("document/document")
local DrawContext = require("ffi/drawcontext") local DrawContext = require("ffi/drawcontext")
local KoptOptions = require("ui/data/koptoptions")
local DjvuDocument = Document:new{ local DjvuDocument = Document:new{
_document = false, _document = false,
@ -9,7 +8,6 @@ local DjvuDocument = Document:new{
is_djvu = true, is_djvu = true,
djvulibre_cache_size = nil, djvulibre_cache_size = nil,
dc_null = DrawContext.new(), dc_null = DrawContext.new(),
options = KoptOptions,
koptinterface = nil, koptinterface = nil,
color_bb_type = Blitbuffer.TYPE_BBRGB24, color_bb_type = Blitbuffer.TYPE_BBRGB24,
provider = "djvulibre", provider = "djvulibre",
@ -30,7 +28,7 @@ function DjvuDocument:init()
self:updateColorRendering() self:updateColorRendering()
local djvu = require("libs/libkoreader-djvu") local djvu = require("libs/libkoreader-djvu")
self.koptinterface = require("document/koptinterface") self.koptinterface = require("document/koptinterface")
self.configurable:loadDefaults(self.options) self.koptinterface:setDefaultConfigurable(self.configurable)
if not validDjvuFile(self.file) then if not validDjvuFile(self.file) then
error("Not a valid DjVu file") error("Not a valid DjVu file")
end end

@ -54,6 +54,24 @@ function OCREngine:onFree()
end end
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) function KoptInterface:waitForContext(kc)
-- if koptcontext is being processed in background thread -- if koptcontext is being processed in background thread
-- the isPreCache will return 1. -- 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) function KoptInterface:getTextBoxes(doc, pageno)
local text = doc:getPageTextBoxes(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 return text
-- if we have no text in original page then we will reuse native word boxes -- 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 -- in reflow mode and find text boxes from scratch in non-reflow mode

@ -3,7 +3,6 @@ local CacheItem = require("cacheitem")
local Device = require("device") local Device = require("device")
local Document = require("document/document") local Document = require("document/document")
local DrawContext = require("ffi/drawcontext") local DrawContext = require("ffi/drawcontext")
local KoptOptions = require("ui/data/koptoptions")
local logger = require("logger") local logger = require("logger")
local util = require("util") local util = require("util")
local ffi = require("ffi") local ffi = require("ffi")
@ -16,7 +15,6 @@ local PdfDocument = Document:new{
_document = false, _document = false,
is_pdf = true, is_pdf = true,
dc_null = DrawContext.new(), dc_null = DrawContext.new(),
options = KoptOptions,
epub_font_size = G_reader_settings:readSetting("copt_font_size") epub_font_size = G_reader_settings:readSetting("copt_font_size")
or DCREREADER_CONFIG_DEFAULT_FONT_SIZE or 22, or DCREREADER_CONFIG_DEFAULT_FONT_SIZE or 22,
koptinterface = nil, koptinterface = nil,
@ -39,7 +37,7 @@ function PdfDocument:init()
end end
end end
self.koptinterface = require("document/koptinterface") self.koptinterface = require("document/koptinterface")
self.configurable:loadDefaults(self.options) self.koptinterface:setDefaultConfigurable(self.configurable)
local ok local ok
ok, self._document = pcall(pdf.openDocument, self.file) ok, self._document = pcall(pdf.openDocument, self.file)
if not ok then if not ok then

@ -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

@ -1,8 +1,8 @@
local Device = require("device") local Device = require("device")
local Screen = Device.screen
local S = require("ui/data/strings") local S = require("ui/data/strings")
local optionsutil = require("ui/data/optionsutil") local optionsutil = require("ui/data/optionsutil")
local _ = require("gettext") local _ = require("gettext")
local Screen = Device.screen
-- add multiply operator to Aa dict -- add multiply operator to Aa dict
local Aa = setmetatable({"Aa"}, { local Aa = setmetatable({"Aa"}, {

Loading…
Cancel
Save