2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/ui/font.lua

143 lines
4.0 KiB
Lua
Raw Normal View History

2014-08-14 11:49:42 +00:00
local lfs = require("libs/libkoreader-lfs")
local Freetype = require("ffi/freetype")
local Screen = require("device").screen
local Device = require("device")
2013-10-22 18:51:29 +00:00
local DEBUG = require("dbg")
2013-10-18 20:38:07 +00:00
local Font = {
2014-03-13 13:52:43 +00:00
fontmap = {
-- default font for menu contents
cfont = "noto/NotoSans-Regular.ttf",
2014-03-13 13:52:43 +00:00
-- default font for title
--tfont = "NimbusSanL-BoldItal.cff",
tfont = "noto/NotoSans-Bold.ttf",
2014-03-13 13:52:43 +00:00
-- default font for footer
ffont = "noto/NotoSans-Regular.ttf",
2014-03-13 13:52:43 +00:00
-- default font for reading position info
rifont = "noto/NotoSans-Regular.ttf",
2014-03-13 13:52:43 +00:00
-- default font for pagination display
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
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",
-- font for info messages
infofont = "noto/NotoSans-Regular.ttf",
2014-03-13 13:52:43 +00:00
},
fallbacks = {
[1] = "noto/NotoSansCJK-Regular.ttf",
[2] = "noto/NotoSans-Regular.ttf",
[3] = "freefont/FreeSans.ttf",
2014-03-13 13:52:43 +00:00
},
fontdir = "./fonts",
2014-03-13 13:52:43 +00:00
-- face table
faces = {},
}
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
-- original size before scaling by screen DPI
local orig_size = size
local 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
2014-08-14 11:49:42 +00:00
ok, face = pcall(Freetype.newFace, realname, size)
2014-03-13 13:52:43 +00:00
if not ok then
DEBUG("#! Font "..font.." ("..realname..") not supported: "..face)
return nil
end
2015-09-09 03:10:17 +00:00
face_obj = {
size = size,
orig_size = orig_size,
ftface = face,
hash = hash
}
self.faces[hash] = face_obj
-- DEBUG("getFace, found: "..realname.." size:"..size)
2014-03-13 13:52:43 +00:00
end
2015-09-09 03:10:17 +00:00
return face_obj
end
--[[
These fonts from Kindle system cannot be loaded by Freetype.
--]]
local kindle_fonts_blacklist = {
["HYGothicBold.ttf"] = true,
["HYGothicMedium.ttf"] = true,
["HYMyeongJoBold.ttf"] = true,
["HYMyeongJoMedium.ttf"] = true,
["MYingHeiTBold.ttf"] = true,
["MYingHeiTMedium.ttf"] = true,
["SongTBold.ttf"] = true,
["SongTMedium.ttf"] = true,
}
local function isInFontsBlacklist(f)
2015-10-22 13:19:34 +00:00
if Device:isKindle() then
return kindle_fonts_blacklist[f]
end
end
function Font:_readList(target, dir)
-- 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
self:_readList(target, dir.."/"..f)
2014-03-13 13:52:43 +00:00
else
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
2014-03-13 13:52:43 +00:00
end
end
end
end
function Font:_getExternalFontDir()
if Device:isAndroid() then
return ANDROID_FONT_DIR
else
return os.getenv("EXT_FONT_DIR")
end
end
function Font:getFontList()
local fontlist = {}
self:_readList(fontlist, self.fontdir)
-- multiple paths should be joined with semicolon
for dir in string.gmatch(self:_getExternalFontDir() or "", "([^;]+)") do
self:_readList(fontlist, dir)
end
2014-03-13 13:52:43 +00:00
table.sort(fontlist)
return fontlist
end
2013-10-18 20:38:07 +00:00
return Font