[Desktop] Open writable font dir, toggle system+user/user fonts, fix openLink on mac (#5220)

Fixes #5093
pull/5233/head
Martín Fernández 5 years ago committed by Frans de Jonge
parent 8be113e4bd
commit 3a957d71e3

@ -49,6 +49,9 @@ function ReaderFont:init()
end end
-- build face_table for menu -- build face_table for menu
self.face_table = {} self.face_table = {}
if Device:isDesktop() then
table.insert(self.face_table, require("ui/elements/font_settings"):getMenuTable())
end
local face_list = cre.getFontFaces() local face_list = cre.getFontFaces()
for k,v in ipairs(face_list) do for k,v in ipairs(face_list) do
table.insert(self.face_table, { table.insert(self.face_table, {

@ -40,6 +40,9 @@ local Device = {
canToggleGSensor = no, canToggleGSensor = no,
canToggleMassStorage = no, canToggleMassStorage = no,
canUseWAL = yes, -- requires mmap'ed I/O on the target FS 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 -- use these only as a last resort. We should abstract the functionality
-- and have device dependent implementations in the corresponting -- and have device dependent implementations in the corresponting
@ -53,9 +56,7 @@ local Device = {
isSonyPRSTUX = no, isSonyPRSTUX = no,
isSDL = no, isSDL = no,
isEmulator = no, isEmulator = no,
canRestart = yes, isDesktop = no,
canReboot = no,
canPowerOff = no,
-- some devices have part of their screen covered by the bezel -- some devices have part of their screen covered by the bezel
viewport = nil, viewport = nil,

@ -6,6 +6,42 @@ local logger = require("logger")
local function yes() return true end local function yes() return true end
local function no() return false 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{ local Device = Generic:new{
model = "SDL", model = "SDL",
isSDL = yes, isSDL = yes,
@ -17,10 +53,11 @@ local Device = Generic:new{
needsScreenRefreshAfterResume = no, needsScreenRefreshAfterResume = no,
hasColorScreen = yes, hasColorScreen = yes,
hasEinkScreen = no, hasEinkScreen = no,
canOpenLink = yes, canOpenLink = getLinkOpener,
openLink = function(self, link) openLink = function(self, link)
if not link or type(link) ~= "string" then return end local enabled, tool = getLinkOpener()
return os.execute("xdg-open '"..link.."'") == 0 if not enabled or not tool or not link or type(link) ~= "string" then return end
return os.execute(tool.." '"..link.."'") == 0
end, end,
} }
@ -28,6 +65,7 @@ local AppImage = Device:new{
model = "AppImage", model = "AppImage",
hasMultitouch = no, hasMultitouch = no,
hasOTAUpdates = yes, hasOTAUpdates = yes,
isDesktop = yes,
} }
local Emulator = Device:new{ local Emulator = Device:new{
@ -37,10 +75,12 @@ local Emulator = Device:new{
hasFrontlight = yes, hasFrontlight = yes,
hasWifiToggle = yes, hasWifiToggle = yes,
hasWifiManager = yes, hasWifiManager = yes,
isDesktop = yes,
} }
local Linux = Device:new{ local Linux = Device:new{
model = "Linux", model = "Linux",
isDesktop = yes,
} }
local UbuntuTouch = Device:new{ local UbuntuTouch = Device:new{

@ -37,6 +37,7 @@ The following key is required for a device object:
function CanvasContext:init(device) function CanvasContext:init(device)
self.screen = device.screen self.screen = device.screen
self.isAndroid = device.isAndroid self.isAndroid = device.isAndroid
self.isDesktop = device.isDesktop
self.isKindle = device.isKindle self.isKindle = device.isKindle
self.should_restrict_JIT = device.should_restrict_JIT self.should_restrict_JIT = device.should_restrict_JIT
self:setColorRenderingEnabled(device.screen.isColorEnabled()) self:setColorRenderingEnabled(device.screen.isColorEnabled())

@ -65,6 +65,8 @@ end
local function getExternalFontDir() local function getExternalFontDir()
if CanvasContext.isAndroid() then if CanvasContext.isAndroid() then
return ANDROID_FONT_DIR return ANDROID_FONT_DIR
elseif CanvasContext.isDesktop() then
return require("frontend/ui/elements/font_settings"):getPath()
else else
return os.getenv("EXT_FONT_DIR") return os.getenv("EXT_FONT_DIR")
end end

@ -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

@ -10,9 +10,6 @@ cd "${KOREADER_DIR}" || exit
# export load library path # export load library path
export LD_LIBRARY_PATH=${KOREADER_DIR}/libs:$LD_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 RETURN_VALUE=85
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then

@ -23,10 +23,6 @@ cd "${KOREADER_DIR}" || exit
# export load library path # export load library path
export LD_LIBRARY_PATH=${KOREADER_DIR}/libs:$LD_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 RETURN_VALUE=85
while [ $RETURN_VALUE -eq 85 ]; do while [ $RETURN_VALUE -eq 85 ]; do
./reader.lua "${ARGS}" ./reader.lua "${ARGS}"

Loading…
Cancel
Save