Allow toggling use of additional fallback fonts (#6095)

reviewable/pr6103/r1
poire-z 4 years ago committed by GitHub
parent e5b2c2953b
commit 1a91893576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -49,11 +49,15 @@ function ReaderFont:init()
event = "ChangeLineSpace", args = "decrease" },
}
end
-- build face_table for menu
-- Build face_table for menu
self.face_table = {}
if Device:isAndroid() or Device:isDesktop() or Device:isEmulator() then
table.insert(self.face_table, require("ui/elements/font_settings"):getMenuTable())
end
-- Font settings
table.insert(self.face_table, {
text = _("Font settings"),
sub_item_table = self:getFontSettingsTable(),
separator = true,
})
-- Font list
local face_list = cre.getFontFaces()
for k,v in ipairs(face_list) do
table.insert(self.face_table, {
@ -82,20 +86,6 @@ function ReaderFont:init()
})
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 an HTML document showing some sample text rendered with each available font?");
ok_callback = function()
self:buildFontsTestDocument()
end
})
end
})
end
self.ui.menu:registerToMainMenu(self)
end
@ -346,24 +336,79 @@ function ReaderFont:onAdjustFontSize(ges, direction)
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"
function ReaderFont:getFontSettingsTable()
local settings_table = {}
if Device:isAndroid() or Device:isDesktop() or Device:isEmulator() then
for _, item in ipairs(require("ui/elements/font_settings"):getSystemFontMenuItems()) do
table.insert(settings_table, item)
end
settings_table[#settings_table].separator = true
end
table.insert(settings_table, {
text = _("Use additional fallback fonts"),
checked_func = function()
return G_reader_settings:nilOrTrue("additional_fallback_fonts")
end,
callback = function()
G_reader_settings:flipNilOrTrue("additional_fallback_fonts")
self.ui.document:setupFallbackFontFaces()
self.ui:handleEvent(Event:new("UpdatePos"))
end,
help_text = T(_([[
Enable additional fallback fonts, for the most complete script and language coverage.
These fonts will be used in this order:
%1
You can set a preferred fallback font with a long-press on a font name, and it will be used before these.
If that font happens to be part of this list already, it will be used first.]]),
table.concat(self.ui.document.fallback_fonts, "\n")),
separator = true,
})
table.insert(settings_table, {
text = _("Generate font test document"),
callback = function()
UIManager:show(ConfirmBox:new{
text = _("Would you like to generate an HTML document showing some sample text rendered with each available font?");
ok_callback = function()
self:buildFontsTestDocument()
end
})
end,
})
return settings_table
end
-- Default sample file
local FONT_TEST_DEFAULT_SAMPLE_PATH = "frontend/ui/elements/font-test-sample-default.html"
-- Users can set their own sample file, that will be used if found
local FONT_TEST_USER_SAMPLE_PATH = require("datastorage"):getSettingsDir() .. "/font-test-sample.html"
-- This document will be generated in the home or default directory
local FONT_TEST_FINAL_FILENAME = "font-test.html"
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 html_sample
local f = io.open(FONT_TEST_USER_SAMPLE_PATH, "r")
if f then
html_sample = f:read("*all")
f:close()
end
if not html_sample then
f = io.open(FONT_TEST_DEFAULT_SAMPLE_PATH, "r")
if not f then return nil end
html_sample = f:read("*all")
f:close()
end
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
local font_test_final_path = dir .. "/" .. FONT_TEST_FINAL_FILENAME
f = io.open(font_test_final_path, "w")
if not f then return end
-- Using <section><title>...</title></section> allows for a TOC to be built by crengine
f:write(string.format([[
<?xml version="1.0" encoding="UTF-8"?>
<html>
@ -400,10 +445,10 @@ a { color: black; }
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?"), BD.filepath(fonts_test_path)),
text = T(_("Document created as:\n%1\n\nWould you like to read it now?"), BD.filepath(font_test_final_path)),
ok_callback = function()
UIManager:scheduleIn(1.0, function()
self.ui:switchDocument(fonts_test_path)
self.ui:switchDocument(font_test_final_path)
end)
end,
})

@ -639,6 +639,13 @@ function CreDocument:setupFallbackFontFaces()
seen_fonts[font_name] = true
end
end
if G_reader_settings:isFalse("additional_fallback_fonts") then
-- Keep the first fallback font (user set or first from self.fallback_fonts),
-- as crengine won't reset its current set when provided with an empty string
for i=#fallbacks, 2, -1 do
table.remove(fallbacks, i)
end
end
-- We use '|' as the delimiter (which is less likely to be found in font
-- names than ',' or ';', without the need to have to use quotes.
local s_fallbacks = table.concat(fallbacks, "|")

@ -0,0 +1,62 @@
<p><b>Leonardo di ser Piero da Vinci</b> (Italian: [leoˈnardo di
ˌsɛr ˈpjɛːro da (v)ˈvintʃi]; 14/15 April 1452 2
May 1519), known as Leonardo da Vinci, was an Italian polymath of the
Renaissance whose areas of interest included «invention, drawing,
painting, sculpture, architecture, science, music, mathematics,
engineering, literature, anatomy, geology, astronomy, botany,
paleontology, and cartography». He has been variously called the
father of palaeontology, ichnology, and architecture, and is widely
considered one of the greatest painters of all time (despite perhaps
only 15 of his paintings having survived).</p>
<p>Leonardo is renowned primarily as a painter. The <i>Mona Lisa</i>
is the most famous of his works <a href="#footnote34"><sup>34</sup></a>
and the most popular portrait ever made. <i>The Last Supper</i>
is the most reproduced religious painting of all
time<a href="#footnote35"><sup>35</sup></a> and his
Vitruvian Man drawing is regarded as a cultural icon as
well<a href="#footnote36"><sup>36</sup></a>. Salvator Mundi was
sold for a world record $450.3 million at a Christie's auction
in New York, 15 November 2017, the <a href="nolink">highest price</a>
ever paid for a work of art. <i>Leonardo's paintings and preparatory
drawings—together with his notebooks, which contain sketches,
scientific diagrams, and his thoughts on the nature of
painting—compose a contribution to later generations of artists
rivalled only by that of his contemporary Michelangelo.</i></p>
<p>&nbsp;</p>
<div>Above is some random text to appreciate lines height, and how
much height or pages the same text occupy with each font &mdash;
with bits in bold and italic, numbers and footnote links.</div>
<div>&nbsp;</div>
<div>These are some numbers ans possibly ambiguous letters and symbols:<br/>
427, 809, 1635, 1IlLi, oO0, _-=+*#$~,;:!()[]{} "'«»</div>
<div>&nbsp;</div>
<div>Next are some bits to appreciate this font's OpenType features:<br/>
This might show some ligatures: afflicting filling.<br/>
<span style="font-variant: small-caps ">This line might be in Small Caps</span>.<br/>
Lining numbers <span style="font-variant: lining-nums">123456</span> vs
<span style="font-variant: oldstyle-nums">123456</span> oldstyle numbers.
</div>
<div>&nbsp;</div>
<div>Finally, some words in various scripts to see how the baseline
and height of this font and those of the fallback fonts compare:
a few classic greek words are νους, intelligence, ειδος, form,
and λογος, which have nothing to do with the following text
in simplified chinese <span lang="zh-CN">关门吃及刃</span>,
and the same text in traditional chinese <span lang="zh-Hant">关门吃及刃</span>
or in japanese <span lang="ja">关门吃及刃</span>.
Nothing in common either with these RTL and BiDi bits
in hebrew סֶּלַע ססס
and in arabic الرحيم (123 456) الْحَمْدُ and وال (بااتي: abcd) من.
This should be enough to appreciate this font.</div>
<div>&nbsp;</div>
<div>This is some sample content. You can have this document generated
with some text of yours in your prefered language(s) by creating and
adding it to a file named:<br/>
<code>koreader/settings/font-test-sample.html</code>
</div>

@ -77,7 +77,7 @@ function FontSettings:getPath()
return getUserDir()
end
function FontSettings:getMenuTable()
function FontSettings:getSystemFontMenuItems()
local t = {{
text = _("Enable system fonts"),
checked_func = usesSystemFonts,
@ -98,11 +98,7 @@ function FontSettings:getMenuTable()
})
end
return {
text = _("Font settings"),
separator = true,
sub_item_table = t
}
return t
end
return FontSettings

Loading…
Cancel
Save