From 3a957d71e34ff1a80a036f6a03c76602e04f111f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Fern=C3=A1ndez?= Date: Tue, 20 Aug 2019 18:38:02 +0200 Subject: [PATCH] [Desktop] Open writable font dir, toggle system+user/user fonts, fix openLink on mac (#5220) Fixes #5093 --- frontend/apps/reader/modules/readerfont.lua | 3 + frontend/device/generic/device.lua | 7 +- frontend/device/sdl/device.lua | 46 +++++++++++- frontend/document/canvascontext.lua | 1 + frontend/fontlist.lua | 2 + frontend/ui/elements/font_settings.lua | 81 +++++++++++++++++++++ platform/appimage/AppRun | 3 - platform/debian/koreader.sh | 4 - 8 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 frontend/ui/elements/font_settings.lua diff --git a/frontend/apps/reader/modules/readerfont.lua b/frontend/apps/reader/modules/readerfont.lua index 0d7ae877d..eff2727d4 100644 --- a/frontend/apps/reader/modules/readerfont.lua +++ b/frontend/apps/reader/modules/readerfont.lua @@ -49,6 +49,9 @@ function ReaderFont:init() end -- build face_table for menu self.face_table = {} + if Device:isDesktop() then + table.insert(self.face_table, require("ui/elements/font_settings"):getMenuTable()) + end local face_list = cre.getFontFaces() for k,v in ipairs(face_list) do table.insert(self.face_table, { diff --git a/frontend/device/generic/device.lua b/frontend/device/generic/device.lua index a5647733f..c83b888e3 100644 --- a/frontend/device/generic/device.lua +++ b/frontend/device/generic/device.lua @@ -40,6 +40,9 @@ local Device = { canToggleGSensor = no, canToggleMassStorage = no, canUseWAL = yes, -- requires mmap'ed I/O on the target FS + canRestart = yes, + canReboot = no, + canPowerOff = no, -- use these only as a last resort. We should abstract the functionality -- and have device dependent implementations in the corresponting @@ -53,9 +56,7 @@ local Device = { isSonyPRSTUX = no, isSDL = no, isEmulator = no, - canRestart = yes, - canReboot = no, - canPowerOff = no, + isDesktop = no, -- some devices have part of their screen covered by the bezel viewport = nil, diff --git a/frontend/device/sdl/device.lua b/frontend/device/sdl/device.lua index 67f7a6cc2..602a44910 100644 --- a/frontend/device/sdl/device.lua +++ b/frontend/device/sdl/device.lua @@ -6,6 +6,42 @@ local logger = require("logger") local function yes() return true end local function no() return false end +-- xdg-open is used on most linux systems +local function hasXdgOpen() + local std_out = io.popen("xdg-open --version 2>/dev/null") + local version = nil + if std_out ~= nil then + version = std_out:read() + std_out:close() + end + return version ~= nil +end + +-- open is the macOS counterpart +local function hasMacOpen() + local std_out = io.popen("open") + local all = nil + if std_out ~= nil then + all = std_out:read() + std_out:close() + end + return all ~= nil +end + +-- get the name of the binary used to open links +local function getLinkOpener() + local enabled = false + local tool = nil + if jit.os == "Linux" and hasXdgOpen() then + enabled = true + tool = "xdg-open" + elseif jit.os == "OSX" and hasMacOpen() then + enabled = true + tool = "open" + end + return enabled, tool +end + local Device = Generic:new{ model = "SDL", isSDL = yes, @@ -17,10 +53,11 @@ local Device = Generic:new{ needsScreenRefreshAfterResume = no, hasColorScreen = yes, hasEinkScreen = no, - canOpenLink = yes, + canOpenLink = getLinkOpener, openLink = function(self, link) - if not link or type(link) ~= "string" then return end - return os.execute("xdg-open '"..link.."'") == 0 + local enabled, tool = getLinkOpener() + if not enabled or not tool or not link or type(link) ~= "string" then return end + return os.execute(tool.." '"..link.."'") == 0 end, } @@ -28,6 +65,7 @@ local AppImage = Device:new{ model = "AppImage", hasMultitouch = no, hasOTAUpdates = yes, + isDesktop = yes, } local Emulator = Device:new{ @@ -37,10 +75,12 @@ local Emulator = Device:new{ hasFrontlight = yes, hasWifiToggle = yes, hasWifiManager = yes, + isDesktop = yes, } local Linux = Device:new{ model = "Linux", + isDesktop = yes, } local UbuntuTouch = Device:new{ diff --git a/frontend/document/canvascontext.lua b/frontend/document/canvascontext.lua index 68e2cb082..5bd6ee157 100644 --- a/frontend/document/canvascontext.lua +++ b/frontend/document/canvascontext.lua @@ -37,6 +37,7 @@ The following key is required for a device object: function CanvasContext:init(device) self.screen = device.screen self.isAndroid = device.isAndroid + self.isDesktop = device.isDesktop self.isKindle = device.isKindle self.should_restrict_JIT = device.should_restrict_JIT self:setColorRenderingEnabled(device.screen.isColorEnabled()) diff --git a/frontend/fontlist.lua b/frontend/fontlist.lua index 70b2f252c..bb4b0c20c 100644 --- a/frontend/fontlist.lua +++ b/frontend/fontlist.lua @@ -65,6 +65,8 @@ end local function getExternalFontDir() if CanvasContext.isAndroid() then return ANDROID_FONT_DIR + elseif CanvasContext.isDesktop() then + return require("frontend/ui/elements/font_settings"):getPath() else return os.getenv("EXT_FONT_DIR") end diff --git a/frontend/ui/elements/font_settings.lua b/frontend/ui/elements/font_settings.lua new file mode 100644 index 000000000..349c685aa --- /dev/null +++ b/frontend/ui/elements/font_settings.lua @@ -0,0 +1,81 @@ +local Device = require("device") +local logger = require("logger") +local util = require("util") +local _ = require("gettext") + +--[[ Font settings for desktop linux and mac ]]-- + +local function getUserDir() + return os.getenv("HOME").."/.local/share/fonts" +end + +-- System fonts are common in linux +local function getSystemDir() + local path = "/usr/share/fonts" + if util.pathExists(path) then + return path + else + -- mac doesn't use ttf fonts + return nil + end +end + +local function usesSystemFonts() + return G_reader_settings:isTrue("system_fonts") +end + +local function openFontDir() + local user_dir = getUserDir() + local openable = util.pathExists(user_dir) + if not openable then + logger.info("Font path not found, making one in ", user_dir) + openable = util.makePath(user_dir) + end + if not openable then + logger.warn("Unable to create the folder ", user_dir) + return + end + if Device:canOpenLink() then + Device:openLink(user_dir) + end +end + +local FontSettings = {} + +function FontSettings:getPath() + if usesSystemFonts() then + local system_path = getSystemDir() + if system_path ~= nil then + return getUserDir()..";"..system_path + end + end + return getUserDir() +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, + }, + } + } +end + +return FontSettings diff --git a/platform/appimage/AppRun b/platform/appimage/AppRun index b8da7e6a2..7518ca2a3 100755 --- a/platform/appimage/AppRun +++ b/platform/appimage/AppRun @@ -10,9 +10,6 @@ cd "${KOREADER_DIR}" || exit # export load library path export LD_LIBRARY_PATH=${KOREADER_DIR}/libs:$LD_LIBRARY_PATH -# export external font directory -export EXT_FONT_DIR="${HOME}/.fonts" - RETURN_VALUE=85 if [ $# -eq 0 ]; then diff --git a/platform/debian/koreader.sh b/platform/debian/koreader.sh index 972969c75..6f9b88b96 100755 --- a/platform/debian/koreader.sh +++ b/platform/debian/koreader.sh @@ -23,10 +23,6 @@ cd "${KOREADER_DIR}" || exit # export load library path export LD_LIBRARY_PATH=${KOREADER_DIR}/libs:$LD_LIBRARY_PATH -# export external font directory -export EXT_FONT_DIR="${HOME}/.config/koreader/fonts" -[ ! -d "${EXT_FONT_DIR}" ] && mkdir -pv "${EXT_FONT_DIR}" - RETURN_VALUE=85 while [ $RETURN_VALUE -eq 85 ]; do ./reader.lua "${ARGS}"