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

android: make system fonts toggable #5670

Merged
merged 2 commits into from Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion Makefile
Expand Up @@ -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* \
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/reader/modules/readerfont.lua
Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions frontend/fontlist.lua
Expand Up @@ -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")
Expand Down
95 changes: 53 additions & 42 deletions frontend/ui/elements/font_settings.lua
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions frontend/ui/font.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down