mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
[Desktop] Open writable font dir, toggle system+user/user fonts, fix openLink on mac (#5220)
Fixes #5093
This commit is contained in:
committed by
Frans de Jonge
parent
8be113e4bd
commit
3a957d71e3
@@ -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, {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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
|
||||
|
||||
81
frontend/ui/elements/font_settings.lua
Normal file
81
frontend/ui/elements/font_settings.lua
Normal file
@@ -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 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
|
||||
|
||||
@@ -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}"
|
||||
|
||||
Reference in New Issue
Block a user