From a5069f1c2610624b341b2af04d81c6f05444d091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Fern=C3=A1ndez?= Date: Sun, 8 Dec 2019 14:54:14 +0100 Subject: [PATCH] android: make system fonts toggable (#5670) Also add droid font back to android and remove the workaround --- Makefile | 1 - frontend/apps/reader/modules/readerfont.lua | 2 +- frontend/fontlist.lua | 4 +- frontend/ui/elements/font_settings.lua | 95 ++++++++++++--------- frontend/ui/font.lua | 6 -- 5 files changed, 55 insertions(+), 53 deletions(-) diff --git a/Makefile b/Makefile index 44c73afcb..de17e18da 100644 --- a/Makefile +++ b/Makefile @@ -350,7 +350,6 @@ androidupdate: all # assets are compressed manually and stored inside the APK. cd $(INSTALL_DIR)/koreader && zip -r9 \ ../../$(ANDROID_LAUNCHER_DIR)/assets/module/koreader-$(VERSION).zip * \ - --exclude=*fonts/droid* \ --exclude=*resources/fonts* \ --exclude=*resources/icons/src* \ --exclude=*share/man* \ diff --git a/frontend/apps/reader/modules/readerfont.lua b/frontend/apps/reader/modules/readerfont.lua index 214e82d5c..fef150b48 100644 --- a/frontend/apps/reader/modules/readerfont.lua +++ b/frontend/apps/reader/modules/readerfont.lua @@ -50,7 +50,7 @@ function ReaderFont:init() end -- build face_table for menu self.face_table = {} - if Device:isDesktop() then + if Device:isAndroid() or Device:isDesktop() then table.insert(self.face_table, require("ui/elements/font_settings"):getMenuTable()) end local face_list = cre.getFontFaces() diff --git a/frontend/fontlist.lua b/frontend/fontlist.lua index 6a0c8669f..3d56bec47 100644 --- a/frontend/fontlist.lua +++ b/frontend/fontlist.lua @@ -64,9 +64,7 @@ local function isInFontsBlacklist(f) end local function getExternalFontDir() - if CanvasContext.isAndroid() then - return require("frontend/ui/elements/font_settings"):getAndroidPath() - elseif CanvasContext.isDesktop() then + if CanvasContext.isAndroid() or CanvasContext.isDesktop() then return require("frontend/ui/elements/font_settings"):getPath() else return os.getenv("EXT_FONT_DIR") diff --git a/frontend/ui/elements/font_settings.lua b/frontend/ui/elements/font_settings.lua index 3a1f93cdc..540361902 100644 --- a/frontend/ui/elements/font_settings.lua +++ b/frontend/ui/elements/font_settings.lua @@ -5,21 +5,44 @@ local _ = require("gettext") --[[ Font settings for desktop linux, mac and android ]]-- +local ANDROID_SYSTEM_FONT_DIR = "/system/fonts" +local LINUX_SYSTEM_FONT_DIR = "/usr/share/fonts" +local DESKTOP_USER_FONT_DIR = "/.local/share/fonts" + +-- get primary storage on Android +local function getAndroidPrimaryStorage() + local A, android = pcall(require, "android") + if not A then return end + local path = android.getExternalStoragePath() + if path ~= "Unknown" then + -- use the external storage identified by the app + return path + else + -- unable to identify external storage. Use defaults + return "/sdcard" + end +end + +-- user font path, should be rw. On linux/mac it goes under $HOME. +-- on Android it goes in the primary storage (internal/sd) local function getUserDir() - local home = os.getenv("HOME") - if home then - return home.."/.local/share/fonts" + if Device:isDesktop() then + local home = os.getenv("HOME") + if home then return home..DESKTOP_USER_FONT_DIR end + elseif Device:isAndroid() then + local p = getAndroidPrimaryStorage() + return p.."/koreader/fonts;"..p.."/fonts" end end --- System fonts are common in linux +-- system (ttf) fonts are available on linux and android but not on mac local function getSystemDir() - local path = "/usr/share/fonts" - if util.pathExists(path) then - return path - else - -- mac doesn't use ttf fonts - return nil + if Device:isDesktop() then + if util.pathExists(LINUX_SYSTEM_FONT_DIR) then + return LINUX_SYSTEM_FONT_DIR + else return nil end + elseif Device:isAndroid() then + return ANDROID_SYSTEM_FONT_DIR end end @@ -54,43 +77,31 @@ function FontSettings:getPath() return getUserDir() end -function FontSettings:getAndroidPath() - local A, android = pcall(require, "android") - if not A then return end - local system_path = "/system/fonts" - local user_path = android.getExternalStoragePath() - if user_path == "Unknown" then - -- unable to identify external storage. Use defaults - return system_path..";".."/sdcard/fonts;/sdcard/koreader/fonts" - else - -- use the external storage identified by the app - return system_path..";"..user_path.."/fonts;"..user_path.."/koreader/fonts" +function FontSettings:getMenuTable() + local t = {{ + text = _("Enable system fonts"), + checked_func = usesSystemFonts, + callback = function() + G_reader_settings:saveSetting("system_fonts", not usesSystemFonts()) + local UIManager = require("ui/uimanager") + local InfoMessage = require("ui/widget/infomessage") + UIManager:show(InfoMessage:new{ + text = _("This will take effect on next restart.") + }) + end, + }} + + if Device:isDesktop() then table.insert(t, 2, { + text = _("Open fonts folder"), + keep_menu_open = true, + callback = openFontDir, + }) end -end -function FontSettings:getMenuTable() return { text = _("Font settings"), separator = true, - sub_item_table = { - { - text = _("Enable system fonts"), - checked_func = usesSystemFonts, - callback = function() - G_reader_settings:saveSetting("system_fonts", not usesSystemFonts()) - local UIManager = require("ui/uimanager") - local InfoMessage = require("ui/widget/infomessage") - UIManager:show(InfoMessage:new{ - text = _("This will take effect on next restart.") - }) - end, - }, - { - text = _("Open fonts folder"), - keep_menu_open = true, - callback = openFontDir, - }, - } + sub_item_table = t } end diff --git a/frontend/ui/font.lua b/frontend/ui/font.lua index 29d5c95b2..5eb0803c2 100644 --- a/frontend/ui/font.lua +++ b/frontend/ui/font.lua @@ -2,8 +2,6 @@ Font module. ]] -local is_android = pcall(require, "android") - local FontList = require("fontlist") local Freetype = require("ffi/freetype") local Screen = require("device").screen @@ -89,10 +87,6 @@ local Font = { faces = {}, } -if is_android then - table.insert(Font.fallbacks, 3, "DroidSansFallback.ttf") -- for some ancient pre-4.4 Androids -end - -- Synthetized bold strength can be tuned: -- local bold_strength_factor = 1 -- really too bold -- local bold_strength_factor = 1/2 -- bold enough