2013-10-22 15:11:31 +00:00
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2013-10-22 15:11:31 +00:00
|
|
|
local Event = require("ui/event")
|
2017-04-22 07:18:10 +00:00
|
|
|
local Input = Device.input
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local Menu = require("ui/widget/menu")
|
|
|
|
local MultiConfirmBox = require("ui/widget/multiconfirmbox")
|
|
|
|
local Notification = require("ui/widget/notification")
|
|
|
|
local Screen = require("device").screen
|
2013-10-18 20:38:07 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2014-11-28 11:48:15 +00:00
|
|
|
local T = require("ffi/util").template
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
|
|
|
|
local ReaderFont = InputContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
font_face = nil,
|
|
|
|
font_size = nil,
|
|
|
|
line_space_percent = nil,
|
2017-09-13 10:44:37 +00:00
|
|
|
font_menu_title = _("Font"),
|
2014-03-13 13:52:43 +00:00
|
|
|
face_table = nil,
|
|
|
|
-- default gamma from crengine's lvfntman.cpp
|
|
|
|
gamma_index = nil,
|
2017-06-29 18:38:39 +00:00
|
|
|
steps = {0,1,1,1,1,1,2,2,2,3,3,3,4,4,5},
|
|
|
|
gestureScale = Screen:getWidth() * FRONTLIGHT_SENSITIVITY_DECREASE,
|
2012-06-12 10:04:08 +00:00
|
|
|
}
|
|
|
|
|
2012-06-12 12:58:15 +00:00
|
|
|
function ReaderFont:init()
|
2014-03-13 13:52:43 +00:00
|
|
|
if Device:hasKeyboard() then
|
|
|
|
-- add shortcut for keyboard
|
|
|
|
self.key_events = {
|
2014-06-05 06:58:53 +00:00
|
|
|
ShowFontMenu = { {"F"}, doc = "show font menu" },
|
2014-03-13 13:52:43 +00:00
|
|
|
IncreaseSize = {
|
|
|
|
{ "Shift", Input.group.PgFwd },
|
2014-06-05 06:58:53 +00:00
|
|
|
doc = "increase font size",
|
2014-03-13 13:52:43 +00:00
|
|
|
event = "ChangeSize", args = "increase" },
|
|
|
|
DecreaseSize = {
|
|
|
|
{ "Shift", Input.group.PgBack },
|
2014-06-05 06:58:53 +00:00
|
|
|
doc = "decrease font size",
|
2014-03-13 13:52:43 +00:00
|
|
|
event = "ChangeSize", args = "decrease" },
|
|
|
|
IncreaseLineSpace = {
|
|
|
|
{ "Alt", Input.group.PgFwd },
|
2014-06-05 06:58:53 +00:00
|
|
|
doc = "increase line space",
|
2014-03-13 13:52:43 +00:00
|
|
|
event = "ChangeLineSpace", args = "increase" },
|
|
|
|
DecreaseLineSpace = {
|
|
|
|
{ "Alt", Input.group.PgBack },
|
2014-06-05 06:58:53 +00:00
|
|
|
doc = "decrease line space",
|
2014-03-13 13:52:43 +00:00
|
|
|
event = "ChangeLineSpace", args = "decrease" },
|
|
|
|
}
|
|
|
|
end
|
|
|
|
-- build face_table for menu
|
|
|
|
self.face_table = {}
|
|
|
|
local face_list = cre.getFontFaces()
|
|
|
|
for k,v in ipairs(face_list) do
|
|
|
|
table.insert(self.face_table, {
|
|
|
|
text = v,
|
|
|
|
callback = function()
|
|
|
|
self:setFont(v)
|
2014-07-02 14:53:06 +00:00
|
|
|
end,
|
|
|
|
hold_callback = function()
|
|
|
|
self:makeDefault(v)
|
|
|
|
end,
|
2014-11-15 15:23:27 +00:00
|
|
|
checked_func = function()
|
|
|
|
return v == self.font_face
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
})
|
|
|
|
face_list[k] = {text = v}
|
|
|
|
end
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
2012-06-26 16:59:47 +00:00
|
|
|
end
|
|
|
|
|
2017-04-22 07:18:10 +00:00
|
|
|
function ReaderFont:onReaderReady()
|
|
|
|
self:setupTouchZones()
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderFont:setupTouchZones()
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ui:registerTouchZones({
|
|
|
|
{
|
|
|
|
id = "id_spread",
|
|
|
|
ges = "spread",
|
|
|
|
screen_zone = {
|
|
|
|
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
|
|
|
|
},
|
2017-06-29 18:38:39 +00:00
|
|
|
handler = function(ges) return self:onAdjustSpread(ges) end
|
2017-04-22 07:18:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id = "id_pinch",
|
|
|
|
ges = "pinch",
|
|
|
|
screen_zone = {
|
|
|
|
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
|
|
|
|
},
|
2017-06-29 18:38:39 +00:00
|
|
|
handler = function(ges) return self:onAdjustPinch(ges) end
|
2017-04-22 07:18:10 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-26 16:59:47 +00:00
|
|
|
function ReaderFont:onSetDimensions(dimen)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.dimen = dimen
|
2012-06-26 16:59:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderFont:onReadSettings(config)
|
2014-08-17 13:45:48 +00:00
|
|
|
self.font_face = config:readSetting("font_face")
|
|
|
|
or self.ui.document.default_font
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.document:setFontFace(self.font_face)
|
|
|
|
|
2014-08-17 13:45:48 +00:00
|
|
|
self.header_font_face = config:readSetting("header_font_face")
|
|
|
|
or self.ui.document.header_font
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.document:setHeaderFont(self.header_font_face)
|
|
|
|
|
2014-08-17 13:45:48 +00:00
|
|
|
self.font_size = config:readSetting("font_size")
|
2015-11-06 14:59:53 +00:00
|
|
|
or G_reader_settings:readSetting("copt_font_size")
|
2014-08-17 13:45:48 +00:00
|
|
|
or DCREREADER_CONFIG_DEFAULT_FONT_SIZE or 22
|
2014-11-20 22:07:39 +00:00
|
|
|
self.ui.document:setFontSize(Screen:scaleBySize(self.font_size))
|
2014-03-13 13:52:43 +00:00
|
|
|
|
2014-08-17 13:45:48 +00:00
|
|
|
self.font_embolden = config:readSetting("font_embolden")
|
|
|
|
or G_reader_settings:readSetting("copt_font_weight") or 0
|
2014-06-04 13:54:01 +00:00
|
|
|
self.ui.document:toggleFontBolder(self.font_embolden)
|
2014-03-13 13:52:43 +00:00
|
|
|
|
2017-09-08 15:38:51 +00:00
|
|
|
self.font_hinting = config:readSetting("font_hinting")
|
|
|
|
or G_reader_settings:readSetting("copt_font_hinting") or 2 -- default in cre.cpp
|
|
|
|
self.ui.document:setFontHinting(self.font_hinting)
|
|
|
|
|
2014-08-17 13:45:48 +00:00
|
|
|
self.line_space_percent = config:readSetting("line_space_percent")
|
|
|
|
or G_reader_settings:readSetting("copt_line_spacing")
|
|
|
|
or DCREREADER_CONFIG_LINE_SPACE_PERCENT_MEDIUM
|
2014-06-04 13:54:01 +00:00
|
|
|
self.ui.document:setInterlineSpacePercent(self.line_space_percent)
|
|
|
|
|
2014-08-17 13:45:48 +00:00
|
|
|
self.gamma_index = config:readSetting("gamma_index")
|
|
|
|
or G_reader_settings:readSetting("copt_font_gamma")
|
|
|
|
or DCREREADER_CONFIG_DEFAULT_FONT_GAMMA
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.document:setGammaIndex(self.gamma_index)
|
|
|
|
|
2016-10-04 17:38:32 +00:00
|
|
|
-- Dirty hack: we have to add following call in order to set
|
2014-03-13 13:52:43 +00:00
|
|
|
-- m_is_rendered(member of LVDocView) to true. Otherwise position inside
|
|
|
|
-- document will be reset to 0 on first view render.
|
|
|
|
-- So far, I don't know why this call will alter the value of m_is_rendered.
|
|
|
|
table.insert(self.ui.postInitCallback, function()
|
|
|
|
self.ui:handleEvent(Event:new("UpdatePos"))
|
|
|
|
end)
|
2012-06-12 12:58:15 +00:00
|
|
|
end
|
|
|
|
|
2012-06-12 10:04:08 +00:00
|
|
|
function ReaderFont:onShowFontMenu()
|
2014-03-13 13:52:43 +00:00
|
|
|
-- build menu widget
|
|
|
|
local main_menu = Menu:new{
|
|
|
|
title = self.font_menu_title,
|
|
|
|
item_table = self.face_table,
|
|
|
|
width = Screen:getWidth() - 100,
|
|
|
|
}
|
|
|
|
-- build container
|
|
|
|
local menu_container = CenterContainer:new{
|
|
|
|
main_menu,
|
|
|
|
dimen = Screen:getSize(),
|
|
|
|
}
|
|
|
|
main_menu.close_callback = function ()
|
|
|
|
UIManager:close(menu_container)
|
|
|
|
end
|
|
|
|
-- show menu
|
2014-05-31 12:43:44 +00:00
|
|
|
|
|
|
|
main_menu.show_parent = menu_container
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:show(menu_container)
|
2014-05-31 12:43:44 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2012-06-12 12:58:15 +00:00
|
|
|
end
|
|
|
|
|
2012-12-22 05:27:46 +00:00
|
|
|
--[[
|
2014-03-13 13:52:43 +00:00
|
|
|
UpdatePos event is used to tell ReaderRolling to update pos.
|
2012-12-22 05:27:46 +00:00
|
|
|
--]]
|
2017-06-29 18:38:39 +00:00
|
|
|
function ReaderFont:onChangeSize(direction, font_delta)
|
2014-03-13 13:52:43 +00:00
|
|
|
local delta = direction == "decrease" and -1 or 1
|
2017-06-29 18:38:39 +00:00
|
|
|
if font_delta then
|
|
|
|
self.font_size = self.font_size + font_delta * delta
|
|
|
|
else
|
|
|
|
self.font_size = self.font_size + delta
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui:handleEvent(Event:new("SetFontSize", self.font_size))
|
|
|
|
return true
|
2012-06-12 10:04:08 +00:00
|
|
|
end
|
|
|
|
|
2013-01-07 04:21:11 +00:00
|
|
|
function ReaderFont:onSetFontSize(new_size)
|
2014-03-13 13:52:43 +00:00
|
|
|
if new_size > 72 then new_size = 72 end
|
|
|
|
if new_size < 12 then new_size = 12 end
|
|
|
|
|
|
|
|
self.font_size = new_size
|
2017-04-22 07:18:10 +00:00
|
|
|
self.ui.document:setFontSize(Screen:scaleBySize(new_size))
|
|
|
|
self.ui:handleEvent(Event:new("UpdatePos"))
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:show(Notification:new{
|
2014-11-28 13:10:37 +00:00
|
|
|
text = T( _("Font size set to %1."), self.font_size),
|
2014-03-13 13:52:43 +00:00
|
|
|
timeout = 1,
|
2014-12-01 14:39:41 +00:00
|
|
|
})
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
return true
|
2013-01-07 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2014-08-17 13:45:48 +00:00
|
|
|
function ReaderFont:onSetLineSpace(space)
|
|
|
|
self.line_space_percent = math.min(200, math.max(80, space))
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:show(Notification:new{
|
2014-11-28 13:10:37 +00:00
|
|
|
text = T( _("Line spacing set to %1%."), self.line_space_percent),
|
2014-03-13 13:52:43 +00:00
|
|
|
timeout = 1,
|
|
|
|
})
|
|
|
|
self.ui.document:setInterlineSpacePercent(self.line_space_percent)
|
|
|
|
self.ui:handleEvent(Event:new("UpdatePos"))
|
|
|
|
return true
|
2012-06-12 13:12:04 +00:00
|
|
|
end
|
|
|
|
|
2014-06-04 13:54:01 +00:00
|
|
|
function ReaderFont:onToggleFontBolder(toggle)
|
|
|
|
self.font_embolden = toggle
|
|
|
|
self.ui.document:toggleFontBolder(toggle)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui:handleEvent(Event:new("UpdatePos"))
|
|
|
|
return true
|
2013-01-07 17:17:34 +00:00
|
|
|
end
|
|
|
|
|
2017-09-08 15:38:51 +00:00
|
|
|
function ReaderFont:onSetFontHinting(mode)
|
|
|
|
self.font_hinting = mode
|
|
|
|
self.ui.document:setFontHinting(mode)
|
|
|
|
self.ui:handleEvent(Event:new("UpdatePos"))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2014-08-17 13:45:48 +00:00
|
|
|
function ReaderFont:onSetFontGamma(gamma)
|
|
|
|
self.gamma_index = gamma
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:show(Notification:new{
|
2014-11-28 13:10:37 +00:00
|
|
|
text = T( _("Font gamma set to %1."), self.gamma_index),
|
2014-03-13 13:52:43 +00:00
|
|
|
timeout = 1
|
|
|
|
})
|
2014-08-17 13:45:48 +00:00
|
|
|
self.ui.document:setGammaIndex(self.gamma_index)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui:handleEvent(Event:new("RedrawCurrentView"))
|
|
|
|
return true
|
2013-01-07 21:42:23 +00:00
|
|
|
end
|
|
|
|
|
2013-12-27 15:18:16 +00:00
|
|
|
function ReaderFont:onSaveSettings()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.doc_settings:saveSetting("font_face", self.font_face)
|
|
|
|
self.ui.doc_settings:saveSetting("header_font_face", self.header_font_face)
|
|
|
|
self.ui.doc_settings:saveSetting("font_size", self.font_size)
|
2014-06-04 13:54:01 +00:00
|
|
|
self.ui.doc_settings:saveSetting("font_embolden", self.font_embolden)
|
2017-09-08 15:38:51 +00:00
|
|
|
self.ui.doc_settings:saveSetting("font_hinting", self.font_hinting)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.doc_settings:saveSetting("line_space_percent", self.line_space_percent)
|
|
|
|
self.ui.doc_settings:saveSetting("gamma_index", self.gamma_index)
|
2012-06-26 09:05:49 +00:00
|
|
|
end
|
2012-12-13 18:32:16 +00:00
|
|
|
|
|
|
|
function ReaderFont:setFont(face)
|
2014-03-13 13:52:43 +00:00
|
|
|
if face and self.font_face ~= face then
|
|
|
|
self.font_face = face
|
|
|
|
UIManager:show(Notification:new{
|
2014-11-28 13:10:37 +00:00
|
|
|
text = T( _("Redrawing with font %1."), face),
|
2014-03-13 13:52:43 +00:00
|
|
|
timeout = 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.ui.document:setFontFace(face)
|
|
|
|
-- signal readerrolling to update pos in new height
|
|
|
|
self.ui:handleEvent(Event:new("UpdatePos"))
|
|
|
|
end
|
2012-12-13 18:32:16 +00:00
|
|
|
end
|
|
|
|
|
2014-07-02 14:53:06 +00:00
|
|
|
function ReaderFont:makeDefault(face)
|
|
|
|
if face then
|
2015-09-02 15:21:38 +00:00
|
|
|
UIManager:show(MultiConfirmBox:new{
|
2016-09-25 19:41:35 +00:00
|
|
|
text = T( _("Set %1 as fallback font? Characters not found in the active font are shown in the fallback font instead."), face),
|
2015-09-04 08:59:07 +00:00
|
|
|
choice1_text = _("Default"),
|
2015-09-02 15:21:38 +00:00
|
|
|
choice1_callback = function()
|
2014-07-02 14:53:06 +00:00
|
|
|
G_reader_settings:saveSetting("cre_font", face)
|
|
|
|
end,
|
2015-09-04 08:59:07 +00:00
|
|
|
choice2_text = _("Fallback"),
|
2015-09-02 15:21:38 +00:00
|
|
|
choice2_callback = function()
|
|
|
|
G_reader_settings:saveSetting("fallback_font", face)
|
|
|
|
end,
|
2014-07-02 14:53:06 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-04 13:46:38 +00:00
|
|
|
function ReaderFont:addToMainMenu(menu_items)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- insert table to main reader menu
|
2017-03-04 13:46:38 +00:00
|
|
|
menu_items.change_font = {
|
2014-03-13 13:52:43 +00:00
|
|
|
text = self.font_menu_title,
|
|
|
|
sub_item_table = self.face_table,
|
2017-02-28 21:46:32 +00:00
|
|
|
}
|
2012-12-15 01:23:02 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2017-06-29 18:38:39 +00:00
|
|
|
function ReaderFont:onAdjustSpread(ges)
|
|
|
|
local step = math.ceil(2 * #self.steps * ges.distance / self.gestureScale)
|
|
|
|
local delta_int = self.steps[step] or self.steps[#self.steps]
|
|
|
|
local info = Notification:new{text = _("Increasing font size…")}
|
2017-04-22 07:18:10 +00:00
|
|
|
UIManager:show(info)
|
|
|
|
UIManager:forceRePaint()
|
2017-06-29 18:38:39 +00:00
|
|
|
self:onChangeSize("increase", delta_int)
|
2017-04-22 07:18:10 +00:00
|
|
|
UIManager:close(info)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2017-06-29 18:38:39 +00:00
|
|
|
function ReaderFont:onAdjustPinch(ges)
|
|
|
|
local step = math.ceil(2 * #self.steps * ges.distance / self.gestureScale)
|
|
|
|
local delta_int = self.steps[step] or self.steps[#self.steps]
|
|
|
|
local info = Notification:new{text = _("Decreasing font size…")}
|
2017-04-22 07:18:10 +00:00
|
|
|
UIManager:show(info)
|
|
|
|
UIManager:forceRePaint()
|
2017-06-29 18:38:39 +00:00
|
|
|
self:onChangeSize("decrease", delta_int)
|
2017-04-22 07:18:10 +00:00
|
|
|
UIManager:close(info)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return ReaderFont
|