Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font menu: add symbols for default and fallback fonts #3941

Merged
merged 7 commits into from May 11, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 99 additions & 5 deletions frontend/apps/reader/modules/readerfont.lua
@@ -1,4 +1,5 @@
local CenterContainer = require("ui/widget/container/centercontainer")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local Event = require("ui/event")
local Input = Device.input
Expand Down Expand Up @@ -51,19 +52,46 @@ function ReaderFont:init()
local face_list = cre.getFontFaces()
for k,v in ipairs(face_list) do
table.insert(self.face_table, {
text = v,
text_func = function()
-- defaults are hardcoded in credocument.lua
local default_font = G_reader_settings:readSetting("cre_font") or self.ui.document.default_font
local fallback_font = G_reader_settings:readSetting("fallback_font") or self.ui.document.fallback_font
local text = v
if v == default_font then
text = text .. " ★"
end
if v == fallback_font then
text = text.." ♻"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recycling? :-P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much choice with what's available in Noto fonts (including the updated ones, seen no other symbols than the ones I'm used to).
test_unichars.html.txt : sample file where I do my icon shopping.

Copy link
Member

@NiLuJe NiLuJe May 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a (couple of) relatively small NotoSymbols fonts that we could pull if you fancy something in there... ;).

Getting the relevant menu entry to use them over the default UI font might get tricky, though...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should be fine with what we have.
Btw, the greek glyphs have been corrected in your updated Noto Sans CJK, and they have also been included in the main Noto Sans, so good idea to update them :)

end
return text
end,
callback = function()
self:setFont(v)
end,
hold_callback = function()
self:makeDefault(v)
hold_may_update_menu = true,
hold_callback = function(refresh_menu_func)
self:makeDefault(v, refresh_menu_func)
end,
checked_func = function()
return v == self.font_face
end
})
face_list[k] = {text = v}
end
if self:hasFontsTestSample() then
self.face_table[#self.face_table].separator = true
table.insert(self.face_table, {
text = _("Generate fonts test html document"),
callback = function()
UIManager:show(ConfirmBox:new{
text = _("Would you like to generate a html document showing some sample text rendered with each available font?");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an HTML document

ok_callback = function()
self:buildFontsTestDocument()
end
})
end
})
end
self.ui.menu:registerToMainMenu(self)
end

Expand Down Expand Up @@ -100,10 +128,12 @@ end

function ReaderFont:onReadSettings(config)
self.font_face = config:readSetting("font_face")
or G_reader_settings:readSetting("cre_font")
or self.ui.document.default_font
self.ui.document:setFontFace(self.font_face)

self.header_font_face = config:readSetting("header_font_face")
or G_reader_settings:readSetting("header_font")
or self.ui.document.header_font
self.ui.document:setHeaderFont(self.header_font_face)

Expand Down Expand Up @@ -256,17 +286,22 @@ function ReaderFont:setFont(face)
end
end

function ReaderFont:makeDefault(face)
function ReaderFont:makeDefault(face, refresh_menu_func)
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)
if refresh_menu_func then refresh_menu_func() end
end,
choice2_text = _("Fallback"),
choice2_callback = function()
G_reader_settings:saveSetting("fallback_font", face)
if self.ui.document:setFallbackFontFace(face) then
G_reader_settings:saveSetting("fallback_font", face)
self.ui:handleEvent(Event:new("UpdatePos"))
end
if refresh_menu_func then refresh_menu_func() end
end,
})
end
Expand Down Expand Up @@ -302,4 +337,63 @@ function ReaderFont:onAdjustPinch(ges)
return true
end

function ReaderFont:hasFontsTestSample()
local font_test_sample = require("datastorage"):getSettingsDir() .. "/fonts-test-sample.html"
local lfs = require("libs/libkoreader-lfs")
return lfs.attributes(font_test_sample, "mode") == "file"
end

