forked from koreader/kindlepdfviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.lua
91 lines (76 loc) · 2.17 KB
/
font.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Font = {
fontmap = {
-- default font for menu contents
cfont = "droid/DroidSans.ttf",
-- default font for title
tfont = "NimbusSanL-BoldItal.cff",
-- default font for footer
ffont = "droid/DroidSans.ttf",
-- default font for reading position info
rifont = "droid/DroidSans.ttf",
-- default font for pagination display
pgfont = "droid/DroidSans.ttf",
-- 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 = "droid/DroidSans.ttf",
-- font for displaying input content
-- we have to use mono here for better distance controlling
infont = "droid/DroidSansMono.ttf",
-- font for info messages
infofont = "droid/DroidSans.ttf",
},
fontdir = os.getenv("FONTDIR") or "./fonts",
-- face table
faces = {},
}
function Font:getFace(font, size)
if not font then
-- default to content font
font = self.cfont
end
local face = self.faces[font..size]
-- build face if not found
if not face then
local realname = self.fontmap[font]
if not realname then
realname = font
end
realname = self.fontdir.."/"..realname
ok, face = pcall(freetype.newFace, realname, size)
if not ok then
print("#! Font "..font.." ("..realname..") not supported: "..face)
return nil
end
self.faces[font..size] = face
--print("getFace, found: "..realname.." size:"..size)
end
return { size = size, ftface = face, hash = font..size }
end
function Font:_readList(target, dir, effective_dir)
for f in lfs.dir(dir) do
if lfs.attributes(dir.."/"..f, "mode") == "directory" and f ~= "." and f ~= ".." then
self:_readList(target, dir.."/"..f, effective_dir..f.."/")
else
local file_type = string.lower(string.match(f, ".+%.([^.]+)") or "")
if file_type == "ttf" or file_type == "cff" or file_type == "otf" then
table.insert(target, effective_dir..f)
end
end
end
end
function Font:getFontList()
fontlist = {}
self:_readList(fontlist, self.fontdir, "")
table.sort(fontlist)
return fontlist
end
function Font:update()
for _k, _v in ipairs(self.faces) do
_v:done()
end
self.faces = {}
clearGlyphCache()
end