2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/apps/reader/modules/readerfont.lua

240 lines
7.7 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
local Notification = require("ui/widget/notification")
local ConfirmBox = require("ui/widget/confirmbox")
local Menu = require("ui/widget/menu")
2013-10-18 20:38:07 +00:00
local Device = require("ui/device")
local Screen = require("ui/screen")
local Input = require("ui/input")
local Event = require("ui/event")
2013-10-18 20:38:07 +00:00
local UIManager = require("ui/uimanager")
local Screen = require("ui/screen")
local DEBUG = require("dbg")
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 = _("Change font"),
face_table = nil,
-- default gamma from crengine's lvfntman.cpp
gamma_index = nil,
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,
2014-03-13 13:52:43 +00:00
})
face_list[k] = {text = v}
end
self.ui.menu:registerToMainMenu(self)
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)
--@TODO change this! 12.01 2013 (houqp)
self.font_size = config:readSetting("font_size") or DCREREADER_CONFIG_DEFAULT_FONT_SIZE
2014-03-13 13:52:43 +00:00
self.ui.document:setFontSize(Screen:scaleByDPI(self.font_size))
self.font_embolden = config:readSetting("font_embolden") or 0
self.ui.document:toggleFontBolder(self.font_embolden)
2014-03-13 13:52:43 +00:00
self.line_space_percent = config:readSetting("line_space_percent") or 100
self.ui.document:setInterlineSpacePercent(self.line_space_percent)
self.gamma_index = config:readSetting("gamma_index") or 15
2014-03-13 13:52:43 +00:00
self.ui.document:setGammaIndex(self.gamma_index)
-- Dirty hack: we have to add folloing call in order to set
-- 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.
--]]
function ReaderFont:onChangeSize(direction)
2014-03-13 13:52:43 +00:00
local delta = direction == "decrease" and -1 or 1
self.font_size = self.font_size + delta
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
UIManager:show(Notification:new{
text = _("Set font size to ")..self.font_size,
timeout = 1,
})
self.ui.document:setFontSize(Screen:scaleByDPI(new_size))
self.ui:handleEvent(Event:new("UpdatePos"))
return true
end
function ReaderFont:onChangeLineSpace(direction)
2014-03-13 13:52:43 +00:00
local msg = ""
if direction == "decrease" then
self.line_space_percent = self.line_space_percent - 10
-- NuPogodi, 15.05.12: reduce lowest space_percent to 80
self.line_space_percent = math.max(self.line_space_percent, 80)
msg = _("Decrease line space to ")
else
self.line_space_percent = self.line_space_percent + 10
self.line_space_percent = math.min(self.line_space_percent, 200)
msg = _("Increase line space to ")
end
UIManager:show(Notification:new{
text = msg..self.line_space_percent.."%",
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
2013-01-07 21:42:23 +00:00
function ReaderFont:onChangeFontGamma(direction)
2014-03-13 13:52:43 +00:00
local msg = ""
if direction == "increase" then
cre.setGammaIndex(self.gamma_index+2)
msg = _("Increase gamma to ")
elseif direction == "decrease" then
cre.setGammaIndex(self.gamma_index-2)
msg = _("Decrease gamma to ")
end
self.gamma_index = cre.getGammaIndex()
UIManager:show(Notification:new{
text = msg..self.gamma_index,
timeout = 1
})
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)
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 = _("Redrawing with font ")..face,
timeout = 1,
})
self.ui.document:setFontFace(face)
-- signal readerrolling to update pos in new height
self.ui:handleEvent(Event:new("UpdatePos"))
UIManager:close(msg)
end
end
function ReaderFont:makeDefault(face)
if face then
UIManager:show(ConfirmBox:new{
2014-07-04 11:29:45 +00:00
text = _("Set default font to ")..face.."?",
ok_callback = function()
G_reader_settings:saveSetting("cre_font", face)
end,
})
end
end
2013-03-14 05:58:42 +00:00
function ReaderFont:addToMainMenu(tab_item_table)
2014-03-13 13:52:43 +00:00
-- insert table to main reader menu
table.insert(tab_item_table.typeset, {
text = self.font_menu_title,
sub_item_table = self.face_table,
})
end
2013-10-18 20:38:07 +00:00
return ReaderFont