2016-02-16 00:14:15 +00:00
|
|
|
--[[--
|
|
|
|
Font module.
|
|
|
|
]]
|
|
|
|
|
2015-06-06 09:21:39 +00:00
|
|
|
local Device = require("device")
|
2017-04-29 08:38:09 +00:00
|
|
|
local Freetype = require("ffi/freetype")
|
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2017-04-29 08:38:09 +00:00
|
|
|
local Screen = Device.screen
|
2012-03-10 08:41:23 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local Font = {
|
2014-03-13 13:52:43 +00:00
|
|
|
fontmap = {
|
|
|
|
-- default font for menu contents
|
2014-07-08 00:11:17 +00:00
|
|
|
cfont = "noto/NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
-- default font for title
|
|
|
|
--tfont = "NimbusSanL-BoldItal.cff",
|
2014-07-08 00:11:17 +00:00
|
|
|
tfont = "noto/NotoSans-Bold.ttf",
|
2017-04-29 08:38:09 +00:00
|
|
|
smalltfont = "noto/NotoSans-Bold.ttf",
|
|
|
|
x_smalltfont = "noto/NotoSans-Bold.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
-- default font for footer
|
2014-07-08 00:11:17 +00:00
|
|
|
ffont = "noto/NotoSans-Regular.ttf",
|
2017-04-29 08:38:09 +00:00
|
|
|
smallffont = "noto/NotoSans-Regular.ttf",
|
|
|
|
largeffont = "noto/NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- default font for reading position info
|
2014-07-08 00:11:17 +00:00
|
|
|
rifont = "noto/NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- default font for pagination display
|
2014-07-08 00:11:17 +00:00
|
|
|
pgfont = "noto/NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- selectmenu: font for item shortcut
|
|
|
|
scfont = "droid/DroidSansMono.ttf",
|
|
|
|
|
|
|
|
-- help page: font for displaying keys
|
|
|
|
hpkfont = "droid/DroidSansMono.ttf",
|
|
|
|
-- font for displaying help messages
|
2014-07-08 00:11:17 +00:00
|
|
|
hfont = "noto/NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- font for displaying input content
|
|
|
|
-- we have to use mono here for better distance controlling
|
|
|
|
infont = "droid/DroidSansMono.ttf",
|
2018-10-08 16:58:43 +00:00
|
|
|
-- small mono font for displaying code
|
|
|
|
smallinfont = "droid/DroidSansMono.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- font for info messages
|
2014-07-08 00:11:17 +00:00
|
|
|
infofont = "noto/NotoSans-Regular.ttf",
|
2017-04-29 08:38:09 +00:00
|
|
|
|
|
|
|
-- small font for info messages
|
|
|
|
smallinfofont = "noto/NotoSans-Regular.ttf",
|
|
|
|
-- small bold font for info messages
|
|
|
|
smallinfofontbold = "noto/NotoSans-Bold.ttf",
|
|
|
|
-- extra small font for info messages
|
|
|
|
x_smallinfofont = "noto/NotoSans-Regular.ttf",
|
|
|
|
-- extra extra small font for info messages
|
|
|
|
xx_smallinfofont = "noto/NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2016-02-15 23:00:38 +00:00
|
|
|
sizemap = {
|
|
|
|
cfont = 24,
|
|
|
|
tfont = 26,
|
2017-04-29 08:38:09 +00:00
|
|
|
smalltfont = 24,
|
|
|
|
x_smalltfont = 22,
|
2016-02-15 23:00:38 +00:00
|
|
|
ffont = 20,
|
2017-04-29 08:38:09 +00:00
|
|
|
smallffont = 15,
|
|
|
|
largeffont = 25,
|
2016-02-15 23:00:38 +00:00
|
|
|
pgfont = 20,
|
|
|
|
scfont = 20,
|
|
|
|
rifont = 16,
|
|
|
|
hpkfont = 20,
|
|
|
|
hfont = 24,
|
|
|
|
infont = 22,
|
2018-10-08 16:58:43 +00:00
|
|
|
smallinfont = 16,
|
2016-02-15 23:00:38 +00:00
|
|
|
infofont = 24,
|
2017-04-29 08:38:09 +00:00
|
|
|
smallinfofont = 22,
|
|
|
|
smallinfofontbold = 22,
|
|
|
|
x_smallinfofont = 20,
|
|
|
|
xx_smallinfofont = 18,
|
2016-02-15 23:00:38 +00:00
|
|
|
},
|
2014-03-13 13:52:43 +00:00
|
|
|
fallbacks = {
|
2018-05-16 14:28:06 +00:00
|
|
|
[1] = "noto/NotoSans-Regular.ttf",
|
|
|
|
[2] = "noto/NotoSansCJKsc-Regular.otf",
|
2015-09-03 14:45:12 +00:00
|
|
|
[3] = "freefont/FreeSans.ttf",
|
2018-05-16 14:28:06 +00:00
|
|
|
[4] = "freefont/FreeSerif.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
|
|
|
|
2015-01-25 08:52:01 +00:00
|
|
|
fontdir = "./fonts",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- face table
|
|
|
|
faces = {},
|
2012-03-10 08:41:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 08:38:09 +00:00
|
|
|
--- Gets font face object.
|
|
|
|
-- @string font
|
|
|
|
-- @int size optional size
|
|
|
|
-- @treturn table @{FontFaceObj}
|
2012-04-09 17:04:26 +00:00
|
|
|
function Font:getFace(font, size)
|
2015-09-09 03:10:17 +00:00
|
|
|
-- default to content font
|
|
|
|
if not font then font = self.cfont end
|
2014-03-13 13:52:43 +00:00
|
|
|
|
2016-02-15 23:00:38 +00:00
|
|
|
if not size then size = self.sizemap[font] end
|
2014-08-20 01:39:50 +00:00
|
|
|
-- original size before scaling by screen DPI
|
|
|
|
local orig_size = size
|
2016-02-16 02:08:04 +00:00
|
|
|
size = Screen:scaleBySize(size)
|
2014-03-13 13:52:43 +00:00
|
|
|
|
2015-09-09 03:10:17 +00:00
|
|
|
local hash = font..size
|
|
|
|
local face_obj = self.faces[hash]
|
2014-03-13 13:52:43 +00:00
|
|
|
-- build face if not found
|
2015-09-09 03:10:17 +00:00
|
|
|
if not face_obj then
|
2014-03-13 13:52:43 +00:00
|
|
|
local realname = self.fontmap[font]
|
|
|
|
if not realname then
|
|
|
|
realname = font
|
|
|
|
end
|
|
|
|
realname = self.fontdir.."/"..realname
|
2016-02-16 02:08:04 +00:00
|
|
|
local ok, face = pcall(Freetype.newFace, realname, size)
|
2014-03-13 13:52:43 +00:00
|
|
|
if not ok then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.warn("#! Font ", font, " (", realname, ") not supported: ", face)
|
2014-03-13 13:52:43 +00:00
|
|
|
return nil
|
|
|
|
end
|
2016-02-16 00:14:15 +00:00
|
|
|
--- Freetype font face wrapper object
|
|
|
|
-- @table FontFaceObj
|
2019-01-15 19:38:25 +00:00
|
|
|
-- @field orig_font font name requested
|
2016-02-16 00:14:15 +00:00
|
|
|
-- @field size size of the font face (after scaled by screen size)
|
|
|
|
-- @field orig_size raw size of the font face (before scale)
|
|
|
|
-- @field ftface font face object from freetype
|
|
|
|
-- @field hash hash key for this font face
|
2015-09-09 03:10:17 +00:00
|
|
|
face_obj = {
|
2019-01-15 19:38:25 +00:00
|
|
|
orig_font = font,
|
2015-09-09 03:10:17 +00:00
|
|
|
size = size,
|
|
|
|
orig_size = orig_size,
|
|
|
|
ftface = face,
|
|
|
|
hash = hash
|
|
|
|
}
|
|
|
|
self.faces[hash] = face_obj
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2015-09-09 03:10:17 +00:00
|
|
|
return face_obj
|
2012-04-09 17:04:26 +00:00
|
|
|
end
|
|
|
|
|
2015-10-22 15:15:41 +00:00
|
|
|
--[[
|
2018-07-02 13:58:27 +00:00
|
|
|
These non-LGC Kindle system fonts fail CRe's moronic header check.
|
2015-10-22 15:15:41 +00:00
|
|
|
--]]
|
|
|
|
local kindle_fonts_blacklist = {
|
2018-07-02 13:58:27 +00:00
|
|
|
["DiwanMuna-Bold.ttf"] = true,
|
|
|
|
["DiwanMuna-Regular.ttf"] = true,
|
2015-10-22 15:15:41 +00:00
|
|
|
["HYGothicBold.ttf"] = true,
|
|
|
|
["HYGothicMedium.ttf"] = true,
|
|
|
|
["HYMyeongJoBold.ttf"] = true,
|
|
|
|
["HYMyeongJoMedium.ttf"] = true,
|
2018-07-02 13:58:27 +00:00
|
|
|
["KindleBlackboxBoldItalic.ttf"] = true,
|
|
|
|
["KindleBlackboxBold.ttf"] = true,
|
|
|
|
["KindleBlackboxItalic.ttf"] = true,
|
|
|
|
["KindleBlackboxRegular.ttf"] = true,
|
|
|
|
["Kindle_MonospacedSymbol.ttf"] = true,
|
|
|
|
["Kindle_Symbol.ttf"] = true,
|
|
|
|
["MTChineseSurrogates.ttf"] = true,
|
2015-10-22 15:15:41 +00:00
|
|
|
["MYingHeiTBold.ttf"] = true,
|
|
|
|
["MYingHeiTMedium.ttf"] = true,
|
2018-07-02 13:58:27 +00:00
|
|
|
["NotoNaskhArabicUI-Bold.ttf"] = true,
|
|
|
|
["NotoNaskhArabicUI-Regular.ttf"] = true,
|
|
|
|
["NotoNaskh-Bold.ttf"] = true,
|
|
|
|
["NotoNaskh-Regular.ttf"] = true,
|
|
|
|
["NotoSansBengali-Regular.ttf"] = true,
|
|
|
|
["NotoSansDevanagari-Regular.ttf"] = true,
|
|
|
|
["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,
|
2015-10-22 15:15:41 +00:00
|
|
|
["SongTBold.ttf"] = true,
|
|
|
|
["SongTMedium.ttf"] = true,
|
2018-07-02 13:58:27 +00:00
|
|
|
["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,
|
|
|
|
["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,
|
|
|
|
["Helvetica_LT_65_Medium.ttf"] = true,
|
|
|
|
["Helvetica_LT_66_Medium_Italic.ttf"] = true,
|
|
|
|
["Helvetica_LT_75_Bold.ttf"] = true,
|
|
|
|
["Helvetica_LT_76_Bold_Italic.ttf"] = true,
|
2015-10-22 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local function isInFontsBlacklist(f)
|
2015-10-22 13:19:34 +00:00
|
|
|
if Device:isKindle() then
|
2015-10-22 15:15:41 +00:00
|
|
|
return kindle_fonts_blacklist[f]
|
2015-10-22 09:02:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-25 08:52:01 +00:00
|
|
|
function Font:_readList(target, dir)
|
2015-01-27 18:17:54 +00:00
|
|
|
-- lfs.dir non-exsitent directory will give error, weird!
|
|
|
|
local ok, iter, dir_obj = pcall(lfs.dir, dir)
|
|
|
|
if not ok then return end
|
|
|
|
for f in iter, dir_obj do
|
2014-03-13 13:52:43 +00:00
|
|
|
if lfs.attributes(dir.."/"..f, "mode") == "directory" and f ~= "." and f ~= ".." then
|
2015-01-25 08:52:01 +00:00
|
|
|
self:_readList(target, dir.."/"..f)
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
2018-07-02 13:58:27 +00:00
|
|
|
if string.sub(f, 1, 1) ~= "." then
|
|
|
|
local file_type = string.lower(string.match(f, ".+%.([^.]+)") or "")
|
|
|
|
if file_type == "ttf" or file_type == "ttc"
|
|
|
|
or file_type == "cff" or file_type == "otf" then
|
|
|
|
if not isInFontsBlacklist(f) then
|
|
|
|
table.insert(target, dir.."/"..f)
|
|
|
|
end
|
2015-10-22 09:02:21 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-04-09 17:04:26 +00:00
|
|
|
end
|
|
|
|
|
2015-06-06 09:21:39 +00:00
|
|
|
function Font:_getExternalFontDir()
|
|
|
|
if Device:isAndroid() then
|
|
|
|
return ANDROID_FONT_DIR
|
|
|
|
else
|
|
|
|
return os.getenv("EXT_FONT_DIR")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-09 17:04:26 +00:00
|
|
|
function Font:getFontList()
|
2015-01-25 08:52:01 +00:00
|
|
|
local fontlist = {}
|
|
|
|
self:_readList(fontlist, self.fontdir)
|
2015-06-06 09:21:39 +00:00
|
|
|
-- multiple paths should be joined with semicolon
|
|
|
|
for dir in string.gmatch(self:_getExternalFontDir() or "", "([^;]+)") do
|
2015-01-25 08:52:01 +00:00
|
|
|
self:_readList(fontlist, dir)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
table.sort(fontlist)
|
|
|
|
return fontlist
|
2012-03-10 08:41:23 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return Font
|