2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/frontend/apps/reader/modules/readerfont.lua

302 lines
10 KiB
Lua
Raw Normal View History

local CenterContainer = require("ui/widget/container/centercontainer")
local Device = require("device")
local Event = require("ui/event")
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")
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,
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
}
function ReaderFont:init()
2014-03-13 13:52:43 +00:00
if Device:hasKeyboard() then
-- add shortcut for keyboard
self.key_events = {
ShowFontMenu = { {"F"}, doc = "show font menu" },
2014-03-13 13:52:43 +00:00
IncreaseSize = {
{ "Shift", Input.group.PgFwd },
doc = "increase font size",
2014-03-13 13:52:43 +00:00
event = "ChangeSize", args = "increase" },
DecreaseSize = {
{ "Shift", Input.group.PgBack },
doc = "decrease font size",
2014-03-13 13:52:43 +00:00
event = "ChangeSize", args = "decrease" },
IncreaseLineSpace = {
{ "Alt", Input.group.PgFwd },
doc = "increase line space",
2014-03-13 13:52:43 +00:00
event = "ChangeLineSpace", args = "increase" },
DecreaseLineSpace = {
{ "Alt", Input.group.PgBack },
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)
end,
hold_callback = function()
self:makeDefault(v)
end,
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)
end
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
},
{
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
},
})
end
end
function ReaderFont:onSetDimensions(dimen)
2014-03-13 13:52:43 +00:00
self.dimen = dimen
end
function ReaderFont:onReadSettings(config)
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)
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)
self.font_size = config:readSetting("font_size")
or G_reader_settings:readSetting("copt_font_size")
or DCREREADER_CONFIG_DEFAULT_FONT_SIZE or 22
self.ui.document:setFontSize(Screen:scaleBySize(self.font_size))
2014-03-13 13:52:43 +00:00
self.font_embolden = config:readSetting("font_embolden")
or G_reader_settings:readSetting("copt_font_weight") or 0
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)
self.line_space_percent = config:readSetting("line_space_percent")
or G_reader_settings:readSetting("copt_line_spacing")
or DCREREADER_CONFIG_LINE_SPACE_PERCENT_MEDIUM
self.ui.document:setInterlineSpacePercent(self.line_space_percent)
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)
-- 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)
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
main_menu.show_parent = menu_container
2014-03-13 13:52:43 +00:00
UIManager:show(menu_container)
2014-03-13 13:52:43 +00:00
return true
end
--[[
2014-03-13 13:52:43 +00:00
UpdatePos event is used to tell ReaderRolling to update pos.
--]]
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
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
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{
text = T( _("Font size set to %1."), self.font_size),
2014-03-13 13:52:43 +00:00
timeout = 1,
})
2014-03-13 13:52:43 +00:00
return true
end
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{
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
end
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
function ReaderFont:onSetFontGamma(gamma)
self.gamma_index = gamma
2014-03-13 13:52:43 +00:00
UIManager:show(Notification:new{
text = T( _("Font gamma set to %1."), self.gamma_index),
2014-03-13 13:52:43 +00:00
timeout = 1
})
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)
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)
end
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{
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
end
function ReaderFont:makeDefault(face)
if face then
UIManager:show(MultiConfirmBox:new{
text = T( _("Set %1 as fallback font? Characters not found in the active font are shown in the fallback font instead."), face),
choice1_text = _("Default"),
choice1_callback = function()
G_reader_settings:saveSetting("cre_font", face)
end,
choice2_text = _("Fallback"),
choice2_callback = function()
G_reader_settings:saveSetting("fallback_font", face)
end,
})
end
end
function ReaderFont:addToMainMenu(menu_items)
2014-03-13 13:52:43 +00:00
-- insert table to main reader menu
menu_items.change_font = {
2014-03-13 13:52:43 +00:00
text = self.font_menu_title,
sub_item_table = self.face_table,
}
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…")}
UIManager:show(info)
UIManager:forceRePaint()
2017-06-29 18:38:39 +00:00
self:onChangeSize("increase", delta_int)
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…")}
UIManager:show(info)
UIManager:forceRePaint()
2017-06-29 18:38:39 +00:00
self:onChangeSize("decrease", delta_int)
UIManager:close(info)
return true
end
2013-10-18 20:38:07 +00:00
return ReaderFont