2019-03-03 02:50:05 +00:00
|
|
|
local CanvasContext = require("document/canvascontext")
|
2020-10-20 04:30:41 +00:00
|
|
|
local DataStorage = require("datastorage")
|
|
|
|
local FT = require("ffi/freetype")
|
|
|
|
local HB = require("ffi/harfbuzz")
|
2021-01-13 10:45:30 +00:00
|
|
|
local Persist = require("persist")
|
2020-10-20 04:30:41 +00:00
|
|
|
local util = require("util")
|
2022-09-14 01:49:50 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2020-10-20 04:30:41 +00:00
|
|
|
local logger = require("logger")
|
2019-03-03 02:50:05 +00:00
|
|
|
|
|
|
|
local FontList = {
|
|
|
|
fontdir = "./fonts",
|
2021-04-09 16:30:51 +00:00
|
|
|
cachedir = DataStorage:getDataDir() .. "/cache/fontlist", -- in a subdirectory, so as not to mess w/ the Cache module.
|
2019-08-24 09:11:30 +00:00
|
|
|
fontlist = {},
|
2020-10-20 04:30:41 +00:00
|
|
|
fontinfo = {},
|
|
|
|
fontnames = {},
|
2019-03-03 02:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
--[[
|
|
|
|
These non-LGC Kindle system fonts fail CRe's moronic header check.
|
2020-03-14 10:14:23 +00:00
|
|
|
Also applies to a number of LGC fonts that have different family names for different styles...
|
|
|
|
(Those are actually "fixed" via FontConfig in the stock system).
|
2019-03-03 02:50:05 +00:00
|
|
|
--]]
|
|
|
|
local kindle_fonts_blacklist = {
|
|
|
|
["DiwanMuna-Bold.ttf"] = true,
|
|
|
|
["DiwanMuna-Regular.ttf"] = true,
|
|
|
|
["HYGothicBold.ttf"] = true,
|
|
|
|
["HYGothicMedium.ttf"] = true,
|
|
|
|
["HYMyeongJoBold.ttf"] = true,
|
|
|
|
["HYMyeongJoMedium.ttf"] = true,
|
|
|
|
["KindleBlackboxBoldItalic.ttf"] = true,
|
|
|
|
["KindleBlackboxBold.ttf"] = true,
|
|
|
|
["KindleBlackboxItalic.ttf"] = true,
|
|
|
|
["KindleBlackboxRegular.ttf"] = true,
|
|
|
|
["Kindle_MonospacedSymbol.ttf"] = true,
|
|
|
|
["Kindle_Symbol.ttf"] = true,
|
|
|
|
["MTChineseSurrogates.ttf"] = true,
|
|
|
|
["MYingHeiTBold.ttf"] = true,
|
|
|
|
["MYingHeiTMedium.ttf"] = true,
|
|
|
|
["NotoNaskhArabicUI-Bold.ttf"] = true,
|
|
|
|
["NotoNaskhArabicUI-Regular.ttf"] = true,
|
|
|
|
["NotoNaskh-Bold.ttf"] = true,
|
|
|
|
["NotoNaskh-Regular.ttf"] = true,
|
|
|
|
["NotoSansDevanagari-Regular.ttf"] = true,
|
2022-03-15 22:13:13 +00:00
|
|
|
["NotoSansBengali-Regular.ttf"] = true,
|
2019-03-03 02:50:05 +00:00
|
|
|
["NotoSansGujarati-Regular.ttf"] = true,
|
|
|
|
["NotoSansKannada-Regular.ttf"] = true,
|
|
|
|
["NotoSansMalayalam-Regular.ttf"] = true,
|
|
|
|
["NotoSansTamil-Regular.ttf"] = true,
|
|
|
|
["NotoSansTelugu-Regular.ttf"] = true,
|
|
|
|
["SakkalKitab-Bold.ttf"] = true,
|
|
|
|
["SakkalKitab-Regular.ttf"] = true,
|
|
|
|
["SongTBold.ttf"] = true,
|
|
|
|
["SongTMedium.ttf"] = true,
|
|
|
|
["STHeitiBold.ttf"] = true,
|
|
|
|
["STHeitiMedium.ttf"] = true,
|
|
|
|
["STSongBold.ttf"] = true,
|
|
|
|
["STSongMedium.ttf"] = true,
|
|
|
|
["TBGothicBold_213.ttf"] = true,
|
|
|
|
["TBGothicMed_213.ttf"] = true,
|
|
|
|
["TBMinchoBold_213.ttf"] = true,
|
|
|
|
["TBMinchoMedium_213.ttf"] = true,
|
|
|
|
["STKaiMedium.ttf"] = true,
|
2020-03-14 10:14:23 +00:00
|
|
|
["Amazon-Ember-Bold.ttf"] = false,
|
|
|
|
["Amazon-Ember-BoldItalic.ttf"] = false,
|
|
|
|
["Amazon-Ember-Heavy.ttf"] = true,
|
|
|
|
["Amazon-Ember-HeavyItalic.ttf"] = true,
|
|
|
|
["Amazon-Ember-Medium.ttf"] = true,
|
|
|
|
["Amazon-Ember-MediumItalic.ttf"] = true,
|
|
|
|
["Amazon-Ember-Regular.ttf"] = false,
|
|
|
|
["Amazon-Ember-RegularItalic.ttf"] = false,
|
|
|
|
["AmazonEmberBold-Bold.ttf"] = true,
|
|
|
|
["AmazonEmberBold-BoldItalic.ttf"] = true,
|
|
|
|
["AmazonEmberBold-Italic.ttf"] = true,
|
|
|
|
["AmazonEmberBold-Regular.ttf"] = true,
|
|
|
|
["Caecilia_LT_65_Medium.ttf"] = false,
|
2021-09-20 13:28:21 +00:00
|
|
|
["Caecilia_LT_66_Medium_Italic.ttf"] = true,
|
2020-03-14 10:14:23 +00:00
|
|
|
["Caecilia_LT_75_Bold.ttf"] = false,
|
2021-09-20 13:28:21 +00:00
|
|
|
["Caecilia_LT_76_Bold_Italic.ttf"] = true,
|
2019-03-03 02:50:05 +00:00
|
|
|
["Caecilia_LT_67_Cond_Medium.ttf"] = true,
|
|
|
|
["Caecilia_LT_68_Cond_Medium_Italic.ttf"] = true,
|
|
|
|
["Caecilia_LT_77_Cond_Bold.ttf"] = true,
|
|
|
|
["Caecilia_LT_78_Cond_Bold_Italic.ttf"] = true,
|
2020-03-14 10:14:23 +00:00
|
|
|
["Futura-Bold.ttf"] = true,
|
|
|
|
["Futura-BoldOblique.ttf"] = true,
|
2021-09-20 13:28:21 +00:00
|
|
|
["Helvetica_LT_65_Medium.ttf"] = false,
|
2019-03-03 02:50:05 +00:00
|
|
|
["Helvetica_LT_66_Medium_Italic.ttf"] = true,
|
|
|
|
["Helvetica_LT_75_Bold.ttf"] = true,
|
|
|
|
["Helvetica_LT_76_Bold_Italic.ttf"] = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
local function isInFontsBlacklist(f)
|
|
|
|
-- write test for this
|
2022-09-14 01:49:50 +00:00
|
|
|
return CanvasContext:isKindle() and kindle_fonts_blacklist[f]
|
2019-03-03 02:50:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function getExternalFontDir()
|
2022-09-14 01:49:50 +00:00
|
|
|
if CanvasContext:hasSystemFonts() then
|
2019-08-20 16:38:02 +00:00
|
|
|
return require("frontend/ui/elements/font_settings"):getPath()
|
2019-03-03 02:50:05 +00:00
|
|
|
else
|
|
|
|
return os.getenv("EXT_FONT_DIR")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-20 04:30:41 +00:00
|
|
|
-- Query FreeType/HarfBuzz about font metadata
|
|
|
|
local function collectFaceInfo(path)
|
|
|
|
local res = {}
|
|
|
|
local n = FT.getFaceCount(path)
|
|
|
|
if not n then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for i=0, n-1 do
|
2023-07-04 07:13:16 +00:00
|
|
|
local ok, ftsize = pcall(FT.newFaceSize, path, nil, i)
|
2020-10-20 04:30:41 +00:00
|
|
|
if not ok then
|
|
|
|
return nil
|
2019-03-03 02:50:05 +00:00
|
|
|
end
|
2020-10-20 04:30:41 +00:00
|
|
|
|
2020-10-24 11:23:05 +00:00
|
|
|
-- If family_name is missing, it's probably too broken to be useful
|
2023-07-04 07:13:16 +00:00
|
|
|
if ftsize.face.family_name ~= nil then
|
|
|
|
local fres = ftsize:getInfo()
|
|
|
|
local hbface = HB.hb_ft_face_create_referenced(ftsize.face)
|
2020-10-24 11:23:05 +00:00
|
|
|
fres.names = hbface:getNames()
|
|
|
|
fres.scripts, fres.langs = hbface:getCoverage()
|
|
|
|
fres.path = path
|
|
|
|
fres.index = i
|
|
|
|
table.insert(res, fres)
|
|
|
|
hbface:destroy()
|
|
|
|
end
|
2023-07-04 07:13:16 +00:00
|
|
|
ftsize:done()
|
2019-03-03 02:50:05 +00:00
|
|
|
end
|
2020-10-20 04:30:41 +00:00
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
local font_exts = {
|
|
|
|
["ttf"] = true,
|
|
|
|
["ttc"] = true,
|
|
|
|
["cff"] = true,
|
|
|
|
["otf"] = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
function FontList:_readList(dir, mark)
|
|
|
|
util.findFiles(dir, function(path, file, attr)
|
|
|
|
-- See if we're interested
|
2022-09-14 01:49:50 +00:00
|
|
|
if file:sub(1, 1) == "." then return end
|
2020-10-20 04:30:41 +00:00
|
|
|
local file_type = file:lower():match(".+%.([^.]+)") or ""
|
|
|
|
if not font_exts[file_type] then return end
|
|
|
|
|
|
|
|
-- Add it to the list
|
|
|
|
if not isInFontsBlacklist(file) then
|
|
|
|
table.insert(self.fontlist, path)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- And into cached info table
|
|
|
|
mark[path] = true
|
|
|
|
if self.fontinfo[path] and (self.fontinfo[path].change == attr.change) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local fi = collectFaceInfo(path)
|
|
|
|
if not fi then return end
|
|
|
|
fi.change = attr.change
|
|
|
|
self.fontinfo[path] = fi
|
|
|
|
mark.cache_dirty = true
|
|
|
|
end)
|
2019-03-03 02:50:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FontList:getFontList()
|
2022-09-14 01:49:50 +00:00
|
|
|
if self.fontlist[1] then return self.fontlist end
|
2020-10-20 04:30:41 +00:00
|
|
|
|
2021-01-13 10:45:30 +00:00
|
|
|
local cache = Persist:new{
|
2022-09-14 01:49:50 +00:00
|
|
|
path = self.cachedir .. "/fontinfo.dat",
|
2022-09-15 02:42:13 +00:00
|
|
|
codec = "zstd",
|
2021-01-13 10:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local t, err = cache:load()
|
|
|
|
if not t then
|
2022-09-14 01:49:50 +00:00
|
|
|
logger.info(cache.path, err, "-> initializing it")
|
2021-04-09 16:30:51 +00:00
|
|
|
|
|
|
|
-- Create new subdirectory
|
|
|
|
lfs.mkdir(self.cachedir)
|
2020-10-20 04:30:41 +00:00
|
|
|
end
|
2021-01-13 10:45:30 +00:00
|
|
|
self.fontinfo = t or {}
|
2020-10-20 04:30:41 +00:00
|
|
|
|
|
|
|
-- used for marking fonts we're seeing
|
|
|
|
local mark = { cache_dirty = false }
|
|
|
|
|
|
|
|
self:_readList(self.fontdir, mark)
|
2019-03-03 02:50:05 +00:00
|
|
|
-- multiple paths should be joined with semicolon
|
2022-09-14 01:49:50 +00:00
|
|
|
for dir in string.gmatch(getExternalFontDir() or "", "[^;]+") do
|
2020-10-20 04:30:41 +00:00
|
|
|
self:_readList(dir, mark)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- clear fonts that no longer exist
|
|
|
|
for k, _ in pairs(self.fontinfo) do
|
|
|
|
if not mark[k] then
|
|
|
|
self.fontinfo[k] = nil
|
|
|
|
mark.cache_dirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-15 02:42:13 +00:00
|
|
|
-- Update the on-disk cache if necessary
|
|
|
|
if mark.cache_dirty then
|
2021-01-13 10:45:30 +00:00
|
|
|
cache:save(self.fontinfo)
|
2019-03-03 02:50:05 +00:00
|
|
|
end
|
2020-10-20 04:30:41 +00:00
|
|
|
|
|
|
|
local names = self.fontnames
|
2022-09-14 01:49:50 +00:00
|
|
|
for _, coll in pairs(self.fontinfo) do
|
|
|
|
for _, v in ipairs(coll) do
|
2020-10-20 04:30:41 +00:00
|
|
|
local nlist = names[v.name] or {}
|
|
|
|
assert(v.name)
|
|
|
|
if #nlist == 0 then
|
2022-09-15 02:42:13 +00:00
|
|
|
logger.dbg("FontList registered:", v.name)
|
2020-10-20 04:30:41 +00:00
|
|
|
end
|
|
|
|
names[v.name] = nlist
|
|
|
|
table.insert(nlist, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-24 09:11:30 +00:00
|
|
|
table.sort(self.fontlist)
|
|
|
|
return self.fontlist
|
2019-03-03 02:50:05 +00:00
|
|
|
end
|
|
|
|
|
2022-09-15 02:42:13 +00:00
|
|
|
function FontList:dumpFontList()
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
local serpent = require("ffi/serpent")
|
2022-09-15 02:42:13 +00:00
|
|
|
|
|
|
|
-- FontInfo
|
|
|
|
local path = self.cachedir .. "/fontinfo_dump.lua"
|
|
|
|
local f = io.open(path, "w")
|
|
|
|
if f ~= nil then
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
f:write(serpent.block(self.fontinfo, { indent = " ", comment = false, nocode = true }))
|
2022-09-15 02:42:13 +00:00
|
|
|
f:close()
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- FontList
|
|
|
|
path = self.cachedir .. "/fontlist_dump.lua"
|
|
|
|
f = io.open(path, "w")
|
|
|
|
if f ~= nil then
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
f:write(serpent.block(self.fontlist, { indent = " ", comment = false, nocode = true }))
|
2022-09-15 02:42:13 +00:00
|
|
|
f:close()
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local _ = require("gettext")
|
|
|
|
local T = require("ffi/util").template
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Fontlist data has been dumped in:\n%1"), self.cachedir)
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2020-10-20 04:30:41 +00:00
|
|
|
-- Try to determine the localized font name
|
|
|
|
function FontList:getLocalizedFontName(file, index)
|
|
|
|
local lang = G_reader_settings:readSetting("language")
|
|
|
|
if not lang then return end
|
|
|
|
lang = lang:lower():gsub("_","-")
|
|
|
|
local altname = self.fontinfo[file]
|
|
|
|
altname = altname and altname[index+1]
|
|
|
|
altname = altname and altname.names and (altname.names[lang] or altname.names[lang:match("%w+")])
|
|
|
|
altname = altname and (altname[tonumber(HB.HB_OT_NAME_ID_FULL_NAME)] or altname[tonumber(HB.HB_OT_NAME_ID_FONT_FAMILY)])
|
|
|
|
if not altname then return end -- ensure nil
|
|
|
|
return altname
|
|
|
|
end
|
|
|
|
|
2021-12-16 20:31:22 +00:00
|
|
|
function FontList:getFontArgFunc()
|
2022-09-27 23:10:50 +00:00
|
|
|
local cre = require("document/credocument"):engineInit()
|
2021-12-16 20:31:22 +00:00
|
|
|
local toggle = {}
|
|
|
|
local face_list = cre.getFontFaces()
|
2022-09-14 01:49:50 +00:00
|
|
|
for _, v in ipairs(face_list) do
|
2021-12-16 20:31:22 +00:00
|
|
|
table.insert(toggle, FontList:getLocalizedFontName(cre.getFontFaceFilenameAndFaceIndex(v)) or v)
|
|
|
|
end
|
|
|
|
return face_list, toggle
|
|
|
|
end
|
|
|
|
|
2019-03-03 02:50:05 +00:00
|
|
|
return FontList
|