function ReaderFont:buildFontsTestDocument()
local font_test_sample = require("datastorage"):getSettingsDir() .. "/fonts-test-sample.html"
local f = io.open(font_test_sample, "r")
if not f then return nil end
local html_sample = f:read("*all")
f:close()
local dir = G_reader_settings:readSetting("home_dir")
if not dir then dir = require("apps/filemanager/filemanagerutil").getDefaultDir() end
if not dir then dir = "." end
local fonts_test_path = dir .. "/fonts-test-all.html"
f = io.open(fonts_test_path, "w")
-- Using <section><title>...</title></section> allows for a TOC to be built
f:write(string.format([[
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<title>%s</title>
</head>
<body>
<section id="list"><title>%s</title></section>
]], _("Available fonts test document"), _("AVAILABLE FONTS")))
local face_list = cre.getFontFaces()
f:write("<div style='margin: 2em'>\n")
for _, font_name in ipairs(face_list) do
local font_id = font_name:gsub(" ", "_"):gsub("'", "_")
f:write(string.format(" <div><a href='#%s'>%s</a></div>\n", font_id, font_name))
end
f:write("</div>\n\n")
for _, font_name in ipairs(face_list) do
local font_id = font_name:gsub(" ", "_"):gsub("'", "_")
f:write(string.format("<section id='%s'><title>%s</title></section>\n", font_id, font_name))
f:write(string.format("<div style='font-family: %s'>\n", font_name))
f:write(html_sample)
f:write("\n</div>\n\n")
end
f:write("</body></html>\n")
f:close()
UIManager:show(ConfirmBox:new{
text = T(_("Document created as:\n%1\n\nWould you like to read it now?"), fonts_test_path),
ok_callback = function()
-- close current ReaderUI in 1 sec, and create a new one
UIManager:scheduleIn(1.0, function()
local ReaderUI = require("apps/reader/readerui")
local reader = ReaderUI:_getRunningInstance()
if reader then
reader:onClose()
end
ReaderUI:showReader(fonts_test_path)
end)
end,
})
end

return ReaderFont
25 changes: 21 additions & 4 deletions frontend/document/credocument.lua
Expand Up @@ -23,9 +23,9 @@ local CreDocument = Document:new{
_cre_dom_version = nil,

line_space_percent = 100,
default_font = G_reader_settings:readSetting("cre_font") or "Noto Serif",
header_font = G_reader_settings:readSetting("header_font") or "Noto Sans",
fallback_font = G_reader_settings:readSetting("fallback_font") or "Noto Sans CJK SC",
default_font = "Noto Serif",
header_font = "Noto Sans",
fallback_font = "Noto Sans CJK SC",
default_css = "./data/cr3.css",
options = CreOptions,
provider = "crengine",
Expand Down Expand Up @@ -128,7 +128,8 @@ function CreDocument:init()
self._document:adjustFontSizes(Screen:getDPI())

-- set fallback font face
self._document:setStringProperty("crengine.font.fallback.face", self.fallback_font)
self._document:setStringProperty("crengine.font.fallback.face",
G_reader_settings:readSetting("fallback_font") or self.fallback_font)

-- We would have liked to call self._document:loadDocument(self.file)
-- here, to detect early if file is a supported document, but we
Expand Down Expand Up @@ -406,6 +407,22 @@ function CreDocument:setFontFace(new_font_face)
end
end

function CreDocument:setFallbackFontFace(new_fallback_font_face)
if new_fallback_font_face then
logger.dbg("CreDocument: set fallback font face", new_fallback_font_face)
self._document:setStringProperty("crengine.font.fallback.face", new_fallback_font_face)
-- crengine may not accept our fallback font, we need to check
local set_fallback_font_face = self._document:getStringProperty("crengine.font.fallback.face")
logger.dbg("CreDocument: crengine fallback font face", set_fallback_font_face)
if set_fallback_font_face ~= new_fallback_font_face then
logger.info("CreDocument:", new_fallback_font_face, "is not usable as a fallback font")
return false
end
self.fallback_font = new_fallback_font_face
return true
end
end

function CreDocument:setHyphDictionary(new_hyph_dictionary)
if new_hyph_dictionary then
logger.dbg("CreDocument: set hyphenation dictionary", new_hyph_dictionary)
Expand Down
8 changes: 6 additions & 2 deletions frontend/ui/widget/touchmenu.lua
Expand Up @@ -726,8 +726,12 @@ function TouchMenu:onMenuHold(item)
end
if callback then
UIManager:scheduleIn(0.1, function()
self:closeMenu()
callback()
if item.hold_may_update_menu then
callback(function() self:updateItems() end)
else
self:closeMenu()
callback()
end
end)
end
end
Expand Down