From 1a918935769d249913933a0eeb293e2c172e84bc Mon Sep 17 00:00:00 2001 From: poire-z Date: Tue, 28 Apr 2020 23:57:10 +0200 Subject: [PATCH] Allow toggling use of additional fallback fonts (#6095) --- frontend/apps/reader/modules/readerfont.lua | 109 +++++++++++++----- frontend/document/credocument.lua | 7 ++ .../ui/elements/font-test-sample-default.html | 62 ++++++++++ frontend/ui/elements/font_settings.lua | 8 +- 4 files changed, 148 insertions(+), 38 deletions(-) create mode 100644 frontend/ui/elements/font-test-sample-default.html diff --git a/frontend/apps/reader/modules/readerfont.lua b/frontend/apps/reader/modules/readerfont.lua index a1287101f..af8c39cc6 100644 --- a/frontend/apps/reader/modules/readerfont.lua +++ b/frontend/apps/reader/modules/readerfont.lua @@ -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
...
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
...
allows for a TOC to be built by crengine f:write(string.format([[ @@ -400,10 +445,10 @@ a { color: black; } f:write("\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, }) diff --git a/frontend/document/credocument.lua b/frontend/document/credocument.lua index bb0bbd999..c396522ff 100644 --- a/frontend/document/credocument.lua +++ b/frontend/document/credocument.lua @@ -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, "|") diff --git a/frontend/ui/elements/font-test-sample-default.html b/frontend/ui/elements/font-test-sample-default.html new file mode 100644 index 000000000..1cf84b365 --- /dev/null +++ b/frontend/ui/elements/font-test-sample-default.html @@ -0,0 +1,62 @@ +

Leonardo di ser Piero da Vinci (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).

+ +

Leonardo is renowned primarily as a painter. The Mona Lisa +is the most famous of his works 34 +and the most popular portrait ever made. The Last Supper +is the most reproduced religious painting of all +time35 and his +Vitruvian Man drawing is regarded as a cultural icon as +well36. Salvator Mundi was +sold for a world record $450.3 million at a Christie's auction +in New York, 15 November 2017, the highest price +ever paid for a work of art. 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.

+

 

+ +
Above is some random text to appreciate lines height, and how +much height or pages the same text occupy with each font — +with bits in bold and italic, numbers and footnote links.
+
 
+ +
These are some numbers ans possibly ambiguous letters and symbols:
+427, 809, 1635, 1IlLi, oO0, _-=+*#$~,;:!()[]{} "'«»
+
 
+ +
Next are some bits to appreciate this font's OpenType features:
+This might show some ligatures: afflicting filling.
+This line might be in Small Caps.
+Lining numbers 123456 vs +123456 oldstyle numbers. +
+
 
+ +
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 关门吃及刃, +and the same text in traditional chinese 关门吃及刃 +or in japanese 关门吃及刃. +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.
+
 
+ +
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:
+koreader/settings/font-test-sample.html +
diff --git a/frontend/ui/elements/font_settings.lua b/frontend/ui/elements/font_settings.lua index 4d7c8a467..c3aa64cb7 100644 --- a/frontend/ui/elements/font_settings.lua +++ b/frontend/ui/elements/font_settings.lua @@ -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